123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- #ifndef BOOST_ASIO_EXPERIMENTAL_PARALLEL_GROUP_HPP
- #define BOOST_ASIO_EXPERIMENTAL_PARALLEL_GROUP_HPP
- #if defined(_MSC_VER) && (_MSC_VER >= 1200)
- # pragma once
- #endif
- #include <boost/asio/detail/config.hpp>
- #include <vector>
- #include <boost/asio/async_result.hpp>
- #include <boost/asio/detail/array.hpp>
- #include <boost/asio/detail/memory.hpp>
- #include <boost/asio/detail/type_traits.hpp>
- #include <boost/asio/detail/utility.hpp>
- #include <boost/asio/experimental/cancellation_condition.hpp>
- #include <boost/asio/detail/push_options.hpp>
- namespace boost {
- namespace asio {
- namespace experimental {
- namespace detail {
- template <typename Signature>
- struct parallel_op_signature_as_tuple;
- template <typename R, typename... Args>
- struct parallel_op_signature_as_tuple<R(Args...)>
- {
- typedef std::tuple<decay_t<Args>...> type;
- };
- template <std::size_t N, typename Offsets, typename... Signatures>
- struct parallel_group_signature;
- template <std::size_t N, typename R0, typename... Args0>
- struct parallel_group_signature<N, R0(Args0...)>
- {
- typedef boost::asio::detail::array<std::size_t, N> order_type;
- typedef R0 raw_type(Args0...);
- typedef R0 type(order_type, Args0...);
- };
- template <std::size_t N,
- typename R0, typename... Args0,
- typename R1, typename... Args1>
- struct parallel_group_signature<N, R0(Args0...), R1(Args1...)>
- {
- typedef boost::asio::detail::array<std::size_t, N> order_type;
- typedef R0 raw_type(Args0..., Args1...);
- typedef R0 type(order_type, Args0..., Args1...);
- };
- template <std::size_t N, typename Sig0,
- typename Sig1, typename... SigN>
- struct parallel_group_signature<N, Sig0, Sig1, SigN...>
- {
- typedef boost::asio::detail::array<std::size_t, N> order_type;
- typedef typename parallel_group_signature<N,
- typename parallel_group_signature<N, Sig0, Sig1>::raw_type,
- SigN...>::raw_type raw_type;
- typedef typename parallel_group_signature<N,
- typename parallel_group_signature<N, Sig0, Sig1>::raw_type,
- SigN...>::type type;
- };
- template <typename Condition, typename Handler,
- typename... Ops, std::size_t... I>
- void parallel_group_launch(Condition cancellation_condition, Handler handler,
- std::tuple<Ops...>& ops, boost::asio::detail::index_sequence<I...>);
- template <typename Signature, typename Allocator>
- struct ranged_parallel_group_signature;
- template <typename R, typename... Args, typename Allocator>
- struct ranged_parallel_group_signature<R(Args...), Allocator>
- {
- typedef std::vector<std::size_t,
- BOOST_ASIO_REBIND_ALLOC(Allocator, std::size_t)> order_type;
- typedef R raw_type(
- std::vector<Args, BOOST_ASIO_REBIND_ALLOC(Allocator, Args)>...);
- typedef R type(order_type,
- std::vector<Args, BOOST_ASIO_REBIND_ALLOC(Allocator, Args)>...);
- };
- template <typename Condition, typename Handler,
- typename Range, typename Allocator>
- void ranged_parallel_group_launch(Condition cancellation_condition,
- Handler handler, Range&& range, const Allocator& allocator);
- char (¶llel_group_has_iterator_helper(...))[2];
- template <typename T>
- char parallel_group_has_iterator_helper(T*, typename T::iterator* = 0);
- template <typename T>
- struct parallel_group_has_iterator_typedef
- {
- enum { value = (sizeof((parallel_group_has_iterator_helper)((T*)(0))) == 1) };
- };
- }
- template <typename T>
- struct is_async_operation_range
- {
- #if defined(GENERATING_DOCUMENTATION)
-
-
- static const bool value;
- #else
- enum
- {
- value = detail::parallel_group_has_iterator_typedef<T>::value
- };
- #endif
- };
- template <typename... Ops>
- class parallel_group
- {
- private:
- struct initiate_async_wait
- {
- template <typename Handler, typename Condition>
- void operator()(Handler&& h, Condition&& c, std::tuple<Ops...>&& ops) const
- {
- detail::parallel_group_launch(
- std::forward<Condition>(c), std::forward<Handler>(h),
- ops, boost::asio::detail::index_sequence_for<Ops...>());
- }
- };
- std::tuple<Ops...> ops_;
- public:
-
- explicit parallel_group(Ops... ops)
- : ops_(std::move(ops)...)
- {
- }
-
- typedef typename detail::parallel_group_signature<sizeof...(Ops),
- completion_signature_of_t<Ops>...>::type signature;
-
-
- template <typename CancellationCondition,
- BOOST_ASIO_COMPLETION_TOKEN_FOR(signature) CompletionToken>
- auto async_wait(CancellationCondition cancellation_condition,
- CompletionToken&& token)
- -> decltype(
- boost::asio::async_initiate<CompletionToken, signature>(
- declval<initiate_async_wait>(), token,
- std::move(cancellation_condition), std::move(ops_)))
- {
- return boost::asio::async_initiate<CompletionToken, signature>(
- initiate_async_wait(), token,
- std::move(cancellation_condition), std::move(ops_));
- }
- };
- template <typename... Ops>
- BOOST_ASIO_NODISCARD inline parallel_group<Ops...>
- make_parallel_group(Ops... ops)
- {
- return parallel_group<Ops...>(std::move(ops)...);
- }
- template <typename Range, typename Allocator = std::allocator<void>>
- class ranged_parallel_group
- {
- private:
- struct initiate_async_wait
- {
- template <typename Handler, typename Condition>
- void operator()(Handler&& h, Condition&& c,
- Range&& range, const Allocator& allocator) const
- {
- detail::ranged_parallel_group_launch(std::move(c),
- std::move(h), std::forward<Range>(range), allocator);
- }
- };
- Range range_;
- Allocator allocator_;
- public:
-
- explicit ranged_parallel_group(Range range,
- const Allocator& allocator = Allocator())
- : range_(std::move(range)),
- allocator_(allocator)
- {
- }
-
- typedef typename detail::ranged_parallel_group_signature<
- completion_signature_of_t<
- decay_t<decltype(*std::declval<typename Range::iterator>())>>,
- Allocator>::type signature;
-
-
- template <typename CancellationCondition,
- BOOST_ASIO_COMPLETION_TOKEN_FOR(signature) CompletionToken>
- auto async_wait(CancellationCondition cancellation_condition,
- CompletionToken&& token)
- -> decltype(
- boost::asio::async_initiate<CompletionToken, signature>(
- declval<initiate_async_wait>(), token,
- std::move(cancellation_condition),
- std::move(range_), allocator_))
- {
- return boost::asio::async_initiate<CompletionToken, signature>(
- initiate_async_wait(), token,
- std::move(cancellation_condition),
- std::move(range_), allocator_);
- }
- };
- template <typename Range>
- BOOST_ASIO_NODISCARD inline ranged_parallel_group<decay_t<Range>>
- make_parallel_group(Range&& range,
- constraint_t<
- is_async_operation_range<decay_t<Range>>::value
- > = 0)
- {
- return ranged_parallel_group<decay_t<Range>>(std::forward<Range>(range));
- }
- template <typename Allocator, typename Range>
- BOOST_ASIO_NODISCARD inline ranged_parallel_group<decay_t<Range>, Allocator>
- make_parallel_group(allocator_arg_t, const Allocator& allocator, Range&& range,
- constraint_t<
- is_async_operation_range<decay_t<Range>>::value
- > = 0)
- {
- return ranged_parallel_group<decay_t<Range>, Allocator>(
- std::forward<Range>(range), allocator);
- }
- }
- }
- }
- #include <boost/asio/detail/pop_options.hpp>
- #include <boost/asio/experimental/impl/parallel_group.hpp>
- #endif
|