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