SharedPtr.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_SHAREDPTR_HPP
  14. #define ZT_SHAREDPTR_HPP
  15. #include "AtomicCounter.hpp"
  16. #include "Mutex.hpp"
  17. namespace ZeroTier {
  18. /**
  19. * Simple zero-overhead introspective reference counted pointer
  20. *
  21. * This is an introspective shared pointer. Classes that need to be reference
  22. * counted must list this as a 'friend' and must have a private instance of
  23. * AtomicCounter called __refCount.
  24. */
  25. template <typename T> class SharedPtr {
  26. public:
  27. SharedPtr() : _ptr((T*)0)
  28. {
  29. }
  30. SharedPtr(T* obj) : _ptr(obj)
  31. {
  32. ++obj->__refCount;
  33. }
  34. SharedPtr(const SharedPtr& sp) : _ptr(sp._getAndInc())
  35. {
  36. }
  37. ~SharedPtr()
  38. {
  39. if (_ptr) {
  40. if (--_ptr->__refCount <= 0) {
  41. delete _ptr;
  42. }
  43. }
  44. }
  45. inline SharedPtr& operator=(const SharedPtr& sp)
  46. {
  47. if (_ptr != sp._ptr) {
  48. T* p = sp._getAndInc();
  49. if (_ptr) {
  50. if (--_ptr->__refCount <= 0) {
  51. delete _ptr;
  52. }
  53. }
  54. _ptr = p;
  55. }
  56. return *this;
  57. }
  58. /**
  59. * Set to a naked pointer and increment its reference count
  60. *
  61. * This assumes this SharedPtr is NULL and that ptr is not a 'zombie.' No
  62. * checks are performed.
  63. *
  64. * @param ptr Naked pointer to assign
  65. */
  66. inline void set(T* ptr)
  67. {
  68. zero();
  69. ++ptr->__refCount;
  70. _ptr = ptr;
  71. }
  72. /**
  73. * Swap with another pointer 'for free' without ref count overhead
  74. *
  75. * @param with Pointer to swap with
  76. */
  77. inline void swap(SharedPtr& with)
  78. {
  79. T* tmp = _ptr;
  80. _ptr = with._ptr;
  81. with._ptr = tmp;
  82. }
  83. inline operator bool() const
  84. {
  85. return (_ptr != (T*)0);
  86. }
  87. inline T& operator*() const
  88. {
  89. return *_ptr;
  90. }
  91. inline T* operator->() const
  92. {
  93. return _ptr;
  94. }
  95. /**
  96. * @return Raw pointer to held object
  97. */
  98. inline T* ptr() const
  99. {
  100. return _ptr;
  101. }
  102. /**
  103. * Set this pointer to NULL
  104. */
  105. inline void zero()
  106. {
  107. if (_ptr) {
  108. if (--_ptr->__refCount <= 0) {
  109. delete _ptr;
  110. }
  111. _ptr = (T*)0;
  112. }
  113. }
  114. /**
  115. * @return Number of references according to this object's ref count or 0 if NULL
  116. */
  117. inline int references()
  118. {
  119. if (_ptr) {
  120. return _ptr->__refCount.load();
  121. }
  122. return 0;
  123. }
  124. inline bool operator==(const SharedPtr& sp) const
  125. {
  126. return (_ptr == sp._ptr);
  127. }
  128. inline bool operator!=(const SharedPtr& sp) const
  129. {
  130. return (_ptr != sp._ptr);
  131. }
  132. inline bool operator>(const SharedPtr& sp) const
  133. {
  134. return (_ptr > sp._ptr);
  135. }
  136. inline bool operator<(const SharedPtr& sp) const
  137. {
  138. return (_ptr < sp._ptr);
  139. }
  140. inline bool operator>=(const SharedPtr& sp) const
  141. {
  142. return (_ptr >= sp._ptr);
  143. }
  144. inline bool operator<=(const SharedPtr& sp) const
  145. {
  146. return (_ptr <= sp._ptr);
  147. }
  148. private:
  149. inline T* _getAndInc() const
  150. {
  151. if (_ptr) {
  152. ++_ptr->__refCount;
  153. }
  154. return _ptr;
  155. }
  156. T* _ptr;
  157. };
  158. } // namespace ZeroTier
  159. #endif