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>
141 lines
4.5 KiB
C++
141 lines
4.5 KiB
C++
#pragma once
|
|
|
|
#include <Void/Functional/Either.hpp>
|
|
#include <functional>
|
|
#include <optional>
|
|
#include <stdexcept>
|
|
#include <type_traits>
|
|
#include <utility>
|
|
|
|
namespace Void::Either
|
|
{
|
|
|
|
// ── Opt<T> ────────────────────────────────────────────────────────────────────
|
|
// Fluent wrapper around std::optional with monadic chaining.
|
|
// Complements Either: use Opt when a value is simply present or absent,
|
|
// Either when absent carries a specific error.
|
|
|
|
template <typename T>
|
|
class Opt
|
|
{
|
|
public:
|
|
constexpr Opt() = default;
|
|
explicit constexpr Opt(std::optional<T> o) : inner_{std::move(o)} {}
|
|
explicit constexpr Opt(T v) : inner_{std::move(v)} {}
|
|
|
|
[[nodiscard]] static constexpr Opt none() { return Opt{std::optional<T>{}}; }
|
|
[[nodiscard]] static constexpr Opt some(T v) { return Opt{std::move(v)}; }
|
|
|
|
[[nodiscard]] constexpr bool has_value() const noexcept { return inner_.has_value(); }
|
|
[[nodiscard]] constexpr explicit operator bool() const noexcept { return has_value(); }
|
|
[[nodiscard]] constexpr const T& operator*() const & { return *inner_; }
|
|
[[nodiscard]] constexpr T& operator*() & { return *inner_; }
|
|
[[nodiscard]] constexpr T value_or(T def) const { return inner_.value_or(std::move(def)); }
|
|
|
|
template <typename F>
|
|
[[nodiscard]] constexpr auto map(F&& fn) const
|
|
-> Opt<std::invoke_result_t<F, const T&>>
|
|
{
|
|
using U = std::invoke_result_t<F, const T&>;
|
|
if (!inner_) return Opt<U>{};
|
|
return Opt<U>{std::invoke(std::forward<F>(fn), *inner_)};
|
|
}
|
|
|
|
template <typename F>
|
|
[[nodiscard]] constexpr auto flat_map(F&& fn) const
|
|
{
|
|
using Res = std::invoke_result_t<F, const T&>;
|
|
if (!inner_) return Res{};
|
|
return std::invoke(std::forward<F>(fn), *inner_);
|
|
}
|
|
|
|
template <typename Pred>
|
|
[[nodiscard]] constexpr Opt filter(Pred&& pred) const
|
|
{
|
|
if (inner_ && std::invoke(std::forward<Pred>(pred), *inner_)) return *this;
|
|
return none();
|
|
}
|
|
|
|
template <typename F>
|
|
[[nodiscard]] constexpr Opt or_else(F&& fn) const
|
|
{
|
|
if (inner_) return *this;
|
|
return std::invoke(std::forward<F>(fn));
|
|
}
|
|
|
|
// Convert to Either: Some(v) → Right(v), None → Left(err).
|
|
template <typename L>
|
|
[[nodiscard]] Either<L, T> ok_or(L err) const
|
|
{
|
|
if (inner_) return Either<L, T>::right(*inner_);
|
|
return Either<L, T>::left(std::move(err));
|
|
}
|
|
|
|
[[nodiscard]] constexpr std::optional<T> to_optional() const { return inner_; }
|
|
|
|
private:
|
|
std::optional<T> inner_;
|
|
};
|
|
|
|
template <typename T>
|
|
[[nodiscard]] constexpr Opt<std::decay_t<T>> some(T&& v)
|
|
{
|
|
return Opt<std::decay_t<T>>::some(std::forward<T>(v));
|
|
}
|
|
|
|
template <typename T>
|
|
[[nodiscard]] constexpr Opt<T> none()
|
|
{
|
|
return Opt<T>::none();
|
|
}
|
|
|
|
// ── free optional utilities ───────────────────────────────────────────────────
|
|
// Operate on std::optional directly without the Opt<T> wrapper.
|
|
|
|
template <typename T, typename F>
|
|
[[nodiscard]] constexpr auto opt_transform(std::optional<T> opt, F&& fn)
|
|
-> std::optional<std::invoke_result_t<F, T>>
|
|
{
|
|
if (!opt) return std::nullopt;
|
|
return std::invoke(std::forward<F>(fn), *std::move(opt));
|
|
}
|
|
|
|
template <typename T, typename F>
|
|
[[nodiscard]] constexpr auto opt_and_then(std::optional<T> opt, F&& fn)
|
|
{
|
|
if (!opt) return std::invoke_result_t<F, T>{};
|
|
return std::invoke(std::forward<F>(fn), *std::move(opt));
|
|
}
|
|
|
|
template <typename T, typename Pred>
|
|
[[nodiscard]] constexpr std::optional<T> opt_filter(std::optional<T> opt, Pred&& pred)
|
|
{
|
|
if (opt && std::invoke(std::forward<Pred>(pred), *opt)) return opt;
|
|
return std::nullopt;
|
|
}
|
|
|
|
template <typename T>
|
|
[[nodiscard]] const T& opt_expect(const std::optional<T>& opt,
|
|
const char* msg = "optional is empty")
|
|
{
|
|
if (!opt) throw std::runtime_error{msg};
|
|
return *opt;
|
|
}
|
|
|
|
template <typename T, typename U>
|
|
[[nodiscard]] constexpr std::optional<std::pair<T, U>>
|
|
opt_zip(const std::optional<T>& a, const std::optional<U>& b)
|
|
{
|
|
if (a && b) return std::pair<T, U>{*a, *b};
|
|
return std::nullopt;
|
|
}
|
|
|
|
template <typename T>
|
|
[[nodiscard]] constexpr std::optional<T> opt_flatten(std::optional<std::optional<T>> opt)
|
|
{
|
|
if (!opt || !*opt) return std::nullopt;
|
|
return **opt;
|
|
}
|
|
|
|
} // namespace Void::Either
|