Files
boost_beast/include/beast/bind_handler.hpp
T

162 lines
4.0 KiB
C++
Raw Normal View History

2016-04-19 20:29:16 -04:00
//
// Copyright (c) 2013-2016 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// 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)
//
2014-03-07 20:06:12 -08:00
2016-04-19 20:29:16 -04:00
#ifndef BEAST_BIND_HANDLER_HPP
#define BEAST_BIND_HANDLER_HPP
2014-03-07 20:06:12 -08:00
#include <boost/asio/detail/handler_alloc_helpers.hpp>
#include <boost/asio/detail/handler_cont_helpers.hpp>
#include <boost/asio/detail/handler_invoke_helpers.hpp>
#include <functional>
2015-10-07 20:46:52 -07:00
#include <type_traits>
#include <utility>
2014-03-07 20:06:12 -08:00
namespace beast {
namespace detail {
2016-04-19 20:29:16 -04:00
/* Nullary handler that calls Handler with bound arguments.
2016-04-01 11:46:32 -04:00
The bound handler provides the same io_service execution
2014-03-07 20:06:12 -08:00
guarantees as the original handler.
*/
2016-04-01 11:46:32 -04:00
template<class Handler, class... Args>
2014-03-07 20:06:12 -08:00
class bound_handler
{
private:
2016-04-01 11:46:32 -04:00
using args_type = std::tuple<std::decay_t<Args>...>;
2014-03-07 20:06:12 -08:00
2016-04-01 11:46:32 -04:00
Handler h_;
args_type args_;
2014-03-07 20:06:12 -08:00
2016-04-01 11:46:32 -04:00
template<class Tuple, std::size_t... S>
static void invoke(Handler& h, Tuple& args,
2014-03-07 20:06:12 -08:00
std::index_sequence <S...>)
{
2016-04-01 11:46:32 -04:00
h(std::get<S>(args)...);
2014-03-07 20:06:12 -08:00
}
public:
2015-05-21 19:12:10 -04:00
using result_type = void;
2014-03-07 20:06:12 -08:00
2016-04-01 11:46:32 -04:00
template<class DeducedHandler>
2014-07-23 13:48:47 -07:00
explicit
2016-04-01 11:46:32 -04:00
bound_handler(DeducedHandler&& handler, Args&&... args)
: h_(std::forward<DeducedHandler>(handler))
, args_(std::forward<Args>(args)...)
2014-03-07 20:06:12 -08:00
{
}
void
2016-04-01 11:46:32 -04:00
operator()()
2014-03-07 20:06:12 -08:00
{
2016-04-01 11:46:32 -04:00
invoke(h_, args_,
std::index_sequence_for<Args...> ());
2014-03-07 20:06:12 -08:00
}
void
2016-04-01 11:46:32 -04:00
operator()() const
2014-03-07 20:06:12 -08:00
{
2016-04-01 11:46:32 -04:00
invoke(h_, args_,
std::index_sequence_for<Args...> ());
2014-03-07 20:06:12 -08:00
}
friend
void*
2016-04-01 11:46:32 -04:00
asio_handler_allocate(
std::size_t size, bound_handler* h)
2014-03-07 20:06:12 -08:00
{
return boost_asio_handler_alloc_helpers::
2016-04-01 11:46:32 -04:00
allocate(size, h->h_);
2014-03-07 20:06:12 -08:00
}
friend
void
2016-04-01 11:46:32 -04:00
asio_handler_deallocate(
void* p, std::size_t size, bound_handler* h)
2014-03-07 20:06:12 -08:00
{
boost_asio_handler_alloc_helpers::
2016-04-01 11:46:32 -04:00
deallocate(p, size, h->h_);
2014-03-07 20:06:12 -08:00
}
friend
bool
2016-04-01 11:46:32 -04:00
asio_handler_is_continuation(bound_handler* h)
2014-03-07 20:06:12 -08:00
{
return boost_asio_handler_cont_helpers::
2016-04-01 11:46:32 -04:00
is_continuation (h->h_);
}
template<class F>
friend
void
asio_handler_invoke(F&& f, bound_handler* h)
{
boost_asio_handler_invoke_helpers::
invoke(f, h->h_);
2014-03-07 20:06:12 -08:00
}
};
2016-04-01 11:46:32 -04:00
} // detail
2014-03-07 20:06:12 -08:00
//------------------------------------------------------------------------------
2016-04-01 11:46:32 -04:00
/** Bind parameters to a completion handler, creating a wrapped handler.
This function creates a new handler which invoked with no parameters
calls the original handler with the list of bound arguments. The passed
handler and arguments are forwarded into the returned handler, which
provides the same `io_service` execution guarantees as the original
handler.
Unlike `io_service::wrap`, the returned handler can be used in a
subsequent call to `io_service::post` instead of `io_service::dispatch`,
to ensure that the handler will not be invoked immediately by the
calling function.
Example:
@code
template<class AsyncReadStream, ReadHandler>
void
do_cancel(AsyncReadStream& stream, ReadHandler&& handler)
{
stream.get_io_service().post(
bind_handler(std::forward<ReadHandler>(handler),
boost::asio::error::operation_aborted, 0));
}
@endcode
@param handler The handler to wrap.
@param args A list of arguments to bind to the handler. The
arguments are forwarded into the returned
2014-03-07 20:06:12 -08:00
*/
2016-04-01 11:46:32 -04:00
template<class CompletionHandler, class... Args>
#if GENERATING_DOCS
implementation_defined
#else
detail::bound_handler<std::decay_t<CompletionHandler>, Args...>
#endif
bind_handler(CompletionHandler&& handler, Args&&... args)
2014-03-07 20:06:12 -08:00
{
2016-04-01 11:46:32 -04:00
return detail::bound_handler<std::decay_t<
CompletionHandler>, Args...>(std::forward<
CompletionHandler>(handler),
std::forward<Args>(args)...);
2014-03-07 20:06:12 -08:00
}
2016-04-01 11:46:32 -04:00
} // beast
2014-03-07 20:06:12 -08:00
namespace std {
2016-04-01 11:46:32 -04:00
template<class Handler, class... Args>
void bind(beast::detail::bound_handler<
Handler, Args...>, ...) = delete;
} // std
2014-03-07 20:06:12 -08:00
#endif