1 //===-- CFCMutableDictionary.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 "CFCMutableDictionary.h"
11 #include "CFCString.h"
12 //----------------------------------------------------------------------
13 // CFCString constructor
14 //----------------------------------------------------------------------
15 CFCMutableDictionary::CFCMutableDictionary(CFMutableDictionaryRef s)
16     : CFCReleaser<CFMutableDictionaryRef>(s) {}
17 
18 //----------------------------------------------------------------------
19 // CFCMutableDictionary copy constructor
20 //----------------------------------------------------------------------
21 CFCMutableDictionary::CFCMutableDictionary(const CFCMutableDictionary &rhs)
22     : CFCReleaser<CFMutableDictionaryRef>(rhs) {}
23 
24 //----------------------------------------------------------------------
25 // CFCMutableDictionary copy constructor
26 //----------------------------------------------------------------------
27 const CFCMutableDictionary &CFCMutableDictionary::
28 operator=(const CFCMutableDictionary &rhs) {
29   if (this != &rhs)
30     *this = rhs;
31   return *this;
32 }
33 
34 //----------------------------------------------------------------------
35 // Destructor
36 //----------------------------------------------------------------------
37 CFCMutableDictionary::~CFCMutableDictionary() {}
38 
39 CFIndex CFCMutableDictionary::GetCount() const {
40   CFMutableDictionaryRef dict = get();
41   if (dict)
42     return ::CFDictionaryGetCount(dict);
43   return 0;
44 }
45 
46 CFIndex CFCMutableDictionary::GetCountOfKey(const void *key) const
47 
48 {
49   CFMutableDictionaryRef dict = get();
50   if (dict)
51     return ::CFDictionaryGetCountOfKey(dict, key);
52   return 0;
53 }
54 
55 CFIndex CFCMutableDictionary::GetCountOfValue(const void *value) const
56 
57 {
58   CFMutableDictionaryRef dict = get();
59   if (dict)
60     return ::CFDictionaryGetCountOfValue(dict, value);
61   return 0;
62 }
63 
64 void CFCMutableDictionary::GetKeysAndValues(const void **keys,
65                                             const void **values) const {
66   CFMutableDictionaryRef dict = get();
67   if (dict)
68     ::CFDictionaryGetKeysAndValues(dict, keys, values);
69 }
70 
71 const void *CFCMutableDictionary::GetValue(const void *key) const
72 
73 {
74   CFMutableDictionaryRef dict = get();
75   if (dict)
76     return ::CFDictionaryGetValue(dict, key);
77   return NULL;
78 }
79 
80 Boolean
81 CFCMutableDictionary::GetValueIfPresent(const void *key,
82                                         const void **value_handle) const {
83   CFMutableDictionaryRef dict = get();
84   if (dict)
85     return ::CFDictionaryGetValueIfPresent(dict, key, value_handle);
86   return false;
87 }
88 
89 CFMutableDictionaryRef CFCMutableDictionary::Dictionary(bool can_create) {
90   CFMutableDictionaryRef dict = get();
91   if (can_create && dict == NULL) {
92     dict = ::CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
93                                        &kCFTypeDictionaryKeyCallBacks,
94                                        &kCFTypeDictionaryValueCallBacks);
95     reset(dict);
96   }
97   return dict;
98 }
99 
100 bool CFCMutableDictionary::AddValue(CFStringRef key, const void *value,
101                                     bool can_create) {
102   CFMutableDictionaryRef dict = Dictionary(can_create);
103   if (dict != NULL) {
104     // Let the dictionary own the CFNumber
105     ::CFDictionaryAddValue(dict, key, value);
106     return true;
107   }
108   return false;
109 }
110 
111 bool CFCMutableDictionary::SetValue(CFStringRef key, const void *value,
112                                     bool can_create) {
113   CFMutableDictionaryRef dict = Dictionary(can_create);
114   if (dict != NULL) {
115     // Let the dictionary own the CFNumber
116     ::CFDictionarySetValue(dict, key, value);
117     return true;
118   }
119   return false;
120 }
121 
122 bool CFCMutableDictionary::AddValueSInt8(CFStringRef key, int8_t value,
123                                          bool can_create) {
124   CFMutableDictionaryRef dict = Dictionary(can_create);
125   if (dict != NULL) {
126     CFCReleaser<CFNumberRef> cf_number(
127         ::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt8Type, &value));
128     if (cf_number.get()) {
129       // Let the dictionary own the CFNumber
130       ::CFDictionaryAddValue(dict, key, cf_number.get());
131       return true;
132     }
133   }
134   return false;
135 }
136 
137 bool CFCMutableDictionary::SetValueSInt8(CFStringRef key, int8_t value,
138                                          bool can_create) {
139   CFMutableDictionaryRef dict = Dictionary(can_create);
140   if (dict != NULL) {
141     CFCReleaser<CFNumberRef> cf_number(
142         ::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt8Type, &value));
143     if (cf_number.get()) {
144       // Let the dictionary own the CFNumber
145       ::CFDictionarySetValue(dict, key, cf_number.get());
146       return true;
147     }
148   }
149   return false;
150 }
151 
152 bool CFCMutableDictionary::AddValueSInt16(CFStringRef key, int16_t value,
153                                           bool can_create) {
154   CFMutableDictionaryRef dict = Dictionary(can_create);
155   if (dict != NULL) {
156     CFCReleaser<CFNumberRef> cf_number(
157         ::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt16Type, &value));
158     if (cf_number.get()) {
159       // Let the dictionary own the CFNumber
160       ::CFDictionaryAddValue(dict, key, cf_number.get());
161       return true;
162     }
163   }
164   return false;
165 }
166 
167 bool CFCMutableDictionary::SetValueSInt16(CFStringRef key, int16_t value,
168                                           bool can_create) {
169   CFMutableDictionaryRef dict = Dictionary(can_create);
170   if (dict != NULL) {
171     CFCReleaser<CFNumberRef> cf_number(
172         ::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt16Type, &value));
173     if (cf_number.get()) {
174       // Let the dictionary own the CFNumber
175       ::CFDictionarySetValue(dict, key, cf_number.get());
176       return true;
177     }
178   }
179   return false;
180 }
181 
182 bool CFCMutableDictionary::AddValueSInt32(CFStringRef key, int32_t value,
183                                           bool can_create) {
184   CFMutableDictionaryRef dict = Dictionary(can_create);
185   if (dict != NULL) {
186     CFCReleaser<CFNumberRef> cf_number(
187         ::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &value));
188     if (cf_number.get()) {
189       // Let the dictionary own the CFNumber
190       ::CFDictionaryAddValue(dict, key, cf_number.get());
191       return true;
192     }
193   }
194   return false;
195 }
196 
197 bool CFCMutableDictionary::SetValueSInt32(CFStringRef key, int32_t value,
198                                           bool can_create) {
199   CFMutableDictionaryRef dict = Dictionary(can_create);
200   if (dict != NULL) {
201     CFCReleaser<CFNumberRef> cf_number(
202         ::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &value));
203     if (cf_number.get()) {
204       // Let the dictionary own the CFNumber
205       ::CFDictionarySetValue(dict, key, cf_number.get());
206       return true;
207     }
208   }
209   return false;
210 }
211 
212 bool CFCMutableDictionary::AddValueSInt64(CFStringRef key, int64_t value,
213                                           bool can_create) {
214   CFMutableDictionaryRef dict = Dictionary(can_create);
215   if (dict != NULL) {
216     CFCReleaser<CFNumberRef> cf_number(
217         ::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &value));
218     if (cf_number.get()) {
219       // Let the dictionary own the CFNumber
220       ::CFDictionaryAddValue(dict, key, cf_number.get());
221       return true;
222     }
223   }
224   return false;
225 }
226 
227 bool CFCMutableDictionary::SetValueSInt64(CFStringRef key, int64_t value,
228                                           bool can_create) {
229   CFMutableDictionaryRef dict = Dictionary(can_create);
230   if (dict != NULL) {
231     CFCReleaser<CFNumberRef> cf_number(
232         ::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &value));
233     if (cf_number.get()) {
234       // Let the dictionary own the CFNumber
235       ::CFDictionarySetValue(dict, key, cf_number.get());
236       return true;
237     }
238   }
239   return false;
240 }
241 
242 bool CFCMutableDictionary::AddValueUInt8(CFStringRef key, uint8_t value,
243                                          bool can_create) {
244   CFMutableDictionaryRef dict = Dictionary(can_create);
245   if (dict != NULL) {
246     // Have to promote to the next size type so things don't appear negative of
247     // the MSBit is set...
248     int16_t sval = value;
249     CFCReleaser<CFNumberRef> cf_number(
250         ::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt16Type, &sval));
251     if (cf_number.get()) {
252       // Let the dictionary own the CFNumber
253       ::CFDictionaryAddValue(dict, key, cf_number.get());
254       return true;
255     }
256   }
257   return false;
258 }
259 
260 bool CFCMutableDictionary::SetValueUInt8(CFStringRef key, uint8_t value,
261                                          bool can_create) {
262   CFMutableDictionaryRef dict = Dictionary(can_create);
263   if (dict != NULL) {
264     // Have to promote to the next size type so things don't appear negative of
265     // the MSBit is set...
266     int16_t sval = value;
267     CFCReleaser<CFNumberRef> cf_number(
268         ::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt16Type, &sval));
269     if (cf_number.get()) {
270       // Let the dictionary own the CFNumber
271       ::CFDictionarySetValue(dict, key, cf_number.get());
272       return true;
273     }
274   }
275   return false;
276 }
277 
278 bool CFCMutableDictionary::AddValueUInt16(CFStringRef key, uint16_t value,
279                                           bool can_create) {
280   CFMutableDictionaryRef dict = Dictionary(can_create);
281   if (dict != NULL) {
282     // Have to promote to the next size type so things don't appear negative of
283     // the MSBit is set...
284     int32_t sval = value;
285     CFCReleaser<CFNumberRef> cf_number(
286         ::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &sval));
287     if (cf_number.get()) {
288       // Let the dictionary own the CFNumber
289       ::CFDictionaryAddValue(dict, key, cf_number.get());
290       return true;
291     }
292   }
293   return false;
294 }
295 
296 bool CFCMutableDictionary::SetValueUInt16(CFStringRef key, uint16_t value,
297                                           bool can_create) {
298   CFMutableDictionaryRef dict = Dictionary(can_create);
299   if (dict != NULL) {
300     // Have to promote to the next size type so things don't appear negative of
301     // the MSBit is set...
302     int32_t sval = value;
303     CFCReleaser<CFNumberRef> cf_number(
304         ::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &sval));
305     if (cf_number.get()) {
306       // Let the dictionary own the CFNumber
307       ::CFDictionarySetValue(dict, key, cf_number.get());
308       return true;
309     }
310   }
311   return false;
312 }
313 
314 bool CFCMutableDictionary::AddValueUInt32(CFStringRef key, uint32_t value,
315                                           bool can_create) {
316   CFMutableDictionaryRef dict = Dictionary(can_create);
317   if (dict != NULL) {
318     // Have to promote to the next size type so things don't appear negative of
319     // the MSBit is set...
320     int64_t sval = value;
321     CFCReleaser<CFNumberRef> cf_number(
322         ::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &sval));
323     if (cf_number.get()) {
324       // Let the dictionary own the CFNumber
325       ::CFDictionaryAddValue(dict, key, cf_number.get());
326       return true;
327     }
328   }
329   return false;
330 }
331 
332 bool CFCMutableDictionary::SetValueUInt32(CFStringRef key, uint32_t value,
333                                           bool can_create) {
334   CFMutableDictionaryRef dict = Dictionary(can_create);
335   if (dict != NULL) {
336     // Have to promote to the next size type so things don't appear negative of
337     // the MSBit is set...
338     int64_t sval = value;
339     CFCReleaser<CFNumberRef> cf_number(
340         ::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &sval));
341     if (cf_number.get()) {
342       // Let the dictionary own the CFNumber
343       ::CFDictionarySetValue(dict, key, cf_number.get());
344       return true;
345     }
346   }
347   return false;
348 }
349 
350 bool CFCMutableDictionary::AddValueUInt64(CFStringRef key, uint64_t value,
351                                           bool can_create) {
352   CFMutableDictionaryRef dict = Dictionary(can_create);
353   if (dict != NULL) {
354     // The number may appear negative if the MSBit is set in "value". Due to a
355     // limitation of CFNumber, there isn't a way to have it show up otherwise
356     // as of this writing.
357     CFCReleaser<CFNumberRef> cf_number(
358         ::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &value));
359     if (cf_number.get()) {
360       // Let the dictionary own the CFNumber
361       ::CFDictionaryAddValue(dict, key, cf_number.get());
362       return true;
363     }
364   }
365   return false;
366 }
367 
368 bool CFCMutableDictionary::SetValueUInt64(CFStringRef key, uint64_t value,
369                                           bool can_create) {
370   CFMutableDictionaryRef dict = Dictionary(can_create);
371   if (dict != NULL) {
372     // The number may appear negative if the MSBit is set in "value". Due to a
373     // limitation of CFNumber, there isn't a way to have it show up otherwise
374     // as of this writing.
375     CFCReleaser<CFNumberRef> cf_number(
376         ::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &value));
377     if (cf_number.get()) {
378       // Let the dictionary own the CFNumber
379       ::CFDictionarySetValue(dict, key, cf_number.get());
380       return true;
381     }
382   }
383   return false;
384 }
385 
386 bool CFCMutableDictionary::AddValueDouble(CFStringRef key, double value,
387                                           bool can_create) {
388   CFMutableDictionaryRef dict = Dictionary(can_create);
389   if (dict != NULL) {
390     // The number may appear negative if the MSBit is set in "value". Due to a
391     // limitation of CFNumber, there isn't a way to have it show up otherwise
392     // as of this writing.
393     CFCReleaser<CFNumberRef> cf_number(
394         ::CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &value));
395     if (cf_number.get()) {
396       // Let the dictionary own the CFNumber
397       ::CFDictionaryAddValue(dict, key, cf_number.get());
398       return true;
399     }
400   }
401   return false;
402 }
403 
404 bool CFCMutableDictionary::SetValueDouble(CFStringRef key, double value,
405                                           bool can_create) {
406   CFMutableDictionaryRef dict = Dictionary(can_create);
407   if (dict != NULL) {
408     // The number may appear negative if the MSBit is set in "value". Due to a
409     // limitation of CFNumber, there isn't a way to have it show up otherwise
410     // as of this writing.
411     CFCReleaser<CFNumberRef> cf_number(
412         ::CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &value));
413     if (cf_number.get()) {
414       // Let the dictionary own the CFNumber
415       ::CFDictionarySetValue(dict, key, cf_number.get());
416       return true;
417     }
418   }
419   return false;
420 }
421 
422 bool CFCMutableDictionary::AddValueCString(CFStringRef key, const char *cstr,
423                                            bool can_create) {
424   CFMutableDictionaryRef dict = Dictionary(can_create);
425   if (dict != NULL) {
426     CFCString cf_str(cstr, kCFStringEncodingUTF8);
427     if (cf_str.get()) {
428       // Let the dictionary own the CFNumber
429       ::CFDictionaryAddValue(dict, key, cf_str.get());
430       return true;
431     }
432   }
433   return false;
434 }
435 
436 bool CFCMutableDictionary::SetValueCString(CFStringRef key, const char *cstr,
437                                            bool can_create) {
438   CFMutableDictionaryRef dict = Dictionary(can_create);
439   if (dict != NULL) {
440     CFCString cf_str(cstr, kCFStringEncodingUTF8);
441     if (cf_str.get()) {
442       // Let the dictionary own the CFNumber
443       ::CFDictionarySetValue(dict, key, cf_str.get());
444       return true;
445     }
446   }
447   return false;
448 }
449 
450 void CFCMutableDictionary::RemoveAllValues() {
451   CFMutableDictionaryRef dict = get();
452   if (dict)
453     ::CFDictionaryRemoveAllValues(dict);
454 }
455 
456 void CFCMutableDictionary::RemoveValue(const void *value) {
457   CFMutableDictionaryRef dict = get();
458   if (dict)
459     ::CFDictionaryRemoveValue(dict, value);
460 }
461 void CFCMutableDictionary::ReplaceValue(const void *key, const void *value) {
462   CFMutableDictionaryRef dict = get();
463   if (dict)
464     ::CFDictionaryReplaceValue(dict, key, value);
465 }
466