Jamoma API  0.6.0.a19
CACFArray.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(__CACFArray_h__)
42 #define __CACFArray_h__
43 
44 //=============================================================================
45 // Includes
46 //=============================================================================
47 
48 // System Includes
49 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
50  #include <CoreAudio/CoreAudioTypes.h>
51  #include <CoreFoundation/CoreFoundation.h>
52 #else
53  #include <CoreAudioTypes.h>
54  #include <CoreFoundation.h>
55 #endif
56 
57 #include "CADebugMacros.h"
58 
59 //=============================================================================
60 // Types
61 //=============================================================================
62 
63 class CACFDictionary;
64 class CACFString;
65 
66 //=============================================================================
67 // CACFArray
68 //=============================================================================
69 
70 class CACFArray
71 {
72 
73 // Construction/Destruction
74 public:
75  CACFArray(bool inRelease = true) : mCFArray(CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks)), mRelease(inRelease), mMutable(true) {}
76  CACFArray(UInt32 inMaxNumberItems, bool inRelease) : mCFArray(CFArrayCreateMutable(NULL, inMaxNumberItems, &kCFTypeArrayCallBacks)), mRelease(inRelease), mMutable(true) {}
77  CACFArray(CFArrayRef inCFArray, bool inRelease) : mCFArray(const_cast<CFMutableArrayRef>(inCFArray)), mRelease(inRelease), mMutable(false) {}
78  CACFArray(CFMutableArrayRef inCFArray, bool inRelease) : mCFArray(inCFArray), mRelease(inRelease), mMutable(true) {}
79  CACFArray(const CACFArray& inArray) : mCFArray(inArray.mCFArray), mRelease(inArray.mRelease), mMutable(inArray.mMutable) { Retain(); }
80  CACFArray& operator=(const CACFArray& inArray) { Release(); mCFArray = inArray.mCFArray; mRelease = inArray.mRelease; mMutable = inArray.mMutable; Retain(); return *this; }
81  CACFArray& operator=(CFArrayRef inCFArray) { Release(); mCFArray = const_cast<CFMutableArrayRef>(inCFArray); mMutable = false; Retain(); return *this; }
82  CACFArray& operator=(CFMutableArrayRef inCFArray) { Release(); mCFArray = inCFArray; mMutable = true; Retain(); return *this; }
83  ~CACFArray() { Release(); }
84 
85 private:
86  void Retain() { if(mRelease && (mCFArray != NULL)) { CFRetain(mCFArray); } }
87  void Release() { if(mRelease && (mCFArray != NULL)) { CFRelease(mCFArray); } }
88 
89 // Attributes
90 public:
91  bool IsValid() const { return mCFArray != NULL; }
92  bool IsMutable() const { return mMutable; }
93  bool CanModify() const { return mMutable && (mCFArray != NULL); }
94 
95  bool WillRelease() const { return mRelease; }
96  void ShouldRelease(bool inRelease) { mRelease = inRelease; }
97 
98  CFTypeID GetTypeID() const { return CFGetTypeID(mCFArray); }
99 
100  CFArrayRef GetCFArray() const { return mCFArray; }
101  CFArrayRef CopyCFArray() const { if(mCFArray != NULL) { CFRetain(mCFArray); } return mCFArray; }
102 
103  CFMutableArrayRef GetCFMutableArray() const { return mCFArray; }
104  CFMutableArrayRef CopyCFMutableArray() const { if(mCFArray != NULL) { CFRetain(mCFArray); } return mCFArray; }
105  CFPropertyListRef AsPropertyList() const { return mCFArray; }
106 
107  void SetCFMutableArrayFromCopy(CFArrayRef inArray, bool inRelease = true) { Release(); mCFArray = CFArrayCreateMutableCopy(NULL, 0, inArray); mMutable = true; mRelease = inRelease; }
108 
109 // Item Operations
110 public:
111  UInt32 GetNumberItems() const { UInt32 theAnswer = 0; if(mCFArray != NULL) { theAnswer = ToUInt32(CFArrayGetCount(mCFArray)); } return theAnswer; }
112  bool HasItem(const void* inItem) const;
113  void RemoveItem(const void* inItem) { UInt32 theIndex; if(CanModify() && GetIndexOfItem(inItem, theIndex)) { RemoveItemAtIndex(theIndex); } }
114  bool GetIndexOfItem(const void* inItem, UInt32& outIndex) const;
115  void RemoveItemAtIndex(UInt32 inIndex) { if(CanModify()) { CFArrayRemoveValueAtIndex(mCFArray, inIndex); } }
116  void Clear() { if(CanModify()) { CFArrayRemoveAllValues(mCFArray); } }
117  void Sort(CFComparatorFunction inCompareFunction) { if(CanModify()) { CFRange theRange = { 0, CFArrayGetCount(mCFArray) }; CFArraySortValues(mCFArray, theRange, inCompareFunction, NULL); } }
118  void SortNumbers() { Sort((CFComparatorFunction)CFNumberCompare); }
119  void SortStrings() { Sort((CFComparatorFunction)CFStringCompare); }
120 
121  bool GetBool(UInt32 inIndex, bool& outValue) const;
122  bool GetSInt32(UInt32 inIndex, SInt32& outItem) const;
123  bool GetUInt32(UInt32 inIndex, UInt32& outItem) const;
124  bool GetSInt64(UInt32 inIndex, SInt64& outItem) const;
125  bool GetUInt64(UInt32 inIndex, UInt64& outItem) const;
126  bool GetFloat32(UInt32 inIndex, Float32& outItem) const;
127  bool GetFloat64(UInt32 inIndex, Float64& outItem) const;
128  bool GetString(UInt32 inIndex, CFStringRef& outItem) const;
129  bool GetArray(UInt32 inIndex, CFArrayRef& outItem) const;
130  bool GetDictionary(UInt32 inIndex, CFDictionaryRef& outItem) const;
131  bool GetData(UInt32 inIndex, CFDataRef& outItem) const;
132  bool GetUUID(UInt32 inIndex, CFUUIDRef& outItem) const;
133  bool GetCFType(UInt32 inIndex, CFTypeRef& outItem) const;
134 
135  void GetCACFString(UInt32 inIndex, CACFString& outItem) const;
136  void GetCACFArray(UInt32 inIndex, CACFArray& outItem) const;
137  void GetCACFDictionary(UInt32 inIndex, CACFDictionary& outItem) const;
138 
139  bool AppendBool(bool inItem);
140  bool AppendSInt32(SInt32 inItem);
141  bool AppendUInt32(UInt32 inItem);
142  bool AppendSInt64(SInt64 inItem);
143  bool AppendUInt64(UInt64 inItem);
144  bool AppendFloat32(Float32 inItem);
145  bool AppendFloat64(Float64 inItem);
146  bool AppendString(const CFStringRef inItem);
147  bool AppendArray(const CFArrayRef inItem);
148  bool AppendDictionary(const CFDictionaryRef inItem);
149  bool AppendData(const CFDataRef inItem);
150  bool AppendCFType(const CFTypeRef inItem);
151 
152  bool InsertBool(UInt32 inIndex, bool inItem);
153  bool InsertSInt32(UInt32 inIndex, SInt32 inItem);
154  bool InsertUInt32(UInt32 inIndex, UInt32 inItem);
155  bool InsertSInt64(UInt32 inIndex, SInt64 inItem);
156  bool InsertUInt64(UInt32 inIndex, UInt64 inItem);
157  bool InsertFloat32(UInt32 inIndex, Float32 inItem);
158  bool InsertFloat64(UInt32 inIndex, Float64 inItem);
159  bool InsertString(UInt32 inIndex, const CFStringRef inItem);
160  bool InsertArray(UInt32 inIndex, const CFArrayRef inItem);
161  bool InsertDictionary(UInt32 inIndex, const CFDictionaryRef inItem);
162  bool InsertData(UInt32 inIndex, const CFDataRef inItem);
163  bool InsertCFType(UInt32 inIndex, const CFTypeRef inItem);
164 
165  bool SetBool(UInt32 inIndex, bool inItem);
166  bool SetSInt32(UInt32 inIndex, SInt32 inItem);
167  bool SetUInt32(UInt32 inIndex, UInt32 inItem);
168  bool SetSInt64(UInt32 inIndex, SInt64 inItem);
169  bool SetUInt64(UInt32 inIndex, UInt64 inItem);
170  bool SetFloat32(UInt32 inIndex, Float32 inItem);
171  bool SetFloat64(UInt32 inIndex, Float64 inItem);
172  bool SetString(UInt32 inIndex, const CFStringRef inItem);
173  bool SetArray(UInt32 inIndex, const CFArrayRef inItem);
174  bool SetDictionary(UInt32 inIndex, const CFDictionaryRef inItem);
175  bool SetData(UInt32 inIndex, const CFDataRef inItem);
176  bool SetCFType(UInt32 inIndex, const CFTypeRef inItem);
177 
178 // Implementation
179 private:
180  CFMutableArrayRef mCFArray;
181  bool mRelease;
182  bool mMutable;
183 
184 };
185 
186 #endif