summaryrefslogtreecommitdiff
path: root/include/libcamera/base/flags.h
blob: a1b404bdf23b155815730427ad9eae8a71cc5a31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2020, Google Inc.
 *
 * flags.h - Type-safe enum-based bitfields
 */

#pragma once

#include <type_traits>

namespace libcamera {

template<typename E>
class Flags
{
public:
	static_assert(std::is_enum<E>::value,
		      "Flags<> template parameter must be an enum");

	using Type = std::underlying_type_t<E>;

	constexpr Flags()
		: value_(0)
	{
	}

	constexpr Flags(E flag)
		: value_(static_cast<Type>(flag))
	{
	}

	constexpr Flags &operator&=(E flag)
	{
		value_ &= static_cast<Type>(flag);
		return *this;
	}

	constexpr Flags &operator&=(Flags other)
	{
		value_ &= other.value_;
		return *this;
	}

	constexpr Flags &operator|=(E flag)
	{
		value_ |= static_cast<Type>(flag);
		return *this;
	}

	constexpr Flags &operator|=(Flags other)
	{
		value_ |= other.value_;
		return *this;
	}

	constexpr Flags &operator^=(E flag)
	{
		value_ ^= static_cast<Type>(flag);
		return *this;
	}

	constexpr Flags &operator^=(Flags other)
	{
		value_ ^= other.value_;
		return *this;
	}

	constexpr bool operator==(E flag)
	{
		return value_ == static_cast<Type>(flag);
	}

	constexpr bool operator==(Flags other)
	{
		return value_ == static_cast<Type>(other);
	}

	constexpr bool operator!=(E flag)
	{
		return value_ != static_cast<Type>(flag);
	}

	constexpr bool operator!=(Flags other)
	{
		return value_ != static_cast<Type>(other);
	}

	constexpr explicit operator Type() const
	{
		return value_;
	}

	constexpr explicit operator bool() const
	{
		return !!value_;
	}

	constexpr Flags operator&(E flag) const
	{
		return Flags(static_cast<E>(value_ & static_cast<Type>(flag)));
	}

	constexpr Flags operator&(Flags other) const
	{
		return Flags(static_cast<E>(value_ & other.value_));
	}

	constexpr Flags operator|(E flag) const
	{
		return Flags(static_cast<E>(value_ | static_cast<Type>(flag)));
	}

	constexpr Flags operator|(Flags other) const
	{
		return Flags(static_cast<E>(value_ | other.value_));
	}

	constexpr Flags operator^(E flag) const
	{
		return Flags(static_cast<E>(value_ ^ static_cast<Type>(flag)));
	}

	constexpr Flags operator^(Flags other) const
	{
		return Flags(static_cast<E>(value_ ^ other.value_));
	}

	constexpr Flags operator~() const
	{
		return Flags(static_cast<E>(~value_));
	}

	constexpr bool operator!() const
	{
		return !value_;
	}

private:
	Type value_;
};

#ifndef __DOXYGEN__
template<typename E>
struct flags_enable_operators {
	static const bool enable = false;
};

template<typename E>
std::enable_if_t<flags_enable_operators<E>::enable, Flags<E>>
operator|(E lhs, E rhs)
{
	using type = std::underlying_type_t<E>;
	return Flags<E>(static_cast<E>(static_cast<type>(lhs) | static_cast<type>(rhs)));
}

template<typename E>
std::enable_if_t<flags_enable_operators<E>::enable, Flags<E>>
operator&(E lhs, E rhs)
{
	using type = std::underlying_type_t<E>;
	return Flags<E>(static_cast<E>(static_cast<type>(lhs) & static_cast<type>(rhs)));
}

template<typename E>
std::enable_if_t<flags_enable_operators<E>::enable, Flags<E>>
operator^(E lhs, E rhs)
{
	using type = std::underlying_type_t<E>;
	return Flags<E>(static_cast<E>(static_cast<type>(lhs) ^ static_cast<type>(rhs)));
}

template<typename E>
std::enable_if_t<flags_enable_operators<E>::enable, Flags<E>>
operator~(E rhs)
{
	using type = std::underlying_type_t<E>;
	return Flags<E>(static_cast<E>(~static_cast<type>(rhs)));
}

#define LIBCAMERA_FLAGS_ENABLE_OPERATORS(_enum)				\
template<>								\
struct flags_enable_operators<_enum> {					\
	static const bool enable = true;				\
};

#else /* __DOXYGEN__ */

#define LIBCAMERA_FLAGS_ENABLE_OPERATORS(_enum)

#endif /* __DOXYGEN__ */

} /* namespace libcamera */