Shared Buffer
Loading...
Searching...
No Matches
shared_buffer.hpp
1
71#ifndef SHARED_BUFFER_HPP_INCLUDED
72#define SHARED_BUFFER_HPP_INCLUDED
73
74#include <cstddef> // std::byte
75#include <vector>
76#include <memory> // std::shared_ptr
77#include <compare> // spaceship operator
78#include <span>
79#include <bit> // std::bit_cast
80
81#include <utility> // std::move, std::swap
82#include <algorithm> // std::copy
83
84// TODO - add concepts and / or requires
85//
86// modify the templated constructor that takes a buffer of any valid
87// byte type, add constraints which makes the casting safer
88
89namespace chops {
90
91class const_shared_buffer;
92
119public:
120 using byte_vec = std::vector<std::byte>;
121 using size_type = typename byte_vec::size_type;
122
123private:
124 std::shared_ptr<byte_vec> m_data;
125
126private:
127
128 friend class const_shared_buffer;
129
130 friend bool operator==(const mutable_shared_buffer&, const const_shared_buffer&) noexcept;
131 friend bool operator==(const const_shared_buffer&, const mutable_shared_buffer&) noexcept;
132
133public:
134
135 // default copy and move construction, copy and move assignment
138 mutable_shared_buffer& operator=(const mutable_shared_buffer&) = default;
139 mutable_shared_buffer& operator=(mutable_shared_buffer&&) = default;
140
146 m_data{std::make_shared<byte_vec>(size_type(0))} { }
147
155 template <std::size_t Ext>
156 explicit mutable_shared_buffer(std::span<const std::byte, Ext> sp) :
157 m_data{std::make_shared<byte_vec>(sp.data(), sp.data()+sp.size())} { }
158
173 mutable_shared_buffer(const std::byte* buf, std::size_t sz) :
174 mutable_shared_buffer(std::as_bytes(std::span<const std::byte>{buf, sz})) { }
175
186 explicit mutable_shared_buffer(byte_vec&& bv) noexcept :
187 m_data{std::make_shared<byte_vec>(size_type(0))} {
188 *m_data = std::move(bv);
189 }
190
201 explicit mutable_shared_buffer(size_type sz) :
202 m_data{std::make_shared<byte_vec>(sz)} { }
203
204
215 template <typename T, std::size_t Ext>
216 mutable_shared_buffer(std::span<const T, Ext> sp) :
217 mutable_shared_buffer(std::as_bytes(sp)) { }
218
236 template <typename T>
237 mutable_shared_buffer(const T* buf, size_type num) :
238 mutable_shared_buffer(std::as_bytes(std::span<const T>{buf, num})) { }
239
252 mutable_shared_buffer(const void* buf, size_type sz) :
253 mutable_shared_buffer(std::as_bytes(
254 std::span<const std::byte>{std::bit_cast<const std::byte*>(buf), sz})) { }
255
265 template <typename InIt>
266 mutable_shared_buffer(InIt beg, InIt end) :
267 m_data(std::make_shared<byte_vec>(beg, end)) { }
268
281 std::byte* data() noexcept { return m_data->data(); }
282
290 const std::byte* data() const noexcept { return m_data->data(); }
291
297 size_type size() const noexcept { return m_data->size(); }
298
309 byte_vec& get_byte_vec() noexcept { return *m_data; }
310
316 bool empty() const noexcept { return m_data->empty(); }
317
327 void clear() noexcept { m_data->clear(); }
328
339 void resize(size_type sz) { m_data->resize(sz); }
340
344 void swap(mutable_shared_buffer& rhs) noexcept {
345 using std::swap; // swap idiom
346 swap(m_data, rhs.m_data);
347 }
348
358 mutable_shared_buffer& append(const std::byte* buf, std::size_t sz) {
359 size_type old_sz = size();
360 resize(old_sz + sz); // set up buffer space
361 std::copy(buf, buf+sz, data()+old_sz);
362 return *this;
363 }
364
372 template <std::size_t Ext>
373 mutable_shared_buffer& append(std::span<const std::byte, Ext> sp) {
374 return append(sp.data(), sp.size());
375 }
376
389 template <typename T>
390 mutable_shared_buffer& append(const T* buf, std::size_t num) {
391 return append(std::as_bytes(std::span<const T>{buf, num}));
392 }
393
406 mutable_shared_buffer& append(const void* buf, size_type sz) {
407 return append(std::as_bytes(
408 std::span<const std::byte>{std::bit_cast<const std::byte*>(buf), sz}));
409 }
410
424 template <typename T, std::size_t Ext>
425 mutable_shared_buffer& append(std::span<const T, Ext> sp) {
426 return append(std::as_bytes(sp));
427 }
428
437 return append(rhs.data(), rhs.size());
438 }
439
446 return append(rhs);
447 }
448
457 return append(&b, 1);
458 }
459
466 return append(b);
467 }
468
478 bool operator== (const mutable_shared_buffer& rhs) const noexcept {
479 return *m_data == *rhs.m_data;
480 }
481
492 auto operator<=>(const mutable_shared_buffer& rhs) const noexcept {
493 return *m_data <=> *rhs.m_data;
494 }
495
496}; // end mutable_shared_buffer class
497
498// non-member functions
504inline void swap(mutable_shared_buffer& lhs, mutable_shared_buffer& rhs) noexcept {
505 lhs.swap(rhs);
506}
507
508
524public:
525 using byte_vec = std::vector<std::byte>;
526 using size_type = typename byte_vec::size_type;
527
528private:
529 std::shared_ptr<byte_vec> m_data;
530
531private:
532
533 friend bool operator==(const mutable_shared_buffer&, const const_shared_buffer&) noexcept;
534 friend bool operator==(const const_shared_buffer&, const mutable_shared_buffer&) noexcept;
535
536public:
537
538 const_shared_buffer() = delete;
539
540 // default copy and move construction, should do the right thing
543 // copy and move assignment disabled
544 const_shared_buffer& operator=(const const_shared_buffer&) = delete;
545 const_shared_buffer& operator=(const_shared_buffer&&) = delete;
546
554 template <std::size_t Ext>
555 explicit const_shared_buffer(std::span<const std::byte, Ext> sp) :
556 m_data(std::make_shared<byte_vec>(sp.data(), sp.data()+sp.size())) { }
567 const_shared_buffer(const std::byte* buf, std::size_t sz) :
568 const_shared_buffer(std::as_bytes(std::span<const std::byte>{buf, sz})) { }
569
580 template <typename T, std::size_t Ext>
581 const_shared_buffer(std::span<const T, Ext> sp) :
582 const_shared_buffer(std::as_bytes(sp)) { }
601 template <typename T>
602 const_shared_buffer(const T* buf, std::size_t num) :
603 const_shared_buffer(std::as_bytes(std::span<const T>{buf, num})) { }
604
617 const_shared_buffer(const void* buf, size_type sz) :
618 const_shared_buffer(std::as_bytes(
619 std::span<const std::byte>{std::bit_cast<const std::byte*>(buf), sz})) { }
620
631 const_shared_buffer(rhs.data(), rhs.size()) { }
632
644 explicit const_shared_buffer(mutable_shared_buffer&& rhs) noexcept :
645 m_data(std::move(rhs.m_data)) {
646 rhs.m_data = std::make_shared<byte_vec>(0); // set rhs back to invariant
647 }
648
656 explicit const_shared_buffer(byte_vec&& bv) noexcept :
657 m_data(std::make_shared<byte_vec>()) {
658 *m_data = std::move(bv);
659 }
660
670 template <typename InIt>
671 const_shared_buffer(InIt beg, InIt end) : m_data(std::make_shared<byte_vec>(beg, end)) { }
672
685 const std::byte* data() const noexcept { return m_data->data(); }
686
692 size_type size() const noexcept { return m_data->size(); }
693
699 bool empty() const noexcept { return (*m_data).empty(); }
700
711 bool operator== (const const_shared_buffer& rhs) const noexcept {
712 return *m_data == *rhs.m_data;
713 }
724 auto operator<=> (const const_shared_buffer& rhs) const noexcept {
725 return *m_data <=> *rhs.m_data;
726 }
727
728}; // end const_shared_buffer class
729
730// non-member functions
731
738inline bool operator== (const const_shared_buffer& lhs, const mutable_shared_buffer& rhs) noexcept {
739 return *lhs.m_data == *rhs.m_data;
740}
741
748inline bool operator== (const mutable_shared_buffer& lhs, const const_shared_buffer& rhs) noexcept {
749 return *lhs.m_data == *rhs.m_data;
750}
751
752} // end namespace
753
754#endif
755
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