From 9891543b1f7bd7c3b45a44d28db1c7e402af3174 Mon Sep 17 00:00:00 2001 From: Denis Blank Date: Fri, 2 Mar 2018 02:51:53 +0100 Subject: [PATCH] Enable and document the new when_all --- .../continuable/continuable-compositions.hpp | 74 ++++++++++++++++--- include/continuable/detail/composition.hpp | 7 +- test/playground/test-playground.cpp | 3 +- 3 files changed, 70 insertions(+), 14 deletions(-) diff --git a/include/continuable/continuable-compositions.hpp b/include/continuable/continuable-compositions.hpp index 2ba00bf..0171f5b 100644 --- a/include/continuable/continuable-compositions.hpp +++ b/include/continuable/continuable-compositions.hpp @@ -43,20 +43,72 @@ #include namespace cti { -/// Connects the given continuables with an *all* logic. +/// Connects the given arguments with an all logic. +/// All continuables contained inside the given nested pack are +/// invoked at once. On completion the final handler is called +/// with the aggregated result of all continuables. /// -/// \param continuables The continuable_base objects to connect. -/// Requires at least 2 objects to connect. +/// \param args Arbitrary arguments which are connected. +/// Every type is allowed as arguments, continuables may be +/// contained inside tuple like types (`std::tuple`) +/// or in homogeneous containers such as `std::vector`. +/// Non continuable arguments are preserved and passed +/// to the final result as shown below: +/// ```cpp +/// cti::when_all( +/// cti::make_ready_continuable(0, 1), +/// 2, //< See this plain value +/// std::vector>{cti::make_ready_continuable(3), +/// cti::make_ready_continuable(4)}, +/// std::make_tuple(std::make_tuple(cti::make_ready_continuable(5)))) +/// .then([](int r0, int r1, int r2, std::vector r34, +/// std::tuple> r5) { +/// // ... +/// }); +/// ``` /// -/// \see continuable_base::operator && for details. +/// \see continuable_base::operator&& for details. /// -/// \since 1.1.0 -template -auto when_all(Continuables&&... continuables) { - static_assert(sizeof...(continuables) >= 2, - "Requires at least 2 continuables!"); - return CONTINUABLE_FOLD_EXPRESSION( - &&, std::forward(continuables)...); +/// \since 1.1.0 +template +auto when_all(Args&&... args) { + return detail::composition::apply_composition( + detail::composition::composition_strategy_all_tag{}, + std::forward(args)...); +} + +/// Connects the given arguments with an all logic. +/// The content of the iterator is moved out and converted +/// to a temporary `std::vector` which is then passed to when_all. +/// +/// ```cpp +/// std::vector> v{cti::make_ready_continuable(0), +/// cti::make_ready_continuable(1)}; +/// +/// cti::when_all(v.begin(), v.end()) +/// .then([](std::vector r01) { +/// // ... +/// }); +/// ``` +/// +/// \param begin The begin iterator to the range which will be moved out +/// and used as the arguments to the all connection +/// +/// \param end The end iterator to the range which will be moved out +/// and used as the arguments to the all connection +/// +/// \see when_all for details. +/// +/// \attention Prefer to invoke when_all with the whole container the +/// iterators were taken from, since this saves us +/// the creation of a temporary storage. +/// +/// \since 3.0.0 +template < + typename Iterator, + std::enable_if_t::value>* = nullptr> +auto when_all(Iterator begin, Iterator end) { + return when_all(detail::range::persist_range(begin, end)); } /// Connects the given arguments with a sequential logic. diff --git a/include/continuable/detail/composition.hpp b/include/continuable/detail/composition.hpp index 1278d7a..eb13033 100644 --- a/include/continuable/detail/composition.hpp +++ b/include/continuable/detail/composition.hpp @@ -177,9 +177,14 @@ struct materializer< } }; -struct prepare_continuables { +class prepare_continuables { util::ownership& ownership_; +public: + explicit constexpr prepare_continuables(util::ownership& ownership) + : ownership_(ownership) { + } + template >::value>* = nullptr> diff --git a/test/playground/test-playground.cpp b/test/playground/test-playground.cpp index a82ba84..4929417 100644 --- a/test/playground/test-playground.cpp +++ b/test/playground/test-playground.cpp @@ -158,8 +158,7 @@ int main(int, char**) { // ... }); - composition::apply_composition( - composition::composition_strategy_all_tag{}, + cti::when_all( cti::make_ready_continuable(0, 1), 2, //< See this plain value std::vector>{cti::make_ready_continuable(3), cti::make_ready_continuable(4)},