SharedPtr.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. inline SharedPtr &operator=(const SharedPtr &sp)
  40. {
  41. if (_ptr != sp._ptr) {
  42. T *p = sp._getAndInc();
  43. if (_ptr) {
  44. if (--_ptr->__refCount <= 0)
  45. delete _ptr;
  46. }
  47. _ptr = p;
  48. }
  49. return *this;
  50. }
  51. /**
  52. * Set to a naked pointer and increment its reference count
  53. *
  54. * This assumes this SharedPtr is NULL and that ptr is not a 'zombie.' No
  55. * checks are performed.
  56. *
  57. * @param ptr Naked pointer to assign
  58. */
  59. inline void set(T *ptr)
  60. {
  61. zero();
  62. ++ptr->__refCount;
  63. _ptr = ptr;
  64. }
  65. /**
  66. * Swap with another pointer 'for free' without ref count overhead
  67. *
  68. * @param with Pointer to swap with
  69. */
  70. inline void swap(SharedPtr &with)
  71. {
  72. T *tmp = _ptr;
  73. _ptr = with._ptr;
  74. with._ptr = tmp;
  75. }
  76. inline operator bool() const { return (_ptr != (T *)0); }
  77. inline T &operator*() const { return *_ptr; }
  78. inline T *operator->() const { return _ptr; }
  79. /**
  80. * @return Raw pointer to held object
  81. */
  82. inline T *ptr() const { return _ptr; }
  83. /**
  84. * Set this pointer to NULL
  85. */
  86. inline void zero()
  87. {
  88. if (_ptr) {
  89. if (--_ptr->__refCount <= 0)
  90. delete _ptr;
  91. _ptr = (T *)0;
  92. }
  93. }
  94. /**
  95. * @return Number of references according to this object's ref count or 0 if NULL
  96. */
  97. inline int references()
  98. {
  99. if (_ptr)
  100. return _ptr->__refCount.load();
  101. return 0;
  102. }
  103. inline bool operator==(const SharedPtr &sp) const { return (_ptr == sp._ptr); }
  104. inline bool operator!=(const SharedPtr &sp) const { return (_ptr != sp._ptr); }
  105. inline bool operator>(const SharedPtr &sp) const { return (_ptr > sp._ptr); }
  106. inline bool operator<(const SharedPtr &sp) const { return (_ptr < sp._ptr); }
  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. private:
  110. inline T *_getAndInc() const
  111. {
  112. if (_ptr)
  113. ++_ptr->__refCount;
  114. return _ptr;
  115. }
  116. T *_ptr;
  117. };
  118. } // namespace ZeroTier
  119. #endif