BsGameObjectHandle.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. /** @name Internal
  94. * @{
  95. */
  96. /** Returns internal handle data. */
  97. std::shared_ptr<GameObjectHandleData> _getHandleData() const { return mData; }
  98. /** Resolves a handle to a proper GameObject in case it was created uninitialized. */
  99. void _resolve(const GameObjectHandleBase& object);
  100. /** Changes the GameObject instance the handle is pointing to. */
  101. void _setHandleData(const GameObjectPtr& object);
  102. /** @} */
  103. protected:
  104. friend class GameObjectManager;
  105. template<class _Ty1, class _Ty2>
  106. friend bool operator==(const GameObjectHandle<_Ty1>& _Left, const GameObjectHandle<_Ty2>& _Right);
  107. GameObjectHandleBase(const std::shared_ptr<GameObject> ptr);
  108. GameObjectHandleBase(const std::shared_ptr<GameObjectHandleData>& data);
  109. GameObjectHandleBase(std::nullptr_t ptr);
  110. /** Throws an exception if the referenced GameObject has been destroyed. */
  111. inline void throwIfDestroyed() const;
  112. /** Invalidates the handle signifying the referenced object was destroyed. */
  113. void destroy()
  114. {
  115. // It's important not to clear mData->mPtr as some code might rely
  116. // on it. (for example for restoring lost handles)
  117. if (mData->mPtr != nullptr)
  118. mData->mPtr->object = nullptr;
  119. }
  120. std::shared_ptr<GameObjectHandleData> mData;
  121. /************************************************************************/
  122. /* RTTI */
  123. /************************************************************************/
  124. public:
  125. friend class GameObjectHandleRTTI;
  126. static RTTITypeBase* getRTTIStatic();
  127. virtual RTTITypeBase* getRTTI() const override;
  128. };
  129. /** @} */
  130. /** @addtogroup Scene
  131. * @{
  132. */
  133. /**
  134. * @copydoc GameObjectHandleBase
  135. *
  136. * @note It is important this class contains no data since we often value cast it to its base.
  137. */
  138. template <typename T>
  139. class GameObjectHandle : public GameObjectHandleBase
  140. {
  141. public:
  142. /** Constructs a new empty handle. */
  143. GameObjectHandle()
  144. :GameObjectHandleBase()
  145. {
  146. mData = bs_shared_ptr_new<GameObjectHandleData>();
  147. }
  148. /** Copy constructor from another handle of the same type. */
  149. template <typename T1>
  150. GameObjectHandle(const GameObjectHandle<T1>& ptr)
  151. :GameObjectHandleBase()
  152. {
  153. mData = ptr._getHandleData();
  154. }
  155. /** Copy constructor from another handle of the base type. */
  156. GameObjectHandle(const GameObjectHandleBase& ptr)
  157. :GameObjectHandleBase()
  158. {
  159. mData = ptr._getHandleData();
  160. }
  161. /** Invalidates the handle. */
  162. GameObjectHandle<T>& operator=(std::nullptr_t ptr)
  163. {
  164. mData = bs_shared_ptr_new<GameObjectHandleData>();
  165. return *this;
  166. }
  167. /** Casts a specific handle to the base handle. */
  168. operator GameObjectHandleBase()
  169. {
  170. GameObjectHandleBase base(mData);
  171. return base;
  172. }
  173. /**
  174. * Returns a pointer to the referenced GameObject.
  175. *
  176. * @note Throws exception if the GameObject was destroyed.
  177. */
  178. T* get() const
  179. {
  180. throwIfDestroyed();
  181. return reinterpret_cast<T*>(mData->mPtr->object.get());
  182. }
  183. /**
  184. * Returns a smart pointer to the referenced GameObject.
  185. *
  186. * @note Throws exception if the GameObject was destroyed.
  187. */
  188. std::shared_ptr<T> getInternalPtr() const
  189. {
  190. throwIfDestroyed();
  191. return std::static_pointer_cast<T>(mData->mPtr->object);
  192. }
  193. /**
  194. * Returns pointer to the referenced GameObject.
  195. *
  196. * @note Throws exception if the GameObject was destroyed.
  197. */
  198. T* operator->() const { return get(); }
  199. /**
  200. * Returns reference to the referenced GameObject.
  201. *
  202. * @note Throws exception if the GameObject was destroyed.
  203. */
  204. T& operator*() const { return *get(); }
  205. public: // ***** INTERNAL ******
  206. /** @name Internal
  207. * @{
  208. */
  209. template<class _Ty>
  210. struct Bool_struct
  211. {
  212. int _Member;
  213. };
  214. /**
  215. * Allows direct conversion of handle to bool.
  216. *
  217. * @note
  218. * This is needed because we can't directly convert to bool since then we can assign pointer to bool and that's
  219. * weird.
  220. */
  221. operator int Bool_struct<T>::*() const
  222. {
  223. return (((mData->mPtr != nullptr) && (mData->mPtr->object != nullptr)) ? &Bool_struct<T>::_Member : 0);
  224. }
  225. /** @} */
  226. };
  227. /** Casts one GameObject handle type to another. */
  228. template<class _Ty1, class _Ty2>
  229. GameObjectHandle<_Ty1> static_object_cast(const GameObjectHandle<_Ty2>& other)
  230. {
  231. return GameObjectHandle<_Ty1>(other);
  232. }
  233. /** @cond INTERNAL */
  234. /** Compares if two handles point to the same GameObject. */
  235. template<class _Ty1, class _Ty2>
  236. bool operator==(const GameObjectHandle<_Ty1>& _Left, const GameObjectHandle<_Ty2>& _Right)
  237. {
  238. return (_Left.mData == nullptr && _Right.mData == nullptr) ||
  239. (_Left.mData != nullptr && _Right.mData != nullptr && _Left.getInstanceId() == _Right.getInstanceId());
  240. }
  241. /** Compares if two handles point to different GameObject%s. */
  242. template<class _Ty1, class _Ty2>
  243. bool operator!=(const GameObjectHandle<_Ty1>& _Left, const GameObjectHandle<_Ty2>& _Right)
  244. {
  245. return (!(_Left == _Right));
  246. }
  247. /** @endcond */
  248. /** @} */
  249. }