Jamoma API  0.6.0.a19
CACFDictionary.cpp
1 /* Copyright 2007 Apple Inc. All Rights Reserved.
2 
3  Disclaimer: IMPORTANT: This Apple software is supplied to you by
4  Apple Inc. ("Apple") in consideration of your agreement to the
5  following terms, and your use, installation, modification or
6  redistribution of this Apple software constitutes acceptance of these
7  terms. If you do not agree with these terms, please do not use,
8  install, modify or redistribute this Apple software.
9 
10  In consideration of your agreement to abide by the following terms, and
11  subject to these terms, Apple grants you a personal, non-exclusive
12  license, under Apple's copyrights in this original Apple software (the
13  "Apple Software"), to use, reproduce, modify and redistribute the Apple
14  Software, with or without modifications, in source and/or binary forms;
15  provided that if you redistribute the Apple Software in its entirety and
16  without modifications, you must retain this notice and the following
17  text and disclaimers in all such redistributions of the Apple Software.
18  Neither the name, trademarks, service marks or logos of Apple Inc.
19  may be used to endorse or promote products derived from the Apple
20  Software without specific prior written permission from Apple. Except
21  as expressly stated in this notice, no other rights or licenses, express
22  or implied, are granted by Apple herein, including but not limited to
23  any patent rights that may be infringed by your derivative works or by
24  other works in which the Apple Software may be incorporated.
25 
26  The Apple Software is provided by Apple on an "AS IS" basis. APPLE
27  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
28  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
29  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
30  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
31 
32  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
33  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
36  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
37  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
38  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
39  POSSIBILITY OF SUCH DAMAGE.
40 */
41 //=============================================================================
42 // Includes
43 //=============================================================================
44 
45 // Self Include
46 #include "CACFDictionary.h"
47 
48 // PublicUtility Includes
49 #include "CACFArray.h"
50 #include "CACFNumber.h"
51 #include "CACFString.h"
52 
53 //=============================================================================
54 // CACFDictionary
55 //=============================================================================
56 
57 bool CACFDictionary::HasKey(const CFStringRef inKey) const
58 {
59  return CFDictionaryContainsKey(mCFDictionary, inKey) != 0;
60 }
61 
62 UInt32 CACFDictionary::Size () const
63 {
64  return ToUInt32(CFDictionaryGetCount(mCFDictionary));
65 }
66 
67 void CACFDictionary::GetKeys (const void **keys) const
68 {
69  CFDictionaryGetKeysAndValues(mCFDictionary, keys, NULL);
70 }
71 
72 bool CACFDictionary::GetBool(const CFStringRef inKey, bool& outValue) const
73 {
74  bool theAnswer = false;
75 
76  CFTypeRef theValue = NULL;
77  if(GetCFType(inKey, theValue))
78  {
79  if((theValue != NULL) && (CFGetTypeID(theValue) == CFBooleanGetTypeID()))
80  {
81  outValue = CFBooleanGetValue(static_cast<CFBooleanRef>(theValue));
82  theAnswer = true;
83  }
84  else if((theValue != NULL) && (CFGetTypeID(theValue) == CFNumberGetTypeID()))
85  {
86  SInt32 theNumericValue = 0;
87  CFNumberGetValue(static_cast<CFNumberRef>(theValue), kCFNumberSInt32Type, &theNumericValue);
88  outValue = theNumericValue != 0;
89  theAnswer = true;
90  }
91  }
92 
93  return theAnswer;
94 }
95 
96 bool CACFDictionary::GetSInt32(const CFStringRef inKey, SInt32& outValue) const
97 {
98  bool theAnswer = false;
99 
100  CFTypeRef theValue = NULL;
101  if(GetCFType(inKey, theValue))
102  {
103  if((theValue != NULL) && (CFGetTypeID(theValue) == CFNumberGetTypeID()))
104  {
105  CFNumberGetValue(static_cast<CFNumberRef>(theValue), kCFNumberSInt32Type, &outValue);
106  theAnswer = true;
107  }
108  }
109 
110  return theAnswer;
111 }
112 
113 bool CACFDictionary::GetUInt32(const CFStringRef inKey, UInt32& outValue) const
114 {
115  bool theAnswer = false;
116 
117  CFTypeRef theValue = NULL;
118  if(GetCFType(inKey, theValue))
119  {
120  if((theValue != NULL) && (CFGetTypeID(theValue) == CFNumberGetTypeID()))
121  {
122  CFNumberGetValue(static_cast<CFNumberRef>(theValue), kCFNumberSInt32Type, &outValue);
123  theAnswer = true;
124  }
125  }
126 
127  return theAnswer;
128 }
129 
130 bool CACFDictionary::GetSInt64(const CFStringRef inKey, SInt64& outValue) const
131 {
132  bool theAnswer = false;
133 
134  CFTypeRef theValue = NULL;
135  if(GetCFType(inKey, theValue))
136  {
137  if((theValue != NULL) && (CFGetTypeID(theValue) == CFNumberGetTypeID()))
138  {
139  CFNumberGetValue(static_cast<CFNumberRef>(theValue), kCFNumberSInt64Type, &outValue);
140  theAnswer = true;
141  }
142  }
143 
144  return theAnswer;
145 }
146 
147 bool CACFDictionary::GetUInt64(const CFStringRef inKey, UInt64& outValue) const
148 {
149  bool theAnswer = false;
150 
151  CFTypeRef theValue = NULL;
152  if(GetCFType(inKey, theValue))
153  {
154  if((theValue != NULL) && (CFGetTypeID(theValue) == CFNumberGetTypeID()))
155  {
156  CFNumberGetValue(static_cast<CFNumberRef>(theValue), kCFNumberSInt64Type, &outValue);
157  theAnswer = true;
158  }
159  }
160 
161  return theAnswer;
162 }
163 
164 bool CACFDictionary::GetFloat32(const CFStringRef inKey, Float32& outValue) const
165 {
166  bool theAnswer = false;
167 
168  CFTypeRef theValue = NULL;
169  if(GetCFType(inKey, theValue))
170  {
171  if((theValue != NULL) && (CFGetTypeID(theValue) == CFNumberGetTypeID()))
172  {
173  CFNumberGetValue(static_cast<CFNumberRef>(theValue), kCFNumberFloat32Type, &outValue);
174  theAnswer = true;
175  }
176  }
177 
178  return theAnswer;
179 }
180 
181 bool CACFDictionary::GetFloat64(const CFStringRef inKey, Float64& outValue) const
182 {
183  bool theAnswer = false;
184 
185  CFTypeRef theValue = NULL;
186  if(GetCFType(inKey, theValue))
187  {
188  if((theValue != NULL) && (CFGetTypeID(theValue) == CFNumberGetTypeID()))
189  {
190  CFNumberGetValue(static_cast<CFNumberRef>(theValue), kCFNumberFloat64Type, &outValue);
191  theAnswer = true;
192  }
193  }
194 
195  return theAnswer;
196 }
197 
198 bool CACFDictionary::GetFixed64(const CFStringRef inKey, Float64& outValue) const
199 {
200  bool theAnswer = false;
201 
202  CFTypeRef theValue = NULL;
203  if(GetCFType(inKey, theValue))
204  {
205  if((theValue != NULL) && (CFGetTypeID(theValue) == CFNumberGetTypeID()))
206  {
207  SInt64 theFixed64 = 0;
208  CFNumberGetValue(static_cast<CFNumberRef>(theValue), kCFNumberSInt64Type, &theFixed64);
209  outValue = static_cast<Float64>(theFixed64 >> 32);
210  outValue += static_cast<Float64>(theFixed64 & 0x00000000FFFFFFFFLL) / static_cast<Float64>(0x0000000100000000LL);
211  theAnswer = true;
212  }
213  }
214 
215  return theAnswer;
216 }
217 
218 bool CACFDictionary::GetString(const CFStringRef inKey, CFStringRef& outValue) const
219 {
220  bool theAnswer = false;
221 
222  CFTypeRef theValue = NULL;
223  if(GetCFType(inKey, theValue))
224  {
225  if((theValue != NULL) && (CFGetTypeID(theValue) == CFStringGetTypeID()))
226  {
227  outValue = static_cast<CFStringRef>(theValue);
228  theAnswer = true;
229  }
230  }
231 
232  return theAnswer;
233 }
234 
235 bool CACFDictionary::GetArray(const CFStringRef inKey, CFArrayRef& outValue) const
236 {
237  bool theAnswer = false;
238 
239  CFTypeRef theValue = NULL;
240  if(GetCFType(inKey, theValue))
241  {
242  if((theValue != NULL) && (CFGetTypeID(theValue) == CFArrayGetTypeID()))
243  {
244  outValue = static_cast<CFArrayRef>(theValue);
245  theAnswer = true;
246  }
247  }
248 
249  return theAnswer;
250 }
251 
252 bool CACFDictionary::GetDictionary(const CFStringRef inKey, CFDictionaryRef& outValue) const
253 {
254  bool theAnswer = false;
255 
256  CFTypeRef theValue = NULL;
257  if(GetCFType(inKey, theValue))
258  {
259  if((theValue != NULL) && (CFGetTypeID(theValue) == CFDictionaryGetTypeID()))
260  {
261  outValue = static_cast<CFDictionaryRef>(theValue);
262  theAnswer = true;
263  }
264  }
265 
266  return theAnswer;
267 }
268 
269 bool CACFDictionary::GetData(const CFStringRef inKey, CFDataRef& outValue) const
270 {
271  bool theAnswer = false;
272 
273  CFTypeRef theValue = NULL;
274  if(GetCFType(inKey, theValue))
275  {
276  if((theValue != NULL) && (CFGetTypeID(theValue) == CFDataGetTypeID()))
277  {
278  outValue = static_cast<CFDataRef>(theValue);
279  theAnswer = true;
280  }
281  }
282 
283  return theAnswer;
284 }
285 
286 bool CACFDictionary::GetCFType(const CFStringRef inKey, CFTypeRef& outValue) const
287 {
288  bool theAnswer = false;
289 
290  if(mCFDictionary != NULL)
291  {
292  outValue = CFDictionaryGetValue(mCFDictionary, inKey);
293  theAnswer = (outValue != NULL);
294  }
295 
296  return theAnswer;
297 }
298 
299 bool CACFDictionary::GetCFTypeWithCStringKey(const char* inKey, CFTypeRef& outValue) const
300 {
301  bool theAnswer = false;
302 
303  if(mCFDictionary != NULL)
304  {
305  CACFString theKey(inKey);
306  if(theKey.IsValid())
307  {
308  theAnswer = GetCFType(theKey.GetCFString(), outValue);
309  }
310  }
311 
312  return theAnswer;
313 }
314 
315 void CACFDictionary::GetCACFString(const CFStringRef inKey, CACFString& outValue) const
316 {
317  outValue = static_cast<CFStringRef>(NULL);
318  CFTypeRef theValue = NULL;
319  if(GetCFType(inKey, theValue))
320  {
321  if((theValue != NULL) && (CFGetTypeID(theValue) == CFStringGetTypeID()))
322  {
323  outValue = static_cast<CFStringRef>(theValue);
324  }
325  }
326 }
327 
328 void CACFDictionary::GetCACFArray(const CFStringRef inKey, CACFArray& outValue) const
329 {
330  outValue = static_cast<CFArrayRef>(NULL);
331  CFTypeRef theValue = NULL;
332  if(GetCFType(inKey, theValue))
333  {
334  if((theValue != NULL) && (CFGetTypeID(theValue) == CFArrayGetTypeID()))
335  {
336  outValue = static_cast<CFArrayRef>(theValue);
337  }
338  }
339 }
340 
341 void CACFDictionary::GetCACFDictionary(const CFStringRef inKey, CACFDictionary& outValue) const
342 {
343  outValue = static_cast<CFDictionaryRef>(NULL);
344  CFTypeRef theValue = NULL;
345  if(GetCFType(inKey, theValue))
346  {
347  if((theValue != NULL) && (CFGetTypeID(theValue) == CFDictionaryGetTypeID()))
348  {
349  outValue = static_cast<CFDictionaryRef>(theValue);
350  }
351  }
352 }
353 
354 bool CACFDictionary::AddBool(const CFStringRef inKey, bool inValue)
355 {
356  bool theAnswer = false;
357 
358  if(mMutable && (mCFDictionary != NULL))
359  {
360  CACFBoolean theValue(inValue);
361  theAnswer = AddCFType(inKey, theValue.GetCFBoolean());
362  }
363 
364  return theAnswer;
365 }
366 
367 bool CACFDictionary::AddSInt32(const CFStringRef inKey, SInt32 inValue)
368 {
369  bool theAnswer = false;
370 
371  if(mMutable && (mCFDictionary != NULL))
372  {
373  CACFNumber theValue(inValue);
374  theAnswer = AddCFType(inKey, theValue.GetCFNumber());
375  }
376 
377  return theAnswer;
378 }
379 
380 bool CACFDictionary::AddUInt32(const CFStringRef inKey, UInt32 inValue)
381 {
382  bool theAnswer = false;
383 
384  if(mMutable && (mCFDictionary != NULL))
385  {
386  CACFNumber theValue(inValue);
387  theAnswer = AddCFType(inKey, theValue.GetCFNumber());
388  }
389 
390  return theAnswer;
391 }
392 
393 bool CACFDictionary::AddSInt64(const CFStringRef inKey, SInt64 inValue)
394 {
395  bool theAnswer = false;
396 
397  if(mMutable && (mCFDictionary != NULL))
398  {
399  CACFNumber theValue(inValue);
400  theAnswer = AddCFType(inKey, theValue.GetCFNumber());
401  }
402 
403  return theAnswer;
404 }
405 
406 bool CACFDictionary::AddUInt64(const CFStringRef inKey, UInt64 inValue)
407 {
408  bool theAnswer = false;
409 
410  if(mMutable && (mCFDictionary != NULL))
411  {
412  CACFNumber theValue(inValue);
413  theAnswer = AddCFType(inKey, theValue.GetCFNumber());
414  }
415 
416  return theAnswer;
417 }
418 
419 bool CACFDictionary::AddFloat32(const CFStringRef inKey, Float32 inValue)
420 {
421  bool theAnswer = false;
422 
423  if(mMutable && (mCFDictionary != NULL))
424  {
425  CACFNumber theValue(inValue);
426  theAnswer = AddCFType(inKey, theValue.GetCFNumber());
427  }
428 
429  return theAnswer;
430 }
431 
432 bool CACFDictionary::AddFloat64(const CFStringRef inKey, Float64 inValue)
433 {
434  bool theAnswer = false;
435 
436  if(mMutable && (mCFDictionary != NULL))
437  {
438  CACFNumber theValue(inValue);
439  theAnswer = AddCFType(inKey, theValue.GetCFNumber());
440  }
441 
442  return theAnswer;
443 }
444 
445 bool CACFDictionary::AddNumber(const CFStringRef inKey, const CFNumberRef inValue)
446 {
447  bool theAnswer = false;
448 
449  if(mMutable && (mCFDictionary != NULL))
450  {
451  theAnswer = AddCFType(inKey, inValue);
452  }
453 
454  return theAnswer;
455 }
456 
457 bool CACFDictionary::AddString(const CFStringRef inKey, const CFStringRef inValue)
458 {
459  bool theAnswer = false;
460 
461  if(mMutable && (mCFDictionary != NULL))
462  {
463  theAnswer = AddCFType(inKey, inValue);
464  }
465 
466  return theAnswer;
467 }
468 
469 bool CACFDictionary::AddArray(const CFStringRef inKey, const CFArrayRef inValue)
470 {
471  bool theAnswer = false;
472 
473  if(mMutable && (mCFDictionary != NULL))
474  {
475  theAnswer = AddCFType(inKey, inValue);
476  }
477 
478  return theAnswer;
479 }
480 
481 bool CACFDictionary::AddDictionary(const CFStringRef inKey, const CFDictionaryRef inValue)
482 {
483  bool theAnswer = false;
484 
485  if(mMutable && (mCFDictionary != NULL))
486  {
487  theAnswer = AddCFType(inKey, inValue);
488  }
489 
490  return theAnswer;
491 }
492 
493 bool CACFDictionary::AddData(const CFStringRef inKey, const CFDataRef inValue)
494 {
495  bool theAnswer = false;
496 
497  if(mMutable && (mCFDictionary != NULL))
498  {
499  theAnswer = AddCFType(inKey, inValue);
500  }
501 
502  return theAnswer;
503 }
504 
505 bool CACFDictionary::AddCFType(const CFStringRef inKey, const CFTypeRef inValue)
506 {
507  bool theAnswer = false;
508 
509  if(mMutable && (mCFDictionary != NULL))
510  {
511  CFDictionarySetValue(mCFDictionary, inKey, inValue);
512  theAnswer = true;
513  }
514 
515  return theAnswer;
516 }
517 
518 bool CACFDictionary::AddURL(const CFStringRef inKey, const CFURLRef inValue)
519 {
520  bool theAnswer = false;
521 
522  if(mMutable && (mCFDictionary != NULL))
523  {
524  CFDictionarySetValue(mCFDictionary, inKey, inValue);
525  theAnswer = true;
526  }
527 
528  return theAnswer;
529 }
530 
531 bool CACFDictionary::AddCFTypeWithCStringKey(const char* inKey, const CFTypeRef inValue)
532 {
533  bool theAnswer = false;
534 
535  if(mMutable && (mCFDictionary != NULL))
536  {
537  CACFString theKey(inKey);
538  if(theKey.IsValid())
539  {
540  theAnswer = AddCFType(theKey.GetCFString(), inValue);
541  }
542  }
543 
544  return theAnswer;
545 }
546 
547 bool CACFDictionary::AddCString(const CFStringRef inKey, const char* inValue)
548 {
549  bool theAnswer = false;
550 
551  if(mMutable && (mCFDictionary != NULL))
552  {
553  CACFString theValue(inValue);
554  if(theValue.IsValid())
555  {
556  theAnswer = AddCFType(inKey, theValue.GetCFString());
557  }
558  }
559 
560  return theAnswer;
561 }