#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 #include #include #include namespace molo::clients { template class Task { public: struct promise_type { std::optional value; std::exception_ptr error; Task get_return_object() { return Task{std::coroutine_handle::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; 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 { public: struct promise_type { std::exception_ptr error; Task get_return_object() { return Task{std::coroutine_handle::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; 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 T block_on(Task task) { return task.get(); } inline void block_on(Task task) { task.get(); } } // namespace molo::clients