SharedPtr.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_SHAREDPTR_HPP
  28. #define ZT_SHAREDPTR_HPP
  29. #include "Mutex.hpp"
  30. #include "AtomicCounter.hpp"
  31. namespace ZeroTier {
  32. /**
  33. * Simple reference counted pointer
  34. *
  35. * This is an introspective shared pointer. Classes that need to be reference
  36. * counted must list this as a 'friend' and must have a private instance of
  37. * AtomicCounter called __refCount. They should also have private destructors,
  38. * since only this class should delete them.
  39. *
  40. * Because this is introspective, it is safe to apply to a naked pointer
  41. * multiple times provided there is always at least one holding SharedPtr.
  42. *
  43. * Once C++11 is ubiquitous, this and a few other things like Thread might get
  44. * torn out for their standard equivalents.
  45. */
  46. template<typename T>
  47. class SharedPtr
  48. {
  49. public:
  50. SharedPtr()
  51. throw() :
  52. _ptr((T *)0)
  53. {
  54. }
  55. SharedPtr(T *obj)
  56. throw() :
  57. _ptr(obj)
  58. {
  59. ++obj->__refCount;
  60. }
  61. SharedPtr(const SharedPtr &sp)
  62. throw() :
  63. _ptr(sp._getAndInc())
  64. {
  65. }
  66. ~SharedPtr()
  67. {
  68. if (_ptr) {
  69. if (--_ptr->__refCount <= 0)
  70. delete _ptr;
  71. }
  72. }
  73. inline SharedPtr &operator=(const SharedPtr &sp)
  74. {
  75. if (_ptr != sp._ptr) {
  76. T *p = sp._getAndInc();
  77. if (_ptr) {
  78. if (--_ptr->__refCount <= 0)
  79. delete _ptr;
  80. }
  81. _ptr = p;
  82. }
  83. return *this;
  84. }
  85. /**
  86. * Set to a naked pointer and increment its reference count
  87. *
  88. * This assumes this SharedPtr is NULL and that ptr is not a 'zombie.' No
  89. * checks are performed.
  90. *
  91. * @param ptr Naked pointer to assign
  92. */
  93. inline void setToUnsafe(T *ptr)
  94. {
  95. ++ptr->__refCount;
  96. _ptr = ptr;
  97. }
  98. /**
  99. * Swap with another pointer 'for free' without ref count overhead
  100. *
  101. * @param with Pointer to swap with
  102. */
  103. inline void swap(SharedPtr &with)
  104. throw()
  105. {
  106. T *tmp = _ptr;
  107. _ptr = with._ptr;
  108. with._ptr = tmp;
  109. }
  110. inline operator bool() const throw() { return (_ptr != (T *)0); }
  111. inline T &operator*() const throw() { return *_ptr; }
  112. inline T *operator->() const throw() { return _ptr; }
  113. /**
  114. * @return Raw pointer to held object
  115. */
  116. inline T *ptr() const throw() { return _ptr; }
  117. /**
  118. * Set this pointer to null
  119. */
  120. inline void zero()
  121. {
  122. if (_ptr) {
  123. if (--_ptr->__refCount <= 0)
  124. delete _ptr;
  125. }
  126. _ptr = (T *)0;
  127. }
  128. inline bool operator==(const SharedPtr &sp) const throw() { return (_ptr == sp._ptr); }
  129. inline bool operator!=(const SharedPtr &sp) const throw() { return (_ptr != sp._ptr); }
  130. inline bool operator>(const SharedPtr &sp) const throw() { return (_ptr > sp._ptr); }
  131. inline bool operator<(const SharedPtr &sp) const throw() { return (_ptr < sp._ptr); }
  132. inline bool operator>=(const SharedPtr &sp) const throw() { return (_ptr >= sp._ptr); }
  133. inline bool operator<=(const SharedPtr &sp) const throw() { return (_ptr <= sp._ptr); }
  134. private:
  135. inline T *_getAndInc() const
  136. throw()
  137. {
  138. if (_ptr)
  139. ++_ptr->__refCount;
  140. return _ptr;
  141. }
  142. T *_ptr;
  143. };
  144. } // namespace ZeroTier
  145. #endif