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>
114 lines
3.3 KiB
C++
114 lines
3.3 KiB
C++
#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
|