simPersistID.cpp
Engine/source/console/simPersistID.cpp
Public Variables
Public Functions
DefineNewEngineMethod(SimPersistID , getObject , SimObject * , () , "" )
DefineNewEngineMethod(SimPersistID , getUUID , Torque::UUID , () , "" )
Detailed Description
Public Variables
END_IMPLEMENT_CLASS
Public Functions
DefineNewEngineMethod(SimPersistID , getObject , SimObject * , () , "" )
DefineNewEngineMethod(SimPersistID , getUUID , Torque::UUID , () , "" )
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#include "console/simPersistID.h" 25#include "console/simObject.h" 26#include "core/util/tDictionary.h" 27#include "core/util/safeDelete.h" 28#include "engineAPI.h" 29 30 31//#define DEBUG_SPEW 32 33IMPLEMENT_CLASS(SimPersistID, "") 34END_IMPLEMENT_CLASS; 35 36SimPersistID::LookupTableType* SimPersistID::smLookupTable; 37 38 39//----------------------------------------------------------------------------- 40 41SimPersistID::SimPersistID() 42{ 43 mObject = NULL; 44 mUUID.generate(); 45 smLookupTable->insertUnique(mUUID, this); 46} 47 48SimPersistID::SimPersistID( SimObject* object ) 49 : mObject( object ) 50{ 51 AssertFatal( object, "SimPersistID::SimPersistID - got a NULL object!" ); 52 AssertFatal( !object->getPersistentId(), "SimPersistID::SimPersistID - object already has a persistent ID!" ); 53 54 mUUID.generate(); 55 smLookupTable->insertUnique( mUUID, this ); 56} 57 58//----------------------------------------------------------------------------- 59 60SimPersistID::SimPersistID( const Torque::UUID& uuid ) 61 : mObject( NULL ), 62 mUUID( uuid ) 63{ 64 AssertFatal( !uuid.isNull(), "SimPersistID::SimPersistID - invalid UUID!" ); 65 smLookupTable->insertUnique( mUUID, this ); 66} 67 68//----------------------------------------------------------------------------- 69 70SimPersistID::~SimPersistID() 71{ 72 smLookupTable->erase( getUUID() ); 73} 74 75//----------------------------------------------------------------------------- 76 77void SimPersistID::init() 78{ 79 smLookupTable = new LookupTableType; 80} 81 82//----------------------------------------------------------------------------- 83 84void SimPersistID::shutdown() 85{ 86 SAFE_DELETE( smLookupTable ); 87} 88 89//----------------------------------------------------------------------------- 90 91SimPersistID* SimPersistID::create( SimObject* object ) 92{ 93 SimPersistID* pid = new SimPersistID( object ); 94 95 #ifdef DEBUG_SPEW 96 Platform::outputDebugString( "[SimPersistID] Created new pid for object %i:%s (%s) with uuid '%s'", 97 object->getId(), object->getClassName(), object->getName(), 98 pid->getUUID().toString().c_str() ); 99 #endif 100 101 return pid; 102} 103 104//----------------------------------------------------------------------------- 105 106void SimPersistID::resolve( SimObject* object ) 107{ 108 AssertFatal( !mObject, "SimPersistID::resolve - PID is already resolved!" ); 109 mObject = object; 110 111 #ifdef DEBUG_SPEW 112 Platform::outputDebugString( "[SimPersistID] Resolving pid '%s' to %i:%s (%s)", 113 getUUID().toString().c_str(), 114 object->getId(), object->getClassName(), object->getName() ); 115 #endif 116} 117 118//----------------------------------------------------------------------------- 119 120SimPersistID* SimPersistID::find( const Torque::UUID& uuid ) 121{ 122 AssertFatal( smLookupTable, "SimPersistID::find - system has not been initialized" ); 123 124 LookupTableType::Iterator iter = smLookupTable->find( uuid ); 125 if( iter != smLookupTable->end() ) 126 return iter->value; 127 128 return NULL; 129} 130 131//----------------------------------------------------------------------------- 132 133SimPersistID* SimPersistID::findOrCreate( const Torque::UUID& uuid ) 134{ 135 AssertFatal( smLookupTable, "SimPersistID::findOrCreate - system has not been initialized" ); 136 137 SimPersistID* pid = find( uuid ); 138 if( !pid ) 139 { 140 pid = new SimPersistID( uuid ); 141 142 #ifdef DEBUG_SPEW 143 Platform::outputDebugString( "[SimPersistID] Created unresolved pid for UUID '%s'", 144 uuid.toString().c_str() ); 145 #endif 146 } 147 148 return pid; 149} 150 151DefineNewEngineMethod(SimPersistID, getUUID, Torque::UUID, (), , "") 152{ 153 return object->getUUID(); 154} 155 156DefineNewEngineMethod(SimPersistID, getObject, SimObject*, (), , "") 157{ 158 return object->getObject(); 159} 160