7#include "../macros.hpp"
10#include "exception.hpp"
18hi_export_module(hikogui.utility.enum_metadata);
24hi_warning_ignore_msvc(26445);
26hi_export
namespace hi {
inline namespace v1 {
34template<
typename ValueType,
typename NameType, std::
size_t N>
48 static_assert(std::is_enum_v<value_type>,
"value_type Must be an enum");
49 static_assert(N != 0);
62 return std::get<0>(_by_value).value;
69 return std::get<N - 1>(_by_value).value;
85 template<
typename...
Args>
88 static_assert(
sizeof...(Args) == N * 2);
92 return a.name < b.name;
96 return std::to_underlying(a.value) < std::to_underlying(b.value);
107 [[
nodiscard]]
constexpr bool contains(std::convertible_to<name_type>
auto&& name)
const noexcept
109 return find(name_type{hi_forward(name)}) !=
nullptr;
119 return find(value) !=
nullptr;
128 [[
nodiscard]]
constexpr value_type
at(std::convertible_to<name_type>
auto&& name)
const
130 if (hilet *value = find(name_type{hi_forward(name)})) {
143 [[
nodiscard]]
constexpr name_type
const&
at(value_type value)
const
145 if (hilet *name = find(value)) {
157 [[
nodiscard]]
constexpr std::optional<value_type>
at_if(std::convertible_to<name_type>
auto&& name)
const noexcept
159 if (hilet *value = find(name_type{hi_forward(name)})) {
174 if (hilet *value = find(name_type{hi_forward(name)})) {
189 if (hilet *name = find(value)) {
204 auto *value = find(name_type{hi_forward(name)});
205 hi_assert_not_null(value);
217 auto *name = find(value);
218 hi_assert_not_null(name);
227 constexpr value_name()
noexcept : value(), name() {}
228 constexpr value_name(value_type value, name_type name)
noexcept : value(value), name(
std::move(name)) {}
234 [[
nodiscard]]
constexpr name_type
const *find(value_type value)
const noexcept
239 hilet offset = std::to_underlying(
it->value);
240 hilet i = std::to_underlying(value) - offset;
245 return item.value < key;
248 return (
it != _by_value.
end()
and it->value == value) ? &
it->name :
nullptr;
252 [[
nodiscard]]
constexpr value_type
const *find(name_type
const& name)
const noexcept
255 return item.name < key;
258 return (
it != _by_name.
end()
and it->name == name) ? &
it->value :
nullptr;
266 constexpr void add_value_name(value_type value, name_type name,
Rest const&...
rest)
noexcept
268 static_assert(
sizeof...(Rest) % 2 == 0);
270 std::get<I>(_by_name) = {value, name};
271 std::get<I>(_by_value) = {value,
std::move(name)};
273 if constexpr (
sizeof...(Rest) > 0) {
285 for (hilet& item : _by_value) {
286 if (std::to_underlying(item.value) !=
check_value++) {
296 using type = std::decay_t<T>;
Functions for casting values between types savely.
DOXYGEN BUG.
Definition algorithm.hpp:16
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
A object that holds enum-values and strings.
Definition enum_metadata.hpp:35
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:157
constexpr value_type maximum() const noexcept
Get the maximum value.
Definition enum_metadata.hpp:67
constexpr value_type operator[](std::convertible_to< name_type > auto &&name) const noexcept
Get an enum-value from a name.
Definition enum_metadata.hpp:202
constexpr value_type minimum() const noexcept
Get the minimum value.
Definition enum_metadata.hpp:60
constexpr bool contains(std::convertible_to< name_type > auto &&name) const noexcept
Check if the enum has a name.
Definition enum_metadata.hpp:107
constexpr bool contains(value_type value) const noexcept
Check if the enum has a value.
Definition enum_metadata.hpp:117
constexpr name_type const & at(value_type value) const
Get a name from an enum-value.
Definition enum_metadata.hpp:143
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:187
static constexpr std::size_t count
The number of enum values.
Definition enum_metadata.hpp:42
constexpr size_t size() const noexcept
Get the number of enum values.
Definition enum_metadata.hpp:53
constexpr value_type at(std::convertible_to< name_type > auto &&name) const
Get an enum-value from a name.
Definition enum_metadata.hpp:128
constexpr enum_metadata(Args const &...args) noexcept
Construct a enum-names table object.
Definition enum_metadata.hpp:86
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:172
bool values_are_continues
The numeric values in the enum do not contain a gap.
Definition enum_metadata.hpp:46
constexpr name_type const & operator[](value_type value) const noexcept
Get a name from an enum-value.
Definition enum_metadata.hpp:215
Definition enum_metadata.hpp:295