libcamera  v0.3.0
Supporting cameras in Linux since 2019
flags.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /*
3  * Copyright (C) 2020, Google Inc.
4  *
5  * Type-safe enum-based bitfields
6  */
7 
8 #pragma once
9 
10 #include <type_traits>
11 
12 namespace libcamera {
13 
14 template<typename E>
15 class Flags
16 {
17 public:
18  static_assert(std::is_enum<E>::value,
19  "Flags<> template parameter must be an enum");
20 
21  using Type = std::underlying_type_t<E>;
22 
23  constexpr Flags()
24  : value_(0)
25  {
26  }
27 
28  constexpr Flags(E flag)
29  : value_(static_cast<Type>(flag))
30  {
31  }
32 
33  constexpr Flags &operator&=(E flag)
34  {
35  value_ &= static_cast<Type>(flag);
36  return *this;
37  }
38 
39  constexpr Flags &operator&=(Flags other)
40  {
41  value_ &= other.value_;
42  return *this;
43  }
44 
45  constexpr Flags &operator|=(E flag)
46  {
47  value_ |= static_cast<Type>(flag);
48  return *this;
49  }
50 
51  constexpr Flags &operator|=(Flags other)
52  {
53  value_ |= other.value_;
54  return *this;
55  }
56 
57  constexpr Flags &operator^=(E flag)
58  {
59  value_ ^= static_cast<Type>(flag);
60  return *this;
61  }
62 
63  constexpr Flags &operator^=(Flags other)
64  {
65  value_ ^= other.value_;
66  return *this;
67  }
68 
69  constexpr bool operator==(E flag)
70  {
71  return value_ == static_cast<Type>(flag);
72  }
73 
74  constexpr bool operator==(Flags other)
75  {
76  return value_ == static_cast<Type>(other);
77  }
78 
79  constexpr bool operator!=(E flag)
80  {
81  return value_ != static_cast<Type>(flag);
82  }
83 
84  constexpr bool operator!=(Flags other)
85  {
86  return value_ != static_cast<Type>(other);
87  }
88 
89  constexpr explicit operator Type() const
90  {
91  return value_;
92  }
93 
94  constexpr explicit operator bool() const
95  {
96  return !!value_;
97  }
98 
99  constexpr Flags operator&(E flag) const
100  {
101  return Flags(static_cast<E>(value_ & static_cast<Type>(flag)));
102  }
103 
104  constexpr Flags operator&(Flags other) const
105  {
106  return Flags(static_cast<E>(value_ & other.value_));
107  }
108 
109  constexpr Flags operator|(E flag) const
110  {
111  return Flags(static_cast<E>(value_ | static_cast<Type>(flag)));
112  }
113 
114  constexpr Flags operator|(Flags other) const
115  {
116  return Flags(static_cast<E>(value_ | other.value_));
117  }
118 
119  constexpr Flags operator^(E flag) const
120  {
121  return Flags(static_cast<E>(value_ ^ static_cast<Type>(flag)));
122  }
123 
124  constexpr Flags operator^(Flags other) const
125  {
126  return Flags(static_cast<E>(value_ ^ other.value_));
127  }
128 
129  constexpr Flags operator~() const
130  {
131  return Flags(static_cast<E>(~value_));
132  }
133 
134  constexpr bool operator!() const
135  {
136  return !value_;
137  }
138 
139 private:
140  Type value_;
141 };
142 
143 #ifndef __DOXYGEN__
144 template<typename E>
145 struct flags_enable_operators {
146  static const bool enable = false;
147 };
148 
149 template<typename E>
150 std::enable_if_t<flags_enable_operators<E>::enable, Flags<E>>
151 operator|(E lhs, E rhs)
152 {
153  using type = std::underlying_type_t<E>;
154  return Flags<E>(static_cast<E>(static_cast<type>(lhs) | static_cast<type>(rhs)));
155 }
156 
157 template<typename E>
158 std::enable_if_t<flags_enable_operators<E>::enable, Flags<E>>
159 operator&(E lhs, E rhs)
160 {
161  using type = std::underlying_type_t<E>;
162  return Flags<E>(static_cast<E>(static_cast<type>(lhs) & static_cast<type>(rhs)));
163 }
164 
165 template<typename E>
166 std::enable_if_t<flags_enable_operators<E>::enable, Flags<E>>
167 operator^(E lhs, E rhs)
168 {
169  using type = std::underlying_type_t<E>;
170  return Flags<E>(static_cast<E>(static_cast<type>(lhs) ^ static_cast<type>(rhs)));
171 }
172 
173 template<typename E>
174 std::enable_if_t<flags_enable_operators<E>::enable, Flags<E>>
175 operator~(E rhs)
176 {
177  using type = std::underlying_type_t<E>;
178  return Flags<E>(static_cast<E>(~static_cast<type>(rhs)));
179 }
180 
181 #define LIBCAMERA_FLAGS_ENABLE_OPERATORS(_enum) \
182 template<> \
183 struct flags_enable_operators<_enum> { \
184  static const bool enable = true; \
185 };
186 
187 #else /* __DOXYGEN__ */
188 
189 #define LIBCAMERA_FLAGS_ENABLE_OPERATORS(_enum)
190 
191 #endif /* __DOXYGEN__ */
192 
193 } /* namespace libcamera */
constexpr Transform & operator &=(Transform &t0, Transform t1)
Apply bitwise AND-assignment operator between the bits in the two transforms.
Definition: transform.h:44
constexpr Flags operator|(Flags other) const
Compute the bitwise OR of this Flags and the other Flags.
Definition: flags.h:114
constexpr Flags & operator|=(Flags other)
Store the bitwise OR of this Flags and the other Flags in this Flags.
Definition: flags.h:51
constexpr Flags operator^(Flags other) const
Compute the bitwise XOR of this Flags and the other Flags.
Definition: flags.h:124
constexpr Transform operator &(Transform t0, Transform t1)
Apply bitwise AND operator between the bits in the two transforms.
Definition: transform.h:29
Top-level libcamera namespace.
Definition: backtrace.h:17
constexpr bool operator==(Flags other)
Compare flags for equality.
Definition: flags.h:74
constexpr Flags()
Construct a Flags instance with a zero value.
Definition: flags.h:23
constexpr bool operator!=(E flag)
Compare flags for non-equality.
Definition: flags.h:79
constexpr Flags & operator^=(Flags other)
Store the bitwise XOR of this Flags and the other Flags in this Flags.
Definition: flags.h:63
constexpr Flags operator^(E flag) const
Compute the bitwise XOR of this Flags and the flag.
Definition: flags.h:119
std::underlying_type_t< OpenModeFlag > Type
The underlying data type of the enum.
Definition: flags.h:21
constexpr bool operator!() const
Check if flags are set.
Definition: flags.h:134
constexpr Flags & operator|=(E flag)
Store the bitwise OR of this Flags and the flag in this Flags.
Definition: flags.h:45
constexpr bool operator==(E flag)
Compare flags for equality.
Definition: flags.h:69
constexpr Flags & operator^=(E flag)
Store the bitwise XOR of this Flags and the flag in this Flags.
Definition: flags.h:57
constexpr Flags operator|(E flag) const
Compute the bitwise OR of this Flags and the flag.
Definition: flags.h:109
constexpr bool operator!=(Flags other)
Compare flags for non-equality.
Definition: flags.h:84
Type-safe container for enum-based bitfields.
Definition: flags.h:15
constexpr Flags operator~() const
Compute the bitwise NOT of this Flags.
Definition: flags.h:129
constexpr Flags(E flag)
Construct a Flags instance storing the flag.
Definition: flags.h:28