mirror of
https://github.com/aantron/better-enums.git
synced 2025-12-06 16:56:42 +08:00
314 lines
8.7 KiB
HTML
314 lines
8.7 KiB
HTML
<!-- Generated automatically - edit the templates! -->
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
<head>
|
|
|
|
<title>Better Enums - Clean reflective enums for C++</title>
|
|
|
|
<link rel="canonical" href="http://aantron.github.io/better-enums" />
|
|
<meta name="description" content="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." />
|
|
<meta name="author" content="Anton Bachin" />
|
|
|
|
<meta name="viewport" content="width=device-width" />
|
|
|
|
<link rel="stylesheet" href="better-enums.css" />
|
|
|
|
<script>
|
|
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
|
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
|
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
|
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
|
|
|
ga('create', 'UA-62962513-1', 'auto');
|
|
ga('send', 'pageview');
|
|
|
|
</script>
|
|
|
|
</head>
|
|
<body class="index">
|
|
|
|
<nav>
|
|
<div class="container">
|
|
<a class="first" href="https://raw.githubusercontent.com/aantron/better-enums/0.11.3/enum.h"
|
|
download>Download</a>
|
|
<a href="https://github.com/aantron/better-enums">GitHub</a>
|
|
<a href="index.html">Home</a>
|
|
<a href="tutorial/HelloWorld.html">Tutorial</a>
|
|
<a href="ApiReference.html">Reference</a>
|
|
</div>
|
|
</nav>
|
|
|
|
<div class="spacer"> </div>
|
|
|
|
<header>
|
|
<div class="container">
|
|
<section>
|
|
<h1><a href="index.html">Better Enums</a></h1>
|
|
<h2>Reflective compile-time enums for <span class="cpp">C++</span></h2>
|
|
<h3>Open-source under the BSD license</h3>
|
|
</section>
|
|
|
|
<section class="notes">
|
|
<p>Version 0.11.3</p>
|
|
<p>To install, just add <code>enum.h</code> to your project.</p>
|
|
<p>
|
|
Visit the GitHub repo for issues, feedback, and the latest development.
|
|
</p>
|
|
</section>
|
|
|
|
<section class="buttons">
|
|
<a href="https://raw.githubusercontent.com/aantron/better-enums/0.11.3/enum.h"
|
|
download>Download <code>enum.h</code></a>
|
|
<a href="https://github.com/aantron/better-enums">GitHub</a>
|
|
</section>
|
|
</div>
|
|
</header>
|
|
|
|
<div class="main">
|
|
<div class="container">
|
|
|
|
|
|
<div class="splash">
|
|
<pre class="left">enable
|
|
|
|
declare
|
|
|
|
|
|
parse
|
|
format
|
|
|
|
|
|
count
|
|
iterate
|
|
|
|
|
|
|
|
|
|
switch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
safe cast
|
|
|
|
|
|
during
|
|
compilation
|
|
</pre>
|
|
<pre class="right"><em>#include <enum.h></em>
|
|
|
|
<em>BETTER_ENUM(Channel, int, Red = 1, Green, Blue)</em>
|
|
|
|
|
|
Channel c = <em>Channel::_from_string("Red")</em>;
|
|
const char *s = <em>c._to_string()</em>;
|
|
|
|
|
|
size_t n = <em>Channel::_size()</em>;
|
|
<em>for (Channel c : Channel::_values())</em> {
|
|
run_some_function(c);
|
|
}
|
|
|
|
|
|
<em>switch (c)</em> {
|
|
<em>case Channel::Red</em>: // ...
|
|
<em>case Channel::Green</em>: // ...
|
|
<em>case Channel::Blue</em>: // ...
|
|
}
|
|
|
|
|
|
Channel c = <em>Channel::_from_integral(3)</em>;
|
|
|
|
|
|
<em>constexpr</em> Channel c =
|
|
<em>Channel::_from_string("Blue")</em>;</pre>
|
|
</div>
|
|
|
|
<p class="splash-text">
|
|
<span class="self">Better Enums</span> is a single, lightweight header file that makes your compiler generate
|
|
<em>reflective</em> enum types.
|
|
</p>
|
|
|
|
<p>That means you can easily convert enums to and from strings,
|
|
validate them, and loop over them. In <span class="cpp">C++</span><span class="eleven">11</span>, you can do it all at
|
|
compile time.</p>
|
|
<p>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.</p>
|
|
<p>To use it, just include <code>enum.h</code> and begin the
|
|
<a href="tutorial/HelloWorld.html">tutorial</a>!</p>
|
|
<div class="hack"></div>
|
|
|
|
<a id="Highlights"></a><h3>Highlights</h3>
|
|
<ul class="blurbs">
|
|
<li class="zero-mod-two zero-mod-three">
|
|
<strong>Unobtrusive syntax</strong>
|
|
<em>
|
|
No ugly macros. Use initializers as with built-in <code>enums</code>.
|
|
Internal members have underscores to avoid clashing with your constant
|
|
names.
|
|
</em>
|
|
</li>
|
|
|
|
<li class="one-mod-two one-mod-three">
|
|
<strong>No external dependencies</strong>
|
|
<em>
|
|
Uses only standard <span class="cpp">C++</span>. Installation is simple — just download
|
|
<code>enum.h</code>. There are no objects or libraries to link with.
|
|
</em>
|
|
</li>
|
|
|
|
<li class="zero-mod-two two-mod-three">
|
|
<strong>No generator program needed</strong>
|
|
<em>
|
|
Just include <code>enum.h</code>. It's a metaprogram executed by your
|
|
compiler.
|
|
</em>
|
|
</li>
|
|
|
|
<li class="one-mod-two zero-mod-three">
|
|
<strong>Plays nice with <code>switch</code></strong>
|
|
<em>
|
|
Use a Better Enum like a built-in <code>enum</code>, and still have the
|
|
compiler do case checking.
|
|
</em>
|
|
</li>
|
|
|
|
<li class="zero-mod-two one-mod-three">
|
|
<strong>Don't repeat yourself</strong>
|
|
<em>
|
|
No more unmaintanable maps or <code>switch</code> statements for
|
|
converting enums to strings.
|
|
</em>
|
|
</li>
|
|
|
|
<li class="one-mod-two two-mod-three">
|
|
<strong>Non-contiguous sequences</strong>
|
|
<em>
|
|
Iteration and counting are much easier to maintain than with an extra
|
|
<code>Count</code> constant and assuming a dense range.
|
|
</em>
|
|
</li>
|
|
|
|
<li class="zero-mod-two zero-mod-three">
|
|
<strong>Fast compilation</strong>
|
|
<em>
|
|
Much less impact on build time than even just including
|
|
<code>iostream</code>. <code>enum.h</code> is only slightly more than 1000
|
|
lines long.
|
|
</em>
|
|
</li>
|
|
|
|
<li class="one-mod-two one-mod-three">
|
|
<strong>Compile-time reflection</strong>
|
|
<em>
|
|
Have the compiler do additional enum processing using your own
|
|
templates or <code>constexpr</code> functions.
|
|
</em>
|
|
</li>
|
|
|
|
<li class="zero-mod-two two-mod-three">
|
|
<strong>Uniform interface for <span class="cpp">C++</span><span class="eleven">98</span>, <span class="cpp">C++</span><span class="eleven">11</span></strong>
|
|
<em>
|
|
Scoped, sized, reflective enums for <span class="cpp">C++</span><span class="eleven">98</span>, and an easy upgrade
|
|
path.
|
|
</em>
|
|
</li>
|
|
|
|
<li class="one-mod-two zero-mod-three">
|
|
<strong>Stream operators</strong>
|
|
<em>
|
|
Write enum names directly to <code>std::cout</code> or use
|
|
<code>boost::lexical_cast</code>.
|
|
</em>
|
|
</li>
|
|
|
|
<li class="zero-mod-two one-mod-three">
|
|
<strong>Free and open source</strong>
|
|
<em>
|
|
Released under the BSD license for use in any project, free or commercial.
|
|
</em>
|
|
</li>
|
|
</ul>
|
|
|
|
<div class="hack"></div>
|
|
|
|
<a id="Documentation"></a><h3>Documentation</h3>
|
|
<ul class="blurbs resources">
|
|
<li class="zero-mod-two zero-mod-three">
|
|
<a id="Tutorial"></a>
|
|
<strong>Tutorial</strong>
|
|
<ol>
|
|
<li><a href="tutorial/HelloWorld.html">Hello, World!</a></li>
|
|
<li><a href="tutorial/Conversions.html">Conversions</a></li>
|
|
<li><a href="tutorial/Iteration.html">Iteration</a></li>
|
|
<li><a href="tutorial/SafeSwitch.html">Safe switch</a></li>
|
|
<li><a href="tutorial/Maps.html">Maps</a></li>
|
|
<li><a href="tutorial/StreamOperators.html">Stream operators</a></li>
|
|
<li><a href="tutorial/ScopeAndSafety.html">Scope and safety</a></li>
|
|
<li><a href="tutorial/Representation.html">Representation</a></li>
|
|
<li><a href="tutorial/CompileTimeUsage.html">Compile-time usage</a></li>
|
|
|
|
</ol>
|
|
</li>
|
|
|
|
<li class="one-mod-two one-mod-three">
|
|
<strong>Reference</strong>
|
|
<ul>
|
|
<li><a href="ApiReference.html">API reference</a></li>
|
|
<li><a href="CompilerSupport.html">Compiler support</a></li>
|
|
<li><a href="OptInFeatures.html">Opt-in features</a></li>
|
|
<li><a href="ExtendingLimits.html">Extending limits</a></li>
|
|
<li><a href="Performance.html">Performance</a></li>
|
|
<li>
|
|
<a href="DesignDecisionsFAQ.html">Design decisions FAQ</a>
|
|
</li>
|
|
<li>
|
|
<a href="http://www.codeproject.com/Articles/1002895/Clean-Reflective-Enums-Enum-to-String-with-Nice-Sy">
|
|
Implementation <span class="external">[CodeProject]</span>
|
|
</a>
|
|
</li>
|
|
</ul>
|
|
</li>
|
|
|
|
<li class="zero-mod-two two-mod-three">
|
|
<a id="CompileTimeDemos"></a>
|
|
<strong>Advanced</strong>
|
|
<ul>
|
|
<li><a href="demo/SpecialValues.html">Special values</a></li>
|
|
<li><a href="demo/BitSets.html">Bit sets</a></li>
|
|
<li><a href="demo/SemiQuine.html">Semi-quine</a></li>
|
|
<li><a href="demo/C++17ReflectionProposal.html">C++17 reflection proposal</a></li>
|
|
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
|
|
<div class="hack"></div>
|
|
|
|
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<footer>
|
|
<div class="container">
|
|
Copyright © 2015-2019 Anton Bachin. Released under the BSD 2-clause
|
|
license. See
|
|
<a href="https://github.com/aantron/better-enums/blob/0.11.3/doc/LICENSE">
|
|
LICENSE</a>.
|
|
<br />
|
|
This page is part of the documentation for Better Enums 0.11.3.
|
|
</div>
|
|
</footer>
|
|
|
|
</body>
|
|
</html>
|
|
|