simPersistID.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #include "console/simPersistID.h"
  23. #include "console/simObject.h"
  24. #include "core/util/tDictionary.h"
  25. #include "core/util/safeDelete.h"
  26. //#define DEBUG_SPEW
  27. SimPersistID::LookupTableType* SimPersistID::smLookupTable;
  28. //-----------------------------------------------------------------------------
  29. SimPersistID::SimPersistID( SimObject* object )
  30. : mObject( object )
  31. {
  32. AssertFatal( object, "SimPersistID::SimPersistID - got a NULL object!" );
  33. AssertFatal( !object->getPersistentId(), "SimPersistID::SimPersistID - object already has a persistent ID!" );
  34. mUUID.generate();
  35. smLookupTable->insertUnique( mUUID, this );
  36. }
  37. //-----------------------------------------------------------------------------
  38. SimPersistID::SimPersistID( const Torque::UUID& uuid )
  39. : mUUID( uuid ),
  40. mObject( NULL )
  41. {
  42. AssertFatal( !uuid.isNull(), "SimPersistID::SimPersistID - invalid UUID!" );
  43. smLookupTable->insertUnique( mUUID, this );
  44. }
  45. //-----------------------------------------------------------------------------
  46. SimPersistID::~SimPersistID()
  47. {
  48. smLookupTable->erase( getUUID() );
  49. }
  50. //-----------------------------------------------------------------------------
  51. void SimPersistID::init()
  52. {
  53. smLookupTable = new LookupTableType;
  54. }
  55. //-----------------------------------------------------------------------------
  56. void SimPersistID::shutdown()
  57. {
  58. SAFE_DELETE( smLookupTable );
  59. }
  60. //-----------------------------------------------------------------------------
  61. SimPersistID* SimPersistID::create( SimObject* object )
  62. {
  63. SimPersistID* pid = new SimPersistID( object );
  64. #ifdef DEBUG_SPEW
  65. Platform::outputDebugString( "[SimPersistID] Created new pid for object %i:%s (%s) with uuid '%s'",
  66. object->getId(), object->getClassName(), object->getName(),
  67. pid->getUUID().toString().c_str() );
  68. #endif
  69. return pid;
  70. }
  71. //-----------------------------------------------------------------------------
  72. void SimPersistID::resolve( SimObject* object )
  73. {
  74. AssertFatal( !mObject, "SimPersistID::resolve - PID is already resolved!" );
  75. mObject = object;
  76. #ifdef DEBUG_SPEW
  77. Platform::outputDebugString( "[SimPersistID] Resolving pid '%s' to %i:%s (%s)",
  78. getUUID().toString().c_str(),
  79. object->getId(), object->getClassName(), object->getName() );
  80. #endif
  81. }
  82. //-----------------------------------------------------------------------------
  83. SimPersistID* SimPersistID::find( const Torque::UUID& uuid )
  84. {
  85. AssertFatal( smLookupTable, "SimPersistID::find - system has not been initialized" );
  86. LookupTableType::Iterator iter = smLookupTable->find( uuid );
  87. if( iter != smLookupTable->end() )
  88. return iter->value;
  89. return NULL;
  90. }
  91. //-----------------------------------------------------------------------------
  92. SimPersistID* SimPersistID::findOrCreate( const Torque::UUID& uuid )
  93. {
  94. AssertFatal( smLookupTable, "SimPersistID::findOrCreate - system has not been initialized" );
  95. SimPersistID* pid = find( uuid );
  96. if( !pid )
  97. {
  98. pid = new SimPersistID( uuid );
  99. #ifdef DEBUG_SPEW
  100. Platform::outputDebugString( "[SimPersistID] Created unresolved pid for UUID '%s'",
  101. uuid.toString().c_str() );
  102. #endif
  103. }
  104. return pid;
  105. }