Restructure into umbrella modules (~55 -> 16 targets)

Headers move, CMake targets merge, C++ namespaces stay unchanged:
- Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant
- Functional: Pipeline, Either, Result, Retry
- Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval
- Math: +Matrix, Numeric, Random, Histogram
- Text: Str, Format, Tokenizer, Diff, Encoding, Semver
- Data: Json, Csv, Ini
- Async: Coro, Concurrency, EventBus, Observable
- Time: +Bench
- Log: Log facade + Logging framework (facade now header-only, breaking
  the Stacktrace<->Log link cycle the merge would have created)
- System: FileSystem + Process
- Configuration: +Argparse
- Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail

Module system fixes:
- OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no
  longer depends on the alphabetical discovery order of src/ subdirs
  (Testing -> Text would have been silently dropped).
- Every header-only module now has a tracked module.cmake; previously the
  empty src/<Name> marker dirs were untracked, so a fresh clone would have
  silently skipped those modules.
- VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT.

Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files
prefixed: ResultBasic etc.). Test count verified identical (831) before and
after. README/CLAUDE.md module tables updated.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Fable 5 committed 2026-06-12 18:50:02 +03:00
1 parent da2e93c405
commit 28b94883fd
156 files changed
+278 -220

No files matched your search

+178
View File
@@ -0,0 +1,178 @@
#pragma once
#include <bit>
#include <concepts>
#include <cstdint>
#include <limits>
namespace Void::Bits
{
// ── Queries ───────────────────────────────────────────────────────────────────
template <std::unsigned_integral T>
[[nodiscard]] constexpr int popcount(T v) noexcept
{
return std::popcount(v);
}
template <std::unsigned_integral T>
[[nodiscard]] constexpr int count_leading_zeros(T v) noexcept
{
return std::countl_zero(v);
}
template <std::unsigned_integral T>
[[nodiscard]] constexpr int count_trailing_zeros(T v) noexcept
{
return std::countr_zero(v);
}
template <std::unsigned_integral T>
[[nodiscard]] constexpr int count_leading_ones(T v) noexcept
{
return std::countl_one(v);
}
template <std::unsigned_integral T>
[[nodiscard]] constexpr int count_trailing_ones(T v) noexcept
{
return std::countr_one(v);
}
template <std::unsigned_integral T>
[[nodiscard]] constexpr bool has_single_bit(T v) noexcept
{
return std::has_single_bit(v);
}
/// Number of bits needed to represent v (floor(log2(v)) + 1). Zero for v=0.
template <std::unsigned_integral T>
[[nodiscard]] constexpr int bit_width(T v) noexcept
{
return std::bit_width(v);
}
// ── Rotation ─────────────────────────────────────────────────────────────────
template <std::unsigned_integral T>
[[nodiscard]] constexpr T rotl(T v, int s) noexcept
{
return std::rotl(v, s);
}
template <std::unsigned_integral T>
[[nodiscard]] constexpr T rotr(T v, int s) noexcept
{
return std::rotr(v, s);
}
// ── Bit manipulation ─────────────────────────────────────────────────────────
template <std::unsigned_integral T>
[[nodiscard]] constexpr bool test_bit(T v, int bit) noexcept
{
return (v >> bit) & T{1};
}
template <std::unsigned_integral T>
[[nodiscard]] constexpr T set_bit(T v, int bit) noexcept
{
return v | (T{1} << bit);
}
template <std::unsigned_integral T>
[[nodiscard]] constexpr T clear_bit(T v, int bit) noexcept
{
return v & ~(T{1} << bit);
}
template <std::unsigned_integral T>
[[nodiscard]] constexpr T toggle_bit(T v, int bit) noexcept
{
return v ^ (T{1} << bit);
}
/// Extract bits [lo, lo+len) from v.
template <std::unsigned_integral T>
[[nodiscard]] constexpr T extract_bits(T v, int lo, int len) noexcept
{
return (v >> lo) & ((T{1} << len) - T{1});
}
/// Deposit val into bits [lo, lo+len) of dest.
template <std::unsigned_integral T>
[[nodiscard]] constexpr T deposit_bits(T dest, T val, int lo, int len) noexcept
{
const T mask = ((T{1} << len) - T{1}) << lo;
return (dest & ~mask) | ((val << lo) & mask);
}
// ── Byte swap / endian ────────────────────────────────────────────────────────
template <std::unsigned_integral T>
[[nodiscard]] constexpr T byteswap(T v) noexcept
{
return std::byteswap(v);
}
template <std::unsigned_integral T>
[[nodiscard]] constexpr T to_little_endian(T v) noexcept
{
if constexpr (std::endian::native == std::endian::little) return v;
return std::byteswap(v);
}
template <std::unsigned_integral T>
[[nodiscard]] constexpr T to_big_endian(T v) noexcept
{
if constexpr (std::endian::native == std::endian::big) return v;
return std::byteswap(v);
}
// ── Power-of-two helpers ─────────────────────────────────────────────────────
template <std::unsigned_integral T>
[[nodiscard]] constexpr T ceil_pow2(T v) noexcept
{
return std::bit_ceil(v);
}
template <std::unsigned_integral T>
[[nodiscard]] constexpr T floor_pow2(T v) noexcept
{
return std::bit_floor(v);
}
/// Round v up to the next multiple of align (align must be a power of two).
template <std::unsigned_integral T>
[[nodiscard]] constexpr T align_up(T v, T align) noexcept
{
return (v + align - T{1}) & ~(align - T{1});
}
template <std::unsigned_integral T>
[[nodiscard]] constexpr T align_down(T v, T align) noexcept
{
return v & ~(align - T{1});
}
// ── Masks ────────────────────────────────────────────────────────────────────
/// Create a mask with `n` low bits set.
template <std::unsigned_integral T>
[[nodiscard]] constexpr T low_mask(int n) noexcept
{
if (n == 0) return T{0};
if (n >= std::numeric_limits<T>::digits) return ~T{0};
return (T{1} << n) - T{1};
}
/// Create a mask with bits [lo, hi] set.
template <std::unsigned_integral T>
[[nodiscard]] constexpr T range_mask(int lo, int hi) noexcept
{
return low_mask<T>(hi - lo + 1) << lo;
}
} // namespace Void::Bits
+106
View File
@@ -0,0 +1,106 @@
#pragma once
#include <atomic>
#include <cstddef>
namespace Void::Counted
{
// ── InstanceCounter<Tag> ──────────────────────────────────────────────────────
// Mix-in that counts how many instances of a class exist.
// Use a unique Tag type per class to keep counters separate.
//
// struct MyClass : InstanceCounter<MyClass> { ... };
// std::size_t alive = MyClass::instance_count(); // thread-safe
template <typename Tag>
class InstanceCounter
{
public:
InstanceCounter() noexcept { count_.fetch_add(1, std::memory_order_relaxed); }
~InstanceCounter() noexcept { count_.fetch_sub(1, std::memory_order_relaxed); }
InstanceCounter(const InstanceCounter&) noexcept { count_.fetch_add(1, std::memory_order_relaxed); }
InstanceCounter(InstanceCounter&&) noexcept { count_.fetch_add(1, std::memory_order_relaxed); }
InstanceCounter& operator=(const InstanceCounter&) noexcept { return *this; }
InstanceCounter& operator=(InstanceCounter&&) noexcept { return *this; }
[[nodiscard]] static std::size_t instance_count() noexcept
{
return count_.load(std::memory_order_relaxed);
}
private:
static std::atomic<std::size_t> count_;
};
template <typename Tag>
std::atomic<std::size_t> InstanceCounter<Tag>::count_{0};
// ── AtomicCounter ─────────────────────────────────────────────────────────────
// Standalone thread-safe counter with increment/decrement/reset.
class AtomicCounter
{
public:
explicit AtomicCounter(std::size_t initial = 0) noexcept : value_{initial} {}
std::size_t increment(std::size_t by = 1) noexcept
{
return value_.fetch_add(by, std::memory_order_relaxed) + by;
}
std::size_t decrement(std::size_t by = 1) noexcept
{
return value_.fetch_sub(by, std::memory_order_relaxed) - by;
}
void reset(std::size_t v = 0) noexcept { value_.store(v, std::memory_order_relaxed); }
[[nodiscard]] std::size_t get() const noexcept
{
return value_.load(std::memory_order_relaxed);
}
[[nodiscard]] std::size_t operator++(int) noexcept
{
return value_.fetch_add(1, std::memory_order_relaxed);
}
[[nodiscard]] std::size_t operator--(int) noexcept
{
return value_.fetch_sub(1, std::memory_order_relaxed);
}
AtomicCounter& operator++() noexcept { increment(); return *this; }
AtomicCounter& operator--() noexcept { decrement(); return *this; }
private:
std::atomic<std::size_t> value_;
};
// ── SeqId ────────────────────────────────────────────────────────────────────
// Monotonically increasing sequence ID generator.
// Each SeqId<Tag> has its own counter, starting from 1.
template <typename Tag = void>
class SeqId
{
public:
SeqId() : id_{next_.fetch_add(1, std::memory_order_relaxed)} {}
[[nodiscard]] std::size_t id() const noexcept { return id_; }
[[nodiscard]] static std::size_t next_id() noexcept
{
return next_.load(std::memory_order_relaxed);
}
private:
static std::atomic<std::size_t> next_;
std::size_t id_;
};
template <typename Tag>
std::atomic<std::size_t> SeqId<Tag>::next_{1};
} // namespace Void::Counted
+114
View File
@@ -0,0 +1,114 @@
#pragma once
#include <initializer_list>
#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};
}
} // namespace Void::Flags
+170
View File
@@ -0,0 +1,170 @@
#pragma once
#include <array>
#include <cstdint>
#include <span>
#include <string_view>
namespace Void::Hash
{
// ── FNV-1a (32-bit) ──────────────────────────────────────────────────────────
[[nodiscard]] constexpr std::uint32_t fnv1a32(std::string_view s) noexcept
{
std::uint32_t h = 2166136261u;
for (unsigned char c : s) {
h ^= c;
h *= 16777619u;
}
return h;
}
[[nodiscard]] constexpr std::uint32_t
fnv1a32(std::span<const std::byte> data) noexcept
{
std::uint32_t h = 2166136261u;
for (auto b : data) {
h ^= static_cast<unsigned char>(b);
h *= 16777619u;
}
return h;
}
// ── FNV-1a (64-bit) ──────────────────────────────────────────────────────────
[[nodiscard]] constexpr std::uint64_t fnv1a64(std::string_view s) noexcept
{
std::uint64_t h = 14695981039346656037ull;
for (unsigned char c : s) {
h ^= c;
h *= 1099511628211ull;
}
return h;
}
[[nodiscard]] constexpr std::uint64_t
fnv1a64(std::span<const std::byte> data) noexcept
{
std::uint64_t h = 14695981039346656037ull;
for (auto b : data) {
h ^= static_cast<unsigned char>(b);
h *= 1099511628211ull;
}
return h;
}
// ── CRC-32 (ISO 3309 / Ethernet polynomial) ───────────────────────────────────
namespace Detail
{
[[nodiscard]] constexpr std::array<std::uint32_t, 256> make_crc32_table() noexcept
{
std::array<std::uint32_t, 256> t{};
for (std::uint32_t i = 0; i < 256; ++i) {
std::uint32_t c = i;
for (int k = 0; k < 8; ++k) {
c = (c & 1) ? (0xEDB88320u ^ (c >> 1)) : (c >> 1);
}
t[i] = c;
}
return t;
}
} // namespace Detail
[[nodiscard]] inline std::uint32_t crc32(std::string_view s) noexcept
{
static constexpr auto table = Detail::make_crc32_table();
std::uint32_t c = 0xFFFFFFFFu;
for (unsigned char b : s) {
c = table[(c ^ b) & 0xFF] ^ (c >> 8);
}
return c ^ 0xFFFFFFFFu;
}
[[nodiscard]] inline std::uint32_t
crc32(std::span<const std::byte> data) noexcept
{
static constexpr auto table = Detail::make_crc32_table();
std::uint32_t c = 0xFFFFFFFFu;
for (auto b : data) {
c = table[(c ^ static_cast<unsigned char>(b)) & 0xFF] ^ (c >> 8);
}
return c ^ 0xFFFFFFFFu;
}
// ── DJB2 ──────────────────────────────────────────────────────────────────────
[[nodiscard]] constexpr std::uint64_t djb2(std::string_view s) noexcept
{
std::uint64_t h = 5381;
for (unsigned char c : s) {
h = ((h << 5) + h) ^ c;
}
return h;
}
// ── MurmurHash3 (32-bit, x86) ─────────────────────────────────────────────────
[[nodiscard]] inline std::uint32_t murmur3_32(std::string_view key,
std::uint32_t seed = 0) noexcept
{
const auto* data = reinterpret_cast<const std::uint8_t*>(key.data());
const std::size_t len = key.size();
const std::size_t nblocks = len / 4;
std::uint32_t h = seed;
constexpr std::uint32_t c1 = 0xcc9e2d51u;
constexpr std::uint32_t c2 = 0x1b873593u;
for (std::size_t i = 0; i < nblocks; ++i) {
std::uint32_t k;
__builtin_memcpy(&k, data + i * 4, 4);
k *= c1;
k = (k << 15) | (k >> 17);
k *= c2;
h ^= k;
h = (h << 13) | (h >> 19);
h = h * 5 + 0xe6546b64u;
}
const auto* tail = data + nblocks * 4;
std::uint32_t k = 0;
switch (len & 3) {
case 3: k ^= static_cast<std::uint32_t>(tail[2]) << 16; [[fallthrough]];
case 2: k ^= static_cast<std::uint32_t>(tail[1]) << 8; [[fallthrough]];
case 1:
k ^= static_cast<std::uint32_t>(tail[0]);
k *= c1;
k = (k << 15) | (k >> 17);
k *= c2;
h ^= k;
}
h ^= static_cast<std::uint32_t>(len);
h ^= h >> 16;
h *= 0x85ebca6bu;
h ^= h >> 13;
h *= 0xc2b2ae35u;
h ^= h >> 16;
return h;
}
// ── Convenience: string literal compile-time hash ────────────────────────────
inline namespace Literals
{
[[nodiscard]] consteval std::uint32_t
operator""_fnv32(const char* s, std::size_t n) noexcept
{
return fnv1a32(std::string_view{s, n});
}
[[nodiscard]] consteval std::uint64_t
operator""_fnv64(const char* s, std::size_t n) noexcept
{
return fnv1a64(std::string_view{s, n});
}
} // namespace Literals
} // namespace Void::Hash
+113
View File
@@ -0,0 +1,113 @@
#pragma once
#include <functional>
#include <mutex>
#include <optional>
#include <utility>
namespace Void::Lazy
{
// ── Lazy<T> ───────────────────────────────────────────────────────────────────
// Lazily evaluates a factory callable on first access.
// Thread-safety: NOT thread-safe. Use LazySync<T> for multi-threaded contexts.
template <typename T>
class Lazy
{
public:
using factory_type = std::function<T()>;
explicit Lazy(factory_type factory) : factory_{std::move(factory)} {}
Lazy(const Lazy&) = delete;
Lazy& operator=(const Lazy&) = delete;
Lazy(Lazy&&) = default;
Lazy& operator=(Lazy&&) = default;
[[nodiscard]] T& get()
{
if (!value_) value_.emplace(factory_());
return *value_;
}
[[nodiscard]] const T& get() const
{
if (!value_) value_.emplace(factory_());
return *value_;
}
[[nodiscard]] T& operator*() { return get(); }
[[nodiscard]] const T& operator*() const { return get(); }
[[nodiscard]] T* operator->() { return &get(); }
[[nodiscard]] const T* operator->() const { return &get(); }
[[nodiscard]] bool evaluated() const noexcept { return value_.has_value(); }
void reset()
{
value_.reset();
}
private:
factory_type factory_;
mutable std::optional<T> value_;
};
// ── LazySync<T> ───────────────────────────────────────────────────────────────
// Thread-safe variant using std::call_once.
template <typename T>
class LazySync
{
public:
using factory_type = std::function<T()>;
explicit LazySync(factory_type factory) : factory_{std::move(factory)} {}
LazySync(const LazySync&) = delete;
LazySync& operator=(const LazySync&) = delete;
LazySync(LazySync&&) = delete;
LazySync& operator=(LazySync&&) = delete;
[[nodiscard]] T& get()
{
std::call_once(flag_, [&] { value_.emplace(factory_()); });
return *value_;
}
[[nodiscard]] const T& get() const
{
std::call_once(flag_, [&] { value_.emplace(factory_()); });
return *value_;
}
[[nodiscard]] T& operator*() { return get(); }
[[nodiscard]] const T& operator*() const { return get(); }
[[nodiscard]] T* operator->() { return &get(); }
[[nodiscard]] const T* operator->() const { return &get(); }
[[nodiscard]] bool evaluated() const noexcept { return value_.has_value(); }
private:
factory_type factory_;
mutable std::once_flag flag_;
mutable std::optional<T> value_;
};
// ── make_lazy — deduction helper ──────────────────────────────────────────────
template <typename F>
[[nodiscard]] auto make_lazy(F&& factory) -> Lazy<std::invoke_result_t<F>>
{
return Lazy<std::invoke_result_t<F>>{std::forward<F>(factory)};
}
template <typename F>
[[nodiscard]] auto make_lazy_sync(F&& factory) -> LazySync<std::invoke_result_t<F>>
{
return LazySync<std::invoke_result_t<F>>{std::forward<F>(factory)};
}
} // namespace Void::Lazy
+131
View File
@@ -0,0 +1,131 @@
#pragma once
#include <exception>
#include <utility>
namespace Void::Scope
{
// ── ScopeExit — runs on scope exit unconditionally ───────────────────────────
template <std::invocable F>
class [[nodiscard]] ScopeExit
{
public:
explicit ScopeExit(F f) noexcept(std::is_nothrow_move_constructible_v<F>)
: f_{std::move(f)}
{
}
ScopeExit(ScopeExit&& o) noexcept
: f_{std::move(o.f_)}, active_{o.active_}
{
o.active_ = false;
}
~ScopeExit() noexcept { if (active_) f_(); }
ScopeExit(const ScopeExit&) = delete;
ScopeExit& operator=(const ScopeExit&) = delete;
ScopeExit& operator=(ScopeExit&&) = delete;
void release() noexcept { active_ = false; }
void dismiss() noexcept { active_ = false; }
[[nodiscard]] bool active() const noexcept { return active_; }
private:
F f_;
bool active_{true};
};
// ── ScopeSuccess — runs only when scope exits normally (no exception) ─────────
template <std::invocable F>
class [[nodiscard]] ScopeSuccess
{
public:
explicit ScopeSuccess(F f) noexcept(std::is_nothrow_move_constructible_v<F>)
: f_{std::move(f)}, exceptions_{std::uncaught_exceptions()}
{
}
~ScopeSuccess() noexcept
{
if (active_ && std::uncaught_exceptions() == exceptions_) f_();
}
ScopeSuccess(const ScopeSuccess&) = delete;
ScopeSuccess& operator=(const ScopeSuccess&) = delete;
ScopeSuccess(ScopeSuccess&&) = delete;
ScopeSuccess& operator=(ScopeSuccess&&) = delete;
void release() noexcept { active_ = false; }
void dismiss() noexcept { active_ = false; }
private:
F f_;
int exceptions_;
bool active_{true};
};
// ── ScopeFail — runs only when scope exits via exception ──────────────────────
template <std::invocable F>
class [[nodiscard]] ScopeFail
{
public:
explicit ScopeFail(F f) noexcept(std::is_nothrow_move_constructible_v<F>)
: f_{std::move(f)}, exceptions_{std::uncaught_exceptions()}
{
}
~ScopeFail() noexcept
{
if (active_ && std::uncaught_exceptions() > exceptions_) f_();
}
ScopeFail(const ScopeFail&) = delete;
ScopeFail& operator=(const ScopeFail&) = delete;
ScopeFail(ScopeFail&&) = delete;
ScopeFail& operator=(ScopeFail&&) = delete;
void release() noexcept { active_ = false; }
void dismiss() noexcept { active_ = false; }
private:
F f_;
int exceptions_;
bool active_{true};
};
// ── Factory functions ─────────────────────────────────────────────────────────
template <std::invocable F>
[[nodiscard]] auto on_exit(F&& f)
{
return ScopeExit<std::decay_t<F>>{std::forward<F>(f)};
}
template <std::invocable F>
[[nodiscard]] auto on_success(F&& f)
{
return ScopeSuccess<std::decay_t<F>>{std::forward<F>(f)};
}
template <std::invocable F>
[[nodiscard]] auto on_fail(F&& f)
{
return ScopeFail<std::decay_t<F>>{std::forward<F>(f)};
}
// Aliases matching std::scope_{exit,success,failure} proposal naming.
template <std::invocable F>
[[nodiscard]] auto scope_exit(F&& f) { return on_exit(std::forward<F>(f)); }
template <std::invocable F>
[[nodiscard]] auto scope_success(F&& f) { return on_success(std::forward<F>(f)); }
template <std::invocable F>
[[nodiscard]] auto scope_failure(F&& f) { return on_fail(std::forward<F>(f)); }
} // namespace Void::Scope
+280
View File
@@ -0,0 +1,280 @@
#pragma once
#include <concepts>
#include <cstddef>
#include <tuple>
#include <type_traits>
namespace Void::TypeList
{
// ── TypeList<Ts...> ───────────────────────────────────────────────────────────
template <typename... Ts>
struct TypeList
{
static constexpr std::size_t size = sizeof...(Ts);
using as_tuple = std::tuple<Ts...>;
};
// ── size_of ───────────────────────────────────────────────────────────────────
template <typename List>
inline constexpr std::size_t size_of = 0;
template <typename... Ts>
inline constexpr std::size_t size_of<TypeList<Ts...>> = sizeof...(Ts);
// ── at<I, List> ───────────────────────────────────────────────────────────────
template <std::size_t I, typename List>
struct At;
template <std::size_t I, typename Head, typename... Tail>
struct At<I, TypeList<Head, Tail...>> : At<I - 1, TypeList<Tail...>> {};
template <typename Head, typename... Tail>
struct At<0, TypeList<Head, Tail...>>
{
using type = Head;
};
template <std::size_t I, typename List>
using at = typename At<I, List>::type;
// ── front / back ──────────────────────────────────────────────────────────────
template <typename List>
struct Front;
template <typename Head, typename... Tail>
struct Front<TypeList<Head, Tail...>>
{
using type = Head;
};
template <typename List>
using front = typename Front<List>::type;
template <typename List>
struct Back;
template <typename T>
struct Back<TypeList<T>>
{
using type = T;
};
template <typename Head, typename... Tail>
struct Back<TypeList<Head, Tail...>> : Back<TypeList<Tail...>> {};
template <typename List>
using back = typename Back<List>::type;
// ── contains<T, List> ─────────────────────────────────────────────────────────
template <typename T, typename List>
struct Contains;
template <typename T, typename... Ts>
struct Contains<T, TypeList<Ts...>>
: std::bool_constant<(std::is_same_v<T, Ts> || ...)>
{};
template <typename T, typename List>
inline constexpr bool contains = Contains<T, List>::value;
// ── index_of<T, List> — first index, or size if not found ────────────────────
template <typename T, typename List>
struct IndexOf;
template <typename T>
struct IndexOf<T, TypeList<>>
{
static constexpr std::size_t value = 0;
};
template <typename T, typename Head, typename... Tail>
struct IndexOf<T, TypeList<Head, Tail...>>
{
static constexpr std::size_t value =
std::is_same_v<T, Head>
? 0
: 1 + IndexOf<T, TypeList<Tail...>>::value;
};
template <typename T, typename List>
inline constexpr std::size_t index_of = IndexOf<T, List>::value;
// ── push_front / push_back ────────────────────────────────────────────────────
template <typename T, typename List>
struct PushFront;
template <typename T, typename... Ts>
struct PushFront<T, TypeList<Ts...>>
{
using type = TypeList<T, Ts...>;
};
template <typename T, typename List>
using push_front = typename PushFront<T, List>::type;
template <typename T, typename List>
struct PushBack;
template <typename T, typename... Ts>
struct PushBack<T, TypeList<Ts...>>
{
using type = TypeList<Ts..., T>;
};
template <typename T, typename List>
using push_back = typename PushBack<T, List>::type;
// ── pop_front ─────────────────────────────────────────────────────────────────
template <typename List>
struct PopFront;
template <typename Head, typename... Tail>
struct PopFront<TypeList<Head, Tail...>>
{
using type = TypeList<Tail...>;
};
template <typename List>
using pop_front = typename PopFront<List>::type;
// ── concat<A, B> ─────────────────────────────────────────────────────────────
template <typename A, typename B>
struct Concat;
template <typename... As, typename... Bs>
struct Concat<TypeList<As...>, TypeList<Bs...>>
{
using type = TypeList<As..., Bs...>;
};
template <typename A, typename B>
using concat = typename Concat<A, B>::type;
// ── filter<Pred, List> ────────────────────────────────────────────────────────
template <template <typename> typename Pred, typename List>
struct Filter;
template <template <typename> typename Pred>
struct Filter<Pred, TypeList<>>
{
using type = TypeList<>;
};
template <template <typename> typename Pred, typename Head, typename... Tail>
struct Filter<Pred, TypeList<Head, Tail...>>
{
using filtered_tail = typename Filter<Pred, TypeList<Tail...>>::type;
using type = std::conditional_t<Pred<Head>::value,
push_front<Head, filtered_tail>,
filtered_tail>;
};
template <template <typename> typename Pred, typename List>
using filter = typename Filter<Pred, List>::type;
// ── transform<F, List> ────────────────────────────────────────────────────────
template <template <typename> typename F, typename List>
struct Transform;
template <template <typename> typename F, typename... Ts>
struct Transform<F, TypeList<Ts...>>
{
using type = TypeList<typename F<Ts>::type...>;
};
template <template <typename> typename F, typename List>
using transform = typename Transform<F, List>::type;
// ── reverse<List> ────────────────────────────────────────────────────────────
template <typename List, typename Acc = TypeList<>>
struct Reverse;
template <typename Acc>
struct Reverse<TypeList<>, Acc>
{
using type = Acc;
};
template <typename Head, typename... Tail, typename Acc>
struct Reverse<TypeList<Head, Tail...>, Acc>
: Reverse<TypeList<Tail...>, push_front<Head, Acc>>
{};
template <typename List>
using reverse = typename Reverse<List>::type;
// ── unique<List> — remove duplicate types (keep first occurrence) ─────────────
template <typename List, typename Seen = TypeList<>>
struct Unique;
template <typename Seen>
struct Unique<TypeList<>, Seen>
{
using type = Seen;
};
template <typename Head, typename... Tail, typename Seen>
struct Unique<TypeList<Head, Tail...>, Seen>
{
using type = std::conditional_t<
contains<Head, Seen>,
typename Unique<TypeList<Tail...>, Seen>::type,
typename Unique<TypeList<Tail...>, push_back<Head, Seen>>::type>;
};
template <typename List>
using unique = typename Unique<List>::type;
// ── for_each — call f() for each type in the list ────────────────────────────
template <typename List, typename F>
constexpr void for_each(F&& f)
{
[]<typename... Ts>(std::type_identity<TypeList<Ts...>>, auto& fn) {
(fn.template operator()<Ts>(), ...);
}(std::type_identity<List>{}, f);
}
// ── all_of / any_of / none_of ─────────────────────────────────────────────────
template <template <typename> typename Pred, typename List>
struct AllOf;
template <template <typename> typename Pred, typename... Ts>
struct AllOf<Pred, TypeList<Ts...>>
: std::bool_constant<(Pred<Ts>::value && ...)>
{};
template <template <typename> typename Pred, typename List>
inline constexpr bool all_of = AllOf<Pred, List>::value;
template <template <typename> typename Pred, typename List>
struct AnyOf;
template <template <typename> typename Pred, typename... Ts>
struct AnyOf<Pred, TypeList<Ts...>>
: std::bool_constant<(Pred<Ts>::value || ...)>
{};
template <template <typename> typename Pred, typename List>
inline constexpr bool any_of = AnyOf<Pred, List>::value;
template <template <typename> typename Pred, typename List>
inline constexpr bool none_of = !any_of<Pred, List>;
} // namespace Void::TypeList
+94
View File
@@ -0,0 +1,94 @@
#pragma once
#include <any>
#include <cstddef>
#include <optional>
#include <stdexcept>
#include <typeindex>
#include <unordered_map>
namespace Void::TypeMap
{
// ── TypeMap ───────────────────────────────────────────────────────────────────
// Heterogeneous map indexed by C++ type. Each type can hold at most one value.
// Values are type-erased internally; full type safety at the API level.
//
// TypeMap m;
// m.put<int>(42);
// m.put<std::string>("hello");
// int v = *m.get<int>(); // 42
// bool ok = m.contains<int>(); // true
class TypeMap
{
public:
// Store value of type T. Overwrites any previous value of the same type.
template <typename T>
void put(T value)
{
map_.insert_or_assign(std::type_index{typeid(T)}, std::any{std::move(value)});
}
// Retrieve value of type T, or nullopt if not stored.
template <typename T>
[[nodiscard]] std::optional<T> get() const
{
const auto it = map_.find(std::type_index{typeid(T)});
if (it == map_.end()) return std::nullopt;
return std::any_cast<T>(it->second);
}
// Get reference to value of type T. Throws std::out_of_range if absent.
template <typename T>
[[nodiscard]] const T& get_or_throw() const
{
const auto it = map_.find(std::type_index{typeid(T)});
if (it == map_.end()) throw std::out_of_range{"TypeMap: type not found"};
return std::any_cast<const T&>(it->second);
}
// Get value or a default if absent.
template <typename T>
[[nodiscard]] T get_or(T def) const
{
const auto it = map_.find(std::type_index{typeid(T)});
if (it == map_.end()) return def;
return std::any_cast<T>(it->second);
}
// Returns true if a value of type T is stored.
template <typename T>
[[nodiscard]] bool contains() const
{
return map_.count(std::type_index{typeid(T)}) > 0;
}
// Remove the value of type T. Returns true if something was erased.
template <typename T>
bool erase()
{
return map_.erase(std::type_index{typeid(T)}) > 0;
}
// Get-or-insert: if T is absent, construct it with args and store it.
template <typename T, typename... Args>
T& get_or_insert(Args&&... args)
{
const auto key = std::type_index{typeid(T)};
const auto it = map_.find(key);
if (it != map_.end()) return std::any_cast<T&>(it->second);
auto [ins, _] = map_.emplace(key, std::any{T{std::forward<Args>(args)...}});
return std::any_cast<T&>(ins->second);
}
void clear() noexcept { map_.clear(); }
[[nodiscard]] std::size_t size() const noexcept { return map_.size(); }
[[nodiscard]] bool empty() const noexcept { return map_.empty(); }
private:
std::unordered_map<std::type_index, std::any> map_;
};
} // namespace Void::TypeMap
+165
View File
@@ -0,0 +1,165 @@
#pragma once
#include <Void/Math/Random.hpp>
#include <Void/Core/Hash.hpp>
#include <array>
#include <cstdint>
#include <format>
#include <optional>
#include <string>
#include <string_view>
namespace Void::Uuid
{
// ── UUID type ─────────────────────────────────────────────────────────────────
struct Uuid
{
std::array<std::uint8_t, 16> bytes{};
[[nodiscard]] bool operator==(const Uuid&) const noexcept = default;
[[nodiscard]] auto operator<=>(const Uuid&) const noexcept = default;
[[nodiscard]] bool is_nil() const noexcept
{
for (auto b : bytes) {
if (b != 0) return false;
}
return true;
}
/// Format as lowercase 8-4-4-4-12 hex string.
[[nodiscard]] std::string to_string() const
{
return std::format(
"{:02x}{:02x}{:02x}{:02x}-"
"{:02x}{:02x}-"
"{:02x}{:02x}-"
"{:02x}{:02x}-"
"{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
bytes[0], bytes[1], bytes[2], bytes[3],
bytes[4], bytes[5],
bytes[6], bytes[7],
bytes[8], bytes[9],
bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15]);
}
[[nodiscard]] static Uuid nil() noexcept { return {}; }
};
// ── v4 (random) ───────────────────────────────────────────────────────────────
[[nodiscard]] inline Uuid v4() noexcept
{
Uuid u;
Random::fill_random(
{reinterpret_cast<std::byte*>(u.bytes.data()), u.bytes.size()});
// version 4
u.bytes[6] = (u.bytes[6] & 0x0F) | 0x40;
// variant 10xx (RFC 4122)
u.bytes[8] = (u.bytes[8] & 0x3F) | 0x80;
return u;
}
// ── v5 (name-based SHA-1 substitute using FNV-1a + CRC-32) ──────────────────
// Not cryptographically equivalent to SHA-1, but deterministic and collision-
// resistant enough for UUID namespacing without pulling in a crypto library.
[[nodiscard]] inline Uuid v5_lite(Uuid ns, std::string_view name) noexcept
{
Uuid u;
// Mix namespace bytes + name bytes using two orthogonal hash functions.
std::string blob;
blob.reserve(16 + name.size());
for (auto b : ns.bytes) blob += static_cast<char>(b);
blob += name;
const std::uint64_t h64 = Hash::fnv1a64(blob);
const std::uint32_t h32 = Hash::crc32(blob);
u.bytes[0] = static_cast<std::uint8_t>(h64 >> 56);
u.bytes[1] = static_cast<std::uint8_t>(h64 >> 48);
u.bytes[2] = static_cast<std::uint8_t>(h64 >> 40);
u.bytes[3] = static_cast<std::uint8_t>(h64 >> 32);
u.bytes[4] = static_cast<std::uint8_t>(h64 >> 24);
u.bytes[5] = static_cast<std::uint8_t>(h64 >> 16);
u.bytes[6] = static_cast<std::uint8_t>((h64 >> 8) & 0x0F) | 0x50; // version 5
u.bytes[7] = static_cast<std::uint8_t>(h64);
u.bytes[8] = static_cast<std::uint8_t>((h32 >> 24) & 0x3F) | 0x80; // variant
u.bytes[9] = static_cast<std::uint8_t>(h32 >> 16);
u.bytes[10] = static_cast<std::uint8_t>(h32 >> 8);
u.bytes[11] = static_cast<std::uint8_t>(h32);
// Refill bytes 12-15 by re-hashing
const std::uint32_t h32b = Hash::murmur3_32(name, static_cast<std::uint32_t>(h64));
u.bytes[12] = static_cast<std::uint8_t>(h32b >> 24);
u.bytes[13] = static_cast<std::uint8_t>(h32b >> 16);
u.bytes[14] = static_cast<std::uint8_t>(h32b >> 8);
u.bytes[15] = static_cast<std::uint8_t>(h32b);
return u;
}
// ── Parsing ───────────────────────────────────────────────────────────────────
[[nodiscard]] inline std::optional<Uuid> parse(std::string_view s) noexcept
{
// Accept "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" (36 chars) or 32 hex chars.
if (s.size() == 36 &&
s[8] == '-' && s[13] == '-' && s[18] == '-' && s[23] == '-') {
std::string flat;
flat.reserve(32);
for (std::size_t i = 0; i < s.size(); ++i) {
if (s[i] != '-') flat += s[i];
}
s = flat; // rebind for uniform parsing below
if (flat.size() != 32) return std::nullopt;
Uuid u;
for (std::size_t i = 0; i < 16; ++i) {
const char hi = flat[i * 2];
const char lo = flat[i * 2 + 1];
auto hex = [](char c) -> std::optional<std::uint8_t> {
if (c >= '0' && c <= '9') return static_cast<std::uint8_t>(c - '0');
if (c >= 'a' && c <= 'f') return static_cast<std::uint8_t>(c - 'a' + 10);
if (c >= 'A' && c <= 'F') return static_cast<std::uint8_t>(c - 'A' + 10);
return std::nullopt;
};
auto h = hex(hi), l = hex(lo);
if (!h || !l) return std::nullopt;
u.bytes[i] = static_cast<std::uint8_t>((*h << 4) | *l);
}
return u;
}
if (s.size() == 32) {
Uuid u;
for (std::size_t i = 0; i < 16; ++i) {
auto hex = [](char c) -> std::optional<std::uint8_t> {
if (c >= '0' && c <= '9') return static_cast<std::uint8_t>(c - '0');
if (c >= 'a' && c <= 'f') return static_cast<std::uint8_t>(c - 'a' + 10);
if (c >= 'A' && c <= 'F') return static_cast<std::uint8_t>(c - 'A' + 10);
return std::nullopt;
};
auto h = hex(s[i * 2]), l = hex(s[i * 2 + 1]);
if (!h || !l) return std::nullopt;
u.bytes[i] = static_cast<std::uint8_t>((*h << 4) | *l);
}
return u;
}
return std::nullopt;
}
// ── Well-known namespaces (RFC 4122) ─────────────────────────────────────────
namespace Namespace
{
// 6ba7b810-9dad-11d1-80b4-00c04fd430c8
inline constexpr Uuid Dns{{
0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1,
0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}};
// 6ba7b811-9dad-11d1-80b4-00c04fd430c8
inline constexpr Uuid Url{{
0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1,
0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}};
} // namespace Namespace
} // namespace Void::Uuid
+101
View File
@@ -0,0 +1,101 @@
#pragma once
#include <functional>
#include <optional>
#include <type_traits>
#include <utility>
#include <variant>
namespace Void::Variant
{
// ── overload ──────────────────────────────────────────────────────────────────
// Aggregate multiple callables into one visitor. Usage:
// std::visit(overload{[](int i){...}, [](std::string s){...}}, v);
template <typename... Fs>
struct overload : Fs... { using Fs::operator()...; };
// ── match ─────────────────────────────────────────────────────────────────────
// Apply visitor lambdas to a std::variant inline. Usage:
// match(v, [](int i){ return i; }, [](double d){ return (int)d; });
template <typename V, typename... Fs>
[[nodiscard]] decltype(auto) match(V&& v, Fs&&... fs)
{
return std::visit(overload<std::decay_t<Fs>...>{std::forward<Fs>(fs)...},
std::forward<V>(v));
}
// ── holds<T> ──────────────────────────────────────────────────────────────────
// Compile-time-named alternative to std::holds_alternative.
template <typename T, typename V>
[[nodiscard]] bool holds(const V& v) noexcept
{
return std::holds_alternative<T>(v);
}
// ── get_or<T> ─────────────────────────────────────────────────────────────────
// Returns the value if the variant holds T, otherwise returns default_val.
template <typename T, typename V>
[[nodiscard]] T get_or(const V& v, T default_val) noexcept
{
if (const auto* p = std::get_if<T>(&v)) return *p;
return default_val;
}
// ── try_get<T> ────────────────────────────────────────────────────────────────
// Returns optional<T> — present iff the variant holds T.
template <typename T, typename V>
[[nodiscard]] std::optional<T> try_get(const V& v) noexcept
{
if (const auto* p = std::get_if<T>(&v)) return *p;
return std::nullopt;
}
// ── index_of<T, V> ────────────────────────────────────────────────────────────
// Compile-time index of T within a std::variant<...> type V.
namespace Detail
{
template <typename T, typename V, std::size_t I = 0>
constexpr std::size_t variant_index()
{
if constexpr (I >= std::variant_size_v<V>) return std::variant_npos;
else if constexpr (std::is_same_v<T, std::variant_alternative_t<I, V>>) return I;
else return variant_index<T, V, I + 1>();
}
} // namespace Detail
template <typename T, typename V>
inline constexpr std::size_t index_of = Detail::variant_index<T, V>();
// ── apply_to<T> ───────────────────────────────────────────────────────────────
// Call fn only if the variant holds T. Returns optional of the result.
template <typename T, typename V, typename F>
[[nodiscard]] auto apply_to(const V& v, F&& fn)
-> std::optional<std::invoke_result_t<F, const T&>>
{
if (const auto* p = std::get_if<T>(&v))
return std::invoke(std::forward<F>(fn), *p);
return std::nullopt;
}
// ── narrow<U, V> ─────────────────────────────────────────────────────────────
// Map variant: if the active type is convertible to U, return U; else nullopt.
template <typename U, typename... Ts>
[[nodiscard]] std::optional<U> narrow(const std::variant<Ts...>& v)
{
return std::visit([](const auto& alt) -> std::optional<U> {
using T = std::decay_t<decltype(alt)>;
if constexpr (std::is_convertible_v<T, U>) return static_cast<U>(alt);
else return std::nullopt;
}, v);
}
} // namespace Void::Variant