diff options
author | Alexander Smirnov <alex@ydb.tech> | 2024-10-16 12:11:24 +0000 |
---|---|---|
committer | Alexander Smirnov <alex@ydb.tech> | 2024-10-16 12:11:24 +0000 |
commit | 40811e93f3fdf9342a9295369994012420fac548 (patch) | |
tree | a8d85e094a9c21e10aa250f537c101fc2016a049 /library/cpp/yt/misc/cast-inl.h | |
parent | 30ebe5357bb143648c6be4d151ecd4944af81ada (diff) | |
parent | 28a0c4a9f297064538a018c512cd9bbd00a1a35d (diff) | |
download | ydb-40811e93f3fdf9342a9295369994012420fac548.tar.gz |
Merge branch 'rightlib' into mergelibs-241016-1210
Diffstat (limited to 'library/cpp/yt/misc/cast-inl.h')
-rw-r--r-- | library/cpp/yt/misc/cast-inl.h | 30 |
1 files changed, 22 insertions, 8 deletions
diff --git a/library/cpp/yt/misc/cast-inl.h b/library/cpp/yt/misc/cast-inl.h index a694394f88..00e6e240b8 100644 --- a/library/cpp/yt/misc/cast-inl.h +++ b/library/cpp/yt/misc/cast-inl.h @@ -18,35 +18,35 @@ namespace NYT { namespace NDetail { template <class T, class S> -bool IsInIntegralRange(S value) +constexpr bool IsInIntegralRange(S value) requires std::is_signed_v<T> && std::is_signed_v<S> { return value >= std::numeric_limits<T>::lowest() && value <= std::numeric_limits<T>::max(); } template <class T, class S> -bool IsInIntegralRange(S value) +constexpr bool IsInIntegralRange(S value) requires std::is_signed_v<T> && std::is_unsigned_v<S> { return value <= static_cast<typename std::make_unsigned<T>::type>(std::numeric_limits<T>::max()); } template <class T, class S> -bool IsInIntegralRange(S value) +constexpr bool IsInIntegralRange(S value) requires std::is_unsigned_v<T> && std::is_signed_v<S> { return value >= 0 && static_cast<typename std::make_unsigned<S>::type>(value) <= std::numeric_limits<T>::max(); } template <class T, class S> -bool IsInIntegralRange(S value) +constexpr bool IsInIntegralRange(S value) requires std::is_unsigned_v<T> && std::is_unsigned_v<S> { return value <= std::numeric_limits<T>::max(); } template <class T, class S> -bool IsInIntegralRange(S value) +constexpr bool IsInIntegralRange(S value) requires std::is_enum_v<S> { return IsInIntegralRange<T>(static_cast<std::underlying_type_t<S>>(value)); @@ -79,10 +79,24 @@ inline TString FormatInvalidCastValue(char8_t value) //////////////////////////////////////////////////////////////////////////////// + +template <class T, class S> +constexpr bool CanFitSubtype() +{ + return NDetail::IsInIntegralRange<T>(std::numeric_limits<S>::min()) && + NDetail::IsInIntegralRange<T>(std::numeric_limits<S>::max()); +} + +template <class T, class S> +constexpr bool IsInIntegralRange(S value) +{ + return NDetail::IsInIntegralRange<T>(value); +} + template <class T, class S> -bool TryIntegralCast(S value, T* result) +constexpr bool TryIntegralCast(S value, T* result) { - if (!NYT::NDetail::IsInIntegralRange<T>(value)) { + if (!NDetail::IsInIntegralRange<T>(value)) { return false; } *result = static_cast<T>(value); @@ -105,7 +119,7 @@ T CheckedIntegralCast(S value) } template <class T, class S> -bool TryEnumCast(S value, T* result) +constexpr bool TryEnumCast(S value, T* result) { std::underlying_type_t<T> underlying; if (!TryIntegralCast<std::underlying_type_t<T>>(value, &underlying)) { |