diff options
author | arkady-e1ppa <arkady-e1ppa@yandex-team.com> | 2023-11-24 02:58:57 +0300 |
---|---|---|
committer | arkady-e1ppa <arkady-e1ppa@yandex-team.com> | 2023-11-24 03:21:46 +0300 |
commit | a7789e237827cb31cf0cf2f0d3f4a388a75d73e3 (patch) | |
tree | 526956192159b539e0c3119793897b316c917750 | |
parent | 7b1701b1c40fea4015925f883a628af9e20f8f6d (diff) | |
download | ydb-a7789e237827cb31cf0cf2f0d3f4a388a75d73e3.tar.gz |
YT-20547: TJobId is now strongly typed
-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 | ||||
-rw-r--r-- | yt/yt/client/api/operation_client.h | 2 | ||||
-rw-r--r-- | yt/yt/client/job_tracker_client/public.h | 4 | ||||
-rw-r--r-- | yt/yt/client/object_client/public.h | 1 |
7 files changed, 47 insertions, 18 deletions
diff --git a/library/cpp/yt/misc/guid-inl.h b/library/cpp/yt/misc/guid-inl.h index 2d94b5701b..1c71d794bd 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 ce024885f7..328503f902 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 43bdabf583..00f0cc0cc8 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 dfb479a8c6..8ed0810b53 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) \ diff --git a/yt/yt/client/api/operation_client.h b/yt/yt/client/api/operation_client.h index 9a0c0d1b9e..4630ef1e28 100644 --- a/yt/yt/client/api/operation_client.h +++ b/yt/yt/client/api/operation_client.h @@ -301,7 +301,7 @@ struct TListOperationsResult struct TJob { NJobTrackerClient::TJobId Id; - NJobTrackerClient::TJobId OperationId; + NJobTrackerClient::TOperationId OperationId; std::optional<NJobTrackerClient::EJobType> Type; std::optional<NJobTrackerClient::EJobState> ControllerState; std::optional<NJobTrackerClient::EJobState> ArchiveState; diff --git a/yt/yt/client/job_tracker_client/public.h b/yt/yt/client/job_tracker_client/public.h index bfa4869039..984fa0fcec 100644 --- a/yt/yt/client/job_tracker_client/public.h +++ b/yt/yt/client/job_tracker_client/public.h @@ -2,12 +2,14 @@ #include <library/cpp/yt/misc/enum.h> #include <library/cpp/yt/misc/guid.h> +#include <library/cpp/yt/misc/strong_typedef.h> namespace NYT::NJobTrackerClient { //////////////////////////////////////////////////////////////////////////////// -using TJobId = TGuid; +YT_DEFINE_STRONG_TYPEDEF(TJobId, TGuid); + extern const TJobId NullJobId; using TOperationId = TGuid; diff --git a/yt/yt/client/object_client/public.h b/yt/yt/client/object_client/public.h index b4d15d0b0f..d8f98323f6 100644 --- a/yt/yt/client/object_client/public.h +++ b/yt/yt/client/object_client/public.h @@ -346,7 +346,6 @@ using TTransactionId = TObjectId; constexpr TTransactionId NullTransactionId = {}; using TOperationId = TObjectId; -using TJobId = TObjectId; //////////////////////////////////////////////////////////////////////////////// |