123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- #ifndef BOOST_ASIO_COMPOSE_HPP
- #define BOOST_ASIO_COMPOSE_HPP
- #if defined(_MSC_VER) && (_MSC_VER >= 1200)
- # pragma once
- #endif
- #include <boost/asio/detail/config.hpp>
- #include <boost/asio/associated_executor.hpp>
- #include <boost/asio/async_result.hpp>
- #include <boost/asio/detail/base_from_cancellation_state.hpp>
- #include <boost/asio/detail/composed_work.hpp>
- #include <boost/asio/detail/handler_cont_helpers.hpp>
- #include <boost/asio/detail/type_traits.hpp>
- #include <boost/asio/detail/push_options.hpp>
- namespace boost {
- namespace asio {
- namespace detail {
- template <typename Impl, typename Work, typename Handler, typename Signature>
- class composed_op;
- template <typename Impl, typename Work, typename Handler,
- typename R, typename... Args>
- class composed_op<Impl, Work, Handler, R(Args...)>
- : public base_from_cancellation_state<Handler>
- {
- public:
- template <typename I, typename W, typename H>
- composed_op(I&& impl,
- W&& work,
- H&& handler)
- : base_from_cancellation_state<Handler>(
- handler, enable_terminal_cancellation()),
- impl_(static_cast<I&&>(impl)),
- work_(static_cast<W&&>(work)),
- handler_(static_cast<H&&>(handler)),
- invocations_(0)
- {
- }
- composed_op(composed_op&& other)
- : base_from_cancellation_state<Handler>(
- static_cast<base_from_cancellation_state<Handler>&&>(other)),
- impl_(static_cast<Impl&&>(other.impl_)),
- work_(static_cast<Work&&>(other.work_)),
- handler_(static_cast<Handler&&>(other.handler_)),
- invocations_(other.invocations_)
- {
- }
- typedef typename composed_work_guard<
- typename Work::head_type>::executor_type io_executor_type;
- io_executor_type get_io_executor() const noexcept
- {
- return work_.head_.get_executor();
- }
- typedef associated_executor_t<Handler, io_executor_type> executor_type;
- executor_type get_executor() const noexcept
- {
- return (get_associated_executor)(handler_, work_.head_.get_executor());
- }
- typedef associated_allocator_t<Handler, std::allocator<void>> allocator_type;
- allocator_type get_allocator() const noexcept
- {
- return (get_associated_allocator)(handler_, std::allocator<void>());
- }
- template<typename... T>
- void operator()(T&&... t)
- {
- if (invocations_ < ~0u)
- ++invocations_;
- this->get_cancellation_state().slot().clear();
- impl_(*this, static_cast<T&&>(t)...);
- }
- void complete(Args... args)
- {
- this->work_.reset();
- static_cast<Handler&&>(this->handler_)(static_cast<Args&&>(args)...);
- }
- void reset_cancellation_state()
- {
- base_from_cancellation_state<Handler>::reset_cancellation_state(handler_);
- }
- template <typename Filter>
- void reset_cancellation_state(Filter&& filter)
- {
- base_from_cancellation_state<Handler>::reset_cancellation_state(handler_,
- static_cast<Filter&&>(filter));
- }
- template <typename InFilter, typename OutFilter>
- void reset_cancellation_state(InFilter&& in_filter,
- OutFilter&& out_filter)
- {
- base_from_cancellation_state<Handler>::reset_cancellation_state(handler_,
- static_cast<InFilter&&>(in_filter),
- static_cast<OutFilter&&>(out_filter));
- }
- cancellation_type_t cancelled() const noexcept
- {
- return base_from_cancellation_state<Handler>::cancelled();
- }
- Impl impl_;
- Work work_;
- Handler handler_;
- unsigned invocations_;
- };
- template <typename Impl, typename Work, typename Handler, typename Signature>
- inline bool asio_handler_is_continuation(
- composed_op<Impl, Work, Handler, Signature>* this_handler)
- {
- return this_handler->invocations_ > 1 ? true
- : boost_asio_handler_cont_helpers::is_continuation(
- this_handler->handler_);
- }
- template <typename Signature, typename Executors>
- class initiate_composed_op
- {
- public:
- typedef typename composed_io_executors<Executors>::head_type executor_type;
- template <typename T>
- explicit initiate_composed_op(int, T&& executors)
- : executors_(static_cast<T&&>(executors))
- {
- }
- executor_type get_executor() const noexcept
- {
- return executors_.head_;
- }
- template <typename Handler, typename Impl>
- void operator()(Handler&& handler,
- Impl&& impl) const
- {
- composed_op<decay_t<Impl>, composed_work<Executors>,
- decay_t<Handler>, Signature>(
- static_cast<Impl&&>(impl),
- composed_work<Executors>(executors_),
- static_cast<Handler&&>(handler))();
- }
- private:
- composed_io_executors<Executors> executors_;
- };
- template <typename Signature, typename Executors>
- inline initiate_composed_op<Signature, Executors> make_initiate_composed_op(
- composed_io_executors<Executors>&& executors)
- {
- return initiate_composed_op<Signature, Executors>(0,
- static_cast<composed_io_executors<Executors>&&>(executors));
- }
- }
- #if !defined(GENERATING_DOCUMENTATION)
- template <template <typename, typename> class Associator,
- typename Impl, typename Work, typename Handler,
- typename Signature, typename DefaultCandidate>
- struct associator<Associator,
- detail::composed_op<Impl, Work, Handler, Signature>,
- DefaultCandidate>
- : Associator<Handler, DefaultCandidate>
- {
- static typename Associator<Handler, DefaultCandidate>::type get(
- const detail::composed_op<Impl, Work, Handler, Signature>& h) noexcept
- {
- return Associator<Handler, DefaultCandidate>::get(h.handler_);
- }
- static auto get(const detail::composed_op<Impl, Work, Handler, Signature>& h,
- const DefaultCandidate& c) noexcept
- -> decltype(Associator<Handler, DefaultCandidate>::get(h.handler_, c))
- {
- return Associator<Handler, DefaultCandidate>::get(h.handler_, c);
- }
- };
- #endif
- template <typename CompletionToken, typename Signature,
- typename Implementation, typename... IoObjectsOrExecutors>
- auto async_compose(Implementation&& implementation,
- type_identity_t<CompletionToken>& token,
- IoObjectsOrExecutors&&... io_objects_or_executors)
- -> decltype(
- async_initiate<CompletionToken, Signature>(
- detail::make_initiate_composed_op<Signature>(
- detail::make_composed_io_executors(
- detail::get_composed_io_executor(
- static_cast<IoObjectsOrExecutors&&>(
- io_objects_or_executors))...)),
- token, static_cast<Implementation&&>(implementation)))
- {
- return async_initiate<CompletionToken, Signature>(
- detail::make_initiate_composed_op<Signature>(
- detail::make_composed_io_executors(
- detail::get_composed_io_executor(
- static_cast<IoObjectsOrExecutors&&>(
- io_objects_or_executors))...)),
- token, static_cast<Implementation&&>(implementation));
- }
- }
- }
- #include <boost/asio/detail/pop_options.hpp>
- #endif
|