Files
Library/include/Void/Core/Variant.hpp
T
faumarayandClaude Fable 5 28b94883fd 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>
2026-06-12 18:50:02 +03:00

102 lines
4.3 KiB
C++

#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