Refactor static_buffer:

* Improve tests and coverage
* Add static_buffer_base::clear
* Use BOOST_BEAST_DECL
This commit is contained in:
Vinnie Falco committed 2018-12-19 20:59:20 -08:00
1 parent 0006ab3b2b
commit 321af29d25
6 files changed
+106 -205

No files matched your search

@@ -78,6 +78,7 @@ public:
{
b_[0] = *other.begin();
b_[1] = *(other.begin() + 1);
return *this;
}
buffers_pair(value_type b0, value_type b1)
@@ -76,7 +76,18 @@ public:
reset(p, n);
}
/// Change the number of readable and writable bytes to zero.
/** Clear the readable and writable bytes to zero.
This function causes the readable and writable bytes
to become empty. The capacity is not changed.
Buffer sequences previously obtained using @ref data or
@ref prepare become invalid.
@par Exception Safety
No-throw guarantee.
*/
BOOST_BEAST_DECL
void
clear() noexcept;
+10 -12
View File
@@ -29,6 +29,15 @@ static_buffer_base(
{
}
void
static_buffer_base::
clear() noexcept
{
in_off_ = 0;
in_size_ = 0;
out_size_ = 0;
}
auto
static_buffer_base::
data() const noexcept ->
@@ -73,7 +82,7 @@ prepare(std::size_t n) ->
using net::mutable_buffer;
if(n > capacity_ - in_size_)
BOOST_THROW_EXCEPTION(std::length_error{
"buffer overflow"});
"static_buffer overflow"});
out_size_ = n;
auto const out_off =
(in_off_ + in_size_) % capacity_;
@@ -117,17 +126,6 @@ consume(std::size_t n) noexcept
}
}
void
static_buffer_base::
reset(void* p, std::size_t n) noexcept
{
begin_ = static_cast<char*>(p);
capacity_ = n;
in_off_ = 0;
in_size_ = 0;
out_size_ = 0;
}
//------------------------------------------------------------------------------
template<std::size_t N>
+25 -37
View File
@@ -73,9 +73,25 @@ public:
@param size The number of valid bytes pointed to by `p`.
*/
inline
BOOST_BEAST_DECL
static_buffer_base(void* p, std::size_t size) noexcept;
/** Clear the readable and writable bytes to zero.
This function causes the readable and writable bytes
to become empty. The capacity is not changed.
Buffer sequences previously obtained using @ref data or
@ref prepare become invalid.
@par Exception Safety
No-throw guarantee.
*/
BOOST_BEAST_DECL
void
clear() noexcept;
//--------------------------------------------------------------------------
#if BOOST_BEAST_DOXYGEN
@@ -115,7 +131,7 @@ public:
}
/// Returns a constant buffer sequence representing the readable bytes
inline
BOOST_BEAST_DECL
const_buffers_type
data() const noexcept;
@@ -127,7 +143,7 @@ public:
}
/// Returns a mutable buffer sequence representing the readable bytes
inline
BOOST_BEAST_DECL
mutable_data_type
data() noexcept;
@@ -149,7 +165,7 @@ public:
Strong guarantee.
*/
inline
BOOST_BEAST_DECL
mutable_buffers_type
prepare(std::size_t n);
@@ -171,7 +187,7 @@ public:
No-throw guarantee.
*/
inline
BOOST_BEAST_DECL
void
commit(std::size_t n) noexcept;
@@ -190,37 +206,9 @@ public:
No-throw guarantee.
*/
inline
BOOST_BEAST_DECL
void
consume(std::size_t n) noexcept;
protected:
/** Constructor
The buffer will be in an undefined state. It is necessary
for the derived class to call @ref reset in order to
initialize the object.
*/
static_buffer_base() = default;
/** Reset the pointed-to buffer.
This function resets the internal state to the buffer provided.
All input and output sequences are invalidated. This function
allows the derived class to construct its members before
initializing the static buffer.
@param p A pointer to valid storage of at least `n` bytes.
@param size The number of valid bytes pointed to by `p`.
@par Exception Safety
No-throw guarantee.
*/
inline
void
reset(void* p, std::size_t size) noexcept;
};
//------------------------------------------------------------------------------
@@ -261,15 +249,15 @@ class static_buffer : public static_buffer_base
char buf_[N];
public:
/// Constructor
static_buffer(static_buffer const&) noexcept;
/// Constructor
static_buffer() noexcept
: static_buffer_base(buf_, N)
{
}
/// Constructor
static_buffer(static_buffer const&) noexcept;
/// Assignment
static_buffer& operator=(static_buffer const&) noexcept;