intrusive_ptr.h 8.9 KB

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