better-enums/doc/index.md
Anton Bachin faf3676fec Made it easier to generate offline documentation.
Documentation can be generated by going to doc/ and running "make".
This requires Python. Before this change, the user had to install the
mistune library, which is used by the generator. The mistune library is
now included in the Better Enums distribution.

The generated docs are available at doc/html/index.html. Note that some
links won't be local (the GitHub repo, the download link, outgoing
links to MSDN, tutorial source in the GitHub repo, and so on). All the
pages belonging to the actual docs will be local, however.

The online version of the docs can be generated by running "make web".
The only difference between the online and offline versions is that the
former includes Google Analytics tracking code, and may include social
communication buttons, comment section, or other useless things in the
future.

Also included errata since the last release.

Resolves #2.
2015-06-27 13:56:27 -05:00

5.4 KiB

enable

declare

parse format

count iterate

switch

safe cast

during compilation

#include <enum.h>

ENUM(Channel, int, Red = 1, Green, Blue)


Channel     c = Channel::_from_string("Red");
const char  *s = c._to_string();


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::_from_integral(3);


constexpr Channel c =
    Channel::_from_string("Blue");

$be 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 $cxx11, 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.

Try it live online in Wandbox, or begin the tutorial!

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 $cxx. 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 switch Use a Better Enum like a built-in enum, and still have the compiler do case checking.
  • Don't repeat yourself No more unmaintanable maps or switch statements for converting enums to strings.
  • Non-contiguous sequences Iteration and counting are much easier to maintain than with an extra Count constant and assuming a dense range.
  • Fast compilation Much less impact on build time than even just including iostream. enum.h is only slightly more than 1000 lines long.
  • Compile-time reflection Have the compiler do additional enum processing using your own templates or constexpr functions.
  • Uniform interface for $cxx98, $cxx11 Scoped, sized, reflective enums for $cxx98, and an easy upgrade path.
  • Stream operators Write enum names directly to std::cout or use boost::lexical_cast.
  • Non-integral underlying types Have sets of named, switch-friendly constants of any literal type.
  • Free and open source Released under the BSD license for use in any project, free or commercial.

Documentation

%% title = Clean reflective enums for C++

%% description = Reflective enums in a single header file, with clean syntax. The enums can be converted to string, iterated, and counted, at run time or as part of metaprogramming. Free and open source under the BSD license.

%% class = index