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:
1 parent
da2e93c405
commit
28b94883fd
156 files changed
+278
-220
No files matched your search
@@ -0,0 +1,195 @@
|
||||
#pragma once
|
||||
|
||||
#include <coroutine>
|
||||
#include <exception>
|
||||
#include <optional>
|
||||
#include <utility>
|
||||
|
||||
namespace Void::Coro
|
||||
{
|
||||
|
||||
// ── Task<T> ───────────────────────────────────────────────────────────────────
|
||||
// A lazy coroutine that produces a single value via co_return.
|
||||
// The coroutine does NOT run until explicitly driven (run() or sync_wait()).
|
||||
//
|
||||
// Awaiting a Task<T> from inside another Task<T> is supported:
|
||||
// Task<int> inner() { co_return 42; }
|
||||
// Task<int> outer() { co_return co_await inner() + 1; }
|
||||
//
|
||||
// Task is NOT thread-safe and uses no scheduler. Everything runs on the
|
||||
// caller's thread synchronously when sync_wait() is called.
|
||||
|
||||
template <typename T>
|
||||
class Task
|
||||
{
|
||||
public:
|
||||
struct promise_type
|
||||
{
|
||||
std::optional<T> result;
|
||||
std::exception_ptr exception;
|
||||
std::coroutine_handle<> continuation; // parent waiting on this task
|
||||
|
||||
Task get_return_object()
|
||||
{
|
||||
return Task{std::coroutine_handle<promise_type>::from_promise(*this)};
|
||||
}
|
||||
|
||||
std::suspend_always initial_suspend() noexcept { return {}; }
|
||||
|
||||
struct FinalAwaiter
|
||||
{
|
||||
bool await_ready() noexcept { return false; }
|
||||
void await_resume() noexcept {}
|
||||
std::coroutine_handle<> await_suspend(
|
||||
std::coroutine_handle<promise_type> h) noexcept
|
||||
{
|
||||
// Resume parent if it was awaiting us; otherwise just stop.
|
||||
auto cont = h.promise().continuation;
|
||||
return cont ? cont : std::noop_coroutine();
|
||||
}
|
||||
};
|
||||
|
||||
FinalAwaiter final_suspend() noexcept { return {}; }
|
||||
|
||||
void return_value(T v) { result.emplace(std::move(v)); }
|
||||
void unhandled_exception() { exception = std::current_exception(); }
|
||||
};
|
||||
|
||||
using handle_type = std::coroutine_handle<promise_type>;
|
||||
|
||||
// ── construction ──────────────────────────────────────────────────────────
|
||||
|
||||
explicit Task(handle_type h) noexcept : handle_{h} {}
|
||||
|
||||
Task(const Task&) = delete;
|
||||
Task& operator=(const Task&) = delete;
|
||||
|
||||
Task(Task&& o) noexcept : handle_{std::exchange(o.handle_, {})} {}
|
||||
Task& operator=(Task&& o) noexcept
|
||||
{
|
||||
if (this != &o) {
|
||||
if (handle_) handle_.destroy();
|
||||
handle_ = std::exchange(o.handle_, {});
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
~Task() { if (handle_) handle_.destroy(); }
|
||||
|
||||
[[nodiscard]] bool done() const noexcept { return !handle_ || handle_.done(); }
|
||||
|
||||
// ── co_await support (allows Task<T> to be awaited by another Task) ───────
|
||||
|
||||
[[nodiscard]] bool await_ready() const noexcept { return false; }
|
||||
|
||||
std::coroutine_handle<> await_suspend(std::coroutine_handle<> continuation) noexcept
|
||||
{
|
||||
handle_.promise().continuation = continuation;
|
||||
return handle_; // resume this task's coroutine
|
||||
}
|
||||
|
||||
T await_resume()
|
||||
{
|
||||
auto& p = handle_.promise();
|
||||
if (p.exception) std::rethrow_exception(p.exception);
|
||||
return std::move(*p.result);
|
||||
}
|
||||
|
||||
// Synchronous execution — runs the coroutine to completion on the
|
||||
// calling thread. Returns the result or rethrows any stored exception.
|
||||
T run()
|
||||
{
|
||||
handle_.resume();
|
||||
return await_resume();
|
||||
}
|
||||
|
||||
private:
|
||||
handle_type handle_;
|
||||
};
|
||||
|
||||
// ── Task<void> ────────────────────────────────────────────────────────────────
|
||||
|
||||
template <>
|
||||
class Task<void>
|
||||
{
|
||||
public:
|
||||
struct promise_type
|
||||
{
|
||||
std::exception_ptr exception;
|
||||
std::coroutine_handle<> continuation;
|
||||
|
||||
Task get_return_object()
|
||||
{
|
||||
return Task{std::coroutine_handle<promise_type>::from_promise(*this)};
|
||||
}
|
||||
|
||||
std::suspend_always initial_suspend() noexcept { return {}; }
|
||||
|
||||
struct FinalAwaiter
|
||||
{
|
||||
bool await_ready() noexcept { return false; }
|
||||
void await_resume() noexcept {}
|
||||
std::coroutine_handle<> await_suspend(
|
||||
std::coroutine_handle<promise_type> h) noexcept
|
||||
{
|
||||
auto cont = h.promise().continuation;
|
||||
return cont ? cont : std::noop_coroutine();
|
||||
}
|
||||
};
|
||||
|
||||
FinalAwaiter final_suspend() noexcept { return {}; }
|
||||
|
||||
void return_void() noexcept {}
|
||||
void unhandled_exception() { exception = std::current_exception(); }
|
||||
};
|
||||
|
||||
using handle_type = std::coroutine_handle<promise_type>;
|
||||
|
||||
explicit Task(handle_type h) noexcept : handle_{h} {}
|
||||
|
||||
Task(const Task&) = delete;
|
||||
Task& operator=(const Task&) = delete;
|
||||
Task(Task&& o) noexcept : handle_{std::exchange(o.handle_, {})} {}
|
||||
Task& operator=(Task&& o) noexcept
|
||||
{
|
||||
if (this != &o) {
|
||||
if (handle_) handle_.destroy();
|
||||
handle_ = std::exchange(o.handle_, {});
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
~Task() { if (handle_) handle_.destroy(); }
|
||||
|
||||
[[nodiscard]] bool done() const noexcept { return !handle_ || handle_.done(); }
|
||||
|
||||
[[nodiscard]] bool await_ready() const noexcept { return false; }
|
||||
std::coroutine_handle<> await_suspend(std::coroutine_handle<> c) noexcept
|
||||
{
|
||||
handle_.promise().continuation = c;
|
||||
return handle_;
|
||||
}
|
||||
void await_resume()
|
||||
{
|
||||
if (handle_.promise().exception)
|
||||
std::rethrow_exception(handle_.promise().exception);
|
||||
}
|
||||
|
||||
void run()
|
||||
{
|
||||
handle_.resume();
|
||||
await_resume();
|
||||
}
|
||||
|
||||
private:
|
||||
handle_type handle_;
|
||||
};
|
||||
|
||||
// ── sync_wait — drive a Task<T> to completion synchronously ──────────────────
|
||||
|
||||
template <typename T>
|
||||
T sync_wait(Task<T> task) { return task.run(); }
|
||||
|
||||
inline void sync_wait(Task<void> task) { task.run(); }
|
||||
|
||||
} // namespace Void::Coro
|
||||
Reference in new issue
Block a user