Jamoma API  0.6.0.a19
ComponentBase.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 #ifndef __ComponentBase_h__
42 #define __ComponentBase_h__
43 
44 #include <new>
45 #include "CADebugMacros.h"
46 
47 #if !defined(__COREAUDIO_USE_FLAT_INCLUDES__)
48  #include <CoreAudio/CoreAudioTypes.h>
49  #include <AudioUnit/AudioUnit.h>
50 
51  #if !TARGET_OS_IPHONE
52  #include <CoreServices/../Frameworks/CarbonCore.framework/Headers/Components.h>
53 
54  #if (MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_5)
55  #define AudioComponentInstance ComponentInstance
56  #define AudioComponentDescription ComponentDescription
57  #define AudioComponent Component
58  #define CA_DO_NOT_USE_AUDIO_COMPONENT 1
59  #endif
60  #endif
61 
62  #if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4
63  typedef Float32 AudioUnitParameterValue;
64  #endif
65  #if COREAUDIOTYPES_VERSION < 1051
66  typedef Float32 AudioUnitSampleType;
67  #endif
68 
69  #include <pthread.h>
70 #else
71  #include "CoreAudioTypes.h"
72  #include "Components.h"
73  #include "AudioUnit.h"
74  #include "CAGuard.h"
75 #endif
76 
77 #ifndef COMPONENT_THROW
78  #if VERBOSE_COMPONENT_THROW
79  #define COMPONENT_THROW(throw_err) \
80  do { DebugMessage(#throw_err); throw static_cast<OSStatus>(throw_err); } while (0)
81  #else
82  #define COMPONENT_THROW(throw_err) \
83  throw static_cast<OSStatus>(throw_err)
84  #endif
85 #endif
86 
87 #define COMPONENT_CATCH \
88  catch (std::bad_alloc &) { result = -108/*memFullErr*/; } \
89  catch (OSStatus catch_err) { result = catch_err; } \
90  catch (OSErr catch_err) { result = catch_err; } \
91  catch (...) { result = -1; }
92 
93 class ComponentInitLocker
94 {
95 #if TARGET_OS_MAC
96 public:
97  ComponentInitLocker() { pthread_mutex_lock(&sComponentOpenMutex); }
98  ~ComponentInitLocker() { pthread_mutex_unlock(&sComponentOpenMutex); }
99 private:
100  static pthread_mutex_t sComponentOpenMutex;
101 #elif TARGET_OS_WIN32
102 public:
103  bool sNeedsUnlocking;
104  ComponentInitLocker() { sNeedsUnlocking = sComponentOpenGuard.Lock(); }
105  ~ComponentInitLocker() { if(sNeedsUnlocking) { sComponentOpenGuard.Unlock(); } }
106 private:
107  static CAGuard sComponentOpenGuard;
108 #endif
109 };
110 
111 #if !TARGET_OS_IPHONE
112  /*! @class ComponentEntryPoint */
113 template <class Class>
114 class ComponentEntryPoint {
115 public:
116  /*! @method Dispatch */
117  static OSStatus Dispatch(ComponentParameters *params, Class *obj)
118  {
119  OSStatus result = noErr;
120 
121  try {
122  if (params->what == kComponentOpenSelect) {
123  // solve a host of initialization thread safety issues.
124  ComponentInitLocker lock;
125 
126  ComponentInstance ci = (ComponentInstance)(params->params[0]);
127  Class *This = new Class((AudioComponentInstance)ci);
128  This->PostConstructor(); // allows base class to do additional initialization
129  // once the derived class is fully constructed
130 
131  #if !CA_AU_IS_ONLY_PLUGIN
132  SetComponentInstanceStorage(ci, (Handle)This);
133  #endif
134  } else
135  result = Class::ComponentEntryDispatch(params, obj);
136  }
137  COMPONENT_CATCH
138 
139  return result;
140  }
141 
142  /*! @method Register */
143  static Component Register(OSType compType, OSType subType, OSType manufacturer)
144  {
145  ComponentDescription description = {compType, subType, manufacturer, 0, 0};
146  Component component = RegisterComponent(&description, (ComponentRoutineUPP) Dispatch, registerComponentGlobal, NULL, NULL, NULL);
147  if (component != NULL) {
148  SetDefaultComponent(component, defaultComponentAnyFlagsAnyManufacturerAnySubType);
149  }
150  return component;
151  }
152 };
153 
154 #if TARGET_OS_MAC && TARGET_CPU_PPC && !TARGET_RT_MAC_MACHO
155  // for OS 9, a PPC native component's entry point must be a routine descriptor
156  #define COMPONENT_ENTRY(Class) \
157  extern "C" OSStatus Class##Entry(ComponentParameters *params, Class *obj); \
158  extern "C" OSStatus Class##Entry(ComponentParameters *params, Class *obj) { \
159  return ComponentEntryPoint<Class>::Dispatch(params, obj); \
160  } \
161  \
162  struct RoutineDescriptor Class##EntryRD = \
163  BUILD_ROUTINE_DESCRIPTOR((kPascalStackBased | RESULT_SIZE (kFourByteCode) | \
164  STACK_ROUTINE_PARAMETER (1, kFourByteCode) | \
165  STACK_ROUTINE_PARAMETER (2, kFourByteCode)), Class##Entry);
166 #else
167  #define COMPONENT_ENTRY(Class) \
168  extern "C" OSStatus Class##Entry(ComponentParameters *params, Class *obj); \
169  extern "C" OSStatus Class##Entry(ComponentParameters *params, Class *obj) { \
170  return ComponentEntryPoint<Class>::Dispatch(params, obj); \
171  }
172 #endif
173 
174  /*! @class ComponentRegistrar */
175 template <class Class, OSType Type, OSType Subtype, OSType Manufacturer>
176 class ComponentRegistrar {
177 public:
178  /*! @ctor ComponentRegistrar */
179  ComponentRegistrar() { ComponentEntryPoint<Class>::Register(Type, Subtype, Manufacturer); }
180 };
181 
182 #define COMPONENT_REGISTER(Class,Type,Subtype,Manufacturer) \
183  static ComponentRegistrar<Class, Type, Subtype, Manufacturer> gRegistrar##Class
184 #else
185 #define COMPONENT_ENTRY(Class)
186 #define COMPONENT_REGISTER(Class)
187 #endif // !TARGET_OS_IPHONE
188 
189 
190  /*! @class ComponentBase */
191 class ComponentBase {
192 public:
193  // classic MacErrors
194  enum { noErr = 0, paramErr = -50, memFullErr = -108 };
195 
196  /*! @ctor ComponentBase */
197  ComponentBase(AudioComponentInstance inInstance) : mComponentInstance(inInstance) { }
198  /*! @dtor ~ComponentBase */
199  virtual ~ComponentBase();
200 
201  /*! @method PostConstructor */
202  virtual void PostConstructor();
203 
204  /*! @method PreDestructor */
205  virtual void PreDestructor();
206 
207 #if !TARGET_OS_IPHONE
208  /*! @method Version */
209  virtual OSStatus Version();
210 
211  /*! @method ComponentEntryDispatch */
212  static OSStatus ComponentEntryDispatch(ComponentParameters *p, ComponentBase *This);
213 #endif
214 
215  /*! @method GetComponentInstance */
216  AudioComponentInstance GetComponentInstance() const { return mComponentInstance; }
217 
218  /*! @method GetComponentDescription */
219  AudioComponentDescription GetComponentDescription() const;
220 
221 protected:
222  /*! @var mComponentInstance */
223  AudioComponentInstance mComponentInstance;
224 };
225 
226 #endif // __ComponentBase_h__