COROIO: coroio/corochain.hpp File Reference
COROIO
 
Loading...
Searching...
No Matches
corochain.hpp File Reference

Implementation of a promise/future system for coroutines. More...

#include <coroutine>
#include <optional>
#include <expected>
#include <memory>
#include <functional>
#include <exception>
#include <utility>
#include "promises.hpp"
#include "poller.hpp"

Go to the source code of this file.

Classes

struct  NNet::TPromiseBase< T >
 Base promise type for coroutines. More...
 
struct  NNet::TPromise< T >
 Promise for coroutines that return a value of type T. More...
 
struct  NNet::TFutureBase< T >
 Base future type for coroutines. More...
 
struct  NNet::TFuture< T >
 Owned coroutine handle that carries a result of type T. More...
 
struct  NNet::TPromise< void >
 Promise specialization for coroutines that return void. More...
 
struct  NNet::TFuture< void >
 Owned coroutine handle for coroutines that return void. More...
 
struct  NNet::TFinalAwaiter< T >
 Final awaiter for a coroutine. More...
 

Functions

template<typename T >
TFuture< std::vector< T > > NNet::All (std::vector< TFuture< T > > &&futures)
 Awaits every future in order and collects their results.
 
TFuture< void > NNet::All (std::vector< TFuture< void > > &&futures)
 Awaits every void future in order until all have completed.
 
template<typename T >
TFuture< T > NNet::Any (std::vector< TFuture< T > > &&futures)
 Returns the result of whichever future completes first.
 
TFuture< void > NNet::Any (std::vector< TFuture< void > > &&futures)
 Completes when the first void future finishes; others are abandoned.
 

Detailed Description

Implementation of a promise/future system for coroutines.

This file defines the promise and future types used to manage coroutine execution. It provides mechanisms to retrieve coroutine results (or exceptions) and to coordinate multiple asynchronous operations.

Example Usage

#include <vector>
#include <iostream>
using namespace NNet;
// A coroutine that returns an integer.
TFuture<int> getInt() {
co_return 42;
}
// A coroutine that returns void.
TFuture<void> doSomething() {
// Perform some work...
co_return;
}
// A coroutine that waits for all futures to complete.
TFuture<void> testAll() {
std::vector<TFuture<int>> futures;
futures.push_back(getInt());
futures.push_back(getInt());
auto results = co_await All(std::move(futures));
// Process results...
for (auto res : results) {
std::cout << "Result: " << res << std::endl;
}
co_return;
}
// A coroutine that waits for any future to complete.
TFuture<int> testAny() {
std::vector<TFuture<int>> futures;
futures.push_back(getInt());
futures.push_back(getInt());
int result = co_await Any(std::move(futures));
co_return result;
}
Implementation of a promise/future system for coroutines.
TFuture< T > Any(std::vector< TFuture< T > > &&futures)
Returns the result of whichever future completes first.
Definition corochain.hpp:353
TFuture< std::vector< T > > All(std::vector< TFuture< T > > &&futures)
Awaits every future in order and collects their results.
Definition corochain.hpp:317
Owned coroutine handle that carries a result of type T.
Definition corochain.hpp:185

Function Documentation

◆ All() [1/2]

template<typename T >
TFuture< std::vector< T > > NNet::All ( std::vector< TFuture< T > > &&  futures)

Awaits every future in order and collects their results.

Futures are co_await-ed sequentially (not concurrently). The caller suspends until each future finishes before moving to the next.

Template Parameters
TThe type of each coroutine's result.
Parameters
futuresA vector of TFuture<T> objects (moved in).
Returns
TFuture<std::vector<T>> containing results in input order.

◆ All() [2/2]

TFuture< void > NNet::All ( std::vector< TFuture< void > > &&  futures)
inline

Awaits every void future in order until all have completed.

Parameters
futuresA vector of TFuture<void> objects (moved in).
Returns
TFuture<void> that completes after all futures finish.

◆ Any() [1/2]

template<typename T >
TFuture< T > NNet::Any ( std::vector< TFuture< T > > &&  futures)

Returns the result of whichever future completes first.

If one future is already done, its result is returned without suspension. Otherwise, all futures register this coroutine as their continuation; when the first one resumes it, the remaining futures are abandoned (their frames are destroyed when the vector goes out of scope).

Template Parameters
TThe type of each coroutine's result.
Parameters
futuresA vector of TFuture<T> objects (moved in).
Returns
TFuture<T> with the result from the first completed future.

◆ Any() [2/2]

TFuture< void > NNet::Any ( std::vector< TFuture< void > > &&  futures)
inline

Completes when the first void future finishes; others are abandoned.

Parameters
futuresA vector of TFuture<void> objects (moved in).
Returns
TFuture<void> that completes as soon as one future finishes.