intrusive_ptr.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #pragma once
  2. #include "ref_counted.h"
  3. #include <util/generic/hash.h>
  4. #include <util/generic/utility.h>
  5. #include <utility>
  6. #include <type_traits>
  7. namespace NYT {
  8. ////////////////////////////////////////////////////////////////////////////////
  9. template <class T>
  10. class TIntrusivePtr
  11. {
  12. public:
  13. using TUnderlying = T;
  14. constexpr TIntrusivePtr() noexcept
  15. { }
  16. constexpr TIntrusivePtr(std::nullptr_t) noexcept
  17. { }
  18. //! Constructor from an unqualified reference.
  19. /*!
  20. * Note that this constructor could be racy due to unsynchronized operations
  21. * on the object and on the counter.
  22. *
  23. * Note that it notoriously hard to make this constructor explicit
  24. * given the current amount of code written.
  25. */
  26. TIntrusivePtr(T* obj, bool addReference = true) noexcept
  27. : T_(obj)
  28. {
  29. if (T_ && addReference) {
  30. Ref(T_);
  31. }
  32. }
  33. //! Copy constructor.
  34. TIntrusivePtr(const TIntrusivePtr& other) noexcept
  35. : T_(other.Get())
  36. {
  37. if (T_) {
  38. Ref(T_);
  39. }
  40. }
  41. //! Copy constructor with an upcast.
  42. template <class U, class = typename std::enable_if_t<std::is_convertible_v<U*, T*>>>
  43. TIntrusivePtr(const TIntrusivePtr<U>& other) noexcept
  44. : T_(other.Get())
  45. {
  46. static_assert(
  47. std::derived_from<T, TRefCountedBase>,
  48. "Cast allowed only for types derived from TRefCountedBase");
  49. if (T_) {
  50. Ref(T_);
  51. }
  52. }
  53. //! Move constructor.
  54. TIntrusivePtr(TIntrusivePtr&& other) noexcept
  55. : T_(other.Get())
  56. {
  57. other.T_ = nullptr;
  58. }
  59. //! Move constructor with an upcast.
  60. template <class U, class = typename std::enable_if_t<std::is_convertible_v<U*, T*>>>
  61. TIntrusivePtr(TIntrusivePtr<U>&& other) noexcept
  62. : T_(other.Get())
  63. {
  64. static_assert(
  65. std::derived_from<T, TRefCountedBase>,
  66. "Cast allowed only for types derived from TRefCountedBase");
  67. other.T_ = nullptr;
  68. }
  69. //! Destructor.
  70. ~TIntrusivePtr()
  71. {
  72. if (T_) {
  73. Unref(T_);
  74. }
  75. }
  76. //! Copy assignment operator.
  77. TIntrusivePtr& operator=(const TIntrusivePtr& other) noexcept
  78. {
  79. TIntrusivePtr(other).Swap(*this);
  80. return *this;
  81. }
  82. //! Copy assignment operator with an upcast.
  83. template <class U>
  84. TIntrusivePtr& operator=(const TIntrusivePtr<U>& other) noexcept
  85. {
  86. static_assert(
  87. std::is_convertible_v<U*, T*>,
  88. "U* must be convertible to T*");
  89. static_assert(
  90. std::derived_from<T, TRefCountedBase>,
  91. "Cast allowed only for types derived from TRefCountedBase");
  92. TIntrusivePtr(other).Swap(*this);
  93. return *this;
  94. }
  95. //! Move assignment operator.
  96. TIntrusivePtr& operator=(TIntrusivePtr&& other) noexcept
  97. {
  98. TIntrusivePtr(std::move(other)).Swap(*this);
  99. return *this;
  100. }
  101. //! Move assignment operator with an upcast.
  102. template <class U>
  103. TIntrusivePtr& operator=(TIntrusivePtr<U>&& other) noexcept
  104. {
  105. static_assert(
  106. std::is_convertible_v<U*, T*>,
  107. "U* must be convertible to T*");
  108. static_assert(
  109. std::derived_from<T, TRefCountedBase>,
  110. "Cast allowed only for types derived from TRefCountedBase");
  111. TIntrusivePtr(std::move(other)).Swap(*this);
  112. return *this;
  113. }
  114. //! Drop the pointer.
  115. void Reset() // noexcept
  116. {
  117. TIntrusivePtr().Swap(*this);
  118. }
  119. //! Replace the pointer with a specified one.
  120. void Reset(T* p) // noexcept
  121. {
  122. TIntrusivePtr(p).Swap(*this);
  123. }
  124. //! Returns the pointer.
  125. T* Get() const noexcept
  126. {
  127. return T_;
  128. }
  129. //! Returns the pointer and releases the ownership.
  130. T* Release() noexcept
  131. {
  132. auto* p = T_;
  133. T_ = nullptr;
  134. return p;
  135. }
  136. T& operator*() const noexcept
  137. {
  138. YT_ASSERT(T_);
  139. return *T_;
  140. }
  141. T* operator->() const noexcept
  142. {
  143. YT_ASSERT(T_);
  144. return T_;
  145. }
  146. explicit operator bool() const noexcept
  147. {
  148. return T_ != nullptr;
  149. }
  150. //! Swap the pointer with the other one.
  151. void Swap(TIntrusivePtr& r) noexcept
  152. {
  153. DoSwap(T_, r.T_);
  154. }
  155. private:
  156. template <class U>
  157. friend class TIntrusivePtr;
  158. T* T_ = nullptr;
  159. };
  160. ////////////////////////////////////////////////////////////////////////////////
  161. //! Creates a strong pointer wrapper for a given raw pointer.
  162. //! Compared to |TIntrusivePtr<T>::ctor|, type inference enables omitting |T|.
  163. template <class T>
  164. TIntrusivePtr<T> MakeStrong(T* p)
  165. {
  166. return TIntrusivePtr<T>(p);
  167. }
  168. //! Tries to obtain an intrusive pointer for an object that may had
  169. //! already lost all of its references and, thus, is about to be deleted.
  170. /*!
  171. * You may call this method at any time provided that you have a valid
  172. * raw pointer to an object. The call either returns an intrusive pointer
  173. * for the object (thus ensuring that the object won't be destroyed until
  174. * you're holding this pointer) or NULL indicating that the last reference
  175. * had already been lost and the object is on its way to heavens.
  176. * All these steps happen atomically.
  177. *
  178. * Under all circumstances it is caller's responsibility the make sure that
  179. * the object is not destroyed during the call to #DangerousGetPtr.
  180. * Typically this is achieved by keeping a (lock-protected) collection of
  181. * raw pointers, taking a lock in object's destructor, and unregistering
  182. * its raw pointer from the collection there.
  183. */
  184. template <class T>
  185. Y_FORCE_INLINE TIntrusivePtr<T> DangerousGetPtr(T* object)
  186. {
  187. return object->TryRef()
  188. ? TIntrusivePtr<T>(object, false)
  189. : TIntrusivePtr<T>();
  190. }
  191. ////////////////////////////////////////////////////////////////////////////////
  192. template <class T, class U>
  193. TIntrusivePtr<T> StaticPointerCast(const TIntrusivePtr<U>& ptr)
  194. {
  195. return {static_cast<T*>(ptr.Get())};
  196. }
  197. template <class T, class U>
  198. TIntrusivePtr<T> StaticPointerCast(TIntrusivePtr<U>&& ptr)
  199. {
  200. return {static_cast<T*>(ptr.Release()), false};
  201. }
  202. template <class T, class U>
  203. TIntrusivePtr<T> ConstPointerCast(const TIntrusivePtr<U>& ptr)
  204. {
  205. return {const_cast<T*>(ptr.Get())};
  206. }
  207. template <class T, class U>
  208. TIntrusivePtr<T> ConstPointerCast(TIntrusivePtr<U>&& ptr)
  209. {
  210. return {const_cast<T*>(ptr.Release()), false};
  211. }
  212. template <class T, class U>
  213. TIntrusivePtr<T> DynamicPointerCast(const TIntrusivePtr<U>& ptr)
  214. {
  215. return {dynamic_cast<T*>(ptr.Get())};
  216. }
  217. ////////////////////////////////////////////////////////////////////////////////
  218. template <class T>
  219. bool operator<(const TIntrusivePtr<T>& lhs, const TIntrusivePtr<T>& rhs)
  220. {
  221. return lhs.Get() < rhs.Get();
  222. }
  223. template <class T>
  224. bool operator>(const TIntrusivePtr<T>& lhs, const TIntrusivePtr<T>& rhs)
  225. {
  226. return lhs.Get() > rhs.Get();
  227. }
  228. template <class T, class U>
  229. bool operator==(const TIntrusivePtr<T>& lhs, const TIntrusivePtr<U>& rhs)
  230. {
  231. static_assert(
  232. std::is_convertible_v<U*, T*>,
  233. "U* must be convertible to T*");
  234. return lhs.Get() == rhs.Get();
  235. }
  236. template <class T, class U>
  237. bool operator!=(const TIntrusivePtr<T>& lhs, const TIntrusivePtr<U>& rhs)
  238. {
  239. static_assert(
  240. std::is_convertible_v<U*, T*>,
  241. "U* must be convertible to T*");
  242. return lhs.Get() != rhs.Get();
  243. }
  244. template <class T, class U>
  245. bool operator==(const TIntrusivePtr<T>& lhs, U* rhs)
  246. {
  247. return lhs.Get() == rhs;
  248. }
  249. template <class T, class U>
  250. bool operator!=(const TIntrusivePtr<T>& lhs, U* rhs)
  251. {
  252. static_assert(
  253. std::is_convertible_v<U*, T*>,
  254. "U* must be convertible to T*");
  255. return lhs.Get() != rhs;
  256. }
  257. template <class T, class U>
  258. bool operator==(T* lhs, const TIntrusivePtr<U>& rhs)
  259. {
  260. return lhs == rhs.Get();
  261. }
  262. template <class T, class U>
  263. bool operator!=(T* lhs, const TIntrusivePtr<U>& rhs)
  264. {
  265. static_assert(
  266. std::is_convertible_v<U*, T*>,
  267. "U* must be convertible to T*");
  268. return lhs != rhs.Get();
  269. }
  270. template <class T>
  271. bool operator==(std::nullptr_t, const TIntrusivePtr<T>& rhs)
  272. {
  273. return nullptr == rhs.Get();
  274. }
  275. template <class T>
  276. bool operator!=(std::nullptr_t, const TIntrusivePtr<T>& rhs)
  277. {
  278. return nullptr != rhs.Get();
  279. }
  280. template <class T>
  281. bool operator==(const TIntrusivePtr<T>& lhs, std::nullptr_t)
  282. {
  283. return nullptr == lhs.Get();
  284. }
  285. template <class T>
  286. bool operator!=(const TIntrusivePtr<T>& lhs, std::nullptr_t)
  287. {
  288. return nullptr != lhs.Get();
  289. }
  290. ////////////////////////////////////////////////////////////////////////////////
  291. } //namespace NYT
  292. //! A hasher for TIntrusivePtr.
  293. template <class T>
  294. struct THash<NYT::TIntrusivePtr<T>>
  295. {
  296. Y_FORCE_INLINE size_t operator () (const NYT::TIntrusivePtr<T>& ptr) const
  297. {
  298. return THash<T*>()(ptr.Get());
  299. }
  300. };