simPersistID.cpp 4.8 KB

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