SharedPtr.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_SHAREDPTR_HPP
  27. #define ZT_SHAREDPTR_HPP
  28. #include "Mutex.hpp"
  29. #include "AtomicCounter.hpp"
  30. namespace ZeroTier {
  31. /**
  32. * Simple reference counted pointer
  33. *
  34. * This is an introspective shared pointer. Classes that need to be reference
  35. * counted must list this as a 'friend' and must have a private instance of
  36. * AtomicCounter called __refCount. They should also have private destructors,
  37. * since only this class should delete them.
  38. *
  39. * Because this is introspective, it is safe to apply to a naked pointer
  40. * multiple times provided there is always at least one holding SharedPtr.
  41. *
  42. * Once C++11 is ubiquitous, this and a few other things like Thread might get
  43. * torn out for their standard equivalents.
  44. */
  45. template<typename T>
  46. class SharedPtr
  47. {
  48. public:
  49. SharedPtr()
  50. throw() :
  51. _ptr((T *)0)
  52. {
  53. }
  54. SharedPtr(T *obj)
  55. throw() :
  56. _ptr(obj)
  57. {
  58. ++obj->__refCount;
  59. }
  60. SharedPtr(const SharedPtr &sp)
  61. throw() :
  62. _ptr(sp._getAndInc())
  63. {
  64. }
  65. ~SharedPtr()
  66. {
  67. if (_ptr) {
  68. if (--_ptr->__refCount <= 0)
  69. delete _ptr;
  70. }
  71. }
  72. inline SharedPtr &operator=(const SharedPtr &sp)
  73. {
  74. if (_ptr != sp._ptr) {
  75. T *p = sp._getAndInc();
  76. if (_ptr) {
  77. if (--_ptr->__refCount <= 0)
  78. delete _ptr;
  79. }
  80. _ptr = p;
  81. }
  82. return *this;
  83. }
  84. /**
  85. * Set to a naked pointer and increment its reference count
  86. *
  87. * This assumes this SharedPtr is NULL and that ptr is not a 'zombie.' No
  88. * checks are performed.
  89. *
  90. * @param ptr Naked pointer to assign
  91. */
  92. inline void setToUnsafe(T *ptr)
  93. {
  94. ++ptr->__refCount;
  95. _ptr = ptr;
  96. }
  97. /**
  98. * Swap with another pointer 'for free' without ref count overhead
  99. *
  100. * @param with Pointer to swap with
  101. */
  102. inline void swap(SharedPtr &with)
  103. throw()
  104. {
  105. T *tmp = _ptr;
  106. _ptr = with._ptr;
  107. with._ptr = tmp;
  108. }
  109. inline operator bool() const throw() { return (_ptr != (T *)0); }
  110. inline T &operator*() const throw() { return *_ptr; }
  111. inline T *operator->() const throw() { return _ptr; }
  112. /**
  113. * @return Raw pointer to held object
  114. */
  115. inline T *ptr() const throw() { return _ptr; }
  116. /**
  117. * Set this pointer to NULL
  118. */
  119. inline void zero()
  120. {
  121. if (_ptr) {
  122. if (--_ptr->__refCount <= 0)
  123. delete _ptr;
  124. _ptr = (T *)0;
  125. }
  126. }
  127. /**
  128. * Set this pointer to NULL if this is the only pointer holding the object
  129. *
  130. * @return True if object was deleted and SharedPtr is now NULL (or was already NULL)
  131. */
  132. inline bool reclaimIfWeak()
  133. {
  134. if (_ptr) {
  135. if (++_ptr->__refCount <= 2) {
  136. if (--_ptr->__refCount <= 1) {
  137. delete _ptr;
  138. _ptr = (T *)0;
  139. return true;
  140. } else {
  141. return false;
  142. }
  143. } else {
  144. return false;
  145. }
  146. } else {
  147. return true;
  148. }
  149. }
  150. inline bool operator==(const SharedPtr &sp) const throw() { return (_ptr == sp._ptr); }
  151. inline bool operator!=(const SharedPtr &sp) const throw() { return (_ptr != sp._ptr); }
  152. inline bool operator>(const SharedPtr &sp) const throw() { return (_ptr > sp._ptr); }
  153. inline bool operator<(const SharedPtr &sp) const throw() { return (_ptr < sp._ptr); }
  154. inline bool operator>=(const SharedPtr &sp) const throw() { return (_ptr >= sp._ptr); }
  155. inline bool operator<=(const SharedPtr &sp) const throw() { return (_ptr <= sp._ptr); }
  156. private:
  157. inline T *_getAndInc() const
  158. throw()
  159. {
  160. if (_ptr)
  161. ++_ptr->__refCount;
  162. return _ptr;
  163. }
  164. T *_ptr;
  165. };
  166. } // namespace ZeroTier
  167. #endif