Cortex  10.0.0-a4
RunTimeTyped.h
1 //
3 // Copyright (c) 2007-2014, Image Engine Design Inc. All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //
12 // * Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
15 //
16 // * Neither the name of Image Engine Design nor the names of any
17 // other contributors to this software may be used to endorse or
18 // promote products derived from this software without specific prior
19 // written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
34 
35 #ifndef IE_CORE_RUNTIMETYPED_H
36 #define IE_CORE_RUNTIMETYPED_H
37 
38 #include "IECore/Export.h"
39 #include "IECore/RefCounted.h"
40 #include "IECore/TypeIds.h"
41 
42 #include <string>
43 #include <string.h>
44 #include <map>
45 #include <vector>
46 #include <set>
47 
48 #include "tbb/spin_rw_mutex.h"
49 
50 namespace IECore
51 {
52 
53 #define IE_CORE_DECLARETYPEFNS( TYPENAME )\
54  IECore::TypeId typeId() const override { return TYPENAME ## TypeId; };\
55  const char *typeName() const override { return #TYPENAME; };\
56  static IECore::TypeId staticTypeId() { return TYPENAME ## TypeId; };\
57  static const char *staticTypeName() { return #TYPENAME; };\
58  static IECore::TypeId baseTypeId() { return BaseClass::staticTypeId(); };\
59  static const char *baseTypeName() { return BaseClass::staticTypeName(); };
60 
61 #define IE_CORE_DECLAREEXTENSIONTYPEFNS( TYPENAME, TYPEID )\
62  IECore::TypeId typeId() const override { return IECore::TypeId(TYPEID); };\
63  const char *typeName() const override { return #TYPENAME; };\
64  static IECore::TypeId staticTypeId() { return IECore::TypeId(TYPEID); };\
65  static const char *staticTypeName() { return #TYPENAME; };\
66  static IECore::TypeId baseTypeId() { return BaseClass::staticTypeId(); };\
67  static const char *baseTypeName() { return BaseClass::staticTypeName(); };
68 
69 #define IE_CORE_DECLARETYPEISINSTANCEFUNCTIONS() \
70  bool isInstanceOf( IECore::TypeId typeId ) const override { return typeId==staticTypeId() ? true : BaseClass::isInstanceOf( typeId ); }; \
71  bool isInstanceOf( const char *typeName ) const override { return !strcmp( typeName, staticTypeName() ) ? true : BaseClass::isInstanceOf( typeName ); }; \
72 
73 #define IE_CORE_DECLAREINHERITSFROMFUNCTIONS() \
74  static bool inheritsFrom( IECore::TypeId typeId ) { return BaseClass::staticTypeId()==typeId ? true : BaseClass::inheritsFrom( typeId ); }; \
75  static bool inheritsFrom( const char *typeName ) { return !strcmp(BaseClass::staticTypeName(), typeName ) ? true : BaseClass::inheritsFrom( typeName ); } \
76 
77 #define IE_CORE_DECLARERUNTIMETYPEDDESCRIPTION( TYPE ) \
78  private: \
79  static const IECore::RunTimeTyped::TypeDescription<TYPE> g_typeDescription;\
80  public:
81 
82 #define IE_CORE_DEFINERUNTIMETYPEDDESCRIPTION( TYPE ) \
83  const IECore::RunTimeTyped::TypeDescription<TYPE> TYPE::g_typeDescription;
84 
85 #define IE_CORE_DEFINERUNTIMETYPED( TYPE ) \
86  IE_CORE_DEFINERUNTIMETYPEDDESCRIPTION( TYPE )
87 
92 #define IE_CORE_DECLARERUNTIMETYPED( TYPE, BASETYPE )\
93  typedef BASETYPE BaseClass;\
94  IE_CORE_DECLAREMEMBERPTR( TYPE )\
95  IE_CORE_DECLARETYPEFNS( TYPE )\
96  IE_CORE_DECLARETYPEISINSTANCEFUNCTIONS()\
97  IE_CORE_DECLAREINHERITSFROMFUNCTIONS()\
98  IE_CORE_DECLARERUNTIMETYPEDDESCRIPTION( TYPE ) \
99 
100 
101 
106 #define IE_CORE_DECLARERUNTIMETYPEDEXTENSION( TYPE, TYPEID, BASETYPE )\
107  typedef BASETYPE BaseClass; \
108  IE_CORE_DECLAREMEMBERPTR( TYPE ) \
109  IE_CORE_DECLAREEXTENSIONTYPEFNS( TYPE, TYPEID )\
110  IE_CORE_DECLARETYPEISINSTANCEFUNCTIONS()\
111  IE_CORE_DECLAREINHERITSFROMFUNCTIONS()\
112  IE_CORE_DECLARERUNTIMETYPEDDESCRIPTION( TYPE )
113 
116 #define IECORE_RUNTIMETYPED_DECLARETEMPLATE( TYPENAME, BASETYPENAME ) \
117  IE_CORE_DECLAREMEMBERPTR( TYPENAME ) \
118  IECore::TypeId typeId() const override; \
119  const char *typeName() const override; \
120  static IECore::TypeId staticTypeId(); \
121  static const char *staticTypeName(); \
122  static IECore::TypeId baseTypeId(); \
123  static const char *baseTypeName(); \
124  bool isInstanceOf( IECore::TypeId typeId ) const override; \
125  bool isInstanceOf( const char *typeName ) const override; \
126  static bool inheritsFrom( IECore::TypeId typeId ); \
127  static bool inheritsFrom( const char *typeName ); \
128  typedef BASETYPENAME BaseClass; \
129 
130 #define IECORE_RUNTIMETYPED_DEFINETEMPLATESPECIALISATION( TYPENAME, TYPEID ) \
133  \
134  \
135  template<> IECORE_API \
136  IECore::TypeId TYPENAME::typeId() const \
137  { \
138  return IECore::TypeId( TYPEID ); \
139  } \
140  template<> IECORE_API \
141  IECore::TypeId TYPENAME::staticTypeId() \
142  { \
143  return IECore::TypeId( TYPEID ); \
144  } \
145  template<> IECORE_API \
146  const char *TYPENAME::typeName() const \
147  { \
148  return #TYPENAME; \
149  } \
150  template<> IECORE_API \
151  const char *TYPENAME::staticTypeName() \
152  { \
153  return #TYPENAME; \
154  } \
155  \
156  template<> IECORE_API \
157  IECore::TypeId TYPENAME::baseTypeId() \
158  { \
159  return BaseClass::staticTypeId(); \
160  } \
161  \
162  template<> IECORE_API \
163  const char *TYPENAME::baseTypeName() \
164  { \
165  return BaseClass::staticTypeName(); \
166  } \
167  \
168  template<> IECORE_API \
169  bool TYPENAME::isInstanceOf( IECore::TypeId typeId ) const \
170  { \
171  if( typeId==staticTypeId() ) \
172  { \
173  return true; \
174  } \
175  return BaseClass::isInstanceOf( typeId ); \
176  } \
177  \
178  template<> IECORE_API \
179  bool TYPENAME::isInstanceOf( const char *typeName ) const \
180  { \
181  if( !strcmp( typeName, staticTypeName() ) ) \
182  { \
183  return true; \
184  } \
185  return BaseClass::isInstanceOf( typeName ); \
186  } \
187  \
188  template<> IECORE_API \
189  bool TYPENAME::inheritsFrom( IECore::TypeId typeId ) \
190  { \
191  return BaseClass::staticTypeId()==typeId ? true : BaseClass::inheritsFrom( typeId ); \
192  } \
193  \
194  template<> IECORE_API \
195  bool TYPENAME::inheritsFrom( const char *typeName ) \
196  { \
197  return !strcmp( BaseClass::staticTypeName(), typeName ) ? true : BaseClass::inheritsFrom( typeName ); \
198  } \
199 
200 class IECORE_API RunTimeTyped : public RefCounted
212 {
213  public:
214 
217 
218  IE_CORE_DECLAREMEMBERPTR( RunTimeTyped );
219 
220  RunTimeTyped();
221  ~RunTimeTyped() override;
222 
230 
231 
235  virtual TypeId typeId() const;
238  virtual const char *typeName() const;
239 
241  static TypeId staticTypeId();
243  static const char *staticTypeName();
244 
247  static TypeId baseTypeId();
248 
251  static const char *baseTypeName();
252 
255  virtual bool isInstanceOf( TypeId typeId ) const;
258  virtual bool isInstanceOf( const char *typeName ) const;
259 
261  static bool inheritsFrom( TypeId typeId );
263  static bool inheritsFrom( const char *typeName );
265  static bool inheritsFrom( TypeId type, TypeId baseType );
267  static bool inheritsFrom( const char *typeName, const char *baseTypeName );
268 
270  static TypeId baseTypeId( TypeId typeId );
271 
277  static const std::vector<TypeId> &baseTypeIds( TypeId typeId );
278 
282  static const std::set<TypeId> &derivedTypeIds( TypeId typeId );
283 
287  static TypeId typeIdFromTypeName( const char *typeName );
291  static const char *typeNameFromTypeId( TypeId typeId );
292 
294  static void registerType( TypeId derivedTypeId, const char *derivedTypeName, TypeId baseTypeId );
296 
297  protected :
298 
299  template<class T>
300  struct TypeDescription
301  {
302  TypeDescription();
303  };
304 
305  typedef std::map< TypeId, TypeId > BaseTypeRegistryMap;
306  typedef std::map< TypeId, std::vector<TypeId> > BaseTypesRegistryMap;
307  typedef std::map< TypeId, std::set< TypeId > > DerivedTypesRegistryMap;
308 
309  typedef tbb::spin_rw_mutex Mutex;
310 
311  static Mutex g_baseTypeIdsMutex;
312  static Mutex g_derivedTypeIdsMutex;
313 
314  static BaseTypeRegistryMap &baseTypeRegistry();
315  static DerivedTypesRegistryMap &derivedTypesRegistry();
316 
317  static BaseTypesRegistryMap &completeBaseTypesRegistry();
318  static DerivedTypesRegistryMap &completeDerivedTypesRegistry();
319 
320  static void derivedTypeIdsWalk( TypeId typeId, std::set<TypeId> & );
321 
322  typedef std::map<TypeId, std::string> TypeIdsToTypeNamesMap;
323  typedef std::map<std::string, TypeId> TypeNamesToTypeIdsMap;
324 
325  static TypeIdsToTypeNamesMap &typeIdsToTypeNames();
326  static TypeNamesToTypeIdsMap &typeNamesToTypeIds();
327 
328 };
329 
330 IE_CORE_DECLAREPTR( RunTimeTyped );
331 
335 template<typename T, typename S>
336 boost::intrusive_ptr<T> runTimeCast( const boost::intrusive_ptr<S> &src );
340 template<typename T, typename S>
341 T *runTimeCast( S *src );
342 
347 template<typename T, typename S>
348 inline boost::intrusive_ptr<T> assertedStaticCast( const boost::intrusive_ptr<T> &src );
349 
353 template<typename T, typename S>
354 inline T* assertedStaticCast( S* src );
355 
356 } // namespace IECore
357 
358 #include "IECore/RunTimeTyped.inl"
359 
360 #endif // IE_CORE_RUNTIMETYPED_H
boost::intrusive_ptr< T > runTimeCast(const boost::intrusive_ptr< S > &src)
RefCounted BaseClass
A typedef for the class this class derives from. All RunTimeTyped classes define this typedef...
Definition: RunTimeTyped.h:216
boost::intrusive_ptr< T > assertedStaticCast(const boost::intrusive_ptr< T > &src)
TypeId
Definition: TypeIds.h:46
Definition: RefCounted.h:124
Definition: RunTimeTyped.h:211
This namespace contains all components of the core library.
Definition: AddSmoothSkinningInfluencesOp.h:43