Coder Social home page Coder Social logo

fwk_enum's Introduction

fwk::enum ?

It's a header-only C++17 library with an improved enum class. Features:

  • ability to iterate over, enumerate in a range-for and count all elements
  • type-safe bitwise operations support without any additional code
  • conversion to / from strings (const char*)
  • no weirdness, you can use it just like you would use enum class
  • VERY lightweight: less than 300 lines of code in a single header file
  • decent compilation speed and low runtime overhead
  • enums safe to use in switches (assuming decent compiler)
  • no dependencies

For more goodness (including EnumMap) have a look at libfwk:
https://github.com/nadult/libfwk

Requirements

  • compiler with C++17 support (Clang 4 or GCC 7.1)

Limitations

  • you cannot specify custom values for each enum
  • you cannot define enums within class or function scope
  • number of enum elements is limited to 64

Examples


FWK_ENUM(MyEnum, item_one, item_two, item_three, item_four);

fromString<MyEnum>("item_two") == MyEnum::item_two
fromString<MyEnum>("not an item").valid() == false
string("item_three") == toString(MyEnum::item_three)

string text;
for(auto elem : all<MyEnum>())
	text = text + toString(elem) + " ";

// Cyclic iteration over enum values
next(MyEnum::item_four) == MyEnum::item_one
prev(MyEnum::item_three) == MyEnum::item_two

// It's better to use EnumMap from libfwk instead: it's just as fast
// and it provides some compile and run time checks
int values[count<MyOtherEnum>()] = {1, 2, 3, 4, 5};

for(auto elem : all<MyOtherEnum>())
	cout << elem << ": " << values[(int)elem] << endl;

// Flag operations work out of the box:
flags1 = MyEnum::item_one | MyEnum::item_two | MyEnum::item_three;
flags1 == ~MyEnum::item_four
flags1.bits == 1 + 2 + 4
flag(MyEnum::item_four).bits == 8

// MyFlags uses unsigned char as underlying type because MyEnum has only 4 elements
using MyFlags = EnumFlags<MyEnum>;

MyFlags flags = flags1 ^ MyEnum::item_one;

License

fwk::enum is distributed under the terms of Boost Software License (version 1.0).
See license.txt for details.

fwk_enum's People

Contributors

nadult avatar

Stargazers

Sébastien avatar Beaf avatar Csoba István avatar Marco Craveiro avatar  avatar ttldtor avatar Kamal Govindraj avatar Teppei Fujisawa avatar

Watchers

James Cloos avatar  avatar

Forkers

newlifer

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.