|
morph 0.1.0
Asynchronous bridge between GUI thread and business models
|
Move-only handle representing the eventual result of an asynchronous operation. More...
#include <completion.hpp>
Classes | |
| class | Promise |
| Producer-side handle paired with a Completion<T> by makeSettleable(). More... | |
Public Member Functions | |
| Completion ()=default | |
| Constructs an empty (no-op) completion. | |
| Completion (std::shared_ptr< detail::CompletionState< T > > statePtr, ::morph::exec::IExecutor *execPtr) | |
Constructs a completion backed by statePtr, delivering callbacks via execPtr. | |
| Completion (Completion &&) noexcept=default | |
| Move constructor — transfers ownership of the underlying state. | |
| Completion & | operator= (Completion &&) noexcept=default |
| Move assignment — transfers ownership of the underlying state. | |
| Completion (const Completion &)=delete | |
| Completion & | operator= (const Completion &)=delete |
| Completion & | then (std::function< void(const T &)> handler) |
| Registers a success callback. | |
| Completion & | onError (std::function< void(std::exception_ptr)> handler) |
| Registers an error callback. | |
| Completion & | then (const CallbackScope &scope, std::function< void(const T &)> handler) |
Registers a success callback gated on scope's lifetime and stop state. | |
| Completion & | then (CallbackToken token, std::function< void(const T &)> handler) |
Registers a success callback gated on an already-issued token. | |
| Completion & | onError (const CallbackScope &scope, std::function< void(std::exception_ptr)> handler) |
Registers an error callback gated on scope's lifetime and stop state. | |
| Completion & | onError (CallbackToken token, std::function< void(std::exception_ptr)> handler) |
Registers an error callback gated on an already-issued token. | |
| Completion & | thenDetached (std::function< void(const T &)> handler) |
| Registers a success callback whose lifetime is deliberately unmanaged. | |
| Completion & | onErrorDetached (std::function< void(std::exception_ptr)> handler) |
| Registers an error callback whose lifetime is deliberately unmanaged. | |
| detail::CompletionAwaiter< T > | operator co_await () && |
| Awaits this completion from a coroutine, consuming it. | |
| std::shared_ptr< detail::CompletionState< T > > | state () const |
| Returns the underlying shared state (for advanced / internal use). | |
Static Public Member Functions | |
| static std::pair< Completion< T >, Promise > | makeSettleable (::morph::exec::IExecutor *execPtr) |
| Constructs a Completion<T>/Promise<T> pair sharing one settleable state. | |
Move-only handle representing the eventual result of an asynchronous operation.
Callbacks are posted to the IExecutor supplied at construction time, so they always run on the intended thread (e.g. the GUI thread).
The budget does not depend on how many handlers are attached, nor on whether they attached before or after the completion settled. The value is observed, never consumed: no handler can move out of the stored value, so a then() attached after settling still sees the genuine result. A handler that wants to consume takes T by value and moves out of its own copy. See docs/spec/core/completion.md, "Value-handling contract".
| T | Type of the success value. Must be move-constructible. |
Definition at line 466 of file completion.hpp.
|
inline |
Constructs a completion backed by statePtr, delivering callbacks via execPtr.
| statePtr | Shared state produced by the backend. |
| execPtr | The completion's owner: settling posts the delivery there, and callbacks are attached and run there. It must be serial (IExecutor::isSerial): a pool or the inline executor is refused, asserted in a debug build. If nullptr, callbacks are never delivered — attaching one is a no-op. |
Definition at line 478 of file completion.hpp.
|
defaultnoexcept |
Move assignment — transfers ownership of the underlying state.
|
inline |
Registers a success callback.
handler runs on the executor with the result value when the operation completes successfully. If the result has already been delivered, the callback is posted on its own. Call on the owner (see the class's thread-safety note).
| handler | Callable receiving the result. Take const T& to observe it for free; take T by value to get your own copy, at the cost of exactly one copy. |
Definition at line 512 of file completion.hpp.
|
inline |
Registers an error callback.
handler runs on the executor with the std::exception_ptr when the operation fails. If the failure has already been delivered, the callback is posted on its own. Attaching this handler suppresses orphan logging. Call on the owner (see the class's thread-safety note).
| handler | Callable receiving the exception pointer. |
Definition at line 529 of file completion.hpp.
|
inline |
Registers a success callback gated on scope's lifetime and stop state.
Identical to then(handler) except that handler is wrapped in a CallbackToken gate at attach time: when the result is delivered, the handler runs only if scope is still alive and has not been stopped or reset(). Nothing else changes — the gate lives inside the stored handler, so fan-out, the attach-after-ready fire-now path and executor marshalling all behave exactly as they do for the ungated form.
Attaching also makes the call this completion reports on one the scope owns: when the call carries a stop source (a Bridge call whose handler returns a Task), stopping, resetting or destroying the scope before it settles requests stop on it.
The scope is observed weakly; attaching does not extend its lifetime.
| scope | Receiver-owned gate. Only a token for its current generation is captured, so a later reset() retires this attachment. |
| handler | Callable receiving the result. Take const T& to observe it for free; take T by value to get your own copy, at the cost of exactly one copy. |
Definition at line 559 of file completion.hpp.
|
inline |
Registers a success callback gated on an already-issued token.
The token-taking form of then(const CallbackScope&, handler), for callers that hold a token rather than the scope itself (a helper that was handed one, or code that captured a token before dispatching).
| token | Gate observing some receiver's CallbackScope. A default-constructed token suppresses unconditionally. |
| handler | Callable receiving the result. Take const T& to observe it for free; take T by value to get your own copy, at the cost of exactly one copy. |
Definition at line 575 of file completion.hpp.
|
inline |
Registers an error callback gated on scope's lifetime and stop state.
Identical to onError(handler) except that handler is wrapped in a CallbackToken gate at attach time.
Orphan logging is unaffected: attaching a gated handler suppresses it exactly as the ungated form does, and an error whose delivery the scope then refuses still counts as handled. Suppression is a deliberate act by the receiver, not a dropped error nobody asked about.
| scope | Receiver-owned gate; observed weakly. |
| handler | Callable receiving the exception pointer. |
Definition at line 596 of file completion.hpp.
|
inline |
Registers an error callback gated on an already-issued token.
The token-taking form of onError(const CallbackScope&, handler).
| token | Gate observing some receiver's CallbackScope. A default-constructed token suppresses unconditionally. |
| handler | Callable receiving the exception pointer. |
Definition at line 610 of file completion.hpp.
|
inline |
Registers a success callback whose lifetime is deliberately unmanaged.
Exactly then(handler), spelled so that "no scope gates this" is a statement the author made on purpose and a reviewer can grep for. Use it when the handler genuinely owns everything it touches — it captures only values, or a shared_ptr it keeps alive itself.
| handler | Callable receiving the result. Take const T& to observe it for free; take T by value to get your own copy, at the cost of exactly one copy. |
Definition at line 628 of file completion.hpp.
|
inline |
Registers an error callback whose lifetime is deliberately unmanaged.
The onError counterpart of thenDetached().
| handler | Callable receiving the exception pointer. |
Definition at line 639 of file completion.hpp.
|
inlinenodiscard |
Awaits this completion from a coroutine, consuming it.
co_await std::move(completion) yields a copy of the settled value or rethrows the stored exception. The coroutine resumes in the resumption context it suspended in – a spawned task's executor, a Task handler's strand – or, with none, on this completion's executor, where then() handlers run. A stop requested on the awaiting coroutine's token withdraws the await and resumes it with core::async::OperationCancelled. Rvalue only: awaiting moves the state out, so an lvalue co_await would hide that the completion is empty afterwards. See docs/spec/core/coroutines.md.
Definition at line 655 of file completion.hpp.
|
inlinenodiscard |
Returns the underlying shared state (for advanced / internal use).
Definition at line 664 of file completion.hpp.
|
inlinestaticnodiscard |
Constructs a Completion<T>/Promise<T> pair sharing one settleable state.
The public "settleable promise" seam: lets a caller — typically test code — construct a Completion<T> it can resolve or reject on demand, without a full Bridge/IBackend round trip and without reaching into morph::async::detail::CompletionState<T>. Everything Completion(state, executor) already provided by hand is available through this factory instead: the returned Completion<T> is exactly what then()/onError() observe; the returned Promise is exactly what settles it.
| execPtr | Executor callbacks are posted on; nullptr for a write-only completion (see the two-argument constructor). |
Definition at line 738 of file completion.hpp.