simObjectRef.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _SIMOBJECTREF_H_
  23. #define _SIMOBJECTREF_H_
  24. #ifndef _DYNAMIC_CONSOLETYPES_H_
  25. #include "console/dynamicTypes.h"
  26. #endif
  27. #ifndef _SIM_H_
  28. #include "console/sim.h"
  29. #endif
  30. // SimObjectRef<T>
  31. template< class T >
  32. class SimObjectRef
  33. {
  34. public:
  35. static S32 _smTypeId;
  36. SimObjectRef()
  37. : mName( "" ),
  38. mId( 0 ),
  39. mObject( NULL )
  40. {
  41. }
  42. T* operator-> () { return _get(); }
  43. operator T*() { return _get(); }
  44. operator bool () { return _get() != NULL; }
  45. SimObjectRef<T>& operator= ( U32 id )
  46. {
  47. _set( id );
  48. return *this;
  49. }
  50. SimObjectRef<T>& operator= ( const char *name )
  51. {
  52. _set( name );
  53. return *this;
  54. }
  55. protected:
  56. T* _get();
  57. void _set( U32 id );
  58. void _set( const char *name );
  59. protected:
  60. StringTableEntry mName;
  61. U32 mId;
  62. T *mObject;
  63. };
  64. // static initialization
  65. template<class T>
  66. S32 SimObjectRef<T>::_smTypeId = 0;
  67. // inline methods
  68. template<class T>
  69. void SimObjectRef<T>::_set( const char *name )
  70. {
  71. mName = StringTable->insert( name );
  72. mObject = NULL;
  73. mId = 0;
  74. }
  75. template<class T>
  76. void SimObjectRef<T>::_set( U32 id )
  77. {
  78. mId = id;
  79. mObject = NULL;
  80. mName = "";
  81. }
  82. template<class T>
  83. T* SimObjectRef<T>::_get()
  84. {
  85. if ( mObject == NULL )
  86. {
  87. if ( mId != 0 )
  88. Sim::findObject( mId, mObject );
  89. else if ( mName != NULL && dStrlen( mName ) != 0 )
  90. Sim::findObject( mName, mObject );
  91. }
  92. return mObject;
  93. }
  94. // SimObjectRefConsoleBaseType<T>
  95. template< class T >
  96. class SimObjectRefConsoleBaseType : public ConsoleBaseType
  97. {
  98. public:
  99. typedef ConsoleBaseType Parent;
  100. SimObjectRefConsoleBaseType( const char *typeName )
  101. : Parent( sizeof(SimObjectRef<T>), &SimObjectRef<T>::_smTypeId, typeName )
  102. {
  103. }
  104. public:
  105. virtual const char* getData( void *dptr, const EnumTable*, BitSet32 )
  106. {
  107. SimObjectRef<T> *objRef = static_cast< SimObjectRef<T>* >( dptr );
  108. T *obj = *objRef;
  109. return T::__getObjectId( obj );
  110. }
  111. virtual void setData( void* dptr, S32 argc, const char** argv, const EnumTable*, BitSet32 )
  112. {
  113. SimObjectRef<T> *objRef = static_cast< SimObjectRef<T>* >( dptr );
  114. if ( argc != 1 )
  115. return;
  116. *objRef = argv[0];
  117. }
  118. virtual const bool isDatablock()
  119. {
  120. return T::__smIsDatablock;
  121. };
  122. virtual const char* getTypeClassName()
  123. {
  124. return T::getStaticClassRep()->getClassName();
  125. }
  126. virtual void* getNativeVariable()
  127. {
  128. SimObjectRef<T> *var = new SimObjectRef<T>();
  129. return (void*)var;
  130. }
  131. virtual void deleteNativeVariable( void *var )
  132. {
  133. SimObjectRef<T> *nativeVar = reinterpret_cast< SimObjectRef<T>* >( var );
  134. delete nativeVar;
  135. }
  136. };
  137. #endif // _SIMOBJECTREF_H_