Coder Social home page Coder Social logo

jslee02 / better-enums Goto Github PK

View Code? Open in Web Editor NEW

This project forked from aantron/better-enums

0.0 3.0 0.0 550 KB

Compile-time enum to string, iteration, in a single header file.

Home Page: http://aantron.github.io/better-enums

C++ 86.77% Makefile 3.90% Python 4.53% CMake 4.81%

better-enums's Introduction

Better Enums   version 0.11.1 Try online Travis status AppVeyor status BSD license

Reflective compile-time enum library with clean syntax, in a single header file, and without dependencies.

Better Enums code overview

In C++11, everything can be used at compile time. You can convert your enums, loop over them, find their max, statically enforce conventions, and pass along the results as template arguments or to constexpr functions. All the reflection is available for your metaprogramming needs.

The interface is the same for C++98 — you just have to use most of it at run time only. This library does provide scoped and sized enums, something not built into C++98.

See the project page for full documentation.

Installation

Simply add enum.h to your project.

Additional features

  • Uses only standard C++, though, for C++98, variadic macro support is required (major compilers have it).
  • Supported and tested on clang, gcc, and msvc.
  • Fast compilation. You have to declare a few dozen enums to slow down your compiler as much as only including iostream does.
  • Use any initializers and sparse ranges, just like with a built-in enum.
  • Control over size and alignment — you choose the representation type.
  • Stream operators.
  • Does not use the heap and can be compiled with exceptions disabled, for use in minimal freestanding environments.

Limitations

The biggest limitation is that the BETTER_ENUM macro can't be used inside a class. This seems difficult to remove. There is a workaround with typedef (or C++11 using):

BETTER_ENUM(SomePrefix_Color, uint8_t, Red, Green, Blue)

struct triplet {
    typedef SomePrefix_Color    Color;
    Color                       r, g, b;
};

triplet::Color  color;

You can, however, use BETTER_ENUM inside a namespace.

The macro has a soft limit of 64 declared constants. You can extend it by following these instructions. Ultimately, the number of constants is limited by your compiler's maximum macro argument count.

In some cases, it is necessary to prefix constants such as Channel::Red with a + to explicitly promote them to type Channel. For example, if you are doing a comparison:

channel == +Channel::Red

Contact and development

Don't hesitate to contact me about features or bugs: [email protected], or open an issue on GitHub.

If you'd like to help develop Better Enums, see CONTRIBUTING. One area that could use fresh ideas is finding a compile-time data structure that both compiles quickly and allows lookup in sub-linear time.

License and history

Better Enums is released under the BSD 2-clause license. See LICENSE.

The original version of the library was developed by the author in the winter of 2012-2013 at Hudson River Trading, as a replacement for an older generator called BETTER_ENUM.

better-enums's People

Contributors

aantron avatar benalexau avatar unrealquester avatar zafirus avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.