SharedPtr.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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(T *obj,bool runAwayFromZombies)
  62. throw() :
  63. _ptr(obj)
  64. {
  65. // HACK: this is used in "handlers" to take ownership of naked pointers,
  66. // an ugly pattern that really ought to be factored out.
  67. if (runAwayFromZombies) {
  68. if ((int)(++obj->__refCount) < 2) {
  69. --obj->__refCount;
  70. _ptr = (T *)0;
  71. }
  72. } else ++obj->__refCount;
  73. }
  74. SharedPtr(const SharedPtr &sp)
  75. throw() :
  76. _ptr(sp._getAndInc())
  77. {
  78. }
  79. ~SharedPtr()
  80. {
  81. if (_ptr) {
  82. if (--_ptr->__refCount <= 0)
  83. delete _ptr;
  84. }
  85. }
  86. inline SharedPtr &operator=(const SharedPtr &sp)
  87. {
  88. if (_ptr != sp._ptr) {
  89. T *p = sp._getAndInc();
  90. if (_ptr) {
  91. if (--_ptr->__refCount <= 0)
  92. delete _ptr;
  93. }
  94. _ptr = p;
  95. }
  96. return *this;
  97. }
  98. inline void swap(SharedPtr &with)
  99. throw()
  100. {
  101. T *tmp = _ptr;
  102. _ptr = with._ptr;
  103. with._ptr = tmp;
  104. }
  105. inline operator bool() const throw() { return (_ptr != (T *)0); }
  106. inline T &operator*() const throw() { return *_ptr; }
  107. inline T *operator->() const throw() { return _ptr; }
  108. /**
  109. * @return Raw pointer to held object
  110. */
  111. inline T *ptr() const throw() { return _ptr; }
  112. /**
  113. * Set this pointer to null
  114. */
  115. inline void zero()
  116. {
  117. if (_ptr) {
  118. if (--_ptr->__refCount <= 0)
  119. delete _ptr;
  120. }
  121. _ptr = (T *)0;
  122. }
  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. inline bool operator>(const SharedPtr &sp) const throw() { return (_ptr > sp._ptr); }
  126. inline bool operator<(const SharedPtr &sp) const throw() { return (_ptr < sp._ptr); }
  127. inline bool operator>=(const SharedPtr &sp) const throw() { return (_ptr >= sp._ptr); }
  128. inline bool operator<=(const SharedPtr &sp) const throw() { return (_ptr <= sp._ptr); }
  129. private:
  130. inline T *_getAndInc() const
  131. throw()
  132. {
  133. if (_ptr)
  134. ++_ptr->__refCount;
  135. return _ptr;
  136. }
  137. T *_ptr;
  138. };
  139. } // namespace ZeroTier
  140. #endif