eenum is a utility macro that automatically builds metadata for an enum definition.
This library requires C++20.
A nix development environment is defined to easily run unit tests:
$ nix develop
$ buildWith meson, ninja and a C++20 capable compiler it is also possible to:
$ meson build
$ ninja -C build testTo use the library it is enough to include both the hpp files in src/ folder.
To define an empowered enum, the macro eenum must be called.
Its signatures is:
eenum(name, underlying type, values ...)For example:
eenum(test, unsigned, TEST1 = 5, TEST2, TEST3 = 42)Will define an enum called test that inherits from unsigned. It is equivalent to:
enum class test: unsigned
{
TEST1 = 5,
TEST2
TEST3 = 42
};The generated eenum can be used just like a normal enum class:
test t = test::TEST1;
switch(t)
{
case (test::TEST1): ...
case (test::TEST2): ...
} // compiler warning: TEST3 unhandledAnd it will be able to:
t = "TEST2"; //equivalent to t = test::TEST2
std::string t_string = t; //"TEST1"
t_string = t.to_string()l //"TEST1"Casts to and from strings are constexpr, but at runtime will cost respectively O(log(n)) and O(n), where n is the number of values.