15namespace hi::inline v1 {
22template<
typename T, std::
size_t N>
24 static_assert(std::is_enum_v<T>,
"Must be an enum");
25 static_assert(N != 0);
40 [[nodiscard]]
constexpr value_type
minimum() const noexcept
42 return std::get<0>(_by_value).value;
47 [[nodiscard]]
constexpr value_type
maximum() const noexcept
49 return std::get<N-1>(_by_value).value;
65 template<
typename... Args>
68 static_assert(
sizeof...(Args) == N * 2);
69 add_value_name<0>(args...);
72 return a.name < b.name;
76 return to_underlying(a.value) < to_underlying(b.value);
79 values_are_continues = check_values_are_continues();
87 [[nodiscard]]
constexpr bool contains(std::string_view name)
const noexcept
89 return find(name) !=
nullptr;
97 [[nodiscard]]
constexpr bool contains(value_type value)
const noexcept
99 return find(value) !=
nullptr;
108 [[nodiscard]]
constexpr value_type
at(std::string_view name)
const
110 if (
hilet *value = find(name)) {
123 [[nodiscard]]
constexpr std::string_view
const &
at(value_type value)
const
125 if (
hilet *name = find(value)) {
138 [[nodiscard]]
constexpr value_type
at(std::string_view name, value_type default_value)
const noexcept
140 if (
hilet *value = find(name)) {
143 return default_value;
153 [[nodiscard]]
constexpr std::string_view
at(value_type value, std::string_view default_name)
const noexcept
155 if (
hilet *name = find(value)) {
168 [[nodiscard]]
constexpr value_type
operator[](std::string_view name)
const noexcept
170 auto *value = find(name);
171 hi_axiom(value !=
nullptr);
181 [[nodiscard]]
constexpr std::string_view
const &
operator[](value_type value)
const noexcept
183 auto *name = find(value);
184 hi_axiom(name !=
nullptr);
191 std::string_view name;
193 constexpr value_name() noexcept : value(), name() {}
194 constexpr value_name(value_type value, std::string_view name) noexcept : value(value), name(name) {}
200 [[nodiscard]]
constexpr std::string_view
const *
find(value_type value)
const noexcept
202 if (values_are_continues) {
205 hilet offset = to_underlying(it->value);
206 hilet i = to_underlying(value) - offset;
207 return (i >= 0 and i < N) ? &(it + i)->name : nullptr;
211 return item.value < key;
214 return (it != _by_value.
end() and it->value == value) ? &it->name :
nullptr;
218 [[nodiscard]]
constexpr value_type
const *
find(std::string_view name)
const noexcept
221 return item.name < key;
224 return (it != _by_name.
end() and it->name == name) ? &it->value :
nullptr;
232 constexpr void add_value_name(value_type value, std::string_view name, Rest
const &...rest)
noexcept
234 static_assert(
sizeof...(Rest) % 2 == 0);
236 std::get<I>(_by_name) = {value, name};
237 std::get<I>(_by_value) = {value, name};
239 if constexpr (
sizeof...(Rest) > 0) {
240 add_value_name<I + 1>(rest...);
248 [[nodiscard]]
constexpr bool check_values_are_continues() const noexcept
250 auto check_value = to_underlying(minimum());
251 for (
hilet &item : _by_value) {
252 if (to_underlying(item.value) != check_value++) {
260template<
typename T,
typename... Rest>
261enum_metadata(T
const &, Rest
const &...) -> enum_metadata<T, (
sizeof...(Rest) + 1) / 2>;
This file includes required definitions.
#define hilet
Invariant should be the default for variables.
Definition required.hpp:23
A object that holds enum-values and strings.
Definition enum_metadata.hpp:23
constexpr std::string_view const & operator[](value_type value) const noexcept
Get a name from an enum-value.
Definition enum_metadata.hpp:181
constexpr std::string_view const & at(value_type value) const
Get a name from an enum-value.
Definition enum_metadata.hpp:123
constexpr value_type at(std::string_view name, value_type default_value) const noexcept
Get an enum-value from a name.
Definition enum_metadata.hpp:138
bool values_are_continues
The numeric values in the enum do not contain a gap.
Definition enum_metadata.hpp:36
constexpr value_type at(std::string_view name) const
Get an enum-value from a name.
Definition enum_metadata.hpp:108
constexpr enum_metadata(Args const &...args) noexcept
Construct a enum-names table object.
Definition enum_metadata.hpp:66
constexpr value_type operator[](std::string_view name) const noexcept
Get an enum-value from a name.
Definition enum_metadata.hpp:168
constexpr value_type minimum() const noexcept
Get the minimum value.
Definition enum_metadata.hpp:40
constexpr bool contains(value_type value) const noexcept
Check if the enum has a value.
Definition enum_metadata.hpp:97
constexpr bool contains(std::string_view name) const noexcept
Check if the enum has a name.
Definition enum_metadata.hpp:87
constexpr std::string_view at(value_type value, std::string_view default_name) const noexcept
Get a name from an enum-value.
Definition enum_metadata.hpp:153
constexpr value_type maximum() const noexcept
Get the maximum value.
Definition enum_metadata.hpp:47