https://github.com/jasujm/enhanced-enum
First class enums in C++
https://github.com/jasujm/enhanced-enum
code-generation code-generator cpp cpp17 cpp20 cxx cxx17 cxx20 enum
Last synced: 4 months ago
JSON representation
First class enums in C++
- Host: GitHub
- URL: https://github.com/jasujm/enhanced-enum
- Owner: jasujm
- License: mit
- Created: 2019-11-30T18:41:17.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T13:43:49.000Z (over 3 years ago)
- Last Synced: 2025-12-05T22:40:43.909Z (7 months ago)
- Topics: code-generation, code-generator, cpp, cpp17, cpp20, cxx, cxx17, cxx20, enum
- Language: Python
- Homepage: https://enhanced-enum.readthedocs.io/
- Size: 319 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.rst
- Changelog: CHANGELOG.rst
- License: LICENSE
Awesome Lists containing this project
README
Enhanced Enum is a library that gives C++ enums capabilities that they don't
normally have:
.. code-block:: c++
enum class StatusLabel {
INITIALIZING,
WAITING_FOR_INPUT,
BUSY,
};
constexpr auto status = Statuses::INITIALIZING;
Their value is no longer restricted to integers:
.. code-block:: c++
static_assert( status.value() == "initializing" );
static_assert( status == Status::from("initializing") );
They can be iterated:
.. code-block:: c++
std::cout << "Listing " << Statuses::size() << " enumerators:\n";
for (const auto status : Statuses::all()) {
std::cout << status.value() << "\n";
}
...all while taking remaining largely compatible with the fundamental enums:
.. code-block:: c++
static_assert( sizeof(status) == sizeof(StatusLabel) );
static_assert( status == StatusLabel::INITIALIZING );
static_assert( status != StatusLabel::WAITING_FOR_INPUT );
Why yet another enum library for C++?
-------------------------------------
There are plethora of options available for application writers that
want similar capabilities than this library provides. Why write
another instead of picking one of them?
Short answer: Because it solved a problem for me, and I hope it will
solve similar problems for other people
Longer answer: There is a fundamental limitations to the capabilities
of native enums within the standard C++, and in order to cope with
them, enum library writers must choose from more or less
unsatisfactory options:
- Resort to compiler implementation details. While this is a
non-intrusive way to introduce reflection, it's not what I'm after.
- Use macros. By far the most common approach across the ecosystem is
to use preprocessor macros to generate the type definitions. To me
macros are just another form of code generation. The advantage is
that this approach needs standard C++ compiler only. The drawback is
the inflexibility of macro expansions.
Enhanced Enum utilizes a proper code generator to create the necessary
boilerplate for enum types. The generator is written in Python, and
unlocks all the power and nice syntax that Python provides. The
generated code is clean and IDE friendly. This approach enables the
enums created using the library to have arbitrary values, not just
strings derived from the enumerator names. The drawback is the need to
include another library in the build toolchain.
Getting started
---------------
The C++ library is header only. Just copy the contents of the
``cxx/include/`` directory in the repository to your include path. If
you prefer, you can create and install the CMake targets for the
library:
.. code-block:: console
$ cmake /path/to/repository
$ make && make install
In your project:
.. code-block:: cmake
find_package(EnhancedEnum)
target_link_libraries(my-target EnhancedEnum::EnhancedEnum)
The enum definitions are created with the EnumECG library written in
Python. It can be installed using ``pip``:
.. code-block:: console
$ pip install EnumECG
The library and code generation API are documented in the user guide
hosted at `Read the Docs `_.
Contact
-------
The author of the library is Jaakko Moisio. For feedback and
suggestions, please contact jaakko@moisio.fi.