simPersistID.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _SIMPERSISTID_H_
  23. #define _SIMPERSISTID_H_
  24. #ifndef _TORQUE_UUID_H_
  25. #include "core/util/uuid.h"
  26. #endif
  27. #ifndef _REFBASE_H_
  28. #include "core/util/refBase.h"
  29. #endif
  30. #ifndef _ENGINEOBJECT_H_
  31. #include "console/engineObject.h"
  32. #endif
  33. /// @file
  34. /// Persistent IDs for SimObjects.
  35. class SimObject;
  36. template< typename, typename > class HashTable;
  37. /// A globally unique persistent ID for a SimObject.
  38. class SimPersistID : public EngineObject
  39. {
  40. public:
  41. DECLARE_CLASS(SimPersistID, EngineObject);
  42. typedef void Parent;
  43. friend class SimObject;
  44. ///
  45. SimPersistID();
  46. /// Construct a new persistent ID for "object" by generating a fresh
  47. /// unique identifier.
  48. SimPersistID(SimObject* object);
  49. /// Construct a persistent ID stub for the given unique identifier.
  50. /// The stub remains not bound to any object until it is resolved.
  51. SimPersistID(const Torque::UUID& uuid);
  52. ///
  53. ~SimPersistID();
  54. protected:
  55. typedef HashTable< Torque::UUID, SimPersistID* > LookupTableType;
  56. /// Reference to the SimObject. Will be NULL for as long as the
  57. /// persistent ID is not resolved.
  58. SimObject* mObject;
  59. /// The UUID assigned to the object. Never changes.
  60. Torque::UUID mUUID;
  61. /// Table of persistent object IDs.
  62. static LookupTableType* smLookupTable;
  63. /// Bind this unresolved PID to the given object.
  64. void resolve( SimObject* object );
  65. ///
  66. void unresolve() { mObject = NULL; }
  67. /// Create a persistent ID for the given object.
  68. static SimPersistID* create( SimObject* object );
  69. public:
  70. /// Initialize the persistent ID system.
  71. static void init();
  72. /// Uninitialize the persistent ID system.
  73. static void shutdown();
  74. /// Look up a persistent ID by its UUID. Return NULL if no PID is bound to the given UUID.
  75. static SimPersistID* find( const Torque::UUID& uuid );
  76. /// Look up a persistent ID by its UUID. If no PID is bound to the given UUID yet, create a
  77. /// new PID and bind it to the UUID.
  78. static SimPersistID* findOrCreate( const Torque::UUID& uuid );
  79. /// Find a SimObject by the UUID assigned to its PID. Return NULL if either no PID is bound
  80. /// to the given UUID or if the PID bound to it is not yet resolved.
  81. static SimObject* findObjectByUUID( const Torque::UUID& uuid );
  82. /// Return the object that is bound to this PID. If the PID has not yet been resolved,
  83. /// return NULL.
  84. SimObject* getObject() const { return mObject; }
  85. /// Return the UUID bound to this PID.
  86. const Torque::UUID& getUUID() const { return mUUID; }
  87. };
  88. #endif // !_SIMPERSISTID_H_