boost::asio::composed
Creates an initiation function object that may be used to launch an asynchronous operation with a stateful implementation.
Synopsis
Declared in <boost/asio/composed.hpp>
template<
boost::asio::completion_signature... Signatures,
typename Implementation,
typename... IoObjectsOrExecutors>
decltype(detail::make_initiate_composed<Signatures...>(
static_cast<Implementation&&>(implementation),
detail::make_composed_io_executors(
detail::get_composed_io_executor(
static_cast<IoObjectsOrExecutors&&>(
io_objects_or_executors))...)))
composed(
Implementation&& implementation,
IoObjectsOrExecutors...&&... io_objects_or_executors);
Description
The composed function simplifies the implementation of composed asynchronous operations automatically by wrapping a stateful function object for use as an initiation function object.
Per‐Operation Cancellation
By default, terminal per‐operation cancellation is enabled for composed operations that are implemented using composed. To disable cancellation for the composed operation, or to alter its supported cancellation types, call the self object's reset_cancellation_state function.
Example:
struct async_echo_implementation
{
tcp::socket& socket_;
boost::asio::mutable_buffer buffer_;
enum { starting, reading, writing } state_;
template <typename Self>
void operator()(Self& self,
boost::system::error_code error,
std::size_t n)
{
switch (state_)
{
case starting:
state_ = reading;
socket_.async_read_some(
buffer_, std::move(self));
break;
case reading:
if (error)
{
self.complete(error, 0);
}
else
{
state_ = writing;
boost::asio::async_write(socket_, buffer_,
boost::asio::transfer_exactly(n),
std::move(self));
}
break;
case writing:
self.complete(error, n);
break;
}
}
};
template <typename CompletionToken>
auto async_echo(tcp::socket& socket,
boost::asio::mutable_buffer buffer,
CompletionToken&& token)
-> decltype(
boost::asio::async_initiate<CompletionToken,
void(boost::system::error_code, std::size_t)>(
boost::asio::composed(
async_echo_implementation{socket, buffer,
async_echo_implementation::starting}, socket),
token))
{
return boost::asio::async_initiate<CompletionToken,
void(boost::system::error_code, std::size_t)>(
boost::asio::composed(
async_echo_implementation{socket, buffer,
async_echo_implementation::starting}, socket),
token, boost::system::error_code{}, 0);
}
Parameters
Name |
Description |
implementation |
A function object that contains the implementation of the composed asynchronous operation. The first argument to the function object is a non‐const reference to the enclosing intermediate completion handler. The remaining arguments are any arguments that originate from the completion handlers of any asynchronous operations performed by the implementation. |
io_objects_or_executors |
Zero or more I/O objects or I/O executors for which outstanding work must be maintained. |
Created with MrDocs