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