SharedPtr.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 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 zero-overhead introspective 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.
  37. */
  38. template<typename T>
  39. class SharedPtr
  40. {
  41. public:
  42. SharedPtr() : _ptr((T *)0) {}
  43. SharedPtr(T *obj) : _ptr(obj) { ++obj->__refCount; }
  44. SharedPtr(const SharedPtr &sp) : _ptr(sp._getAndInc()) {}
  45. ~SharedPtr()
  46. {
  47. if (_ptr) {
  48. if (--_ptr->__refCount <= 0)
  49. delete _ptr;
  50. }
  51. }
  52. inline SharedPtr &operator=(const SharedPtr &sp)
  53. {
  54. if (_ptr != sp._ptr) {
  55. T *p = sp._getAndInc();
  56. if (_ptr) {
  57. if (--_ptr->__refCount <= 0)
  58. delete _ptr;
  59. }
  60. _ptr = p;
  61. }
  62. return *this;
  63. }
  64. /**
  65. * Set to a naked pointer and increment its reference count
  66. *
  67. * This assumes this SharedPtr is NULL and that ptr is not a 'zombie.' No
  68. * checks are performed.
  69. *
  70. * @param ptr Naked pointer to assign
  71. */
  72. inline void set(T *ptr)
  73. {
  74. zero();
  75. ++ptr->__refCount;
  76. _ptr = ptr;
  77. }
  78. /**
  79. * Swap with another pointer 'for free' without ref count overhead
  80. *
  81. * @param with Pointer to swap with
  82. */
  83. inline void swap(SharedPtr &with)
  84. {
  85. T *tmp = _ptr;
  86. _ptr = with._ptr;
  87. with._ptr = tmp;
  88. }
  89. inline operator bool() const { return (_ptr != (T *)0); }
  90. inline T &operator*() const { return *_ptr; }
  91. inline T *operator->() const { return _ptr; }
  92. /**
  93. * @return Raw pointer to held object
  94. */
  95. inline T *ptr() const { return _ptr; }
  96. /**
  97. * Set this pointer to NULL
  98. */
  99. inline void zero()
  100. {
  101. if (_ptr) {
  102. if (--_ptr->__refCount <= 0)
  103. delete _ptr;
  104. _ptr = (T *)0;
  105. }
  106. }
  107. /**
  108. * @return Number of references according to this object's ref count or 0 if NULL
  109. */
  110. inline int references()
  111. {
  112. if (_ptr)
  113. return _ptr->__refCount.load();
  114. return 0;
  115. }
  116. inline bool operator==(const SharedPtr &sp) const { return (_ptr == sp._ptr); }
  117. inline bool operator!=(const SharedPtr &sp) const { return (_ptr != sp._ptr); }
  118. inline bool operator>(const SharedPtr &sp) const { return (_ptr > sp._ptr); }
  119. inline bool operator<(const SharedPtr &sp) const { return (_ptr < sp._ptr); }
  120. inline bool operator>=(const SharedPtr &sp) const { return (_ptr >= sp._ptr); }
  121. inline bool operator<=(const SharedPtr &sp) const { return (_ptr <= sp._ptr); }
  122. private:
  123. inline T *_getAndInc() const
  124. {
  125. if (_ptr)
  126. ++_ptr->__refCount;
  127. return _ptr;
  128. }
  129. T *_ptr;
  130. };
  131. } // namespace ZeroTier
  132. #endif