BsGameObjectHandle.h 8.1 KB

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