better-enums/doc/index.md
Anton Bachin 2acb5743fa Complete documentation and testing overhaul.
The documentation is now generated from markdown. Samples are generated from the
tutorial pages. Testing is done by a Python script which runs the tests for a
large number of compilers.

This version is not very developer-friendly - the Python scripts need ways of
limiting what compilers they try to run. If you don't have 15 compilers
installed, you won't be able to run the tests in this commit. Fix coming soon.
2015-05-27 09:58:34 -05:00

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 &lt;iostream&gt;

#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 &lt;&lt; <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++