Worked around a bug with vararg macro expansion in VC++ and tested with Visual Studio 2013. This commit does not include exhaustive tests for that compiler as for clang and gcc. They are coming in a follow-on commit. https://connect.microsoft.com/VisualStudio/feedback/details/521844/variadic-macro-treating-va-args-as-a-single-parameter-for-other-macros
3.0 KiB
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!
$cxx11
<pre>#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");
$cxx98
#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 = <em>Channel::_values()[i]</em>; std::cout << <em>c._to_string()</em>;}
switch (c) { case Channel::Red: return c._to_integral(); case Channel::Green: return 15; case Channel::Blue: return 42; } }
To install, simply <a $download>download enum.h and add it to your project.
That's all. The library is header-only and has no dependencies. It is published under the BSD license, so you can do pretty much anything you want with it.
Better Enums is under active development and will always be supported. Follow the project on GitHub for updates.
Tutorials
-
$tutorial_toc
Advanced demos
-
$demo_toc
%% title = Clean reflective enums for C++