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
291template<flag_enum E>
292[[nodiscard]] constexpr auto to_underlying(flags<E> f) noexcept {
293 return f.bits;
294}
295
303template<flag_enum E>
304[[nodiscard]] inline std::string to_string(flags<E> f) {
305 using underlying = typename flags<E>::underlying;
306 char buf[2 + sizeof(underlying) * 2];
307 buf[0] = '0';
308 buf[1] = 'x';
309 auto [ptr, ec] = std::to_chars(buf + 2, buf + sizeof(buf), to_underlying(f), 16);
310 return std::string(buf, ptr);
311}
312
313} // namespace idfxx
314
315// Free operators for flag enums, defined at global scope so that ADL finds them
316// regardless of which namespace the enum is declared in. They are constrained by
317// the flag_enum concept, so they only match opted-in enums.
318
330template<idfxx::flag_enum E>
331[[nodiscard]] constexpr idfxx::flags<E> operator|(E a, E b) noexcept {
332 return idfxx::flags<E>{a} | idfxx::flags<E>{b};
333}
334
343template<idfxx::flag_enum E>
344[[nodiscard]] constexpr idfxx::flags<E> operator&(E a, E b) noexcept {
345 return idfxx::flags<E>{a} & idfxx::flags<E>{b};
346}
347
356template<idfxx::flag_enum E>
357[[nodiscard]] constexpr idfxx::flags<E> operator^(E a, E b) noexcept {
358 return idfxx::flags<E>{a} ^ idfxx::flags<E>{b};
359}
360
368template<idfxx::flag_enum E>
369[[nodiscard]] constexpr idfxx::flags<E> operator~(E a) noexcept {
370 return ~idfxx::flags<E>{a};
371}
372
373#include "sdkconfig.h"
374#ifdef CONFIG_IDFXX_STD_FORMAT
376#include <algorithm>
377#include <format>
378namespace std {
379template<idfxx::flag_enum E>
380struct formatter<idfxx::flags<E>> {
381 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
382
383 template<typename FormatContext>
384 auto format(idfxx::flags<E> f, FormatContext& ctx) const {
385 auto s = to_string(f);
386 return std::copy(s.begin(), s.end(), ctx.out());
387 }
388};
389} // namespace std
391#endif // CONFIG_IDFXX_STD_FORMAT
392
// 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
constexpr idfxx::flags< E > operator|(E a, E b) noexcept
Combines two enum values into a flags object.
Definition flags.hpp:331
constexpr idfxx::flags< E > operator^(E a, E b) noexcept
Toggles two enum values into a flags object.
Definition flags.hpp:357
constexpr idfxx::flags< E > operator~(E a) noexcept
Computes the bitwise complement of an enum value.
Definition flags.hpp:369
constexpr idfxx::flags< E > operator&(E a, E b) noexcept
Intersects two enum values into a flags object.
Definition flags.hpp:344
std::string to_string(core_id c)
Returns a string representation of a CPU core identifier.
Definition cpu.hpp:52
constexpr bool enable_flags_operators
Opt-in trait for enabling flag operators on an enum.
Definition flags.hpp:56
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:292