uvco 0.1
Loading...
Searching...
No Matches
Combinators

Namespaces

namespace  uvco::detail

Classes

class  uvco::WaitPoint
class  uvco::TaskSet

Functions

Promise< void > uvco::yield ()
MultiPromise< unsigned > uvco::yield (unsigned count)
 Generate count values from 0 to count - 1.
template<typename... PromiseTypes>
Promise< std::vector< std::variant< PromiseTypes... > > > uvco::waitAny (Promise< PromiseTypes > &...promises)
template<typename... PromiseTypes>
Promise< std::vector< std::variant< PromiseTypes... > > > uvco::race (Promise< PromiseTypes >... promises)
template<typename... PromiseTypes>
Promise< void > uvco::raceIgnore (Promise< PromiseTypes >... promises)
template<typename... PromiseTypes>
Promise< std::tuple< typename detail::ReplaceVoid< PromiseTypes >::type... > > uvco::waitAll (Promise< PromiseTypes >... promises)

Detailed Description

Functions and classes useful to combine promises and generators into higher-level items.

Function Documentation

◆ race()

template<typename... PromiseTypes>
Promise< std::vector< std::variant< PromiseTypes... > > > uvco::race ( Promise< PromiseTypes >... promises)

Like waitAny, but cancels all promises that were not ready in time. Returns a vector of the results that were ready first; most frequently, only one element will be set.

61 {
62 co_return (co_await waitAny(promises...));
63}
Promise< std::vector< std::variant< PromiseTypes... > > > waitAny(Promise< PromiseTypes > &...promises)
Definition combinators.h:43

◆ raceIgnore()

template<typename... PromiseTypes>
Promise< void > uvco::raceIgnore ( Promise< PromiseTypes >... promises)

Wait on any promise to be ready, ignoring results. When any promise is ready, all others are dropped and cancelled. This is very useful to run background promises concurrently, stopping all of them once one of them finishes or fails.

70 {
71 using S = SelectSet<PromiseTypes...>;
72 S selectSet{promises...};
73 co_await selectSet;
74}
Definition select.h:56

◆ waitAll()

template<typename... PromiseTypes>
Promise< std::tuple< typename detail::ReplaceVoid< PromiseTypes >::type... > > uvco::waitAll ( Promise< PromiseTypes >... promises)

Wait for all promises to finish, returning all results in a tuple. In the returned tuple, any Promise<void> is represented by a Void struct, which is just an empty struct.

106 {
107 co_return std::make_tuple(co_await detail::awaitAndReplaceVoid(promises)...);
108}
Promise< typename ReplaceVoid< PromiseType >::type > awaitAndReplaceVoid(Promise< PromiseType > &promise)
Definition combinators.h:90

◆ waitAny()

template<typename... PromiseTypes>
Promise< std::vector< std::variant< PromiseTypes... > > > uvco::waitAny ( Promise< PromiseTypes > &... promises)

Wait on any of the promises to be ready. Returns a vector of variants of possible values; one for each supplied promise that became ready. Non-ready promises are not cancelled.

This can be called repeatedly to wait until all promises are ready.

waitAny() is only a wrapper around SelectSet.

43 {
44 using S = SelectSet<PromiseTypes...>;
45 using V = std::variant<PromiseTypes...>;
46 S selectSet{promises...};
47 auto readyPromises = co_await selectSet;
48 std::vector<V> results;
49 for (auto &promise : readyPromises) {
50 results.emplace_back(std::visit([](auto *p) -> V { return p->unwrap(); },
51 std::move(promise)));
52 }
53 co_return results;
54}

◆ yield() [1/2]

Promise< void > uvco::yield ( )

Suspend current coroutine until next event loop iteration. This is especially useful when running expensive computations during which I/O should still happen to avoid starving other tasks; or as replacement for short-duration sleeps.

34{ co_await YieldAwaiter_{}; }

◆ yield() [2/2]

MultiPromise< unsigned > uvco::yield ( unsigned count)

Generate count values from 0 to count - 1.

36 {
37 for (unsigned i = 0; i < count; ++i) {
38 co_yield i;
39 }
40}