Skip to content

Commit

Permalink
serializer constructs source
Browse files Browse the repository at this point in the history
  • Loading branch information
ashtum committed Mar 21, 2024
1 parent 39a71bf commit bfab1cb
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 29 deletions.
18 changes: 6 additions & 12 deletions include/boost/http_proto/impl/serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@

#include <boost/http_proto/detail/except.hpp>
#include <boost/buffers/range.hpp>
#include <iterator>
#include <new>
#include <utility>

namespace boost {
namespace http_proto {
Expand Down Expand Up @@ -91,21 +88,18 @@ start(

template<
class Source,
class... Args,
class>
auto
Source&
serializer::
start(
message_view_base const& m,
Source&& src0) ->
typename std::decay<
Source>::type&
Args&&... args)
{
start_init(m);
auto& src = ws_.push(
std::forward<
Source>(src0));
start_source(
m, std::addressof(src));
auto& src = construct_source<Source>(
std::forward<Args>(args)...);
start_source(m, std::addressof(src));
return src;
}

Expand Down
50 changes: 45 additions & 5 deletions include/boost/http_proto/serializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,17 @@ class BOOST_SYMBOL_VISIBLE
undefined behavior.
*/
template<
class Source
class Source,
class... Args
#ifndef BOOST_HTTP_PROTO_DOCS
,class = typename std::enable_if<
is_source<Source>::value>::type
#endif
>
auto
Source&
start(
message_view_base const& m,
Source&& body) ->
typename std::decay<
Source>::type&;
Args&&... args);

//--------------------------------------------

Expand Down Expand Up @@ -192,6 +191,47 @@ class BOOST_SYMBOL_VISIBLE
make_array(std::size_t n) ->
detail::array_of_const_buffers;

template<
class Source,
class... Args,
typename std::enable_if<
std::is_constructible<
Source,
Args...>::value>::type* = nullptr>
BOOST_HTTP_PROTO_DECL
Source&
construct_source(Args&&... args)
{
// TODO: add ws_.emplace<T>(...) interface
// to prevent the unnecessary move
return ws_.push(
Source(std::forward<Args>(args)...));
}

template<
class Source,
class... Args,
typename std::enable_if<
std::is_constructible<
Source,
buffered_base::allocator&,
Args...>::value>::type* = nullptr>
BOOST_HTTP_PROTO_DECL
Source&
construct_source(Args&&... args)
{
// TODO: take into account the occupied space
// by the subsequent call to ws_.push(...)
buffered_base::allocator a(
ws_.data(), ws_.size()/2, false);
// TODO: add ws_.emplace<T>(...) interface
// to prevent the unnecessary move
auto& src = ws_.push(
Source(a, std::forward<Args>(args)...));
ws_.reserve_front(a.size_used());
return src;
}

BOOST_HTTP_PROTO_DECL void start_init(message_view_base const&);
BOOST_HTTP_PROTO_DECL void start_empty(message_view_base const&);
BOOST_HTTP_PROTO_DECL void start_buffers(message_view_base const&);
Expand Down
1 change: 0 additions & 1 deletion include/boost/http_proto/source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ namespace http_proto {
*/
struct BOOST_HTTP_PROTO_DECL
source
: buffered_base
{
/** The results of producing data.
*/
Expand Down
5 changes: 0 additions & 5 deletions src/serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,11 +435,6 @@ start_source(
2); // tmp
//if(! cod_)
{
buffered_base::allocator a(
ws_.data(), ws_.size()/2, false);
src->init(a);
ws_.reserve_front(a.size_used());

tmp0_ = { ws_.data(), ws_.size() };
if(tmp0_.capacity() <
18 + // chunk size
Expand Down
12 changes: 6 additions & 6 deletions test/unit/serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct serializer_test

results
on_read(
buffers::mutable_buffer b)
buffers::mutable_buffer b) override
{
BOOST_TEST(! is_done_);
results rv;
Expand Down Expand Up @@ -162,10 +162,10 @@ struct serializer_test
sr.start(res);
sr.start(res, buffers::const_buffer{});
sr.start(res, buffers::mutable_buffer{});
sr.start(res, test_source{"12345"});
sr.start<test_source>(res, "12345");
sr.start(res, make_const(buffers::const_buffer{}));
sr.start(res, make_const(buffers::mutable_buffer{}));
sr.start(res, make_const(test_source{"12345"}));
sr.start<test_source>(res, make_const("12345"));

serializer(65536);
#ifdef BOOST_HTTP_PROTO_HAS_ZLIB
Expand Down Expand Up @@ -244,7 +244,7 @@ struct serializer_test
// we limit the buffer size of the serializer, requiring
// it to make multiple calls to source::read
serializer sr(1024);
sr.start(res, std::forward<
sr.start<Source>(res, std::forward<
Source>(src));
std::string s = read(sr);
f(s);
Expand Down Expand Up @@ -377,7 +377,7 @@ struct serializer_test
"Expect: 100-continue\r\n"
"Content-Length: 5\r\n"
"\r\n");
sr.start(req, test_source{"12345"});
sr.start<test_source>(req, "12345");
std::string s;
system::result<
serializer::const_buffers_type> rv;
Expand Down Expand Up @@ -457,7 +457,7 @@ struct serializer_test
"\r\n";
serializer sr;
response res(sv);
sr.start(res, test_source{"12345"});
sr.start<test_source>(res, "12345");
auto s = read(sr);
BOOST_TEST(s ==
"HTTP/1.1 200 OK\r\n"
Expand Down

0 comments on commit bfab1cb

Please sign in to comment.