idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
flags.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Chris Leishman
3
4#pragma once
5
37#include <charconv>
38#include <concepts>
39#include <string>
40#include <type_traits>
41#include <utility>
42
43namespace idfxx {
44
55template<typename E>
56inline constexpr bool enable_flags_operators = false;
57
66template<typename E>
67concept flag_enum = std::is_enum_v<E> && enable_flags_operators<E>;
68
87template<flag_enum E>
88class flags {
89public:
91 using enum_type = E;
92
94 using underlying = std::underlying_type_t<E>;
95
98
99 constexpr explicit flags(underlying v) noexcept
100 : bits(v) {}
106 constexpr flags() noexcept = default;
107
118
126 [[nodiscard]] constexpr flags operator|(flags other) const noexcept { return flags{bits | other.bits}; }
127
135 constexpr flags& operator|=(flags other) noexcept {
136 bits |= other.bits;
137 return *this;
138 }
139
147 [[nodiscard]] constexpr flags operator&(flags other) const noexcept { return flags{bits & other.bits}; }
148
156 constexpr flags& operator&=(flags other) noexcept {
157 bits &= other.bits;
158 return *this;
159 }
160
168 [[nodiscard]] constexpr flags operator^(flags other) const noexcept { return flags{bits ^ other.bits}; }
169
177 constexpr flags& operator^=(flags other) noexcept {
178 bits ^= other.bits;
179 return *this;
180 }
181
191 [[nodiscard]] constexpr flags operator-(flags other) const noexcept { return flags{bits & ~other.bits}; }
192
200 constexpr flags& operator-=(flags other) noexcept {
201 bits &= ~other.bits;
202 return *this;
203 }
204
213 [[nodiscard]] constexpr flags operator~() const noexcept { return flags{~bits}; }
214
225 [[nodiscard]] constexpr bool contains(flags other) const noexcept {
226 return other.bits != 0 && (bits & other.bits) == other.bits;
227 }
228
238 [[nodiscard]] constexpr bool contains_any(flags other) const noexcept { return (bits & other.bits) != 0; }
239
245 [[nodiscard]] constexpr bool empty() const noexcept { return bits == 0; }
246
252 [[nodiscard]] constexpr explicit operator bool() const noexcept { return bits != 0; }
253
259 [[nodiscard]] constexpr bool operator==(flags const&) const noexcept = default;
260
268 [[nodiscard]] constexpr bool operator==(E e) const noexcept { return bits == std::to_underlying(e); }
269};
270
279template<flag_enum E>
281
293template<flag_enum E>
294[[nodiscard]] constexpr flags<E> operator|(E a, E b) noexcept {
295 return flags<E>{a} | flags<E>{b};
296}
297
306template<flag_enum E>
307[[nodiscard]] constexpr flags<E> operator&(E a, E b) noexcept {
308 return flags<E>{a} & flags<E>{b};
309}
310
319template<flag_enum E>
320[[nodiscard]] constexpr flags<E> operator^(E a, E b) noexcept {
321 return flags<E>{a} ^ flags<E>{b};
322}
323
331template<flag_enum E>
332[[nodiscard]] constexpr flags<E> operator~(E a) noexcept {
333 return ~flags<E>{a};
334}
335
345template<flag_enum E>
346[[nodiscard]] constexpr auto to_underlying(flags<E> f) noexcept {
347 return f.bits;
348}
349
357template<flag_enum E>
358[[nodiscard]] inline std::string to_string(flags<E> f) {
359 using underlying = typename flags<E>::underlying;
360 char buf[2 + sizeof(underlying) * 2];
361 buf[0] = '0';
362 buf[1] = 'x';
363 auto [ptr, ec] = std::to_chars(buf + 2, buf + sizeof(buf), to_underlying(f), 16);
364 return std::string(buf, ptr);
365}
366
367} // namespace idfxx
368
369#include "sdkconfig.h"
370#ifdef CONFIG_IDFXX_STD_FORMAT
372#include <algorithm>
373#include <format>
374namespace std {
375template<idfxx::flag_enum E>
376struct formatter<idfxx::flags<E>> {
377 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
378
379 template<typename FormatContext>
380 auto format(idfxx::flags<E> f, FormatContext& ctx) const {
381 auto s = to_string(f);
382 return std::copy(s.begin(), s.end(), ctx.out());
383 }
384};
385} // namespace std
387#endif // CONFIG_IDFXX_STD_FORMAT
388
// end of idfxx_core_flags // end of idfxx_core
Type-safe set of flags from a scoped enum.
Definition flags.hpp:88
constexpr flags operator|(flags other) const noexcept
Combines flags using bitwise OR.
Definition flags.hpp:126
constexpr bool operator==(flags const &) const noexcept=default
Equality comparison between flags objects.
std::underlying_type_t< E > underlying
The underlying integral type of the enum.
Definition flags.hpp:94
constexpr bool contains_any(flags other) const noexcept
Checks if any of the specified flags are set.
Definition flags.hpp:238
constexpr flags operator-(flags other) const noexcept
Clears specific flags (set difference).
Definition flags.hpp:191
constexpr bool contains(flags other) const noexcept
Checks if all specified flags are set.
Definition flags.hpp:225
constexpr flags() noexcept=default
Default constructor, initializes to empty flags (zero).
constexpr bool empty() const noexcept
Checks if no flags are set.
Definition flags.hpp:245
constexpr flags operator&(flags other) const noexcept
Intersects flags using bitwise AND.
Definition flags.hpp:147
constexpr bool operator==(E e) const noexcept
Equality comparison with an individual enum value.
Definition flags.hpp:268
constexpr flags operator^(flags other) const noexcept
Toggles flags using bitwise XOR.
Definition flags.hpp:168
constexpr flags & operator|=(flags other) noexcept
Combines flags in-place using bitwise OR.
Definition flags.hpp:135
constexpr flags & operator&=(flags other) noexcept
Intersects flags in-place using bitwise AND.
Definition flags.hpp:156
E enum_type
The scoped enum type.
Definition flags.hpp:91
constexpr flags operator~() const noexcept
Computes the bitwise complement.
Definition flags.hpp:213
constexpr flags & operator^=(flags other) noexcept
Toggles flags in-place using bitwise XOR.
Definition flags.hpp:177
constexpr flags & operator-=(flags other) noexcept
Clears specific flags in-place (set difference).
Definition flags.hpp:200
Concept for enums that have opted into flag operators.
Definition flags.hpp:67
std::string to_string(core_id c)
Returns a string representation of a CPU core identifier.
Definition cpu.hpp:52
constexpr flags< E > operator~(E a) noexcept
Computes the bitwise complement of an enum value.
Definition flags.hpp:332
constexpr bool enable_flags_operators
Opt-in trait for enabling flag operators on an enum.
Definition flags.hpp:56
constexpr flags< E > operator^(E a, E b) noexcept
Toggles two enum values into a flags object.
Definition flags.hpp:320
std::expected< T, std::error_code > result
result type wrapping a value or error code.
Definition error.hpp:120
flags(E) -> flags< E >
Class template argument deduction guide.
constexpr auto to_underlying(flags< E > f) noexcept
Returns the underlying integral value of a flags object.
Definition flags.hpp:346
constexpr flags< E > operator|(E a, E b) noexcept
Combines two enum values into a flags object.
Definition flags.hpp:294
constexpr flags< E > operator&(E a, E b) noexcept
Intersects two enum values into a flags object.
Definition flags.hpp:307