16#include <unordered_map>
20#include "CompilerSupport.hpp"
76#define _BUILDER_ENUM_TO_STRING_MAPPING(v, l) case v: return l;
77#define ENUM_TO_STRING_GENERATE_MAPPING(...) GENERATOR_2_X(_BUILDER_ENUM_TO_STRING_MAPPING, __VA_ARGS__)
79#define _BUILDER_ENUM_FROM_STRING_MAPPING(v, l) { v, l },
80#define ENUM_FROM_STRING_GENERATE_MAPPING(...) GENERATOR_2_X(_BUILDER_ENUM_FROM_STRING_MAPPING, __VA_ARGS__)
82#define _BUILDER_ENUM_STRING_ONE_OUT_OF_TWO_1(v, l) v,
83#define ENUM_STRING_ONE_OUT_OF_TWO_1(...) GENERATOR_2_X(_BUILDER_ENUM_STRING_ONE_OUT_OF_TWO_1, __VA_ARGS__)
85#define _BUILDER_ENUM_STRING_ONE_OUT_OF_TWO_2(v, l) l,
86#define ENUM_STRING_ONE_OUT_OF_TWO_2(...) GENERATOR_2_X(_BUILDER_ENUM_STRING_ONE_OUT_OF_TWO_2, __VA_ARGS__)
103#define ENUM_TO_STRING_IMPL(E, ...) \
104 namespace dfx::Enum \
106 [[nodiscard]] std::string_view toStringView(E e) noexcept \
110 ENUM_TO_STRING_GENERATE_MAPPING(__VA_ARGS__) \
112 UNREACHABLE_IMPL(); \
114 [[nodiscard]] std::string toString(E e) noexcept \
115 { return std::string(toStringView(e)); } \
144 [[nodiscard]] std::optional<E>
fromStringView(std::string_view)
noexcept =
delete;
151 [[nodiscard]] std::optional<E>
fromString(std::string
const &)
noexcept =
delete;
163#define ENUM_FROM_STRING_IMPL(E) \
164 namespace dfx::Enum \
167 [[nodiscard]] std::optional<E> fromStringView(std::string_view str) noexcept \
169 auto const & map = allValuesMapView<E>(); \
170 auto const itr = map.find(str); \
171 if (itr == map.end()) \
172 return std::nullopt; \
173 return itr->second; \
176 [[nodiscard]] std::optional<E> fromString(std::string const & str) noexcept \
177 { return fromStringView<E>(str); } \
188 [[nodiscard]] std::unordered_map<std::string, E>
const &
allValuesMap() noexcept = delete;
208#define ENUM_ALL_VALUES_MAP_IMPL(E, ...) \
209 namespace dfx::Enum \
212 [[nodiscard]] std::unordered_map<std::string, E> const & allValuesMap() noexcept \
214 static std::unordered_map<std::string, E> const map = { \
215 ENUM_FROM_STRING_GENERATE_MAPPING(__VA_ARGS__) \
220 [[nodiscard]] std::unordered_map<std::string_view, E> const & allValuesMapView() noexcept \
222 static std::unordered_map<std::string_view, E> const map = { \
223 ENUM_FROM_STRING_GENERATE_MAPPING(__VA_ARGS__) \
235 [[nodiscard]] std::vector<E>
const &
allValues() noexcept = delete;
252#define ENUM_ALL_VALUES_IMPL(E, ...) \
253 namespace dfx::Enum \
256 [[nodiscard]] std::vector<E> const & allValues() noexcept \
258 static std::vector<E> const vec = { \
259 ENUM_STRING_ONE_OUT_OF_TWO_1(__VA_ARGS__) \
264 [[nodiscard]] std::vector<std::string_view> const & allValuesStringView<E>() noexcept \
266 static std::vector<std::string_view> const vec = { \
267 ENUM_STRING_ONE_OUT_OF_TWO_2(__VA_ARGS__) \
284#define ENUM_STD_FMT_FORMATTER(E) \
286struct std::formatter<E> : public formatter<std::string_view> \
288 auto format(E const & e, format_context & ctx) const \
289 { return formatter<std::string_view>::format(dfx::Enum::toStringView(e), ctx); } \
307#define DEFINE_ENUM_STRING(Enum, ...) \
308 ENUM_TO_STRING_IMPL(Enum, __VA_ARGS__) \
309 ENUM_FROM_STRING_IMPL(Enum) \
310 ENUM_ALL_VALUES_MAP_IMPL(Enum, SWAP_POSITION(__VA_ARGS__)) \
311 ENUM_ALL_VALUES_IMPL(Enum, __VA_ARGS__)
327#define DECLARE_ENUM_STRING_FUNCTIONS(E) \
328 namespace dfx::Enum \
330 [[nodiscard]] std::string_view toStringView(E e) noexcept; \
331 [[nodiscard]] std::string toString(E e) noexcept; \
332 template<> [[nodiscard]] std::optional<E> fromStringView(std::string_view str) noexcept; \
333 template<> [[nodiscard]] std::optional<E> fromString(std::string const & str) noexcept; \
334 template<> [[nodiscard]] std::unordered_map<std::string_view, E> const & allValuesMapView() noexcept; \
335 template<> [[nodiscard]] std::unordered_map<std::string, E> const & allValuesMap() noexcept; \
336 template<> [[nodiscard]] std::vector<E> const & allValues() noexcept; \
337 template<> [[nodiscard]] std::vector<std::string_view> const & allValuesStringView<E>() noexcept; \
339 ENUM_STD_FMT_FORMATTER(E)
Enum string conversion and enumeration utilities.
Definition EnumString.hpp:120
std::string toString(E e) noexcept
Convert an enum value into a string.
std::unordered_map< std::string, E > const & allValuesMap() noexcept=delete
Map from label (owned string) to enum value for type E.
std::vector< std::string_view > const & allValuesStringView() noexcept=delete
Return all labels registered for E, in the order provided to the macro. Deleted by default; provided ...
std::unordered_map< std::string_view, E > const & allValuesMapView() noexcept=delete
Map from label (string_view) to enum value for type E.
std::optional< E > fromString(std::string const &) noexcept=delete
Convert a string to an enum value.
std::vector< E > const & allValues() noexcept=delete
Return all enum values registered for E, in the order provided to the macro. Deleted by default; prov...
std::string_view toStringView(E e) noexcept
Convert an enum value into a string view.
std::optional< E > fromStringView(std::string_view) noexcept=delete
Convert a string view to an enum value.