#include "wise_enum.h" #include "compact_optional.h" #include #include // Equivalent to enum Color {GREEN = 2, RED}; namespace my_lib { WISE_ENUM(Color, (GREEN, 2), RED) } // Equivalent to enum class MoreColor : int64_t {BLUE, BLACK = 1}; WISE_ENUM_CLASS((MoreColor, int64_t), BLUE, (BLACK, 1)) // Inside a class, must use a different macro, but still works struct Bar { WISE_ENUM_MEMBER(Foo, BUZ) }; // Adapt an existing enum you don't control so it works with generic code namespace another_lib { enum class SomebodyElse { FIRST, SECOND }; } WISE_ENUM_ADAPT(another_lib::SomebodyElse, FIRST, SECOND) int main() { // Number of enumerations: static_assert(wise_enum::size == 2, ""); std::cerr << "Number of enumerators: " << wise_enum::size << "\n"; // Iterate over enums std::cerr << "Enum values and names:\n"; for (auto e : wise_enum::range) { std::cerr << static_cast(e.value) << " " << e.name << "\n"; } std::cerr << "\n"; // Convert any enum to a string std::cerr << wise_enum::to_string(my_lib::Color::RED) << "\n"; // Convert any string to an optional enum auto x1 = wise_enum::from_string("GREEN"); auto x2 = wise_enum::from_string("Greeeeeeen"); assert(x1.value() == my_lib::Color::GREEN); assert(!x2); // Everything is constexpr, and a type trait is made available for easy use in // enable_if/tag dispatch static_assert(wise_enum::is_wise_enum_v, ""); static_assert(!wise_enum::is_wise_enum_v, ""); enum flub { blub, glub }; static_assert(!wise_enum::is_wise_enum_v, ""); // We made a regular enum wise! static_assert(wise_enum::is_wise_enum_v, ""); // Assert underlying type static_assert(std::is_same>::value, ""); // Full API available for adapted wise enums for (auto e : wise_enum::range) { std::cerr << static_cast(e.value) << " " << wise_enum::to_string(e.value) << "\n"; } // Compact optional demo wise_enum::compact_optional o; assert(!o); assert(o.value_or(my_lib::Color::RED) == my_lib::Color::RED); o = my_lib::Color::GREEN; assert(o.value() == my_lib::Color::GREEN); o.reset(); assert(!o); return 0; }