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 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 value_type value() const noexcept
87  {
88  return flag_value;
89  }
90 
91 private:
92  value_type flag_value;
93 };
94 
95 template<typename Enum>
96 constexpr Flags<Enum> operator|(Flags<Enum> flags, Enum e) noexcept
97 {
98  return Flags<Enum>(flags.value() | static_cast<decltype(flags.value())>(e));
99 }
100 
101 template<typename Enum>
102 constexpr Flags<Enum> operator|(Enum e, Flags<Enum> flags) noexcept
103 {
104  return Flags<Enum>(flags.value() | static_cast<decltype(flags.value())>(e));
105 }
106 
107 template<typename Enum>
108 constexpr Enum operator&(Enum e, Flags<Enum> flags) noexcept
109 {
110  return static_cast<Enum>(flags.value() & static_cast<decltype(flags.value())>(e));
111 }
112 
113 template<typename Enum>
114 constexpr Enum operator&(Flags<Enum> flags, Enum e) noexcept
115 {
116  return static_cast<Enum>(flags.value() & static_cast<decltype(flags.value())>(e));
117 }
118 
119 template<typename Enum>
120 constexpr bool operator==(Flags<Enum> flags, Enum e) noexcept
121 {
122  return e == static_cast<Enum>(flags.value());
123 }
124 
125 template<typename Enum>
126 constexpr bool operator==(Enum e, Flags<Enum> flags) noexcept
127 {
128  return e == static_cast<Enum>(flags.value());
129 }
130 
131 template<typename Enum>
132 constexpr bool contains(Flags<Enum> flags, Enum e) noexcept
133 {
134  return e == static_cast<Enum>(flags.value() & static_cast<decltype(flags.value())>(e));
135 }
136 
137 }
138 
139 template<typename Enum>
141 operator|(Enum lhs, Enum rhs) noexcept
142 {
143  return mir::Flags<Enum>(lhs) | mir::Flags<Enum>(rhs);
144 }
145 
146 template<typename Enum>
148 operator&(Enum lhs, Enum rhs) noexcept
149 {
150  return mir::Flags<Enum>(lhs) & mir::Flags<Enum>(rhs);
151 }
152 
153 
154 template<typename Enum>
156 operator^(Enum lhs, Enum rhs) noexcept
157 {
158  return mir::Flags<Enum>(lhs) ^ mir::Flags<Enum>(rhs);
159 }
160 
161 #endif
Definition: as_render_target.h:27
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 value_type value() const noexcept
Definition: flags.h:86
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 bool operator==(Flags< Enum > other) const noexcept
Definition: flags.h:81
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:132
Flags< Enum > & operator|=(Flags< Enum > other) noexcept
Definition: flags.h:63

Copyright © 2012-2016 Canonical Ltd.
Generated on Sat Dec 3 12:48:59 UTC 2016