Torque3D Documentation / _generateds / runtimeClassRep.h

runtimeClassRep.h

Engine/source/console/runtimeClassRep.h

More...

Classes:

class

This class is to allow new types to be added, at run-time, to Torque.

Public Defines

define
DECLARE_RUNTIME_CONOBJECT(className)    ( className, Parent );                   \
   static  _sTypeId; \
   static <className> dynRTClassRep;      \
   static * getParentStaticClassRep();  \
   static * getStaticClassRep();        \
   virtual * getClassRep() 
define
IMPLEMENT_RUNTIME_CONOBJECT(className)    ( className,  )                                                              \
   ;                                                                            \
    className::_sTypeId; \
   * className::getClassRep()  { return &className::dynRTClassRep; }           \
   * className::getStaticClassRep() { return &dynRTClassRep; }                      \
   * className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
   <className> className::dynRTClassRep(#className, "Type" #className, &_sTypeId, 0, -1, 0, className::getParentStaticClassRep())

Detailed Description

Public Defines

DECLARE_RUNTIME_CONOBJECT(className)    ( className, Parent );                   \
   static  _sTypeId; \
   static <className> dynRTClassRep;      \
   static * getParentStaticClassRep();  \
   static * getStaticClassRep();        \
   virtual * getClassRep() 
IMPLEMENT_RUNTIME_CONOBJECT(className)    ( className,  )                                                              \
   ;                                                                            \
    className::_sTypeId; \
   * className::getClassRep()  { return &className::dynRTClassRep; }           \
   * className::getStaticClassRep() { return &dynRTClassRep; }                      \
   * className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
   <className> className::dynRTClassRep(#className, "Type" #className, &_sTypeId, 0, -1, 0, className::getParentStaticClassRep())
  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _RUNTIME_CLASSREP_H_
 25#define _RUNTIME_CLASSREP_H_
 26
 27#include "console/consoleObject.h"
 28#include "console/consoleInternal.h"
 29
 30/// This class is to allow new types to be added, at run-time, to Torque.
 31/// This is primarily for dynamic libraries, plugins etc.
 32///
 33/// The idea is the same, one instance per type which is getting registered
 34/// however it doesn't get added to the dictionary until it is told to
 35/// add itself. It can be removed, as well, though no safe-execution
 36/// assurances are made by this class. If an object type is removed
 37/// behavior of existing console objects of that type is undefined. 
 38/// (aka bad stuff will probably happen but I don't know exactly what)
 39template <class T>
 40class RuntimeClassRep : public AbstractClassRep
 41{
 42protected:
 43   static bool smConRegistered; ///< This variable will be set to true if this class-rep is currently registered
 44
 45public:
 46   RuntimeClassRep( const char *name, const char* conTypeName, S32* conTypeIdPtr, S32 netClassGroupMask, S32 netClassType, S32 netEventDir, AbstractClassRep *parent )
 47      : AbstractClassRep( conTypeIdPtr, conTypeName )
 48   {
 49      // name is a static compiler string so no need to worry about copying or deleting
 50      mClassName = name;
 51      mTypeInfo = _MAPTYPE< T >();
 52
 53      // Clean up mClassId
 54      for( U32 i = 0; i < NetClassGroupsCount; i++ )
 55         mClassId[i] = -1;
 56
 57      // Set properties for this ACR
 58      mClassType      = netClassType;
 59      mClassGroupMask = netClassGroupMask;
 60      mNetEventDir    = netEventDir;
 61      parentClass     = parent;
 62   };
 63
 64   virtual AbstractClassRep* getContainerChildClass(const bool recurse)
 65   {
 66      // Fetch container children type.
 67      AbstractClassRep* pChildren = T::getContainerChildStaticClassRep();
 68      if (!recurse || pChildren != NULL)
 69         return pChildren;
 70
 71      // Fetch parent type.
 72      AbstractClassRep* pParent = T::getParentStaticClassRep();
 73      if (pParent == NULL)
 74         return NULL;
 75
 76      // Get parent container children.
 77      return pParent->getContainerChildClass(recurse);
 78   }
 79
 80   virtual WriteCustomTamlSchema getCustomTamlSchema(void)
 81   {
 82      return T::getStaticWriteCustomTamlSchema();
 83   }
 84
 85   /// Perform class specific initialization tasks.
 86   ///
 87   /// Link namespaces, call initPersistFields() and consoleInit().
 88   void init() const
 89   {
 90      // Get handle to our parent class, if any, and ourselves (we are our parent's child).
 91      AbstractClassRep *parent = T::getParentStaticClassRep();
 92      AbstractClassRep *child  = T::getStaticClassRep      ();
 93
 94      // If we got reps, then link those namespaces! (To get proper inheritance.)
 95      if( parent && child )
 96         Con::classLinkNamespaces( parent->getNameSpace(), child->getNameSpace() );
 97
 98      // Finally, do any class specific initialization...
 99      T::initPersistFields();
100      T::consoleInit();
101   }
102
103   /// Wrap constructor.
104   ConsoleObject *create() const { return new T; }
105
106   //-----------------------------------------------------------------------------
107
108   /// Register this class with the Torque console
109   void consoleRegister()
110   {
111      AssertFatal( !smConRegistered, "Calling consoleRegister, but this type is already linked into the class list" );
112
113      if( !smConRegistered )
114         registerClassRep( this );
115
116      // Now initialize the namespace
117      mNamespace = Con::lookupNamespace( StringTable->insert( getClassName() ) );
118      mNamespace->mClassRep = this;
119
120      // Perform field initialization
121      sg_tempFieldList.setSize(0);
122
123      init();
124
125      // So if we have things in it, copy it over...
126      if ( sg_tempFieldList.size() != 0 )
127         mFieldList = sg_tempFieldList;
128
129      // And of course delete it every round.
130      sg_tempFieldList.clear();
131
132      smConRegistered = true;
133   }
134
135   /// Unregister this class with the Torque console
136   void consoleUnRegister()
137   {
138      AssertFatal( smConRegistered, "Calling consoleUnRegister, but this type is not linked into the class list" );
139      if( !smConRegistered )
140         return;
141
142      removeClassRep( this );
143      smConRegistered = false;
144   }
145
146   /// Returns true if this class type is registered with the console system
147   static const bool isRegistered() { return smConRegistered; }
148
149   /// @name Console Type Interface
150   /// @{
151
152   virtual void setData( void* dptr, S32 argc, const char** argv, const EnumTable* tbl, BitSet32 flag )
153   {
154      if( argc == 1 )
155      {
156         T** obj = ( T** ) dptr;
157         *obj = dynamic_cast< T* >( T::__findObject( argv[ 0 ] ) );
158      }
159      else
160         Con::errorf( "Cannot set multiple args to a single ConsoleObject*.");
161   }
162   
163   virtual const char* getData( void* dptr, const EnumTable* tbl, BitSet32 flag )
164   {
165      T** obj = ( T** ) dptr;
166      return Con::getReturnBuffer( T::__getObjectId( *obj ) );
167   }
168   
169   virtual const char* getTypeClassName() { return mClassName; }
170   virtual const bool isDatablock() { return T::__smIsDatablock; };
171   
172   /// @}
173};
174
175template<class T> bool RuntimeClassRep<T>::smConRegistered = false;
176
177//-----------------------------------------------------------------------------
178
179#define DECLARE_RUNTIME_CONOBJECT(className)                    \
180   DECLARE_CLASS( className, Parent );                   \
181   static S32 _sTypeId; \
182   static RuntimeClassRep<className> dynRTClassRep;      \
183   static AbstractClassRep* getParentStaticClassRep();  \
184   static AbstractClassRep* getStaticClassRep();        \
185   virtual AbstractClassRep* getClassRep() const
186
187#define IMPLEMENT_RUNTIME_CONOBJECT(className)                                                     \
188   IMPLEMENT_CLASS( className, NULL )                                                              \
189   END_IMPLEMENT_CLASS;                                                                            \
190   S32 className::_sTypeId; \
191   AbstractClassRep* className::getClassRep() const { return &className::dynRTClassRep; }           \
192   AbstractClassRep* className::getStaticClassRep() { return &dynRTClassRep; }                      \
193   AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
194   RuntimeClassRep<className> className::dynRTClassRep(#className, "Type" #className, &_sTypeId, 0, -1, 0, className::getParentStaticClassRep())
195
196#endif
197