BsGameObjectHandle.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #pragma once
  2. namespace BansheeEngine
  3. {
  4. class GameObjectManager;
  5. /**
  6. * @brief Internal data shared between GameObject handles.
  7. */
  8. struct BS_CORE_EXPORT GameObjectHandleData
  9. {
  10. GameObjectHandleData()
  11. :mPtr(nullptr), mInstanceId(0)
  12. { }
  13. GameObjectHandleData(const std::shared_ptr<GameObjectInstanceData>& ptr)
  14. {
  15. mPtr = ptr;
  16. if(ptr != nullptr)
  17. mInstanceId = ptr->object->getInstanceId();
  18. else
  19. mInstanceId = 0;
  20. }
  21. std::shared_ptr<GameObjectInstanceData> mPtr;
  22. UINT64 mInstanceId;
  23. };
  24. /**
  25. * @brief A handle that can point to various types of game objects.
  26. * It primarily keeps track if the object is still alive, so anything
  27. * still referencing it doesn't accidentally use it.
  28. *
  29. * @note This class exists because references between game objects should be quite loose.
  30. * For example one game object should be able to reference another one without the other
  31. * one knowing. But if that is the case I also need to handle the case when the other
  32. * object we're referencing has been deleted, and that is the main purpose of this class.
  33. *
  34. */
  35. class BS_CORE_EXPORT GameObjectHandleBase : public IReflectable
  36. {
  37. public:
  38. GameObjectHandleBase();
  39. /**
  40. * @brief Returns true if the object the handle is pointing to has been destroyed.
  41. */
  42. bool isDestroyed() const { return mData->mPtr == nullptr || mData->mPtr->object == nullptr; }
  43. /**
  44. * @brief Returns the instance ID of the object the handle is referencing.
  45. */
  46. UINT64 getInstanceId() const { return mData->mInstanceId; }
  47. /**
  48. * @brief Returns pointer to the referenced GameObject.
  49. *
  50. * @note Throws exception if the GameObject was destroyed.
  51. */
  52. GameObject* get() const
  53. {
  54. throwIfDestroyed();
  55. return mData->mPtr->object.get();
  56. }
  57. /**
  58. * @brief Returns a smart pointer to the referenced GameObject.
  59. *
  60. * @note Throws exception if the GameObject was destroyed.
  61. */
  62. std::shared_ptr<GameObject> getInternalPtr() const
  63. {
  64. throwIfDestroyed();
  65. return mData->mPtr->object;
  66. }
  67. /**
  68. * @brief Returns pointer to the referenced GameObject.
  69. *
  70. * @note Throws exception if the GameObject was destroyed.
  71. */
  72. GameObject* operator->() const { return get(); }
  73. /**
  74. * @brief Returns reference to the referenced GameObject.
  75. *
  76. * @note Throws exception if the GameObject was destroyed.
  77. */
  78. GameObject& operator*() const { return *get(); }
  79. /**
  80. * @brief Returns internal handle data.
  81. *
  82. * @note Internal method.
  83. */
  84. std::shared_ptr<GameObjectHandleData> _getHandleData() const { return mData; }
  85. /**
  86. * @brief Resolves a handle to a proper GameObject in case it was created uninitialized.
  87. *
  88. * @note Internal method.
  89. */
  90. void _resolve(const GameObjectHandleBase& object);
  91. /**
  92. * @brief Changes the GameObject instance the handle is pointing to.
  93. *
  94. * @note Internal method.
  95. */
  96. void _setHandleData(const GameObjectPtr& object);
  97. protected:
  98. friend class SceneObject;
  99. friend class SceneObjectRTTI;
  100. friend class GameObjectManager;
  101. GameObjectHandleBase(const std::shared_ptr<GameObject> ptr);
  102. GameObjectHandleBase(const std::shared_ptr<GameObjectHandleData>& data);
  103. GameObjectHandleBase(std::nullptr_t ptr);
  104. /**
  105. * @brief Throws an exception if the referenced GameObject has been destroyed.
  106. */
  107. inline void throwIfDestroyed() const;
  108. /**
  109. * @brief Invalidates the handle signifying the referenced object was destroyed.
  110. */
  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;
  126. };
  127. /**
  128. * @copydoc GameObjectHandleBase
  129. *
  130. * @note It is important this class contains no data since we often
  131. * value cast it to its base.
  132. */
  133. template <typename T>
  134. class GameObjectHandle : public GameObjectHandleBase
  135. {
  136. public:
  137. /**
  138. * @brief Constructs a new empty handle.
  139. */
  140. GameObjectHandle()
  141. :GameObjectHandleBase()
  142. {
  143. mData = bs_shared_ptr<GameObjectHandleData, PoolAlloc>();
  144. }
  145. /**
  146. * @brief Copy constructor from another handle of the same type.
  147. */
  148. template <typename T1>
  149. GameObjectHandle(const GameObjectHandle<T1>& ptr)
  150. :GameObjectHandleBase()
  151. {
  152. mData = ptr._getHandleData();
  153. }
  154. /**
  155. * @brief Copy constructor from another handle of the base type.
  156. */
  157. GameObjectHandle(const GameObjectHandleBase& ptr)
  158. :GameObjectHandleBase()
  159. {
  160. mData = ptr._getHandleData();
  161. }
  162. /**
  163. * @brief Invalidates the handle.
  164. */
  165. inline GameObjectHandle<T>& operator=(std::nullptr_t ptr)
  166. {
  167. mData = bs_shared_ptr<GameObjectHandleData, PoolAlloc>();
  168. return *this;
  169. }
  170. /**
  171. * @brief Casts a specific handle to the base handle.
  172. */
  173. inline operator GameObjectHandleBase()
  174. {
  175. GameObjectHandleBase base(mData);
  176. return base;
  177. }
  178. /**
  179. * @brief Returns a pointer to the referenced GameObject.
  180. *
  181. * @note Throws exception if the GameObject was destroyed.
  182. */
  183. T* get() const
  184. {
  185. throwIfDestroyed();
  186. return reinterpret_cast<T*>(mData->mPtr->object.get());
  187. }
  188. /**
  189. * @brief Returns a smart pointer to the referenced GameObject.
  190. *
  191. * @note Throws exception if the GameObject was destroyed.
  192. */
  193. std::shared_ptr<T> getInternalPtr() const
  194. {
  195. throwIfDestroyed();
  196. return std::static_pointer_cast<T>(mData->mPtr->object);
  197. }
  198. /**
  199. * @brief Returns pointer to the referenced GameObject.
  200. *
  201. * @note Throws exception if the GameObject was destroyed.
  202. */
  203. T* operator->() const { return get(); }
  204. /**
  205. * @brief Returns reference to the referenced GameObject.
  206. *
  207. * @note Throws exception if the GameObject was destroyed.
  208. */
  209. T& operator*() const { return *get(); }
  210. template<class _Ty>
  211. struct Bool_struct
  212. {
  213. int _Member;
  214. };
  215. /**
  216. * @brief Allows direct conversion of handle to bool.
  217. *
  218. * @note This is needed because we can't directly convert to bool
  219. * since then we can assign pointer to bool and that's 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. private:
  226. friend class SceneObject;
  227. friend class SceneObjectRTTI;
  228. friend class GameObjectManager;
  229. /**
  230. * @brief Creates a GameObject handle from a smart pointer.
  231. */
  232. explicit GameObjectHandle(const std::shared_ptr<T> ptr)
  233. :GameObjectHandleBase(ptr)
  234. { }
  235. };
  236. /**
  237. * @brief Casts one GameObject handle type to another.
  238. */
  239. template<class _Ty1, class _Ty2>
  240. GameObjectHandle<_Ty1> static_object_cast(const GameObjectHandle<_Ty2>& other)
  241. {
  242. return GameObjectHandle<_Ty1>(other);
  243. }
  244. /**
  245. * @brief Compares if two handles point to the same GameObject.
  246. */
  247. template<class _Ty1, class _Ty2>
  248. bool operator==(const GameObjectHandle<_Ty1>& _Left, const GameObjectHandle<_Ty2>& _Right)
  249. {
  250. return (_Left == nullptr && _Right == nullptr) || (_Left != nullptr && _Right != nullptr && _Left.get() == _Right.get());
  251. }
  252. /**
  253. * @brief Compares if two handles point to different GameObjects.
  254. */
  255. template<class _Ty1, class _Ty2>
  256. bool operator!=(const GameObjectHandle<_Ty1>& _Left, const GameObjectHandle<_Ty2>& _Right)
  257. {
  258. return (!(_Left == _Right));
  259. }
  260. }