diff options
author | babenko <babenko@yandex-team.com> | 2023-03-06 11:32:26 +0300 |
---|---|---|
committer | babenko <babenko@yandex-team.com> | 2023-03-06 11:32:26 +0300 |
commit | b92c1faacc932ce36e23dce74b5b3d31a1cbb02c (patch) | |
tree | 3a9460573148cc337d7bdf11e078b1d69ac70cc7 /library/cpp | |
parent | e0657f78a0630fb2a4be66130bad2799e1109913 (diff) | |
download | ydb-b92c1faacc932ce36e23dce74b5b3d31a1cbb02c.tar.gz |
Use strong typedefs for TRefCountedTypeCookie and TRefCountedTypeKey
Diffstat (limited to 'library/cpp')
-rw-r--r-- | library/cpp/yt/memory/ref_tracked-inl.h | 36 | ||||
-rw-r--r-- | library/cpp/yt/memory/ref_tracked.h | 34 | ||||
-rw-r--r-- | library/cpp/yt/misc/strong_typedef-inl.h | 96 | ||||
-rw-r--r-- | library/cpp/yt/misc/strong_typedef.h | 61 |
4 files changed, 200 insertions, 27 deletions
diff --git a/library/cpp/yt/memory/ref_tracked-inl.h b/library/cpp/yt/memory/ref_tracked-inl.h index efd95edd2d..0c16c02ff8 100644 --- a/library/cpp/yt/memory/ref_tracked-inl.h +++ b/library/cpp/yt/memory/ref_tracked-inl.h @@ -11,7 +11,7 @@ namespace NYT { template <class T> TRefCountedTypeKey GetRefCountedTypeKey() { - return &typeid(T); + return TRefCountedTypeKey(&typeid(T)); } template <class T> @@ -46,4 +46,38 @@ Y_FORCE_INLINE TRefCountedTypeCookie GetRefCountedTypeCookieWithLocation(const T //////////////////////////////////////////////////////////////////////////////// +#ifdef YT_ENABLE_REF_COUNTED_TRACKING + +template <class T> +TRefTracked<T>::TRefTracked() +{ + auto cookie = GetRefCountedTypeCookie<T>(); + TRefCountedTrackerFacade::AllocateInstance(cookie); +} + +template <class T> +TRefTracked<T>::TRefTracked(const TRefTracked&) +{ + auto cookie = GetRefCountedTypeCookie<T>(); + TRefCountedTrackerFacade::AllocateInstance(cookie); +} + +template <class T> +TRefTracked<T>::TRefTracked(TRefTracked&&) +{ + auto cookie = GetRefCountedTypeCookie<T>(); + TRefCountedTrackerFacade::AllocateInstance(cookie); +} + +template <class T> +TRefTracked<T>::~TRefTracked() +{ + auto cookie = GetRefCountedTypeCookie<T>(); + TRefCountedTrackerFacade::FreeInstance(cookie); +} + +#endif + +//////////////////////////////////////////////////////////////////////////////// + } // namespace NYT diff --git a/library/cpp/yt/memory/ref_tracked.h b/library/cpp/yt/memory/ref_tracked.h index 75c1eb5985..df6a06fcf0 100644 --- a/library/cpp/yt/memory/ref_tracked.h +++ b/library/cpp/yt/memory/ref_tracked.h @@ -2,6 +2,7 @@ #include <library/cpp/yt/misc/port.h> #include <library/cpp/yt/misc/source_location.h> +#include <library/cpp/yt/misc/strong_typedef.h> #include <util/system/defaults.h> @@ -12,10 +13,10 @@ namespace NYT { //////////////////////////////////////////////////////////////////////////////// -using TRefCountedTypeCookie = int; -const int NullRefCountedTypeCookie = -1; +YT_DEFINE_STRONG_TYPEDEF(TRefCountedTypeCookie, int) +constexpr TRefCountedTypeCookie NullRefCountedTypeCookie{-1}; -using TRefCountedTypeKey = const void*; +YT_DEFINE_STRONG_TYPEDEF(TRefCountedTypeKey, const std::type_info*) //////////////////////////////////////////////////////////////////////////////// @@ -76,29 +77,10 @@ class TRefTracked { public: #ifdef YT_ENABLE_REF_COUNTED_TRACKING - TRefTracked() - { - auto cookie = GetRefCountedTypeCookie<T>(); - TRefCountedTrackerFacade::AllocateInstance(cookie); - } - - TRefTracked(const TRefTracked&) - { - auto cookie = GetRefCountedTypeCookie<T>(); - TRefCountedTrackerFacade::AllocateInstance(cookie); - } - - TRefTracked(TRefTracked&&) - { - auto cookie = GetRefCountedTypeCookie<T>(); - TRefCountedTrackerFacade::AllocateInstance(cookie); - } - - ~TRefTracked() - { - auto cookie = GetRefCountedTypeCookie<T>(); - TRefCountedTrackerFacade::FreeInstance(cookie); - } + TRefTracked(); + TRefTracked(const TRefTracked&); + TRefTracked(TRefTracked&&); + ~TRefTracked(); #endif }; diff --git a/library/cpp/yt/misc/strong_typedef-inl.h b/library/cpp/yt/misc/strong_typedef-inl.h new file mode 100644 index 0000000000..1729e3db7e --- /dev/null +++ b/library/cpp/yt/misc/strong_typedef-inl.h @@ -0,0 +1,96 @@ +#ifndef STRONG_TYPEDEF_INL_H_ +#error "Direct inclusion of this file is not allowed, include strong_typedef.h" +// For the sake of sane code completion. +#include "strong_typedef.h" +#endif + +#include <functional> + +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + +template <class T, class TTag> +constexpr TStrongTypedef<T, TTag>::TStrongTypedef() + requires std::is_default_constructible_v<T> + : Underlying_{} +{ } + +template <class T, class TTag> +constexpr TStrongTypedef<T, TTag>::TStrongTypedef(const T& underlying) + requires std::is_copy_constructible_v<T> + : Underlying_(underlying) +{ } + +template <class T, class TTag> +constexpr TStrongTypedef<T, TTag>::TStrongTypedef(T&& underlying) + requires std::is_move_constructible_v<T> + : Underlying_(std::move(underlying)) +{ } + +template <class T, class TTag> +constexpr TStrongTypedef<T, TTag>& TStrongTypedef<T, TTag>::operator=(const T& rhs) + requires std::is_copy_assignable_v<T> +{ + Underlying_ = rhs; + return *this; +} + +template <class T, class TTag> +constexpr TStrongTypedef<T, TTag>& TStrongTypedef<T, TTag>::operator=(T&& rhs) + requires std::is_move_assignable_v<T> +{ + Underlying_ = std::move(rhs); + return *this; +} + +template <class T, class TTag> +constexpr TStrongTypedef<T, TTag>::operator const T&() const +{ + return Underlying_; +} + +template <class T, class TTag> +constexpr TStrongTypedef<T, TTag>::operator T&() +{ + return Underlying_; +} + +template <class T, class TTag> +constexpr T& TStrongTypedef<T, TTag>::Underlying() & +{ + return Underlying_; +} + +template <class T, class TTag> +constexpr const T& TStrongTypedef<T, TTag>::Underlying() const& +{ + return Underlying_; +} + +template <class T, class TTag> +constexpr T&& TStrongTypedef<T, TTag>::Underlying() && +{ + return std::move(Underlying_); +} + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT + +namespace std { + +//////////////////////////////////////////////////////////////////////////////// + +template <class T, class TTag> +struct hash<NYT::TStrongTypedef<T, TTag>> +{ + size_t operator()(const NYT::TStrongTypedef<T, TTag>& value) const + { + return std::hash<T>()(value.Underlying()); + } +}; + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace std diff --git a/library/cpp/yt/misc/strong_typedef.h b/library/cpp/yt/misc/strong_typedef.h new file mode 100644 index 0000000000..a74ee3cf23 --- /dev/null +++ b/library/cpp/yt/misc/strong_typedef.h @@ -0,0 +1,61 @@ +#pragma once + +#include <type_traits> +#include <utility> + +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + +template <class T, class TTag> +class TStrongTypedef +{ +public: + using TUnderlying = T; + + constexpr TStrongTypedef() + requires std::is_default_constructible_v<T>; + + constexpr explicit TStrongTypedef(const T& underlying) + requires std::is_copy_constructible_v<T>; + + constexpr explicit TStrongTypedef(T&& underlying) + requires std::is_move_constructible_v<T>; + + TStrongTypedef(const TStrongTypedef&) = default; + TStrongTypedef(TStrongTypedef&&) = default; + + TStrongTypedef& operator=(const TStrongTypedef&) = default; + TStrongTypedef& operator=(TStrongTypedef&&) = default; + + constexpr TStrongTypedef& operator=(const T& rhs) + requires std::is_copy_assignable_v<T>; + + constexpr TStrongTypedef& operator=(T&& rhs) + requires std::is_move_assignable_v<T>; + + constexpr explicit operator const T&() const; + constexpr explicit operator T&(); + + constexpr auto operator<=>(const TStrongTypedef& rhs) const = default; + + constexpr T& Underlying() &; + constexpr const T& Underlying() const &; + constexpr T&& Underlying() &&; + +private: + T Underlying_; +}; + +#define YT_DEFINE_STRONG_TYPEDEF(T, TUnderlying) \ + struct T ## Tag \ + { }; \ + using T = ::NYT::TStrongTypedef<TUnderlying, T##Tag>; \ + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT + +#define STRONG_TYPEDEF_INL_H_ +#include "strong_typedef-inl.h" +#undef STRONG_TYPEDEF_INL_H_ |