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 356 // CFNumber, there isn't a way to have it show up otherwise as of this 357 // writing. 358 CFCReleaser<CFNumberRef> cf_number( 359 ::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &value)); 360 if (cf_number.get()) { 361 // Let the dictionary own the CFNumber 362 ::CFDictionaryAddValue(dict, key, cf_number.get()); 363 return true; 364 } 365 } 366 return false; 367 } 368 369 bool CFCMutableDictionary::SetValueUInt64(CFStringRef key, uint64_t value, 370 bool can_create) { 371 CFMutableDictionaryRef dict = Dictionary(can_create); 372 if (dict != NULL) { 373 // The number may appear negative if the MSBit is set in "value". Due to a 374 // limitation of 375 // CFNumber, there isn't a way to have it show up otherwise as of this 376 // writing. 377 CFCReleaser<CFNumberRef> cf_number( 378 ::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &value)); 379 if (cf_number.get()) { 380 // Let the dictionary own the CFNumber 381 ::CFDictionarySetValue(dict, key, cf_number.get()); 382 return true; 383 } 384 } 385 return false; 386 } 387 388 bool CFCMutableDictionary::AddValueDouble(CFStringRef key, double value, 389 bool can_create) { 390 CFMutableDictionaryRef dict = Dictionary(can_create); 391 if (dict != NULL) { 392 // The number may appear negative if the MSBit is set in "value". Due to a 393 // limitation of 394 // CFNumber, there isn't a way to have it show up otherwise as of this 395 // writing. 396 CFCReleaser<CFNumberRef> cf_number( 397 ::CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &value)); 398 if (cf_number.get()) { 399 // Let the dictionary own the CFNumber 400 ::CFDictionaryAddValue(dict, key, cf_number.get()); 401 return true; 402 } 403 } 404 return false; 405 } 406 407 bool CFCMutableDictionary::SetValueDouble(CFStringRef key, double value, 408 bool can_create) { 409 CFMutableDictionaryRef dict = Dictionary(can_create); 410 if (dict != NULL) { 411 // The number may appear negative if the MSBit is set in "value". Due to a 412 // limitation of 413 // CFNumber, there isn't a way to have it show up otherwise as of this 414 // writing. 415 CFCReleaser<CFNumberRef> cf_number( 416 ::CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &value)); 417 if (cf_number.get()) { 418 // Let the dictionary own the CFNumber 419 ::CFDictionarySetValue(dict, key, cf_number.get()); 420 return true; 421 } 422 } 423 return false; 424 } 425 426 bool CFCMutableDictionary::AddValueCString(CFStringRef key, const char *cstr, 427 bool can_create) { 428 CFMutableDictionaryRef dict = Dictionary(can_create); 429 if (dict != NULL) { 430 CFCString cf_str(cstr, kCFStringEncodingUTF8); 431 if (cf_str.get()) { 432 // Let the dictionary own the CFNumber 433 ::CFDictionaryAddValue(dict, key, cf_str.get()); 434 return true; 435 } 436 } 437 return false; 438 } 439 440 bool CFCMutableDictionary::SetValueCString(CFStringRef key, const char *cstr, 441 bool can_create) { 442 CFMutableDictionaryRef dict = Dictionary(can_create); 443 if (dict != NULL) { 444 CFCString cf_str(cstr, kCFStringEncodingUTF8); 445 if (cf_str.get()) { 446 // Let the dictionary own the CFNumber 447 ::CFDictionarySetValue(dict, key, cf_str.get()); 448 return true; 449 } 450 } 451 return false; 452 } 453 454 void CFCMutableDictionary::RemoveAllValues() { 455 CFMutableDictionaryRef dict = get(); 456 if (dict) 457 ::CFDictionaryRemoveAllValues(dict); 458 } 459 460 void CFCMutableDictionary::RemoveValue(const void *value) { 461 CFMutableDictionaryRef dict = get(); 462 if (dict) 463 ::CFDictionaryRemoveValue(dict, value); 464 } 465 void CFCMutableDictionary::ReplaceValue(const void *key, const void *value) { 466 CFMutableDictionaryRef dict = get(); 467 if (dict) 468 ::CFDictionaryReplaceValue(dict, key, value); 469 } 470