mirror of
https://github.com/aantron/better-enums.git
synced 2025-12-06 16:56:42 +08:00
Minimally updated documentation.
This commit is contained in:
parent
6c60edcc46
commit
10c9977a9c
39
README.md
39
README.md
@ -25,30 +25,18 @@ for (Channel channel : Channel::_values) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
...and more. See the [project page](http://aantron.github.io/better-enums) and
|
...and more. See the
|
||||||
|
[project page](http://aantron.github.io/better-enums#tutorial) and
|
||||||
[examples](https://github.com/aantron/better-enums/tree/master/example) for a
|
[examples](https://github.com/aantron/better-enums/tree/master/example) for a
|
||||||
tutorial.
|
tutorial.
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
Simply add `enum.h` and `enum_preprocessor_map.h` to your project. The current
|
Simply add `enum.h` from `master` to your project.
|
||||||
version can be found at
|
|
||||||
[https://github.com/aantron/better-enums/releases](https://github.com/aantron/better-enums/releases).
|
|
||||||
|
|
||||||
`enum_preprocessor_map.h` can handle enums with up to 256 constants. If you have
|
|
||||||
more, re-generate it by running something like:
|
|
||||||
|
|
||||||
./pp_map_gen.py enum_preprocessor_map.h 512
|
|
||||||
|
|
||||||
This only needs to be done once when the constant limit is exceeded. You don't
|
|
||||||
need to do this on every build. I hope to remove the need for this completely in
|
|
||||||
a future version.
|
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- Generated at compile time by `constexpr` functions and the preprocessor.
|
- Generated at compile time by `constexpr` functions and the preprocessor.
|
||||||
`pp_map_gen.py` is only needed if you have an enum with more than 256
|
|
||||||
constants.
|
|
||||||
- Safe conversions between enums and integers and strings.
|
- Safe conversions between enums and integers and strings.
|
||||||
[1-basic.cc](https://github.com/aantron/better-enums/blob/master/example/1-basic.cc)
|
[1-basic.cc](https://github.com/aantron/better-enums/blob/master/example/1-basic.cc)
|
||||||
- Iterable collections of constants and names.
|
- Iterable collections of constants and names.
|
||||||
@ -58,22 +46,21 @@ a future version.
|
|||||||
[2-iterate.cc](https://github.com/aantron/better-enums/blob/master/example/2-iterate.cc)
|
[2-iterate.cc](https://github.com/aantron/better-enums/blob/master/example/2-iterate.cc)
|
||||||
- Switch case checking.
|
- Switch case checking.
|
||||||
[3-switch.cc](https://github.com/aantron/better-enums/blob/master/example/3-switch.cc)
|
[3-switch.cc](https://github.com/aantron/better-enums/blob/master/example/3-switch.cc)
|
||||||
- Almost all operations are `constexpr` and can be used at compile time in your
|
- All operations are `constexpr` and can be used at compile time in your own
|
||||||
own `constexpr` code.
|
`constexpr` code.
|
||||||
[4-constexpr.cc](https://github.com/aantron/better-enums/blob/master/example/4-constexpr.cc)
|
[4-constexpr.cc](https://github.com/aantron/better-enums/blob/master/example/4-constexpr.cc)
|
||||||
- Constant values can be set (`Red = 1`) and aliased (`Favorite = Green`), just
|
- Constant values can be set (`Red = 1`) and aliased (`Favorite = Green`), just
|
||||||
like with built-in enums.
|
like with built-in enums.
|
||||||
- Generating a large number of enums is faster than including a typical standard
|
- Generating a large number of enums is about as fast as including a typical
|
||||||
header like `iostream` – performance test included.
|
standard header like `iostream` – performance test included.
|
||||||
- Explicit choice of underlying representation type.
|
- Explicit choice of underlying representation type.
|
||||||
- Header-only.
|
- Header-only.
|
||||||
- No dependencies besides the standard library.
|
- No dependencies besides the standard library.
|
||||||
- Tested on gcc 4.9 and clang 3.6.
|
- Tested on gcc 4.9 and clang 3.6.
|
||||||
|
|
||||||
The library compiles only with gcc and clang due to use of weak symbols and due
|
The library is standard C++, but it compiles only with gcc and clang due to
|
||||||
to lagging C++11 support in msvc. It should ultimately be portable to msvc,
|
lagging C++11 support in msvc. A future runtime fallback version will allow
|
||||||
since msvc has its own version of weak symbols. Everything else in the library
|
msvc and non-C++11 usage.
|
||||||
is standard C++.
|
|
||||||
|
|
||||||
## Contact
|
## Contact
|
||||||
|
|
||||||
@ -98,12 +85,6 @@ See the full [documentation](http://aantron.github.io/better-enums).
|
|||||||
|
|
||||||
There are several areas that still need improvement.
|
There are several areas that still need improvement.
|
||||||
|
|
||||||
- I will try to eliminate the need for `pp_map_gen.py`. The library will then
|
|
||||||
consist of one file, `enum.h`.
|
|
||||||
- `to_string` can be made `constexpr`, like most of the rest of the code.
|
|
||||||
- Iterators over defined constants should be made random-access. This would
|
|
||||||
allow `constexpr` functions to use them by adding 1 – the only way to advance
|
|
||||||
them now is by mutating increment.
|
|
||||||
- Some enum types might have a sensible choice for a default constructor. The
|
- Some enum types might have a sensible choice for a default constructor. The
|
||||||
library should allow it to be customized.
|
library should allow it to be customized.
|
||||||
- All safety checks are currently done by linear scans. This may be a
|
- All safety checks are currently done by linear scans. This may be a
|
||||||
|
|||||||
15
doc/api.html
15
doc/api.html
@ -637,17 +637,16 @@ do_something(Enum::A); // converted silently</pre>
|
|||||||
|
|
||||||
<a id="to_string"></a>
|
<a id="to_string"></a>
|
||||||
<h3>
|
<h3>
|
||||||
<span>member</span> const char* enum_value.to_string()
|
<span>member constexpr</span> const char* enum_value.to_string()
|
||||||
<span>const</span>
|
<span>const</span>
|
||||||
</h3>
|
</h3>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
Returns the string representation of enum value on which the method is
|
Returns the string representation of enum value on which the method is
|
||||||
called. If multiple constants have the same numeric value, the string
|
called. If multiple constants have the same numeric value, the string
|
||||||
returned can be the representation any of the constants. Note that this
|
returned can be the representation any of the constants. Complexity is
|
||||||
method is <em>not</em> <tt>constexpr</tt>. Complexity is <em>linear</em> in
|
<em>linear</em> in the number of constants. If the string does not name a
|
||||||
the number of constants. If the string does not name a constant,
|
constant, <em>throws</em> <tt>std::runtime_error</tt>.
|
||||||
<em>throws</em> <tt>std::runtime_error</tt>.
|
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<a id="name"></a>
|
<a id="name"></a>
|
||||||
@ -728,9 +727,9 @@ Enum::_values::end()</pre>
|
|||||||
<h3><span>class</span> _NameIterable::iterator</h3>
|
<h3><span>class</span> _NameIterable::iterator</h3>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
An iterator over defined constant names with a dereference operator that is
|
An iterator over defined constant names with a <tt>constexpr</tt>
|
||||||
<em>not</em> <tt>constexpr</tt>. Can be created explicitly by the
|
dereference operator. Can be created explicitly by the <tt>constexpr</tt>
|
||||||
<tt>constexpr</tt> expressions
|
expressions
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>Enum::_names::begin()
|
<pre>Enum::_names::begin()
|
||||||
|
|||||||
@ -59,8 +59,8 @@
|
|||||||
|
|
||||||
<p>
|
<p>
|
||||||
Better Enums gives you rich, reflective enums with the nicest syntax yet
|
Better Enums gives you rich, reflective enums with the nicest syntax yet
|
||||||
seen.<sup><a href="#note">1</a></sup> All you have to do is add two short,
|
seen.<sup><a href="#note">1</a></sup> All you have to do is add one short
|
||||||
simple header files to your project, and you are ready to go.
|
and simple header file to your project, and you are ready to go.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>#include <enum.h>
|
<pre>#include <enum.h>
|
||||||
@ -71,15 +71,16 @@ ENUM(Channel, int, Red, Green, Blue);</pre>
|
|||||||
reflective capacity, including string conversions, value listing,
|
reflective capacity, including string conversions, value listing,
|
||||||
compile-time operations, static information about the range of declared
|
compile-time operations, static information about the range of declared
|
||||||
constants, and (last and probably least) the name <code>"Channel"</code>
|
constants, and (last and probably least) the name <code>"Channel"</code>
|
||||||
itself.
|
itself. You can even assign explicit values to constants and alias them
|
||||||
|
like with a normal built-in <code>enum</code>.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
The enum is easy to maintain. There is no duplication of value names,
|
The enum is easy to maintain. There is no duplication of value names,
|
||||||
no repetition of cumbersome macros, and no generator to run on every
|
no repetition of cumbersome macros, and no generator to run on every
|
||||||
build. The library is header-only and has no dependencies, so there aren't
|
build. The library is header-only and has no dependencies, so there aren't
|
||||||
any object files to link with. You can assign explicit values to constants
|
any object files to link with. It is also standard
|
||||||
and alias them like with a normal <code>enum</code>.
|
<span class="cpp">C++</span>.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
@ -95,13 +96,11 @@ ENUM(Channel, int, Red, Green, Blue);</pre>
|
|||||||
<h2>Installation</h2>
|
<h2>Installation</h2>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
Download the
|
Download
|
||||||
<a href="https://github.com/aantron/better-enums/releases/latest">
|
<a href="https://github.com/aantron/better-enums/blob/master/enum.h">
|
||||||
latest release</a>, then copy the files
|
enum.h</a> and copy it to your project. That's it! Just make sure it's in
|
||||||
<code>enum.h</code> and <code>enum_preprocessor_map.h</code> to your
|
your include path and you are compiling with <span class="cc">gcc</span>
|
||||||
project. That's it! Just make sure both files are in your include path and
|
or <span class="cc">clang</span> in
|
||||||
you are compiling with <span class="cc">gcc</span> or
|
|
||||||
<span class="cc">clang</span> in
|
|
||||||
<span class="cpp">C++</span><span class="eleven">11</span> mode.
|
<span class="cpp">C++</span><span class="eleven">11</span> mode.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
@ -212,13 +211,13 @@ Channel my_channel = Channel::_from_integral(2);</pre>
|
|||||||
<hr />
|
<hr />
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
Finally, most of the above is declared <code>constexpr</code> and can be
|
Finally, all of the above can be done at compile time. This means you can
|
||||||
run at compile time. This means you can do all sorts of parsing and
|
do all sorts of parsing and processing at the same time the rest of your
|
||||||
processing at the same time the rest of your code is being compiled,
|
code is being compiled, improving runtime and startup performance! See
|
||||||
improving runtime and startup performance! See some
|
some
|
||||||
<a href="https://github.com/aantron/better-enums/blob/master/example/4-constexpr.cc">examples</a>
|
<a href="https://github.com/aantron/better-enums/blob/master/example/4-constexpr.cc">examples</a>
|
||||||
and another
|
<a href="https://github.com/aantron/better-enums/blob/master/example/6-traits.cc">2</a>
|
||||||
<a href="https://github.com/aantron/better-enums/blob/master/example/6-traits.cc">example</a>.
|
<a href="https://github.com/aantron/better-enums/blob/master/example/8-constexpr-iterate.cc">3</a>.
|
||||||
</p>
|
</p>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
|||||||
@ -2,11 +2,9 @@
|
|||||||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||||
<url>
|
<url>
|
||||||
<loc>http://aantron.github.io/better-enums</loc>
|
<loc>http://aantron.github.io/better-enums</loc>
|
||||||
<changefreq>daily</changefreq>
|
|
||||||
<priority>1.0</priority>
|
<priority>1.0</priority>
|
||||||
</url>
|
</url>
|
||||||
<url>
|
<url>
|
||||||
<loc>http://aantron.github.io/better-enums/api.html</loc>
|
<loc>http://aantron.github.io/better-enums/api.html</loc>
|
||||||
<changefreq>daily</changefreq>
|
|
||||||
</url>
|
</url>
|
||||||
</urlset>
|
</urlset>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user