SharedPtr.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_SHAREDPTR_HPP
  14. #define ZT_SHAREDPTR_HPP
  15. #include "Mutex.hpp"
  16. #include "AtomicCounter.hpp"
  17. namespace ZeroTier {
  18. /**
  19. * Simple zero-overhead introspective reference counted pointer
  20. *
  21. * This is an introspective shared pointer. Classes that need to be reference
  22. * counted must list this as a 'friend' and must have a private instance of
  23. * AtomicCounter called __refCount.
  24. */
  25. template<typename T>
  26. class SharedPtr
  27. {
  28. public:
  29. SharedPtr() : _ptr((T *)0) {}
  30. SharedPtr(T *obj) : _ptr(obj) { ++obj->__refCount; }
  31. SharedPtr(const SharedPtr &sp) : _ptr(sp._getAndInc()) {}
  32. ~SharedPtr()
  33. {
  34. if (_ptr) {
  35. if (--_ptr->__refCount <= 0) {
  36. delete _ptr;
  37. }
  38. }
  39. }
  40. inline SharedPtr &operator=(const SharedPtr &sp)
  41. {
  42. if (_ptr != sp._ptr) {
  43. T *p = sp._getAndInc();
  44. if (_ptr) {
  45. if (--_ptr->__refCount <= 0) {
  46. delete _ptr;
  47. }
  48. }
  49. _ptr = p;
  50. }
  51. return *this;
  52. }
  53. /**
  54. * Set to a naked pointer and increment its reference count
  55. *
  56. * This assumes this SharedPtr is NULL and that ptr is not a 'zombie.' No
  57. * checks are performed.
  58. *
  59. * @param ptr Naked pointer to assign
  60. */
  61. inline void set(T *ptr)
  62. {
  63. zero();
  64. ++ptr->__refCount;
  65. _ptr = ptr;
  66. }
  67. /**
  68. * Swap with another pointer 'for free' without ref count overhead
  69. *
  70. * @param with Pointer to swap with
  71. */
  72. inline void swap(SharedPtr &with)
  73. {
  74. T *tmp = _ptr;
  75. _ptr = with._ptr;
  76. with._ptr = tmp;
  77. }
  78. inline operator bool() const { return (_ptr != (T *)0); }
  79. inline T &operator*() const { return *_ptr; }
  80. inline T *operator->() const { return _ptr; }
  81. /**
  82. * @return Raw pointer to held object
  83. */
  84. inline T *ptr() const { return _ptr; }
  85. /**
  86. * Set this pointer to NULL
  87. */
  88. inline void zero()
  89. {
  90. if (_ptr) {
  91. if (--_ptr->__refCount <= 0) {
  92. delete _ptr;
  93. }
  94. _ptr = (T *)0;
  95. }
  96. }
  97. /**
  98. * @return Number of references according to this object's ref count or 0 if NULL
  99. */
  100. inline int references()
  101. {
  102. if (_ptr) {
  103. return _ptr->__refCount.load();
  104. }
  105. return 0;
  106. }
  107. inline bool operator==(const SharedPtr &sp) const { return (_ptr == sp._ptr); }
  108. inline bool operator!=(const SharedPtr &sp) const { return (_ptr != sp._ptr); }
  109. inline bool operator>(const SharedPtr &sp) const { return (_ptr > sp._ptr); }
  110. inline bool operator<(const SharedPtr &sp) const { return (_ptr < sp._ptr); }
  111. inline bool operator>=(const SharedPtr &sp) const { return (_ptr >= sp._ptr); }
  112. inline bool operator<=(const SharedPtr &sp) const { return (_ptr <= sp._ptr); }
  113. private:
  114. inline T *_getAndInc() const
  115. {
  116. if (_ptr) {
  117. ++_ptr->__refCount;
  118. }
  119. return _ptr;
  120. }
  121. T *_ptr;
  122. };
  123. } // namespace ZeroTier
  124. #endif