From 9ab9f0918910cba8f3cbbcb02fb1f96c63ff7113 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sat, 3 Jun 2017 07:25:22 -0700 Subject: [PATCH] Make buffer_prefix_view public fix #408 --- CHANGELOG.md | 2 + doc/2_core.qbk | 7 + doc/quickref.xml | 5 +- include/beast/core/buffer_prefix.hpp | 105 +++++++++++-- .../buffer_prefix.ipp} | 143 ++++-------------- 5 files changed, 137 insertions(+), 125 deletions(-) rename include/beast/core/{detail/buffer_prefix.hpp => impl/buffer_prefix.ipp} (57%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2bee5ba9..e4d74e56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ Version 48 +* Make buffer_prefix_view public + API Changes: * Tidy up chunk decorator diff --git a/doc/2_core.qbk b/doc/2_core.qbk index 4b18e7df..1c82e07f 100644 --- a/doc/2_core.qbk +++ b/doc/2_core.qbk @@ -326,6 +326,13 @@ caller; ownership is not transferred. This function returns a new buffer or buffer sequence which represents a prefix of the original buffers. ]] +[[ + [link beast.ref.buffer_prefix_view `buffer_prefix_view`] +][ + This class represents the buffer sequence formed from a prefix of + an existing buffer sequence. This is the type of buffer returned by + [link beast.ref.buffer_prefix `buffer_prefix`]. +]] [[ [link beast.ref.consuming_buffers `consuming_buffers`] ][ diff --git a/doc/quickref.xml b/doc/quickref.xml index 207e8074..99285cb1 100644 --- a/doc/quickref.xml +++ b/doc/quickref.xml @@ -166,10 +166,11 @@ async_return_type basic_flat_buffer basic_multi_buffer + buffer_cat_view + buffer_prefix_view + buffered_read_stream buffers_adapter consuming_buffers - buffer_cat_view - buffered_read_stream error_category error_code error_condition diff --git a/include/beast/core/buffer_prefix.hpp b/include/beast/core/buffer_prefix.hpp index dfad8ace..a5417b62 100644 --- a/include/beast/core/buffer_prefix.hpp +++ b/include/beast/core/buffer_prefix.hpp @@ -10,14 +10,97 @@ #include #include -#include #include #include #include namespace beast { -/** Returns a prefix of a constant buffer sequence. +/** A buffer sequence adapter that shortens the sequence size. + + The class adapts a buffer sequence to efficiently represent + a shorter subset of the original list of buffers starting + with the first byte of the original sequence. + + @tparam BufferSequence The buffer sequence to adapt. +*/ +template +class buffer_prefix_view +{ + using iter_type = + typename BufferSequence::const_iterator; + + BufferSequence bs_; + iter_type back_; + iter_type end_; + std::size_t size_; + + template + buffer_prefix_view(Deduced&& other, + std::size_t nback, std::size_t nend) + : bs_(std::forward(other).bs_) + , back_(std::next(bs_.begin(), nback)) + , end_(std::next(bs_.begin(), nend)) + , size_(other.size_) + { + } + + void + setup(std::size_t n); + +public: + /// The type for each element in the list of buffers. + using value_type = typename std::conditional< + std::is_convertible::value_type, + boost::asio::mutable_buffer>::value, + boost::asio::mutable_buffer, + boost::asio::const_buffer>::type; + +#if BEAST_DOXYGEN + /// A bidirectional iterator type that may be used to read elements. + using const_iterator = implementation_defined; + +#else + class const_iterator; + +#endif + + /// Move constructor. + buffer_prefix_view(buffer_prefix_view&&); + + /// Copy constructor. + buffer_prefix_view(buffer_prefix_view const&); + + /// Move assignment. + buffer_prefix_view& operator=(buffer_prefix_view&&); + + /// Copy assignment. + buffer_prefix_view& operator=(buffer_prefix_view const&); + + /** Construct a shortened buffer 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. + */ + buffer_prefix_view(std::size_t n, BufferSequence const& buffers); + + /// Get a bidirectional iterator to the first element. + const_iterator + begin() const; + + /// Get a bidirectional iterator to one past the last element. + const_iterator + end() const; +}; + +/** Returns a prefix of a constant buffer. The returned buffer points to the same memory as the passed buffer, but with a size that is equal to or less @@ -25,8 +108,8 @@ namespace beast { @param n The size of the returned buffer. - @param buffer The buffer to shorten. Ownership of the - underlying memory is not transferred. + @param buffer The buffer to shorten. The underlying + memory is not modified. @return A new buffer that points to the first `n` bytes of the original buffer. @@ -42,7 +125,7 @@ buffer_prefix(std::size_t n, (std::min)(n, buffer_size(buffer)) }; } -/** Returns a prefix of a mutable buffer sequence. +/** Returns a prefix of a mutable buffer. The returned buffer points to the same memory as the passed buffer, but with a size that is equal to or less @@ -50,8 +133,8 @@ buffer_prefix(std::size_t n, @param n The size of the returned buffer. - @param buffer The buffer to shorten. Ownership of the - underlying memory is not transferred. + @param buffer The buffer to shorten. The underlying + memory is not modified. @return A new buffer that points to the first `n` bytes of the original buffer. @@ -85,13 +168,13 @@ buffer_prefix(std::size_t n, */ template #if BEAST_DOXYGEN -implementation_defined +buffer_prefix_view #else inline typename std::enable_if< ! std::is_same::value && ! std::is_same::value, - detail::buffer_prefix_helper>::type + buffer_prefix_view>::type #endif buffer_prefix(std::size_t n, BufferSequence const& buffers) { @@ -99,9 +182,11 @@ buffer_prefix(std::size_t n, BufferSequence const& buffers) is_const_buffer_sequence::value || is_mutable_buffer_sequence::value, "BufferSequence requirements not met"); - return detail::buffer_prefix_helper(n, buffers); + return buffer_prefix_view(n, buffers); } } // beast +#include + #endif diff --git a/include/beast/core/detail/buffer_prefix.hpp b/include/beast/core/impl/buffer_prefix.ipp similarity index 57% rename from include/beast/core/detail/buffer_prefix.hpp rename to include/beast/core/impl/buffer_prefix.ipp index 9ce74b20..ff5afda8 100644 --- a/include/beast/core/detail/buffer_prefix.hpp +++ b/include/beast/core/impl/buffer_prefix.ipp @@ -5,10 +5,9 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // -#ifndef BEAST_DETAIL_PREPARE_BUFFER_HPP -#define BEAST_DETAIL_PREPARE_BUFFER_HPP +#ifndef BEAST_IMPL_BUFFER_PREFIX_IPP +#define BEAST_IMPL_BUFFER_PREFIX_IPP -#include #include #include #include @@ -17,6 +16,7 @@ #include namespace beast { + namespace detail { inline @@ -41,99 +41,17 @@ buffer_prefix(std::size_t n, (std::min)(n, buffer_size(buffer)) }; } -/** A buffer sequence adapter that shortens the sequence size. - - The class adapts a buffer sequence to efficiently represent - a shorter subset of the original list of buffers starting - with the first byte of the original sequence. - - @tparam BufferSequence The buffer sequence to adapt. -*/ -template -class buffer_prefix_helper -{ - using iter_type = - typename BufferSequence::const_iterator; - - BufferSequence bs_; - iter_type back_; - iter_type end_; - std::size_t size_; - - template - buffer_prefix_helper(Deduced&& other, - std::size_t nback, std::size_t nend) - : bs_(std::forward(other).bs_) - , back_(std::next(bs_.begin(), nback)) - , end_(std::next(bs_.begin(), nend)) - , size_(other.size_) - { - } - - void - setup(std::size_t n); - -public: - /// The type for each element in the list of buffers. - using value_type = typename std::conditional< - std::is_convertible::value_type, - boost::asio::mutable_buffer>::value, - boost::asio::mutable_buffer, - boost::asio::const_buffer>::type; - -#if BEAST_DOXYGEN - /// A bidirectional iterator type that may be used to read elements. - using const_iterator = implementation_defined; - -#else - class const_iterator; - -#endif - - /// Move constructor. - buffer_prefix_helper(buffer_prefix_helper&&); - - /// Copy constructor. - buffer_prefix_helper(buffer_prefix_helper const&); - - /// Move assignment. - buffer_prefix_helper& operator=(buffer_prefix_helper&&); - - /// Copy assignment. - buffer_prefix_helper& operator=(buffer_prefix_helper const&); - - /** Construct a shortened buffer 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. - */ - buffer_prefix_helper(std::size_t n, BufferSequence const& buffers); - - /// Get a bidirectional iterator to the first element. - const_iterator - begin() const; - - /// Get a bidirectional iterator to one past the last element. - const_iterator - end() const; -}; +} // detail template -class buffer_prefix_helper::const_iterator +class buffer_prefix_view::const_iterator { - friend class buffer_prefix_helper; + friend class buffer_prefix_view; using iter_type = typename BufferSequence::const_iterator; - buffer_prefix_helper const* b_ = nullptr; + buffer_prefix_view const* b_ = nullptr; typename BufferSequence::const_iterator it_; public: @@ -171,7 +89,7 @@ public: operator*() const { if(it_ == b_->back_) - return buffer_prefix(b_->size_, *it_); + return detail::buffer_prefix(b_->size_, *it_); return *it_; } @@ -209,7 +127,7 @@ public: } private: - const_iterator(buffer_prefix_helper const& b, + const_iterator(buffer_prefix_view const& b, bool at_end) : b_(&b) , it_(at_end ? b.end_ : b.bs_.begin()) @@ -219,7 +137,7 @@ private: template void -buffer_prefix_helper:: +buffer_prefix_view:: setup(std::size_t n) { for(end_ = bs_.begin(); end_ != bs_.end(); ++end_) @@ -239,7 +157,7 @@ setup(std::size_t n) } template -buffer_prefix_helper::const_iterator:: +buffer_prefix_view::const_iterator:: const_iterator(const_iterator&& other) : b_(other.b_) , it_(std::move(other.it_)) @@ -247,7 +165,7 @@ const_iterator(const_iterator&& other) } template -buffer_prefix_helper::const_iterator:: +buffer_prefix_view::const_iterator:: const_iterator(const_iterator const& other) : b_(other.b_) , it_(other.it_) @@ -256,7 +174,7 @@ const_iterator(const_iterator const& other) template auto -buffer_prefix_helper::const_iterator:: +buffer_prefix_view::const_iterator:: operator=(const_iterator&& other) -> const_iterator& { @@ -267,7 +185,7 @@ operator=(const_iterator&& other) -> template auto -buffer_prefix_helper::const_iterator:: +buffer_prefix_view::const_iterator:: operator=(const_iterator const& other) -> const_iterator& { @@ -279,18 +197,18 @@ operator=(const_iterator const& other) -> } template -buffer_prefix_helper:: -buffer_prefix_helper(buffer_prefix_helper&& other) - : buffer_prefix_helper(std::move(other), +buffer_prefix_view:: +buffer_prefix_view(buffer_prefix_view&& other) + : buffer_prefix_view(std::move(other), std::distance(other.bs_.begin(), other.back_), std::distance(other.bs_.begin(), other.end_)) { } template -buffer_prefix_helper:: -buffer_prefix_helper(buffer_prefix_helper const& other) - : buffer_prefix_helper(other, +buffer_prefix_view:: +buffer_prefix_view(buffer_prefix_view const& other) + : buffer_prefix_view(other, std::distance(other.bs_.begin(), other.back_), std::distance(other.bs_.begin(), other.end_)) { @@ -298,9 +216,9 @@ buffer_prefix_helper(buffer_prefix_helper const& other) template auto -buffer_prefix_helper:: -operator=(buffer_prefix_helper&& other) -> - buffer_prefix_helper& +buffer_prefix_view:: +operator=(buffer_prefix_view&& other) -> + buffer_prefix_view& { auto const nback = std::distance( other.bs_.begin(), other.back_); @@ -315,9 +233,9 @@ operator=(buffer_prefix_helper&& other) -> template auto -buffer_prefix_helper:: -operator=(buffer_prefix_helper const& other) -> - buffer_prefix_helper& +buffer_prefix_view:: +operator=(buffer_prefix_view const& other) -> + buffer_prefix_view& { auto const nback = std::distance( other.bs_.begin(), other.back_); @@ -331,8 +249,8 @@ operator=(buffer_prefix_helper const& other) -> } template -buffer_prefix_helper:: -buffer_prefix_helper(std::size_t n, BufferSequence const& bs) +buffer_prefix_view:: +buffer_prefix_view(std::size_t n, BufferSequence const& bs) : bs_(bs) { setup(n); @@ -341,7 +259,7 @@ buffer_prefix_helper(std::size_t n, BufferSequence const& bs) template inline auto -buffer_prefix_helper::begin() const -> +buffer_prefix_view::begin() const -> const_iterator { return const_iterator{*this, false}; @@ -350,13 +268,12 @@ buffer_prefix_helper::begin() const -> template inline auto -buffer_prefix_helper::end() const -> +buffer_prefix_view::end() const -> const_iterator { return const_iterator{*this, true}; } -} // detail } // beast #endif