enable 3 | 4 | declare 5 | 6 | 7 | parse 8 | format 9 | 10 | 11 | count 12 | iterate 13 | 14 | 15 | 16 | 17 | switch 18 | 19 | 20 | 21 | 22 | 23 | 24 | safe cast 25 | 26 | 27 | during 28 | compilation 29 |30 |
#include <enum.h> 31 | 32 | BETTER_ENUM(Channel, int, Red = 1, Green, Blue) 33 | 34 | 35 | Channel c = Channel::_from_string("Red"); 36 | const char *s = c._to_string(); 37 | 38 | 39 | size_t n = Channel::_size(); 40 | for (Channel c : Channel::_values()) { 41 | run_some_function(c); 42 | } 43 | 44 | 45 | switch (c) { 46 | case Channel::Red: // ... 47 | case Channel::Green: // ... 48 | case Channel::Blue: // ... 49 | } 50 | 51 | 52 | Channel c = Channel::_from_integral(3); 53 | 54 | 55 | constexpr Channel c = 56 | Channel::_from_string("Blue");57 |
60 | $be is a single, lightweight header file that makes your compiler generate 61 | reflective enum types. 62 |
63 | 64 | That means you can easily convert enums to and from strings, 65 | validate them, and loop over them. In $cxx11, you can do it all at 66 | compile time. 67 | 68 | It's what built-in enums ought to support. Better Enums simply adds the missing 69 | features. And, it is based on the best known techniques, thoroughly tested, 70 | fast, portable, and documented exhaustively. 71 | 72 | To use it, just includeenum.h
and begin the
73 | [tutorial](${prefix}tutorial/HelloWorld.html)!
74 |
75 |
76 |
77 | ### Highlights
78 |
79 | enums
.
84 | Internal members have underscores to avoid clashing with your constant
85 | names.
86 |
87 | enum.h
. There are no objects or libraries to link with.
94 |
95 | enum.h
. It's a metaprogram executed by your
101 | compiler.
102 |
103 | switch
107 |
108 | Use a Better Enum like a built-in enum
, and still have the
109 | compiler do case checking.
110 |
111 | switch
statements for
117 | converting enums to strings.
118 |
119 | Count
constant and assuming a dense range.
126 |
127 | iostream
. enum.h
is only slightly more than 1000
134 | lines long.
135 |
136 | constexpr
functions.
143 |
144 | std::cout
or use
158 | boost::lexical_cast
.
159 |
160 | 2 | This is an example of code you can write on top of Better Enums. It's a valid 3 | program — you can download it and try it out. The 4 | program is also part of the test suite. 5 |
6 | 7 | $demo_body 8 | 9 | 14 | -------------------------------------------------------------------------------- /doc/template/download.tmpl: -------------------------------------------------------------------------------- 1 | href="https://raw.githubusercontent.com/aantron/better-enums/$ref/enum.h" 2 | download -------------------------------------------------------------------------------- /doc/template/footer.tmpl: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 14 | 15 |