Mir
flags.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2015 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 2 or 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored By: Andreas Pokorny <andreas.pokorny@canonical.com>
17  */
18 
19 #ifndef MIR_FLAGS_H_
20 #define MIR_FLAGS_H_
21 
22 #include <type_traits>
23 
24 namespace mir
25 {
26 
37 template<typename Enum>
38 struct Flags
39 {
40  using value_type = typename std::underlying_type<Enum>::type;
41 
42  explicit constexpr Flags(value_type flag_value = 0) noexcept
43  : flag_value{flag_value} {}
44  constexpr Flags(Enum flag_value) noexcept
45  : flag_value{static_cast<value_type>(flag_value)} {}
46 
47  constexpr Flags<Enum> operator|(Flags<Enum> other) const noexcept
48  {
49  return Flags<Enum>(flag_value|other.flag_value);
50  }
51 
52  constexpr Flags<Enum> operator&(Flags<Enum> other) const noexcept
53  {
54  return Flags<Enum>(flag_value & other.flag_value);
55  }
56 
57  constexpr Flags<Enum> operator^(Flags<Enum> other) const noexcept
58  {
59  return Flags<Enum>(flag_value ^ other.flag_value);
60  }
61 
62  // those mutating operators could be trated as constexpr with c++14
64  {
65  flag_value |= other.flag_value;
66  return *this;
67  }
68 
70  {
71  flag_value &= other.flag_value;
72  return *this;
73  }
74 
76  {
77  flag_value ^= other.flag_value;
78  return *this;
79  }
80 
81  constexpr bool operator==(Flags<Enum> other) const noexcept
82  {
83  return flag_value == other.flag_value;
84  }
85 
86  constexpr bool operator!=(Flags<Enum> other) const noexcept
87  {
88  return flag_value != other.flag_value;
89  }
90 
91  constexpr value_type value() const noexcept
92  {
93  return flag_value;
94  }
95 
96 private:
97  value_type flag_value;
98 };
99 
100 template<typename Enum>
101 constexpr Flags<Enum> operator|(Flags<Enum> flags, Enum e) noexcept
102 {
103  return Flags<Enum>(flags.value() | static_cast<decltype(flags.value())>(e));
104 }
105 
106 template<typename Enum>
107 constexpr Flags<Enum> operator|(Enum e, Flags<Enum> flags) noexcept
108 {
109  return Flags<Enum>(flags.value() | static_cast<decltype(flags.value())>(e));
110 }
111 
112 template<typename Enum>
113 constexpr Enum operator&(Enum e, Flags<Enum> flags) noexcept
114 {
115  return static_cast<Enum>(flags.value() & static_cast<decltype(flags.value())>(e));
116 }
117 
118 template<typename Enum>
119 constexpr Enum operator&(Flags<Enum> flags, Enum e) noexcept
120 {
121  return static_cast<Enum>(flags.value() & static_cast<decltype(flags.value())>(e));
122 }
123 
124 template<typename Enum>
125 constexpr bool operator==(Flags<Enum> flags, Enum e) noexcept
126 {
127  return e == static_cast<Enum>(flags.value());
128 }
129 
130 template<typename Enum>
131 constexpr bool operator==(Enum e, Flags<Enum> flags) noexcept
132 {
133  return e == static_cast<Enum>(flags.value());
134 }
135 
136 template<typename Enum>
137 constexpr bool contains(Flags<Enum> flags, Enum e) noexcept
138 {
139  return e == static_cast<Enum>(flags.value() & static_cast<decltype(flags.value())>(e));
140 }
141 
142 }
143 
144 template<typename Enum>
146 operator|(Enum lhs, Enum rhs) noexcept
147 {
148  return mir::Flags<Enum>(lhs) | mir::Flags<Enum>(rhs);
149 }
150 
151 template<typename Enum>
153 operator&(Enum lhs, Enum rhs) noexcept
154 {
155  return mir::Flags<Enum>(lhs) & mir::Flags<Enum>(rhs);
156 }
157 
158 
159 template<typename Enum>
161 operator^(Enum lhs, Enum rhs) noexcept
162 {
163  return mir::Flags<Enum>(lhs) ^ mir::Flags<Enum>(rhs);
164 }
165 
166 #endif
AutoUnblockThread is a helper thread class that can gracefully shutdown at destruction time...
Definition: blob.h:26
constexpr Flags(value_type flag_value=0) noexcept
Definition: flags.h:42
constexpr Flags< Enum > operator^(Flags< Enum > other) const noexcept
Definition: flags.h:57
Flags< Enum > operator &=(Flags< Enum > other) noexcept
Definition: flags.h:69
constexpr bool operator==(Flags< Enum > flags, Enum e) noexcept
Definition: flags.h:125
constexpr value_type value() const noexcept
Definition: flags.h:91
Flags< Enum > operator^=(Flags< Enum > other) noexcept
Definition: flags.h:75
typename std::underlying_type< DeviceCapability >::type value_type
Definition: flags.h:40
constexpr Flags< Enum > operator|(Flags< Enum > other) const noexcept
Definition: flags.h:47
Definition: flags.h:38
constexpr Flags< Enum > operator|(Flags< Enum > flags, Enum e) noexcept
Definition: flags.h:101
constexpr Enum operator &(Enum e, Flags< Enum > flags) noexcept
Definition: flags.h:113
constexpr bool operator==(Flags< Enum > other) const noexcept
Definition: flags.h:81
constexpr bool operator!=(Flags< Enum > other) const noexcept
Definition: flags.h:86
constexpr Flags(Enum flag_value) noexcept
Definition: flags.h:44
constexpr Flags< Enum > operator &(Flags< Enum > other) const noexcept
Definition: flags.h:52
constexpr bool contains(Flags< Enum > flags, Enum e) noexcept
Definition: flags.h:137
Flags< Enum > & operator|=(Flags< Enum > other) noexcept
Definition: flags.h:63

Copyright © 2012-2018 Canonical Ltd.
Generated on Sat Mar 31 14:22:42 UTC 2018