71#ifndef SHARED_BUFFER_HPP_INCLUDED
72#define SHARED_BUFFER_HPP_INCLUDED
91class const_shared_buffer;
120 using byte_vec = std::vector<std::byte>;
121 using size_type =
typename byte_vec::size_type;
124 std::shared_ptr<byte_vec> m_data;
146 m_data{std::make_shared<byte_vec>(size_type(0))} { }
155 template <std::
size_t Ext>
157 m_data{std::make_shared<byte_vec>(sp.
data(), sp.
data()+sp.
size())} { }
187 m_data{std::make_shared<byte_vec>(size_type(0))} {
188 *m_data = std::move(bv);
202 m_data{std::make_shared<byte_vec>(sz)} { }
215 template <
typename T, std::
size_t Ext>
236 template <
typename T>
254 std::span<const std::byte>{std::bit_cast<const std::byte*>(buf), sz})) { }
265 template <
typename InIt>
267 m_data(std::make_shared<byte_vec>(beg, end)) { }
281 std::byte*
data() noexcept {
return m_data->data(); }
290 const std::byte*
data() const noexcept {
return m_data->data(); }
297 size_type
size() const noexcept {
return m_data->size(); }
316 bool empty() const noexcept {
return m_data->empty(); }
327 void clear() noexcept { m_data->clear(); }
339 void resize(size_type sz) { m_data->resize(sz); }
346 swap(m_data, rhs.m_data);
359 size_type old_sz =
size();
361 std::copy(buf, buf+sz,
data()+old_sz);
372 template <std::
size_t Ext>
374 return append(sp.data(), sp.size());
389 template <
typename T>
391 return append(std::as_bytes(std::span<const T>{buf, num}));
407 return append(std::as_bytes(
408 std::span<const std::byte>{std::bit_cast<const std::byte*>(buf), sz}));
424 template <
typename T, std::
size_t Ext>
426 return append(std::as_bytes(sp));
479 return *m_data == *rhs.m_data;
493 return *m_data <=> *rhs.m_data;
504inline void swap(mutable_shared_buffer& lhs, mutable_shared_buffer& rhs)
noexcept {
525 using byte_vec = std::vector<std::byte>;
526 using size_type =
typename byte_vec::size_type;
529 std::shared_ptr<byte_vec> m_data;
554 template <std::
size_t Ext>
556 m_data(std::make_shared<byte_vec>(sp.
data(), sp.
data()+sp.
size())) { }
580 template <
typename T, std::
size_t Ext>
601 template <
typename T>
619 std::span<const std::byte>{std::bit_cast<const std::byte*>(buf), sz})) { }
645 m_data(std::move(rhs.m_data)) {
646 rhs.m_data = std::make_shared<byte_vec>(0);
657 m_data(std::make_shared<byte_vec>()) {
658 *m_data = std::move(bv);
670 template <
typename InIt>
685 const std::byte*
data() const noexcept {
return m_data->data(); }
692 size_type
size() const noexcept {
return m_data->size(); }
699 bool empty() const noexcept {
return (*m_data).empty(); }
712 return *m_data == *rhs.m_data;
725 return *m_data <=> *rhs.m_data;
739 return *lhs.m_data == *rhs.m_data;
749 return *lhs.m_data == *rhs.m_data;
A reference counted non-modifiable buffer class with various convenience methods, providing efficient...
Definition shared_buffer.hpp:523
const_shared_buffer(std::span< const T, Ext > sp)
Construct by copying from a std::span.
Definition shared_buffer.hpp:581
const_shared_buffer(std::span< const std::byte, Ext > sp)
Construct by copying from a std::span of std::byte.
Definition shared_buffer.hpp:555
auto operator<=>(const const_shared_buffer &rhs) const noexcept
Compare two const_shared_buffer objects for internal buffer byte-by-byte spaceship operator ordering.
Definition shared_buffer.hpp:724
friend bool operator==(const mutable_shared_buffer &, const const_shared_buffer &) noexcept
Compare a mutable_shared_buffer object with a const_shared_buffer for internal buffer byte-by-byte eq...
Definition shared_buffer.hpp:748
const_shared_buffer(mutable_shared_buffer &&rhs) noexcept
Construct by moving from a mutable_shared_buffer object.
Definition shared_buffer.hpp:644
bool empty() const noexcept
Query to see if size is zero.
Definition shared_buffer.hpp:699
const_shared_buffer(const mutable_shared_buffer &rhs)
Construct by copying from a mutable_shared_buffer object.
Definition shared_buffer.hpp:630
size_type size() const noexcept
Return size (number of bytes) of buffer.
Definition shared_buffer.hpp:692
const_shared_buffer(InIt beg, InIt end)
Construct from input iterators.
Definition shared_buffer.hpp:671
const_shared_buffer(const std::byte *buf, std::size_t sz)
Construct by copying from a std::byte array.
Definition shared_buffer.hpp:567
const_shared_buffer(byte_vec &&bv) noexcept
Move construct from a std::vector of std::bytes.
Definition shared_buffer.hpp:656
const_shared_buffer(const void *buf, size_type sz)
Construct by copying bytes from a void pointer.
Definition shared_buffer.hpp:617
const std::byte * data() const noexcept
Return const std::byte pointer to beginning of buffer.
Definition shared_buffer.hpp:685
const_shared_buffer(const T *buf, std::size_t num)
Construct by copying bytes from an arbitrary pointer.
Definition shared_buffer.hpp:602
A mutable (modifiable) byte buffer class with convenience methods, internally reference-counted for e...
Definition shared_buffer.hpp:118
mutable_shared_buffer(const void *buf, size_type sz)
Construct by copying bytes from a void pointer.
Definition shared_buffer.hpp:252
mutable_shared_buffer & append(const std::byte *buf, std::size_t sz)
Append a std::byte buffer to the end of the internal buffer.
Definition shared_buffer.hpp:358
void resize(size_type sz)
Resize internal buffer.
Definition shared_buffer.hpp:339
std::byte * data() noexcept
Return std::byte pointer to beginning of buffer.
Definition shared_buffer.hpp:281
auto operator<=>(const mutable_shared_buffer &rhs) const noexcept
Compare two mutable_shared_buffer objects for internal buffer byte-by-byte spaceship operator orderin...
Definition shared_buffer.hpp:492
mutable_shared_buffer & append(const T *buf, std::size_t num)
Append by copying bytes from an arbitrary pointer.
Definition shared_buffer.hpp:390
mutable_shared_buffer() noexcept
Default construct the mutable_shared_buffer.
Definition shared_buffer.hpp:145
mutable_shared_buffer(const std::byte *buf, std::size_t sz)
Construct by copying from a std::byte array.
Definition shared_buffer.hpp:173
friend bool operator==(const mutable_shared_buffer &, const const_shared_buffer &) noexcept
Compare a mutable_shared_buffer object with a const_shared_buffer for internal buffer byte-by-byte eq...
Definition shared_buffer.hpp:748
mutable_shared_buffer(const T *buf, size_type num)
Construct by copying bytes from an arbitrary pointer.
Definition shared_buffer.hpp:237
mutable_shared_buffer(std::span< const T, Ext > sp)
Construct by copying bytes from a std::span.
Definition shared_buffer.hpp:216
mutable_shared_buffer & append(const void *buf, size_type sz)
Append by copying bytes from a void pointer.
Definition shared_buffer.hpp:406
mutable_shared_buffer & append(std::span< const T, Ext > sp)
Append a std::span that is a non std::byte buffer.
Definition shared_buffer.hpp:425
mutable_shared_buffer(InIt beg, InIt end)
Construct from input iterators.
Definition shared_buffer.hpp:266
void swap(mutable_shared_buffer &rhs) noexcept
Swap with the contents of another mutable_shared_buffer object.
Definition shared_buffer.hpp:344
void clear() noexcept
Clear the internal contents back to an empty state.
Definition shared_buffer.hpp:327
mutable_shared_buffer & operator+=(const mutable_shared_buffer &rhs)
Append the contents of another mutable_shared_buffer to the end.
Definition shared_buffer.hpp:445
mutable_shared_buffer & operator+=(std::byte b)
Append a single std::byte to the end.
Definition shared_buffer.hpp:465
mutable_shared_buffer & append(std::span< const std::byte, Ext > sp)
Append a std::span of std::bytes to the end of the internal buffer.
Definition shared_buffer.hpp:373
mutable_shared_buffer(byte_vec &&bv) noexcept
Move construct from a std::vector of std::bytes.
Definition shared_buffer.hpp:186
mutable_shared_buffer & append(const mutable_shared_buffer &rhs)
Append the contents of another mutable_shared_buffer to the end.
Definition shared_buffer.hpp:436
mutable_shared_buffer & append(std::byte b)
Append a single std::byte to the end.
Definition shared_buffer.hpp:456
mutable_shared_buffer(std::span< const std::byte, Ext > sp)
Construct by copying from a std::span of std::byte.
Definition shared_buffer.hpp:156
mutable_shared_buffer(size_type sz)
Construct a mutable_shared_buffer with an initial size, contents of each byte set to zero.
Definition shared_buffer.hpp:201
size_type size() const noexcept
Return size (number of bytes) of buffer.
Definition shared_buffer.hpp:297
bool empty() const noexcept
Query to see if size is zero.
Definition shared_buffer.hpp:316
const std::byte * data() const noexcept
Return const std::byte pointer to beginning of buffer.
Definition shared_buffer.hpp:290
byte_vec & get_byte_vec() noexcept
Return access to underlying std::vector.
Definition shared_buffer.hpp:309