aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/misc/enum.h
diff options
context:
space:
mode:
authorAlexander Smirnov <alex@ydb.tech>2024-10-28 20:34:11 +0000
committerAlexander Smirnov <alex@ydb.tech>2024-10-28 20:34:11 +0000
commitef9875b11a33dbd25e92bc6b4cf692c18c9ba0ce (patch)
tree1f2fd4e4d9e585da35937b42fbda5f854af04728 /library/cpp/yt/misc/enum.h
parent37ae9cc90160b53eb0e22021c47b3996a01cd656 (diff)
parente3c8507a3d1cb090278f211232ddfde3bedc54d4 (diff)
downloadydb-ef9875b11a33dbd25e92bc6b4cf692c18c9ba0ce.tar.gz
Merge branch 'rightlib' into mergelibs-241028-2033
Diffstat (limited to 'library/cpp/yt/misc/enum.h')
-rw-r--r--library/cpp/yt/misc/enum.h46
1 files changed, 29 insertions, 17 deletions
diff --git a/library/cpp/yt/misc/enum.h b/library/cpp/yt/misc/enum.h
index 4d40ab8ec4..d9611b2823 100644
--- a/library/cpp/yt/misc/enum.h
+++ b/library/cpp/yt/misc/enum.h
@@ -33,18 +33,11 @@ template <class T>
using TEnumTraitsImpl = decltype(GetEnumTraitsImpl(T()));
template <class T>
-constexpr bool IsEnumDomainSizeKnown()
-{
- if constexpr(requires{ TEnumTraitsImpl<T>::DomainSize; }) {
- return true;
- } else {
- return false;
- }
-}
+constexpr std::optional<T> TryGetEnumUnknownValueImpl(T);
template <
class T,
- bool = IsEnumDomainSizeKnown<T>()
+ bool DomainSizeKnown = requires{ TEnumTraitsImpl<T>::DomainSize; }
>
struct TEnumTraitsWithKnownDomain
{ };
@@ -62,7 +55,7 @@ struct TEnumTraits
};
template <class T>
-struct TEnumTraitsWithKnownDomain<T, true>
+struct TEnumTraitsWithKnownDomain<T, /*DomainSizeKnown*/ true>
{
static constexpr int GetDomainSize();
@@ -76,6 +69,8 @@ struct TEnumTraitsWithKnownDomain<T, true>
requires (!TEnumTraitsImpl<T>::IsBitEnum);
// For bit enums only.
+ static constexpr T GetAllSetValue()
+ requires (TEnumTraitsImpl<T>::IsBitEnum);
static std::vector<T> Decompose(T value)
requires (TEnumTraitsImpl<T>::IsBitEnum);
};
@@ -91,8 +86,11 @@ struct TEnumTraits<T, true>
static TStringBuf GetTypeName();
+ static constexpr std::optional<T> TryGetUnknownValue();
static std::optional<TStringBuf> FindLiteralByValue(T value);
static std::optional<T> FindValueByLiteral(TStringBuf literal);
+ static constexpr bool IsKnownValue(T value)
+ requires (!TEnumTraitsImpl<T>::IsBitEnum);
static TString ToString(T value);
static T FromString(TStringBuf literal);
@@ -102,7 +100,7 @@ struct TEnumTraits<T, true>
//! Defines a smart enumeration with a specific underlying type.
/*!
- * \param enumType Enumeration enumType.
+ * \param enumType Enumeration type.
* \param seq Enumeration domain encoded as a <em>sequence</em>.
* \param underlyingType Underlying type.
*/
@@ -127,35 +125,37 @@ struct TEnumTraits<T, true>
//! Defines a smart enumeration with a specific underlying type.
/*!
- * \param enumType Enumeration enumType.
+ * \param enumType Enumeration type.
* \param seq Enumeration domain encoded as a <em>sequence</em>.
* \param underlyingType Underlying type.
*/
#define DEFINE_BIT_ENUM_WITH_UNDERLYING_TYPE(enumType, underlyingType, seq) \
ENUM__CLASS(enumType, underlyingType, seq) \
+ ENUM__BITWISE_OPS(enumType) \
ENUM__BEGIN_TRAITS(enumType, underlyingType, true, false, seq) \
ENUM__VALIDATE_UNIQUE(enumType) \
+ ENUM__ALL_SET_VALUE(enumType, seq) \
ENUM__END_TRAITS(enumType) \
- ENUM__BITWISE_OPS(enumType) \
static_assert(true)
//! Defines a smart enumeration with a specific underlying type.
//! Duplicate enumeration values are allowed.
/*!
- * \param enumType Enumeration enumType.
+ * \param enumType Enumeration type.
* \param seq Enumeration domain encoded as a <em>sequence</em>.
* \param underlyingType Underlying type.
*/
#define DEFINE_AMBIGUOUS_BIT_ENUM_WITH_UNDERLYING_TYPE(enumType, underlyingType, seq) \
ENUM__CLASS(enumType, underlyingType, seq) \
+ ENUM__BITWISE_OPS(enumType) \
ENUM__BEGIN_TRAITS(enumType, underlyingType, true, false, seq) \
+ ENUM__ALL_SET_VALUE(enumType, seq) \
ENUM__END_TRAITS(enumType) \
- ENUM__BITWISE_OPS(enumType) \
static_assert(true)
//! Defines a smart enumeration with the default |unsigned int| underlying type.
/*!
- * \param enumType Enumeration enumType.
+ * \param enumType Enumeration type.
* \param seq Enumeration domain encoded as a <em>sequence</em>.
*/
#define DEFINE_BIT_ENUM(enumType, seq) \
@@ -163,7 +163,7 @@ struct TEnumTraits<T, true>
//! Defines a smart enumeration with a specific underlying type and IsStringSerializable attribute.
/*!
- * \param enumType Enumeration enumType.
+ * \param enumType Enumeration type.
* \param seq Enumeration domain encoded as a <em>sequence</em>.
* \param underlyingType Underlying type.
*/
@@ -186,6 +186,18 @@ struct TEnumTraits<T, true>
#define DEFINE_STRING_SERIALIZABLE_ENUM(enumType, seq) \
DEFINE_STRING_SERIALIZABLE_ENUM_WITH_UNDERLYING_TYPE(enumType, int, seq)
+//! When enum from another representation (e.g. string or protobuf integer),
+//! instructs the parser to treat undeclared values as |unknownValue|.
+/*!
+ * \param enumType Enumeration type.
+ * \param unknownValue A sentinel value of #enumType.
+ */
+#define DEFINE_ENUM_UNKNOWN_VALUE(enumType, unknownValue) \
+ [[maybe_unused]] constexpr std::optional<enumType> TryGetEnumUnknownValueImpl(enumType) \
+ { \
+ return enumType::unknownValue; \
+ }
+
////////////////////////////////////////////////////////////////////////////////
//! Returns |true| iff the enumeration value is not bitwise zero.