Files
boost_beast/beast/asio/bind_handler.h
T

173 lines
4.8 KiB
C++
Raw Normal View History

2014-03-07 20:06:12 -08:00
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef BEAST_ASIO_BIND_HANDLER_H_INCLUDED
#define BEAST_ASIO_BIND_HANDLER_H_INCLUDED
#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 {
/** 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