simObjectRef.h

Engine/source/console/simObjectRef.h

More...

Classes:

Detailed Description

  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 _SIMOBJECTREF_H_
 25#define _SIMOBJECTREF_H_
 26
 27
 28#ifndef _DYNAMIC_CONSOLETYPES_H_
 29#include "console/dynamicTypes.h"
 30#endif
 31
 32#ifndef _SIM_H_
 33#include "console/sim.h"
 34#endif
 35
 36
 37
 38// SimObjectRef<T>
 39
 40template< class T >
 41class SimObjectRef
 42{
 43public:         
 44
 45   static S32 _smTypeId;
 46
 47   SimObjectRef()
 48      : mName( "" ),
 49        mId( 0 ),
 50        mObject( NULL )
 51   {
 52   }
 53   
 54   T* operator->() { return _get(); }   
 55
 56   operator T*() { return _get(); }
 57
 58   operator bool () { return _get() != NULL; }   
 59
 60   SimObjectRef<T>& operator=( U32 id )
 61   {
 62      _set( id );
 63      return *this;
 64   }
 65
 66   SimObjectRef<T>& operator=( const char *name )
 67   {
 68      _set( name );
 69      return *this;
 70   }
 71
 72protected:
 73
 74   T* _get();
 75   void _set( U32 id );      
 76   void _set( const char *name );      
 77
 78protected:
 79
 80   StringTableEntry mName;
 81   U32 mId;
 82   T *mObject;
 83};
 84
 85
 86// static initialization
 87
 88template<class T>
 89S32 SimObjectRef<T>::_smTypeId = 0;
 90
 91
 92// inline methods
 93
 94template<class T>
 95void SimObjectRef<T>::_set( const char *name )
 96{
 97   mName = StringTable->insert( name );
 98   mObject = NULL;
 99   mId = 0;
100}
101
102template<class T>
103void SimObjectRef<T>::_set( U32 id )
104{
105   mId = id;   
106   mObject = NULL;
107   mName = "";
108}
109
110template<class T> 
111T* SimObjectRef<T>::_get()
112{  
113   if ( mObject == NULL )   
114   {
115      if ( mId != 0 )
116         Sim::findObject( mId, mObject );   
117      else if ( mName != NULL && dStrlen( mName ) != 0 )
118         Sim::findObject( mName, mObject );
119   }
120
121   return mObject;
122}
123
124
125// SimObjectRefConsoleBaseType<T>
126
127template< class T >
128class SimObjectRefConsoleBaseType : public ConsoleBaseType
129{
130public:
131
132   typedef ConsoleBaseType Parent;
133   
134   SimObjectRefConsoleBaseType( const char *typeName )
135      : Parent( sizeof(SimObjectRef<T>), &SimObjectRef<T>::_smTypeId, typeName ) 
136   {
137   }
138
139public:
140
141   virtual const char* getData( void *dptr, const EnumTable*, BitSet32 )
142   {      
143      SimObjectRef<T> *objRef = static_cast< SimObjectRef<T>* >( dptr );
144      T *obj = *objRef;
145      return T::__getObjectId( obj );
146   }
147
148   virtual void setData( void* dptr, S32 argc, const char** argv, const EnumTable*, BitSet32 )
149   {
150      SimObjectRef<T> *objRef = static_cast< SimObjectRef<T>* >( dptr );
151
152      if ( argc != 1 ) 
153         return;
154
155      *objRef = argv[0];
156   }
157
158   virtual const bool isDatablock() 
159   { 
160      return T::__smIsDatablock; 
161   };
162
163   virtual const char* getTypeClassName()
164   {
165      return T::getStaticClassRep()->getClassName();
166   }
167      
168   virtual void* getNativeVariable() 
169   { 
170      SimObjectRef<T> *var = new SimObjectRef<T>(); 
171      return (void*)var; 
172   }
173   
174   virtual void deleteNativeVariable( void *var ) 
175   { 
176      SimObjectRef<T> *nativeVar = reinterpret_cast< SimObjectRef<T>* >( var ); 
177      delete nativeVar; 
178   }   
179};
180
181
182#endif // _SIMOBJECTREF_H_
183