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 <variant>
#include <memory>
#include <functional>
#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 >
 Future type for coroutines returning a value of type T. More...
 
struct  NNet::TPromise< void >
 Promise specialization for coroutines that return void. More...
 
struct  NNet::TFuture< void >
 Future specialization 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 the completion of all futures and collects their results.
 
TFuture< void > NNet::All (std::vector< TFuture< void > > &&futures)
 Awaits the completion of all void-returning coroutines.
 
template<typename T>
TFuture< T > NNet::Any (std::vector< TFuture< T > > &&futures)
 Awaits the completion of any one of the given futures and returns its result.
 
TFuture< void > NNet::Any (std::vector< TFuture< void > > &&futures)
 Awaits the completion of any one of the void-returning coroutines.
 

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)
Awaits the completion of any one of the given futures and returns its result.
Definition corochain.hpp:336
TFuture< std::vector< T > > All(std::vector< TFuture< T > > &&futures)
Awaits the completion of all futures and collects their results.
Definition corochain.hpp:302
Future type for coroutines returning a value of type T.
Definition corochain.hpp:177

Function Documentation

◆ All() [1/2]

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

Awaits the completion of all futures and collects their results.

Template Parameters
TThe type of each coroutine's result.
Parameters
futuresA vector of TFuture<T> objects.
Returns
TFuture<std::vector<T>> containing the results from all coroutines.

◆ All() [2/2]

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

Awaits the completion of all void-returning coroutines.

Parameters
futuresA vector of TFuture<void> objects.
Returns
TFuture<void> representing the completion of all coroutines.

◆ Any() [1/2]

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

Awaits the completion of any one of the given futures and returns its result.

If one of the futures has already finished, its result is returned immediately. Otherwise, the current coroutine is suspended until one of the futures completes.

Template Parameters
TThe type of the coroutine's result.
Parameters
futuresA vector of TFuture<T> objects.
Returns
TFuture<T> with the result from the first completed coroutine.

◆ Any() [2/2]

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

Awaits the completion of any one of the void-returning coroutines.

Parameters
futuresA vector of TFuture<void> objects.
Returns
TFuture<void> representing the completion of one of the coroutines.