Reference.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  5. #include <atomic>
  6. JPH_SUPPRESS_WARNINGS_STD_END
  7. JPH_NAMESPACE_BEGIN
  8. // Forward declares
  9. template <class T> class Ref;
  10. template <class T> class RefConst;
  11. /// Simple class to facilitate reference counting / releasing
  12. /// Derive your class from RefTarget and you can reference it by using Ref<classname> or RefConst<classname>
  13. ///
  14. /// Reference counting classes keep an integer which indicates how many references
  15. /// to the object are active. Reference counting objects are derived from RefTarget
  16. /// and staT & their life with a reference count of zero. They can then be assigned
  17. /// to equivalents of pointers (Ref) which will increase the reference count immediately.
  18. /// If the destructor of Ref is called or another object is assigned to the reference
  19. /// counting pointer it will decrease the reference count of the object again. If this
  20. /// reference count becomes zero, the object is destroyed.
  21. ///
  22. /// This provides a very powerful mechanism to prevent memory leaks, but also gives
  23. /// some responsibility to the programmer. The most notable point is that you cannot
  24. /// have one object reference another and have the other reference the first one
  25. /// back, because this way the reference count of both objects will never become
  26. /// lower than 1, resulting in a memory leak. By carefully designing your classses
  27. /// (and particularly identifying who owns who in the class hierarchy) you can avoid
  28. /// these problems.
  29. template <class T>
  30. class RefTarget
  31. {
  32. public:
  33. /// Constructor
  34. inline RefTarget() = default;
  35. inline RefTarget(const RefTarget &) { /* Do not copy refcount */ }
  36. inline ~RefTarget() { JPH_ASSERT(mRefCount == 0 || mRefCount == cEmbedded); } ///< assert no one is referencing us
  37. /// Mark this class as embedded, this means the type can be used in a compound or constructed on the stack.
  38. /// The Release function will never destruct the object, it is assumed the destructor will be called by whoever allocated
  39. /// the object and at that point in time it is checked that no references are left to the structure.
  40. inline void SetEmbedded() { JPH_ASSERT(mRefCount < cEmbedded); mRefCount += cEmbedded; }
  41. /// Assignment operator
  42. inline RefTarget & operator = (const RefTarget &) { /* Don't copy refcount */ return *this; }
  43. /// Get current refcount of this object
  44. uint32 GetRefCount() const { return mRefCount; }
  45. /// Add or release a reference to this object
  46. inline void AddRef() const { ++mRefCount; }
  47. inline void Release() const { if (--mRefCount == 0) delete static_cast<const T *>(this); }
  48. /// INTERNAL HELPER FUNCTION USED BY SERIALIZATION
  49. static int sInternalGetRefCountOffset() { return offsetof(T, mRefCount); }
  50. /// INTERNAL HELPER FUNCTION TO OVERRIDE THE REFCOUNT OF AN OBJECT. USE WITH GREAT CARE!
  51. inline void SetRefCountInternal(uint32 inRefCount) { JPH_ASSERT(inRefCount >= 0); mRefCount = inRefCount; }
  52. protected:
  53. static constexpr uint32 cEmbedded = 0x0ebedded; ///< A large value that gets added to the refcount to mark the object as embedded
  54. mutable atomic<uint32> mRefCount = 0; ///< Current reference count
  55. };
  56. /// Pure virtual version of RefTarget
  57. class RefTargetVirtual
  58. {
  59. public:
  60. /// Virtual destructor
  61. virtual ~RefTargetVirtual() = default;
  62. /// Virtual add reference
  63. virtual void AddRef() = 0;
  64. /// Virtual release reference
  65. virtual void Release() = 0;
  66. };
  67. /// Class for automatic referencing, this is the equivalent of a pointer to type T
  68. /// if you assign a value to this class it will increment the reference count by one
  69. /// of this object, and if you assign something else it will decrease the reference
  70. /// count of the first object again. If it reaches a reference count of zero it will
  71. /// be deleted
  72. template <class T>
  73. class Ref
  74. {
  75. public:
  76. /// Constructor
  77. inline Ref() : mPtr(nullptr) { }
  78. inline Ref(T *inRHS) : mPtr(inRHS) { AddRef(); }
  79. inline Ref(const Ref<T> &inRHS) : mPtr(inRHS.mPtr) { AddRef(); }
  80. inline Ref(Ref<T> &&inRHS) noexcept : mPtr(inRHS.mPtr) { inRHS.mPtr = nullptr; }
  81. inline ~Ref() { Release(); }
  82. /// Assignment operators
  83. inline Ref<T> & operator = (T *inRHS) { if (mPtr != inRHS) { Release(); mPtr = inRHS; AddRef(); } return *this; }
  84. inline Ref<T> & operator = (const Ref<T> &inRHS) { if (mPtr != inRHS.mPtr) { Release(); mPtr = inRHS.mPtr; AddRef(); } return *this; }
  85. inline Ref<T> & operator = (Ref<T> &&inRHS) noexcept { if (mPtr != inRHS.mPtr) { Release(); mPtr = inRHS.mPtr; inRHS.mPtr = nullptr; } return *this; }
  86. /// Casting operators
  87. inline operator T * const () const { return mPtr; }
  88. inline operator T *() { return mPtr; }
  89. /// Access like a normal pointer
  90. inline T * const operator -> () const { return mPtr; }
  91. inline T * operator -> () { return mPtr; }
  92. inline T & operator * () const { return *mPtr; }
  93. /// Comparison
  94. inline bool operator == (const T * inRHS) const { return mPtr == inRHS; }
  95. inline bool operator == (const Ref<T> &inRHS) const { return mPtr == inRHS.mPtr; }
  96. inline bool operator != (const T * inRHS) const { return mPtr != inRHS; }
  97. inline bool operator != (const Ref<T> &inRHS) const { return mPtr != inRHS.mPtr; }
  98. /// Get pointer
  99. inline T * GetPtr() const { return mPtr; }
  100. inline T * GetPtr() { return mPtr; }
  101. /// INTERNAL HELPER FUNCTION USED BY SERIALIZATION
  102. void ** InternalGetPointer() { return reinterpret_cast<void **>(&mPtr); }
  103. private:
  104. template <class T2> friend class RefConst;
  105. /// Use "variable = nullptr;" to release an object, do not call these functions
  106. inline void AddRef() { if (mPtr != nullptr) mPtr->AddRef(); }
  107. inline void Release() { if (mPtr != nullptr) mPtr->Release(); }
  108. T * mPtr; ///< Pointer to object that we are reference counting
  109. };
  110. /// Class for automatic referencing, this is the equivalent of a CONST pointer to type T
  111. /// if you assign a value to this class it will increment the reference count by one
  112. /// of this object, and if you assign something else it will decrease the reference
  113. /// count of the first object again. If it reaches a reference count of zero it will
  114. /// be deleted
  115. template <class T>
  116. class RefConst
  117. {
  118. public:
  119. /// Constructor
  120. inline RefConst() : mPtr(nullptr) { }
  121. inline RefConst(const T * inRHS) : mPtr(inRHS) { AddRef(); }
  122. inline RefConst(const RefConst<T> &inRHS) : mPtr(inRHS.mPtr) { AddRef(); }
  123. inline RefConst(RefConst<T> &&inRHS) noexcept : mPtr(inRHS.mPtr) { inRHS.mPtr = nullptr; }
  124. inline RefConst(const Ref<T> &inRHS) : mPtr(inRHS.mPtr) { AddRef(); }
  125. inline RefConst(Ref<T> &&inRHS) noexcept : mPtr(inRHS.mPtr) { inRHS.mPtr = nullptr; }
  126. inline ~RefConst() { Release(); }
  127. /// Assignment operators
  128. inline RefConst<T> & operator = (const T * inRHS) { if (mPtr != inRHS) { Release(); mPtr = inRHS; AddRef(); } return *this; }
  129. inline RefConst<T> & operator = (const RefConst<T> &inRHS) { if (mPtr != inRHS.mPtr) { Release(); mPtr = inRHS.mPtr; AddRef(); } return *this; }
  130. inline RefConst<T> & operator = (RefConst<T> &&inRHS) noexcept { if (mPtr != inRHS.mPtr) { Release(); mPtr = inRHS.mPtr; inRHS.mPtr = nullptr; } return *this; }
  131. inline RefConst<T> & operator = (const Ref<T> &inRHS) { if (mPtr != inRHS.mPtr) { Release(); mPtr = inRHS.mPtr; AddRef(); } return *this; }
  132. inline RefConst<T> & operator = (Ref<T> &&inRHS) noexcept { if (mPtr != inRHS.mPtr) { Release(); mPtr = inRHS.mPtr; inRHS.mPtr = nullptr; } return *this; }
  133. /// Casting operators
  134. inline operator const T * () const { return mPtr; }
  135. /// Access like a normal pointer
  136. inline const T * operator -> () const { return mPtr; }
  137. inline const T & operator * () const { return *mPtr; }
  138. /// Comparison
  139. inline bool operator == (const T * inRHS) const { return mPtr == inRHS; }
  140. inline bool operator == (const RefConst<T> &inRHS) const { return mPtr == inRHS.mPtr; }
  141. inline bool operator == (const Ref<T> &inRHS) const { return mPtr == inRHS.mPtr; }
  142. inline bool operator != (const T * inRHS) const { return mPtr != inRHS; }
  143. inline bool operator != (const RefConst<T> &inRHS) const { return mPtr != inRHS.mPtr; }
  144. inline bool operator != (const Ref<T> &inRHS) const { return mPtr != inRHS.mPtr; }
  145. /// Get pointer
  146. inline const T * GetPtr() const { return mPtr; }
  147. /// INTERNAL HELPER FUNCTION USED BY SERIALIZATION
  148. void ** InternalGetPointer() { return const_cast<void **>(reinterpret_cast<const void **>(&mPtr)); }
  149. private:
  150. /// Use "variable = nullptr;" to release an object, do not call these functions
  151. inline void AddRef() { if (mPtr != nullptr) mPtr->AddRef(); }
  152. inline void Release() { if (mPtr != nullptr) mPtr->Release(); }
  153. const T * mPtr; ///< Pointer to object that we are reference counting
  154. };
  155. JPH_NAMESPACE_END
  156. JPH_SUPPRESS_WARNING_PUSH
  157. JPH_CLANG_SUPPRESS_WARNING("-Wc++98-compat")
  158. namespace std
  159. {
  160. /// Declare std::hash for Ref
  161. template <class T>
  162. struct hash<JPH::Ref<T>>
  163. {
  164. size_t operator () (const JPH::Ref<T> &inRHS) const
  165. {
  166. return hash<T *> { }(inRHS.GetPtr());
  167. }
  168. };
  169. /// Declare std::hash for RefConst
  170. template <class T>
  171. struct hash<JPH::RefConst<T>>
  172. {
  173. size_t operator () (const JPH::RefConst<T> &inRHS) const
  174. {
  175. return hash<const T *> { }(inRHS.GetPtr());
  176. }
  177. };
  178. }
  179. JPH_SUPPRESS_WARNING_POP