SharedPtr.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  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. inline void swap(SharedPtr &with)
  86. throw()
  87. {
  88. T *tmp = _ptr;
  89. _ptr = with._ptr;
  90. with._ptr = tmp;
  91. }
  92. inline operator bool() const throw() { return (_ptr != (T *)0); }
  93. inline T &operator*() const throw() { return *_ptr; }
  94. inline T *operator->() const throw() { return _ptr; }
  95. /**
  96. * @return Raw pointer to held object
  97. */
  98. inline T *ptr() const throw() { return _ptr; }
  99. /**
  100. * Set this pointer to null
  101. */
  102. inline void zero()
  103. {
  104. if (_ptr) {
  105. if (--_ptr->__refCount <= 0)
  106. delete _ptr;
  107. }
  108. _ptr = (T *)0;
  109. }
  110. inline bool operator==(const SharedPtr &sp) const throw() { return (_ptr == sp._ptr); }
  111. inline bool operator!=(const SharedPtr &sp) const throw() { return (_ptr != sp._ptr); }
  112. inline bool operator>(const SharedPtr &sp) const throw() { return (_ptr > sp._ptr); }
  113. inline bool operator<(const SharedPtr &sp) const throw() { return (_ptr < sp._ptr); }
  114. inline bool operator>=(const SharedPtr &sp) const throw() { return (_ptr >= sp._ptr); }
  115. inline bool operator<=(const SharedPtr &sp) const throw() { return (_ptr <= sp._ptr); }
  116. private:
  117. inline T *_getAndInc() const
  118. throw()
  119. {
  120. if (_ptr)
  121. ++_ptr->__refCount;
  122. return _ptr;
  123. }
  124. T *_ptr;
  125. };
  126. } // namespace ZeroTier
  127. #endif