From 043386aeb35783524a28e0c9b0091bbd0dfc0592 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Wed, 10 May 2017 13:17:11 -0700 Subject: [PATCH] Move prepare_buffers to prepare_buffer.hpp (API Change) fix #338 --- CHANGELOG.md | 1 + include/beast/core.hpp | 2 +- ...prepare_buffers.hpp => prepare_buffer.hpp} | 91 ++++++---- include/beast/core/prepare_buffer.hpp | 34 ++++ include/beast/core/prepare_buffers.hpp | 53 ------ include/beast/websocket/impl/accept.ipp | 2 +- include/beast/websocket/impl/read.ipp | 2 +- include/beast/websocket/impl/stream.ipp | 2 +- include/beast/websocket/impl/write.ipp | 2 +- test/Jamfile | 1 - test/core/CMakeLists.txt | 1 - test/core/prepare_buffer.cpp | 168 ++++++++++++++++++ test/core/prepare_buffers.cpp | 167 ----------------- test/http/design.cpp | 2 +- 14 files changed, 265 insertions(+), 263 deletions(-) rename include/beast/core/detail/{prepare_buffers.hpp => prepare_buffer.hpp} (75%) delete mode 100644 include/beast/core/prepare_buffers.hpp diff --git a/CHANGELOG.md b/CHANGELOG.md index 488e20f3..1522acc4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 -------------------------------------------------------------------------------- diff --git a/include/beast/core.hpp b/include/beast/core.hpp index c7b308dd..e069656d 100644 --- a/include/beast/core.hpp +++ b/include/beast/core.hpp @@ -25,7 +25,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/include/beast/core/detail/prepare_buffers.hpp b/include/beast/core/detail/prepare_buffer.hpp similarity index 75% rename from include/beast/core/detail/prepare_buffers.hpp rename to include/beast/core/detail/prepare_buffer.hpp index 040e087f..90a07b3b 100644 --- a/include/beast/core/detail/prepare_buffers.hpp +++ b/include/beast/core/detail/prepare_buffer.hpp @@ -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 #include #include #include @@ -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(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(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 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 - prepared_buffers(Deduced&& other, + prepare_buffers_helper(Deduced&& other, std::size_t nback, std::size_t nend) : bs_(std::forward(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 prepared_buffers::const_iterator +class prepare_buffers_helper::const_iterator { - friend class prepared_buffers; + friend class prepare_buffers_helper; 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 void -prepared_buffers:: +prepare_buffers_helper:: setup(std::size_t n) { for(end_ = bs_.begin(); end_ != bs_.end(); ++end_) @@ -218,7 +239,7 @@ setup(std::size_t n) } template -prepared_buffers::const_iterator:: +prepare_buffers_helper::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 -prepared_buffers::const_iterator:: +prepare_buffers_helper::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 auto -prepared_buffers::const_iterator:: +prepare_buffers_helper::const_iterator:: operator=(const_iterator&& other) -> const_iterator& { @@ -246,7 +267,7 @@ operator=(const_iterator&& other) -> template auto -prepared_buffers::const_iterator:: +prepare_buffers_helper::const_iterator:: operator=(const_iterator const& other) -> const_iterator& { @@ -258,18 +279,18 @@ operator=(const_iterator const& other) -> } template -prepared_buffers:: -prepared_buffers(prepared_buffers&& other) - : prepared_buffers(std::move(other), +prepare_buffers_helper:: +prepare_buffers_helper(prepare_buffers_helper&& other) + : prepare_buffers_helper(std::move(other), std::distance(other.bs_.begin(), other.back_), std::distance(other.bs_.begin(), other.end_)) { } template -prepared_buffers:: -prepared_buffers(prepared_buffers const& other) - : prepared_buffers(other, +prepare_buffers_helper:: +prepare_buffers_helper(prepare_buffers_helper const& other) + : prepare_buffers_helper(other, std::distance(other.bs_.begin(), other.back_), std::distance(other.bs_.begin(), other.end_)) { @@ -277,9 +298,9 @@ prepared_buffers(prepared_buffers const& other) template auto -prepared_buffers:: -operator=(prepared_buffers&& other) -> - prepared_buffers& +prepare_buffers_helper:: +operator=(prepare_buffers_helper&& other) -> + prepare_buffers_helper& { auto const nback = std::distance( other.bs_.begin(), other.back_); @@ -294,9 +315,9 @@ operator=(prepared_buffers&& other) -> template auto -prepared_buffers:: -operator=(prepared_buffers const& other) -> - prepared_buffers& +prepare_buffers_helper:: +operator=(prepare_buffers_helper const& other) -> + prepare_buffers_helper& { auto const nback = std::distance( other.bs_.begin(), other.back_); @@ -310,8 +331,8 @@ operator=(prepared_buffers const& other) -> } template -prepared_buffers:: -prepared_buffers(std::size_t n, BufferSequence const& bs) +prepare_buffers_helper:: +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 inline auto -prepared_buffers::begin() const -> +prepare_buffers_helper::begin() const -> const_iterator { return const_iterator{*this, false}; @@ -329,7 +350,7 @@ prepared_buffers::begin() const -> template inline auto -prepared_buffers::end() const -> +prepare_buffers_helper::end() const -> const_iterator { return const_iterator{*this, true}; diff --git a/include/beast/core/prepare_buffer.hpp b/include/beast/core/prepare_buffer.hpp index ab0fae64..da442ca6 100644 --- a/include/beast/core/prepare_buffer.hpp +++ b/include/beast/core/prepare_buffer.hpp @@ -9,8 +9,14 @@ #define BEAST_PREPARE_BUFFER_HPP #include +#include #include #include +#include +#include +#include +#include +#include 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 +#if BEAST_DOXYGEN +implementation_defined +#else +inline +detail::prepare_buffers_helper +#endif +prepare_buffers(std::size_t n, BufferSequence const& buffers) +{ + return detail::prepare_buffers_helper(n, buffers); +} + } // beast #endif diff --git a/include/beast/core/prepare_buffers.hpp b/include/beast/core/prepare_buffers.hpp deleted file mode 100644 index 0a960ea8..00000000 --- a/include/beast/core/prepare_buffers.hpp +++ /dev/null @@ -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 -#include -#include -#include -#include -#include -#include -#include -#include - -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 -#if BEAST_DOXYGEN -implementation_defined -#else -inline -detail::prepared_buffers -#endif -prepare_buffers(std::size_t n, BufferSequence const& buffers) -{ - return detail::prepared_buffers(n, buffers); -} - -} // beast - -#endif diff --git a/include/beast/websocket/impl/accept.ipp b/include/beast/websocket/impl/accept.ipp index 6c2c6449..7d632975 100644 --- a/include/beast/websocket/impl/accept.ipp +++ b/include/beast/websocket/impl/accept.ipp @@ -16,7 +16,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/include/beast/websocket/impl/read.ipp b/include/beast/websocket/impl/read.ipp index d9ded16b..598cb397 100644 --- a/include/beast/websocket/impl/read.ipp +++ b/include/beast/websocket/impl/read.ipp @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/include/beast/websocket/impl/stream.ipp b/include/beast/websocket/impl/stream.ipp index e3acceaa..03fd274a 100644 --- a/include/beast/websocket/impl/stream.ipp +++ b/include/beast/websocket/impl/stream.ipp @@ -18,7 +18,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/include/beast/websocket/impl/write.ipp b/include/beast/websocket/impl/write.ipp index f72e5d85..7387a452 100644 --- a/include/beast/websocket/impl/write.ipp +++ b/include/beast/websocket/impl/write.ipp @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/test/Jamfile b/test/Jamfile index 5aa8919d..8feacdb3 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -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 diff --git a/test/core/CMakeLists.txt b/test/core/CMakeLists.txt index ab7f7dc4..525988ea 100644 --- a/test/core/CMakeLists.txt +++ b/test/core/CMakeLists.txt @@ -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 diff --git a/test/core/prepare_buffer.cpp b/test/core/prepare_buffer.cpp index 989ce0c4..daa13994 100644 --- a/test/core/prepare_buffer.cpp +++ b/test/core/prepare_buffer.cpp @@ -7,3 +7,171 @@ // Test that header file is self-contained. #include + +#include +#include +#include +#include + +namespace beast { + +class prepare_buffer_test : public beast::unit_test::suite +{ +public: + template + 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 + 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 + 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 + 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 + 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(b), + buffer_size(b)); + return s; + } + + template + 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 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 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 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(); + testMatrix(); + testNullBuffers(); + testIterator(); + } +}; + +BEAST_DEFINE_TESTSUITE(prepare_buffer,core,beast); + +} // beast diff --git a/test/core/prepare_buffers.cpp b/test/core/prepare_buffers.cpp index b9d481e3..f7db3840 100644 --- a/test/core/prepare_buffers.cpp +++ b/test/core/prepare_buffers.cpp @@ -8,170 +8,3 @@ // Test that header file is self-contained. #include -#include -#include -#include -#include - -namespace beast { - -class prepare_buffers_test : public beast::unit_test::suite -{ -public: - template - 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 - 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 - 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 - 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 - 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(b), - buffer_size(b)); - return s; - } - - template - 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 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 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 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(); - testMatrix(); - testNullBuffers(); - testIterator(); - } -}; - -BEAST_DEFINE_TESTSUITE(prepare_buffers,core,beast); - -} // beast diff --git a/test/http/design.cpp b/test/http/design.cpp index b44acdcc..4407488a 100644 --- a/test/http/design.cpp +++ b/test/http/design.cpp @@ -6,7 +6,7 @@ // #include -#include +#include #include #include #include