From 3c70024c0bcd2aab54c3425b614126917142d121 Mon Sep 17 00:00:00 2001 From: Denis Blank Date: Tue, 27 Feb 2018 17:18:52 +0100 Subject: [PATCH] Make it possible to apply when_seq to iterators --- .../continuable/continuable-compositions.hpp | 33 +++++++--- include/continuable/detail/range.hpp | 65 +++++++++++++++++++ test/playground/test-playground.cpp | 11 +++- 3 files changed, 99 insertions(+), 10 deletions(-) create mode 100644 include/continuable/detail/range.hpp diff --git a/include/continuable/continuable-compositions.hpp b/include/continuable/continuable-compositions.hpp index 6e769b5..99e3d00 100644 --- a/include/continuable/continuable-compositions.hpp +++ b/include/continuable/continuable-compositions.hpp @@ -33,8 +33,14 @@ #include -#include +#include +#include +#include +#include #include +#include +#include +#include namespace cti { /// Connects the given continuables with an *all* logic. @@ -71,18 +77,29 @@ auto when_any(Continuables&&... continuables) { /// Connects the given continuables with a *seq* logic. /// -/// \param continuables The continuable_base objects to connect. +/// \param args The continuable_base objects to connect. /// Requires at least 2 objects to connect. /// /// \see continuable_base::operator>> for details. /// /// \since 1.1.0 -template -auto when_seq(Continuables&&... continuables) { - static_assert(sizeof...(continuables) >= 2, - "Requires at least 2 continuables!"); - return CONTINUABLE_FOLD_EXPRESSION( - >>, std::forward(continuables)...); +template +auto when_seq(Args&&... args) { + return detail::composition::apply_composition( + detail::composition::composition_strategy_seq_tag{}, + std::forward(args)...); +} + +/// bla +/// +/// \copydetail when_seq +/// +/// \since 3.0.0 +template < + typename Iterator, + std::enable_if_t::value>* = nullptr> +auto when_seq(Iterator begin, Iterator end) { + return when_seq(detail::range::persist_range(begin, end)); } } // namespace cti diff --git a/include/continuable/detail/range.hpp b/include/continuable/detail/range.hpp new file mode 100644 index 0000000..cc65090 --- /dev/null +++ b/include/continuable/detail/range.hpp @@ -0,0 +1,65 @@ + +/* + + /~` _ _ _|_. _ _ |_ | _ + \_,(_)| | | || ||_|(_||_)|(/_ + + https://github.com/Naios/continuable + v3.0.0 + + Copyright(c) 2015 - 2018 Denis Blank + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files(the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and / or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions : + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +**/ + +#ifndef CONTINUABLE_DETAIL_RANGE_HPP_INCLUDED +#define CONTINUABLE_DETAIL_RANGE_HPP_INCLUDED + +#include +#include +#include + +#include + +namespace cti { +namespace detail { +namespace range { +/// Deduces to a true_type if the given type is an interator +template +struct is_iterator : std::false_type {}; +template +struct is_iterator::value_type>> + : std::true_type {}; + +/// Moves to content of the given iterators to a persistent storage +template +auto persist_range(Iterator begin, Iterator end) { + std::vector::value_type> storage; + + storage.insert(storage.end(), std::make_move_iterator(begin), + std::make_move_iterator(end)); + + return storage; // RVO +} +} // namespace range +} // namespace detail +} // namespace cti + +#endif // CONTINUABLE_DETAIL_RANGE_HPP_INCLUDED diff --git a/test/playground/test-playground.cpp b/test/playground/test-playground.cpp index fdc7b26..4f65ffe 100644 --- a/test/playground/test-playground.cpp +++ b/test/playground/test-playground.cpp @@ -124,8 +124,7 @@ void old() { int main(int, char**) { using namespace cti::detail; - apply_composition( - composition::composition_strategy_seq_tag{}, + cti::when_seq( cti::make_ready_continuable(0, 1), 2, std::vector>{cti::make_ready_continuable(8), cti::make_ready_continuable(9)}, @@ -134,4 +133,12 @@ int main(int, char**) { // ... util::unused(a0, a1, a2, o1, o2); }); + + std::vector> v{cti::make_ready_continuable(8), + cti::make_ready_continuable(9)}; + + cti::when_seq(v.begin(), v.end()).then([](auto o2) { + // ... + util::unused(o2); + }); }