1 /* Copyright: © Copyright 2005 Apple Computer, Inc. All rights reserved.
3 Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
4 ("Apple") in consideration of your agreement to the following terms, and your
5 use, installation, modification or redistribution of this Apple software
6 constitutes acceptance of these terms. If you do not agree with these terms,
7 please do not use, install, modify or redistribute this Apple software.
9 In consideration of your agreement to abide by the following terms, and subject
10 to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs
11 copyrights in this original Apple software (the "Apple Software"), to use,
12 reproduce, modify and redistribute the Apple Software, with or without
13 modifications, in source and/or binary forms; provided that if you redistribute
14 the Apple Software in its entirety and without modifications, you must retain
15 this notice and the following text and disclaimers in all such redistributions of
16 the Apple Software. Neither the name, trademarks, service marks or logos of
17 Apple Computer, Inc. may be used to endorse or promote products derived from the
18 Apple Software without specific prior written permission from Apple. Except as
19 expressly stated in this notice, no other rights or licenses, express or implied,
20 are granted by Apple herein, including but not limited to any patent rights that
21 may be infringed by your derivative works or by other works in which the Apple
22 Software may be incorporated.
24 The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
25 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
26 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
28 COMBINATION WITH YOUR PRODUCTS.
30 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
31 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
32 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
34 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
35 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
36 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 /*=============================================================================
42 =============================================================================*/
44 //=============================================================================
46 //=============================================================================
49 #include "CACFDictionary.h"
51 // PublicUtility Includes
52 #include "CACFString.h"
53 #include "CACFNumber.h"
55 //=============================================================================
57 //=============================================================================
59 bool CACFDictionary::HasKey(const CFStringRef inKey
) const
61 return CFDictionaryContainsKey(mCFDictionary
, inKey
) != 0;
64 UInt32
CACFDictionary::Size () const
66 return CFDictionaryGetCount(mCFDictionary
);
69 void CACFDictionary::GetKeys (const void **keys
) const
71 CFDictionaryGetKeysAndValues(mCFDictionary
, keys
, NULL
);
74 bool CACFDictionary::GetBool(const CFStringRef inKey
, bool& outValue
) const
76 bool theAnswer
= false;
78 CFTypeRef theValue
= NULL
;
79 if(GetCFType(inKey
, theValue
))
81 if((theValue
!= NULL
) && (CFGetTypeID(theValue
) == CFBooleanGetTypeID()))
83 outValue
= CFBooleanGetValue(static_cast<CFBooleanRef
>(theValue
));
86 else if((theValue
!= NULL
) && (CFGetTypeID(theValue
) == CFNumberGetTypeID()))
88 SInt32 theNumericValue
= 0;
89 CFNumberGetValue(static_cast<CFNumberRef
>(theValue
), kCFNumberSInt32Type
, &theNumericValue
);
90 outValue
= theNumericValue
!= 0;
98 bool CACFDictionary::GetSInt32(const CFStringRef inKey
, SInt32
& outValue
) const
100 bool theAnswer
= false;
102 CFTypeRef theValue
= NULL
;
103 if(GetCFType(inKey
, theValue
))
105 if((theValue
!= NULL
) && (CFGetTypeID(theValue
) == CFNumberGetTypeID()))
107 CFNumberGetValue(static_cast<CFNumberRef
>(theValue
), kCFNumberSInt32Type
, &outValue
);
115 bool CACFDictionary::GetUInt32(const CFStringRef inKey
, UInt32
& outValue
) const
117 bool theAnswer
= false;
119 CFTypeRef theValue
= NULL
;
120 if(GetCFType(inKey
, theValue
))
122 if((theValue
!= NULL
) && (CFGetTypeID(theValue
) == CFNumberGetTypeID()))
124 CFNumberGetValue(static_cast<CFNumberRef
>(theValue
), kCFNumberSInt32Type
, &outValue
);
132 bool CACFDictionary::GetSInt64(const CFStringRef inKey
, SInt64
& outValue
) const
134 bool theAnswer
= false;
136 CFTypeRef theValue
= NULL
;
137 if(GetCFType(inKey
, theValue
))
139 if((theValue
!= NULL
) && (CFGetTypeID(theValue
) == CFNumberGetTypeID()))
141 CFNumberGetValue(static_cast<CFNumberRef
>(theValue
), kCFNumberSInt64Type
, &outValue
);
149 bool CACFDictionary::GetUInt64(const CFStringRef inKey
, UInt64
& outValue
) const
151 bool theAnswer
= false;
153 CFTypeRef theValue
= NULL
;
154 if(GetCFType(inKey
, theValue
))
156 if((theValue
!= NULL
) && (CFGetTypeID(theValue
) == CFNumberGetTypeID()))
158 CFNumberGetValue(static_cast<CFNumberRef
>(theValue
), kCFNumberSInt64Type
, &outValue
);
166 bool CACFDictionary::GetFloat32(const CFStringRef inKey
, Float32
& outValue
) const
168 bool theAnswer
= false;
170 CFTypeRef theValue
= NULL
;
171 if(GetCFType(inKey
, theValue
))
173 if((theValue
!= NULL
) && (CFGetTypeID(theValue
) == CFNumberGetTypeID()))
175 CFNumberGetValue(static_cast<CFNumberRef
>(theValue
), kCFNumberFloat32Type
, &outValue
);
183 bool CACFDictionary::GetFloat64(const CFStringRef inKey
, Float64
& outValue
) const
185 bool theAnswer
= false;
187 CFTypeRef theValue
= NULL
;
188 if(GetCFType(inKey
, theValue
))
190 if((theValue
!= NULL
) && (CFGetTypeID(theValue
) == CFNumberGetTypeID()))
192 CFNumberGetValue(static_cast<CFNumberRef
>(theValue
), kCFNumberFloat64Type
, &outValue
);
200 bool CACFDictionary::GetString(const CFStringRef inKey
, CFStringRef
& outValue
) const
202 bool theAnswer
= false;
204 CFTypeRef theValue
= NULL
;
205 if(GetCFType(inKey
, theValue
))
207 if((theValue
!= NULL
) && (CFGetTypeID(theValue
) == CFStringGetTypeID()))
209 outValue
= static_cast<CFStringRef
>(theValue
);
217 bool CACFDictionary::GetArray(const CFStringRef inKey
, CFArrayRef
& outValue
) const
219 bool theAnswer
= false;
221 CFTypeRef theValue
= NULL
;
222 if(GetCFType(inKey
, theValue
))
224 if((theValue
!= NULL
) && (CFGetTypeID(theValue
) == CFArrayGetTypeID()))
226 outValue
= static_cast<CFArrayRef
>(theValue
);
234 bool CACFDictionary::GetDictionary(const CFStringRef inKey
, CFDictionaryRef
& outValue
) const
236 bool theAnswer
= false;
238 CFTypeRef theValue
= NULL
;
239 if(GetCFType(inKey
, theValue
))
241 if((theValue
!= NULL
) && (CFGetTypeID(theValue
) == CFDictionaryGetTypeID()))
243 outValue
= static_cast<CFDictionaryRef
>(theValue
);
251 bool CACFDictionary::GetData(const CFStringRef inKey
, CFDataRef
& outValue
) const
253 bool theAnswer
= false;
255 CFTypeRef theValue
= NULL
;
256 if(GetCFType(inKey
, theValue
))
258 if((theValue
!= NULL
) && (CFGetTypeID(theValue
) == CFDataGetTypeID()))
260 outValue
= static_cast<CFDataRef
>(theValue
);
268 bool CACFDictionary::GetCFType(const CFStringRef inKey
, CFTypeRef
& outValue
) const
270 bool theAnswer
= false;
272 if(mCFDictionary
!= NULL
)
274 outValue
= CFDictionaryGetValue(mCFDictionary
, inKey
);
275 theAnswer
= (outValue
!= NULL
);
281 bool CACFDictionary::GetCFTypeWithCStringKey(const char* inKey
, CFTypeRef
& outValue
) const
283 bool theAnswer
= false;
285 if(mCFDictionary
!= NULL
)
287 CACFString
theKey(inKey
);
290 theAnswer
= GetCFType(theKey
.GetCFString(), outValue
);
297 bool CACFDictionary::AddSInt32(const CFStringRef inKey
, SInt32 inValue
)
299 bool theAnswer
= false;
301 if(mMutable
&& (mCFDictionary
!= NULL
))
303 CACFNumber
theValue(inValue
);
304 theAnswer
= AddCFType(inKey
, theValue
.GetCFNumber());
310 bool CACFDictionary::AddUInt32(const CFStringRef inKey
, UInt32 inValue
)
312 bool theAnswer
= false;
314 if(mMutable
&& (mCFDictionary
!= NULL
))
316 CACFNumber
theValue(inValue
);
317 theAnswer
= AddCFType(inKey
, theValue
.GetCFNumber());
323 bool CACFDictionary::AddSInt64(const CFStringRef inKey
, SInt64 inValue
)
325 bool theAnswer
= false;
327 if(mMutable
&& (mCFDictionary
!= NULL
))
329 CACFNumber
theValue(inValue
);
330 theAnswer
= AddCFType(inKey
, theValue
.GetCFNumber());
336 bool CACFDictionary::AddUInt64(const CFStringRef inKey
, UInt64 inValue
)
338 bool theAnswer
= false;
340 if(mMutable
&& (mCFDictionary
!= NULL
))
342 CACFNumber
theValue(inValue
);
343 theAnswer
= AddCFType(inKey
, theValue
.GetCFNumber());
349 bool CACFDictionary::AddFloat32(const CFStringRef inKey
, Float32 inValue
)
351 bool theAnswer
= false;
353 if(mMutable
&& (mCFDictionary
!= NULL
))
355 CACFNumber
theValue(inValue
);
356 theAnswer
= AddCFType(inKey
, theValue
.GetCFNumber());
362 bool CACFDictionary::AddFloat64(const CFStringRef inKey
, Float64 inValue
)
364 bool theAnswer
= false;
366 if(mMutable
&& (mCFDictionary
!= NULL
))
368 CACFNumber
theValue(inValue
);
369 theAnswer
= AddCFType(inKey
, theValue
.GetCFNumber());
375 bool CACFDictionary::AddNumber(const CFStringRef inKey
, const CFNumberRef inValue
)
377 bool theAnswer
= false;
379 if(mMutable
&& (mCFDictionary
!= NULL
))
381 theAnswer
= AddCFType(inKey
, inValue
);
387 bool CACFDictionary::AddString(const CFStringRef inKey
, const CFStringRef inValue
)
389 bool theAnswer
= false;
391 if(mMutable
&& (mCFDictionary
!= NULL
))
393 theAnswer
= AddCFType(inKey
, inValue
);
399 bool CACFDictionary::AddArray(const CFStringRef inKey
, const CFArrayRef inValue
)
401 bool theAnswer
= false;
403 if(mMutable
&& (mCFDictionary
!= NULL
))
405 theAnswer
= AddCFType(inKey
, inValue
);
411 bool CACFDictionary::AddDictionary(const CFStringRef inKey
, const CFDictionaryRef inValue
)
413 bool theAnswer
= false;
415 if(mMutable
&& (mCFDictionary
!= NULL
))
417 theAnswer
= AddCFType(inKey
, inValue
);
423 bool CACFDictionary::AddData(const CFStringRef inKey
, const CFDataRef inValue
)
425 bool theAnswer
= false;
427 if(mMutable
&& (mCFDictionary
!= NULL
))
429 theAnswer
= AddCFType(inKey
, inValue
);
435 bool CACFDictionary::AddCFType(const CFStringRef inKey
, const CFTypeRef inValue
)
437 bool theAnswer
= false;
439 if(mMutable
&& (mCFDictionary
!= NULL
))
441 CFDictionarySetValue(mCFDictionary
, inKey
, inValue
);
448 bool CACFDictionary::AddCFTypeWithCStringKey(const char* inKey
, const CFTypeRef inValue
)
450 bool theAnswer
= false;
452 if(mMutable
&& (mCFDictionary
!= NULL
))
454 CACFString
theKey(inKey
);
457 theAnswer
= AddCFType(theKey
.GetCFString(), inValue
);
464 bool CACFDictionary::AddCString(const CFStringRef inKey
, const char* inValue
)
466 bool theAnswer
= false;
468 if(mMutable
&& (mCFDictionary
!= NULL
))
470 CACFString
theValue(inValue
);
471 if(theValue
.IsValid())
473 theAnswer
= AddCFType(inKey
, theValue
.GetCFString());