A reference counted non-modifiable buffer class with various convenience methods, providing efficient copying and convenient buffer lifetime management.
More...
#include <shared_buffer.hpp>
|
using | byte_vec = std::vector<std::byte> |
|
using | size_type = typename byte_vec::size_type |
|
A reference counted non-modifiable buffer class with various convenience methods, providing efficient copying and convenient buffer lifetime management.
The primary difference between this class and the mutable_shared_buffer
class is that once a const_shared_buffer
object is constructed, nothing inside it can be modified. This allows it to be used with asynchronous IO functions which depend on the buffer staying the same (i.e. the internal pointer to the data and the size) for the full lifetime of the asynchronous operations.
- Invariant
- There will always be an internal buffer of data, even if the size is zero.
◆ const_shared_buffer() [1/9]
template<std::size_t Ext>
chops::const_shared_buffer::const_shared_buffer |
( |
std::span< const std::byte, Ext > | sp | ) |
|
|
inlineexplicit |
Construct by copying from a std::span
of std::byte
.
- Parameters
-
sp | std::byte span pointing to buffer of data. The data is copied into the internal buffer of the const_shared_buffer . |
◆ const_shared_buffer() [2/9]
chops::const_shared_buffer::const_shared_buffer |
( |
const std::byte * | buf, |
|
|
std::size_t | sz ) |
|
inline |
Construct by copying from a std::byte
array.
- Precondition
- Size cannot be greater than the source buffer.
- Parameters
-
buf | Non-null pointer to std::byte buffer of data. The data is copied into the internal buffer of the const_shared_buffer . |
sz | Size of buffer. |
◆ const_shared_buffer() [3/9]
template<typename T , std::size_t Ext>
chops::const_shared_buffer::const_shared_buffer |
( |
std::span< const T, Ext > | sp | ) |
|
|
inline |
Construct by copying from a std::span
.
The type of the span must be convertible to or be layout compatible with std::byte
.
- Parameters
-
sp | std::span pointing to buffer of data. The std::span pointer is cast into a std::byte pointer and bytes are then copied. |
◆ const_shared_buffer() [4/9]
template<typename T >
chops::const_shared_buffer::const_shared_buffer |
( |
const T * | buf, |
|
|
std::size_t | num ) |
|
inline |
Construct by copying bytes from an arbitrary pointer.
The pointer passed into this constructor is cast into a std::byte
pointer and bytes are then copied. In particular, this method can be used for char
pointers, unsigned
char
pointers, std::uint8_t
pointers, etc. Non character types that are trivially copyable are also allowed, although the usual care must be taken (padding bytes, alignment, etc).
The type of the span must be convertible to or be layout compatible with std::byte
.
- Precondition
- Size cannot be greater than the source buffer.
- Parameters
-
buf | Non-null pointer to an array of data. |
num | Number of elements in the array. |
◆ const_shared_buffer() [5/9]
chops::const_shared_buffer::const_shared_buffer |
( |
const void * | buf, |
|
|
size_type | sz ) |
|
inline |
Construct by copying bytes from a void pointer.
The pointer passed into this constructor is cast into a std::byte
pointer and bytes are then copied.
- Precondition
- Size cannot be greater than the source buffer.
- Parameters
-
buf | Non-null void pointer to a buffer of data. |
sz | Size of buffer, in bytes. |
◆ const_shared_buffer() [6/9]
◆ const_shared_buffer() [7/9]
◆ const_shared_buffer() [8/9]
chops::const_shared_buffer::const_shared_buffer |
( |
byte_vec && | bv | ) |
|
|
inlineexplicitnoexcept |
Move construct from a std::vector
of std::bytes
.
Efficiently construct from a std::vector
of std::bytes
by moving into a const_shared_buffer
.
◆ const_shared_buffer() [9/9]
template<typename InIt >
chops::const_shared_buffer::const_shared_buffer |
( |
InIt | beg, |
|
|
InIt | end ) |
|
inline |
Construct from input iterators.
- Precondition
- Valid iterator range, where each element is convertible to a
std::byte
.
- Parameters
-
beg | Beginning input iterator of range. |
end | Ending input iterator of range. |
◆ data()
const std::byte * chops::const_shared_buffer::data |
( |
| ) |
const |
|
inlinenoexcept |
Return const
std::byte
pointer to beginning of buffer.
This method provides pointer access to the beginning of the buffer. If the buffer is empty the pointer cannot be dereferenced or undefined behavior will occur.
Accessing past the end of the internal buffer (as defined by the size()
method) results in undefined behavior.
- Returns
const
std::byte
pointer to buffer.
◆ empty()
bool chops::const_shared_buffer::empty |
( |
| ) |
const |
|
inlinenoexcept |
Query to see if size is zero.
- Returns
true
if empty (size equals zero).
◆ operator<=>()
Compare two const_shared_buffer
objects for internal buffer byte-by-byte spaceship operator ordering.
Internally this invokes the std::vector
<=>
on std::byte
elements.
- Returns
- Spaceship operator comparison result.
◆ operator==()
Compare two const_shared_buffer
objects for internal buffer byte-by-byte equality.
Internally this invokes the std::vector
operator==
on std::byte
elements.
- Returns
true
if size()
same for each, and each byte compares true
.
◆ size()
size_type chops::const_shared_buffer::size |
( |
| ) |
const |
|
inlinenoexcept |
Return size (number of bytes) of buffer.
- Returns
- Size of buffer, which may be zero.
◆ operator== [1/2]
◆ operator== [2/2]
The documentation for this class was generated from the following file: