distortos  v0.7.0
object-oriented C++ RTOS for microcontrollers
EnumClassFlags.hpp
Go to the documentation of this file.
1 
15 #ifndef ESTD_ENUMCLASSFLAGS_HPP_
16 #define ESTD_ENUMCLASSFLAGS_HPP_
17 
18 #include <type_traits>
19 
20 namespace estd
21 {
22 
29 template<typename T>
30 struct isEnumClassFlags : std::false_type
31 {
32 
33 };
34 
35 } // namespace estd
36 
49 template<typename T>
50 constexpr typename std::enable_if<estd::isEnumClassFlags<T>{}, T>::type operator&(const T& left, const T& right)
51 {
52  using UnderlyingType = typename std::underlying_type<T>::type;
53  return static_cast<T>(static_cast<UnderlyingType>(left) & static_cast<UnderlyingType>(right));
54 }
55 
68 template<typename T>
69 constexpr typename std::enable_if<estd::isEnumClassFlags<T>{}, T>::type operator|(const T& left, const T& right)
70 {
71  using UnderlyingType = typename std::underlying_type<T>::type;
72  return static_cast<T>(static_cast<UnderlyingType>(left) | static_cast<UnderlyingType>(right));
73 }
74 
87 template<typename T>
88 constexpr typename std::enable_if<estd::isEnumClassFlags<T>{}, T>::type operator^(const T& left, const T& right)
89 {
90  using UnderlyingType = typename std::underlying_type<T>::type;
91  return static_cast<T>(static_cast<UnderlyingType>(left) ^ static_cast<UnderlyingType>(right));
92 }
93 
105 template<typename T>
106 constexpr typename std::enable_if<estd::isEnumClassFlags<T>{}, T>::type operator~(const T& object)
107 {
108  using UnderlyingType = typename std::underlying_type<T>::type;
109  return static_cast<T>(~static_cast<UnderlyingType>(object));
110 }
111 
124 template<typename T>
125 constexpr typename std::enable_if<estd::isEnumClassFlags<T>{}, T&>::type operator&=(T& left, const T& right)
126 {
127  return (left = left & right);
128 }
129 
142 template<typename T>
143 constexpr typename std::enable_if<estd::isEnumClassFlags<T>{}, T&>::type operator|=(T& left, const T& right)
144 {
145  return (left = left | right);
146 }
147 
160 template<typename T>
161 constexpr typename std::enable_if<estd::isEnumClassFlags<T>{}, T&>::type operator^=(T& left, const T& right)
162 {
163  return (left = left ^ right);
164 }
165 
166 #endif // ESTD_ENUMCLASSFLAGS_HPP_
Collection of useful templates.
Definition: DmaChannel.hpp:121
constexpr std::enable_if< estd::isEnumClassFlags< T >{}, T >::type operator~(const T &object)
Bitwise NOT operator.
Definition: EnumClassFlags.hpp:106
constexpr std::enable_if< estd::isEnumClassFlags< T >{}, T & >::type operator^=(T &left, const T &right)
Bitwise XOR assignment operator.
Definition: EnumClassFlags.hpp:161
Tag struct used to enable bitwise operators for selected enum class flags.
Definition: EnumClassFlags.hpp:30
constexpr std::enable_if< estd::isEnumClassFlags< T >{}, T & >::type operator|=(T &left, const T &right)
Bitwise OR assignment operator.
Definition: EnumClassFlags.hpp:143
constexpr std::enable_if< estd::isEnumClassFlags< T >{}, T & >::type operator&=(T &left, const T &right)
Bitwise AND assignment operator.
Definition: EnumClassFlags.hpp:125
constexpr std::enable_if< estd::isEnumClassFlags< T >{}, T >::type operator|(const T &left, const T &right)
Bitwise OR operator.
Definition: EnumClassFlags.hpp:69
constexpr std::enable_if< estd::isEnumClassFlags< T >{}, T >::type operator^(const T &left, const T &right)
Bitwise XOR operator.
Definition: EnumClassFlags.hpp:88
constexpr std::enable_if< estd::isEnumClassFlags< T >{}, T >::type operator &(const T &left, const T &right)
Bitwise AND operator.
Definition: EnumClassFlags.hpp:50