VISR  0.12.0
Versatile Interactive Scene Renderer
class_registrar.hpp
Go to the documentation of this file.
1 /* Copyright Institute of Sound and Vibration Research - All rights reserved */
2 
3 #ifndef VISR_MAXMSP_MAXSUPPORT_CLASS_REGISTRAR_HPP_INCLUDED
4 #define VISR_MAXMSP_MAXSUPPORT_CLASS_REGISTRAR_HPP_INCLUDED
5 
6 #include "external_wrapper.hpp"
7 
8 /* Super-safe determination of the MAX define for setting the operating system. */
9 #ifdef __APPLE_CC__
10 #ifndef MAC_VERSION
11 #define MAC_VERSION
12 #undef WIN_VERSION
13 #endif
14 #else
15 #ifdef _MSC_VER
16 #ifndef WIN_VERSION
17 #define WIN_VERSION
18 #endif
19 #undef MAC_VERSION
20 #endif
21 #endif
22 
23 #include <ext.h>
24 
25 namespace visr
26 {
27 namespace maxmsp
28 {
29 
30 template<class ExternalType>
32 {
33 public:
34  explicit ClassRegistrar( char const * externalName );
35 
36  static void * newObject( t_symbol *s, short argc, t_atom *argv );
37 
38 private:
48  static t_class *& staticClassInstance( )
49  {
50  static t_class * sInstance = 0;
51  return sInstance;
52  }
53 };
54 
55 template<class ExternalType>
57 {
58  staticClassInstance() = class_new( externalName,
59  reinterpret_cast<method>(&ClassRegistrar<ExternalType>::newObject),
60  reinterpret_cast<method>(&ExternalWrapper<ExternalType>::free),
61  sizeof(typename ExternalWrapper<ExternalType>::PlainObject), 0, A_GIMME, 0 );
62 
63  class_addmethod( staticClassInstance(),
64  reinterpret_cast<method>(&ExternalWrapper<ExternalType>::dsp64), "dsp64", A_CANT, 0 );
65  class_addmethod( staticClassInstance(),
66  reinterpret_cast<method>(&ExternalWrapper<ExternalType>::assist), "assist", A_CANT, 0 );
67  class_addmethod( staticClassInstance(),
68  reinterpret_cast<method>(&ExternalWrapper<ExternalType>::getFloat),
69  "float", A_FLOAT, 0 );
70 
71  class_dspinit( staticClassInstance() );
72  class_register( CLASS_BOX, staticClassInstance() );
73  post( "ClassRegistrar: Finished registration of external." );
74 }
75 
76 template<class ExternalType>
77 /*static*/ void * ClassRegistrar<ExternalType>::newObject( t_symbol *s, short argc, t_atom *argv )
78 {
79  post( "ClassRegistrar<ExternalType>::newObject() called." );
80  t_class * classInstance = staticClassInstance();
81  typename ExternalWrapper<ExternalType>::PlainObject* newPlainObj
82  = static_cast<typename ExternalWrapper<ExternalType>::PlainObject *>(object_alloc( classInstance ));
83  if( !newPlainObj )
84  {
85  post( "Creation of new object failed." );
86  return nullptr;
87  }
88  newPlainObj->mObject = new ExternalType( newPlainObj->mMaxProxy, argc, argv );
89  return newPlainObj; // returning the pointer to the plain C struct encapsulating the object.
90 }
91 
92 } // namespace visr
93 } // namespace maxmsp
94 
95 #endif // VISR_MAXMSP_MAXSUPPORT_CLASS_REGISTRAR_HPP_INCLUDED
Definition: class_registrar.hpp:31
ClassRegistrar(char const *externalName)
Definition: class_registrar.hpp:56
Definition: options.cpp:10
static void * newObject(t_symbol *s, short argc, t_atom *argv)
Definition: class_registrar.hpp:77
Definition: external_wrapper.hpp:36