19hi_warning_ignore_msvc(26445);
21namespace hi::inline
v1 {
29template<
typename ValueType,
typename NameType, std::
size_t N>
32 using value_type = ValueType;
33 using name_type = NameType;
43 static_assert(std::is_enum_v<value_type>,
"value_type Must be an enum");
44 static_assert(N != 0);
48 [[nodiscard]]
constexpr size_t size() const noexcept
55 [[nodiscard]]
constexpr value_type
minimum() const noexcept
57 return std::get<0>(_by_value).value;
62 [[nodiscard]]
constexpr value_type
maximum() const noexcept
64 return std::get<N - 1>(_by_value).value;
80 template<
typename... Args>
83 static_assert(
sizeof...(Args) == N * 2);
84 add_value_name<0>(args...);
87 return a.name < b.name;
91 return to_underlying(a.value) < to_underlying(b.value);
94 values_are_continues = check_values_are_continues();
102 [[nodiscard]]
constexpr bool contains(std::convertible_to<name_type>
auto&& name)
const noexcept
104 return find(name_type{
hi_forward(name)}) !=
nullptr;
112 [[nodiscard]]
constexpr bool contains(value_type value)
const noexcept
114 return find(value) !=
nullptr;
123 [[nodiscard]]
constexpr value_type
at(std::convertible_to<name_type>
auto&& name)
const
138 [[nodiscard]]
constexpr name_type
const&
at(value_type value)
const
140 if (
hilet *name = find(value)) {
153 [[nodiscard]]
constexpr value_type
at(std::convertible_to<name_type>
auto&& name, value_type default_value)
const noexcept
158 return default_value;
168 [[nodiscard]]
constexpr name_type
at(value_type value, std::convertible_to<name_type>
auto&& default_name)
const noexcept
170 if (
hilet *name = find(value)) {
183 [[nodiscard]]
constexpr value_type
operator[](std::convertible_to<name_type>
auto&& name)
const noexcept
185 auto *value = find(name_type{
hi_forward(name)});
196 [[nodiscard]]
constexpr name_type
const&
operator[](value_type value)
const noexcept
198 auto *name = find(value);
208 constexpr value_name() noexcept : value(), name() {}
209 constexpr value_name(value_type value, name_type name) noexcept : value(value), name(
std::move(name)) {}
215 [[nodiscard]]
constexpr name_type
const *
find(value_type value)
const noexcept
217 if (values_are_continues) {
220 hilet offset = to_underlying(it->value);
221 hilet i = to_underlying(value) - offset;
222 return (i >= 0 and i < N) ? &(it + i)->name : nullptr;
226 return item.value < key;
229 return (it != _by_value.
end() and it->value == value) ? &it->name :
nullptr;
233 [[nodiscard]]
constexpr value_type
const *
find(name_type
const& name)
const noexcept
236 return item.name < key;
239 return (it != _by_name.
end() and it->name == name) ? &it->value :
nullptr;
247 constexpr void add_value_name(value_type value, name_type name, Rest
const&...rest)
noexcept
249 static_assert(
sizeof...(Rest) % 2 == 0);
251 std::get<I>(_by_name) = {value, name};
252 std::get<I>(_by_value) = {value,
std::move(name)};
254 if constexpr (
sizeof...(Rest) > 0) {
255 add_value_name<I + 1>(rest...);
263 [[nodiscard]]
constexpr bool check_values_are_continues() const noexcept
265 auto check_value = to_underlying(minimum());
266 for (
hilet& item : _by_value) {
267 if (to_underlying(item.value) != check_value++) {
277 using type = std::decay_t<T>;
288using enum_metadata_name_t = enum_metadata_name<T>::type;
290template<
typename ValueType,
typename NameType,
typename... Rest>
291enum_metadata(ValueType
const&, NameType
const&, Rest
const&...)
#define hi_assert_not_null(x,...)
Assert if an expression is not nullptr.
Definition assert.hpp:223
Utilities used by the HikoGUI library itself.
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
DOXYGEN BUG.
Definition algorithm.hpp:13
A object that holds enum-values and strings.
Definition enum_metadata.hpp:30
constexpr bool contains(value_type value) const noexcept
Check if the enum has a value.
Definition enum_metadata.hpp:112
constexpr size_t size() const noexcept
Get the number of enum values.
Definition enum_metadata.hpp:48
constexpr value_type maximum() const noexcept
Get the maximum value.
Definition enum_metadata.hpp:62
bool values_are_continues
The numeric values in the enum do not contain a gap.
Definition enum_metadata.hpp:41
constexpr name_type const & operator[](value_type value) const noexcept
Get a name from an enum-value.
Definition enum_metadata.hpp:196
constexpr bool contains(std::convertible_to< name_type > auto &&name) const noexcept
Check if the enum has a name.
Definition enum_metadata.hpp:102
constexpr name_type at(value_type value, std::convertible_to< name_type > auto &&default_name) const noexcept
Get a name from an enum-value.
Definition enum_metadata.hpp:168
constexpr value_type minimum() const noexcept
Get the minimum value.
Definition enum_metadata.hpp:55
constexpr enum_metadata(Args const &...args) noexcept
Construct a enum-names table object.
Definition enum_metadata.hpp:81
constexpr value_type operator[](std::convertible_to< name_type > auto &&name) const noexcept
Get an enum-value from a name.
Definition enum_metadata.hpp:183
constexpr value_type at(std::convertible_to< name_type > auto &&name, value_type default_value) const noexcept
Get an enum-value from a name.
Definition enum_metadata.hpp:153
constexpr value_type at(std::convertible_to< name_type > auto &&name) const
Get an enum-value from a name.
Definition enum_metadata.hpp:123
constexpr name_type const & at(value_type value) const
Get a name from an enum-value.
Definition enum_metadata.hpp:138
Definition enum_metadata.hpp:276