mirror of
https://github.com/boostorg/beast.git
synced 2025-07-30 12:57:31 +02:00
Add detail::bind_continuation
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
Version 214:
|
Version 214:
|
||||||
|
|
||||||
* handler binders use the associated allocator
|
* Handler binders use the associated allocator
|
||||||
|
* Add detail::bind_continuation
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
55
include/boost/beast/core/bind_continuation.hpp
Normal file
55
include/boost/beast/core/bind_continuation.hpp
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) 2016-2017 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)
|
||||||
|
//
|
||||||
|
// Official repository: https://github.com/boostorg/beast
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef BOOST_BEAST_BIND_CONTINUATION_HPP
|
||||||
|
#define BOOST_BEAST_BIND_CONTINUATION_HPP
|
||||||
|
|
||||||
|
#include <boost/beast/core/detail/config.hpp>
|
||||||
|
#include <boost/beast/core/detail/remap_post_to_defer.hpp>
|
||||||
|
#include <boost/asio/bind_executor.hpp>
|
||||||
|
#include <boost/core/empty_value.hpp>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace beast {
|
||||||
|
|
||||||
|
/** Mark a completion handler as a continuation.
|
||||||
|
|
||||||
|
This function wraps a completion handler to associate it with an
|
||||||
|
executor whose `post` operation is remapped to the `defer` operation.
|
||||||
|
It is used by composed asynchronous operation implementations to
|
||||||
|
indicate that a completion handler submitted to an initiating
|
||||||
|
function represents a continuation of the current asynchronous
|
||||||
|
flow of control.
|
||||||
|
|
||||||
|
@see
|
||||||
|
|
||||||
|
@li <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4242.html">[N4242] Executors and Asynchronous Operations, Revision 1</a>
|
||||||
|
*/
|
||||||
|
template<class Executor, class CompletionHandler>
|
||||||
|
#if BOOST_BEAST_DOXYGEN
|
||||||
|
__implementation_defined__
|
||||||
|
#else
|
||||||
|
net::executor_binder<typename
|
||||||
|
std::decay<CompletionHandler>::type,
|
||||||
|
detail::remap_post_to_defer<Executor>>
|
||||||
|
#endif
|
||||||
|
bind_continuation(
|
||||||
|
Executor const& ex, CompletionHandler&& handler)
|
||||||
|
{
|
||||||
|
return net::bind_executor(
|
||||||
|
detail::remap_post_to_defer<Executor>(ex),
|
||||||
|
std::forward<CompletionHandler>(handler));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // beast
|
||||||
|
} // boost
|
||||||
|
|
||||||
|
#endif
|
109
include/boost/beast/core/detail/remap_post_to_defer.hpp
Normal file
109
include/boost/beast/core/detail/remap_post_to_defer.hpp
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) 2019 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)
|
||||||
|
//
|
||||||
|
// Official repository: https://github.com/boostorg/beast
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef BOOST_BEAST_DETAIL_REMAP_POST_TO_DEFER_HPP
|
||||||
|
#define BOOST_BEAST_DETAIL_REMAP_POST_TO_DEFER_HPP
|
||||||
|
|
||||||
|
#include <boost/asio/bind_executor.hpp>
|
||||||
|
#include <boost/asio/is_executor.hpp>
|
||||||
|
#include <boost/core/empty_value.hpp>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace beast {
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
template<class Executor>
|
||||||
|
class remap_post_to_defer
|
||||||
|
: private boost::empty_value<Executor>
|
||||||
|
{
|
||||||
|
BOOST_STATIC_ASSERT(
|
||||||
|
net::is_executor<Executor>::value);
|
||||||
|
|
||||||
|
Executor const&
|
||||||
|
ex() const noexcept
|
||||||
|
{
|
||||||
|
return this->get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
remap_post_to_defer(
|
||||||
|
remap_post_to_defer&&) = default;
|
||||||
|
|
||||||
|
remap_post_to_defer(
|
||||||
|
remap_post_to_defer const&) = default;
|
||||||
|
|
||||||
|
explicit
|
||||||
|
remap_post_to_defer(
|
||||||
|
Executor const& ex)
|
||||||
|
: boost::empty_value<Executor>(
|
||||||
|
boost::empty_init_t{}, ex)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
operator==(
|
||||||
|
remap_post_to_defer const& other) const noexcept
|
||||||
|
{
|
||||||
|
return ex() == other.ex();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
operator!=(
|
||||||
|
remap_post_to_defer const& other) const noexcept
|
||||||
|
{
|
||||||
|
return ex() != other.ex();
|
||||||
|
}
|
||||||
|
|
||||||
|
decltype(std::declval<Executor const&>().context())
|
||||||
|
context() const noexcept
|
||||||
|
{
|
||||||
|
return ex().context();
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
on_work_started() const noexcept
|
||||||
|
{
|
||||||
|
ex().on_work_started();
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
on_work_finished() const noexcept
|
||||||
|
{
|
||||||
|
ex().on_work_finished();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class F, class A>
|
||||||
|
void
|
||||||
|
dispatch(F&& f, A const& a) const
|
||||||
|
{
|
||||||
|
ex().dispatch(std::forward<F>(f), a);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class F, class A>
|
||||||
|
void
|
||||||
|
post(F&& f, A const& a) const
|
||||||
|
{
|
||||||
|
ex().defer(std::forward<F>(f), a);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class F, class A>
|
||||||
|
void
|
||||||
|
defer(F&& f, A const& a) const
|
||||||
|
{
|
||||||
|
ex().defer(std::forward<F>(f), a);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // detail
|
||||||
|
} // beast
|
||||||
|
} // boost
|
||||||
|
|
||||||
|
#endif
|
@ -31,6 +31,7 @@ add_executable (tests-beast-core
|
|||||||
_detail_varint.cpp
|
_detail_varint.cpp
|
||||||
async_op_base.cpp
|
async_op_base.cpp
|
||||||
basic_stream.cpp
|
basic_stream.cpp
|
||||||
|
bind_continuation.cpp
|
||||||
bind_handler.cpp
|
bind_handler.cpp
|
||||||
buffer_size.cpp
|
buffer_size.cpp
|
||||||
buffer_traits.cpp
|
buffer_traits.cpp
|
||||||
|
@ -19,6 +19,7 @@ local SOURCES =
|
|||||||
_detail_varint.cpp
|
_detail_varint.cpp
|
||||||
async_op_base.cpp
|
async_op_base.cpp
|
||||||
basic_stream.cpp
|
basic_stream.cpp
|
||||||
|
bind_continuation.cpp
|
||||||
bind_handler.cpp
|
bind_handler.cpp
|
||||||
buffer_size.cpp
|
buffer_size.cpp
|
||||||
buffer_traits.cpp
|
buffer_traits.cpp
|
||||||
|
186
test/beast/core/bind_continuation.cpp
Normal file
186
test/beast/core/bind_continuation.cpp
Normal file
@ -0,0 +1,186 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) 2018 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)
|
||||||
|
//
|
||||||
|
// Official repository: https://github.com/boostorg/beast
|
||||||
|
//
|
||||||
|
|
||||||
|
// Test that header file is self-contained.
|
||||||
|
#include <boost/beast/core/bind_continuation.hpp>
|
||||||
|
|
||||||
|
#include "test_executor.hpp"
|
||||||
|
#include "test_handler.hpp"
|
||||||
|
|
||||||
|
#include <boost/beast/_experimental/unit_test/suite.hpp>
|
||||||
|
#include <boost/beast/core/error.hpp>
|
||||||
|
#include <boost/beast/core/stream_traits.hpp>
|
||||||
|
#include <boost/asio/io_context.hpp>
|
||||||
|
#include <boost/asio/dispatch.hpp>
|
||||||
|
#include <boost/asio/post.hpp>
|
||||||
|
#include <boost/asio/defer.hpp>
|
||||||
|
#include <boost/core/exchange.hpp>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace beast {
|
||||||
|
|
||||||
|
class bind_continuation_test
|
||||||
|
: public beast::unit_test::suite
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
class handler
|
||||||
|
{
|
||||||
|
bool pass_ = false;
|
||||||
|
|
||||||
|
public:
|
||||||
|
handler() = default;
|
||||||
|
|
||||||
|
~handler()
|
||||||
|
{
|
||||||
|
BEAST_EXPECT(pass_);
|
||||||
|
}
|
||||||
|
|
||||||
|
handler(handler&& other)
|
||||||
|
: pass_(boost::exchange(other.pass_, true))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator()()
|
||||||
|
{
|
||||||
|
pass_ = true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void
|
||||||
|
testBinder()
|
||||||
|
{
|
||||||
|
net::io_context ioc;
|
||||||
|
|
||||||
|
// free functions
|
||||||
|
|
||||||
|
{
|
||||||
|
test_executor<> ex(ioc.get_executor());
|
||||||
|
BEAST_EXPECT(ex->total == 0);
|
||||||
|
net::dispatch(
|
||||||
|
bind_continuation(ex, handler{}));
|
||||||
|
ioc.run();
|
||||||
|
ioc.restart();
|
||||||
|
BEAST_EXPECT(ex->dispatch == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
test_executor<> ex(ioc.get_executor());
|
||||||
|
BEAST_EXPECT(ex->total == 0);
|
||||||
|
net::post(
|
||||||
|
bind_continuation(ex, handler{}));
|
||||||
|
ioc.run();
|
||||||
|
ioc.restart();
|
||||||
|
BEAST_EXPECT(ex->defer == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
test_executor<> ex(ioc.get_executor());
|
||||||
|
BEAST_EXPECT(ex->total == 0);
|
||||||
|
net::defer(
|
||||||
|
bind_continuation(ex, handler{}));
|
||||||
|
ioc.run();
|
||||||
|
ioc.restart();
|
||||||
|
BEAST_EXPECT(ex->defer == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// members
|
||||||
|
|
||||||
|
{
|
||||||
|
test_executor<> ex(ioc.get_executor());
|
||||||
|
BEAST_EXPECT(ex->total == 0);
|
||||||
|
ex.dispatch(
|
||||||
|
bind_continuation(ex, handler{}),
|
||||||
|
std::allocator<void>{});
|
||||||
|
ioc.run();
|
||||||
|
ioc.restart();
|
||||||
|
BEAST_EXPECT(ex->dispatch == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
test_executor<> ex(ioc.get_executor());
|
||||||
|
BEAST_EXPECT(ex->total == 0);
|
||||||
|
ex.post(
|
||||||
|
bind_continuation(ex, handler{}),
|
||||||
|
std::allocator<void>{});
|
||||||
|
ioc.run();
|
||||||
|
ioc.restart();
|
||||||
|
BEAST_EXPECT(ex->post == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
test_executor<> ex(ioc.get_executor());
|
||||||
|
BEAST_EXPECT(ex->total == 0);
|
||||||
|
ex.defer(
|
||||||
|
bind_continuation(ex, handler{}),
|
||||||
|
std::allocator<void>{});
|
||||||
|
ioc.run();
|
||||||
|
ioc.restart();
|
||||||
|
BEAST_EXPECT(ex->defer == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// relational
|
||||||
|
|
||||||
|
{
|
||||||
|
auto h1 = bind_continuation(
|
||||||
|
ioc.get_executor(), handler{});
|
||||||
|
auto h2 = bind_continuation(
|
||||||
|
ioc.get_executor(), handler{});
|
||||||
|
BEAST_EXPECT(
|
||||||
|
net::get_associated_executor(h1) ==
|
||||||
|
net::get_associated_executor(h2));
|
||||||
|
BEAST_EXPECT(
|
||||||
|
std::addressof(
|
||||||
|
net::get_associated_executor(h1).context()) ==
|
||||||
|
std::addressof(
|
||||||
|
net::get_associated_executor(h2).context()));
|
||||||
|
h1();
|
||||||
|
h2();
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
net::io_context ioc1;
|
||||||
|
net::io_context ioc2;
|
||||||
|
auto h1 = bind_continuation(
|
||||||
|
ioc1.get_executor(), handler{});
|
||||||
|
auto h2 = bind_continuation(
|
||||||
|
ioc2.get_executor(), handler{});
|
||||||
|
BEAST_EXPECT(
|
||||||
|
net::get_associated_executor(h1) !=
|
||||||
|
net::get_associated_executor(h2));
|
||||||
|
BEAST_EXPECT(
|
||||||
|
std::addressof(
|
||||||
|
net::get_associated_executor(h1).context()) !=
|
||||||
|
std::addressof(
|
||||||
|
net::get_associated_executor(h2).context()));
|
||||||
|
h1();
|
||||||
|
h2();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void
|
||||||
|
testJavadoc()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void
|
||||||
|
run() override
|
||||||
|
{
|
||||||
|
testBinder();
|
||||||
|
testJavadoc();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
BEAST_DEFINE_TESTSUITE(beast,core,bind_continuation);
|
||||||
|
|
||||||
|
} // beast
|
||||||
|
} // boost
|
122
test/beast/core/test_executor.hpp
Normal file
122
test/beast/core/test_executor.hpp
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) 2018 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)
|
||||||
|
//
|
||||||
|
// Official repository: https://github.com/boostorg/beast
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef BOOST_BEAST_TEST_TEST_EXECUTOR_HPP
|
||||||
|
#define BOOST_BEAST_TEST_TEST_EXECUTOR_HPP
|
||||||
|
|
||||||
|
#include <boost/beast/core/detail/config.hpp>
|
||||||
|
#include <boost/asio/io_context.hpp>
|
||||||
|
#include <boost/asio/io_context.hpp>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace beast {
|
||||||
|
|
||||||
|
template<class Executor = net::io_context::executor_type>
|
||||||
|
class test_executor
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// VFALCO These need to be atomic or something
|
||||||
|
struct info
|
||||||
|
{
|
||||||
|
int dispatch = 0;
|
||||||
|
int post = 0;
|
||||||
|
int defer = 0;
|
||||||
|
int work = 0;
|
||||||
|
int total = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
|
struct state
|
||||||
|
{
|
||||||
|
Executor ex;
|
||||||
|
info info_;
|
||||||
|
|
||||||
|
state(Executor const& ex_)
|
||||||
|
: ex(ex_)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
std::shared_ptr<state> sp_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
test_executor(test_executor const&) = default;
|
||||||
|
test_executor& operator=(test_executor const&) = default;
|
||||||
|
|
||||||
|
explicit
|
||||||
|
test_executor(Executor const& ex)
|
||||||
|
: sp_(std::make_shared<state>(ex))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
decltype(sp_->ex.context())
|
||||||
|
context() const noexcept
|
||||||
|
{
|
||||||
|
return sp_->ex.context();
|
||||||
|
}
|
||||||
|
|
||||||
|
info&
|
||||||
|
operator*() noexcept
|
||||||
|
{
|
||||||
|
return sp_->info_;
|
||||||
|
}
|
||||||
|
|
||||||
|
info*
|
||||||
|
operator->() noexcept
|
||||||
|
{
|
||||||
|
return &sp_->info_;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
on_work_started() const noexcept
|
||||||
|
{
|
||||||
|
++sp_->info_.work;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
on_work_finished() const noexcept
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class F, class A>
|
||||||
|
void
|
||||||
|
dispatch(F&& f, A const& a) const
|
||||||
|
{
|
||||||
|
++sp_->info_.dispatch;
|
||||||
|
++sp_->info_.total;
|
||||||
|
sp_->ex.dispatch(
|
||||||
|
std::forward<F>(f), a);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class F, class A>
|
||||||
|
void
|
||||||
|
post(F&& f, A const& a) const
|
||||||
|
{
|
||||||
|
++sp_->info_.post;
|
||||||
|
++sp_->info_.total;
|
||||||
|
sp_->ex.post(
|
||||||
|
std::forward<F>(f), a);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class F, class A>
|
||||||
|
void
|
||||||
|
defer(F&& f, A const& a) const
|
||||||
|
{
|
||||||
|
++sp_->info_.defer;
|
||||||
|
++sp_->info_.total;
|
||||||
|
sp_->ex.defer(
|
||||||
|
std::forward<F>(f), a);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // beast
|
||||||
|
} // boost
|
||||||
|
|
||||||
|
#endif
|
Reference in New Issue
Block a user