diff options
Diffstat (limited to 'src/misc/bitwise.hpp')
-rw-r--r-- | src/misc/bitwise.hpp | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/misc/bitwise.hpp b/src/misc/bitwise.hpp new file mode 100644 index 0000000..ecce333 --- /dev/null +++ b/src/misc/bitwise.hpp @@ -0,0 +1,38 @@ +#pragma once +#include <type_traits> + +template<typename T> +struct Bitwise { + static const bool enable = false; +}; + +template<typename T> +typename std::enable_if<Bitwise<T>::enable, T>::type operator|(T a, T b) { + using x = typename std::underlying_type<T>::type; + return static_cast<T>(static_cast<x>(a) | static_cast<x>(b)); +} + +template<typename T> +typename std::enable_if<Bitwise<T>::enable, T>::type operator|=(T &a, T b) { + using x = typename std::underlying_type<T>::type; + a = static_cast<T>(static_cast<x>(a) | static_cast<x>(b)); + return a; +} + +template<typename T> +typename std::enable_if<Bitwise<T>::enable, T>::type operator&(T a, T b) { + using x = typename std::underlying_type<T>::type; + return static_cast<T>(static_cast<x>(a) & static_cast<x>(b)); +} + +template<typename T> +typename std::enable_if<Bitwise<T>::enable, T>::type operator&=(T &a, T b) { + using x = typename std::underlying_type<T>::type; + a = static_cast<T>(static_cast<x>(a) & static_cast<x>(b)); + return a; +} + +template<typename T> +typename std::enable_if<Bitwise<T>::enable, T>::type operator~(T a) { + return static_cast<T>(~static_cast<typename std::underlying_type<T>::type>(a)); +} |