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