![]() |
dfx 0.1.0
Linux-based dynamic dataflow executor
|
Macro-based enum <-> string utilities for dfx. More...
Go to the source code of this file.
Namespaces | |
| namespace | dfx::Enum |
| Enum string conversion and enumeration utilities. | |
Macros | |
| #define | ENUM_TO_STRING_IMPL(E, ...) |
| Define dfx::Enum::toStringView(E) and dfx::Enum::toString(E) for enum type E. | |
| #define | ENUM_FROM_STRING_IMPL(E) |
| Define dfx::Enum::fromStringView<E> and dfx::Enum::fromString<E> specializations for enum E. | |
| #define | ENUM_ALL_VALUES_MAP_IMPL(E, ...) |
| Define allValuesMap<E>() and allValuesMapView<E>() specializations for enum E. | |
| #define | ENUM_ALL_VALUES_IMPL(E, ...) |
| Define allValues<E>() and allValuesStringView<E>() specializations for enum E. | |
| #define | ENUM_STD_FMT_FORMATTER(E) |
| Define std::formatter<E> formatting the enum as its label via dfx::Enum::toStringView. | |
| #define | DEFINE_ENUM_STRING(Enum, ...) |
| Define the full enum string API for Enum (definitions + maps + value lists). | |
| #define | DECLARE_ENUM_STRING_FUNCTIONS(E) |
| Declare the enum string API (and std::formatter) for enum type E. | |
Functions | |
| template<typename E> | |
| std::string_view | dfx::Enum::toStringView (E e) noexcept |
| Convert an enum value into a string view. | |
| template<typename E> | |
| std::string | dfx::Enum::toString (E e) noexcept |
| Convert an enum value into a string. | |
| template<typename E> | |
| std::optional< E > | dfx::Enum::fromStringView (std::string_view) noexcept=delete |
| Convert a string view to an enum value. | |
| template<typename E> | |
| std::optional< E > | dfx::Enum::fromString (std::string const &) noexcept=delete |
| Convert a string to an enum value. | |
| template<typename E> | |
| std::unordered_map< std::string, E > const & | dfx::Enum::allValuesMap () noexcept=delete |
| Map from label (owned string) to enum value for type E. | |
| template<typename E> | |
| std::unordered_map< std::string_view, E > const & | dfx::Enum::allValuesMapView () noexcept=delete |
| Map from label (string_view) to enum value for type E. | |
| template<typename E> | |
| std::vector< E > const & | dfx::Enum::allValues () noexcept=delete |
| Return all enum values registered for E, in the order provided to the macro. Deleted by default; provided via macro specialization. | |
| template<typename E> | |
| std::vector< std::string_view > const & | dfx::Enum::allValuesStringView () noexcept=delete |
| Return all labels registered for E, in the order provided to the macro. Deleted by default; provided via macro specialization. | |
Macro-based enum <-> string utilities for dfx.
This header provides a small "enum reflection" facility (without language reflection) by generating:
1) Define an enum:
2) In a header, declare the API for that enum:
3) In exactly one .cpp (or one TU), provide the definitions:
Then you can do:
| #define DECLARE_ENUM_STRING_FUNCTIONS | ( | E | ) |
Declare the enum string API (and std::formatter) for enum type E.
Put this in a header next to the enum definition. Then put DEFINE_ENUM_STRING(E, ...) in one .cpp to provide the definitions.
Declares:
| #define DEFINE_ENUM_STRING | ( | Enum, | |
| ... ) |
Define the full enum string API for Enum (definitions + maps + value lists).
Expands to:
| Enum | The enum type. |
| ... | Alternating (enumValue, label) pairs. |
| #define ENUM_ALL_VALUES_IMPL | ( | E, | |
| ... ) |
Define allValues<E>() and allValuesStringView<E>() specializations for enum E.
| E | The enum type. |
| ... | Alternating (enumValue, label) pairs. |
| #define ENUM_ALL_VALUES_MAP_IMPL | ( | E, | |
| ... ) |
Define allValuesMap<E>() and allValuesMapView<E>() specializations for enum E.
The variadic arguments are expected as alternating (label, enumValue) pairs (note the order), which is why DEFINE_ENUM_STRING feeds it with SWAP_POSITION(__VA_ARGS__).
| #define ENUM_FROM_STRING_IMPL | ( | E | ) |
Define dfx::Enum::fromStringView<E> and dfx::Enum::fromString<E> specializations for enum E.
Implementation performs a lookup in allValuesMapView<E>().
| #define ENUM_STD_FMT_FORMATTER | ( | E | ) |
Define std::formatter<E> formatting the enum as its label via dfx::Enum::toStringView.
This allows:
| #define ENUM_TO_STRING_IMPL | ( | E, | |
| ... ) |
Define dfx::Enum::toStringView(E) and dfx::Enum::toString(E) for enum type E.
Emits:
| E | The enum type (e.g. Color). |
| ... | Alternating (enumValue, label) pairs, e.g. Color::Red, "red", Color::Blue, "blue". |