//----------------------------------------------------------------------------- // Copyright (c) 2012 GarageGames, LLC // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to // deal in the Software without restriction, including without limitation the // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or // sell copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS // IN THE SOFTWARE. //----------------------------------------------------------------------------- #ifndef _SIMOBJECTREF_H_ #define _SIMOBJECTREF_H_ #ifndef _DYNAMIC_CONSOLETYPES_H_ #include "console/dynamicTypes.h" #endif #ifndef _SIM_H_ #include "console/sim.h" #endif // SimObjectRef template< class T > class SimObjectRef { public: static S32 _smTypeId; SimObjectRef() : mName( "" ), mId( 0 ), mObject( NULL ) { } T* operator-> () { return _get(); } operator T*() { return _get(); } operator bool () { return _get() != NULL; } SimObjectRef& operator= ( U32 id ) { _set( id ); return *this; } SimObjectRef& operator= ( const char *name ) { _set( name ); return *this; } protected: T* _get(); void _set( U32 id ); void _set( const char *name ); protected: StringTableEntry mName; U32 mId; T *mObject; }; // static initialization template S32 SimObjectRef::_smTypeId = 0; // inline methods template void SimObjectRef::_set( const char *name ) { mName = StringTable->insert( name ); mObject = NULL; mId = 0; } template void SimObjectRef::_set( U32 id ) { mId = id; mObject = NULL; mName = ""; } template T* SimObjectRef::_get() { if ( mObject == NULL ) { if ( mId != 0 ) Sim::findObject( mId, mObject ); else if ( mName != NULL && dStrlen( mName ) != 0 ) Sim::findObject( mName, mObject ); } return mObject; } // SimObjectRefConsoleBaseType template< class T > class SimObjectRefConsoleBaseType : public ConsoleBaseType { public: typedef ConsoleBaseType Parent; SimObjectRefConsoleBaseType( const char *typeName ) : Parent( sizeof(SimObjectRef), &SimObjectRef::_smTypeId, typeName ) { } public: virtual const char* getData( void *dptr, const EnumTable*, BitSet32 ) { SimObjectRef *objRef = static_cast< SimObjectRef* >( dptr ); T *obj = *objRef; return T::__getObjectId( obj ); } virtual void setData( void* dptr, S32 argc, const char** argv, const EnumTable*, BitSet32 ) { SimObjectRef *objRef = static_cast< SimObjectRef* >( dptr ); if ( argc != 1 ) return; *objRef = argv[0]; } virtual const bool isDatablock() { return T::__smIsDatablock; }; virtual const char* getTypeClassName() { return T::getStaticClassRep()->getClassName(); } virtual void* getNativeVariable() { SimObjectRef *var = new SimObjectRef(); return (void*)var; } virtual void deleteNativeVariable( void *var ) { SimObjectRef *nativeVar = reinterpret_cast< SimObjectRef* >( var ); delete nativeVar; } }; #endif // _SIMOBJECTREF_H_