read_size replaces read_size_helper:

* detail::read_size_helper is removed

* public function read_size helps determine the optimum
  size for calls to a DynamicBuffer's prepare function.

* special case for Asio types which don't conform to
  the concept but have their own helper function.
This commit is contained in:
Vinnie Falco
2017-07-20 08:12:20 -07:00
parent fe4dfed27b
commit a49c096042
18 changed files with 212 additions and 147 deletions
+1
View File
@@ -34,6 +34,7 @@ unit-test core-tests :
core/handler_ptr.cpp
core/multi_buffer.cpp
core/ostream.cpp
core/read_size.cpp
core/static_buffer.cpp
core/static_string.cpp
core/string_param.cpp
+1
View File
@@ -29,6 +29,7 @@ add_executable (core-tests
handler_ptr.cpp
multi_buffer.cpp
ostream.cpp
read_size.cpp
static_buffer.cpp
static_string.cpp
string_param.cpp
+2 -3
View File
@@ -7,8 +7,8 @@
#include <beast/core/flat_buffer.hpp>
#include <beast/core/multi_buffer.hpp>
#include <beast/core/read_size.hpp>
#include <beast/core/string_view.hpp>
#include <beast/core/detail/read_size_helper.hpp>
#include <beast/unit_test/suite.hpp>
#include <boost/asio/streambuf.hpp>
#include <algorithm>
@@ -111,9 +111,8 @@ public:
{
for(auto remain = size; remain;)
{
using beast::detail::read_size_helper;
auto const n = fill(b.prepare(
read_size_helper(b, remain)));
read_size(b, remain)));
b.commit(n);
remain -= n;
total += n;
+1 -32
View File
@@ -9,8 +9,8 @@
#define BEAST_TEST_BUFFER_TEST_HPP
#include <beast/core/string_view.hpp>
#include <beast/core/read_size.hpp>
#include <beast/core/type_traits.hpp>
#include <beast/core/detail/read_size_helper.hpp>
#include <beast/core/detail/type_traits.hpp>
#include <boost/asio/buffer.hpp>
#include <algorithm>
@@ -112,37 +112,6 @@ size_rev_post(ConstBufferSequence const& buffers)
return n;
}
namespace has_read_size_helper
{
using beast::detail::read_size_helper;
template<class T, class = void>
struct trait : std::false_type {};
template<class T>
struct trait<T, decltype(
std::declval<std::size_t&>() =
read_size_helper(std::declval<T>(), 0),
(void)0)> : std::true_type
{
};
}
//------------------------------------------------------------------------------
// Make sure read_size_helper works
template<class DynamicBuffer>
inline
void
check_read_size_helper()
{
static_assert(is_dynamic_buffer<DynamicBuffer>::value,
"DynamicBuffer requirements not met ");
static_assert(has_read_size_helper::trait<DynamicBuffer>::value,
"Missing read_size_helper for dynamic buffer");
}
} // test
} // beast
+1 -5
View File
@@ -183,15 +183,11 @@ public:
type buffer;
buffers_adapter<
type::mutable_buffers_type> ba{buffer.prepare(512)};
using beast::detail::read_size_helper;
read_size_helper(ba, 1024);
read_size(ba, 1024);
}
void run() override
{
test::check_read_size_helper<buffers_adapter<
boost::asio::mutable_buffers_1>>();
testBuffersAdapter();
testCommit();
testIssue386();
+6 -9
View File
@@ -11,8 +11,8 @@
#include "buffer_test.hpp"
#include <beast/core/ostream.hpp>
#include <beast/core/read_size.hpp>
#include <beast/core/string_view.hpp>
#include <beast/core/detail/read_size_helper.hpp>
#include <beast/test/test_allocator.hpp>
#include <beast/unit_test/suite.hpp>
#include <boost/lexical_cast.hpp>
@@ -261,19 +261,18 @@ public:
BEAST_EXPECT(to_string(b.data()) == "4567890123");
}
// read_size_helper
// read_size
{
using detail::read_size_helper;
flat_buffer b{10};
BEAST_EXPECT(read_size_helper(b, 512) == 10);
BEAST_EXPECT(read_size(b, 512) == 10);
b.prepare(4);
b.commit(4);
BEAST_EXPECT(read_size_helper(b, 512) == 6);
BEAST_EXPECT(read_size(b, 512) == 6);
b.consume(2);
BEAST_EXPECT(read_size_helper(b, 512) == 8);
BEAST_EXPECT(read_size(b, 512) == 8);
b.prepare(8);
b.commit(8);
BEAST_EXPECT(read_size_helper(b, 512) == 0);
BEAST_EXPECT(read_size(b, 512) == 0);
}
// swap
@@ -344,8 +343,6 @@ public:
void
run() override
{
test::check_read_size_helper<flat_buffer>();
testBuffer();
}
};
+5 -8
View File
@@ -562,27 +562,24 @@ public:
}
}
// read_size_helper
// read_size
{
using detail::read_size_helper;
multi_buffer b{10};
BEAST_EXPECT(read_size_helper(b, 512) == 10);
BEAST_EXPECT(read_size(b, 512) == 10);
b.prepare(4);
b.commit(4);
BEAST_EXPECT(read_size_helper(b, 512) == 6);
BEAST_EXPECT(read_size(b, 512) == 6);
b.consume(2);
BEAST_EXPECT(read_size_helper(b, 512) == 8);
BEAST_EXPECT(read_size(b, 512) == 8);
b.prepare(8);
b.commit(8);
BEAST_EXPECT(read_size_helper(b, 512) == 0);
BEAST_EXPECT(read_size(b, 512) == 0);
}
}
void
run() override
{
test::check_read_size_helper<multi_buffer>();
testMatrix1();
testMatrix2();
testIterators();
+44
View File
@@ -0,0 +1,44 @@
//
// Copyright (c) 2013-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)
//
// Test that header file is self-contained.
#include <beast/core/read_size.hpp>
#include <beast/core/drain_buffer.hpp>
#include <beast/core/flat_buffer.hpp>
#include <beast/core/multi_buffer.hpp>
#include <beast/unit_test/suite.hpp>
#include <boost/asio/streambuf.hpp>
namespace beast {
class read_size_test : public beast::unit_test::suite
{
public:
template<class DynamicBuffer>
void
check()
{
DynamicBuffer buffer;
read_size(buffer, 65536);
pass();
}
void
run() override
{
check<drain_buffer>();
check<flat_buffer>();
check<multi_buffer>();
check<boost::asio::streambuf>();
}
};
BEAST_DEFINE_TESTSUITE(read_size,core,beast);
} // beast
+5 -8
View File
@@ -190,19 +190,18 @@ public:
}
}
// read_size_helper
// read_size
{
using detail::read_size_helper;
static_buffer_n<10> b;
BEAST_EXPECT(read_size_helper(b, 512) == 10);
BEAST_EXPECT(read_size(b, 512) == 10);
b.prepare(4);
b.commit(4);
BEAST_EXPECT(read_size_helper(b, 512) == 6);
BEAST_EXPECT(read_size(b, 512) == 6);
b.consume(2);
BEAST_EXPECT(read_size_helper(b, 512) == 8);
BEAST_EXPECT(read_size(b, 512) == 8);
b.prepare(8);
b.commit(8);
BEAST_EXPECT(read_size_helper(b, 512) == 0);
BEAST_EXPECT(read_size(b, 512) == 0);
}
// base
@@ -224,8 +223,6 @@ public:
void run() override
{
test::check_read_size_helper<static_buffer_n<32>>();
testBuffer();
//testStaticBuffer();
}
+1 -1
View File
@@ -8,8 +8,8 @@
#include <examples/doc_http_samples.hpp>
#include <examples/file_body.hpp>
#include <beast/core/read_size.hpp>
#include <beast/core/detail/clamp.hpp>
#include <beast/core/detail/read_size_helper.hpp>
#include <beast/test/pipe_stream.hpp>
#include <beast/test/string_istream.hpp>
#include <beast/test/string_ostream.hpp>