1 /*
2      File: CACFNumber.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(__CACFNumber_h__)
48 #define __CACFNumber_h__
49 
50 //=============================================================================
51 //	Includes
52 //=============================================================================
53 
54 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
55 	#include <CoreAudio/CoreAudioTypes.h>
56 	#include <CoreFoundation/CFNumber.h>
57 #else
58 	#include <CoreAudioTypes.h>
59 	#include <CFNumber.h>
60 #endif
61 
62 //=============================================================================
63 //	CACFBoolean
64 //=============================================================================
65 
66 class	CACFBoolean
67 {
68 //	Construction/Destruction
69 public:
CACFBoolean(CFBooleanRef inCFBoolean)70 	explicit		CACFBoolean(CFBooleanRef inCFBoolean) : mCFBoolean(inCFBoolean), mWillRelease(true) {}
CACFBoolean(CFBooleanRef inCFBoolean,bool inWillRelease)71 					CACFBoolean(CFBooleanRef inCFBoolean, bool inWillRelease) : mCFBoolean(inCFBoolean), mWillRelease(inWillRelease) {}
CACFBoolean(bool inValue)72 	explicit		CACFBoolean(bool inValue) : mCFBoolean(inValue ? kCFBooleanTrue : kCFBooleanFalse), mWillRelease(true) { Retain(); }
~CACFBoolean()73 					~CACFBoolean() { Release(); }
CACFBoolean(const CACFBoolean & inBoolean)74 					CACFBoolean(const CACFBoolean& inBoolean) : mCFBoolean(inBoolean.mCFBoolean), mWillRelease(inBoolean.mWillRelease) { Retain(); }
75 	CACFBoolean&	operator=(const CACFBoolean& inBoolean) { Release(); mCFBoolean = inBoolean.mCFBoolean; mWillRelease = inBoolean.mWillRelease; Retain(); return *this; }
76 	CACFBoolean&	operator=(CFBooleanRef inCFBoolean) { Release(); mCFBoolean = inCFBoolean; mWillRelease = true; return *this; }
77 
78 private:
Retain()79 	void			Retain() { if(mWillRelease && (mCFBoolean != NULL)) { CFRetain(mCFBoolean); } }
Release()80 	void			Release() { if(mWillRelease && (mCFBoolean != NULL)) { CFRelease(mCFBoolean); } }
81 
82 	CFBooleanRef	mCFBoolean;
83 	bool			mWillRelease;
84 
85 //	Operations
86 public:
AllowRelease()87 	void			AllowRelease() { mWillRelease = true; }
DontAllowRelease()88 	void			DontAllowRelease() { mWillRelease = false; }
IsValid()89 	bool			IsValid() { return mCFBoolean != NULL; }
90 
91 //	Value Access
92 public:
GetCFBoolean()93 	CFBooleanRef	GetCFBoolean() const { return mCFBoolean; }
CopyCFBoolean()94 	CFBooleanRef	CopyCFBoolean() const { if(mCFBoolean != NULL) { CFRetain(mCFBoolean); } return mCFBoolean; }
95 
GetBoolean()96 	bool			GetBoolean() const { bool theAnswer = false; if(mCFBoolean != NULL) { theAnswer = CFEqual(mCFBoolean, kCFBooleanTrue); } return theAnswer; }
97 
98 					CACFBoolean(const void*);	// prevent accidental instantiation with a pointer via bool constructor
99 };
100 
101 //=============================================================================
102 //	CACFNumber
103 //=============================================================================
104 
105 class	CACFNumber
106 {
107 //	Construction/Destruction
108 public:
CACFNumber(CFNumberRef inCFNumber)109 	explicit	CACFNumber(CFNumberRef inCFNumber) : mCFNumber(inCFNumber), mWillRelease(true) {}
CACFNumber(CFNumberRef inCFNumber,bool inWillRelease)110 				CACFNumber(CFNumberRef inCFNumber, bool inWillRelease) : mCFNumber(inCFNumber), mWillRelease(inWillRelease) {}
CACFNumber(SInt32 inValue)111 				CACFNumber(SInt32 inValue) : mCFNumber(CFNumberCreate(NULL, kCFNumberSInt32Type, &inValue)), mWillRelease(true) {}
CACFNumber(UInt32 inValue)112 				CACFNumber(UInt32 inValue) : mCFNumber(CFNumberCreate(NULL, kCFNumberSInt32Type, &inValue)), mWillRelease(true) {}
CACFNumber(SInt64 inValue)113 				CACFNumber(SInt64 inValue) : mCFNumber(CFNumberCreate(NULL, kCFNumberSInt64Type, &inValue)), mWillRelease(true) {}
CACFNumber(UInt64 inValue)114 				CACFNumber(UInt64 inValue) : mCFNumber(CFNumberCreate(NULL, kCFNumberSInt64Type, &inValue)), mWillRelease(true) {}
CACFNumber(Float32 inValue)115 				CACFNumber(Float32 inValue) : mCFNumber(CFNumberCreate(NULL, kCFNumberFloat32Type, &inValue)), mWillRelease(true) {}
CACFNumber(Float64 inValue)116 				CACFNumber(Float64 inValue) : mCFNumber(CFNumberCreate(NULL, kCFNumberFloat64Type, &inValue)), mWillRelease(true) {}
~CACFNumber()117 				~CACFNumber() { Release(); }
CACFNumber(const CACFNumber & inNumber)118 				CACFNumber(const CACFNumber& inNumber) : mCFNumber(inNumber.mCFNumber), mWillRelease(inNumber.mWillRelease) { Retain(); }
119 	CACFNumber&	operator=(const CACFNumber& inNumber) { Release(); mCFNumber = inNumber.mCFNumber; mWillRelease = inNumber.mWillRelease; Retain(); return *this; }
120 	CACFNumber&	operator=(CFNumberRef inCFNumber) { Release(); mCFNumber = inCFNumber; mWillRelease = true; return *this; }
121 
122 private:
Retain()123 	void		Retain() { if(mWillRelease && (mCFNumber != NULL)) { CFRetain(mCFNumber); } }
Release()124 	void		Release() { if(mWillRelease && (mCFNumber != NULL)) { CFRelease(mCFNumber); } }
125 
126 	CFNumberRef	mCFNumber;
127 	bool		mWillRelease;
128 
129 //	Operations
130 public:
AllowRelease()131 	void		AllowRelease() { mWillRelease = true; }
DontAllowRelease()132 	void		DontAllowRelease() { mWillRelease = false; }
IsValid()133 	bool		IsValid() const { return mCFNumber != NULL; }
134 
135 //	Value Access
136 public:
GetCFNumber()137 	CFNumberRef	GetCFNumber() const { return mCFNumber; }
CopyCFNumber()138 	CFNumberRef	CopyCFNumber() const { if(mCFNumber != NULL) { CFRetain(mCFNumber); } return mCFNumber; }
139 
GetSInt8()140 	SInt8		GetSInt8() const { SInt8 theAnswer = 0; if(mCFNumber != NULL) { CFNumberGetValue(mCFNumber, kCFNumberSInt8Type, &theAnswer); } return theAnswer; }
GetSInt32()141 	SInt32		GetSInt32() const { SInt32 theAnswer = 0; if(mCFNumber != NULL) { CFNumberGetValue(mCFNumber, kCFNumberSInt32Type, &theAnswer); } return theAnswer; }
GetUInt32()142 	UInt32		GetUInt32() const { UInt32 theAnswer = 0; if(mCFNumber != NULL) { CFNumberGetValue(mCFNumber, kCFNumberSInt32Type, &theAnswer); } return theAnswer; }
GetFloat32()143 	Float32		GetFloat32() const { Float32 theAnswer = 0.0f; if(mCFNumber != NULL) { CFNumberGetValue(mCFNumber, kCFNumberFloat32Type, &theAnswer); } return theAnswer; }
144 	Float32		GetFixed32() const;
145 	Float64		GetFixed64() const;
GetSInt64()146 	SInt64		GetSInt64() const { SInt64 theAnswer = 0; if(mCFNumber != NULL) { CFNumberGetValue(mCFNumber, kCFNumberSInt64Type, &theAnswer); } return theAnswer; }
147 
148 				CACFNumber(const void*);	// prevent accidental instantiation with a pointer via bool constructor
149 };
150 
151 #endif
152