123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- #pragma once
- #include "ref_counted.h"
- #include <util/generic/hash.h>
- #include <util/generic/utility.h>
- #include <utility>
- #include <type_traits>
- namespace NYT {
- template <class T>
- class TIntrusivePtr
- {
- public:
- using TUnderlying = T;
-
- using element_type = T;
- constexpr TIntrusivePtr() noexcept
- { }
- constexpr TIntrusivePtr(std::nullptr_t) noexcept
- { }
-
-
- TIntrusivePtr(T* obj, bool addReference = true) noexcept
- : T_(obj)
- {
- if (T_ && addReference) {
- Ref(T_);
- }
- }
-
- TIntrusivePtr(const TIntrusivePtr& other) noexcept
- : T_(other.Get())
- {
- if (T_) {
- Ref(T_);
- }
- }
-
- template <class U, class = typename std::enable_if_t<std::is_convertible_v<U*, T*>>>
- TIntrusivePtr(const TIntrusivePtr<U>& other) noexcept
- : T_(other.Get())
- {
- static_assert(
- std::derived_from<T, TRefCountedBase>,
- "Cast allowed only for types derived from TRefCountedBase");
- if (T_) {
- Ref(T_);
- }
- }
-
- TIntrusivePtr(TIntrusivePtr&& other) noexcept
- : T_(other.Get())
- {
- other.T_ = nullptr;
- }
-
- template <class U, class = typename std::enable_if_t<std::is_convertible_v<U*, T*>>>
- TIntrusivePtr(TIntrusivePtr<U>&& other) noexcept
- : T_(other.Get())
- {
- static_assert(
- std::derived_from<T, TRefCountedBase>,
- "Cast allowed only for types derived from TRefCountedBase");
- other.T_ = nullptr;
- }
-
- ~TIntrusivePtr()
- {
- if (T_) {
- Unref(T_);
- }
- }
-
- TIntrusivePtr& operator=(const TIntrusivePtr& other) noexcept
- {
- TIntrusivePtr(other).Swap(*this);
- return *this;
- }
-
- template <class U>
- TIntrusivePtr& operator=(const TIntrusivePtr<U>& other) noexcept
- {
- static_assert(
- std::is_convertible_v<U*, T*>,
- "U* must be convertible to T*");
- static_assert(
- std::derived_from<T, TRefCountedBase>,
- "Cast allowed only for types derived from TRefCountedBase");
- TIntrusivePtr(other).Swap(*this);
- return *this;
- }
-
- TIntrusivePtr& operator=(TIntrusivePtr&& other) noexcept
- {
- TIntrusivePtr(std::move(other)).Swap(*this);
- return *this;
- }
-
- template <class U>
- TIntrusivePtr& operator=(TIntrusivePtr<U>&& other) noexcept
- {
- static_assert(
- std::is_convertible_v<U*, T*>,
- "U* must be convertible to T*");
- static_assert(
- std::derived_from<T, TRefCountedBase>,
- "Cast allowed only for types derived from TRefCountedBase");
- TIntrusivePtr(std::move(other)).Swap(*this);
- return *this;
- }
-
- void Reset()
- {
- TIntrusivePtr().Swap(*this);
- }
-
- void Reset(T* p)
- {
- TIntrusivePtr(p).Swap(*this);
- }
-
- T* Get() const noexcept
- {
- return T_;
- }
-
- T* get() const noexcept
- {
- return T_;
- }
-
- T* Release() noexcept
- {
- auto* p = T_;
- T_ = nullptr;
- return p;
- }
- T& operator*() const noexcept
- {
- YT_ASSERT(T_);
- return *T_;
- }
- T* operator->() const noexcept
- {
- YT_ASSERT(T_);
- return T_;
- }
- explicit operator bool() const noexcept
- {
- return T_ != nullptr;
- }
-
- void Swap(TIntrusivePtr& r) noexcept
- {
- DoSwap(T_, r.T_);
- }
- private:
- template <class U>
- friend class TIntrusivePtr;
- T* T_ = nullptr;
- };
- template <class T>
- TIntrusivePtr<T> MakeStrong(T* p)
- {
- return TIntrusivePtr<T>(p);
- }
- template <class T>
- Y_FORCE_INLINE TIntrusivePtr<T> DangerousGetPtr(T* object)
- {
- return object->TryRef()
- ? TIntrusivePtr<T>(object, false)
- : TIntrusivePtr<T>();
- }
- template <class T, class U>
- TIntrusivePtr<T> StaticPointerCast(const TIntrusivePtr<U>& ptr)
- {
- return {static_cast<T*>(ptr.Get())};
- }
- template <class T, class U>
- TIntrusivePtr<T> StaticPointerCast(TIntrusivePtr<U>&& ptr)
- {
- return {static_cast<T*>(ptr.Release()), false};
- }
- template <class T, class U>
- TIntrusivePtr<T> ConstPointerCast(const TIntrusivePtr<U>& ptr)
- {
- return {const_cast<T*>(ptr.Get())};
- }
- template <class T, class U>
- TIntrusivePtr<T> ConstPointerCast(TIntrusivePtr<U>&& ptr)
- {
- return {const_cast<T*>(ptr.Release()), false};
- }
- template <class T, class U>
- TIntrusivePtr<T> DynamicPointerCast(const TIntrusivePtr<U>& ptr)
- {
- return {dynamic_cast<T*>(ptr.Get())};
- }
- template <class T>
- bool operator<(const TIntrusivePtr<T>& lhs, const TIntrusivePtr<T>& rhs)
- {
- return lhs.Get() < rhs.Get();
- }
- template <class T>
- bool operator>(const TIntrusivePtr<T>& lhs, const TIntrusivePtr<T>& rhs)
- {
- return lhs.Get() > rhs.Get();
- }
- template <class T, class U>
- bool operator==(const TIntrusivePtr<T>& lhs, const TIntrusivePtr<U>& rhs)
- {
- static_assert(
- std::is_convertible_v<U*, T*>,
- "U* must be convertible to T*");
- return lhs.Get() == rhs.Get();
- }
- template <class T, class U>
- bool operator!=(const TIntrusivePtr<T>& lhs, const TIntrusivePtr<U>& rhs)
- {
- static_assert(
- std::is_convertible_v<U*, T*>,
- "U* must be convertible to T*");
- return lhs.Get() != rhs.Get();
- }
- template <class T, class U>
- bool operator==(const TIntrusivePtr<T>& lhs, U* rhs)
- {
- return lhs.Get() == rhs;
- }
- template <class T, class U>
- bool operator!=(const TIntrusivePtr<T>& lhs, U* rhs)
- {
- static_assert(
- std::is_convertible_v<U*, T*>,
- "U* must be convertible to T*");
- return lhs.Get() != rhs;
- }
- template <class T, class U>
- bool operator==(T* lhs, const TIntrusivePtr<U>& rhs)
- {
- return lhs == rhs.Get();
- }
- template <class T, class U>
- bool operator!=(T* lhs, const TIntrusivePtr<U>& rhs)
- {
- static_assert(
- std::is_convertible_v<U*, T*>,
- "U* must be convertible to T*");
- return lhs != rhs.Get();
- }
- template <class T>
- bool operator==(std::nullptr_t, const TIntrusivePtr<T>& rhs)
- {
- return nullptr == rhs.Get();
- }
- template <class T>
- bool operator!=(std::nullptr_t, const TIntrusivePtr<T>& rhs)
- {
- return nullptr != rhs.Get();
- }
- template <class T>
- bool operator==(const TIntrusivePtr<T>& lhs, std::nullptr_t)
- {
- return nullptr == lhs.Get();
- }
- template <class T>
- bool operator!=(const TIntrusivePtr<T>& lhs, std::nullptr_t)
- {
- return nullptr != lhs.Get();
- }
- }
- template <class T>
- struct THash<NYT::TIntrusivePtr<T>>
- {
- Y_FORCE_INLINE size_t operator () (const NYT::TIntrusivePtr<T>& ptr) const
- {
- return THash<T*>()(ptr.Get());
- }
- };
|