mirror of
https://github.com/aantron/better-enums.git
synced 2025-12-13 15:09:53 +08:00
support to VC++. The unit test is currently not being run on VC++ due to a problem with CxxTest, Cygwin, and paths. However, the examples are being compiled and having their output checked, and the multiple translation unit test is being run. Running "(cd test ; ./test.py)" should now run tests only using the default compiler, on a Unix-like system. test.py --all runs tests on the full array of compilers that I have installed and symlinked on my development machines.
66 lines
2.1 KiB
Markdown
66 lines
2.1 KiB
Markdown
# Better Enums
|
||
|
||
Reflective compile-time C++ enum library with clean syntax. For example:
|
||
|
||
ENUM(Channel, int, Red = 1, Green, Blue)
|
||
|
||
defines a type `Channel`. You can then do natural things such as:
|
||
|
||
```cpp
|
||
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 (C++11 only)
|
||
|
||
for (Channel channel : Channel::_values()) {
|
||
// Iterate over all channels
|
||
}
|
||
```
|
||
|
||
...and more. See the [project page](http://aantron.github.io/better-enums).
|
||
|
||
## Installation
|
||
|
||
Simply add `enum.h` to your project.
|
||
|
||
## Features
|
||
|
||
- Requires no external utility.
|
||
- Safe conversions to/from integers and strings.
|
||
- Iteration over declared values.
|
||
- Switch case checking.
|
||
- All operations are `constexpr` and can be used at compile time in your own
|
||
`constexpr` code. See demos on the
|
||
[project page](http://aantron.github.io/better-enums) for how to define
|
||
default values, for example.
|
||
- Constant values can be initialized with expressions (`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.3 to 5.1, clang 3.3 to 3.6, and VS2013, VS2015.
|
||
|
||
## Contact
|
||
|
||
Don't hesitate to contact me about features (or bugs!):
|
||
<a href="mailto:antonbachin@yahoo.com">antonbachin@yahoo.com</a>
|
||
|
||
## License
|
||
|
||
Better Enums is released under the BSD 2-clause license. See
|
||
[LICENSE](https://github.com/aantron/better-enums/blob/master/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`.
|