BsGameObjectHandle.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #pragma once
  2. namespace BansheeEngine
  3. {
  4. /** @addtogroup Implementation
  5. * @{
  6. */
  7. class GameObjectManager;
  8. template <typename T>
  9. class GameObjectHandle;
  10. /** @cond INTERNAL */
  11. /** Internal data shared between GameObject handles. */
  12. struct BS_CORE_EXPORT GameObjectHandleData
  13. {
  14. GameObjectHandleData()
  15. :mPtr(nullptr)
  16. { }
  17. GameObjectHandleData(const std::shared_ptr<GameObjectInstanceData>& ptr)
  18. {
  19. mPtr = ptr;
  20. }
  21. std::shared_ptr<GameObjectInstanceData> mPtr;
  22. };
  23. /** @endcond */
  24. /**
  25. * A handle that can point to various types of game objects. It primarily keeps track if the object is still alive,
  26. * so anything still referencing it doesn't accidentally use it.
  27. *
  28. * @note
  29. * This class exists because references between game objects should be quite loose. For example one game object should
  30. * be able to reference another one without the other one knowing. But if that is the case I also need to handle the
  31. * case when the other object we're referencing has been deleted, and that is the main purpose of this class.
  32. */
  33. class BS_CORE_EXPORT GameObjectHandleBase : public IReflectable
  34. {
  35. public:
  36. GameObjectHandleBase();
  37. /**
  38. * Returns true if the object the handle is pointing to has been destroyed.
  39. *
  40. * @param[in] checkQueued Game objects can be queued for destruction but not actually destroyed yet, and still
  41. * accessible. If this is false this method will return true only if the object is
  42. * completely inaccessible (fully destroyed). If this is true this method will return true
  43. * if object is completely inaccessible or if it is just queued for destruction.
  44. */
  45. bool isDestroyed(bool checkQueued = false) const
  46. {
  47. return mData->mPtr == nullptr || mData->mPtr->object == nullptr
  48. || (checkQueued && mData->mPtr->object->_getIsDestroyed());
  49. }
  50. /** Returns the instance ID of the object the handle is referencing. */
  51. UINT64 getInstanceId() const { return mData->mPtr != nullptr ? mData->mPtr->mInstanceId : 0; }
  52. /**
  53. * Returns pointer to the referenced GameObject.
  54. *
  55. * @note Throws exception if the GameObject was destroyed.
  56. */
  57. GameObject* get() const
  58. {
  59. throwIfDestroyed();
  60. return mData->mPtr->object.get();
  61. }
  62. /**
  63. * Returns a smart pointer to the referenced GameObject.
  64. *
  65. * @note Throws exception if the GameObject was destroyed.
  66. */
  67. std::shared_ptr<GameObject> getInternalPtr() const
  68. {
  69. throwIfDestroyed();
  70. return mData->mPtr->object;
  71. }
  72. /**
  73. * Returns pointer to the referenced GameObject.
  74. *
  75. * @note Throws exception if the GameObject was destroyed.
  76. */
  77. GameObject* operator->() const { return get(); }
  78. /**
  79. * Returns reference to the referenced GameObject.
  80. *
  81. * @note Throws exception if the GameObject was destroyed.
  82. */
  83. GameObject& operator*() const { return *get(); }
  84. /** @cond INTERNAL */
  85. /** Returns internal handle data. */
  86. std::shared_ptr<GameObjectHandleData> _getHandleData() const { return mData; }
  87. /** Resolves a handle to a proper GameObject in case it was created uninitialized. */
  88. void _resolve(const GameObjectHandleBase& object);
  89. /** Changes the GameObject instance the handle is pointing to. */
  90. void _setHandleData(const GameObjectPtr& object);
  91. /** @endcond */
  92. protected:
  93. friend class GameObjectManager;
  94. template<class _Ty1, class _Ty2>
  95. friend bool operator==(const GameObjectHandle<_Ty1>& _Left, const GameObjectHandle<_Ty2>& _Right);
  96. GameObjectHandleBase(const std::shared_ptr<GameObject> ptr);
  97. GameObjectHandleBase(const std::shared_ptr<GameObjectHandleData>& data);
  98. GameObjectHandleBase(std::nullptr_t ptr);
  99. /** Throws an exception if the referenced GameObject has been destroyed. */
  100. inline void throwIfDestroyed() const;
  101. /** Invalidates the handle signifying the referenced object was destroyed. */
  102. void destroy()
  103. {
  104. // It's important not to clear mData->mPtr as some code might rely
  105. // on it. (e.g. for restoring lost handles)
  106. if (mData->mPtr != nullptr)
  107. mData->mPtr->object = nullptr;
  108. }
  109. std::shared_ptr<GameObjectHandleData> mData;
  110. /************************************************************************/
  111. /* RTTI */
  112. /************************************************************************/
  113. public:
  114. friend class GameObjectHandleRTTI;
  115. static RTTITypeBase* getRTTIStatic();
  116. virtual RTTITypeBase* getRTTI() const override;
  117. };
  118. /** @} */
  119. /** @addtogroup Scene
  120. * @{
  121. */
  122. /**
  123. * @copydoc GameObjectHandleBase
  124. *
  125. * @note It is important this class contains no data since we often value cast it to its base.
  126. */
  127. template <typename T>
  128. class GameObjectHandle : public GameObjectHandleBase
  129. {
  130. public:
  131. /** Constructs a new empty handle. */
  132. GameObjectHandle()
  133. :GameObjectHandleBase()
  134. {
  135. mData = bs_shared_ptr_new<GameObjectHandleData>();
  136. }
  137. /** Copy constructor from another handle of the same type. */
  138. template <typename T1>
  139. GameObjectHandle(const GameObjectHandle<T1>& ptr)
  140. :GameObjectHandleBase()
  141. {
  142. mData = ptr._getHandleData();
  143. }
  144. /** Copy constructor from another handle of the base type. */
  145. GameObjectHandle(const GameObjectHandleBase& ptr)
  146. :GameObjectHandleBase()
  147. {
  148. mData = ptr._getHandleData();
  149. }
  150. /** Invalidates the handle. */
  151. GameObjectHandle<T>& operator=(std::nullptr_t ptr)
  152. {
  153. mData = bs_shared_ptr_new<GameObjectHandleData>();
  154. return *this;
  155. }
  156. /** Casts a specific handle to the base handle. */
  157. operator GameObjectHandleBase()
  158. {
  159. GameObjectHandleBase base(mData);
  160. return base;
  161. }
  162. /**
  163. * Returns a pointer to the referenced GameObject.
  164. *
  165. * @note Throws exception if the GameObject was destroyed.
  166. */
  167. T* get() const
  168. {
  169. throwIfDestroyed();
  170. return reinterpret_cast<T*>(mData->mPtr->object.get());
  171. }
  172. /**
  173. * Returns a smart pointer to the referenced GameObject.
  174. *
  175. * @note Throws exception if the GameObject was destroyed.
  176. */
  177. std::shared_ptr<T> getInternalPtr() const
  178. {
  179. throwIfDestroyed();
  180. return std::static_pointer_cast<T>(mData->mPtr->object);
  181. }
  182. /**
  183. * Returns pointer to the referenced GameObject.
  184. *
  185. * @note Throws exception if the GameObject was destroyed.
  186. */
  187. T* operator->() const { return get(); }
  188. /**
  189. * Returns reference to the referenced GameObject.
  190. *
  191. * @note Throws exception if the GameObject was destroyed.
  192. */
  193. T& operator*() const { return *get(); }
  194. /** @cond INTERNAL */
  195. template<class _Ty>
  196. struct Bool_struct
  197. {
  198. int _Member;
  199. };
  200. /**
  201. * Allows direct conversion of handle to bool.
  202. *
  203. * @note
  204. * This is needed because we can't directly convert to bool since then we can assign pointer to bool and that's
  205. * weird.
  206. */
  207. operator int Bool_struct<T>::*() const
  208. {
  209. return (((mData->mPtr != nullptr) && (mData->mPtr->object != nullptr)) ? &Bool_struct<T>::_Member : 0);
  210. }
  211. /** @endcond */
  212. };
  213. /** Casts one GameObject handle type to another. */
  214. template<class _Ty1, class _Ty2>
  215. GameObjectHandle<_Ty1> static_object_cast(const GameObjectHandle<_Ty2>& other)
  216. {
  217. return GameObjectHandle<_Ty1>(other);
  218. }
  219. /** @cond INTERNAL */
  220. /** Compares if two handles point to the same GameObject. */
  221. template<class _Ty1, class _Ty2>
  222. bool operator==(const GameObjectHandle<_Ty1>& _Left, const GameObjectHandle<_Ty2>& _Right)
  223. {
  224. return (_Left.mData == nullptr && _Right.mData == nullptr) ||
  225. (_Left.mData != nullptr && _Right.mData != nullptr && _Left.getInstanceId() == _Right.getInstanceId());
  226. }
  227. /** Compares if two handles point to different GameObject%s. */
  228. template<class _Ty1, class _Ty2>
  229. bool operator!=(const GameObjectHandle<_Ty1>& _Left, const GameObjectHandle<_Ty2>& _Right)
  230. {
  231. return (!(_Left == _Right));
  232. }
  233. /** @endcond */
  234. /** @} */
  235. }