40 #include <boost/preprocessor.hpp>
46 #include <string_view>
48 #define HEAVYAI_DEFINE_ENUM_CLASS(enum_class, ...) \
49 enum class enum_class { __VA_ARGS__, N_ }; \
51 constexpr char const* to_string(enum_class const e) { \
52 constexpr char const* strings[]{HEAVYAI_QUOTE_EACH(__VA_ARGS__)}; \
53 constexpr size_t nstrings = sizeof(strings) / sizeof(*strings); \
54 static_assert(nstrings == size_t(enum_class::N_)); \
55 return strings[size_t(e)]; \
58 inline std::ostream& operator<<(std::ostream& os, enum_class const e) { \
59 return os << to_string(e); \
62 #define HEAVYAI_DEFINE_ENUM_CLASS_WITH_DESCRIPTIONS(enum_class, ...) \
63 HEAVYAI_DEFINE_ENUM_CLASS(enum_class, HEAVYAI_PLUCK(0, __VA_ARGS__)) \
65 constexpr char const* to_description(enum_class const e) { \
66 constexpr char const* strings[]{HEAVYAI_PLUCK(1, __VA_ARGS__)}; \
67 constexpr size_t nstrings = sizeof(strings) / sizeof(*strings); \
68 static_assert(nstrings == size_t(enum_class::N_)); \
69 return strings[size_t(e)]; \
73 #define HEAVYAI_QUOTE(r, data, i, elem) BOOST_PP_COMMA_IF(i) BOOST_PP_STRINGIZE(elem)
74 #define HEAVYAI_QUOTE_EACH(...) \
75 BOOST_PP_SEQ_FOR_EACH_I(HEAVYAI_QUOTE, , BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))
76 #define HEAVYAI_PLUCK_ONE(r, j, i, pair) \
77 BOOST_PP_COMMA_IF(i) BOOST_PP_TUPLE_ELEM(2, j, pair)
78 #define HEAVYAI_PLUCK(j, ...) \
79 BOOST_PP_SEQ_FOR_EACH_I(HEAVYAI_PLUCK_ONE, j, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__))
84 template <
typename Enum>
92 template <
typename Enum,
size_t... I>
94 return std::array<StringEnum<Enum>,
sizeof...(I)>{
98 template <
typename T,
size_t N>
100 for (
size_t i = 1; i <
N; ++i) {
103 for (; j && key.name < arr[j - 1].name; --j) {
110 template <
typename Enum>
111 constexpr std::array<StringEnum<Enum>, size_t(Enum::N_)>
sort_by_name() {
112 auto arr = enum_to_array<Enum>(std::make_index_sequence<size_t(Enum::N_)>());
118 template <
typename Enum>
120 constexpr std::array<StringEnum<Enum>, size_t(Enum::N_)> arr = sort_by_name<Enum>();
122 bool const found = itr != arr.end() && itr->name ==
name;
123 return found ? std::make_optional(itr->value) : std::nullopt;
127 template <
auto... Values>
129 template <
typename T>
133 return ((static_cast<T>(Values) == value) || ...);
bool operator<(std::string_view const name) const
constexpr std::array< StringEnum< Enum >, size_t(Enum::N_)> sort_by_name()
std::string to_string(const UUID &uuid)
constexpr auto enum_to_array(std::index_sequence< I...>)
std::optional< Enum > to_enum(std::string_view const name)
constexpr void insertion_sort(std::array< T, N > &arr)
DEVICE auto lower_bound(ARGS &&...args)
static bool check(T const value)