source: icGREP/icgrep-devel/icgrep/util/enum_flags.hpp @ 6231

Last change on this file since 6231 was 6231, checked in by cameron, 5 months ago

Fix for clang/Mac portability

File size: 3.0 KB
Line 
1#ifndef ENUM_FLAGS_HPP
2#define ENUM_FLAGS_HPP
3
4#include <type_traits>
5namespace {
6template<typename EnumType>
7struct __EnumFlagSet {
8    static const bool enable = false;
9};
10}
11
12#define ENABLE_ENUM_FLAGS(EnumType) \
13namespace { \
14template<> \
15struct __EnumFlagSet<EnumType> { \
16    static const bool enable = true; \
17}; \
18}
19
20template<typename EnumType>
21constexpr typename std::enable_if<__EnumFlagSet<EnumType>::enable, EnumType>::type
22operator|(EnumType lhs, EnumType rhs) {
23    using IntType = typename std::underlying_type<EnumType>::type;
24    return static_cast<EnumType>(static_cast<IntType>(lhs) | static_cast<IntType>(rhs));
25}
26
27template<typename EnumType>
28constexpr typename std::enable_if<__EnumFlagSet<EnumType>::enable, EnumType>::type
29operator&(EnumType lhs, EnumType rhs) {
30    using IntType = typename std::underlying_type<EnumType>::type;
31    return static_cast<EnumType>(static_cast<IntType>(lhs) & static_cast<IntType>(rhs));
32}
33
34template<typename EnumType>
35constexpr typename std::enable_if<__EnumFlagSet<EnumType>::enable, EnumType>::type
36operator^(EnumType lhs, EnumType rhs) {
37    using IntType = typename std::underlying_type<EnumType>::type;
38    return static_cast<EnumType>(static_cast<IntType>(lhs) ^ static_cast<IntType>(rhs));
39}
40
41template<typename EnumType>
42constexpr typename std::enable_if<__EnumFlagSet<EnumType>::enable, EnumType>::type
43operator~(EnumType lhs){
44    using IntType = typename std::underlying_type<EnumType>::type;
45    return static_cast<EnumType>(~static_cast<IntType>(lhs));
46}
47
48template<typename EnumType>
49constexpr typename std::enable_if<__EnumFlagSet<EnumType>::enable, EnumType&>::type
50operator|=(EnumType & lhs, EnumType rhs) {
51    using IntType = typename std::underlying_type<EnumType>::type;
52    return lhs = static_cast<EnumType>(static_cast<IntType>(lhs) | static_cast<IntType>(rhs));
53}
54
55template<typename EnumType>
56constexpr typename std::enable_if<__EnumFlagSet<EnumType>::enable, EnumType&>::type
57operator&=(EnumType & lhs, EnumType rhs) {
58    using IntType = typename std::underlying_type<EnumType>::type;
59    return lhs = static_cast<EnumType>(static_cast<IntType>(lhs) & static_cast<IntType>(rhs));
60}
61
62template<typename EnumType>
63constexpr typename std::enable_if<__EnumFlagSet<EnumType>::enable, EnumType&>::type
64operator^=(EnumType & lhs, EnumType rhs) {
65    using IntType = typename std::underlying_type<EnumType>::type;
66    return lhs = static_cast<EnumType>(static_cast<IntType>(lhs) ^ static_cast<IntType>(rhs));
67}
68
69template<typename EnumType>
70constexpr typename std::enable_if<__EnumFlagSet<EnumType>::enable, EnumType &>::type
71operator++(EnumType & lhs) {
72    using IntType = typename std::underlying_type<EnumType>::type;
73    return lhs = static_cast<EnumType>(static_cast<IntType>(lhs) << 1);
74}
75
76template<typename EnumType>
77constexpr typename std::enable_if<__EnumFlagSet<EnumType>::enable, EnumType &>::type
78operator--(EnumType & lhs) {
79    using IntType = typename std::underlying_type<EnumType>::type;
80    return lhs = static_cast<EnumType>(static_cast<IntType>(lhs) >> 1);
81}
82
83#endif
Note: See TracBrowser for help on using the repository browser.