Finish the async implementation

This commit is contained in:
Denis Blank 2019-01-14 18:07:37 +01:00
parent b86fe7a255
commit 60b75a6134
2 changed files with 36 additions and 9 deletions

View File

@ -45,20 +45,24 @@ auto async(Callable&& callable, Args&&... args) {
decltype(util::invoke(std::forward<decltype(callable)>(callable),
std::forward<decltype(args)>(args)...));
auto const hint =
decltype(base::decoration::invoker_of(identify<result_t>{}))::hint();
constexpr auto hint =
decltype(base::decoration::invoker_of(identity<result_t>{}))::hint();
auto continuation = [callable = std::forward<decltype(callable)>(callable),
args = std::make_tuple(std::forward<decltype(args)>(
args)...)](auto&& promise) mutable {
// Select the invoker
auto invoker = base::decoration::invoker_of(identify<result_t>{});
traits::unpack([&](auto&&... args) {
// Invoke the promise through the dedicated invoker
invoker(std::move(callable), std::forward<decltype(promise)>(promise),
std::forward<decltype(args)>(args)...);
});
auto invoker = base::decoration::invoker_of(identity<result_t>{});
using promise_t = decltype(promise);
traits::unpack(
[&](auto&&... args) {
// Invoke the promise through the dedicated invoker
invoker(std::move(callable), std::forward<promise_t>(promise),
std::forward<decltype(args)>(args)...);
},
std::move(args));
};
return base::attorney::create_from(std::move(continuation), //

View File

@ -37,6 +37,29 @@
namespace cti {
/// \ingroup Operations
/// \{
/// Wraps the given callable inside a continuable_base such that it is
/// invoked when the asynchronous result is requested to return the result.
///
/// The behaviour will be equal as when using make_ready_continuable together
/// with continuable_base::then, but async is implemented in
/// a more efficient way:
/// ```cpp
/// auto do_sth() {
/// return async([] {
/// do_sth_more();
/// return 0;
/// });
/// }
/// ```
///
/// \param callable The callable type which is invoked on request
///
/// \param args The arguments which are passed to the callable upon invocation.
///
/// \returns A continuable_base which asynchronous result type will
/// be computated with the same rules as continuable_base::then .
///
template <typename Callable, typename... Args>
auto async(Callable&& callable, Args&&... args) {
return detail::operations::async(std::forward<Callable>(callable),