Allow packed_dispatch to receive an arbitrary count of tail args

This commit is contained in:
Denis Blank 2017-10-01 01:49:50 +02:00
parent e594989af2
commit d9546c15bb
2 changed files with 10 additions and 22 deletions

View File

@ -228,38 +228,28 @@ constexpr auto invoker_of(traits::identity<std::tuple<Args...>>) {
} // namespace decoration } // namespace decoration
/// Invoke the callback immediately /// Invoke the callback immediately
template <typename Invoker, typename Callback, typename NextCallback, template <typename Invoker, typename... Args>
typename... Args>
void packed_dispatch(types::this_thread_executor_tag, Invoker&& invoker, void packed_dispatch(types::this_thread_executor_tag, Invoker&& invoker,
Callback&& callback, NextCallback&& next_callback,
Args&&... args) { Args&&... args) {
// Invoke the callback with the decorated invoker immediately // Invoke the callback with the decorated invoker immediately
std::forward<Invoker>(invoker)(std::forward<Callback>(callback), std::forward<Invoker>(invoker)(std::forward<Args>(args)...);
std::forward<NextCallback>(next_callback),
std::forward<Args>(args)...);
} }
/// Invoke the callback through the given executor /// Invoke the callback through the given executor
template <typename Executor, typename Invoker, typename Callback, template <typename Executor, typename Invoker, typename... Args>
typename NextCallback, typename... Args> void packed_dispatch(Executor&& executor, Invoker&& invoker, Args&&... args) {
void packed_dispatch(Executor&& executor, Invoker&& invoker,
Callback&& callback, NextCallback&& next_callback,
Args&&... args) {
// Create a worker object which when invoked calls the callback with the // Create a worker object which when invoked calls the callback with the
// the returned arguments. // the returned arguments.
auto work = [ auto work = [
invoker = std::forward<Invoker>(invoker), invoker = std::forward<Invoker>(invoker),
callback = std::forward<Callback>(callback),
next_callback = std::forward<NextCallback>(next_callback),
args = std::make_tuple(std::forward<Args>(args)...) args = std::make_tuple(std::forward<Args>(args)...)
]() mutable { ]() mutable {
traits::unpack(std::move(args), [&](auto&&... captured_args) { traits::unpack(std::move(args), [&](auto&&... captured_args) {
// Just use the packed dispatch method which dispatches the work on // Just use the packed dispatch method which dispatches the work on
// the current thread. // the current thread.
packed_dispatch(types::this_thread_executor_tag{}, std::move(invoker), packed_dispatch(types::this_thread_executor_tag{}, std::move(invoker),
std::move(callback), std::move(next_callback),
std::forward<decltype(captured_args)>(captured_args)...); std::forward<decltype(captured_args)>(captured_args)...);
}); });
}; };
@ -335,16 +325,14 @@ struct error_callback<hints::signature_hint_tag<Args...>, Callback, Executor,
/// The operator which is called when an error occurred /// The operator which is called when an error occurred
void operator()(types::dispatch_error_tag /*tag*/, types::error_type error) { void operator()(types::dispatch_error_tag /*tag*/, types::error_type error) {
// Just invoke the error handler, cancel the calling hierarchy then // Just invoke the error handler, cancel the calling hierarchy after
auto invoker = [](Callback&& callback, NextCallback&&, auto invoker = [](Callback&& callback, types::error_type&& error) {
types::error_type&& error) { std::move(callback)(std::move(error));
callback(std::move(error));
}; };
// Invoke the error handler // Invoke the error handler
packed_dispatch(std::move(executor_), std::move(invoker), packed_dispatch(std::move(executor_), std::move(invoker),
std::move(callback_), std::move(next_callback_), std::move(callback_), std::move(error));
std::move(error));
} }
/// Resolves the continuation with the given values /// Resolves the continuation with the given values

View File

@ -69,13 +69,13 @@ int main(int, char**) {
// ... // ...
}); });
http_request2("github.com") /*http_request2("github.com")
.then([](std::string) { .then([](std::string) {
// ... // ...
}) })
.catching([](std::error_condition) { .catching([](std::error_condition) {
// ... // ...
}); });*/
return 0; return 0;
} }