Files
boost_beast/include/beast/core/async_completion.hpp
T

88 lines
2.9 KiB
C++
Raw Normal View History

2016-04-19 20:29:16 -04:00
//
2017-02-06 20:07:03 -05:00
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
2016-04-19 20:29:16 -04:00
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
2016-04-01 11:46:32 -04:00
2016-04-19 20:29:16 -04:00
#ifndef BEAST_ASYNC_COMPLETION_HPP
#define BEAST_ASYNC_COMPLETION_HPP
2016-04-01 11:46:32 -04:00
2016-05-07 14:57:15 -04:00
#include <beast/core/handler_concepts.hpp>
2016-04-01 11:46:32 -04:00
#include <boost/asio/async_result.hpp>
#include <boost/asio/handler_type.hpp>
#include <type_traits>
#include <utility>
namespace beast {
2016-04-22 19:02:07 -04:00
/** Helper for customizing the return type of asynchronous initiation functions.
2016-04-01 11:46:32 -04:00
2016-04-22 19:02:07 -04:00
This class template is used to transform caller-provided completion
handlers in calls to asynchronous initiation functions. The transformation
2016-04-22 19:02:07 -04:00
allows customization of the return type of the initiating function, and the
function signature of the final handler.
2016-04-01 11:46:32 -04:00
@tparam CompletionHandler A completion handler, or a user defined type
2016-04-01 11:46:32 -04:00
with specializations for customizing the return type (for example,
`boost::asio::use_future` or `boost::asio::yield_context`).
@tparam Signature The callable signature of the final completion handler.
2016-04-22 19:02:07 -04:00
Example:
2016-04-01 11:46:32 -04:00
@code
2016-04-22 07:34:44 -04:00
...
template<class CompletionHandler>
typename async_completion<CompletionHandler,
2016-10-04 18:00:11 -04:00
void(error_code)>::result_type
async_initfn(..., CompletionHandler&& handler)
2016-04-01 11:46:32 -04:00
{
async_completion<CompletionHandler,
2017-01-09 11:25:34 -05:00
void(error_code)> completion{handler};
2016-04-01 11:46:32 -04:00
...
return completion.result.get();
}
@endcode
@note See <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3896.pdf">
2016-04-01 11:46:32 -04:00
Library Foundations For Asynchronous Operations</a>
*/
2016-08-26 08:01:44 -04:00
template<class CompletionHandler, class Signature>
2016-04-01 11:46:32 -04:00
struct async_completion
{
2016-04-22 19:02:07 -04:00
/** The type of the final handler called by the asynchronous initiation function.
2016-04-01 11:46:32 -04:00
2016-04-22 19:02:07 -04:00
Objects of this type will be callable with the specified signature.
2016-04-01 11:46:32 -04:00
*/
using handler_type =
typename boost::asio::handler_type<
CompletionHandler, Signature>::type;
2016-04-01 11:46:32 -04:00
2016-04-22 19:02:07 -04:00
/// The type of the value returned by the asynchronous initiation function.
2016-04-22 07:34:44 -04:00
using result_type = typename
boost::asio::async_result<handler_type>::type;
2016-04-22 19:02:07 -04:00
/** Construct the helper.
2016-04-01 11:46:32 -04:00
@param token The completion handler. Copies will be made as
required. If `CompletionHandler` is movable, it may also be moved.
2016-04-01 11:46:32 -04:00
*/
async_completion(typename std::remove_reference<CompletionHandler>::type& token)
: handler(std::forward<CompletionHandler>(token))
2016-04-01 11:46:32 -04:00
, result(handler)
{
static_assert(is_CompletionHandler<handler_type, Signature>::value,
2016-04-01 11:46:32 -04:00
"Handler requirements not met");
}
2016-04-22 19:02:07 -04:00
/// The final completion handler, callable with the specified signature.
2016-04-01 11:46:32 -04:00
handler_type handler;
2016-04-22 19:02:07 -04:00
/// The return value of the asynchronous initiation function.
2016-04-01 11:46:32 -04:00
boost::asio::async_result<handler_type> result;
};
} // beast
#endif