Last active
December 12, 2015 06:38
-
-
Save yohhoy/4730420 to your computer and use it in GitHub Desktop.
snippet for Bitmask type(17.5.2.1.3 [bitmask.types])
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <type_traits> | |
// 17.5.2.1.3 Bitmask types | |
enum class BitmaskType : unsigned int { | |
Field1 = 0x00000001, | |
Field2 = 0x00000002, | |
Field3 = 0x00000004, | |
Field4 = 0x00000008, | |
Field5 = 0x00000010, | |
//... | |
}; | |
constexpr BitmaskType operator&(BitmaskType x, BitmaskType y) | |
{ | |
typedef typename std::underlying_type<BitmaskType>::type UT; | |
return static_cast<BitmaskType>(static_cast<UT>(x) & static_cast<UT>(y)); | |
} | |
constexpr BitmaskType operator|(BitmaskType x, BitmaskType y) | |
{ | |
typedef typename std::underlying_type<BitmaskType>::type UT; | |
return static_cast<BitmaskType>(static_cast<UT>(x) | static_cast<UT>(y)); | |
} | |
constexpr BitmaskType operator^(BitmaskType x, BitmaskType y) | |
{ | |
typedef typename std::underlying_type<BitmaskType>::type UT; | |
return static_cast<BitmaskType>(static_cast<UT>(x) ^ static_cast<UT>(y)); | |
} | |
constexpr BitmaskType operator~(BitmaskType x) | |
{ | |
typedef typename std::underlying_type<BitmaskType>::type UT; | |
return static_cast<BitmaskType>(~static_cast<UT>(x)); | |
} | |
BitmaskType & operator&=(BitmaskType & x, BitmaskType y) | |
{ | |
x = x & y; | |
return x; | |
} | |
BitmaskType & operator|=(BitmaskType & x, BitmaskType y) | |
{ | |
x = x | y; | |
return x; | |
} | |
BitmaskType & operator^=(BitmaskType & x, BitmaskType y) | |
{ | |
x = x ^ y; | |
return x; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment