Giter Site home page Giter Site logo

alphafork / articleenumclass-v2 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dalzhim/articleenumclass-v2

0.0 1.0 0.0 14 KB

Home Page: https://dalzhim.github.io/2017/08/11/Improving-the-enum-class-bitmask/

License: BSD 2-Clause "Simplified" License

C++ 99.12% CMake 0.88%

articleenumclass-v2's Introduction

Bitmask operators and typesafe comparisons for enum class

EnumClass.h is a utility header that allows you to easily generate bitwise operators for your custom enum class type like so:

enum class myEnum
{
	enumerator1 = 0x1 << 0,
	enumerator2 = 0x1 << 1,
	enumerator3 = 0x1 << 2
};

enableEnumClassBitmask(myEnum); // Activate bitmask operators

This utility relies on two concepts: enumerators and masks. An enumerator’s purpose is to give a name to a specific bit when it is set. A mask, on the other hand, represents the state of every bit (and this way, of every enumerator), whether they are set or cleared. Comparing an enumerator to a mask using operator== or operator!= is a compiler error. A complete blog post explains why and how this is implemented.

Here are some tables that summarize the return type of all of the operators :

Binary bitwise operators

E, E E, bitmask<E> bitmask<E>, E bitmask<E>, bitmask<E>
operator& E E E
operator| bitmask<E> bitmask<E> bitmask<E>
operator^ bitmask<E> bitmask<E> bitmask<E>
operator&= bitmask<E> bitmask<E> bitmask<E>
operator|= bitmask<E> bitmask<E> bitmask<E>
operator^= bitmask<E> bitmask<E> bitmask<E>

Unary bitwise operators

E bitmask<E>
operator~ bitmask<E>

Comparison operators

E, E E, bitmask<E> bitmask<E>, E bitmask<E>, bitmask<E>
operator== bool static_assert static_assert
operator!= bool static_assert static_assert

articleenumclass-v2's People

Contributors

dalzhim avatar

Watchers

 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.