supernova: c++11 compile fix
[supercollider.git] / platform / mac / SuperColliderAU / AUSDK / ComponentBase.h
blobc4789b4d5269bc0d6b94120055379151a48b3597
1 /* Copyright © 2007 Apple Inc. All Rights Reserved.
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.
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.
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.
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.
41 /*=============================================================================
42 ComponentBase.h
44 =============================================================================*/
46 #ifndef __ComponentBase_h__
47 #define __ComponentBase_h__
49 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
50 #include <CoreServices/CoreServices.h>
51 #else
52 #include <ConditionalMacros.h>
53 #include <CoreServices.h>
54 #endif
55 #include <new>
56 #include "CADebugMacros.h"
58 #if !TARGET_OS_WIN32
59 #if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4
60 typedef Float32 AudioUnitParameterValue;
61 typedef Float32 AudioSampleType;
62 #endif
63 #endif
65 #ifndef COMPONENT_THROW
66 #define COMPONENT_THROW(err) \
67 do { DebugMessage(#err); throw static_cast<OSStatus>(err); } while (0)
68 #endif
70 #define COMPONENT_CATCH \
71 catch (std::bad_alloc &) { result = memFullErr; } \
72 catch (OSStatus err) { result = err; } \
73 catch (OSErr err) { result = err; } \
74 catch (...) { result = -1; }
76 #if SUPPORT_AU_VERSION_1
78 #if TARGET_OS_WIN32
79 #include "CAGuard.h"
80 #endif
82 class ComponentInitLocker
84 #if TARGET_OS_MAC
85 public:
86 ComponentInitLocker() { pthread_mutex_lock(&sComponentOpenMutex); }
87 ~ComponentInitLocker() { pthread_mutex_unlock(&sComponentOpenMutex); }
88 private:
89 static pthread_mutex_t sComponentOpenMutex;
90 #elif TARGET_OS_WIN32
91 public:
92 bool sNeedsUnlocking;
93 ComponentInitLocker() { sNeedsUnlocking = sComponentOpenGuard.Lock(); }
94 ~ComponentInitLocker() { if(sNeedsUnlocking) { sComponentOpenGuard.Unlock(); } }
95 private:
96 static CAGuard sComponentOpenGuard;
97 #endif
100 #endif
103 /*! @class ComponentEntryPoint */
104 template <class Class>
105 class ComponentEntryPoint {
106 public:
107 /*! @method Dispatch */
108 static ComponentResult Dispatch(ComponentParameters *params, Class *obj)
110 ComponentResult result = noErr;
112 try {
113 if (params->what == kComponentOpenSelect) {
114 #if SUPPORT_AU_VERSION_1
115 // solve a host of initialization thread safety issues.
116 ComponentInitLocker lock;
117 #endif
118 ComponentInstance ci = (ComponentInstance)(params->params[0]);
119 Class *This = new Class(ci);
120 This->PostConstructor(); // allows base class to do additional initialization
121 // once the derived class is fully constructed
123 SetComponentInstanceStorage(ci, (Handle)This);
124 } else
125 result = Class::ComponentEntryDispatch(params, obj);
127 COMPONENT_CATCH
129 return result;
132 /*! @method Register */
133 static Component Register(OSType compType, OSType subType, OSType manufacturer)
135 ComponentDescription description = {compType, subType, manufacturer, 0, 0};
136 Component component = RegisterComponent(&description, (ComponentRoutineUPP) Dispatch, registerComponentGlobal, NULL, NULL, NULL);
137 if (component != NULL) {
138 SetDefaultComponent(component, defaultComponentAnyFlagsAnyManufacturerAnySubType);
140 return component;
144 #if TARGET_OS_MAC && TARGET_CPU_PPC && !TARGET_RT_MAC_MACHO
145 // for OS 9, a PPC native component's entry point must be a routine descriptor
146 #define COMPONENT_ENTRY(Class) \
147 extern "C" ComponentResult Class##Entry(ComponentParameters *params, Class *obj); \
148 extern "C" ComponentResult Class##Entry(ComponentParameters *params, Class *obj) { \
149 return ComponentEntryPoint<Class>::Dispatch(params, obj); \
152 struct RoutineDescriptor Class##EntryRD = \
153 BUILD_ROUTINE_DESCRIPTOR((kPascalStackBased | RESULT_SIZE (kFourByteCode) | \
154 STACK_ROUTINE_PARAMETER (1, kFourByteCode) | \
155 STACK_ROUTINE_PARAMETER (2, kFourByteCode)), Class##Entry);
156 #else
157 #define COMPONENT_ENTRY(Class) \
158 extern "C" ComponentResult Class##Entry(ComponentParameters *params, Class *obj); \
159 extern "C" ComponentResult Class##Entry(ComponentParameters *params, Class *obj) { \
160 return ComponentEntryPoint<Class>::Dispatch(params, obj); \
162 #endif
164 /*! @class ComponentRegistrar */
165 template <class Class, OSType Type, OSType Subtype, OSType Manufacturer>
166 class ComponentRegistrar {
167 public:
168 /*! @ctor ComponentRegistrar */
169 ComponentRegistrar() { ComponentEntryPoint<Class>::Register(Type, Subtype, Manufacturer); }
172 #define COMPONENT_REGISTER(Class,Type,Subtype,Manufacturer) \
173 static ComponentRegistrar<Class, Type, Subtype, Manufacturer> gRegistrar##Class
176 /*! @class ComponentBase */
177 class ComponentBase {
178 public:
179 /*! @ctor ComponentBase */
180 ComponentBase(ComponentInstance inInstance) : mComponentInstance(inInstance) { }
181 /*! @dtor ~ComponentBase */
182 virtual ~ComponentBase();
184 /*! @method Version */
185 virtual ComponentResult Version();
187 /*! @method PostConstructor */
188 virtual void PostConstructor();
190 /*! @method PreDestructor */
191 virtual void PreDestructor();
193 /*! @method ComponentEntryDispatch */
194 static ComponentResult ComponentEntryDispatch(ComponentParameters *p, ComponentBase *This);
196 /*! @method GetComponentInstance */
197 ComponentInstance GetComponentInstance() const { return mComponentInstance; }
199 protected:
200 /*! @var mComponentInstance */
201 ComponentInstance mComponentInstance;
204 #endif // __ComponentBase_h__