Better Enums - Clean reflective enums for C++ (original) (raw)
enable
declare
parse format
count iterate
switch
safe cast
during compilation
#include <enum.h>
BETTERENUM(Channel, int, Red = 1, Green, Blue)
Channel c = Channel::fromstring("Red"); const char *s = c.tostring();
size_t n = Channel::size(); for (Channel c : Channel::values()) { run_some_function(c); }
switch (c) { case Channel::Red: // ... case Channel::Green: // ... case Channel::Blue: // ... }
Channel c = Channel::fromintegral(3);
constexpr Channel c = Channel::fromstring("Blue");
Better Enums is a single, lightweight header file that makes your compiler generate_reflective_ enum types.
That means you can easily convert enums to and from strings, validate them, and loop over them. In C++11, you can do it all at compile time.
It's what built-in enums ought to support. Better Enums simply adds the missing features. And, it is based on the best known techniques, thoroughly tested, fast, portable, and documented exhaustively.
To use it, just include enum.h and begin thetutorial!
Highlights
- Unobtrusive syntax No ugly macros. Use initializers as with built-in
enums. Internal members have underscores to avoid clashing with your constant names. - No external dependencies Uses only standard C++. Installation is simple — just download
enum.h. There are no objects or libraries to link with. - No generator program needed Just include
enum.h. It's a metaprogram executed by your compiler. - Plays nice with
switchUse a Better Enum like a built-inenum, and still have the compiler do case checking. - Don't repeat yourself No more unmaintanable maps or
switchstatements for converting enums to strings. - Non-contiguous sequences Iteration and counting are much easier to maintain than with an extra
Countconstant and assuming a dense range. - Fast compilation Much less impact on build time than even just including
iostream.enum.his only slightly more than 1000 lines long. - Compile-time reflection Have the compiler do additional enum processing using your own templates or
constexprfunctions. - Uniform interface for C++98, C++11 Scoped, sized, reflective enums for C++98, and an easy upgrade path.
- Stream operators Write enum names directly to
std::coutor useboost::lexicalcast. - Free and open source Released under the BSD license for use in any project, free or commercial.
Documentation
- Tutorial
- Reference
- Advanced