simObjectPtr.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 _SIM_OBJECT_PTR_H_
  23. #define _SIM_OBJECT_PTR_H_
  24. #ifndef _PLATFORM_H_
  25. #include "platform/platform.h"
  26. #endif
  27. //---------------------------------------------------------------------------
  28. /// Smart SimObject pointer.
  29. ///
  30. /// This class keeps track of the book-keeping necessary
  31. /// to keep a registered reference to a SimObject or subclass
  32. /// thereof.
  33. ///
  34. /// Normally, if you want the SimObject to be aware that you
  35. /// have a reference to it, you must call SimObject::registerReference()
  36. /// when you create the reference, and SimObject::unregisterReference() when
  37. /// you're done. If you change the reference, you must also register/unregister
  38. /// it. This is a big headache, so this class exists to automatically
  39. /// keep track of things for you.
  40. ///
  41. /// @code
  42. /// // Assign an object to the
  43. /// SimObjectPtr<GameBase> mOrbitObject = Sim::findObject("anObject");
  44. ///
  45. /// // Use it as a GameBase*.
  46. /// mOrbitObject->getWorldBox().getCenter(&mPosition);
  47. ///
  48. /// // And reassign it - it will automatically update the references.
  49. /// mOrbitObject = Sim::findObject("anotherObject");
  50. /// @endcode
  51. template <class T> class SimObjectPtr
  52. {
  53. private:
  54. SimObject *mObj;
  55. public:
  56. SimObjectPtr() { mObj = 0; }
  57. SimObjectPtr(T* ptr)
  58. {
  59. mObj = ptr;
  60. if(mObj)
  61. mObj->registerReference(&mObj);
  62. }
  63. SimObjectPtr(const SimObjectPtr<T>& rhs)
  64. {
  65. mObj = const_cast<T*>(static_cast<const T*>(rhs));
  66. if(mObj)
  67. mObj->registerReference(&mObj);
  68. }
  69. SimObjectPtr<T>& operator=(const SimObjectPtr<T>& rhs)
  70. {
  71. if(this == &rhs)
  72. return(*this);
  73. if(mObj)
  74. mObj->unregisterReference(&mObj);
  75. mObj = const_cast<T*>(static_cast<const T*>(rhs));
  76. if(mObj)
  77. mObj->registerReference(&mObj);
  78. return(*this);
  79. }
  80. ~SimObjectPtr()
  81. {
  82. if(mObj)
  83. mObj->unregisterReference(&mObj);
  84. }
  85. SimObjectPtr<T>& operator= (T *ptr)
  86. {
  87. if(mObj != (SimObject *) ptr)
  88. {
  89. if(mObj)
  90. mObj->unregisterReference(&mObj);
  91. mObj = (SimObject *) ptr;
  92. if (mObj)
  93. mObj->registerReference(&mObj);
  94. }
  95. return *this;
  96. }
  97. #if defined(__MWERKS__) && (__MWERKS__ < 0x2400)
  98. // CW 5.3 seems to get confused comparing SimObjectPtrs...
  99. bool operator == (const SimObject *ptr) { return mObj == ptr; }
  100. bool operator != (const SimObject *ptr) { return mObj != ptr; }
  101. #endif
  102. bool isNull() const { return mObj == 0; }
  103. bool notNull() const { return mObj != 0; }
  104. T* operator->() const { return static_cast<T*>(mObj); }
  105. T& operator*() const { return *static_cast<T*>(mObj); }
  106. operator T*() const { return static_cast<T*>(mObj)? static_cast<T*>(mObj) : 0; }
  107. };
  108. #endif // _SIM_OBJECT_PTR_H_