Files
moloapp/src/clients/task.h
T
retoor 3c4334cec9 feat: implement MoloApp plans 01-15 with integration
Full C++23 framework tree: runtime, observability, security,
data, app, realtime, supervisor, clients, CLI, example plus
enterprise tenancy, audit, operability, governance,
supplychain. Postgres via docker compose for tests.
2026-10-23 18:20:04 +02:00

128 lines
3.5 KiB
C++

#pragma once
// Minimal eager coroutine task. Coroutine forms are the primary client API;
// sync `_blocking` forms share the implementation through block_on().
// Tasks run eagerly on the calling thread; suspension points are reserved
// for integration with the part 01 runtime loop.
#include <coroutine>
#include <exception>
#include <optional>
#include <utility>
namespace molo::clients {
template <typename T>
class Task {
public:
struct promise_type {
std::optional<T> value;
std::exception_ptr error;
Task get_return_object() {
return Task{std::coroutine_handle<promise_type>::from_promise(*this)};
}
std::suspend_never initial_suspend() noexcept { return {}; }
std::suspend_always final_suspend() noexcept { return {}; }
void return_value(T v) { value.emplace(std::move(v)); }
void unhandled_exception() { error = std::current_exception(); }
};
using Handle = std::coroutine_handle<promise_type>;
Task(const Task&) = delete;
Task& operator=(const Task&) = delete;
Task(Task&& other) noexcept : handle_(other.handle_) {
other.handle_ = nullptr;
}
Task& operator=(Task&& other) noexcept {
if (this != &other) {
destroy();
handle_ = other.handle_;
other.handle_ = nullptr;
}
return *this;
}
~Task() { destroy(); }
// Awaitable interface (already complete: eager evaluation).
bool await_ready() const noexcept { return true; }
void await_suspend(std::coroutine_handle<>) noexcept {}
T await_resume() { return get(); }
T get() {
if (!handle_) throw std::logic_error("molo::clients::Task: empty task");
auto& promise = handle_.promise();
if (promise.error) std::rethrow_exception(promise.error);
if (!promise.value) throw std::logic_error("molo::clients::Task: no value");
return std::move(*promise.value);
}
private:
explicit Task(Handle h) : handle_(h) {}
void destroy() {
if (handle_) handle_.destroy();
handle_ = nullptr;
}
Handle handle_{nullptr};
};
template <>
class Task<void> {
public:
struct promise_type {
std::exception_ptr error;
Task get_return_object() {
return Task{std::coroutine_handle<promise_type>::from_promise(*this)};
}
std::suspend_never initial_suspend() noexcept { return {}; }
std::suspend_always final_suspend() noexcept { return {}; }
void return_void() noexcept {}
void unhandled_exception() { error = std::current_exception(); }
};
using Handle = std::coroutine_handle<promise_type>;
Task(const Task&) = delete;
Task& operator=(const Task&) = delete;
Task(Task&& other) noexcept : handle_(other.handle_) {
other.handle_ = nullptr;
}
Task& operator=(Task&& other) noexcept {
if (this != &other) {
destroy();
handle_ = other.handle_;
other.handle_ = nullptr;
}
return *this;
}
~Task() { destroy(); }
bool await_ready() const noexcept { return true; }
void await_suspend(std::coroutine_handle<>) noexcept {}
void await_resume() { get(); }
void get() {
if (!handle_) throw std::logic_error("molo::clients::Task: empty task");
if (handle_.promise().error)
std::rethrow_exception(handle_.promise().error);
}
private:
explicit Task(Handle h) : handle_(h) {}
void destroy() {
if (handle_) handle_.destroy();
handle_ = nullptr;
}
Handle handle_{nullptr};
};
// Blocking bridge for CLI use. Shares the coroutine implementation.
template <typename T>
T block_on(Task<T> task) {
return task.get();
}
inline void block_on(Task<void> task) { task.get(); }
} // namespace molo::clients