Have you noticed the awkward situation with enums in $cxx? They are missing basic reflective features, such as string conversions. You are forced to put them through big `switch` statements and write duplicate enum names. It's a maintenance nightmare. $be is a short header file that gives you rich, reflective enums, with the nicest syntax yet seen. Just include it, and you are ready to go. You get scoped, sized, printable, iterable enums that support initializers and still play nice with `switch` case checking!
#include <iostream>
#include <enum.h>
ENUM(Channel, int,
Red = 1, Green, Blue)
int main()
{
Channel c = Channel::Red;
std::cout << c._to_string();
for (Channel c : Channel::_values())
std::cout << c._to_string();
switch (c) {
case Channel::Red:
return c._to_integral();
case Channel::Green: return 15;
case Channel::Blue: return 42;
}
}
constexpr Channel c =
Channel::_from_string("Blue");
#include <iostream>
#include <enum.h>
ENUM(Channel, int,
Red = 1, Green, Blue)
int main()
{
Channel c = Channel::Red;
std::cout << c._to_string();
for (size_t i = 0;
i < Channel::_size; ++i) {
c = Channel::_values()[i];
std::cout << c._to_string();
}
switch (c) {
case Channel::Red:
return c._to_integral();
case Channel::Green: return 15;
case Channel::Blue: return 42;
}
}