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)) {
152 [[nodiscard]]
constexpr std::optional<value_type>
at_if(std::convertible_to<name_type>
auto&& name)
const noexcept
167 [[nodiscard]]
constexpr value_type
at(std::convertible_to<name_type>
auto&& name, value_type default_value)
const noexcept
172 return default_value;
182 [[nodiscard]]
constexpr name_type
at(value_type value, std::convertible_to<name_type>
auto&& default_name)
const noexcept
184 if (
hilet *name = find(value)) {
197 [[nodiscard]]
constexpr value_type
operator[](std::convertible_to<name_type>
auto&& name)
const noexcept
199 auto *value = find(name_type{
hi_forward(name)});
210 [[nodiscard]]
constexpr name_type
const&
operator[](value_type value)
const noexcept
212 auto *name = find(value);
222 constexpr value_name() noexcept : value(), name() {}
223 constexpr value_name(value_type value, name_type name) noexcept : value(value), name(
std::move(name)) {}
229 [[nodiscard]]
constexpr name_type
const *
find(value_type value)
const noexcept
231 if (values_are_continues) {
234 hilet offset = to_underlying(it->value);
235 hilet i = to_underlying(value) - offset;
236 return (i >= 0 and i < N) ? &(it + i)->name : nullptr;
240 return item.value < key;
243 return (it != _by_value.
end() and it->value == value) ? &it->name :
nullptr;
247 [[nodiscard]]
constexpr value_type
const *
find(name_type
const& name)
const noexcept
250 return item.name < key;
253 return (it != _by_name.
end() and it->name == name) ? &it->value :
nullptr;
261 constexpr void add_value_name(value_type value, name_type name, Rest
const&...rest)
noexcept
263 static_assert(
sizeof...(Rest) % 2 == 0);
265 std::get<I>(_by_name) = {value, name};
266 std::get<I>(_by_value) = {value,
std::move(name)};
268 if constexpr (
sizeof...(Rest) > 0) {
269 add_value_name<I + 1>(rest...);
277 [[nodiscard]]
constexpr bool check_values_are_continues() const noexcept
279 auto check_value = to_underlying(minimum());
280 for (
hilet& item : _by_value) {
281 if (to_underlying(item.value) != check_value++) {
291 using type = std::decay_t<T>;
302using enum_metadata_name_t = enum_metadata_name<T>::type;
304template<
typename ValueType,
typename NameType,
typename... Rest>
305enum_metadata(ValueType
const&, NameType
const&, Rest
const&...)
#define hi_assert_not_null(x,...)
Assert if an expression is not nullptr.
Definition assert.hpp:238
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 std::optional< value_type > at_if(std::convertible_to< name_type > auto &&name) const noexcept
Get an enum-value from a name.
Definition enum_metadata.hpp:152
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:210
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:182
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:197
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:167
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:290