Skip to content

flag_set is a type-safe class for using enums as flags in C++

License

Notifications You must be signed in to change notification settings

mrts/flag-set-cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

flag-set-cpp

flag_set is a type-safe class for using enums as flags in C++14 with an underlying std::bitset.

The original idea is by Arnaud Kapp (Xaqq) with additions from Barry Revzin.

Usage

flag_set is a header-only library, simply copy include/flag_set.hpp to your project to use it.

The enum type of the flag_set must have a last value sentinel _, a single underscore character. Underscore was chosen as it stands out clearly and is unlikely to collide with a real enum value.

Usage example:

#include "flag_set.hpp"

enum class Options : uint8_t {
    FULLSCREEN,
    INVERT_MOUSE,
    FLASH,
    RED_BACKGROUND,
    RED_FOREGROUND,
    _
};

int main()
{
    flag_set<Options> red(Options::RED_FOREGROUND | Options::RED_BACKGROUND);

    if (red[Options::RED_BACKGROUND]) // or red & Options::RED_BACKGROUND
        cout << "Red background activated";
}

See more examples in tests.

Contributing

Run clang-format on patches as follows:

find include/ tests/ -iname '*.hpp' -o -iname '*.h' -o -iname '*.cpp' | xargs clang-format -i

About

flag_set is a type-safe class for using enums as flags in C++

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published