forked from boostorg/beast
message::body is a member function (API Change):
fix #778 * The body data member is replaced with accessor member functions. Actions Required: * Call member function message::body instead of accessing the data member at call sites.
This commit is contained in:
1 parent
0d4d239d05
commit
54fe7cacf7
36 files changed
+248
-182
No files matched your search
@@ -19,31 +19,32 @@ namespace beast {
|
||||
namespace detail {
|
||||
|
||||
template<class T>
|
||||
struct empty_base_optimization_decide
|
||||
struct is_empty_base_optimization_derived
|
||||
: std::integral_constant<bool,
|
||||
std::is_empty<T>::value &&
|
||||
! boost::is_final<T>::value>
|
||||
{
|
||||
};
|
||||
|
||||
template<
|
||||
class T,
|
||||
int UniqueID = 0,
|
||||
bool ShouldDeriveFrom =
|
||||
empty_base_optimization_decide<T>::value
|
||||
>
|
||||
template<class T, int UniqueID = 0,
|
||||
bool isDerived =
|
||||
is_empty_base_optimization_derived<T>::value>
|
||||
class empty_base_optimization : private T
|
||||
{
|
||||
public:
|
||||
empty_base_optimization() = default;
|
||||
empty_base_optimization(empty_base_optimization&&) = default;
|
||||
empty_base_optimization(empty_base_optimization const&) = default;
|
||||
empty_base_optimization& operator=(empty_base_optimization&&) = default;
|
||||
empty_base_optimization& operator=(empty_base_optimization const&) = default;
|
||||
|
||||
empty_base_optimization(T const& t)
|
||||
: T (t)
|
||||
{}
|
||||
|
||||
empty_base_optimization(T&& t)
|
||||
: T (std::move (t))
|
||||
{}
|
||||
template<class Arg1, class... ArgN>
|
||||
explicit
|
||||
empty_base_optimization(Arg1&& arg1, ArgN&&... argn)
|
||||
: T(std::forward<Arg1>(arg1),
|
||||
std::forward<ArgN>(argn)...)
|
||||
{
|
||||
}
|
||||
|
||||
T& member() noexcept
|
||||
{
|
||||
@@ -64,29 +65,32 @@ template<
|
||||
>
|
||||
class empty_base_optimization <T, UniqueID, false>
|
||||
{
|
||||
T t_;
|
||||
|
||||
public:
|
||||
empty_base_optimization() = default;
|
||||
empty_base_optimization(empty_base_optimization&&) = default;
|
||||
empty_base_optimization(empty_base_optimization const&) = default;
|
||||
empty_base_optimization& operator=(empty_base_optimization&&) = default;
|
||||
empty_base_optimization& operator=(empty_base_optimization const&) = default;
|
||||
|
||||
empty_base_optimization(T const& t)
|
||||
: m_t (t)
|
||||
{}
|
||||
|
||||
empty_base_optimization(T&& t)
|
||||
: m_t (std::move (t))
|
||||
{}
|
||||
template<class Arg1, class... ArgN>
|
||||
explicit
|
||||
empty_base_optimization(Arg1&& arg1, ArgN&&... argn)
|
||||
: t_(std::forward<Arg1>(arg1),
|
||||
std::forward<ArgN>(argn)...)
|
||||
{
|
||||
}
|
||||
|
||||
T& member() noexcept
|
||||
{
|
||||
return m_t;
|
||||
return t_;
|
||||
}
|
||||
|
||||
T const& member() const noexcept
|
||||
{
|
||||
return m_t;
|
||||
return t_;
|
||||
}
|
||||
|
||||
private:
|
||||
T m_t;
|
||||
};
|
||||
|
||||
} // detail
|
||||
|
||||
@@ -74,7 +74,7 @@ struct basic_dynamic_body
|
||||
explicit
|
||||
reader(message<isRequest,
|
||||
basic_dynamic_body, Fields> const& m)
|
||||
: body_(m.body)
|
||||
: body_(m.body())
|
||||
{
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ struct basic_dynamic_body
|
||||
explicit
|
||||
writer(message<isRequest,
|
||||
basic_dynamic_body, Fields>& msg)
|
||||
: body_(msg.body)
|
||||
: body_(msg.body())
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -291,7 +291,7 @@ template<bool isRequest, class Fields>
|
||||
basic_file_body<File>::
|
||||
reader::
|
||||
reader(message<isRequest, basic_file_body, Fields>& m)
|
||||
: body_(m.body)
|
||||
: body_(m.body())
|
||||
{
|
||||
// The file must already be open
|
||||
BOOST_ASSERT(body_.file_.is_open());
|
||||
@@ -442,7 +442,7 @@ template<bool isRequest, class Fields>
|
||||
basic_file_body<File>::
|
||||
writer::
|
||||
writer(message<isRequest, basic_file_body, Fields>& m)
|
||||
: body_(m.body)
|
||||
: body_(m.body())
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -111,7 +111,7 @@ struct buffer_body
|
||||
explicit
|
||||
reader(message<isRequest,
|
||||
buffer_body, Fields> const& msg)
|
||||
: body_(msg.body)
|
||||
: body_(msg.body())
|
||||
{
|
||||
}
|
||||
|
||||
@@ -169,7 +169,7 @@ struct buffer_body
|
||||
template<bool isRequest, class Fields>
|
||||
explicit
|
||||
writer(message<isRequest, buffer_body, Fields>& m)
|
||||
: body_(m.body)
|
||||
: body_(m.body())
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -125,7 +125,7 @@ struct basic_file_body<file_win32>
|
||||
template<bool isRequest, class Fields>
|
||||
reader(message<isRequest,
|
||||
basic_file_body<file_win32>, Fields>& m)
|
||||
: body_(m.body)
|
||||
: body_(m.body())
|
||||
{
|
||||
}
|
||||
|
||||
@@ -168,7 +168,7 @@ struct basic_file_body<file_win32>
|
||||
template<bool isRequest, class Fields>
|
||||
explicit
|
||||
writer(message<isRequest, basic_file_body, Fields>& m)
|
||||
: body_(m.body)
|
||||
: body_(m.body())
|
||||
{
|
||||
}
|
||||
|
||||
@@ -437,7 +437,7 @@ operator()()
|
||||
ov.OffsetHigh = highPart(r.pos_);
|
||||
auto const bSuccess = ::TransmitFile(
|
||||
sock_.native_handle(),
|
||||
sr_.get().body.file_.native_handle(),
|
||||
sr_.get().body().file_.native_handle(),
|
||||
nNumberOfBytesToWrite,
|
||||
0,
|
||||
overlapped.get(),
|
||||
|
||||
@@ -209,7 +209,9 @@ template<class... BodyArgs>
|
||||
message<isRequest, Body, Fields>::
|
||||
message(header_type&& h, BodyArgs&&... body_args)
|
||||
: header_type(std::move(h))
|
||||
, body(std::forward<BodyArgs>(body_args)...)
|
||||
, beast::detail::empty_base_optimization<
|
||||
typename Body::value_type>(
|
||||
std::forward<BodyArgs>(body_args)...)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -218,7 +220,9 @@ template<class... BodyArgs>
|
||||
message<isRequest, Body, Fields>::
|
||||
message(header_type const& h, BodyArgs&&... body_args)
|
||||
: header_type(h)
|
||||
, body(std::forward<BodyArgs>(body_args)...)
|
||||
, beast::detail::empty_base_optimization<
|
||||
typename Body::value_type>(
|
||||
std::forward<BodyArgs>(body_args)...)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -236,7 +240,9 @@ message<isRequest, Body, Fields>::
|
||||
message(verb method, string_view target,
|
||||
Version version, BodyArg&& body_arg)
|
||||
: header_type(method, target, version)
|
||||
, body(std::forward<BodyArg>(body_arg))
|
||||
, beast::detail::empty_base_optimization<
|
||||
typename Body::value_type>(
|
||||
std::forward<BodyArg>(body_arg))
|
||||
{
|
||||
}
|
||||
|
||||
@@ -249,7 +255,9 @@ message(
|
||||
FieldsArg&& fields_arg)
|
||||
: header_type(method, target, version,
|
||||
std::forward<FieldsArg>(fields_arg))
|
||||
, body(std::forward<BodyArg>(body_arg))
|
||||
, beast::detail::empty_base_optimization<
|
||||
typename Body::value_type>(
|
||||
std::forward<BodyArg>(body_arg))
|
||||
{
|
||||
}
|
||||
|
||||
@@ -267,7 +275,9 @@ message<isRequest, Body, Fields>::
|
||||
message(status result, Version version,
|
||||
BodyArg&& body_arg)
|
||||
: header_type(result, version)
|
||||
, body(std::forward<BodyArg>(body_arg))
|
||||
, beast::detail::empty_base_optimization<
|
||||
typename Body::value_type>(
|
||||
std::forward<BodyArg>(body_arg))
|
||||
{
|
||||
}
|
||||
|
||||
@@ -278,7 +288,9 @@ message(status result, Version version,
|
||||
BodyArg&& body_arg, FieldsArg&& fields_arg)
|
||||
: header_type(result, version,
|
||||
std::forward<FieldsArg>(fields_arg))
|
||||
, body(std::forward<BodyArg>(body_arg))
|
||||
, beast::detail::empty_base_optimization<
|
||||
typename Body::value_type>(
|
||||
std::forward<BodyArg>(body_arg))
|
||||
{
|
||||
}
|
||||
|
||||
@@ -409,7 +421,7 @@ swap(
|
||||
swap(
|
||||
static_cast<header<isRequest, Fields>&>(m1),
|
||||
static_cast<header<isRequest, Fields>&>(m2));
|
||||
swap(m1.body, m2.body);
|
||||
swap(m1.body(), m2.body());
|
||||
}
|
||||
|
||||
} // http
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include <boost/beast/http/status.hpp>
|
||||
#include <boost/beast/http/type_traits.hpp>
|
||||
#include <boost/beast/core/string.hpp>
|
||||
#include <boost/beast/core/detail/empty_base_optimization.hpp>
|
||||
#include <boost/beast/core/detail/integer_sequence.hpp>
|
||||
#include <boost/optional.hpp>
|
||||
#include <boost/throw_exception.hpp>
|
||||
@@ -391,6 +392,14 @@ using request_header = header<true, Fields>;
|
||||
template<class Fields = fields>
|
||||
using response_header = header<false, Fields>;
|
||||
|
||||
#if defined(BOOST_MSVC)
|
||||
// Workaround for MSVC bug with private base classes
|
||||
namespace detail {
|
||||
template<class T>
|
||||
using value_type_t = typename T::value_type;
|
||||
} // detail
|
||||
#endif
|
||||
|
||||
/** A container for a complete HTTP message.
|
||||
|
||||
This container is derived from the `Fields` template type.
|
||||
@@ -421,7 +430,12 @@ using response_header = header<false, Fields>;
|
||||
field value pairs.
|
||||
*/
|
||||
template<bool isRequest, class Body, class Fields = fields>
|
||||
struct message : header<isRequest, Fields>
|
||||
struct message
|
||||
: header<isRequest, Fields>
|
||||
#if ! BOOST_BEAST_DOXYGEN
|
||||
, beast::detail::empty_base_optimization<
|
||||
typename Body::value_type>
|
||||
#endif
|
||||
{
|
||||
/// The base class used to hold the header portion of the message.
|
||||
using header_type = header<isRequest, Fields>;
|
||||
@@ -432,9 +446,6 @@ struct message : header<isRequest, Fields>
|
||||
*/
|
||||
using body_type = Body;
|
||||
|
||||
/// A value representing the body.
|
||||
typename Body::value_type body;
|
||||
|
||||
/// Constructor
|
||||
message() = default;
|
||||
|
||||
@@ -736,7 +747,7 @@ struct message : header<isRequest, Fields>
|
||||
@code
|
||||
request<string_body> req{verb::post, "/"};
|
||||
req.set(field::user_agent, "Beast");
|
||||
req.body = "Hello, world!";
|
||||
req.body() = "Hello, world!";
|
||||
req.prepare_payload();
|
||||
@endcode
|
||||
*/
|
||||
@@ -746,6 +757,28 @@ struct message : header<isRequest, Fields>
|
||||
prepare_payload(typename header_type::is_request{});
|
||||
}
|
||||
|
||||
/// Returns the body
|
||||
#if BOOST_BEAST_DOXYGEN || ! defined(BOOST_MSVC)
|
||||
typename body_type::value_type&
|
||||
#else
|
||||
detail::value_type_t<Body>&
|
||||
#endif
|
||||
body() noexcept
|
||||
{
|
||||
return this->member();
|
||||
}
|
||||
|
||||
/// Returns the body
|
||||
#if BOOST_BEAST_DOXYGEN || ! defined(BOOST_MSVC)
|
||||
typename body_type::value_type const&
|
||||
#else
|
||||
detail::value_type_t<Body> const&
|
||||
#endif
|
||||
body() const noexcept
|
||||
{
|
||||
return this->member();
|
||||
}
|
||||
|
||||
private:
|
||||
static_assert(is_body<Body>::value,
|
||||
"Body requirements not met");
|
||||
@@ -757,8 +790,10 @@ private:
|
||||
std::piecewise_construct_t,
|
||||
std::tuple<BodyArgs...>& body_args,
|
||||
beast::detail::index_sequence<IBodyArgs...>)
|
||||
: body(std::forward<BodyArgs>(
|
||||
std::get<IBodyArgs>(body_args))...)
|
||||
: beast::detail::empty_base_optimization<
|
||||
typename Body::value_type>(
|
||||
std::forward<BodyArgs>(
|
||||
std::get<IBodyArgs>(body_args))...)
|
||||
{
|
||||
boost::ignore_unused(body_args);
|
||||
}
|
||||
@@ -776,8 +811,10 @@ private:
|
||||
beast::detail::index_sequence<IFieldsArgs...>)
|
||||
: header_type(std::forward<FieldsArgs>(
|
||||
std::get<IFieldsArgs>(fields_args))...)
|
||||
, body(std::forward<BodyArgs>(
|
||||
std::get<IBodyArgs>(body_args))...)
|
||||
, beast::detail::empty_base_optimization<
|
||||
typename Body::value_type>(
|
||||
std::forward<BodyArgs>(
|
||||
std::get<IBodyArgs>(body_args))...)
|
||||
{
|
||||
boost::ignore_unused(body_args);
|
||||
boost::ignore_unused(fields_args);
|
||||
@@ -786,7 +823,7 @@ private:
|
||||
boost::optional<std::uint64_t>
|
||||
payload_size(std::true_type) const
|
||||
{
|
||||
return Body::size(body);
|
||||
return Body::size(this->body());
|
||||
}
|
||||
|
||||
boost::optional<std::uint64_t>
|
||||
|
||||
@@ -78,7 +78,7 @@ public:
|
||||
explicit
|
||||
reader(message<isRequest,
|
||||
span_body, Fields> const& msg)
|
||||
: body_(msg.body)
|
||||
: body_(msg.body())
|
||||
{
|
||||
}
|
||||
|
||||
@@ -117,7 +117,7 @@ public:
|
||||
explicit
|
||||
writer(message<isRequest,
|
||||
span_body, Fields>& m)
|
||||
: body_(m.body)
|
||||
: body_(m.body())
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -86,7 +86,7 @@ public:
|
||||
explicit
|
||||
reader(message<isRequest,
|
||||
basic_string_body, Fields> const& msg)
|
||||
: body_(msg.body)
|
||||
: body_(msg.body())
|
||||
{
|
||||
}
|
||||
|
||||
@@ -122,7 +122,7 @@ public:
|
||||
explicit
|
||||
writer(message<isRequest,
|
||||
basic_string_body, Fields>& m)
|
||||
: body_(m.body)
|
||||
: body_(m.body())
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -81,7 +81,7 @@ public:
|
||||
explicit
|
||||
reader(message<isRequest,
|
||||
vector_body, Fields> const& msg)
|
||||
: body_(msg.body)
|
||||
: body_(msg.body())
|
||||
{
|
||||
}
|
||||
|
||||
@@ -117,7 +117,7 @@ public:
|
||||
explicit
|
||||
writer(message<isRequest,
|
||||
vector_body, Fields>& m)
|
||||
: body_(m.body)
|
||||
: body_(m.body())
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -577,7 +577,7 @@ build_response(http::request<Body,
|
||||
response_type res;
|
||||
res.version = req.version;
|
||||
res.result(http::status::bad_request);
|
||||
res.body = text;
|
||||
res.body() = text;
|
||||
res.prepare_payload();
|
||||
decorate(res);
|
||||
return res;
|
||||
|
||||
Reference in new issue
Block a user