Files
Library/include/Void/Core/Flags.hpp
faumarayandClaude Opus 4.8 80293ee56f Add async/resilience/reflection primitives + FSM v2 rewrite
A round of new universal capabilities (all generic — templated/reflection
based, no app coupling), verified by the test suite (1103 passing):

Data reflection:
- Reflect.hpp now (de)serializes chrono::duration, chrono::time_point,
  std::variant (tagged), std::array, and non-string-keyed std::map;
  Meta.hpp gains is_time_point_v et al.

Async:
- EventBus gains Mode::Async (worker-thread dispatch + flush()).
- ThreadPool gains a bounded queue, try_submit(), and rejection stats.

New modules / containers:
- Resilience: TokenBucket, KeyedRateLimiter<Key>, CircuitBreaker,
  ResourceMonitor.
- Containers: BufferPool<T> (size-class buffer reuse) and Cache<K,V>
  (per-entry TTL, LRU/LFU/FIFO eviction, hit/miss stats).
- System: process/CPU/memory metrics (Metrics.hpp) and a streaming
  Process API.

Plus FSM v2 (typed Success/Failure, sub-machines, parallel regions,
history states, deferred events), Configuration/Logging reflection
helpers, and the accompanying test suites.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-22 15:45:16 +03:00

234 lines
6.1 KiB
C++

#pragma once
#include <Void/Core/Meta.hpp>
#include <initializer_list>
#include <string>
#include <string_view>
#include <type_traits>
namespace Void::Flags
{
// ── Flags<E>
// ─────────────────────────────────────────────────────────────────
// Type-safe bitmask wrapper over a scoped enum class E.
// E's enumerators must be distinct single-bit values (1, 2, 4, 8, …).
template <typename E>
requires std::is_enum_v<E>
class Flags
{
public:
using underlying = std::underlying_type_t<E>;
constexpr Flags() noexcept = default;
constexpr Flags(E bit) noexcept
: bits_{static_cast<underlying>(bit)}
{
}
constexpr explicit Flags(underlying raw) noexcept : bits_{raw} {}
constexpr Flags(std::initializer_list<E> list) noexcept
{
for (const E e : list) {
bits_ |= static_cast<underlying>(e);
}
}
// ── query
// ─────────────────────────────────────────────────────────────────
[[nodiscard]] constexpr bool test(E bit) const noexcept
{
return (bits_ & static_cast<underlying>(bit)) != 0;
}
[[nodiscard]] constexpr bool any() const noexcept
{
return bits_ != 0;
}
[[nodiscard]] constexpr bool none() const noexcept
{
return bits_ == 0;
}
[[nodiscard]] constexpr bool all(Flags mask) const noexcept
{
return (bits_ & mask.bits_) == mask.bits_;
}
[[nodiscard]] constexpr bool any_of(Flags mask) const noexcept
{
return (bits_ & mask.bits_) != 0;
}
[[nodiscard]] constexpr underlying raw() const noexcept
{
return bits_;
}
// ── mutation
// ──────────────────────────────────────────────────────────────
constexpr Flags& set(E bit) noexcept
{
bits_ |= static_cast<underlying>(bit);
return *this;
}
constexpr Flags& unset(E bit) noexcept
{
bits_ &= ~static_cast<underlying>(bit);
return *this;
}
constexpr Flags& toggle(E bit) noexcept
{
bits_ ^= static_cast<underlying>(bit);
return *this;
}
constexpr void clear() noexcept { bits_ = 0; }
// ── operators
// ─────────────────────────────────────────────────────────────
[[nodiscard]] constexpr Flags operator|(Flags o) const noexcept
{
return Flags{static_cast<underlying>(bits_ | o.bits_)};
}
[[nodiscard]] constexpr Flags operator&(Flags o) const noexcept
{
return Flags{static_cast<underlying>(bits_ & o.bits_)};
}
[[nodiscard]] constexpr Flags operator^(Flags o) const noexcept
{
return Flags{static_cast<underlying>(bits_ ^ o.bits_)};
}
[[nodiscard]] constexpr Flags operator~() const noexcept
{
return Flags{static_cast<underlying>(~bits_)};
}
[[nodiscard]] constexpr Flags operator|(E bit) const noexcept
{
return *this | Flags{bit};
}
[[nodiscard]] constexpr Flags operator&(E bit) const noexcept
{
return *this & Flags{bit};
}
[[nodiscard]] constexpr Flags operator^(E bit) const noexcept
{
return *this ^ Flags { bit };
}
constexpr Flags& operator|=(Flags o) noexcept
{
bits_ |= o.bits_;
return *this;
}
constexpr Flags& operator&=(Flags o) noexcept
{
bits_ &= o.bits_;
return *this;
}
constexpr Flags& operator^=(Flags o) noexcept
{
bits_ ^= o.bits_;
return *this;
}
constexpr Flags& operator|=(E bit) noexcept { return set(bit); }
constexpr Flags& operator&=(E bit) noexcept
{
bits_ &= static_cast<underlying>(bit);
return *this;
}
constexpr Flags& operator^=(E bit) noexcept { return toggle(bit); }
[[nodiscard]] constexpr bool
operator==(const Flags&) const noexcept = default;
[[nodiscard]] constexpr explicit operator bool() const noexcept
{
return any();
}
private:
underlying bits_{0};
};
// ── make_flags — convenience factory
// ─────────────────────────────────────────
template <typename E>
requires std::is_enum_v<E>
[[nodiscard]] constexpr Flags<E>
make_flags(std::initializer_list<E> list) noexcept
{
return Flags<E>{list};
}
template <typename E>
requires std::is_enum_v<E>
[[nodiscard]] constexpr Flags<E> operator|(E a, E b) noexcept
{
return Flags<E>{a} | Flags<E>{b};
}
// ── reflective names (Void::Meta enum reflection)
// ────────────────────────────
#ifdef VOID_HAS_META_REFLECTION
/// Render the set bits as their enumerator names, joined by `sep`
/// (e.g. "Read|Write"). A zero-valued enumerator never appears.
template <typename E>
requires std::is_enum_v<E>
[[nodiscard]] std::string to_string(Flags<E> f, std::string_view sep = "|")
{
std::string out;
for (const auto& [value, name] : Void::Meta::enum_entries<E>()) {
if (f.test(value)) {
if (!out.empty()) {
out += sep;
}
out += name;
}
}
return out;
}
/// Parse a `sep`-separated list of enumerator names into a Flags<E>.
/// Unknown names are ignored.
template <typename E>
requires std::is_enum_v<E>
[[nodiscard]] Flags<E> parse(std::string_view text, char sep = '|')
{
Flags<E> f;
std::size_t pos = 0;
while (pos <= text.size()) {
const auto next = text.find(sep, pos);
const auto end =
(next == std::string_view::npos) ? text.size() : next;
const auto token = text.substr(pos, end - pos);
if (!token.empty()) {
if (const auto e = Void::Meta::enum_parse<E>(token);
e.has_value()) {
f.set(*e);
}
}
if (next == std::string_view::npos) {
break;
}
pos = next + 1;
}
return f;
}
#endif // VOID_HAS_META_REFLECTION
} // namespace Void::Flags