SharedPtr.hpp 3.6 KB

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