1 //===-- CFCMutableArray.cpp -------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "CFCMutableArray.h"
11 #include "CFCString.h"
12
13 //----------------------------------------------------------------------
14 // CFCString constructor
15 //----------------------------------------------------------------------
CFCMutableArray(CFMutableArrayRef s)16 CFCMutableArray::CFCMutableArray(CFMutableArrayRef s) :
17 CFCReleaser<CFMutableArrayRef> (s)
18 {
19 }
20
21 //----------------------------------------------------------------------
22 // CFCMutableArray copy constructor
23 //----------------------------------------------------------------------
CFCMutableArray(const CFCMutableArray & rhs)24 CFCMutableArray::CFCMutableArray(const CFCMutableArray& rhs) :
25 CFCReleaser<CFMutableArrayRef> (rhs) // NOTE: this won't make a copy of the array, just add a new reference to it
26 {
27 }
28
29 //----------------------------------------------------------------------
30 // CFCMutableArray copy constructor
31 //----------------------------------------------------------------------
32 CFCMutableArray&
operator =(const CFCMutableArray & rhs)33 CFCMutableArray::operator=(const CFCMutableArray& rhs)
34 {
35 if (this != &rhs)
36 *this = rhs; // NOTE: this operator won't make a copy of the array, just add a new reference to it
37 return *this;
38 }
39
40 //----------------------------------------------------------------------
41 // Destructor
42 //----------------------------------------------------------------------
~CFCMutableArray()43 CFCMutableArray::~CFCMutableArray()
44 {
45 }
46
47
48 CFIndex
GetCount() const49 CFCMutableArray::GetCount() const
50 {
51 CFMutableArrayRef array = get();
52 if (array)
53 return ::CFArrayGetCount (array);
54 return 0;
55 }
56
57 CFIndex
GetCountOfValue(CFRange range,const void * value) const58 CFCMutableArray::GetCountOfValue(CFRange range, const void *value) const
59 {
60 CFMutableArrayRef array = get();
61 if (array)
62 return ::CFArrayGetCountOfValue (array, range, value);
63 return 0;
64 }
65
66 CFIndex
GetCountOfValue(const void * value) const67 CFCMutableArray::GetCountOfValue(const void *value) const
68 {
69 CFMutableArrayRef array = get();
70 if (array)
71 return ::CFArrayGetCountOfValue (array, CFRangeMake(0, GetCount()), value);
72 return 0;
73 }
74
75 const void *
GetValueAtIndex(CFIndex idx) const76 CFCMutableArray::GetValueAtIndex(CFIndex idx) const
77 {
78 CFMutableArrayRef array = get();
79 if (array)
80 {
81 const CFIndex num_array_items = ::CFArrayGetCount (array);
82 if (0 <= idx && idx < num_array_items)
83 {
84 return ::CFArrayGetValueAtIndex (array, idx);
85 }
86 }
87 return NULL;
88 }
89
90 bool
SetValueAtIndex(CFIndex idx,const void * value)91 CFCMutableArray::SetValueAtIndex(CFIndex idx, const void *value)
92 {
93 CFMutableArrayRef array = get();
94 if (array != NULL)
95 {
96 const CFIndex num_array_items = ::CFArrayGetCount (array);
97 if (0 <= idx && idx < num_array_items)
98 {
99 ::CFArraySetValueAtIndex (array, idx, value);
100 return true;
101 }
102 }
103 return false;
104 }
105
106
107 bool
AppendValue(const void * value,bool can_create)108 CFCMutableArray::AppendValue(const void *value, bool can_create)
109 {
110 CFMutableArrayRef array = get();
111 if (array == NULL)
112 {
113 if (can_create == false)
114 return false;
115 array = ::CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
116 reset ( array );
117 }
118 if (array != NULL)
119 {
120 ::CFArrayAppendValue(array, value);
121 return true;
122 }
123 return false;
124 }
125
126
127 bool
AppendCStringAsCFString(const char * s,CFStringEncoding encoding,bool can_create)128 CFCMutableArray::AppendCStringAsCFString (const char *s, CFStringEncoding encoding, bool can_create)
129 {
130 CFMutableArrayRef array = get();
131 if (array == NULL)
132 {
133 if (can_create == false)
134 return false;
135 array = ::CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
136 reset ( array );
137 }
138 if (array != NULL)
139 {
140 CFCString cf_str (s, encoding);
141 ::CFArrayAppendValue (array, cf_str.get());
142 return true;
143 }
144 return false;
145 }
146
147 bool
AppendFileSystemRepresentationAsCFString(const char * s,bool can_create)148 CFCMutableArray::AppendFileSystemRepresentationAsCFString (const char *s, bool can_create)
149 {
150 CFMutableArrayRef array = get();
151 if (array == NULL)
152 {
153 if (can_create == false)
154 return false;
155 array = ::CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
156 reset ( array );
157 }
158 if (array != NULL)
159 {
160 CFCString cf_path;
161 cf_path.SetFileSystemRepresentation(s);
162 ::CFArrayAppendValue (array, cf_path.get());
163 return true;
164 }
165 return false;
166 }
167