draconisplusplus/include/rfl/internal/enums/Names.hpp

75 lines
2.8 KiB
C++
Raw Normal View History

2024-05-31 22:59:00 -04:00
#ifndef RFL_INTERNAL_ENUMS_NAMES_HPP_
#define RFL_INTERNAL_ENUMS_NAMES_HPP_
#include <algorithm>
#include <array>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include "../../Literal.hpp"
#include "../../define_literal.hpp"
#include "../../make_named_tuple.hpp"
#include "../StringLiteral.hpp"
namespace rfl {
2024-06-08 14:10:59 -04:00
namespace internal {
namespace enums {
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
template <class EnumType, class LiteralType, size_t N, auto... _enums>
struct Names {
/// Contains a collection of enums as compile-time strings.
using Literal = LiteralType;
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
/// The number of possible values
constexpr static size_t size = N;
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
/// A list of all the possible enums
2024-06-16 00:13:15 -04:00
constexpr static std::array<EnumType, N> enums_ = std::array<EnumType, N> { _enums... };
2024-05-31 22:59:00 -04:00
2024-06-16 00:13:15 -04:00
static_assert(N == 0 || LiteralType::size() == N, "Size of literal and enum do not match.");
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
template <class NewLiteral, auto _new_enum>
2024-06-08 15:53:06 -04:00
using AddOneType = std::conditional_t<
2024-06-16 00:13:15 -04:00
N == 0,
Names<EnumType, NewLiteral, 1, _new_enum>,
Names<EnumType, define_literal_t<LiteralType, NewLiteral>, N + 1, _enums..., _new_enum>>;
2024-06-08 14:10:59 -04:00
};
2024-05-31 22:59:00 -04:00
2024-06-16 00:13:15 -04:00
template <class EnumType, size_t N, StringLiteral... _names, auto... _enums>
auto names_to_enumerator_named_tuple(Names<EnumType, Literal<_names...>, N, _enums...>) {
return make_named_tuple(Field<_names, EnumType> { _enums }...);
2024-06-08 14:10:59 -04:00
}
2024-05-31 22:59:00 -04:00
2024-06-16 00:13:15 -04:00
template <class EnumType, size_t N, StringLiteral... _names, auto... _enums>
2024-06-08 15:53:06 -04:00
auto names_to_underlying_enumerator_named_tuple(Names<
EnumType,
Literal<_names...>,
N,
_enums...>) {
2024-06-16 00:13:15 -04:00
return make_named_tuple(Field<_names, std::underlying_type_t<EnumType>> {
static_cast<std::underlying_type_t<EnumType>>(_enums) }...);
2024-06-08 14:10:59 -04:00
}
2024-05-31 22:59:00 -04:00
2024-06-16 00:13:15 -04:00
template <class EnumType, size_t N, StringLiteral... _names, auto... _enums>
2024-06-08 14:10:59 -04:00
constexpr std::array<std::pair<std::string_view, EnumType>, N>
2024-06-16 00:13:15 -04:00
names_to_enumerator_array(Names<EnumType, Literal<_names...>, N, _enums...>) {
return { std::make_pair(LiteralHelper<_names>::field_.string_view(), _enums)... };
2024-06-08 14:10:59 -04:00
}
2024-05-31 22:59:00 -04:00
2024-06-16 00:13:15 -04:00
template <class EnumType, size_t N, StringLiteral... _names, auto... _enums>
constexpr std::array<std::pair<std::string_view, std::underlying_type_t<EnumType>>, N>
names_to_underlying_enumerator_array(Names<EnumType, Literal<_names...>, N, _enums...>) {
return { std::make_pair(
LiteralHelper<_names>::field_.string_view(),
static_cast<std::underlying_type_t<EnumType>>(_enums)
)... };
2024-06-08 14:10:59 -04:00
}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
} // namespace enums
} // namespace internal
} // namespace rfl
2024-05-31 22:59:00 -04:00
#endif