Make sure MSVC knows which version of 'floor()' we want
[ardour.git] / libs / appleutility / CoreAudio / PublicUtility / CACFString.h
1 /*
2      File: CACFString.h
3  Abstract: Part of CoreAudio Utility Classes
4   Version: 1.1
5
6  Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple
7  Inc. ("Apple") in consideration of your agreement to the following
8  terms, and your use, installation, modification or redistribution of
9  this Apple software constitutes acceptance of these terms.  If you do
10  not agree with these terms, please do not use, install, modify or
11  redistribute this Apple software.
12
13  In consideration of your agreement to abide by the following terms, and
14  subject to these terms, Apple grants you a personal, non-exclusive
15  license, under Apple's copyrights in this original Apple software (the
16  "Apple Software"), to use, reproduce, modify and redistribute the Apple
17  Software, with or without modifications, in source and/or binary forms;
18  provided that if you redistribute the Apple Software in its entirety and
19  without modifications, you must retain this notice and the following
20  text and disclaimers in all such redistributions of the Apple Software.
21  Neither the name, trademarks, service marks or logos of Apple Inc. may
22  be used to endorse or promote products derived from the Apple Software
23  without specific prior written permission from Apple.  Except as
24  expressly stated in this notice, no other rights or licenses, express or
25  implied, are granted by Apple herein, including but not limited to any
26  patent rights that may be infringed by your derivative works or by other
27  works in which the Apple Software may be incorporated.
28
29  The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
30  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
31  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
32  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
33  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
34
35  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
36  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
39  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
40  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
41  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
42  POSSIBILITY OF SUCH DAMAGE.
43
44  Copyright (C) 2014 Apple Inc. All Rights Reserved.
45
46 */
47 #if !defined(__CACFString_h__)
48 #define __CACFString_h__
49
50 //=============================================================================
51 //      Includes
52 //=============================================================================
53
54 #include "CADebugMacros.h"
55
56 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
57         #include <CoreAudio/CoreAudioTypes.h>
58         #include <CoreFoundation/CFString.h>
59 #else
60         #include <CoreAudioTypes.h>
61         #include <CFString.h>
62 #endif
63
64 //=============================================================================
65 //      CACFString
66 //
67 //      Notes
68 //      -       Using the AssignWithoutRetain() method will fool the static analyzer into thinking that the
69 //              CFString being assigned will be leaked. This is because the static analyzer is not smart
70 //              enough to understand that the destructor will release the object.
71 //=============================================================================
72
73 class   CACFString
74 {
75 //      Construction/Destruction
76 public:
77                                                 CACFString() : mCFString(NULL), mWillRelease(true) {}
78         explicit                        CACFString(CFStringRef inCFString) : mCFString(inCFString), mWillRelease(true) {}
79                                                 CACFString(const char* inCString) : mCFString(CFStringCreateWithCString(NULL, inCString, kCFStringEncodingASCII)), mWillRelease(true) {}
80                                                 CACFString(CFStringRef inCFString, bool inWillRelease) : mCFString(inCFString), mWillRelease(inWillRelease) {}
81                                                 CACFString(const char* inCString, bool inWillRelease) : mCFString(CFStringCreateWithCString(NULL, inCString, kCFStringEncodingASCII)), mWillRelease(inWillRelease) {}
82                                                 CACFString(const char* inCString, CFStringEncoding inCStringEncoding, bool inWillRelease = true) : mCFString(CFStringCreateWithCString(NULL, inCString, inCStringEncoding)), mWillRelease(inWillRelease) {}
83                                                 ~CACFString() { Release(); }
84                                                 CACFString(const CACFString& inString) : mCFString(inString.mCFString), mWillRelease(inString.mWillRelease) { Retain(); }
85         CACFString&                     operator=(const CACFString& inString) { if (inString.mCFString != mCFString) { Release(); mCFString = inString.mCFString; mWillRelease = inString.mWillRelease; Retain(); } return *this; }
86         CACFString&                     operator=(CFStringRef inCFString) { if (inCFString != mCFString) { Release(); mCFString = inCFString; } mWillRelease = true; Retain(); return *this; }
87         void                            AssignWithoutRetain(CFStringRef inCFString) { if (inCFString != mCFString) { Release(); mCFString = inCFString; } mWillRelease = true; }
88
89 private:
90         void                            Retain() { if(mWillRelease && (mCFString != NULL)) { CFRetain(mCFString); } }
91         void                            Release() { if(mWillRelease && (mCFString != NULL)) { CFRelease(mCFString); } }
92
93         CFStringRef                     mCFString;
94         bool                            mWillRelease;
95
96 //      Operations
97 public:
98         void                            AllowRelease() { mWillRelease = true; }
99         void                            DontAllowRelease() { mWillRelease = false; }
100         bool                            IsValid() const { return mCFString != NULL; }
101         bool                            IsEqualTo(CFStringRef inString) const { bool theAnswer = false; if(mCFString != NULL) { theAnswer = CFStringCompare(inString, mCFString, 0) == kCFCompareEqualTo; } return theAnswer; }
102         bool                            StartsWith(CFStringRef inString) const { bool theAnswer = false; if(mCFString != NULL) { theAnswer = CFStringHasPrefix(mCFString, inString); } return theAnswer; }
103         bool                            EndsWith(CFStringRef inString) const { bool theAnswer = false; if(mCFString != NULL) { theAnswer = CFStringHasSuffix(mCFString, inString); } return theAnswer; }
104
105 //      Value Access
106 public:
107         CFStringRef                     GetCFString() const { return mCFString; }
108         CFStringRef                     CopyCFString() const { if(mCFString != NULL) { CFRetain(mCFString); } return mCFString; }
109         const CFStringRef*      GetPointerToStorage() const     { return &mCFString; }
110         CFStringRef&            GetStorage() { Release(); mWillRelease = true; return mCFString; }
111         UInt32                          GetLength() const { UInt32 theAnswer = 0; if(mCFString != NULL) { theAnswer = ToUInt32(CFStringGetLength(mCFString)); } return theAnswer; }
112         UInt32                          GetByteLength(CFStringEncoding inEncoding = kCFStringEncodingUTF8) const { UInt32 theAnswer = 0; if(mCFString != NULL) { theAnswer = GetStringByteLength(mCFString, inEncoding); } return theAnswer; }
113         void                            GetCString(char* outString, UInt32& ioStringSize, CFStringEncoding inEncoding = kCFStringEncodingUTF8) const { GetCString(mCFString, outString, ioStringSize, inEncoding); }
114         void                            GetUnicodeString(UInt16* outString, UInt32& ioStringSize) const { GetUnicodeString(mCFString, outString, ioStringSize); }
115         SInt32                          GetAsInteger() { return GetAsInteger(mCFString); }
116         Float64                         GetAsFloat64() { return GetAsFloat64(mCFString); }
117
118         static UInt32           GetStringLength(CFStringRef inCFString)  { UInt32 theAnswer = 0; if(inCFString != NULL) { theAnswer = ToUInt32(CFStringGetLength(inCFString)); } return theAnswer; }
119         static UInt32           GetStringByteLength(CFStringRef inCFString, CFStringEncoding inEncoding = kCFStringEncodingUTF8);
120         static void                     GetCString(CFStringRef inCFString, char* outString, UInt32& ioStringSize, CFStringEncoding inEncoding = kCFStringEncodingUTF8);
121         static void                     GetUnicodeString(CFStringRef inCFString, UInt16* outString, UInt32& ioStringSize);
122         static SInt32           GetAsInteger(CFStringRef inCFString) { SInt32 theAnswer = 0; if(inCFString != NULL){ theAnswer = CFStringGetIntValue(inCFString); } return theAnswer; }
123         static Float64          GetAsFloat64(CFStringRef inCFString) { Float64 theAnswer = 0; if(inCFString != NULL){ theAnswer = CFStringGetDoubleValue(inCFString); } return theAnswer; }
124
125 };
126
127 inline bool     operator<(const CACFString& x, const CACFString& y) { return CFStringCompare(x.GetCFString(), y.GetCFString(), 0) == kCFCompareLessThan; }
128 inline bool     operator==(const CACFString& x, const CACFString& y) { return CFStringCompare(x.GetCFString(), y.GetCFString(), 0) == kCFCompareEqualTo; }
129 inline bool     operator!=(const CACFString& x, const CACFString& y) { return !(x == y); }
130 inline bool     operator<=(const CACFString& x, const CACFString& y) { return (x < y) || (x == y); }
131 inline bool     operator>=(const CACFString& x, const CACFString& y) { return !(x < y); }
132 inline bool     operator>(const CACFString& x, const CACFString& y) { return !((x < y) || (x == y)); }
133
134 inline bool     operator<(const CACFString& x, CFStringRef y) { return CFStringCompare(x.GetCFString(), y, 0) == kCFCompareLessThan; }
135 inline bool     operator==(const CACFString& x, CFStringRef y) { return CFStringCompare(x.GetCFString(), y, 0) == kCFCompareEqualTo; }
136 inline bool     operator!=(const CACFString& x, CFStringRef y) { return !(x == y); }
137 inline bool     operator<=(const CACFString& x, CFStringRef y) { return (x < y) || (x == y); }
138 inline bool     operator>=(const CACFString& x, CFStringRef y) { return !(x < y); }
139 inline bool     operator>(const CACFString& x, CFStringRef y) { return !((x < y) || (x == y)); }
140
141 inline bool     operator<(CFStringRef x, const CACFString& y) { return CFStringCompare(x, y.GetCFString(), 0) == kCFCompareLessThan; }
142 inline bool     operator==(CFStringRef x, const CACFString& y) { return CFStringCompare(x, y.GetCFString(), 0) == kCFCompareEqualTo; }
143 inline bool     operator!=(CFStringRef x, const CACFString& y) { return !(x == y); }
144 inline bool     operator<=(CFStringRef x, const CACFString& y) { return (x < y) || (x == y); }
145 inline bool     operator>=(CFStringRef x, const CACFString& y) { return !(x < y); }
146 inline bool     operator>(CFStringRef x, const CACFString& y) { return !((x < y) || (x == y)); }
147
148 //=============================================================================
149 //      CACFMutableString
150 //=============================================================================
151
152 class   CACFMutableString
153 {
154 //      Construction/Destruction
155 public:
156                                                 CACFMutableString() : mCFMutableString(NULL), mWillRelease(true) {}
157                                                 CACFMutableString(CFMutableStringRef inCFMutableString, bool inWillRelease = true) : mCFMutableString(inCFMutableString), mWillRelease(inWillRelease) {}
158                                                 CACFMutableString(CFStringRef inStringToCopy, bool /*inMakeCopy*/, bool inWillRelease = true) : mCFMutableString(CFStringCreateMutableCopy(NULL, 0, inStringToCopy)), mWillRelease(inWillRelease) {}
159                                                 CACFMutableString(const char* inCString, bool inWillRelease = true) : mCFMutableString(NULL), mWillRelease(inWillRelease) { CACFString theString(inCString); mCFMutableString = CFStringCreateMutableCopy(NULL, 0, theString.GetCFString()); }
160                                                 CACFMutableString(const char* inCString, CFStringEncoding inCStringEncoding, bool inWillRelease = true) : mCFMutableString(NULL), mWillRelease(inWillRelease) { CACFString theString(inCString, inCStringEncoding); mCFMutableString = CFStringCreateMutableCopy(NULL, 0, theString.GetCFString()); }
161                                                 ~CACFMutableString() { Release(); }
162                                                 CACFMutableString(const CACFMutableString& inString) : mCFMutableString(inString.mCFMutableString), mWillRelease(inString.mWillRelease) { Retain(); }
163         CACFMutableString&      operator=(const CACFMutableString& inString) { Release(); mCFMutableString = inString.mCFMutableString; mWillRelease = inString.mWillRelease; Retain(); return *this; }
164         CACFMutableString&      operator=(CFMutableStringRef inCFMutableString) { Release(); mCFMutableString = inCFMutableString; mWillRelease = true; return *this; }
165
166 private:
167         void                            Retain() { if(mWillRelease && (mCFMutableString != NULL)) { CFRetain(mCFMutableString); } }
168         void                            Release() { if(mWillRelease && (mCFMutableString != NULL)) { CFRelease(mCFMutableString); } }
169
170         CFMutableStringRef      mCFMutableString;
171         bool                            mWillRelease;
172
173 //      Operations
174 public:
175         void                            AllowRelease() { mWillRelease = true; }
176         void                            DontAllowRelease() { mWillRelease = false; }
177         bool                            IsValid() { return mCFMutableString != NULL; }
178         bool                            IsEqualTo(CFStringRef inString) const { bool theAnswer = false; if(mCFMutableString != NULL) { theAnswer = CFStringCompare(inString, mCFMutableString, 0) == kCFCompareEqualTo; } return theAnswer; }
179         bool                            StartsWith(CFStringRef inString) const { bool theAnswer = false; if(mCFMutableString != NULL) { theAnswer = CFStringHasPrefix(mCFMutableString, inString); } return theAnswer; }
180         bool                            EndsWith(CFStringRef inString) const { bool theAnswer = false; if(mCFMutableString != NULL) { theAnswer = CFStringHasSuffix(mCFMutableString, inString); } return theAnswer; }
181         void                            Append(CFStringRef inString) { if(mCFMutableString != NULL) { CFStringAppend(mCFMutableString, inString); } }
182
183 //      Value Access
184 public:
185         CFMutableStringRef      GetCFMutableString() const { return mCFMutableString; }
186         CFMutableStringRef      CopyCFMutableString() const { if(mCFMutableString != NULL) { CFRetain(mCFMutableString); } return mCFMutableString; }
187         UInt32                          GetLength() const { UInt32 theAnswer = 0; if(mCFMutableString != NULL) { theAnswer = ToUInt32(CFStringGetLength(mCFMutableString)); } return theAnswer; }
188         UInt32                          GetByteLength(CFStringEncoding inEncoding = kCFStringEncodingUTF8) const { UInt32 theAnswer = 0; if(mCFMutableString != NULL) { theAnswer = CACFString::GetStringByteLength(mCFMutableString, inEncoding); } return theAnswer; }
189         void                            GetCString(char* outString, UInt32& ioStringSize, CFStringEncoding inEncoding = kCFStringEncodingUTF8) const { CACFString::GetCString(mCFMutableString, outString, ioStringSize, inEncoding); }
190         void                            GetUnicodeString(UInt16* outString, UInt32& ioStringSize) const { CACFString::GetUnicodeString(mCFMutableString, outString, ioStringSize); }
191         SInt32                          GetAsInteger() { return CACFString::GetAsInteger(mCFMutableString); }
192         Float64                         GetAsFloat64() { return CACFString::GetAsFloat64(mCFMutableString); }
193
194 };
195
196 #endif