-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d18e92e
commit f3f2f27
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#pragma once | ||
|
||
// | ||
// Define operators to enable combining enum class values as flags. | ||
// | ||
// To enable EnumFlags features use: | ||
// | ||
// namespace Foo { | ||
// ... | ||
// ENABLE_BITMASK_OPERATORS(FilesystemAttributes); | ||
// } | ||
// | ||
// From http://blog.bitwigglers.org/using-enum-classes-as-type-safe-bitmasks/ | ||
// | ||
|
||
#include <type_traits> | ||
|
||
namespace Orc { | ||
|
||
template <typename Enum> | ||
struct EnableBitMaskOperators | ||
{ | ||
static const bool enable = false; | ||
}; | ||
|
||
template <typename Enum> | ||
typename std::enable_if<EnableBitMaskOperators<Enum>::enable, bool>::type constexpr operator&(Enum lhs, Enum rhs) | ||
{ | ||
using underlying = typename std::underlying_type<Enum>::type; | ||
return static_cast<underlying>(lhs) & static_cast<underlying>(rhs); | ||
} | ||
|
||
template <typename Enum> | ||
typename std::enable_if<EnableBitMaskOperators<Enum>::enable, bool>::type constexpr operator&=(Enum& lhs, Enum rhs) | ||
{ | ||
using underlying = typename std::underlying_type<Enum>::type; | ||
lhs = static_cast<underlying>(lhs) & static_cast<underlying>(rhs); | ||
return lhs; | ||
} | ||
|
||
template <typename Enum> | ||
typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type constexpr operator|(Enum lhs, Enum rhs) | ||
{ | ||
using underlying = typename std::underlying_type<Enum>::type; | ||
return static_cast<Enum>(static_cast<underlying>(lhs) | static_cast<underlying>(rhs)); | ||
} | ||
|
||
template <typename Enum> | ||
typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type constexpr operator|=(Enum& lhs, Enum rhs) | ||
{ | ||
using underlying = typename std::underlying_type<Enum>::type; | ||
lhs = static_cast<Enum>(static_cast<underlying>(lhs) | static_cast<underlying>(rhs)); | ||
return lhs; | ||
} | ||
|
||
template <typename Enum> | ||
typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type constexpr operator^(Enum lhs, Enum rhs) | ||
{ | ||
using underlying = typename std::underlying_type<Enum>::type; | ||
return static_cast<Enum>(static_cast<underlying>(lhs) ^ static_cast<underlying>(rhs)); | ||
} | ||
|
||
template <typename Enum> | ||
typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type constexpr operator^=(Enum& lhs, Enum rhs) | ||
{ | ||
using underlying = typename std::underlying_type<Enum>::type; | ||
lhs = static_cast<Enum>(static_cast<underlying>(lhs) ^ static_cast<underlying>(rhs)); | ||
return lhs; | ||
} | ||
|
||
template <typename Enum> | ||
typename std::enable_if<EnableBitMaskOperators<Enum>::enable, Enum>::type constexpr operator~(Enum value) | ||
{ | ||
using underlying = typename std::underlying_type<Enum>::type; | ||
return static_cast<Enum>(static_cast<underlying>(value)); | ||
} | ||
|
||
#define ENABLE_BITMASK_OPERATORS(x) \ | ||
template <> \ | ||
struct EnableBitMaskOperators<x> \ | ||
{ \ | ||
static const bool enable = true; \ | ||
}; | ||
|
||
} // namespace Orc |