GithubHelp home page GithubHelp logo

compmaniak / typed_flags Goto Github PK

View Code? Open in Web Editor NEW
23.0 5.0 1.0 127 KB

Type-safe and human-readable set of bool flags

License: MIT License

CMake 4.31% C++ 95.69%
type-safety syntax-sugar templates

typed_flags's Introduction

Typed flags

Build Status Build status Try online

Type-safe and human-readable sets of bool flags.

Quick start

Start by declaring some types

class eats_meat;
class eats_grass;
class has_tail;

Then bind these types to flag identifiers

typedef typed_flags<eats_meat, eats_grass, has_tail> animal;

Create flags from scratch

animal wolf;
wolf.set<eats_grass>(false);
wolf.set<eats_meat, has_tail>();
wolf.set(flag<has_tail>{1}, flag<eats_meat>{1});

Create flags with a flexible human-readable constructor

wolf = animal{flag<has_tail>{1}, flag<eats_meat>{1}, flag<eats_grass>{0}};

Test each flag separately

assert( (wolf.test<eats_meat>() && wolf.test<has_tail>()) );

Test a group of flags in one call

assert( (wolf.all<eats_meat, has_tail>()) );
assert( (wolf.any<eats_meat, has_tail, eats_grass>()) );
assert( (wolf.none<eats_grass>()) );

Extract flag values

flag<eats_meat> f1;
flag<has_tail> f2;
wolf.get(f1, f2);
assert( f1 && f2 );

Create flags from integers or strings and convert back and forth - like std::bitset

auto a1 = animal{3};
auto a2 = animal{"101"};
assert( a1.to_integral<int>() == 3 );
assert( a2.to_string() == "101" );

Documentation

You can find more detailed info here.

Requirements

To build tests and examples you need

  • cmake
  • compiler supporting fold-expressions from C++17 (GCC 6, Clang 3.8+)

typed_flags's People

Contributors

compmaniak avatar rec avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

rec

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.