simPersistID.h

Engine/source/console/simPersistID.h

Persistent IDs for SimObjects.

More...

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