Jamoma API  0.6.0.a19
CACFDictionary.h
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 #if !defined(__CACFDictionary_h__)
42 #define __CACFDictionary_h__
43 
44 //=============================================================================
45 // Includes
46 //=============================================================================
47 
48 // System Includes
49 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
50  #include <CoreFoundation/CoreFoundation.h>
51 #else
52  #include <CoreFoundation.h>
53 #endif
54 
55 //=============================================================================
56 // Types
57 //=============================================================================
58 
59 class CACFArray;
60 class CACFString;
61 
62 //=============================================================================
63 // CACFDictionary
64 //=============================================================================
65 
66 class CACFDictionary
67 {
68 
69 // Construction/Destruction
70 public:
71  CACFDictionary(bool inRelease = true) : mCFDictionary(CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)), mRelease(inRelease), mMutable(true) {}
72  CACFDictionary(CFDictionaryRef inCFDictionary, bool inRelease) : mCFDictionary(const_cast<CFMutableDictionaryRef>(inCFDictionary)), mRelease(inRelease), mMutable(false) {}
73  CACFDictionary(CFMutableDictionaryRef inCFDictionary, bool inRelease) : mCFDictionary(inCFDictionary), mRelease(inRelease), mMutable(true) {}
74  CACFDictionary(const CACFDictionary& inDictionary) : mCFDictionary(inDictionary.mCFDictionary), mRelease(inDictionary.mRelease), mMutable(inDictionary.mMutable) { Retain(); }
75  CACFDictionary& operator=(const CACFDictionary& inDictionary) { Release(); mCFDictionary = inDictionary.mCFDictionary; mRelease = inDictionary.mRelease; mMutable = inDictionary.mMutable; Retain(); return *this; }
76  CACFDictionary& operator=(CFDictionaryRef inDictionary) { Release(); mCFDictionary = const_cast<CFMutableDictionaryRef>(inDictionary); mMutable = false; Retain(); return *this; }
77  CACFDictionary& operator=(CFMutableDictionaryRef inDictionary) { Release(); mCFDictionary = inDictionary; mMutable = true; Retain(); return *this; }
78  ~CACFDictionary() { Release(); }
79 
80 private:
81  void Retain() { if(mRelease && (mCFDictionary != NULL)) { CFRetain(mCFDictionary); } }
82  void Release() { if(mRelease && (mCFDictionary != NULL)) { CFRelease(mCFDictionary); } }
83 
84 // Attributes
85 public:
86  bool IsValid() const { return mCFDictionary != NULL; }
87  bool IsMutable() const { return mMutable;}
88  bool CanModify() const { return mMutable && (mCFDictionary != NULL); }
89 
90  bool WillRelease() const { return mRelease; }
91  void ShouldRelease(bool inRelease) { mRelease = inRelease; }
92 
93  CFDictionaryRef GetDict() const { return mCFDictionary; }
94  CFDictionaryRef GetCFDictionary() const { return mCFDictionary; }
95  CFDictionaryRef CopyCFDictionary() const { if(mCFDictionary != NULL) { CFRetain(mCFDictionary); } return mCFDictionary; }
96 
97  CFMutableDictionaryRef GetMutableDict() { return mCFDictionary; }
98  CFMutableDictionaryRef GetCFMutableDictionary() const { return mCFDictionary; }
99  CFMutableDictionaryRef CopyCFMutableDictionary() const { if(mCFDictionary != NULL) { CFRetain(mCFDictionary); } return mCFDictionary; }
100  void SetCFMutableDictionaryFromCopy(CFDictionaryRef inDictionary, bool inRelease = true) { Release(); mCFDictionary = CFDictionaryCreateMutableCopy(NULL, 0, inDictionary); mMutable = true; mRelease = inRelease; }
101  void SetCFMutableDictionaryToEmpty(bool inRelease = true) { Release(); mCFDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); mMutable = true; mRelease = inRelease; }
102 
103  CFPropertyListRef AsPropertyList() const { return mCFDictionary; }
104  OSStatus GetDictIfMutable(CFMutableDictionaryRef& outDict) const { OSStatus theAnswer = -1; if(mMutable) { outDict = mCFDictionary; theAnswer = 0; } return theAnswer; }
105 
106 // Item Operations
107 public:
108  bool HasKey(const CFStringRef inKey) const;
109  UInt32 Size() const;
110  void GetKeys(const void** keys) const;
111 
112  bool GetBool(const CFStringRef inKey, bool& outValue) const;
113  bool GetSInt32(const CFStringRef inKey, SInt32& outValue) const;
114  bool GetUInt32(const CFStringRef inKey, UInt32& outValue) const;
115  bool GetSInt64(const CFStringRef inKey, SInt64& outValue) const;
116  bool GetUInt64(const CFStringRef inKey, UInt64& outValue) const;
117  bool GetFloat32(const CFStringRef inKey, Float32& outValue) const;
118  bool GetFloat64(const CFStringRef inKey, Float64& outValue) const;
119  bool GetFixed64(const CFStringRef inKey, Float64& outValue) const;
120  bool GetString(const CFStringRef inKey, CFStringRef& outValue) const;
121  bool GetArray(const CFStringRef inKey, CFArrayRef& outValue) const;
122  bool GetDictionary(const CFStringRef inKey, CFDictionaryRef& outValue) const;
123  bool GetData(const CFStringRef inKey, CFDataRef& outValue) const;
124  bool GetCFType(const CFStringRef inKey, CFTypeRef& outValue) const;
125 
126  bool GetCFTypeWithCStringKey(const char* inKey, CFTypeRef& outValue) const;
127 
128  void GetCACFString(const CFStringRef inKey, CACFString& outItem) const;
129  void GetCACFArray(const CFStringRef inKey, CACFArray& outItem) const;
130  void GetCACFDictionary(const CFStringRef inKey, CACFDictionary& outItem) const;
131 
132  bool AddBool(const CFStringRef inKey, bool inValue);
133  bool AddSInt32(const CFStringRef inKey, SInt32 inValue);
134  bool AddUInt32(const CFStringRef inKey, UInt32 inValue);
135  bool AddSInt64(const CFStringRef inKey, SInt64 inValue);
136  bool AddUInt64(const CFStringRef inKey, UInt64 inValue);
137  bool AddFloat32(const CFStringRef inKey, Float32 inValue);
138  bool AddFloat64(const CFStringRef inKey, Float64 inValue);
139  bool AddNumber(const CFStringRef inKey, const CFNumberRef inValue);
140  bool AddString(const CFStringRef inKey, const CFStringRef inValue);
141  bool AddArray(const CFStringRef inKey, const CFArrayRef inValue);
142  bool AddDictionary(const CFStringRef inKey, const CFDictionaryRef inValue);
143  bool AddData(const CFStringRef inKey, const CFDataRef inValue);
144  bool AddCFType(const CFStringRef inKey, const CFTypeRef inValue);
145  bool AddURL(const CFStringRef inKey, const CFURLRef inValue);
146 
147  bool AddCFTypeWithCStringKey(const char* inKey, const CFTypeRef inValue);
148  bool AddCString(const CFStringRef inKey, const char* inValue);
149 
150  void RemoveKey(const CFStringRef inKey) { if(CanModify()) { CFDictionaryRemoveValue(mCFDictionary, inKey); } }
151  void Clear() { if(CanModify()) { CFDictionaryRemoveAllValues(mCFDictionary); } }
152 
153  void Show() { CFShow(mCFDictionary); }
154 
155 // Implementation
156 private:
157  CFMutableDictionaryRef mCFDictionary;
158  bool mRelease;
159  bool mMutable;
160 };
161 
162 #endif //__CACFDictionary_h__