update boost

This commit is contained in:
2023-11-24 12:56:13 -06:00
parent cfc99971af
commit 19d727037a
9260 changed files with 849256 additions and 299957 deletions

View File

@@ -224,11 +224,14 @@ public:
*/
template<
class MutableBufferSequence,
class ReadHandler>
BOOST_BEAST_ASYNC_TPARAM2 ReadHandler =
net::default_completion_token_t<executor_type>
>
BOOST_BEAST_ASYNC_RESULT2(ReadHandler)
async_read_some(
MutableBufferSequence const& buffers,
ReadHandler&& handler);
ReadHandler&& handler =
net::default_completion_token_t<executor_type>{});
/** Write some data to the stream.
@@ -303,11 +306,14 @@ public:
*/
template<
class ConstBufferSequence,
class WriteHandler>
BOOST_BEAST_ASYNC_TPARAM2 WriteHandler =
net::default_completion_token_t<executor_type>
>
BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
async_write_some(
ConstBufferSequence const& buffers,
WriteHandler&& handler);
WriteHandler&& handler =
net::default_completion_token_t<executor_type>{});
};
} // http

View File

@@ -35,7 +35,7 @@ is_icy(ConstBufferSequence const& buffers)
char buf[3];
auto const n = net::buffer_copy(
net::mutable_buffer(buf, 3),
buffers);
buffers);
if(n >= 1 && buf[0] != 'I')
return false;
if(n >= 2 && buf[1] != 'C')
@@ -57,7 +57,7 @@ template<class Buffers, class Handler>
class read_op
: public beast::async_base<Handler,
beast::executor_type<icy_stream>>
, public net::coroutine
, public asio::coroutine
{
icy_stream& s_;
Buffers b_;
@@ -95,10 +95,16 @@ public:
{
// Try to read the first three characters
BOOST_ASIO_CORO_YIELD
s_.next_layer().async_read_some(
net::mutable_buffer(
s_.buf_ + s_.n_, 3 - s_.n_),
std::move(*this));
{
BOOST_ASIO_HANDLER_LOCATION((
__FILE__, __LINE__,
"http::icy_stream::async_read_some"));
s_.next_layer().async_read_some(
net::mutable_buffer(
s_.buf_ + s_.n_, 3 - s_.n_),
std::move(*this));
}
s_.n_ += static_cast<char>(bytes_transferred);
if(ec)
goto upcall;
@@ -127,8 +133,14 @@ public:
else
{
BOOST_ASIO_CORO_YIELD
s_.next_layer().async_read_some(
b_, std::move(*this));
{
BOOST_ASIO_HANDLER_LOCATION((
__FILE__, __LINE__,
"http::icy_stream::async_read_some"));
s_.next_layer().async_read_some(
b_, std::move(*this));
}
}
upcall:
if(! cont)
@@ -136,10 +148,16 @@ public:
ec_ = ec;
n_ = bytes_transferred;
BOOST_ASIO_CORO_YIELD
s_.next_layer().async_read_some(
net::mutable_buffer{},
std::move(*this));
ec = ec_;
{
BOOST_ASIO_HANDLER_LOCATION((
__FILE__, __LINE__,
"http::icy_stream::async_read_some"));
s_.next_layer().async_read_some(
net::mutable_buffer{},
std::move(*this));
}
BOOST_BEAST_ASSIGN_EC(ec, ec_);
bytes_transferred = n_;
}
this->complete_now(ec, bytes_transferred);
@@ -259,7 +277,7 @@ read_some(MutableBufferSequence const& buffers, error_code& ec)
template<class NextLayer>
template<
class MutableBufferSequence,
class ReadHandler>
BOOST_BEAST_ASYNC_TPARAM2 ReadHandler>
BOOST_BEAST_ASYNC_RESULT2(ReadHandler)
icy_stream<NextLayer>::
async_read_some(
@@ -311,7 +329,7 @@ write_some(MutableBufferSequence const& buffers, error_code& ec)
template<class NextLayer>
template<
class MutableBufferSequence,
class WriteHandler>
BOOST_BEAST_ASYNC_TPARAM2 WriteHandler>
BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
icy_stream<NextLayer>::
async_write_some(

View File

@@ -0,0 +1,138 @@
//
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
// Copyright (c) 2020 Richard Hodges (hodges.r@gmail.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_DETAIL_STREAM_STATE_HPP
#define BOOST_BEAST_TEST_DETAIL_STREAM_STATE_HPP
#include <boost/asio/any_io_executor.hpp>
#include <boost/beast/core/detail/config.hpp>
#include <boost/beast/_experimental/test/fail_count.hpp>
#include <boost/beast/core/detail/service_base.hpp>
#include <boost/beast/core/flat_buffer.hpp>
#include <boost/smart_ptr/weak_ptr.hpp>
#include <condition_variable>
#include <memory>
#include <mutex>
#include <vector>
namespace boost {
namespace beast {
namespace test {
namespace detail {
struct stream_state;
struct stream_service_impl
{
std::mutex m_;
std::vector<stream_state*> v_;
BOOST_BEAST_DECL
void
remove(stream_state& impl);
};
//------------------------------------------------------------------------------
class stream_service
: public beast::detail::service_base<stream_service>
{
boost::shared_ptr<detail::stream_service_impl> sp_;
BOOST_BEAST_DECL
void
shutdown() override;
public:
BOOST_BEAST_DECL
explicit
stream_service(net::execution_context& ctx);
BOOST_BEAST_DECL
static
auto
make_impl(
net::any_io_executor exec,
test::fail_count* fc) ->
boost::shared_ptr<detail::stream_state>;
};
//------------------------------------------------------------------------------
struct stream_read_op_base
{
virtual ~stream_read_op_base() = default;
virtual void operator()(error_code ec) = 0;
};
//------------------------------------------------------------------------------
enum class stream_status
{
ok,
eof,
};
//------------------------------------------------------------------------------
struct stream_state
{
net::any_io_executor exec;
boost::weak_ptr<stream_service_impl> wp;
std::mutex m;
flat_buffer b;
std::condition_variable cv;
std::unique_ptr<stream_read_op_base> op;
stream_status code = stream_status::ok;
fail_count* fc = nullptr;
std::size_t nread = 0;
std::size_t nread_bytes = 0;
std::size_t nwrite = 0;
std::size_t nwrite_bytes = 0;
std::size_t read_max =
(std::numeric_limits<std::size_t>::max)();
std::size_t write_max =
(std::numeric_limits<std::size_t>::max)();
BOOST_BEAST_DECL
stream_state(
net::any_io_executor exec_,
boost::weak_ptr<stream_service_impl> wp_,
fail_count* fc_);
BOOST_BEAST_DECL
~stream_state();
BOOST_BEAST_DECL
void
remove() noexcept;
BOOST_BEAST_DECL
void
notify_read();
BOOST_BEAST_DECL
void
cancel_read();
};
} // detail
} // test
} // beast
} // boost
#ifdef BOOST_BEAST_HEADER_ONLY
#include <boost/beast/_experimental/test/detail/stream_state.ipp>
#endif
#endif // BOOST_BEAST_TEST_DETAIL_STREAM_STATE_HPP

View File

@@ -0,0 +1,150 @@
//
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
// Copyright (c) 2020 Richard Hodges (hodges.r@gmail.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_DETAIL_STREAM_STATE_IPP
#define BOOST_BEAST_TEST_DETAIL_STREAM_STATE_IPP
#include <boost/beast/_experimental/test/error.hpp>
#include <boost/make_shared.hpp>
namespace boost {
namespace beast {
namespace test {
namespace detail {
//------------------------------------------------------------------------------
stream_service::
stream_service(net::execution_context& ctx)
: beast::detail::service_base<stream_service>(ctx)
, sp_(boost::make_shared<stream_service_impl>())
{
}
void
stream_service::
shutdown()
{
std::vector<std::unique_ptr<detail::stream_read_op_base>> v;
std::lock_guard<std::mutex> g1(sp_->m_);
v.reserve(sp_->v_.size());
for(auto p : sp_->v_)
{
std::lock_guard<std::mutex> g2(p->m);
v.emplace_back(std::move(p->op));
p->code = detail::stream_status::eof;
}
}
auto
stream_service::
make_impl(
net::any_io_executor exec,
test::fail_count* fc) ->
boost::shared_ptr<detail::stream_state>
{
#if defined(BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
auto& ctx = exec.context();
#else
auto& ctx = net::query(
exec,
net::execution::context);
#endif
auto& svc = net::use_service<stream_service>(ctx);
auto sp = boost::make_shared<detail::stream_state>(exec, svc.sp_, fc);
std::lock_guard<std::mutex> g(svc.sp_->m_);
svc.sp_->v_.push_back(sp.get());
return sp;
}
//------------------------------------------------------------------------------
void
stream_service_impl::
remove(stream_state& impl)
{
std::lock_guard<std::mutex> g(m_);
*std::find(
v_.begin(), v_.end(),
&impl) = std::move(v_.back());
v_.pop_back();
}
//------------------------------------------------------------------------------
stream_state::
stream_state(
net::any_io_executor exec_,
boost::weak_ptr<stream_service_impl> wp_,
fail_count* fc_)
: exec(std::move(exec_))
, wp(std::move(wp_))
, fc(fc_)
{
}
stream_state::
~stream_state()
{
// cancel outstanding read
if(op != nullptr)
(*op)(net::error::operation_aborted);
}
void
stream_state::
remove() noexcept
{
auto sp = wp.lock();
// If this goes off, it means the lifetime of a test::stream object
// extended beyond the lifetime of the associated execution context.
BOOST_ASSERT(sp);
sp->remove(*this);
}
void
stream_state::
notify_read()
{
if(op)
{
auto op_ = std::move(op);
op_->operator()(error_code{});
}
else
{
cv.notify_all();
}
}
void
stream_state::
cancel_read()
{
std::unique_ptr<stream_read_op_base> p;
{
std::lock_guard<std::mutex> lock(m);
code = stream_status::eof;
p = std::move(op);
}
if(p != nullptr)
(*p)(net::error::operation_aborted);
}
} // detail
} // test
} // beast
} // boost
#endif // BOOST_BEAST_TEST_DETAIL_STREAM_STATE_IPP

View File

@@ -35,47 +35,55 @@ class handler
{
boost::optional<error_code> ec_;
bool pass_ = false;
boost::source_location loc_{BOOST_CURRENT_LOCATION};
public:
handler() = default;
handler(boost::source_location loc = BOOST_CURRENT_LOCATION) : loc_(loc) {}
explicit
handler(error_code ec)
: ec_(ec)
handler(error_code ec, boost::source_location loc = BOOST_CURRENT_LOCATION)
: ec_(ec), loc_(loc)
{
}
explicit
handler(boost::none_t)
handler(boost::none_t, boost::source_location loc = BOOST_CURRENT_LOCATION) : loc_(loc)
{
}
handler(handler&& other)
: ec_(other.ec_)
, pass_(boost::exchange(other.pass_, true))
, loc_(other.loc_)
{
}
~handler()
{
BEAST_EXPECT(pass_);
::boost::beast::unit_test::suite::this_suite()->expect(pass_, loc_.file_name(), loc_.line());
}
template<class... Args>
void
operator()(error_code ec, Args&&...)
{
BEAST_EXPECT(! pass_); // can't call twice
BEAST_EXPECTS(! ec_ || ec == *ec_,
ec.message());
::boost::beast::unit_test::suite::this_suite()->expect(!pass_, loc_.file_name(), loc_.line());
if (ec_ && ec != *ec_)
::boost::beast::unit_test::suite::this_suite()->fail(ec.message(), loc_.file_name(), loc_.line());
else
::boost::beast::unit_test::suite::this_suite()->pass();
pass_ = true;
}
void
operator()()
{
BEAST_EXPECT(! pass_); // can't call twice
BEAST_EXPECT(! ec_);
::boost::beast::unit_test::suite::this_suite()->expect(!pass_, loc_.file_name(), loc_.line());
if (ec_ && ec_->failed())
::boost::beast::unit_test::suite::this_suite()->fail(ec_->message(), loc_.file_name(), loc_.line());
else
::boost::beast::unit_test::suite::this_suite()->pass();
pass_ = true;
}
@@ -85,8 +93,11 @@ public:
void
operator()(Arg0&&, Args&&...)
{
BEAST_EXPECT(! pass_); // can't call twice
BEAST_EXPECT(! ec_);
::boost::beast::unit_test::suite::this_suite()->expect(!pass_, loc_.file_name(), loc_.line());
if (ec_ && ec_->failed())
::boost::beast::unit_test::suite::this_suite()->fail(ec_->message(), loc_.file_name(), loc_.line());
else
::boost::beast::unit_test::suite::this_suite()->pass();
pass_ = true;
}
};
@@ -103,9 +114,9 @@ public:
*/
inline
handler
success_handler() noexcept
success_handler(boost::source_location loc = BOOST_CURRENT_LOCATION) noexcept
{
return handler(error_code{});
return handler(error_code{}, loc);
}
/** Return a test CompletionHandler which requires invocation.
@@ -117,9 +128,9 @@ success_handler() noexcept
*/
inline
handler
any_handler() noexcept
any_handler(boost::source_location loc = BOOST_CURRENT_LOCATION) noexcept
{
return handler(boost::none);
return handler(boost::none, loc);
}
/** Return a test CompletionHandler which requires a specific error code.
@@ -136,9 +147,9 @@ any_handler() noexcept
*/
inline
handler
fail_handler(error_code ec) noexcept
fail_handler(error_code ec,boost::source_location loc = BOOST_CURRENT_LOCATION) noexcept
{
return handler(ec);
return handler(ec, loc);
}
/** Run an I/O context.

View File

@@ -12,9 +12,11 @@
#include <boost/beast/core/bind_handler.hpp>
#include <boost/beast/core/buffer_traits.hpp>
#include <boost/beast/core/buffers_prefix.hpp>
#include <boost/beast/core/detail/service_base.hpp>
#include <boost/beast/core/detail/type_traits.hpp>
#include <boost/beast/core/detail/is_invocable.hpp>
#include <boost/asio/any_io_executor.hpp>
#include <boost/asio/dispatch.hpp>
#include <boost/asio/post.hpp>
#include <mutex>
#include <stdexcept>
#include <vector>
@@ -25,55 +27,25 @@ namespace test {
//------------------------------------------------------------------------------
struct stream::service_impl
{
std::mutex m_;
std::vector<state*> v_;
BOOST_BEAST_DECL
void
remove(state& impl);
};
class stream::service
: public beast::detail::service_base<service>
{
boost::shared_ptr<service_impl> sp_;
BOOST_BEAST_DECL
void
shutdown() override;
public:
BOOST_BEAST_DECL
explicit
service(net::execution_context& ctx);
BOOST_BEAST_DECL
static
auto
make_impl(
net::io_context& ctx,
test::fail_count* fc) ->
boost::shared_ptr<state>;
};
//------------------------------------------------------------------------------
template<class Executor>
template<class Handler, class Buffers>
class stream::read_op : public stream::read_op_base
class basic_stream<Executor>::read_op : public detail::stream_read_op_base
{
using ex1_type =
net::io_context::executor_type;
executor_type;
using ex2_type
= net::associated_executor_t<Handler, ex1_type>;
struct lambda
{
Handler h_;
boost::weak_ptr<state> wp_;
boost::weak_ptr<detail::stream_state> wp_;
Buffers b_;
#if defined(BOOST_ASIO_NO_TS_EXECUTORS)
net::any_io_executor wg2_;
#else // defined(BOOST_ASIO_NO_TS_EXECUTORS)
net::executor_work_guard<ex2_type> wg2_;
#endif // defined(BOOST_ASIO_NO_TS_EXECUTORS)
lambda(lambda&&) = default;
lambda(lambda const&) = default;
@@ -81,23 +53,49 @@ class stream::read_op : public stream::read_op_base
template<class Handler_>
lambda(
Handler_&& h,
boost::shared_ptr<state> const& s,
boost::shared_ptr<detail::stream_state> const& s,
Buffers const& b)
: h_(std::forward<Handler_>(h))
, wp_(s)
, b_(b)
#if defined(BOOST_ASIO_NO_TS_EXECUTORS)
, wg2_(net::prefer(
net::get_associated_executor(
h_, s->exec),
net::execution::outstanding_work.tracked))
#else // defined(BOOST_ASIO_NO_TS_EXECUTORS)
, wg2_(net::get_associated_executor(
h_, s->ioc.get_executor()))
h_, s->exec))
#endif // defined(BOOST_ASIO_NO_TS_EXECUTORS)
{
}
using allocator_type = net::associated_allocator_t<Handler>;
allocator_type get_allocator() const noexcept
{
return net::get_associated_allocator(h_);
}
using cancellation_slot_type =
net::associated_cancellation_slot_t<Handler>;
cancellation_slot_type
get_cancellation_slot() const noexcept
{
return net::get_associated_cancellation_slot(h_,
net::cancellation_slot());
}
void
operator()(error_code ec)
{
std::size_t bytes_transferred = 0;
auto sp = wp_.lock();
if(! sp)
ec = net::error::operation_aborted;
{
BOOST_BEAST_ASSIGN_EC(ec, net::error::operation_aborted);
}
if(! ec)
{
std::lock_guard<std::mutex> lock(sp->m);
@@ -108,47 +106,67 @@ class stream::read_op : public stream::read_op_base
net::buffer_copy(
b_, sp->b.data(), sp->read_max);
sp->b.consume(bytes_transferred);
sp->nread_bytes += bytes_transferred;
}
else if (buffer_bytes(b_) > 0)
{
ec = net::error::eof;
BOOST_BEAST_ASSIGN_EC(ec, net::error::eof);
}
}
auto alloc = net::get_associated_allocator(h_);
wg2_.get_executor().dispatch(
#if defined(BOOST_ASIO_NO_TS_EXECUTORS)
net::dispatch(wg2_,
beast::bind_front_handler(std::move(h_),
ec, bytes_transferred), alloc);
ec, bytes_transferred));
wg2_ = net::any_io_executor(); // probably unnecessary
#else // defined(BOOST_ASIO_NO_TS_EXECUTORS)
net::dispatch(wg2_.get_executor(),
beast::bind_front_handler(std::move(h_),
ec, bytes_transferred));
wg2_.reset();
#endif // defined(BOOST_ASIO_NO_TS_EXECUTORS)
}
};
lambda fn_;
net::executor_work_guard<ex1_type> wg1_;
#if defined(BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
net::executor_work_guard<net::any_io_executor> wg1_;
#else
net::any_io_executor wg1_;
#endif
public:
template<class Handler_>
read_op(
Handler_&& h,
boost::shared_ptr<state> const& s,
boost::shared_ptr<detail::stream_state> const& s,
Buffers const& b)
: fn_(std::forward<Handler_>(h), s, b)
, wg1_(s->ioc.get_executor())
#if defined(BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
, wg1_(s->exec)
#else
, wg1_(net::prefer(s->exec,
net::execution::outstanding_work.tracked))
#endif
{
}
void
operator()(error_code ec) override
{
auto alloc = net::get_associated_allocator(fn_.h_);
wg1_.get_executor().post(
beast::bind_front_handler(std::move(fn_), ec), alloc);
#if defined(BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
net::post(wg1_.get_executor(),
beast::bind_front_handler(std::move(fn_), ec));
wg1_.reset();
#else
net::post(wg1_, beast::bind_front_handler(std::move(fn_), ec));
wg1_ = net::any_io_executor(); // probably unnecessary
#endif
}
};
struct stream::run_read_op
template<class Executor>
struct basic_stream<Executor>::run_read_op
{
template<
class ReadHandler,
@@ -156,7 +174,7 @@ struct stream::run_read_op
void
operator()(
ReadHandler&& h,
boost::shared_ptr<state> const& in,
boost::shared_ptr<detail::stream_state> const& in,
MutableBufferSequence const& buffers)
{
// If you get an error on the following line it means
@@ -170,7 +188,7 @@ struct stream::run_read_op
initiate_read(
in,
std::unique_ptr<read_op_base>{
std::unique_ptr<detail::stream_read_op_base>{
new read_op<
typename std::decay<ReadHandler>::type,
MutableBufferSequence>(
@@ -181,7 +199,8 @@ struct stream::run_read_op
}
};
struct stream::run_write_op
template<class Executor>
struct basic_stream<Executor>::run_write_op
{
template<
class WriteHandler,
@@ -189,8 +208,8 @@ struct stream::run_write_op
void
operator()(
WriteHandler&& h,
boost::shared_ptr<state> in_,
boost::weak_ptr<state> out_,
boost::shared_ptr<detail::stream_state> in_,
boost::weak_ptr<detail::stream_state> out_,
ConstBufferSequence const& buffers)
{
// If you get an error on the following line it means
@@ -206,7 +225,7 @@ struct stream::run_write_op
auto const upcall = [&](error_code ec, std::size_t n)
{
net::post(
in_->ioc.get_executor(),
in_->exec,
beast::bind_front_handler(std::move(h), ec, n));
};
@@ -232,6 +251,7 @@ struct stream::run_write_op
std::lock_guard<std::mutex> lock(out->m);
n = net::buffer_copy(out->b.prepare(n), buffers);
out->b.commit(n);
out->nwrite_bytes += n;
out->notify_read();
}
BOOST_ASSERT(! ec);
@@ -241,9 +261,10 @@ struct stream::run_write_op
//------------------------------------------------------------------------------
template<class Executor>
template<class MutableBufferSequence>
std::size_t
stream::
basic_stream<Executor>::
read_some(MutableBufferSequence const& buffers)
{
static_assert(net::is_mutable_buffer_sequence<
@@ -256,9 +277,10 @@ read_some(MutableBufferSequence const& buffers)
return n;
}
template<class Executor>
template<class MutableBufferSequence>
std::size_t
stream::
basic_stream<Executor>::
read_some(MutableBufferSequence const& buffers,
error_code& ec)
{
@@ -286,7 +308,7 @@ read_some(MutableBufferSequence const& buffers,
{
return
in_->b.size() > 0 ||
in_->code != status::ok;
in_->code != detail::stream_status::ok;
});
// deliver bytes before eof
@@ -295,18 +317,21 @@ read_some(MutableBufferSequence const& buffers,
auto const n = net::buffer_copy(
buffers, in_->b.data(), in_->read_max);
in_->b.consume(n);
in_->nread_bytes += n;
return n;
}
// deliver error
BOOST_ASSERT(in_->code != status::ok);
ec = net::error::eof;
BOOST_ASSERT(in_->code != detail::stream_status::ok);
BOOST_BEAST_ASSIGN_EC(ec, net::error::eof);
return 0;
}
template<class MutableBufferSequence, class ReadHandler>
BOOST_BEAST_ASYNC_RESULT2(ReadHandler)
stream::
template<class Executor>
template<class MutableBufferSequence,
BOOST_ASIO_COMPLETION_TOKEN_FOR(void(error_code, std::size_t)) ReadHandler>
BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, void(error_code, std::size_t))
basic_stream<Executor>::
async_read_some(
MutableBufferSequence const& buffers,
ReadHandler&& handler)
@@ -324,9 +349,10 @@ async_read_some(
buffers);
}
template<class Executor>
template<class ConstBufferSequence>
std::size_t
stream::
basic_stream<Executor>::
write_some(ConstBufferSequence const& buffers)
{
static_assert(net::is_const_buffer_sequence<
@@ -340,9 +366,10 @@ write_some(ConstBufferSequence const& buffers)
return bytes_transferred;
}
template<class Executor>
template<class ConstBufferSequence>
std::size_t
stream::
basic_stream<Executor>::
write_some(
ConstBufferSequence const& buffers, error_code& ec)
{
@@ -367,7 +394,7 @@ write_some(
auto out = out_.lock();
if(! out)
{
ec = net::error::connection_reset;
BOOST_BEAST_ASSIGN_EC(ec, net::error::connection_reset);
return 0;
}
@@ -378,14 +405,17 @@ write_some(
std::lock_guard<std::mutex> lock(out->m);
n = net::buffer_copy(out->b.prepare(n), buffers);
out->b.commit(n);
out->nwrite_bytes += n;
out->notify_read();
}
return n;
}
template<class ConstBufferSequence, class WriteHandler>
BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
stream::
template<class Executor>
template<class ConstBufferSequence,
BOOST_ASIO_COMPLETION_TOKEN_FOR(void(error_code, std::size_t)) WriteHandler>
BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler, void(error_code, std::size_t))
basic_stream<Executor>::
async_write_some(
ConstBufferSequence const& buffers,
WriteHandler&& handler)
@@ -406,11 +436,11 @@ async_write_some(
//------------------------------------------------------------------------------
template<class TeardownHandler>
template<class Executor, class TeardownHandler>
void
async_teardown(
role_type,
stream& s,
basic_stream<Executor>& s,
TeardownHandler&& handler)
{
error_code ec;
@@ -423,7 +453,9 @@ async_teardown(
s.close();
if( s.in_->fc &&
s.in_->fc->fail(ec))
ec = net::error::eof;
{
BOOST_BEAST_ASSIGN_EC(ec, net::error::eof);
}
else
ec = {};
@@ -435,8 +467,8 @@ async_teardown(
//------------------------------------------------------------------------------
template<class Arg1, class... ArgN>
stream
template<class Executor, class Arg1, class... ArgN>
basic_stream<Executor>
connect(stream& to, Arg1&& arg1, ArgN&&... argn)
{
stream from{
@@ -446,6 +478,34 @@ connect(stream& to, Arg1&& arg1, ArgN&&... argn)
return from;
}
namespace detail
{
template<class To>
struct extract_executor_op
{
To operator()(net::any_io_executor& ex) const
{
assert(ex.template target<To>());
return *ex.template target<To>();
}
};
template<>
struct extract_executor_op<net::any_io_executor>
{
net::any_io_executor operator()(net::any_io_executor& ex) const
{
return ex;
}
};
}
template<class Executor>
auto basic_stream<Executor>::get_executor() noexcept -> executor_type
{
return detail::extract_executor_op<Executor>()(in_->exec);
}
} // test
} // beast
} // boost

View File

@@ -13,7 +13,6 @@
#include <boost/beast/_experimental/test/stream.hpp>
#include <boost/beast/core/bind_handler.hpp>
#include <boost/beast/core/buffer_traits.hpp>
#include <boost/beast/core/buffers_prefix.hpp>
#include <boost/make_shared.hpp>
#include <stdexcept>
#include <vector>
@@ -24,62 +23,10 @@ namespace test {
//------------------------------------------------------------------------------
stream::
service::
service(net::execution_context& ctx)
: beast::detail::service_base<service>(ctx)
, sp_(boost::make_shared<service_impl>())
{
}
void
stream::
service::
shutdown()
{
std::vector<std::unique_ptr<read_op_base>> v;
std::lock_guard<std::mutex> g1(sp_->m_);
v.reserve(sp_->v_.size());
for(auto p : sp_->v_)
{
std::lock_guard<std::mutex> g2(p->m);
v.emplace_back(std::move(p->op));
p->code = status::eof;
}
}
auto
stream::
service::
make_impl(
net::io_context& ctx,
test::fail_count* fc) ->
boost::shared_ptr<state>
{
auto& svc = net::use_service<service>(ctx);
auto sp = boost::make_shared<state>(ctx, svc.sp_, fc);
std::lock_guard<std::mutex> g(svc.sp_->m_);
svc.sp_->v_.push_back(sp.get());
return sp;
}
void
stream::
service_impl::
remove(state& impl)
{
std::lock_guard<std::mutex> g(m_);
*std::find(
v_.begin(), v_.end(),
&impl) = std::move(v_.back());
v_.pop_back();
}
//------------------------------------------------------------------------------
void stream::initiate_read(
boost::shared_ptr<state> const& in_,
std::unique_ptr<stream::read_op_base>&& op,
template<class Executor>
void basic_stream<Executor>::initiate_read(
boost::shared_ptr<detail::stream_state> const& in_,
std::unique_ptr<detail::stream_read_op_base>&& op,
std::size_t buf_size)
{
std::unique_lock<std::mutex> lock(in_->m);
@@ -107,7 +54,7 @@ void stream::initiate_read(
}
// deliver error
if(in_->code != status::ok)
if(in_->code != detail::stream_status::ok)
{
lock.unlock();
(*op)(net::error::eof);
@@ -118,98 +65,36 @@ void stream::initiate_read(
in_->op = std::move(op);
}
stream::
state::
state(
net::io_context& ioc_,
boost::weak_ptr<service_impl> wp_,
fail_count* fc_)
: ioc(ioc_)
, wp(std::move(wp_))
, fc(fc_)
{
}
stream::
state::
~state()
{
// cancel outstanding read
if(op != nullptr)
(*op)(net::error::operation_aborted);
}
void
stream::
state::
remove() noexcept
{
auto sp = wp.lock();
// If this goes off, it means the lifetime of a test::stream object
// extended beyond the lifetime of the associated execution context.
BOOST_ASSERT(sp);
sp->remove(*this);
}
void
stream::
state::
notify_read()
{
if(op)
{
auto op_ = std::move(op);
op_->operator()(error_code{});
}
else
{
cv.notify_all();
}
}
void
stream::
state::
cancel_read()
{
std::unique_ptr<read_op_base> p;
{
std::lock_guard<std::mutex> lock(m);
code = status::eof;
p = std::move(op);
}
if(p != nullptr)
(*p)(net::error::operation_aborted);
}
//------------------------------------------------------------------------------
stream::
~stream()
template<class Executor>
basic_stream<Executor>::
~basic_stream()
{
close();
in_->remove();
}
stream::
stream(stream&& other)
template<class Executor>
basic_stream<Executor>::
basic_stream(basic_stream&& other)
{
auto in = service::make_impl(
other.in_->ioc, other.in_->fc);
auto in = detail::stream_service::make_impl(
other.in_->exec, other.in_->fc);
in_ = std::move(other.in_);
out_ = std::move(other.out_);
other.in_ = in;
}
stream&
stream::
operator=(stream&& other)
template<class Executor>
basic_stream<Executor>&
basic_stream<Executor>::
operator=(basic_stream&& other)
{
close();
auto in = service::make_impl(
other.in_->ioc, other.in_->fc);
auto in = detail::stream_service::make_impl(
other.in_->exec, other.in_->fc);
in_->remove();
in_ = std::move(other.in_);
out_ = std::move(other.out_);
@@ -219,68 +104,78 @@ operator=(stream&& other)
//------------------------------------------------------------------------------
stream::
stream(net::io_context& ioc)
: in_(service::make_impl(ioc, nullptr))
template<class Executor>
basic_stream<Executor>::
basic_stream(executor_type exec)
: in_(detail::stream_service::make_impl(std::move(exec), nullptr))
{
}
stream::
stream(
template<class Executor>
basic_stream<Executor>::
basic_stream(
net::io_context& ioc,
fail_count& fc)
: in_(service::make_impl(ioc, &fc))
: in_(detail::stream_service::make_impl(ioc.get_executor(), &fc))
{
}
stream::
stream(
template<class Executor>
basic_stream<Executor>::
basic_stream(
net::io_context& ioc,
string_view s)
: in_(service::make_impl(ioc, nullptr))
: in_(detail::stream_service::make_impl(ioc.get_executor(), nullptr))
{
in_->b.commit(net::buffer_copy(
in_->b.prepare(s.size()),
net::buffer(s.data(), s.size())));
}
stream::
stream(
template<class Executor>
basic_stream<Executor>::
basic_stream(
net::io_context& ioc,
fail_count& fc,
string_view s)
: in_(service::make_impl(ioc, &fc))
: in_(detail::stream_service::make_impl(ioc.get_executor(), &fc))
{
in_->b.commit(net::buffer_copy(
in_->b.prepare(s.size()),
net::buffer(s.data(), s.size())));
}
template<class Executor>
void
stream::
connect(stream& remote)
basic_stream<Executor>::
connect(basic_stream& remote)
{
BOOST_ASSERT(! out_.lock());
BOOST_ASSERT(! remote.out_.lock());
std::lock(in_->m, remote.in_->m);
std::lock_guard<std::mutex> guard1{in_->m, std::adopt_lock};
std::lock_guard<std::mutex> guard2{remote.in_->m, std::adopt_lock};
out_ = remote.in_;
remote.out_ = in_;
in_->code = status::ok;
remote.in_->code = status::ok;
in_->code = detail::stream_status::ok;
remote.in_->code = detail::stream_status::ok;
}
template<class Executor>
string_view
stream::
basic_stream<Executor>::
str() const
{
auto const bs = in_->b.data();
if(buffer_bytes(bs) == 0)
return {};
auto const b = beast::buffers_front(bs);
net::const_buffer const b = *net::buffer_sequence_begin(bs);
return {static_cast<char const*>(b.data()), b.size()};
}
template<class Executor>
void
stream::
basic_stream<Executor>::
append(string_view s)
{
std::lock_guard<std::mutex> lock{in_->m};
@@ -289,16 +184,18 @@ append(string_view s)
net::buffer(s.data(), s.size())));
}
template<class Executor>
void
stream::
basic_stream<Executor>::
clear()
{
std::lock_guard<std::mutex> lock{in_->m};
in_->b.consume(in_->b.size());
}
template<class Executor>
void
stream::
basic_stream<Executor>::
close()
{
in_->cancel_read();
@@ -312,31 +209,33 @@ close()
if(out)
{
std::lock_guard<std::mutex> lock(out->m);
if(out->code == status::ok)
if(out->code == detail::stream_status::ok)
{
out->code = status::eof;
out->code = detail::stream_status::eof;
out->notify_read();
}
}
}
}
template<class Executor>
void
stream::
basic_stream<Executor>::
close_remote()
{
std::lock_guard<std::mutex> lock{in_->m};
if(in_->code == status::ok)
if(in_->code == detail::stream_status::ok)
{
in_->code = status::eof;
in_->code = detail::stream_status::eof;
in_->notify_read();
}
}
template<class Executor>
void
teardown(
role_type,
stream& s,
basic_stream<Executor>& s,
boost::system::error_code& ec)
{
if( s.in_->fc &&
@@ -347,23 +246,27 @@ teardown(
if( s.in_->fc &&
s.in_->fc->fail(ec))
ec = net::error::eof;
{
BOOST_BEAST_ASSIGN_EC(ec, net::error::eof);
}
else
ec = {};
}
//------------------------------------------------------------------------------
stream
connect(stream& to)
template<class Executor>
basic_stream<Executor>
connect(basic_stream<Executor>& to)
{
stream from{to.get_executor().context()};
basic_stream<Executor> from(to.get_executor());
from.connect(to);
return from;
}
template<class Executor>
void
connect(stream& s1, stream& s2)
connect(basic_stream<Executor>& s1, basic_stream<Executor>& s2)
{
s1.connect(s2);
}

View File

@@ -16,10 +16,12 @@
#include <boost/beast/core/role.hpp>
#include <boost/beast/core/string.hpp>
#include <boost/beast/_experimental/test/fail_count.hpp>
#include <boost/beast/_experimental/test/detail/stream_state.hpp>
#include <boost/asio/async_result.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/error.hpp>
#include <boost/asio/executor_work_guard.hpp>
#include <boost/asio/any_io_executor.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/post.hpp>
#include <boost/assert.hpp>
@@ -106,69 +108,45 @@ namespace test {
@li <em>AsyncReadStream</em>
@li <em>AsyncWriteStream</em>
*/
class stream
template<class Executor = net::any_io_executor>
class basic_stream;
template<class Executor>
void
teardown(
role_type,
basic_stream<Executor>& s,
boost::system::error_code& ec);
template<class Executor, class TeardownHandler>
void
async_teardown(
role_type role,
basic_stream<Executor>& s,
TeardownHandler&& handler);
template<class Executor>
class basic_stream
{
struct state;
public:
/// The type of the executor associated with the object.
using executor_type =
Executor;
boost::shared_ptr<state> in_;
boost::weak_ptr<state> out_;
enum class status
/// Rebinds the socket type to another executor.
template <typename Executor1>
struct rebind_executor
{
ok,
eof,
/// The socket type when rebound to the specified executor.
typedef basic_stream<Executor1> other;
};
class service;
struct service_impl;
private:
template<class Executor2>
friend class basic_stream;
struct read_op_base
{
virtual ~read_op_base() = default;
virtual void operator()(error_code ec) = 0;
};
struct state
{
friend class stream;
net::io_context& ioc;
boost::weak_ptr<service_impl> wp;
std::mutex m;
flat_buffer b;
std::condition_variable cv;
std::unique_ptr<read_op_base> op;
status code = status::ok;
fail_count* fc = nullptr;
std::size_t nread = 0;
std::size_t nwrite = 0;
std::size_t read_max =
(std::numeric_limits<std::size_t>::max)();
std::size_t write_max =
(std::numeric_limits<std::size_t>::max)();
BOOST_BEAST_DECL
state(
net::io_context& ioc_,
boost::weak_ptr<service_impl> wp_,
fail_count* fc_);
BOOST_BEAST_DECL
~state();
BOOST_BEAST_DECL
void
remove() noexcept;
BOOST_BEAST_DECL
void
notify_read();
BOOST_BEAST_DECL
void
cancel_read();
};
boost::shared_ptr<detail::stream_state> in_;
boost::weak_ptr<detail::stream_state> out_;
template<class Handler, class Buffers>
class read_op;
@@ -176,12 +154,11 @@ class stream
struct run_read_op;
struct run_write_op;
BOOST_BEAST_DECL
static
void
initiate_read(
boost::shared_ptr<state> const& in,
std::unique_ptr<read_op_base>&& op,
boost::shared_ptr<detail::stream_state> const& in,
std::unique_ptr<detail::stream_read_op_base>&& op,
std::size_t buf_size);
#if ! BOOST_BEAST_DOXYGEN
@@ -190,7 +167,7 @@ class stream
template<class>
friend class boost::asio::ssl::stream;
// DEPRECATED
using lowest_layer_type = stream;
using lowest_layer_type = basic_stream;
// DEPRECATED
lowest_layer_type&
lowest_layer() noexcept
@@ -218,36 +195,66 @@ public:
the peer will see the error `net::error::connection_reset`
when performing any reads or writes.
*/
BOOST_BEAST_DECL
~stream();
~basic_stream();
/** Move Constructor
Moving the stream while asynchronous operations are pending
results in undefined behavior.
*/
BOOST_BEAST_DECL
stream(stream&& other);
basic_stream(basic_stream&& other);
/** Move Constructor
Moving the stream while asynchronous operations are pending
results in undefined behavior.
*/
template<class Executor2>
basic_stream(basic_stream<Executor2>&& other)
: in_(std::move(other.in_))
, out_(std::move(other.out_))
{
BOOST_ASSERT(in_->exec.template target<Executor2>() != nullptr);
in_->exec = executor_type(*in_->exec.template target<Executor2>());
}
/** Move Assignment
Moving the stream while asynchronous operations are pending
results in undefined behavior.
*/
BOOST_BEAST_DECL
stream&
operator=(stream&& other);
basic_stream&
operator=(basic_stream&& other);
template<class Executor2>
basic_stream&
operator==(basic_stream<Executor2>&& other);
/** Construct a stream
The stream will be created in a disconnected state.
@param ioc The `io_context` object that the stream will use to
@param context The `io_context` object that the stream will use to
dispatch handlers for any asynchronous operations.
*/
template <typename ExecutionContext>
explicit basic_stream(ExecutionContext& context,
typename std::enable_if<
std::is_convertible<ExecutionContext&, net::execution_context&>::value
>::type* = 0)
: basic_stream(context.get_executor())
{
}
/** Construct a stream
The stream will be created in a disconnected state.
@param exec The `executor` object that the stream will use to
dispatch handlers for any asynchronous operations.
*/
BOOST_BEAST_DECL
explicit
stream(net::io_context& ioc);
basic_stream(executor_type exec);
/** Construct a stream
@@ -261,8 +268,7 @@ public:
fail count. When the fail count reaches its internal limit,
a simulated failure error will be raised.
*/
BOOST_BEAST_DECL
stream(
basic_stream(
net::io_context& ioc,
fail_count& fc);
@@ -276,8 +282,7 @@ public:
@param s A string which will be appended to the input area, not
including the null terminator.
*/
BOOST_BEAST_DECL
stream(
basic_stream(
net::io_context& ioc,
string_view s);
@@ -296,27 +301,18 @@ public:
@param s A string which will be appended to the input area, not
including the null terminator.
*/
BOOST_BEAST_DECL
stream(
basic_stream(
net::io_context& ioc,
fail_count& fc,
string_view s);
/// Establish a connection
BOOST_BEAST_DECL
void
connect(stream& remote);
/// The type of the executor associated with the object.
using executor_type =
net::io_context::executor_type;
connect(basic_stream& remote);
/// Return the executor associated with the object.
executor_type
get_executor() noexcept
{
return in_->ioc.get_executor();
};
get_executor() noexcept;
/// Set the maximum number of bytes returned by read_some
void
@@ -340,17 +336,14 @@ public:
}
/// Returns a string view representing the pending input data
BOOST_BEAST_DECL
string_view
str() const;
/// Appends a string to the pending input data
BOOST_BEAST_DECL
void
append(string_view s);
/// Clear the pending input area
BOOST_BEAST_DECL
void
clear();
@@ -361,6 +354,13 @@ public:
return in_->nread;
}
/// Return the number of bytes read
std::size_t
nread_bytes() const noexcept
{
return in_->nread_bytes;
}
/// Return the number of writes
std::size_t
nwrite() const noexcept
@@ -368,12 +368,18 @@ public:
return in_->nwrite;
}
/// Return the number of bytes written
std::size_t
nwrite_bytes() const noexcept
{
return in_->nwrite_bytes;
}
/** Close the stream.
The other end of the connection will see
`error::eof` after reading all the remaining data.
*/
BOOST_BEAST_DECL
void
close();
@@ -382,7 +388,6 @@ public:
This end of the connection will see
`error::eof` after reading all the remaining data.
*/
BOOST_BEAST_DECL
void
close_remote();
@@ -459,10 +464,14 @@ public:
to ensure that the requested amount of data is read before the asynchronous
operation completes.
*/
template<class MutableBufferSequence, class ReadHandler>
BOOST_BEAST_ASYNC_RESULT2(ReadHandler)
async_read_some(MutableBufferSequence const& buffers,
ReadHandler&& handler);
template<
class MutableBufferSequence,
BOOST_ASIO_COMPLETION_TOKEN_FOR(void(error_code, std::size_t)) ReadHandler
BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, void(error_code, std::size_t))
async_read_some(
MutableBufferSequence const& buffers,
ReadHandler&& handler BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type));
/** Write some data to the stream.
@@ -534,35 +543,38 @@ public:
the peer. Consider using the function `net::async_write` if you need
to ensure that all data is written before the asynchronous operation completes.
*/
template<class ConstBufferSequence, class WriteHandler>
BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
async_write_some(ConstBufferSequence const& buffers,
WriteHandler&& handler);
template<
class ConstBufferSequence,
BOOST_ASIO_COMPLETION_TOKEN_FOR(void(error_code, std::size_t)) WriteHandler
BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler, void(error_code, std::size_t))
async_write_some(
ConstBufferSequence const& buffers,
WriteHandler&& handler BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)
);
#if ! BOOST_BEAST_DOXYGEN
friend
BOOST_BEAST_DECL
void
teardown(
teardown<>(
role_type,
stream& s,
basic_stream& s,
boost::system::error_code& ec);
template<class TeardownHandler>
template<class Ex2, class TeardownHandler>
friend
BOOST_BEAST_DECL
void
async_teardown(
role_type role,
stream& s,
basic_stream<Ex2>& s,
TeardownHandler&& handler);
#endif
};
#if ! BOOST_BEAST_DOXYGEN
inline
template<class Executor>
void
beast_close_socket(stream& s)
beast_close_socket(basic_stream<Executor>& s)
{
s.close();
}
@@ -577,31 +589,34 @@ beast_close_socket(stream& s)
@return The new, connected stream.
*/
template<class Executor>
template<class... Args>
stream
connect(stream& to, Args&&... args);
basic_stream
connect(basic_stream& to, Args&&... args);
#else
BOOST_BEAST_DECL
stream
connect(stream& to);
template<class Executor>
basic_stream<Executor>
connect(basic_stream<Executor>& to);
BOOST_BEAST_DECL
template<class Executor>
void
connect(stream& s1, stream& s2);
connect(basic_stream<Executor>& s1, basic_stream<Executor>& s2);
template<class Arg1, class... ArgN>
stream
connect(stream& to, Arg1&& arg1, ArgN&&... argn);
template<class Executor, class Arg1, class... ArgN>
basic_stream<Executor>
connect(basic_stream<Executor>& to, Arg1&& arg1, ArgN&&... argn);
#endif
using stream = basic_stream<>;
} // test
} // beast
} // boost
#include <boost/beast/_experimental/test/impl/stream.hpp>
#ifdef BOOST_BEAST_HEADER_ONLY
//#ifdef BOOST_BEAST_HEADER_ONLY
#include <boost/beast/_experimental/test/impl/stream.ipp>
#endif
//#endif
#endif

View File

@@ -193,6 +193,10 @@ public:
{
}
virtual ~suite() = default;
suite(suite const&) = delete;
suite& operator=(suite const&) = delete;
/** Invokes the test using the specified runner.
Data members are set up here instead of the constructor as a
@@ -635,6 +639,31 @@ run(runner& r)
(::boost::beast::unit_test::suite::this_suite()->fail((reason), __FILE__, __LINE__), false))
#endif
/** Ensure an exception is thrown
*/
#define BEAST_THROWS( EXPR, EXCEP ) \
try { \
EXPR; \
BEAST_FAIL(); \
} \
catch(EXCEP const&) { \
BEAST_PASS(); \
} \
catch(...) { \
BEAST_FAIL(); \
}
/** Ensure an exception is not thrown
*/
#define BEAST_NO_THROW( EXPR ) \
try { \
EXPR; \
BEAST_PASS(); \
} \
catch(...) { \
BEAST_FAIL(); \
}
} // unit_test
} // beast
} // boost

View File

@@ -13,6 +13,8 @@
#include <boost/beast/_experimental/unit_test/suite_info.hpp>
#include <boost/beast/_experimental/unit_test/detail/const_container.hpp>
#include <boost/assert.hpp>
#include <boost/type_index.hpp>
#include <boost/functional/hash.hpp>
#include <typeindex>
#include <set>
#include <unordered_set>
@@ -28,7 +30,9 @@ class suite_list
private:
#ifndef NDEBUG
std::unordered_set<std::string> names_;
std::unordered_set<std::type_index> classes_;
using type_index = boost::typeindex::type_index;
std::unordered_set<type_index, boost::hash<type_index>> classes_;
#endif
public:
@@ -65,7 +69,7 @@ suite_list::insert(
{
auto const result(classes_.insert(
std::type_index(typeid(Suite))));
boost::typeindex::type_id<Suite>()));
BOOST_ASSERT(result.second); // Duplicate type
}
#endif

View File

@@ -1,128 +0,0 @@
//
// Copyright (c) 2016-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_UNIT_TEST_THREAD_HPP
#define BOOST_BEAST_UNIT_TEST_THREAD_HPP
#include <boost/beast/_experimental/unit_test/suite.hpp>
#include <functional>
#include <thread>
#include <utility>
namespace boost {
namespace beast {
namespace unit_test {
/** Replacement for std::thread that handles exceptions in unit tests. */
class thread
{
private:
suite* s_ = nullptr;
std::thread t_;
public:
using id = std::thread::id;
using native_handle_type = std::thread::native_handle_type;
thread() = default;
thread(thread const&) = delete;
thread& operator=(thread const&) = delete;
thread(thread&& other)
: s_(other.s_)
, t_(std::move(other.t_))
{
}
thread& operator=(thread&& other)
{
s_ = other.s_;
t_ = std::move(other.t_);
return *this;
}
template<class F, class... Args>
explicit
thread(suite& s, F&& f, Args&&... args)
: s_(&s)
{
std::function<void(void)> b =
std::bind(std::forward<F>(f),
std::forward<Args>(args)...);
t_ = std::thread(&thread::run, this,
std::move(b));
}
bool
joinable() const
{
return t_.joinable();
}
std::thread::id
get_id() const
{
return t_.get_id();
}
static
unsigned
hardware_concurrency() noexcept
{
return std::thread::hardware_concurrency();
}
void
join()
{
t_.join();
s_->propagate_abort();
}
void
detach()
{
t_.detach();
}
void
swap(thread& other)
{
std::swap(s_, other.s_);
std::swap(t_, other.t_);
}
private:
void
run(std::function <void(void)> f)
{
try
{
f();
}
catch(suite::abort_exception const&)
{
}
catch(std::exception const& e)
{
s_->fail("unhandled exception: " +
std::string(e.what()));
}
catch(...)
{
s_->fail("unhandled exception");
}
}
};
} // unit_test
} // beast
} // boost
#endif