457 lines · cpp
1//===-- CFCMutableDictionary.cpp ------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "CFCMutableDictionary.h"10#include "CFCString.h"11// CFCString constructor12CFCMutableDictionary::CFCMutableDictionary(CFMutableDictionaryRef s)13 : CFCReleaser<CFMutableDictionaryRef>(s) {}14 15// CFCMutableDictionary copy constructor16CFCMutableDictionary::CFCMutableDictionary(const CFCMutableDictionary &rhs) =17 default;18 19// CFCMutableDictionary copy constructor20const CFCMutableDictionary &CFCMutableDictionary::21operator=(const CFCMutableDictionary &rhs) {22 if (this != &rhs)23 *this = rhs;24 return *this;25}26 27// Destructor28CFCMutableDictionary::~CFCMutableDictionary() = default;29 30CFIndex CFCMutableDictionary::GetCount() const {31 CFMutableDictionaryRef dict = get();32 if (dict)33 return ::CFDictionaryGetCount(dict);34 return 0;35}36 37CFIndex CFCMutableDictionary::GetCountOfKey(const void *key) const38 39{40 CFMutableDictionaryRef dict = get();41 if (dict)42 return ::CFDictionaryGetCountOfKey(dict, key);43 return 0;44}45 46CFIndex CFCMutableDictionary::GetCountOfValue(const void *value) const47 48{49 CFMutableDictionaryRef dict = get();50 if (dict)51 return ::CFDictionaryGetCountOfValue(dict, value);52 return 0;53}54 55void CFCMutableDictionary::GetKeysAndValues(const void **keys,56 const void **values) const {57 CFMutableDictionaryRef dict = get();58 if (dict)59 ::CFDictionaryGetKeysAndValues(dict, keys, values);60}61 62const void *CFCMutableDictionary::GetValue(const void *key) const63 64{65 CFMutableDictionaryRef dict = get();66 if (dict)67 return ::CFDictionaryGetValue(dict, key);68 return NULL;69}70 71Boolean72CFCMutableDictionary::GetValueIfPresent(const void *key,73 const void **value_handle) const {74 CFMutableDictionaryRef dict = get();75 if (dict)76 return ::CFDictionaryGetValueIfPresent(dict, key, value_handle);77 return false;78}79 80CFMutableDictionaryRef CFCMutableDictionary::Dictionary(bool can_create) {81 CFMutableDictionaryRef dict = get();82 if (can_create && dict == NULL) {83 dict = ::CFDictionaryCreateMutable(kCFAllocatorDefault, 0,84 &kCFTypeDictionaryKeyCallBacks,85 &kCFTypeDictionaryValueCallBacks);86 reset(dict);87 }88 return dict;89}90 91bool CFCMutableDictionary::AddValue(CFStringRef key, const void *value,92 bool can_create) {93 CFMutableDictionaryRef dict = Dictionary(can_create);94 if (dict != NULL) {95 // Let the dictionary own the CFNumber96 ::CFDictionaryAddValue(dict, key, value);97 return true;98 }99 return false;100}101 102bool CFCMutableDictionary::SetValue(CFStringRef key, const void *value,103 bool can_create) {104 CFMutableDictionaryRef dict = Dictionary(can_create);105 if (dict != NULL) {106 // Let the dictionary own the CFNumber107 ::CFDictionarySetValue(dict, key, value);108 return true;109 }110 return false;111}112 113bool CFCMutableDictionary::AddValueSInt8(CFStringRef key, int8_t value,114 bool can_create) {115 CFMutableDictionaryRef dict = Dictionary(can_create);116 if (dict != NULL) {117 CFCReleaser<CFNumberRef> cf_number(118 ::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt8Type, &value));119 if (cf_number.get()) {120 // Let the dictionary own the CFNumber121 ::CFDictionaryAddValue(dict, key, cf_number.get());122 return true;123 }124 }125 return false;126}127 128bool CFCMutableDictionary::SetValueSInt8(CFStringRef key, int8_t value,129 bool can_create) {130 CFMutableDictionaryRef dict = Dictionary(can_create);131 if (dict != NULL) {132 CFCReleaser<CFNumberRef> cf_number(133 ::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt8Type, &value));134 if (cf_number.get()) {135 // Let the dictionary own the CFNumber136 ::CFDictionarySetValue(dict, key, cf_number.get());137 return true;138 }139 }140 return false;141}142 143bool CFCMutableDictionary::AddValueSInt16(CFStringRef key, int16_t value,144 bool can_create) {145 CFMutableDictionaryRef dict = Dictionary(can_create);146 if (dict != NULL) {147 CFCReleaser<CFNumberRef> cf_number(148 ::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt16Type, &value));149 if (cf_number.get()) {150 // Let the dictionary own the CFNumber151 ::CFDictionaryAddValue(dict, key, cf_number.get());152 return true;153 }154 }155 return false;156}157 158bool CFCMutableDictionary::SetValueSInt16(CFStringRef key, int16_t value,159 bool can_create) {160 CFMutableDictionaryRef dict = Dictionary(can_create);161 if (dict != NULL) {162 CFCReleaser<CFNumberRef> cf_number(163 ::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt16Type, &value));164 if (cf_number.get()) {165 // Let the dictionary own the CFNumber166 ::CFDictionarySetValue(dict, key, cf_number.get());167 return true;168 }169 }170 return false;171}172 173bool CFCMutableDictionary::AddValueSInt32(CFStringRef key, int32_t value,174 bool can_create) {175 CFMutableDictionaryRef dict = Dictionary(can_create);176 if (dict != NULL) {177 CFCReleaser<CFNumberRef> cf_number(178 ::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &value));179 if (cf_number.get()) {180 // Let the dictionary own the CFNumber181 ::CFDictionaryAddValue(dict, key, cf_number.get());182 return true;183 }184 }185 return false;186}187 188bool CFCMutableDictionary::SetValueSInt32(CFStringRef key, int32_t value,189 bool can_create) {190 CFMutableDictionaryRef dict = Dictionary(can_create);191 if (dict != NULL) {192 CFCReleaser<CFNumberRef> cf_number(193 ::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &value));194 if (cf_number.get()) {195 // Let the dictionary own the CFNumber196 ::CFDictionarySetValue(dict, key, cf_number.get());197 return true;198 }199 }200 return false;201}202 203bool CFCMutableDictionary::AddValueSInt64(CFStringRef key, int64_t value,204 bool can_create) {205 CFMutableDictionaryRef dict = Dictionary(can_create);206 if (dict != NULL) {207 CFCReleaser<CFNumberRef> cf_number(208 ::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &value));209 if (cf_number.get()) {210 // Let the dictionary own the CFNumber211 ::CFDictionaryAddValue(dict, key, cf_number.get());212 return true;213 }214 }215 return false;216}217 218bool CFCMutableDictionary::SetValueSInt64(CFStringRef key, int64_t value,219 bool can_create) {220 CFMutableDictionaryRef dict = Dictionary(can_create);221 if (dict != NULL) {222 CFCReleaser<CFNumberRef> cf_number(223 ::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &value));224 if (cf_number.get()) {225 // Let the dictionary own the CFNumber226 ::CFDictionarySetValue(dict, key, cf_number.get());227 return true;228 }229 }230 return false;231}232 233bool CFCMutableDictionary::AddValueUInt8(CFStringRef key, uint8_t value,234 bool can_create) {235 CFMutableDictionaryRef dict = Dictionary(can_create);236 if (dict != NULL) {237 // Have to promote to the next size type so things don't appear negative of238 // 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 CFNumber244 ::CFDictionaryAddValue(dict, key, cf_number.get());245 return true;246 }247 }248 return false;249}250 251bool CFCMutableDictionary::SetValueUInt8(CFStringRef key, uint8_t value,252 bool can_create) {253 CFMutableDictionaryRef dict = Dictionary(can_create);254 if (dict != NULL) {255 // Have to promote to the next size type so things don't appear negative of256 // 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 CFNumber262 ::CFDictionarySetValue(dict, key, cf_number.get());263 return true;264 }265 }266 return false;267}268 269bool CFCMutableDictionary::AddValueUInt16(CFStringRef key, uint16_t value,270 bool can_create) {271 CFMutableDictionaryRef dict = Dictionary(can_create);272 if (dict != NULL) {273 // Have to promote to the next size type so things don't appear negative of274 // 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 CFNumber280 ::CFDictionaryAddValue(dict, key, cf_number.get());281 return true;282 }283 }284 return false;285}286 287bool CFCMutableDictionary::SetValueUInt16(CFStringRef key, uint16_t value,288 bool can_create) {289 CFMutableDictionaryRef dict = Dictionary(can_create);290 if (dict != NULL) {291 // Have to promote to the next size type so things don't appear negative of292 // 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 CFNumber298 ::CFDictionarySetValue(dict, key, cf_number.get());299 return true;300 }301 }302 return false;303}304 305bool CFCMutableDictionary::AddValueUInt32(CFStringRef key, uint32_t value,306 bool can_create) {307 CFMutableDictionaryRef dict = Dictionary(can_create);308 if (dict != NULL) {309 // Have to promote to the next size type so things don't appear negative of310 // 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 CFNumber316 ::CFDictionaryAddValue(dict, key, cf_number.get());317 return true;318 }319 }320 return false;321}322 323bool CFCMutableDictionary::SetValueUInt32(CFStringRef key, uint32_t value,324 bool can_create) {325 CFMutableDictionaryRef dict = Dictionary(can_create);326 if (dict != NULL) {327 // Have to promote to the next size type so things don't appear negative of328 // 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 CFNumber334 ::CFDictionarySetValue(dict, key, cf_number.get());335 return true;336 }337 }338 return false;339}340 341bool CFCMutableDictionary::AddValueUInt64(CFStringRef key, uint64_t value,342 bool can_create) {343 CFMutableDictionaryRef dict = Dictionary(can_create);344 if (dict != NULL) {345 // The number may appear negative if the MSBit is set in "value". Due to a346 // limitation of CFNumber, there isn't a way to have it show up otherwise347 // 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 CFNumber352 ::CFDictionaryAddValue(dict, key, cf_number.get());353 return true;354 }355 }356 return false;357}358 359bool CFCMutableDictionary::SetValueUInt64(CFStringRef key, uint64_t value,360 bool can_create) {361 CFMutableDictionaryRef dict = Dictionary(can_create);362 if (dict != NULL) {363 // The number may appear negative if the MSBit is set in "value". Due to a364 // limitation of CFNumber, there isn't a way to have it show up otherwise365 // 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 CFNumber370 ::CFDictionarySetValue(dict, key, cf_number.get());371 return true;372 }373 }374 return false;375}376 377bool CFCMutableDictionary::AddValueDouble(CFStringRef key, double value,378 bool can_create) {379 CFMutableDictionaryRef dict = Dictionary(can_create);380 if (dict != NULL) {381 // The number may appear negative if the MSBit is set in "value". Due to a382 // limitation of CFNumber, there isn't a way to have it show up otherwise383 // 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 CFNumber388 ::CFDictionaryAddValue(dict, key, cf_number.get());389 return true;390 }391 }392 return false;393}394 395bool CFCMutableDictionary::SetValueDouble(CFStringRef key, double value,396 bool can_create) {397 CFMutableDictionaryRef dict = Dictionary(can_create);398 if (dict != NULL) {399 // The number may appear negative if the MSBit is set in "value". Due to a400 // limitation of CFNumber, there isn't a way to have it show up otherwise401 // 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 CFNumber406 ::CFDictionarySetValue(dict, key, cf_number.get());407 return true;408 }409 }410 return false;411}412 413bool CFCMutableDictionary::AddValueCString(CFStringRef key, const char *cstr,414 bool can_create) {415 CFMutableDictionaryRef dict = Dictionary(can_create);416 if (dict != NULL) {417 CFCString cf_str(cstr, kCFStringEncodingUTF8);418 if (cf_str.get()) {419 // Let the dictionary own the CFNumber420 ::CFDictionaryAddValue(dict, key, cf_str.get());421 return true;422 }423 }424 return false;425}426 427bool CFCMutableDictionary::SetValueCString(CFStringRef key, const char *cstr,428 bool can_create) {429 CFMutableDictionaryRef dict = Dictionary(can_create);430 if (dict != NULL) {431 CFCString cf_str(cstr, kCFStringEncodingUTF8);432 if (cf_str.get()) {433 // Let the dictionary own the CFNumber434 ::CFDictionarySetValue(dict, key, cf_str.get());435 return true;436 }437 }438 return false;439}440 441void CFCMutableDictionary::RemoveAllValues() {442 CFMutableDictionaryRef dict = get();443 if (dict)444 ::CFDictionaryRemoveAllValues(dict);445}446 447void CFCMutableDictionary::RemoveValue(const void *value) {448 CFMutableDictionaryRef dict = get();449 if (dict)450 ::CFDictionaryRemoveValue(dict, value);451}452void CFCMutableDictionary::ReplaceValue(const void *key, const void *value) {453 CFMutableDictionaryRef dict = get();454 if (dict)455 ::CFDictionaryReplaceValue(dict, key, value);456}457