CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
Async.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
12
13#include <continuable/continuable.hpp>
14#include <continuable/external/asio.hpp>
15
16namespace CeresEngine {
17
22 template<typename... Args> using Async = cti::continuable<Args...>;
23
28 template<typename... Args> using Promise = cti::promise<Args...>;
29
51 template<typename... Args> using AsyncResult = cti::result<Args...>;
52
121 template<typename... Args, typename Continuation> constexpr auto async(Continuation&& continuation) {
122 return cti::make_continuable<Args...>(std::forward<Continuation>(continuation));
123 }
124
135 template<typename... Args> auto async_ready(Args&&... args) { return cti::make_ready_continuable<Args...>(std::forward<Args>(args)...); }
136
150 template<typename... Args, typename Exception> constexpr auto async_except(Exception&& exception) {
151 return cti::make_exceptional_continuable<Args...>(std::forward<Exception>(exception));
152 }
153
154 // ---------------------------------------------------------------------------------------------
155
156 using cti::populate;
157 using cti::promisify;
158 using cti::when_all;
159 using cti::when_any;
160 using cti::when_seq;
161
162
191 template <typename Mapper, typename... T> inline decltype(auto) async_map(Mapper&& mapper, T&&... pack) {
192 return cti::map_pack(std::forward<Mapper>(mapper), std::forward<T>(pack)...);
193 }
194
198 template <typename... T> constexpr decltype(auto) async_spread(T&&... args) {
199 return cti::spread_this(std::forward<T>(args)...);
200 }
201
208 template <typename Mapper, typename... T>
210 return cti::traverse_pack(std::forward<Mapper>(mapper), std::forward<T>(pack)...);
211 }
212
213 // ---------------------------------------------------------------------------------------------
214
215 template<typename Data, typename Annotation, typename Transform>
216 static decltype(auto) operator>>(cti::continuable_base<Data, Annotation>&& continuable, Transform&& transform) {
217 return std::forward<decltype(continuable)>(continuable).apply(std::forward<Transform>(transform));
218 }
219
226 inline auto wait() {
227 return cti::transforms::wait(); //
228 }
229
238 template<class Rep, class Period> inline auto wait(const TTimeInterval<Rep, Period>& timeout) {
239 return cti::transforms::wait_for(timeout); //
240 }
241
250 template<class Clock, class Duration> inline auto wait(const TDate<Clock, Duration>& timeout) {
251 return cti::transforms::wait_until(timeout); //
252 }
253
258 constexpr cti::use_continuable_raw_t useContinuable{};
259
260} // namespace CeresEngine
Definition Exception.hpp:43
Definition Application.hpp:19
auto async_ready(Args &&... args)
Returns a continuable_base with no result which instantly resolves the promise with no values.
Definition Async.hpp:135
std::chrono::time_point< Clock, Duration > TDate
Represents a point in time.
Definition Chrono.hpp:80
auto wait()
Awaits (and blocks) until the continuation has finished running.
Definition Async.hpp:226
cti::continuable< Args... > Async
Defines a non-copyable continuation type which uses the function2 backend for type erasure.
Definition Async.hpp:22
cti::result< Args... > AsyncResult
The result class can carry the three kinds of results an asynchronous operation possibly can return,...
Definition Async.hpp:51
constexpr auto async(Continuation &&continuation)
Creates an Async from a promise/callback taking function.
Definition Async.hpp:121
decltype(auto) async_map(Mapper &&mapper, T &&... pack)
Maps the pack with the given mapper.
Definition Async.hpp:191
std::chrono::duration< Representation, Period > TTimeInterval
Represents a time interval.
Definition Chrono.hpp:29
constexpr auto async_except(Exception &&exception)
Returns a continuable_base with the parameterized result which instantly resolves the promise with th...
Definition Async.hpp:150
constexpr cti::use_continuable_raw_t useContinuable
Special value for instance of use_continuable_raw_t which doesn't perform remapping of asio error cod...
Definition Async.hpp:258
cti::promise< Args... > Promise
Defines a non-copyable promise type which is using the function2 backend for type erasure.
Definition Async.hpp:28
constexpr decltype(auto) async_spread(T &&... args)
Indicate that the result shall be spread across the parent container if possible.
Definition Async.hpp:198
@ Transform
Indicates that the object transform is dirty.
auto transform(Container &container, Transform &&transform)
Returns an iterable object that iterates over the values of the container and applies transform to ev...
Definition Iterator.hpp:436
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
void async_traverse(Mapper &&mapper, T &&... pack)
Traverses the pack with the given visitor.
Definition Async.hpp:209