diff options
| author | arkady-e1ppa <[email protected]> | 2023-11-24 02:58:57 +0300 |
|---|---|---|
| committer | arkady-e1ppa <[email protected]> | 2023-11-24 03:21:46 +0300 |
| commit | a7789e237827cb31cf0cf2f0d3f4a388a75d73e3 (patch) | |
| tree | 526956192159b539e0c3119793897b316c917750 /library/cpp | |
| parent | 7b1701b1c40fea4015925f883a628af9e20f8f6d (diff) | |
YT-20547: TJobId is now strongly typed
Diffstat (limited to 'library/cpp')
| -rw-r--r-- | library/cpp/yt/misc/guid-inl.h | 32 | ||||
| -rw-r--r-- | library/cpp/yt/misc/guid.h | 6 | ||||
| -rw-r--r-- | library/cpp/yt/misc/strong_typedef-inl.h | 8 | ||||
| -rw-r--r-- | library/cpp/yt/misc/strong_typedef.h | 12 |
4 files changed, 43 insertions, 15 deletions
diff --git a/library/cpp/yt/misc/guid-inl.h b/library/cpp/yt/misc/guid-inl.h index 2d94b5701b0..1c71d794bd9 100644 --- a/library/cpp/yt/misc/guid-inl.h +++ b/library/cpp/yt/misc/guid-inl.h @@ -32,18 +32,7 @@ Y_FORCE_INLINE TGuid::operator bool() const //////////////////////////////////////////////////////////////////////////////// -Y_FORCE_INLINE bool operator == (TGuid lhs, TGuid rhs) -{ - return lhs.Parts64[0] == rhs.Parts64[0] && - lhs.Parts64[1] == rhs.Parts64[1]; -} - -Y_FORCE_INLINE bool operator != (TGuid lhs, TGuid rhs) -{ - return !(lhs == rhs); -} - -Y_FORCE_INLINE bool operator < (TGuid lhs, TGuid rhs) +Y_FORCE_INLINE bool LessImpl(TGuid lhs, TGuid rhs) noexcept { #ifdef __GNUC__ ui64 lhs0 = __builtin_bswap64(lhs.Parts64[0]); @@ -62,6 +51,25 @@ Y_FORCE_INLINE bool operator < (TGuid lhs, TGuid rhs) #endif } +Y_FORCE_INLINE bool operator == (const TGuid& lhs, const TGuid& rhs) noexcept +{ + return lhs.Parts64[0] == rhs.Parts64[0] && + lhs.Parts64[1] == rhs.Parts64[1]; +} + +Y_FORCE_INLINE std::strong_ordering operator <=> (const TGuid& lhs, const TGuid& rhs) noexcept +{ + if (LessImpl(lhs, rhs)) { + return std::strong_ordering::less; + } + + if (lhs == rhs) { + return std::strong_ordering::equal; + } + + return std::strong_ordering::greater; +} + //////////////////////////////////////////////////////////////////////////////// } // namespace NYT diff --git a/library/cpp/yt/misc/guid.h b/library/cpp/yt/misc/guid.h index ce024885f73..328503f9020 100644 --- a/library/cpp/yt/misc/guid.h +++ b/library/cpp/yt/misc/guid.h @@ -79,9 +79,9 @@ struct TGuid static bool FromStringHex32(TStringBuf str, TGuid* guid); }; -bool operator == (TGuid lhs, TGuid rhs); -bool operator != (TGuid lhs, TGuid rhs); -bool operator < (TGuid lhs, TGuid rhs); +bool operator == (const TGuid& lhs, const TGuid& rhs) noexcept; + +std::strong_ordering operator <=> (const TGuid& lhs, const TGuid& rhs) noexcept; //////////////////////////////////////////////////////////////////////////////// diff --git a/library/cpp/yt/misc/strong_typedef-inl.h b/library/cpp/yt/misc/strong_typedef-inl.h index 43bdabf5839..00f0cc0cc85 100644 --- a/library/cpp/yt/misc/strong_typedef-inl.h +++ b/library/cpp/yt/misc/strong_typedef-inl.h @@ -86,6 +86,13 @@ constexpr auto TStrongTypedef<T, TTag>::operator<=>(const TStrongTypedef& rhs) c return Underlying_ <=> rhs.Underlying_; } +template <class T, class TTag> +TStrongTypedef<T, TTag>::operator bool() const + noexcept(noexcept(static_cast<bool>(Underlying_))) +{ + return static_cast<bool>(Underlying_); +} + //////////////////////////////////////////////////////////////////////////////// template <class T> @@ -116,6 +123,7 @@ class TStringBuilderBase; template <class T, class TTag> void FormatValue(TStringBuilderBase* builder, const TStrongTypedef<T, TTag>& value, TStringBuf format) + noexcept(noexcept(FormatValue(builder, value.Underlying(), format))) { FormatValue(builder, value.Underlying(), format); } diff --git a/library/cpp/yt/misc/strong_typedef.h b/library/cpp/yt/misc/strong_typedef.h index dfb479a8c69..8ed0810b533 100644 --- a/library/cpp/yt/misc/strong_typedef.h +++ b/library/cpp/yt/misc/strong_typedef.h @@ -2,6 +2,8 @@ #include <utility> +#include <util/generic/string.h> + namespace NYT { //////////////////////////////////////////////////////////////////////////////// @@ -36,12 +38,22 @@ public: constexpr auto operator<=>(const TStrongTypedef& rhs) const noexcept(std::same_as<T, void> || noexcept(Underlying_ <=> rhs.Underlying_)); + explicit operator bool() const + noexcept(noexcept(static_cast<bool>(Underlying_))); + constexpr T& Underlying() &; constexpr const T& Underlying() const &; constexpr T&& Underlying() &&; private: T Underlying_; + + //! NB: Hidden friend definition to make this name accessible only via ADL. + friend TString ToString(const TStrongTypedef& value) + requires requires (T value) { { ToString(value) } -> std::same_as<TString>; } + { + return ToString(value.Underlying_); + } }; #define YT_DEFINE_STRONG_TYPEDEF(T, TUnderlying) \ |
