stlab 2.3.0
Modern, modular C++ algorithms, data structures, and concurrency primitives
Loading...
Searching...
No Matches
ready_future.hpp
Go to the documentation of this file.
1/*
2 Copyright 2015 Adobe
3 Distributed under the Boost Software License, Version 1.0.
4 (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5*/
6
7/**************************************************************************************************/
8
9#ifndef STLAB_CONCURRENCY_READY_FUTURE_HPP
10#define STLAB_CONCURRENCY_READY_FUTURE_HPP
11
20
21#include <stlab/config.hpp>
22
23#include <exception>
24#include <type_traits>
25#include <utility>
26
28
29/**************************************************************************************************/
30
31namespace stlab {
32STLAB_VERSION_NAMESPACE_BEGIN()
33
34
39
40/**************************************************************************************************/
41
42
43template <typename T, typename E>
44auto make_ready_future(T&& x, E executor) -> future<std::decay_t<T>> {
45 auto p = package<std::decay_t<T>(std::decay_t<T>)>(
46 std::move(executor), [](auto&& x) { return std::forward<decltype(x)>(x); });
47 p.first(std::forward<T>(x));
48 return std::move(p.second);
49}
50
52template <typename E>
54 auto p = package<void()>(std::move(executor), []() {});
55 p.first();
56 return p.second;
57}
58
59namespace detail {
60
61template <class T>
62struct _make_exceptional_future {
63 template <typename E>
64 auto operator()(const std::exception_ptr& error, E executor) const -> future<T> {
65 auto p = package<T(T)>(std::move(executor),
66 [](auto&& a) { return std::forward<decltype(a)>(a); });
67 p.first.set_exception(error);
68 return std::move(p.second);
69 }
70};
71
72template <>
73struct _make_exceptional_future<void> {
74 template <typename E>
75 auto operator()(const std::exception_ptr& error, E executor) const -> future<void> {
76 auto p = package<void()>(std::move(executor), []() {});
77 p.first.set_exception(error);
78 return std::move(p.second);
79 }
80};
81
82} // namespace detail
83
85template <typename T, typename E>
86auto make_exceptional_future(const std::exception_ptr& error, E executor) -> future<T> {
87 return detail::_make_exceptional_future<T>{}(error, std::move(executor));
88}
89
90/**************************************************************************************************/
91
93
94STLAB_VERSION_NAMESPACE_END()
95} // namespace stlab
96
97/**************************************************************************************************/
98
99#endif // STLAB_CONCURRENCY_READY_FUTURE_HPP
One-shot asynchronous result: holds a value or exception produced by a promise or packaged_task.
Definition future.hpp:300
Futures, packaged tasks, channels, and coroutine integration.
auto package(E, F &&) -> std::pair< detail::packaged_task_from_signature_t< Sig >, detail::reduced_result_t< Sig > >
Creates a packaged task and its future for the callable f, run on executor.
Definition future.hpp:1269
auto make_exceptional_future(const std::exception_ptr &error, E executor) -> future< T >
Creates a future already failed with error (executor runs attached continuations).
Definition ready_future.hpp:86
auto make_ready_future(T &&x, E executor) -> future< std::decay_t< T > >
Creates a future already completed with x (executor runs attached continuations).
Definition ready_future.hpp:44
Definition reverse.hpp:28
Wraps an executor_t for use with operator&.
Definition executor_base.hpp:74