From 9ecbb00f5a64bb6209a73a7c4dc3c41f1181831c Mon Sep 17 00:00:00 2001 From: Denis Blank Date: Sun, 4 Mar 2018 08:14:11 +0100 Subject: [PATCH] Implement the nested when_any connection --- .../continuable/detail/composition-any.hpp | 40 +++++++++++-------- include/continuable/detail/traits.hpp | 22 ---------- 2 files changed, 23 insertions(+), 39 deletions(-) diff --git a/include/continuable/detail/composition-any.hpp b/include/continuable/detail/composition-any.hpp index 59c25eb..dc8ffc9 100644 --- a/include/continuable/detail/composition-any.hpp +++ b/include/continuable/detail/composition-any.hpp @@ -39,6 +39,7 @@ #include #include +#include #include #include #include @@ -94,14 +95,6 @@ private: } }; -/// Creates a submitter that continues `any` chains -template -auto make_any_result_submitter(Callback&& callback) { - return std::make_shared< - any_result_submitter>>( - std::forward(callback)); -} - struct result_deducer { template static auto deduce_one(std::false_type, traits::identity) { @@ -159,6 +152,21 @@ struct result_deducer { return deduce_tuple_like(i, id); } }; + +template +struct continuable_dispatcher { + std::shared_ptr& submitter; + + template >::value>* = nullptr> + void operator()(Continuable&& continuable) const { + // Retrieve a callback from the submitter and attach it to the continuable + std::forward(continuable) + .next(submitter->create_callback()) + .done(); + } +}; } // namespace any /// Finalizes the any logic of a given composition @@ -175,19 +183,17 @@ struct composition_finalizer { static auto finalize(Composition&& composition) { return [composition = std::forward(composition)]( auto&& callback) mutable { + + using submitter_t = + any::any_result_submitter>; + // Create the submitter which calls the given callback once at the // first callback invocation. - auto submitter = any::make_any_result_submitter( + auto submitter = std::make_shared( std::forward(callback)); - traits::static_for_each_in(std::move(composition), - [&](auto&& entry) mutable { - // Invoke the continuation with a - // submission callback - base::attorney::invoke_continuation( - std::forward(entry), - submitter->create_callback()); - }); + traverse_pack(any::continuable_dispatcher{submitter}, + std::move(composition)); }; } }; diff --git a/include/continuable/detail/traits.hpp b/include/continuable/detail/traits.hpp index 46e239a..60dc905 100644 --- a/include/continuable/detail/traits.hpp +++ b/include/continuable/detail/traits.hpp @@ -198,18 +198,6 @@ constexpr auto static_if_impl(std::false_type, Type&& type, return std::forward(falseCallback)(std::forward(type)); } -/// Applies the given callable to all objects in a sequence -template -struct apply_to_all { - C callable; - - template - constexpr void operator()(T&&... args) { - (void)std::initializer_list{ - 0, ((void)callable(std::forward(args)), 0)...}; - } -}; - /// Evaluates to the size of the given tuple like type, // / if the type has no static size it will be one. template @@ -364,16 +352,6 @@ constexpr auto unpack(F&& first_sequenceable, S&& second_sequenceable, sequence_of(identity_of(second_sequenceable))); } -/// Applies the handler function to each element contained in the sequenceable -// TODO Maybe crashes MSVC in constexpr mode -template -constexpr void static_for_each_in(Sequenceable&& sequenceable, - Handler&& handler) { - unpack(std::forward(sequenceable), - detail::apply_to_all>{ - std::forward(handler)}); -} - /// Adds the given type at the back of the left sequenceable template constexpr auto push(Left&& left, Element&& element) {