16#include <unordered_map>
20#include "CompilerSupport.hpp"
76#define DFX_BUILDER_ENUM_TO_STRING_MAPPING_IMPL(v, l) case v: return l;
77#define DFX_ENUM_TO_STRING_GENERATE_MAPPING(...) DFX_GENERATOR_2_X(DFX_BUILDER_ENUM_TO_STRING_MAPPING_IMPL, __VA_ARGS__)
79#define DFX_BUILDER_ENUM_FROM_STRING_MAPPING_IMPL(v, l) { v, l },
80#define DFX_ENUM_FROM_STRING_GENERATE_MAPPING(...) DFX_GENERATOR_2_X(DFX_BUILDER_ENUM_FROM_STRING_MAPPING_IMPL, __VA_ARGS__)
82#define DFX_BUILDER_ENUM_STRING_ONE_OUT_OF_TWO_1_IMPL(v, l) v,
83#define DFX_ENUM_STRING_ONE_OUT_OF_TWO_1(...) DFX_GENERATOR_2_X(DFX_BUILDER_ENUM_STRING_ONE_OUT_OF_TWO_1_IMPL, __VA_ARGS__)
85#define DFX_BUILDER_ENUM_STRING_ONE_OUT_OF_TWO_2_IMPL(v, l) l,
86#define DFX_ENUM_STRING_ONE_OUT_OF_TWO_2(...) DFX_GENERATOR_2_X(DFX_BUILDER_ENUM_STRING_ONE_OUT_OF_TWO_2_IMPL, __VA_ARGS__)
103#define DFX_ENUM_TO_STRING_IMPL(E, ...) \
104 namespace dfx::Enum \
106 [[nodiscard]] std::string_view toStringView(E e) noexcept \
110 DFX_ENUM_TO_STRING_GENERATE_MAPPING(__VA_ARGS__) \
112 DFX_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 DFX_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 DFX_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 DFX_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 DFX_ENUM_FROM_STRING_GENERATE_MAPPING(__VA_ARGS__) \
235 [[nodiscard]] std::vector<E>
const &
allValues() noexcept = delete;
252#define DFX_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 DFX_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 DFX_ENUM_STRING_ONE_OUT_OF_TWO_2(__VA_ARGS__) \
284#define DFX_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 DFX_DEFINE_ENUM_STRING_FUNCTIONS(Enum, ...) \
308 DFX_ENUM_TO_STRING_IMPL(Enum, __VA_ARGS__) \
309 DFX_ENUM_FROM_STRING_IMPL(Enum) \
310 DFX_ENUM_ALL_VALUES_MAP_IMPL(Enum, DFX_SWAP_POSITION(__VA_ARGS__)) \
311 DFX_ENUM_ALL_VALUES_IMPL(Enum, __VA_ARGS__)
327#define DFX_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 DFX_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.