21template <
typename EnumType>
constexpr bool good_enum(EnumType x)
noexcept {
22 return std::size_t(x) < std::size_t(EnumType::FINAL);
33template <
typename EnumType>
34constexpr std::array<std::string_view, size_t(EnumType::FINAL)>
36 std::array<std::string_view, size_t(EnumType::FINAL)> out;
39 std::string_view::size_type N0 = 0;
42 for (
auto &str : out) {
44 const std::string_view::size_type
N1 =
45 std::min(strchars.find(
',', N0), strchars.size());
48 str = strchars.substr(N0,
N1 - N0);
63template <
typename EnumType>
74template <
typename EnumType>
75constexpr std::array<EnumType, size_t(EnumType::FINAL)>
77 std::array<EnumType, size_t(EnumType::FINAL)> out{};
78 for (
size_t i = 0; i < size_t(EnumType::FINAL); i++)
89template <
typename EnumType,
typename... Messages>
142#define ENUMCLASS(ENUMTYPE, TYPE, ...) \
143 enum class ENUMTYPE : TYPE { __VA_OPT__(__VA_ARGS__, ) FINAL }; \
145 namespace enumstrs { \
146 constexpr auto ENUMTYPE##Names = \
147 enum_strarray<ENUMTYPE>(__VA_OPT__(#__VA_ARGS__)); \
150 namespace enumtyps { \
151 [[maybe_unused]] constexpr auto ENUMTYPE##Types = enum_typarray<ENUMTYPE>(); \
154 constexpr std::string_view toString(ENUMTYPE x) noexcept { \
156 return enumstrs::ENUMTYPE##Names[(TYPE)x]; \
157 return "BAD " #ENUMTYPE; \
160 constexpr ENUMTYPE to##ENUMTYPE(const std::string_view x) noexcept { \
161 for (TYPE i = 0; i < (TYPE)ENUMTYPE::FINAL; i++) \
162 if (enumstrs::ENUMTYPE##Names[i] == x) \
163 return ENUMTYPE(i); \
164 return ENUMTYPE::FINAL; \
167 constexpr ENUMTYPE to##ENUMTYPE##OrThrow(const std::string_view x) { \
168 const ENUMTYPE out = to##ENUMTYPE(x); \
169 check_enum_error(out, "Cannot understand argument: \"", x, \
172 " options are: [" __VA_OPT__(#__VA_ARGS__) "]"); \
176 inline std::ostream &operator<<(std::ostream &os, const ENUMTYPE x) { \
177 return os << toString(x); \
180 inline std::istream &operator>>(std::istream &is, ENUMTYPE &x) { \
183 x = to##ENUMTYPE##OrThrow(val); \
Helper macros for debugging.
#define ARTS_USER_ERROR_IF(condition,...)
constexpr std::array< EnumType, size_t(EnumType::FINAL)> enum_typarray() noexcept
A list of all enum types by index-conversion.
constexpr std::array< std::string_view, 0 > enum_strarray() noexcept
constexpr void check_enum_error(EnumType type, Messages... args)
Checks if the enum class type is good and otherwise throws an error message composed by variadic inpu...
constexpr bool good_enum(EnumType x) noexcept
Checks if the enum number is good.
constexpr int isspace(int ch) noexcept
Returns 1 if x is a standard space-character.