1 //===-- CFCMutableDictionary.cpp ------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "CFCMutableDictionary.h"
10 #include "CFCString.h"
11 // CFCString constructor
12 CFCMutableDictionary::CFCMutableDictionary(CFMutableDictionaryRef s
)
13 : CFCReleaser
<CFMutableDictionaryRef
>(s
) {}
15 // CFCMutableDictionary copy constructor
16 CFCMutableDictionary::CFCMutableDictionary(const CFCMutableDictionary
&rhs
) =
19 // CFCMutableDictionary copy constructor
20 const CFCMutableDictionary
&CFCMutableDictionary::
21 operator=(const CFCMutableDictionary
&rhs
) {
28 CFCMutableDictionary::~CFCMutableDictionary() = default;
30 CFIndex
CFCMutableDictionary::GetCount() const {
31 CFMutableDictionaryRef dict
= get();
33 return ::CFDictionaryGetCount(dict
);
37 CFIndex
CFCMutableDictionary::GetCountOfKey(const void *key
) const
40 CFMutableDictionaryRef dict
= get();
42 return ::CFDictionaryGetCountOfKey(dict
, key
);
46 CFIndex
CFCMutableDictionary::GetCountOfValue(const void *value
) const
49 CFMutableDictionaryRef dict
= get();
51 return ::CFDictionaryGetCountOfValue(dict
, value
);
55 void CFCMutableDictionary::GetKeysAndValues(const void **keys
,
56 const void **values
) const {
57 CFMutableDictionaryRef dict
= get();
59 ::CFDictionaryGetKeysAndValues(dict
, keys
, values
);
62 const void *CFCMutableDictionary::GetValue(const void *key
) const
65 CFMutableDictionaryRef dict
= get();
67 return ::CFDictionaryGetValue(dict
, key
);
72 CFCMutableDictionary::GetValueIfPresent(const void *key
,
73 const void **value_handle
) const {
74 CFMutableDictionaryRef dict
= get();
76 return ::CFDictionaryGetValueIfPresent(dict
, key
, value_handle
);
80 CFMutableDictionaryRef
CFCMutableDictionary::Dictionary(bool can_create
) {
81 CFMutableDictionaryRef dict
= get();
82 if (can_create
&& dict
== NULL
) {
83 dict
= ::CFDictionaryCreateMutable(kCFAllocatorDefault
, 0,
84 &kCFTypeDictionaryKeyCallBacks
,
85 &kCFTypeDictionaryValueCallBacks
);
91 bool CFCMutableDictionary::AddValue(CFStringRef key
, const void *value
,
93 CFMutableDictionaryRef dict
= Dictionary(can_create
);
95 // Let the dictionary own the CFNumber
96 ::CFDictionaryAddValue(dict
, key
, value
);
102 bool CFCMutableDictionary::SetValue(CFStringRef key
, const void *value
,
104 CFMutableDictionaryRef dict
= Dictionary(can_create
);
106 // Let the dictionary own the CFNumber
107 ::CFDictionarySetValue(dict
, key
, value
);
113 bool CFCMutableDictionary::AddValueSInt8(CFStringRef key
, int8_t value
,
115 CFMutableDictionaryRef dict
= Dictionary(can_create
);
117 CFCReleaser
<CFNumberRef
> cf_number(
118 ::CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt8Type
, &value
));
119 if (cf_number
.get()) {
120 // Let the dictionary own the CFNumber
121 ::CFDictionaryAddValue(dict
, key
, cf_number
.get());
128 bool CFCMutableDictionary::SetValueSInt8(CFStringRef key
, int8_t value
,
130 CFMutableDictionaryRef dict
= Dictionary(can_create
);
132 CFCReleaser
<CFNumberRef
> cf_number(
133 ::CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt8Type
, &value
));
134 if (cf_number
.get()) {
135 // Let the dictionary own the CFNumber
136 ::CFDictionarySetValue(dict
, key
, cf_number
.get());
143 bool CFCMutableDictionary::AddValueSInt16(CFStringRef key
, int16_t value
,
145 CFMutableDictionaryRef dict
= Dictionary(can_create
);
147 CFCReleaser
<CFNumberRef
> cf_number(
148 ::CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt16Type
, &value
));
149 if (cf_number
.get()) {
150 // Let the dictionary own the CFNumber
151 ::CFDictionaryAddValue(dict
, key
, cf_number
.get());
158 bool CFCMutableDictionary::SetValueSInt16(CFStringRef key
, int16_t value
,
160 CFMutableDictionaryRef dict
= Dictionary(can_create
);
162 CFCReleaser
<CFNumberRef
> cf_number(
163 ::CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt16Type
, &value
));
164 if (cf_number
.get()) {
165 // Let the dictionary own the CFNumber
166 ::CFDictionarySetValue(dict
, key
, cf_number
.get());
173 bool CFCMutableDictionary::AddValueSInt32(CFStringRef key
, int32_t value
,
175 CFMutableDictionaryRef dict
= Dictionary(can_create
);
177 CFCReleaser
<CFNumberRef
> cf_number(
178 ::CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt32Type
, &value
));
179 if (cf_number
.get()) {
180 // Let the dictionary own the CFNumber
181 ::CFDictionaryAddValue(dict
, key
, cf_number
.get());
188 bool CFCMutableDictionary::SetValueSInt32(CFStringRef key
, int32_t value
,
190 CFMutableDictionaryRef dict
= Dictionary(can_create
);
192 CFCReleaser
<CFNumberRef
> cf_number(
193 ::CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt32Type
, &value
));
194 if (cf_number
.get()) {
195 // Let the dictionary own the CFNumber
196 ::CFDictionarySetValue(dict
, key
, cf_number
.get());
203 bool CFCMutableDictionary::AddValueSInt64(CFStringRef key
, int64_t value
,
205 CFMutableDictionaryRef dict
= Dictionary(can_create
);
207 CFCReleaser
<CFNumberRef
> cf_number(
208 ::CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt64Type
, &value
));
209 if (cf_number
.get()) {
210 // Let the dictionary own the CFNumber
211 ::CFDictionaryAddValue(dict
, key
, cf_number
.get());
218 bool CFCMutableDictionary::SetValueSInt64(CFStringRef key
, int64_t value
,
220 CFMutableDictionaryRef dict
= Dictionary(can_create
);
222 CFCReleaser
<CFNumberRef
> cf_number(
223 ::CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt64Type
, &value
));
224 if (cf_number
.get()) {
225 // Let the dictionary own the CFNumber
226 ::CFDictionarySetValue(dict
, key
, cf_number
.get());
233 bool CFCMutableDictionary::AddValueUInt8(CFStringRef key
, uint8_t value
,
235 CFMutableDictionaryRef dict
= Dictionary(can_create
);
237 // Have to promote to the next size type so things don't appear negative of
238 // the MSBit is set...
239 int16_t sval
= value
;
240 CFCReleaser
<CFNumberRef
> cf_number(
241 ::CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt16Type
, &sval
));
242 if (cf_number
.get()) {
243 // Let the dictionary own the CFNumber
244 ::CFDictionaryAddValue(dict
, key
, cf_number
.get());
251 bool CFCMutableDictionary::SetValueUInt8(CFStringRef key
, uint8_t value
,
253 CFMutableDictionaryRef dict
= Dictionary(can_create
);
255 // Have to promote to the next size type so things don't appear negative of
256 // the MSBit is set...
257 int16_t sval
= value
;
258 CFCReleaser
<CFNumberRef
> cf_number(
259 ::CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt16Type
, &sval
));
260 if (cf_number
.get()) {
261 // Let the dictionary own the CFNumber
262 ::CFDictionarySetValue(dict
, key
, cf_number
.get());
269 bool CFCMutableDictionary::AddValueUInt16(CFStringRef key
, uint16_t value
,
271 CFMutableDictionaryRef dict
= Dictionary(can_create
);
273 // Have to promote to the next size type so things don't appear negative of
274 // the MSBit is set...
275 int32_t sval
= value
;
276 CFCReleaser
<CFNumberRef
> cf_number(
277 ::CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt32Type
, &sval
));
278 if (cf_number
.get()) {
279 // Let the dictionary own the CFNumber
280 ::CFDictionaryAddValue(dict
, key
, cf_number
.get());
287 bool CFCMutableDictionary::SetValueUInt16(CFStringRef key
, uint16_t value
,
289 CFMutableDictionaryRef dict
= Dictionary(can_create
);
291 // Have to promote to the next size type so things don't appear negative of
292 // the MSBit is set...
293 int32_t sval
= value
;
294 CFCReleaser
<CFNumberRef
> cf_number(
295 ::CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt32Type
, &sval
));
296 if (cf_number
.get()) {
297 // Let the dictionary own the CFNumber
298 ::CFDictionarySetValue(dict
, key
, cf_number
.get());
305 bool CFCMutableDictionary::AddValueUInt32(CFStringRef key
, uint32_t value
,
307 CFMutableDictionaryRef dict
= Dictionary(can_create
);
309 // Have to promote to the next size type so things don't appear negative of
310 // the MSBit is set...
311 int64_t sval
= value
;
312 CFCReleaser
<CFNumberRef
> cf_number(
313 ::CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt64Type
, &sval
));
314 if (cf_number
.get()) {
315 // Let the dictionary own the CFNumber
316 ::CFDictionaryAddValue(dict
, key
, cf_number
.get());
323 bool CFCMutableDictionary::SetValueUInt32(CFStringRef key
, uint32_t value
,
325 CFMutableDictionaryRef dict
= Dictionary(can_create
);
327 // Have to promote to the next size type so things don't appear negative of
328 // the MSBit is set...
329 int64_t sval
= value
;
330 CFCReleaser
<CFNumberRef
> cf_number(
331 ::CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt64Type
, &sval
));
332 if (cf_number
.get()) {
333 // Let the dictionary own the CFNumber
334 ::CFDictionarySetValue(dict
, key
, cf_number
.get());
341 bool CFCMutableDictionary::AddValueUInt64(CFStringRef key
, uint64_t value
,
343 CFMutableDictionaryRef dict
= Dictionary(can_create
);
345 // The number may appear negative if the MSBit is set in "value". Due to a
346 // limitation of CFNumber, there isn't a way to have it show up otherwise
347 // as of this writing.
348 CFCReleaser
<CFNumberRef
> cf_number(
349 ::CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt64Type
, &value
));
350 if (cf_number
.get()) {
351 // Let the dictionary own the CFNumber
352 ::CFDictionaryAddValue(dict
, key
, cf_number
.get());
359 bool CFCMutableDictionary::SetValueUInt64(CFStringRef key
, uint64_t value
,
361 CFMutableDictionaryRef dict
= Dictionary(can_create
);
363 // The number may appear negative if the MSBit is set in "value". Due to a
364 // limitation of CFNumber, there isn't a way to have it show up otherwise
365 // as of this writing.
366 CFCReleaser
<CFNumberRef
> cf_number(
367 ::CFNumberCreate(kCFAllocatorDefault
, kCFNumberSInt64Type
, &value
));
368 if (cf_number
.get()) {
369 // Let the dictionary own the CFNumber
370 ::CFDictionarySetValue(dict
, key
, cf_number
.get());
377 bool CFCMutableDictionary::AddValueDouble(CFStringRef key
, double value
,
379 CFMutableDictionaryRef dict
= Dictionary(can_create
);
381 // The number may appear negative if the MSBit is set in "value". Due to a
382 // limitation of CFNumber, there isn't a way to have it show up otherwise
383 // as of this writing.
384 CFCReleaser
<CFNumberRef
> cf_number(
385 ::CFNumberCreate(kCFAllocatorDefault
, kCFNumberDoubleType
, &value
));
386 if (cf_number
.get()) {
387 // Let the dictionary own the CFNumber
388 ::CFDictionaryAddValue(dict
, key
, cf_number
.get());
395 bool CFCMutableDictionary::SetValueDouble(CFStringRef key
, double value
,
397 CFMutableDictionaryRef dict
= Dictionary(can_create
);
399 // The number may appear negative if the MSBit is set in "value". Due to a
400 // limitation of CFNumber, there isn't a way to have it show up otherwise
401 // as of this writing.
402 CFCReleaser
<CFNumberRef
> cf_number(
403 ::CFNumberCreate(kCFAllocatorDefault
, kCFNumberDoubleType
, &value
));
404 if (cf_number
.get()) {
405 // Let the dictionary own the CFNumber
406 ::CFDictionarySetValue(dict
, key
, cf_number
.get());
413 bool CFCMutableDictionary::AddValueCString(CFStringRef key
, const char *cstr
,
415 CFMutableDictionaryRef dict
= Dictionary(can_create
);
417 CFCString
cf_str(cstr
, kCFStringEncodingUTF8
);
419 // Let the dictionary own the CFNumber
420 ::CFDictionaryAddValue(dict
, key
, cf_str
.get());
427 bool CFCMutableDictionary::SetValueCString(CFStringRef key
, const char *cstr
,
429 CFMutableDictionaryRef dict
= Dictionary(can_create
);
431 CFCString
cf_str(cstr
, kCFStringEncodingUTF8
);
433 // Let the dictionary own the CFNumber
434 ::CFDictionarySetValue(dict
, key
, cf_str
.get());
441 void CFCMutableDictionary::RemoveAllValues() {
442 CFMutableDictionaryRef dict
= get();
444 ::CFDictionaryRemoveAllValues(dict
);
447 void CFCMutableDictionary::RemoveValue(const void *value
) {
448 CFMutableDictionaryRef dict
= get();
450 ::CFDictionaryRemoveValue(dict
, value
);
452 void CFCMutableDictionary::ReplaceValue(const void *key
, const void *value
) {
453 CFMutableDictionaryRef dict
= get();
455 ::CFDictionaryReplaceValue(dict
, key
, value
);