enum.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #pragma once
  2. #include "preprocessor.h"
  3. #include <util/generic/strbuf.h>
  4. #include <array>
  5. #include <optional>
  6. #include <type_traits>
  7. #include <vector>
  8. #include <library/cpp/yt/exception/exception.h>
  9. namespace NYT {
  10. ////////////////////////////////////////////////////////////////////////////////
  11. /*
  12. * Smart enumerations augment C++ enum classes with a bunch of reflection
  13. * capabilities accessible via TEnumTraits class specialization.
  14. *
  15. * Please refer to the unit test for an actual example of usage
  16. * (unittests/enum_ut.cpp).
  17. */
  18. // The actual overload must be provided with DEFINE_ENUM* (see below).
  19. template <class T>
  20. void GetEnumTraitsImpl(T);
  21. template <class T>
  22. using TEnumTraitsImpl = decltype(GetEnumTraitsImpl(T()));
  23. template <class T>
  24. constexpr bool IsEnumDomainSizeKnown()
  25. {
  26. if constexpr(requires{ TEnumTraitsImpl<T>::DomainSize; }) {
  27. return true;
  28. } else {
  29. return false;
  30. }
  31. }
  32. template <
  33. class T,
  34. bool = IsEnumDomainSizeKnown<T>()
  35. >
  36. struct TEnumTraitsWithKnownDomain
  37. { };
  38. template <
  39. class T,
  40. bool = std::is_enum_v<T> && !std::is_same_v<TEnumTraitsImpl<T>, void>
  41. >
  42. struct TEnumTraits
  43. {
  44. static constexpr bool IsEnum = false;
  45. static constexpr bool IsBitEnum = false;
  46. static constexpr bool IsStringSerializableEnum = false;
  47. static constexpr bool IsMonotonic = false;
  48. };
  49. template <class T>
  50. struct TEnumTraitsWithKnownDomain<T, true>
  51. {
  52. static constexpr int GetDomainSize();
  53. static constexpr const std::array<TStringBuf, GetDomainSize()>& GetDomainNames();
  54. static constexpr const std::array<T, GetDomainSize()>& GetDomainValues();
  55. // For non-bit enums only.
  56. static constexpr T GetMinValue()
  57. requires (!TEnumTraitsImpl<T>::IsBitEnum);
  58. static constexpr T GetMaxValue()
  59. requires (!TEnumTraitsImpl<T>::IsBitEnum);
  60. // For bit enums only.
  61. static std::vector<T> Decompose(T value)
  62. requires (TEnumTraitsImpl<T>::IsBitEnum);
  63. };
  64. template <class T>
  65. struct TEnumTraits<T, true>
  66. : public TEnumTraitsWithKnownDomain<T>
  67. {
  68. static constexpr bool IsEnum = true;
  69. static constexpr bool IsBitEnum = TEnumTraitsImpl<T>::IsBitEnum;
  70. static constexpr bool IsStringSerializableEnum = TEnumTraitsImpl<T>::IsStringSerializableEnum;
  71. static constexpr bool IsMonotonic = TEnumTraitsImpl<T>::IsMonotonic;
  72. static TStringBuf GetTypeName();
  73. static std::optional<TStringBuf> FindLiteralByValue(T value);
  74. static std::optional<T> FindValueByLiteral(TStringBuf literal);
  75. static TString ToString(T value);
  76. static T FromString(TStringBuf literal);
  77. };
  78. ////////////////////////////////////////////////////////////////////////////////
  79. //! Defines a smart enumeration with a specific underlying type.
  80. /*!
  81. * \param enumType Enumeration enumType.
  82. * \param seq Enumeration domain encoded as a <em>sequence</em>.
  83. * \param underlyingType Underlying type.
  84. */
  85. #define DEFINE_ENUM_WITH_UNDERLYING_TYPE(enumType, underlyingType, seq) \
  86. ENUM__CLASS(enumType, underlyingType, seq) \
  87. ENUM__BEGIN_TRAITS(enumType, underlyingType, false, false, seq) \
  88. ENUM__VALIDATE_UNIQUE(enumType) \
  89. ENUM__END_TRAITS(enumType) \
  90. static_assert(true)
  91. //! Defines a smart enumeration with a specific underlying type.
  92. //! Duplicate enumeration values are allowed.
  93. #define DEFINE_AMBIGUOUS_ENUM_WITH_UNDERLYING_TYPE(enumType, underlyingType, seq) \
  94. ENUM__CLASS(enumType, underlyingType, seq) \
  95. ENUM__BEGIN_TRAITS(enumType, underlyingType, false, false, seq) \
  96. ENUM__END_TRAITS(enumType) \
  97. static_assert(true)
  98. //! Defines a smart enumeration with the default |int| underlying type.
  99. #define DEFINE_ENUM(enumType, seq) \
  100. DEFINE_ENUM_WITH_UNDERLYING_TYPE(enumType, int, seq)
  101. //! Defines a smart enumeration with a specific underlying type.
  102. /*!
  103. * \param enumType Enumeration enumType.
  104. * \param seq Enumeration domain encoded as a <em>sequence</em>.
  105. * \param underlyingType Underlying type.
  106. */
  107. #define DEFINE_BIT_ENUM_WITH_UNDERLYING_TYPE(enumType, underlyingType, seq) \
  108. ENUM__CLASS(enumType, underlyingType, seq) \
  109. ENUM__BEGIN_TRAITS(enumType, underlyingType, true, false, seq) \
  110. ENUM__VALIDATE_UNIQUE(enumType) \
  111. ENUM__END_TRAITS(enumType) \
  112. ENUM__BITWISE_OPS(enumType) \
  113. static_assert(true)
  114. //! Defines a smart enumeration with a specific underlying type.
  115. //! Duplicate enumeration values are allowed.
  116. /*!
  117. * \param enumType Enumeration enumType.
  118. * \param seq Enumeration domain encoded as a <em>sequence</em>.
  119. * \param underlyingType Underlying type.
  120. */
  121. #define DEFINE_AMBIGUOUS_BIT_ENUM_WITH_UNDERLYING_TYPE(enumType, underlyingType, seq) \
  122. ENUM__CLASS(enumType, underlyingType, seq) \
  123. ENUM__BEGIN_TRAITS(enumType, underlyingType, true, false, seq) \
  124. ENUM__END_TRAITS(enumType) \
  125. ENUM__BITWISE_OPS(enumType) \
  126. static_assert(true)
  127. //! Defines a smart enumeration with the default |unsigned int| underlying type.
  128. /*!
  129. * \param enumType Enumeration enumType.
  130. * \param seq Enumeration domain encoded as a <em>sequence</em>.
  131. */
  132. #define DEFINE_BIT_ENUM(enumType, seq) \
  133. DEFINE_BIT_ENUM_WITH_UNDERLYING_TYPE(enumType, unsigned int, seq)
  134. //! Defines a smart enumeration with a specific underlying type and IsStringSerializable attribute.
  135. /*!
  136. * \param enumType Enumeration enumType.
  137. * \param seq Enumeration domain encoded as a <em>sequence</em>.
  138. * \param underlyingType Underlying type.
  139. */
  140. #define DEFINE_STRING_SERIALIZABLE_ENUM_WITH_UNDERLYING_TYPE(enumType, underlyingType, seq) \
  141. ENUM__CLASS(enumType, underlyingType, seq) \
  142. ENUM__BEGIN_TRAITS(enumType, underlyingType, false, true, seq) \
  143. ENUM__VALIDATE_UNIQUE(enumType) \
  144. ENUM__END_TRAITS(enumType) \
  145. static_assert(true)
  146. //! Defines a smart enumeration with a specific underlying type and IsStringSerializable attribute.
  147. //! Duplicate enumeration values are allowed.
  148. #define DEFINE_AMBIGUOUS_STRING_SERIALIZABLE_ENUM_WITH_UNDERLYING_TYPE(enumType, underlyingType, seq) \
  149. ENUM__CLASS(enumType, underlyingType, seq) \
  150. ENUM__BEGIN_TRAITS(enumType, underlyingType, false, true, seq) \
  151. ENUM__END_TRAITS(enumType) \
  152. static_assert(true)
  153. //! Defines a smart enumeration with the default |int| underlying type and IsStringSerializable attribute.
  154. #define DEFINE_STRING_SERIALIZABLE_ENUM(enumType, seq) \
  155. DEFINE_STRING_SERIALIZABLE_ENUM_WITH_UNDERLYING_TYPE(enumType, int, seq)
  156. ////////////////////////////////////////////////////////////////////////////////
  157. //! Returns |true| iff the enumeration value is not bitwise zero.
  158. template <typename E>
  159. requires TEnumTraits<E>::IsBitEnum
  160. constexpr bool Any(E value) noexcept;
  161. //! Returns |true| iff the enumeration value is bitwise zero.
  162. template <typename E>
  163. requires TEnumTraits<E>::IsBitEnum
  164. constexpr bool None(E value) noexcept;
  165. ////////////////////////////////////////////////////////////////////////////////
  166. } // namespace NYT
  167. #define ENUM_INL_H_
  168. #include "enum-inl.h"
  169. #undef ENUM_INL_H_