Move prepare_buffers to prepare_buffer.hpp (API Change)

fix #338
This commit is contained in:
Vinnie Falco
2017-05-10 13:17:11 -07:00
parent 7d8e40537a
commit 043386aeb3
14 changed files with 265 additions and 263 deletions

View File

@@ -6,6 +6,7 @@ API Changes
* Return http::error::end_of_stream on HTTP read eof
* Remove placeholders
* Move prepare_buffers to prepare_buffer.hpp
--------------------------------------------------------------------------------

View File

@@ -25,7 +25,7 @@
#include <beast/core/handler_ptr.hpp>
#include <beast/core/multi_buffer.hpp>
#include <beast/core/ostream.hpp>
#include <beast/core/prepare_buffers.hpp>
#include <beast/core/prepare_buffer.hpp>
#include <beast/core/static_buffer.hpp>
#include <beast/core/static_string.hpp>
#include <beast/core/stream_concepts.hpp>

View File

@@ -5,10 +5,9 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BEAST_DETAIL_PREPARED_BUFFERS_HPP
#define BEAST_DETAIL_PREPARED_BUFFERS_HPP
#ifndef BEAST_DETAIL_PREPARE_BUFFER_HPP
#define BEAST_DETAIL_PREPARE_BUFFER_HPP
#include <beast/core/prepare_buffer.hpp>
#include <boost/asio/buffer.hpp>
#include <algorithm>
#include <cstdint>
@@ -20,6 +19,28 @@
namespace beast {
namespace detail {
inline
boost::asio::const_buffer
prepare_buffer(std::size_t n,
boost::asio::const_buffer buffer)
{
using boost::asio::buffer_cast;
using boost::asio::buffer_size;
return { buffer_cast<void const*>(buffer),
(std::min)(n, buffer_size(buffer)) };
}
inline
boost::asio::mutable_buffer
prepare_buffer(std::size_t n,
boost::asio::mutable_buffer buffer)
{
using boost::asio::buffer_cast;
using boost::asio::buffer_size;
return { buffer_cast<void*>(buffer),
(std::min)(n, buffer_size(buffer)) };
}
/** A buffer sequence adapter that shortens the sequence size.
The class adapts a buffer sequence to efficiently represent
@@ -29,7 +50,7 @@ namespace detail {
@tparam BufferSequence The buffer sequence to adapt.
*/
template<class BufferSequence>
class prepared_buffers
class prepare_buffers_helper
{
using iter_type =
typename BufferSequence::const_iterator;
@@ -40,7 +61,7 @@ class prepared_buffers
std::size_t size_;
template<class Deduced>
prepared_buffers(Deduced&& other,
prepare_buffers_helper(Deduced&& other,
std::size_t nback, std::size_t nend)
: bs_(std::forward<Deduced>(other).bs_)
, back_(std::next(bs_.begin(), nback))
@@ -71,16 +92,16 @@ public:
#endif
/// Move constructor.
prepared_buffers(prepared_buffers&&);
prepare_buffers_helper(prepare_buffers_helper&&);
/// Copy constructor.
prepared_buffers(prepared_buffers const&);
prepare_buffers_helper(prepare_buffers_helper const&);
/// Move assignment.
prepared_buffers& operator=(prepared_buffers&&);
prepare_buffers_helper& operator=(prepare_buffers_helper&&);
/// Copy assignment.
prepared_buffers& operator=(prepared_buffers const&);
prepare_buffers_helper& operator=(prepare_buffers_helper const&);
/** Construct a shortened buffer sequence.
@@ -93,7 +114,7 @@ public:
the sequence will be made, but ownership of the underlying
memory is not transferred.
*/
prepared_buffers(std::size_t n, BufferSequence const& buffers);
prepare_buffers_helper(std::size_t n, BufferSequence const& buffers);
/// Get a bidirectional iterator to the first element.
const_iterator
@@ -105,14 +126,14 @@ public:
};
template<class BufferSequence>
class prepared_buffers<BufferSequence>::const_iterator
class prepare_buffers_helper<BufferSequence>::const_iterator
{
friend class prepared_buffers<BufferSequence>;
friend class prepare_buffers_helper<BufferSequence>;
using iter_type =
typename BufferSequence::const_iterator;
prepared_buffers const* b_ = nullptr;
prepare_buffers_helper const* b_ = nullptr;
typename BufferSequence::const_iterator it_;
public:
@@ -188,7 +209,7 @@ public:
}
private:
const_iterator(prepared_buffers const& b,
const_iterator(prepare_buffers_helper const& b,
bool at_end)
: b_(&b)
, it_(at_end ? b.end_ : b.bs_.begin())
@@ -198,7 +219,7 @@ private:
template<class BufferSequence>
void
prepared_buffers<BufferSequence>::
prepare_buffers_helper<BufferSequence>::
setup(std::size_t n)
{
for(end_ = bs_.begin(); end_ != bs_.end(); ++end_)
@@ -218,7 +239,7 @@ setup(std::size_t n)
}
template<class BufferSequence>
prepared_buffers<BufferSequence>::const_iterator::
prepare_buffers_helper<BufferSequence>::const_iterator::
const_iterator(const_iterator&& other)
: b_(other.b_)
, it_(std::move(other.it_))
@@ -226,7 +247,7 @@ const_iterator(const_iterator&& other)
}
template<class BufferSequence>
prepared_buffers<BufferSequence>::const_iterator::
prepare_buffers_helper<BufferSequence>::const_iterator::
const_iterator(const_iterator const& other)
: b_(other.b_)
, it_(other.it_)
@@ -235,7 +256,7 @@ const_iterator(const_iterator const& other)
template<class BufferSequence>
auto
prepared_buffers<BufferSequence>::const_iterator::
prepare_buffers_helper<BufferSequence>::const_iterator::
operator=(const_iterator&& other) ->
const_iterator&
{
@@ -246,7 +267,7 @@ operator=(const_iterator&& other) ->
template<class BufferSequence>
auto
prepared_buffers<BufferSequence>::const_iterator::
prepare_buffers_helper<BufferSequence>::const_iterator::
operator=(const_iterator const& other) ->
const_iterator&
{
@@ -258,18 +279,18 @@ operator=(const_iterator const& other) ->
}
template<class BufferSequence>
prepared_buffers<BufferSequence>::
prepared_buffers(prepared_buffers&& other)
: prepared_buffers(std::move(other),
prepare_buffers_helper<BufferSequence>::
prepare_buffers_helper(prepare_buffers_helper&& other)
: prepare_buffers_helper(std::move(other),
std::distance<iter_type>(other.bs_.begin(), other.back_),
std::distance<iter_type>(other.bs_.begin(), other.end_))
{
}
template<class BufferSequence>
prepared_buffers<BufferSequence>::
prepared_buffers(prepared_buffers const& other)
: prepared_buffers(other,
prepare_buffers_helper<BufferSequence>::
prepare_buffers_helper(prepare_buffers_helper const& other)
: prepare_buffers_helper(other,
std::distance<iter_type>(other.bs_.begin(), other.back_),
std::distance<iter_type>(other.bs_.begin(), other.end_))
{
@@ -277,9 +298,9 @@ prepared_buffers(prepared_buffers const& other)
template<class BufferSequence>
auto
prepared_buffers<BufferSequence>::
operator=(prepared_buffers&& other) ->
prepared_buffers&
prepare_buffers_helper<BufferSequence>::
operator=(prepare_buffers_helper&& other) ->
prepare_buffers_helper&
{
auto const nback = std::distance<iter_type>(
other.bs_.begin(), other.back_);
@@ -294,9 +315,9 @@ operator=(prepared_buffers&& other) ->
template<class BufferSequence>
auto
prepared_buffers<BufferSequence>::
operator=(prepared_buffers const& other) ->
prepared_buffers&
prepare_buffers_helper<BufferSequence>::
operator=(prepare_buffers_helper const& other) ->
prepare_buffers_helper&
{
auto const nback = std::distance<iter_type>(
other.bs_.begin(), other.back_);
@@ -310,8 +331,8 @@ operator=(prepared_buffers const& other) ->
}
template<class BufferSequence>
prepared_buffers<BufferSequence>::
prepared_buffers(std::size_t n, BufferSequence const& bs)
prepare_buffers_helper<BufferSequence>::
prepare_buffers_helper(std::size_t n, BufferSequence const& bs)
: bs_(bs)
{
setup(n);
@@ -320,7 +341,7 @@ prepared_buffers(std::size_t n, BufferSequence const& bs)
template<class BufferSequence>
inline
auto
prepared_buffers<BufferSequence>::begin() const ->
prepare_buffers_helper<BufferSequence>::begin() const ->
const_iterator
{
return const_iterator{*this, false};
@@ -329,7 +350,7 @@ prepared_buffers<BufferSequence>::begin() const ->
template<class BufferSequence>
inline
auto
prepared_buffers<BufferSequence>::end() const ->
prepare_buffers_helper<BufferSequence>::end() const ->
const_iterator
{
return const_iterator{*this, true};

View File

@@ -9,8 +9,14 @@
#define BEAST_PREPARE_BUFFER_HPP
#include <beast/config.hpp>
#include <beast/core/detail/prepare_buffer.hpp>
#include <boost/asio/buffer.hpp>
#include <algorithm>
#include <cstdint>
#include <iterator>
#include <stdexcept>
#include <type_traits>
#include <utility>
namespace beast {
@@ -64,6 +70,34 @@ prepare_buffer(std::size_t n,
(std::min)(n, buffer_size(buffer)) };
}
/** Return a shortened buffer sequence.
This function returns a new buffer sequence which adapts the
passed buffer sequence and efficiently presents a shorter subset
of the original list of buffers starting with the first byte of
the original sequence.
@param n The maximum number of bytes in the wrapped
sequence. If this is larger than the size of passed,
buffers, the resulting sequence will represent the
entire input sequence.
@param buffers The buffer sequence to adapt. A copy of
the sequence will be made, but ownership of the underlying
memory is not transferred.
*/
template<class BufferSequence>
#if BEAST_DOXYGEN
implementation_defined
#else
inline
detail::prepare_buffers_helper<BufferSequence>
#endif
prepare_buffers(std::size_t n, BufferSequence const& buffers)
{
return detail::prepare_buffers_helper<BufferSequence>(n, buffers);
}
} // beast
#endif

View File

@@ -1,53 +0,0 @@
//
// 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)
//
#ifndef BEAST_PREPARE_BUFFERS_HPP
#define BEAST_PREPARE_BUFFERS_HPP
#include <beast/config.hpp>
#include <beast/core/detail/prepare_buffers.hpp>
#include <boost/asio/buffer.hpp>
#include <algorithm>
#include <cstdint>
#include <iterator>
#include <stdexcept>
#include <type_traits>
#include <utility>
namespace beast {
/** Return a shortened buffer sequence.
This function returns a new buffer sequence which adapts the
passed buffer sequence and efficiently presents a shorter subset
of the original list of buffers starting with the first byte of
the original sequence.
@param n The maximum number of bytes in the wrapped
sequence. If this is larger than the size of passed,
buffers, the resulting sequence will represent the
entire input sequence.
@param buffers The buffer sequence to adapt. A copy of
the sequence will be made, but ownership of the underlying
memory is not transferred.
*/
template<class BufferSequence>
#if BEAST_DOXYGEN
implementation_defined
#else
inline
detail::prepared_buffers<BufferSequence>
#endif
prepare_buffers(std::size_t n, BufferSequence const& buffers)
{
return detail::prepared_buffers<BufferSequence>(n, buffers);
}
} // beast
#endif

View File

@@ -16,7 +16,7 @@
#include <beast/http/write.hpp>
#include <beast/core/handler_helpers.hpp>
#include <beast/core/handler_ptr.hpp>
#include <beast/core/prepare_buffers.hpp>
#include <beast/core/prepare_buffer.hpp>
#include <beast/core/detail/type_traits.hpp>
#include <boost/assert.hpp>
#include <memory>

View File

@@ -12,7 +12,7 @@
#include <beast/core/buffer_concepts.hpp>
#include <beast/core/handler_helpers.hpp>
#include <beast/core/handler_ptr.hpp>
#include <beast/core/prepare_buffers.hpp>
#include <beast/core/prepare_buffer.hpp>
#include <beast/core/static_buffer.hpp>
#include <beast/core/stream_concepts.hpp>
#include <beast/core/detail/clamp.hpp>

View File

@@ -18,7 +18,7 @@
#include <beast/core/buffer_cat.hpp>
#include <beast/core/buffer_concepts.hpp>
#include <beast/core/consuming_buffers.hpp>
#include <beast/core/prepare_buffers.hpp>
#include <beast/core/prepare_buffer.hpp>
#include <beast/core/static_buffer.hpp>
#include <beast/core/stream_concepts.hpp>
#include <beast/core/detail/type_traits.hpp>

View File

@@ -14,7 +14,7 @@
#include <beast/core/consuming_buffers.hpp>
#include <beast/core/handler_helpers.hpp>
#include <beast/core/handler_ptr.hpp>
#include <beast/core/prepare_buffers.hpp>
#include <beast/core/prepare_buffer.hpp>
#include <beast/core/static_buffer.hpp>
#include <beast/core/stream_concepts.hpp>
#include <beast/core/detail/clamp.hpp>

View File

@@ -32,7 +32,6 @@ unit-test core-tests :
core/multi_buffer.cpp
core/ostream.cpp
core/prepare_buffer.cpp
core/prepare_buffers.cpp
core/static_buffer.cpp
core/static_string.cpp
core/stream_concepts.cpp

View File

@@ -25,7 +25,6 @@ add_executable (core-tests
multi_buffer.cpp
ostream.cpp
prepare_buffer.cpp
prepare_buffers.cpp
static_buffer.cpp
static_string.cpp
stream_concepts.cpp

View File

@@ -7,3 +7,171 @@
// Test that header file is self-contained.
#include <beast/core/prepare_buffer.hpp>
#include <beast/core/consuming_buffers.hpp>
#include <beast/unit_test/suite.hpp>
#include <boost/asio/buffer.hpp>
#include <string>
namespace beast {
class prepare_buffer_test : public beast::unit_test::suite
{
public:
template<class ConstBufferSequence>
static
std::size_t
bsize1(ConstBufferSequence const& bs)
{
using boost::asio::buffer_size;
std::size_t n = 0;
for(auto it = bs.begin(); it != bs.end(); ++it)
n += buffer_size(*it);
return n;
}
template<class ConstBufferSequence>
static
std::size_t
bsize2(ConstBufferSequence const& bs)
{
using boost::asio::buffer_size;
std::size_t n = 0;
for(auto it = bs.begin(); it != bs.end(); it++)
n += buffer_size(*it);
return n;
}
template<class ConstBufferSequence>
static
std::size_t
bsize3(ConstBufferSequence const& bs)
{
using boost::asio::buffer_size;
std::size_t n = 0;
for(auto it = bs.end(); it != bs.begin();)
n += buffer_size(*--it);
return n;
}
template<class ConstBufferSequence>
static
std::size_t
bsize4(ConstBufferSequence const& bs)
{
using boost::asio::buffer_size;
std::size_t n = 0;
for(auto it = bs.end(); it != bs.begin();)
{
it--;
n += buffer_size(*it);
}
return n;
}
template<class ConstBufferSequence>
static
std::string
to_string(ConstBufferSequence const& bs)
{
using boost::asio::buffer_cast;
using boost::asio::buffer_size;
std::string s;
s.reserve(buffer_size(bs));
for(auto const& b : bs)
s.append(buffer_cast<char const*>(b),
buffer_size(b));
return s;
}
template<class BufferType>
void testMatrix()
{
using boost::asio::buffer_size;
std::string s = "Hello, world";
BEAST_EXPECT(s.size() == 12);
for(std::size_t x = 1; x < 4; ++x) {
for(std::size_t y = 1; y < 4; ++y) {
std::size_t z = s.size() - (x + y);
{
std::array<BufferType, 3> bs{{
BufferType{&s[0], x},
BufferType{&s[x], y},
BufferType{&s[x+y], z}}};
for(std::size_t i = 0; i <= s.size() + 1; ++i)
{
auto pb = prepare_buffers(i, bs);
BEAST_EXPECT(to_string(pb) == s.substr(0, i));
auto pb2 = pb;
BEAST_EXPECT(to_string(pb2) == to_string(pb));
pb = prepare_buffers(0, bs);
pb2 = pb;
BEAST_EXPECT(buffer_size(pb2) == 0);
pb2 = prepare_buffers(i, bs);
BEAST_EXPECT(to_string(pb2) == s.substr(0, i));
}
}
}}
}
void testNullBuffers()
{
using boost::asio::buffer_copy;
using boost::asio::buffer_size;
using boost::asio::null_buffers;
auto pb0 = prepare_buffers(0, null_buffers{});
BEAST_EXPECT(buffer_size(pb0) == 0);
auto pb1 = prepare_buffers(1, null_buffers{});
BEAST_EXPECT(buffer_size(pb1) == 0);
BEAST_EXPECT(buffer_copy(pb0, pb1) == 0);
using pb_type = decltype(pb0);
consuming_buffers<pb_type> cb(pb0);
BEAST_EXPECT(buffer_size(cb) == 0);
BEAST_EXPECT(buffer_copy(cb, pb1) == 0);
cb.consume(1);
BEAST_EXPECT(buffer_size(cb) == 0);
BEAST_EXPECT(buffer_copy(cb, pb1) == 0);
auto pbc = prepare_buffers(2, cb);
BEAST_EXPECT(buffer_size(pbc) == 0);
BEAST_EXPECT(buffer_copy(pbc, cb) == 0);
}
void testIterator()
{
using boost::asio::buffer_size;
using boost::asio::const_buffer;
char b[3];
std::array<const_buffer, 3> bs{{
const_buffer{&b[0], 1},
const_buffer{&b[1], 1},
const_buffer{&b[2], 1}}};
auto pb = prepare_buffers(2, bs);
BEAST_EXPECT(bsize1(pb) == 2);
BEAST_EXPECT(bsize2(pb) == 2);
BEAST_EXPECT(bsize3(pb) == 2);
BEAST_EXPECT(bsize4(pb) == 2);
std::size_t n = 0;
for(auto it = pb.end(); it != pb.begin(); --it)
{
decltype(pb)::const_iterator it2(std::move(it));
BEAST_EXPECT(buffer_size(*it2) == 1);
it = std::move(it2);
++n;
}
BEAST_EXPECT(n == 2);
}
void run() override
{
testMatrix<boost::asio::const_buffer>();
testMatrix<boost::asio::mutable_buffer>();
testNullBuffers();
testIterator();
}
};
BEAST_DEFINE_TESTSUITE(prepare_buffer,core,beast);
} // beast

View File

@@ -8,170 +8,3 @@
// Test that header file is self-contained.
#include <beast/core/prepare_buffers.hpp>
#include <beast/core/consuming_buffers.hpp>
#include <beast/unit_test/suite.hpp>
#include <boost/asio/buffer.hpp>
#include <string>
namespace beast {
class prepare_buffers_test : public beast::unit_test::suite
{
public:
template<class ConstBufferSequence>
static
std::size_t
bsize1(ConstBufferSequence const& bs)
{
using boost::asio::buffer_size;
std::size_t n = 0;
for(auto it = bs.begin(); it != bs.end(); ++it)
n += buffer_size(*it);
return n;
}
template<class ConstBufferSequence>
static
std::size_t
bsize2(ConstBufferSequence const& bs)
{
using boost::asio::buffer_size;
std::size_t n = 0;
for(auto it = bs.begin(); it != bs.end(); it++)
n += buffer_size(*it);
return n;
}
template<class ConstBufferSequence>
static
std::size_t
bsize3(ConstBufferSequence const& bs)
{
using boost::asio::buffer_size;
std::size_t n = 0;
for(auto it = bs.end(); it != bs.begin();)
n += buffer_size(*--it);
return n;
}
template<class ConstBufferSequence>
static
std::size_t
bsize4(ConstBufferSequence const& bs)
{
using boost::asio::buffer_size;
std::size_t n = 0;
for(auto it = bs.end(); it != bs.begin();)
{
it--;
n += buffer_size(*it);
}
return n;
}
template<class ConstBufferSequence>
static
std::string
to_string(ConstBufferSequence const& bs)
{
using boost::asio::buffer_cast;
using boost::asio::buffer_size;
std::string s;
s.reserve(buffer_size(bs));
for(auto const& b : bs)
s.append(buffer_cast<char const*>(b),
buffer_size(b));
return s;
}
template<class BufferType>
void testMatrix()
{
using boost::asio::buffer_size;
std::string s = "Hello, world";
BEAST_EXPECT(s.size() == 12);
for(std::size_t x = 1; x < 4; ++x) {
for(std::size_t y = 1; y < 4; ++y) {
std::size_t z = s.size() - (x + y);
{
std::array<BufferType, 3> bs{{
BufferType{&s[0], x},
BufferType{&s[x], y},
BufferType{&s[x+y], z}}};
for(std::size_t i = 0; i <= s.size() + 1; ++i)
{
auto pb = prepare_buffers(i, bs);
BEAST_EXPECT(to_string(pb) == s.substr(0, i));
auto pb2 = pb;
BEAST_EXPECT(to_string(pb2) == to_string(pb));
pb = prepare_buffers(0, bs);
pb2 = pb;
BEAST_EXPECT(buffer_size(pb2) == 0);
pb2 = prepare_buffers(i, bs);
BEAST_EXPECT(to_string(pb2) == s.substr(0, i));
}
}
}}
}
void testNullBuffers()
{
using boost::asio::buffer_copy;
using boost::asio::buffer_size;
using boost::asio::null_buffers;
auto pb0 = prepare_buffers(0, null_buffers{});
BEAST_EXPECT(buffer_size(pb0) == 0);
auto pb1 = prepare_buffers(1, null_buffers{});
BEAST_EXPECT(buffer_size(pb1) == 0);
BEAST_EXPECT(buffer_copy(pb0, pb1) == 0);
using pb_type = decltype(pb0);
consuming_buffers<pb_type> cb(pb0);
BEAST_EXPECT(buffer_size(cb) == 0);
BEAST_EXPECT(buffer_copy(cb, pb1) == 0);
cb.consume(1);
BEAST_EXPECT(buffer_size(cb) == 0);
BEAST_EXPECT(buffer_copy(cb, pb1) == 0);
auto pbc = prepare_buffers(2, cb);
BEAST_EXPECT(buffer_size(pbc) == 0);
BEAST_EXPECT(buffer_copy(pbc, cb) == 0);
}
void testIterator()
{
using boost::asio::buffer_size;
using boost::asio::const_buffer;
char b[3];
std::array<const_buffer, 3> bs{{
const_buffer{&b[0], 1},
const_buffer{&b[1], 1},
const_buffer{&b[2], 1}}};
auto pb = prepare_buffers(2, bs);
BEAST_EXPECT(bsize1(pb) == 2);
BEAST_EXPECT(bsize2(pb) == 2);
BEAST_EXPECT(bsize3(pb) == 2);
BEAST_EXPECT(bsize4(pb) == 2);
std::size_t n = 0;
for(auto it = pb.end(); it != pb.begin(); --it)
{
decltype(pb)::const_iterator it2(std::move(it));
BEAST_EXPECT(buffer_size(*it2) == 1);
it = std::move(it2);
++n;
}
BEAST_EXPECT(n == 2);
}
void run() override
{
testMatrix<boost::asio::const_buffer>();
testMatrix<boost::asio::mutable_buffer>();
testNullBuffers();
testIterator();
}
};
BEAST_DEFINE_TESTSUITE(prepare_buffers,core,beast);
} // beast

View File

@@ -6,7 +6,7 @@
//
#include <beast/core/flat_buffer.hpp>
#include <beast/core/prepare_buffers.hpp>
#include <beast/core/prepare_buffer.hpp>
#include <beast/http/chunk_encode.hpp>
#include <beast/http/read.hpp>
#include <beast/http/write.hpp>