Anton Bachin 1b3d1cc784 Forbade nearly all implicit conversions to integral types.
Each Better Enum now has an internal enum class type to which it is convertible,
instead of being convertible to the regular enum that defines its constants.
switch statements are compiled at the enum class type. This comes at the price
of the user having to type +Enum::Constant instead of Enum::Constant in cases,
in order to trigger an explicit promotion of the pre-C++11 enum to Better Enum,
so it can then be implicitly converted to the enum class.

The remaining "hole" is that direct references to constants (Enum::Constant) are
still implicitly convertible to integral types, because they have naked
pre-C++11 enum type.
2015-05-18 19:56:17 -05:00
doc Minimally updated documentation. 2015-05-17 22:49:24 -05:00
example Forbade nearly all implicit conversions to integral types. 2015-05-18 19:56:17 -05:00
test Forbade nearly all implicit conversions to integral types. 2015-05-18 19:56:17 -05:00
.gitattributes Initial release. 2015-05-11 17:38:41 -04:00
.gitignore Removed most weak symbols. Iterators should now be random access. 2015-05-17 18:47:51 -05:00
enum.h Forbade nearly all implicit conversions to integral types. 2015-05-18 19:56:17 -05:00
LICENSE Initial release. 2015-05-11 17:38:41 -04:00
pp_map_gen.py Option to opt in to implicit conversion to enum class instead of enum. 2015-05-18 00:53:00 -05:00
README.md Minimally updated documentation. 2015-05-17 22:49:24 -05:00

Better Enums

Reflective compile-time C++11 enum library with clean syntax. For example:

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

defines a type Channel. You can then do natural things like:

Channel channel = Channel::Green;

channel.to_string();            // Results in the string "Green"
channel.to_integral();          // Results in the int 2

Channel::_from_string("Red");   // Results in Channel::Red
Channel::_from_integral(3);     // Results in Channel::Blue

constexpr auto channel = Channel::_from_integral(3);
                                // Do it at compile time
constexpr auto channel = Channel::_max;
                                // Channel::Blue

for (Channel channel : Channel::_values) {
    // Iterate over all channels
}

...and more. See the project page and examples for a tutorial.

Installation

Simply add enum.h from master to your project.

Features

  • Generated at compile time by constexpr functions and the preprocessor.
  • Safe conversions between enums and integers and strings. 1-basic.cc
  • Iterable collections of constants and names. 2-iterate.cc
  • Range information, such as the number of constants defined and the maximum constant. 2-iterate.cc
  • Switch case checking. 3-switch.cc
  • All operations are constexpr and can be used at compile time in your own constexpr code. 4-constexpr.cc
  • Constant values can be set (Red = 1) and aliased (Favorite = Green), just like with built-in enums.
  • Generating a large number of enums is about as fast as including a typical standard header like iostream performance test included.
  • Explicit choice of underlying representation type.
  • Header-only.
  • No dependencies besides the standard library.
  • Tested on gcc 4.9 and clang 3.6.

The library is standard C++, but it compiles only with gcc and clang due to lagging C++11 support in msvc. A future runtime fallback version will allow msvc and non-C++11 usage.

Contact

Don't hesitate to contact me about features (or bugs!): antonbachin@yahoo.com

Explanation

The ENUM macro specializes a template based around a regular enum declaration, though it is more similar to enum class in the degree of type safety. The following are spiritually equivalent:

ENUM(Channel, int, Red = 1, Green, Blue);
enum class Channel : int {Red = 1, Green, Blue};

ENUM(Depth, char, Indexed8Bit, HighColor, TrueColor);
enum class Depth : char {Indexed8Bit, HighColor, TrueColor};

See the full documentation.

Development plan

There are several areas that still need improvement.

  • Some enum types might have a sensible choice for a default constructor. The library should allow it to be customized.
  • All safety checks are currently done by linear scans. This may be a performance problem for enum types with many constants.
  • Better diagnostics for empty enums or too many constants.
  • Conversions from integers and strings that don't throw exceptions, but indicate failure by some other means.

License

Better Enums is released under the BSD 2-clause license. See LICENSE.

History

The library was originally developed by the author in the winter of 2012-2013 at Hudson River Trading, as a replacement for an older generator called BETTER_ENUM.