123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516 |
- #ifndef BOOST_ASIO_EXPERIMENTAL_BASIC_CONCURRENT_CHANNEL_HPP
- #define BOOST_ASIO_EXPERIMENTAL_BASIC_CONCURRENT_CHANNEL_HPP
- #if defined(_MSC_VER) && (_MSC_VER >= 1200)
- # pragma once
- #endif
- #include <boost/asio/detail/config.hpp>
- #include <boost/asio/detail/non_const_lvalue.hpp>
- #include <boost/asio/detail/mutex.hpp>
- #include <boost/asio/execution/executor.hpp>
- #include <boost/asio/execution_context.hpp>
- #include <boost/asio/experimental/detail/channel_send_functions.hpp>
- #include <boost/asio/experimental/detail/channel_service.hpp>
- #include <boost/asio/detail/push_options.hpp>
- namespace boost {
- namespace asio {
- namespace experimental {
- namespace detail {
- }
- template <typename Executor, typename Traits, typename... Signatures>
- class basic_concurrent_channel
- #if !defined(GENERATING_DOCUMENTATION)
- : public detail::channel_send_functions<
- basic_concurrent_channel<Executor, Traits, Signatures...>,
- Executor, Signatures...>
- #endif
- {
- private:
- class initiate_async_send;
- class initiate_async_receive;
- typedef detail::channel_service<boost::asio::detail::mutex> service_type;
- typedef typename service_type::template implementation_type<
- Traits, Signatures...>::payload_type payload_type;
- template <typename... PayloadSignatures,
- BOOST_ASIO_COMPLETION_TOKEN_FOR(PayloadSignatures...) CompletionToken>
- auto do_async_receive(
- boost::asio::detail::completion_payload<PayloadSignatures...>*,
- CompletionToken&& token)
- -> decltype(
- async_initiate<CompletionToken, PayloadSignatures...>(
- declval<initiate_async_receive>(), token))
- {
- return async_initiate<CompletionToken, PayloadSignatures...>(
- initiate_async_receive(this), token);
- }
- public:
-
- typedef Executor executor_type;
-
- template <typename Executor1>
- struct rebind_executor
- {
-
- typedef basic_concurrent_channel<Executor1, Traits, Signatures...> other;
- };
-
- typedef typename Traits::template rebind<Signatures...>::other traits_type;
-
-
- basic_concurrent_channel(const executor_type& ex,
- std::size_t max_buffer_size = 0)
- : service_(&boost::asio::use_service<service_type>(
- basic_concurrent_channel::get_context(ex))),
- impl_(),
- executor_(ex)
- {
- service_->construct(impl_, max_buffer_size);
- }
-
-
- template <typename ExecutionContext>
- basic_concurrent_channel(ExecutionContext& context,
- std::size_t max_buffer_size = 0,
- constraint_t<
- is_convertible<ExecutionContext&, execution_context&>::value,
- defaulted_constraint
- > = defaulted_constraint())
- : service_(&boost::asio::use_service<service_type>(context)),
- impl_(),
- executor_(context.get_executor())
- {
- service_->construct(impl_, max_buffer_size);
- }
-
-
- basic_concurrent_channel(basic_concurrent_channel&& other)
- : service_(other.service_),
- executor_(other.executor_)
- {
- service_->move_construct(impl_, other.impl_);
- }
-
-
- basic_concurrent_channel& operator=(basic_concurrent_channel&& other)
- {
- if (this != &other)
- {
- service_->move_assign(impl_, *other.service_, other.impl_);
- executor_.~executor_type();
- new (&executor_) executor_type(other.executor_);
- service_ = other.service_;
- }
- return *this;
- }
-
- template <typename, typename, typename...>
- friend class basic_concurrent_channel;
-
-
- template <typename Executor1>
- basic_concurrent_channel(
- basic_concurrent_channel<Executor1, Traits, Signatures...>&& other,
- constraint_t<
- is_convertible<Executor1, Executor>::value
- > = 0)
- : service_(other.service_),
- executor_(other.executor_)
- {
- service_->move_construct(impl_, other.impl_);
- }
-
-
- template <typename Executor1>
- constraint_t<
- is_convertible<Executor1, Executor>::value,
- basic_concurrent_channel&
- > operator=(
- basic_concurrent_channel<Executor1, Traits, Signatures...>&& other)
- {
- if (this != &other)
- {
- service_->move_assign(impl_, *other.service_, other.impl_);
- executor_.~executor_type();
- new (&executor_) executor_type(other.executor_);
- service_ = other.service_;
- }
- return *this;
- }
-
- ~basic_concurrent_channel()
- {
- service_->destroy(impl_);
- }
-
- const executor_type& get_executor() noexcept
- {
- return executor_;
- }
-
- std::size_t capacity() noexcept
- {
- return service_->capacity(impl_);
- }
-
- bool is_open() const noexcept
- {
- return service_->is_open(impl_);
- }
-
- void reset()
- {
- service_->reset(impl_);
- }
-
- void close()
- {
- service_->close(impl_);
- }
-
-
- void cancel()
- {
- service_->cancel(impl_);
- }
-
- bool ready() const noexcept
- {
- return service_->ready(impl_);
- }
- #if defined(GENERATING_DOCUMENTATION)
-
-
- template <typename... Args>
- bool try_send(Args&&... args);
-
-
-
- template <typename... Args>
- bool try_send_via_dispatch(Args&&... args);
-
-
- template <typename... Args>
- std::size_t try_send_n(std::size_t count, Args&&... args);
-
-
-
- template <typename... Args>
- std::size_t try_send_n_via_dispatch(std::size_t count, Args&&... args);
-
- template <typename... Args,
- BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code))
- CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
- auto async_send(Args&&... args,
- CompletionToken&& token);
- #endif
-
-
- template <typename Handler>
- bool try_receive(Handler&& handler)
- {
- return service_->try_receive(impl_, static_cast<Handler&&>(handler));
- }
-
- template <typename CompletionToken
- BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
- auto async_receive(
- CompletionToken&& token
- BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(Executor))
- #if !defined(GENERATING_DOCUMENTATION)
- -> decltype(
- this->do_async_receive(static_cast<payload_type*>(0),
- static_cast<CompletionToken&&>(token)))
- #endif
- {
- return this->do_async_receive(static_cast<payload_type*>(0),
- static_cast<CompletionToken&&>(token));
- }
- private:
-
- basic_concurrent_channel(
- const basic_concurrent_channel&) = delete;
- basic_concurrent_channel& operator=(
- const basic_concurrent_channel&) = delete;
- template <typename, typename, typename...>
- friend class detail::channel_send_functions;
-
- template <typename T>
- static execution_context& get_context(const T& t,
- enable_if_t<execution::is_executor<T>::value>* = 0)
- {
- return boost::asio::query(t, execution::context);
- }
-
- template <typename T>
- static execution_context& get_context(const T& t,
- enable_if_t<!execution::is_executor<T>::value>* = 0)
- {
- return t.context();
- }
- class initiate_async_send
- {
- public:
- typedef Executor executor_type;
- explicit initiate_async_send(basic_concurrent_channel* self)
- : self_(self)
- {
- }
- const executor_type& get_executor() const noexcept
- {
- return self_->get_executor();
- }
- template <typename SendHandler>
- void operator()(SendHandler&& handler,
- payload_type&& payload) const
- {
- boost::asio::detail::non_const_lvalue<SendHandler> handler2(handler);
- self_->service_->async_send(self_->impl_,
- static_cast<payload_type&&>(payload),
- handler2.value, self_->get_executor());
- }
- private:
- basic_concurrent_channel* self_;
- };
- class initiate_async_receive
- {
- public:
- typedef Executor executor_type;
- explicit initiate_async_receive(basic_concurrent_channel* self)
- : self_(self)
- {
- }
- const executor_type& get_executor() const noexcept
- {
- return self_->get_executor();
- }
- template <typename ReceiveHandler>
- void operator()(ReceiveHandler&& handler) const
- {
- boost::asio::detail::non_const_lvalue<ReceiveHandler> handler2(handler);
- self_->service_->async_receive(self_->impl_,
- handler2.value, self_->get_executor());
- }
- private:
- basic_concurrent_channel* self_;
- };
-
- service_type* service_;
-
- typename service_type::template implementation_type<
- Traits, Signatures...> impl_;
-
- Executor executor_;
- };
- }
- }
- }
- #include <boost/asio/detail/pop_options.hpp>
- #endif
|