HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SIM_DataUtils.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  */
7 
8 #ifndef __SIM_DataUtils_h__
9 #define __SIM_DataUtils_h__
10 
11 #include "SIM_Data.h"
12 #include "SIM_DataFactory.h"
13 #include "SIM_Utils.h"
14 #include <UT/UT_StringArray.h>
15 #include <UT/UT_StringHolder.h>
16 #include <SYS/SYS_Compiler.h>
17 
18 /// This macro provides a quick way to define a static function called
19 /// classname() which returns the provided class name as a string.
20 #define DECLARE_CLASSNAME(DataClass, SuperClass) \
21  private: \
22  typedef SuperClass BaseClass; \
23  typedef DataClass ThisClass; \
24  static inline UT_StringHolder classname() \
25  { return SYS_CONCAT(#DataClass,_UTsh); } \
26  virtual const UT_String &getDataTypeSubclass() const \
27  { \
28  static UT_String theType(classname()); \
29  return theType; \
30  } \
31  protected: \
32  static void getDataTypeSuperclasses(UT_StringArray& classes) \
33  { \
34  classes.append(SYS_CONCAT(#SuperClass,_UTsh)); \
35  SuperClass::getDataTypeSuperclasses(classes); \
36  } \
37  private: \
38  /**/
39 
40 /// This function implements a standard version of getDistanceToType().
41 /// Only if your subclass uses multiple inheritance will it be necessary to
42 /// write a customized version of this function. Be sure to add this
43 /// declaration to your class definition or the DECLARE_DATAFACTORY()
44 /// macro will not compile properly.
45 #define DECLARE_STANDARD_GETCASTTOTYPE() \
46  protected: \
47  virtual void *getCastToType(const char *totype) const \
48  { \
49  if( strcmp(classname(), totype) == 0 ) \
50  return (void *)this; \
51  return BaseClass::getCastToType(totype); \
52  } \
53  /**/
54 
55 /// This macro simplifies the calling and casting the return value from
56 /// This macro quickly defines all the static functions needed to declare
57 /// a SIM_DataFactory for a SIM_Data subclass.
58 #define DECLARE_DATAFACTORY(DataClass, SuperClass, Description, DopParms) \
59  public: \
60  class DataClass##Factory : public SIM_DataFactory \
61  { \
62  public: \
63  DataClass##Factory(SIM_Engine *engine) \
64  : SIM_DataFactory(\
65  SYS_CONCAT(#DataClass,_UTsh), \
66  SYS_CONCAT(Description,_UTsh), \
67  DataClass::getDataTypeSuperclasses, \
68  DataClass::constructor, \
69  DataClass::destructor, \
70  DataClass::DopParms, \
71  engine) \
72  { } \
73  virtual ~DataClass##Factory() \
74  { } \
75  }; \
76  static void createDataFactory(SIM_Engine *engine) \
77  { \
78  SIM_DataFactory *factory; \
79  factory = new DataClass##Factory( \
80  engine); \
81  SIM_DataFactoryCreator::addDataFactory \
82  (engine, factory); \
83  } \
84  friend class DataClass##Factory; \
85  private: \
86  DECLARE_CLASSNAME(DataClass, SuperClass); \
87  static SIM_Data *constructor(const SIM_DataFactory *factory) \
88  { return (BaseClass *)new \
89  DataClass(factory); } \
90  static void destructor(SIM_Data *data) \
91  { delete (DataClass *)data-> \
92  getPointerToType(#DataClass); } \
93  /**/
94 
95 /// Add this macro to the initialization function for your library. It uses
96 /// the declaration above to register your data type.
97 #define IMPLEMENT_DATAFACTORY(DataClass) \
98  static SIM_DataFactoryCreator \
99  DataClass##Creator(DataClass::createDataFactory);
100 
101 #endif
102