Make it possible to apply when_seq to iterators

This commit is contained in:
Denis Blank 2018-02-27 17:18:52 +01:00
parent b3bf06c26e
commit 3c70024c0b
3 changed files with 99 additions and 10 deletions

View File

@ -33,8 +33,14 @@
#include <utility>
#include <continuable/continuable-base.hpp>
#include <continuable/detail/composition-all.hpp>
#include <continuable/detail/composition-any.hpp>
#include <continuable/detail/composition-seq.hpp>
#include <continuable/detail/composition.hpp>
#include <continuable/detail/features.hpp>
#include <continuable/detail/range.hpp>
#include <continuable/detail/traits.hpp>
#include <continuable/detail/util.hpp>
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 <typename... Continuables>
auto when_seq(Continuables&&... continuables) {
static_assert(sizeof...(continuables) >= 2,
"Requires at least 2 continuables!");
return CONTINUABLE_FOLD_EXPRESSION(
>>, std::forward<Continuables>(continuables)...);
template <typename... Args>
auto when_seq(Args&&... args) {
return detail::composition::apply_composition(
detail::composition::composition_strategy_seq_tag{},
std::forward<Args>(args)...);
}
/// bla
///
/// \copydetail when_seq
///
/// \since 3.0.0
template <
typename Iterator,
std::enable_if_t<detail::range::is_iterator<Iterator>::value>* = nullptr>
auto when_seq(Iterator begin, Iterator end) {
return when_seq(detail::range::persist_range(begin, end));
}
} // namespace cti

View File

@ -0,0 +1,65 @@
/*
/~` _ _ _|_. _ _ |_ | _
\_,(_)| | | || ||_|(_||_)|(/_
https://github.com/Naios/continuable
v3.0.0
Copyright(c) 2015 - 2018 Denis Blank <denis.blank at outlook dot com>
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 <iterator>
#include <type_traits>
#include <vector>
#include <continuable/detail/traits.hpp>
namespace cti {
namespace detail {
namespace range {
/// Deduces to a true_type if the given type is an interator
template <typename T, typename = void>
struct is_iterator : std::false_type {};
template <typename T>
struct is_iterator<T,
traits::void_t<typename std::iterator_traits<T>::value_type>>
: std::true_type {};
/// Moves to content of the given iterators to a persistent storage
template <typename Iterator>
auto persist_range(Iterator begin, Iterator end) {
std::vector<typename std::iterator_traits<Iterator>::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

View File

@ -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::continuable<int>>{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<cti::continuable<int>> v{cti::make_ready_continuable(8),
cti::make_ready_continuable(9)};
cti::when_seq(v.begin(), v.end()).then([](auto o2) {
// ...
util::unused(o2);
});
}