2
0

SharedPtr.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. template<typename T>
  44. class SharedPtr
  45. {
  46. public:
  47. SharedPtr()
  48. throw() :
  49. _ptr((T *)0)
  50. {
  51. }
  52. SharedPtr(T *obj)
  53. throw() :
  54. _ptr(obj)
  55. {
  56. ++obj->__refCount;
  57. }
  58. SharedPtr(const SharedPtr &sp)
  59. throw() :
  60. _ptr(sp._getAndInc())
  61. {
  62. }
  63. ~SharedPtr()
  64. {
  65. if (_ptr) {
  66. if (--_ptr->__refCount <= 0)
  67. delete _ptr;
  68. }
  69. }
  70. inline SharedPtr &operator=(const SharedPtr &sp)
  71. {
  72. if (_ptr != sp._ptr) {
  73. T *p = sp._getAndInc();
  74. if (_ptr) {
  75. if (--_ptr->__refCount <= 0)
  76. delete _ptr;
  77. }
  78. _ptr = p;
  79. }
  80. return *this;
  81. }
  82. inline void swap(SharedPtr &with)
  83. throw()
  84. {
  85. T *tmp = _ptr;
  86. _ptr = with._ptr;
  87. with._ptr = tmp;
  88. }
  89. inline operator bool() const throw() { return (_ptr); }
  90. inline T &operator*() const throw() { return *_ptr; }
  91. inline T *operator->() const throw() { return _ptr; }
  92. /**
  93. * @return Raw pointer to held object
  94. */
  95. inline T *ptr() const throw() { 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. }
  105. _ptr = (T *)0;
  106. }
  107. inline bool operator==(const SharedPtr &sp) const throw() { return (_ptr == sp._ptr); }
  108. inline bool operator!=(const SharedPtr &sp) const throw() { return (_ptr != sp._ptr); }
  109. inline bool operator>(const SharedPtr &sp) const throw() { return (_ptr > sp._ptr); }
  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. private:
  114. inline T *_getAndInc() const
  115. throw()
  116. {
  117. if (_ptr)
  118. ++_ptr->__refCount;
  119. return _ptr;
  120. }
  121. T *_ptr;
  122. };
  123. } // namespace ZeroTier
  124. #endif