simPersistID.h
Engine/source/console/simPersistID.h
Persistent IDs for SimObjects.
Classes:
class
A globally unique persistent ID for a SimObject.
Detailed Description
Persistent IDs for SimObjects.
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 _SIMPERSISTID_H_ 25#define _SIMPERSISTID_H_ 26 27#ifndef _TORQUE_UUID_H_ 28 #include "core/util/uuid.h" 29#endif 30#ifndef _REFBASE_H_ 31 #include "core/util/refBase.h" 32#endif 33 34 35/// @file 36/// Persistent IDs for SimObjects. 37 38 39class SimObject; 40template< typename, typename > class HashTable; 41 42 43/// A globally unique persistent ID for a SimObject. 44class SimPersistID : public StrongRefBase 45{ 46 public: 47 48 typedef void Parent; 49 friend class SimObject; 50 51 protected: 52 53 typedef HashTable< Torque::UUID, SimPersistID*> LookupTableType; 54 55 /// Reference to the SimObject. Will be NULL for as long as the 56 /// persistent ID is not resolved. 57 SimObject* mObject; 58 59 /// The UUID assigned to the object. Never changes. 60 Torque::UUID mUUID; 61 62 /// Table of persistent object IDs. 63 static LookupTableType* smLookupTable; 64 65 /// Construct a new persistent ID for "object" by generating a fresh 66 /// unique identifier. 67 SimPersistID( SimObject* object ); 68 69 /// Construct a persistent ID stub for the given unique identifier. 70 /// The stub remains not bound to any object until it is resolved. 71 SimPersistID( const Torque::UUID& uuid ); 72 73 /// 74 ~SimPersistID(); 75 76 /// Bind this unresolved PID to the given object. 77 void resolve( SimObject* object ); 78 79 /// 80 void unresolve() { mObject = NULL; } 81 82 /// Create a persistent ID for the given object. 83 static SimPersistID* create( SimObject* object ); 84 85 public: 86 87 /// Initialize the persistent ID system. 88 static void init(); 89 90 /// Uninitialize the persistent ID system. 91 static void shutdown(); 92 93 /// Look up a persistent ID by its UUID. Return NULL if no PID is bound to the given UUID. 94 static SimPersistID* find( const Torque::UUID& uuid ); 95 96 /// Look up a persistent ID by its UUID. If no PID is bound to the given UUID yet, create a 97 /// new PID and bind it to the UUID. 98 static SimPersistID* findOrCreate( const Torque::UUID& uuid ); 99 100 /// Find a SimObject by the UUID assigned to its PID. Return NULL if either no PID is bound 101 /// to the given UUID or if the PID bound to it is not yet resolved. 102 static SimObject* findObjectByUUID( const Torque::UUID& uuid ); 103 104 /// Return the object that is bound to this PID. If the PID has not yet been resolved, 105 /// return NULL. 106 SimObject* getObject() const { return mObject; } 107 108 /// Return the UUID bound to this PID. 109 const Torque::UUID& getUUID() const { return mUUID; } 110}; 111 112#endif // !_SIMPERSISTID_H_ 113
