Shared Buffer
Loading...
Searching...
No Matches
chops::const_shared_buffer Class Reference

A reference counted non-modifiable buffer class with various convenience methods, providing efficient copying and convenient buffer lifetime management. More...

#include <shared_buffer.hpp>

Public Types

using byte_vec = std::vector<std::byte>
 
using size_type = typename byte_vec::size_type
 

Public Member Functions

 const_shared_buffer (const const_shared_buffer &)=default
 
 const_shared_buffer (const_shared_buffer &&)=default
 
const_shared_bufferoperator= (const const_shared_buffer &)=delete
 
const_shared_bufferoperator= (const_shared_buffer &&)=delete
 
template<std::size_t Ext>
 const_shared_buffer (std::span< const std::byte, Ext > sp)
 Construct by copying from a std::span of std::byte.
 
 const_shared_buffer (const std::byte *buf, std::size_t sz)
 Construct by copying from a std::byte array.
 
template<typename T , std::size_t Ext>
 const_shared_buffer (std::span< const T, Ext > sp)
 Construct by copying from a std::span.
 
template<typename T >
 const_shared_buffer (const T *buf, std::size_t num)
 Construct by copying bytes from an arbitrary pointer.
 
 const_shared_buffer (const void *buf, size_type sz)
 Construct by copying bytes from a void pointer.
 
 const_shared_buffer (const mutable_shared_buffer &rhs)
 Construct by copying from a mutable_shared_buffer object.
 
 const_shared_buffer (mutable_shared_buffer &&rhs) noexcept
 Construct by moving from a mutable_shared_buffer object.
 
 const_shared_buffer (byte_vec &&bv) noexcept
 Move construct from a std::vector of std::bytes.
 
template<typename InIt >
 const_shared_buffer (InIt beg, InIt end)
 Construct from input iterators.
 
const std::byte * data () const noexcept
 Return const std::byte pointer to beginning of buffer.
 
size_type size () const noexcept
 Return size (number of bytes) of buffer.
 
bool empty () const noexcept
 Query to see if size is zero.
 
bool operator== (const const_shared_buffer &rhs) const noexcept
 Compare two const_shared_buffer objects for internal buffer byte-by-byte equality.
 
auto operator<=> (const const_shared_buffer &rhs) const noexcept
 Compare two const_shared_buffer objects for internal buffer byte-by-byte spaceship operator ordering.
 

Friends

bool operator== (const mutable_shared_buffer &lhs, const const_shared_buffer &rhs)
 Compare a mutable_shared_buffer object with a const_shared_buffer for internal buffer byte-by-byte equality.
 
bool operator== (const const_shared_buffer &lhs, const mutable_shared_buffer &rhs)
 Compare a const_shared_buffer object with a mutable_shared_buffer for internal buffer byte-by-byte equality.
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ 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
spstd::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
bufNon-null pointer to std::byte buffer of data. The data is copied into the internal buffer of the const_shared_buffer.
szSize 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
spstd::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
bufNon-null pointer to an array of data.
numNumber 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
bufNon-null void pointer to a buffer of data.
szSize of buffer, in bytes.

◆ const_shared_buffer() [6/9]

chops::const_shared_buffer::const_shared_buffer ( const mutable_shared_buffer & rhs)
inlineexplicit

Construct by copying from a mutable_shared_buffer object.

This constructor will copy from a mutable_shared_buffer. There is an alternative constructor that is more efficient which moves from a mutable_shared_buffer instead of copying.

Parameters
rhsmutable_shared_buffer containing bytes to be copied.

◆ const_shared_buffer() [7/9]

chops::const_shared_buffer::const_shared_buffer ( mutable_shared_buffer && rhs)
inlineexplicitnoexcept

Construct by moving from a mutable_shared_buffer object.

This constructor will move from a mutable_shared_buffer into a const_shared_buffer. This allows efficient API boundaries, where application code can construct and fill in a mutable_shared_buffer, then use this constructor which will std::move it into a const_shared_buffer for use with asynchronous functions.

Parameters
rhsmutable_shared_buffer to be moved from; after moving the mutable_shared_buffer will be empty.

◆ 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
begBeginning input iterator of range.
endEnding input iterator of range.

Member Function Documentation

◆ 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<=>()

auto chops::const_shared_buffer::operator<=> ( const const_shared_buffer & rhs) const
inlinenoexcept

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==()

bool chops::const_shared_buffer::operator== ( const const_shared_buffer & rhs) const
inlinenoexcept

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.

Friends And Related Symbol Documentation

◆ operator== [1/2]

bool operator== ( const const_shared_buffer & lhs,
const mutable_shared_buffer & rhs )
friend

Compare a const_shared_buffer object with a mutable_shared_buffer for internal buffer byte-by-byte equality.

Returns
true if size() same for each, and each byte compares true.

◆ operator== [2/2]

bool operator== ( const mutable_shared_buffer & lhs,
const const_shared_buffer & rhs )
friend

Compare a mutable_shared_buffer object with a const_shared_buffer for internal buffer byte-by-byte equality.

Returns
true if size() same for each, and each byte compares true.

The documentation for this class was generated from the following file: