Asio Extensions
Additional functionality built on top of (Boost.)Asio
asioext::basic_linear_buffer< Allocator > Class Template Reference

A dynamically sized contiguously stored buffer. More...

#include <asioext/linear_buffer.hpp>

Public Types

typedef Allocator allocator_type
 
typedef std::allocator_traits< allocator_typeallocator_traits_type
 
typedef uint8_t * iterator
 The type used to represent an iterator for the buffer's data. More...
 
typedef const uint8_t * const_iterator
 
typedef uint8_t & reference
 The type used to represent a reference to a single byte inside the buffer. More...
 
typedef const uint8_t & const_reference
 

Public Member Functions

 basic_linear_buffer () noexcept
 Default-construct a basic_linear_buffer. More...
 
 basic_linear_buffer (const Allocator &a) noexcept
 Construct a dynamic buffer from an allocator. More...
 
 basic_linear_buffer (std::size_t initial_size, std::size_t maximum_size=(std::numeric_limits< std::size_t >::max)())
 Construct a dynamic buffer. More...
 
 basic_linear_buffer (const Allocator &a, std::size_t initial_size, std::size_t maximum_size=(std::numeric_limits< std::size_t >::max)())
 Construct a dynamic buffer from an allocator. More...
 
 basic_linear_buffer (basic_linear_buffer &&other) noexcept
 Move-construct a dynamic buffer. More...
 
 ~basic_linear_buffer ()
 Destroy the basic_linear_buffer. More...
 
basic_linear_bufferoperator= (basic_linear_buffer &&other) noexcept
 Move-assign a dynamic buffer. More...
 
std::size_t size () const noexcept
 Get the size of the input sequence. More...
 
std::size_t max_size () const noexcept
 Get the maximum size of the dynamic buffer. More...
 
std::size_t capacity () const noexcept
 Get the current capacity of the dynamic buffer. More...
 
iterator begin () noexcept
 Get an iterator pointing at the buffer data beginning. More...
 
iterator end () noexcept
 Get an iterator pointing at the buffer data end. More...
 
const_iterator begin () const noexcept
 Get an iterator pointing at the buffer data beginning. More...
 
const_iterator end () const noexcept
 Get an iterator pointing at the buffer data end. More...
 
uint8_t * data () noexcept
 Get a pointer to the buffer data beginning. More...
 
const uint8_t * data () const noexcept
 Get a pointer to the buffer data beginning. More...
 
reference operator[] (std::size_t i) noexcept
 Get a reference to a specific byte inside the buffer. More...
 
const_reference operator[] (std::size_t i) const noexcept
 Get a reference to a specific byte inside the buffer. More...
 
void append (const void *data, std::size_t n)
 Append the given data to the buffer. More...
 
void insert (const_iterator before_this, const void *data, std::size_t n)
 Insert the given data before the specified position. More...
 
void insert (std::size_t before_this, const void *data, std::size_t n)
 Insert the given data before the specified position. More...
 
void erase (const_iterator pos)
 Erase the single byte at the specified position. More...
 
void erase (const_iterator first, const_iterator last)
 Erase the bytes in the range [first, last). More...
 
void erase (std::size_t pos)
 Erase the single byte at the specified position. More...
 
void erase (std::size_t first, std::size_t last)
 Erase the bytes in the range [first, last). More...
 
void resize (std::size_t new_size)
 Resize the buffer. More...
 
void clear () noexcept
 Clear the buffer. More...
 

Detailed Description

template<typename Allocator = std::allocator<uint8_t>>
class asioext::basic_linear_buffer< Allocator >

A dynamically sized contiguously stored buffer.

This class owns and manages a contiguous buffer that is divided into an input and output sequence.

Note
Unlike Asio's DynamicBuffer this class owns the buffer it manages. Unless you are not interested in the data read/written and the buffer object can be destroyed once the operation finishes, it's not recommended to use this class with the DynamicBuffer functions provided by Asio.

Member Typedef Documentation

◆ allocator_traits_type

template<typename Allocator = std::allocator<uint8_t>>
typedef std::allocator_traits<allocator_type> asioext::basic_linear_buffer< Allocator >::allocator_traits_type

◆ allocator_type

template<typename Allocator = std::allocator<uint8_t>>
typedef Allocator asioext::basic_linear_buffer< Allocator >::allocator_type

◆ const_iterator

template<typename Allocator = std::allocator<uint8_t>>
typedef const uint8_t* asioext::basic_linear_buffer< Allocator >::const_iterator

The type used to represent an iterator for a constant view of the buffer's data.

◆ const_reference

template<typename Allocator = std::allocator<uint8_t>>
typedef const uint8_t& asioext::basic_linear_buffer< Allocator >::const_reference

The type used to represent a const. reference to a single byte inside the buffer.

◆ iterator

template<typename Allocator = std::allocator<uint8_t>>
typedef uint8_t* asioext::basic_linear_buffer< Allocator >::iterator

The type used to represent an iterator for the buffer's data.

◆ reference

template<typename Allocator = std::allocator<uint8_t>>
typedef uint8_t& asioext::basic_linear_buffer< Allocator >::reference

The type used to represent a reference to a single byte inside the buffer.

Constructor & Destructor Documentation

◆ basic_linear_buffer() [1/5]

template<typename Allocator = std::allocator<uint8_t>>
asioext::basic_linear_buffer< Allocator >::basic_linear_buffer ( )
inlinenoexcept

Default-construct a basic_linear_buffer.

The constructed basic_linear_buffer is empty and doesn't have any allocated memory.

◆ basic_linear_buffer() [2/5]

template<typename Allocator = std::allocator<uint8_t>>
asioext::basic_linear_buffer< Allocator >::basic_linear_buffer ( const Allocator &  a)
inlineexplicitnoexcept

Construct a dynamic buffer from an allocator.

The constructed basic_linear_buffer is empty and doesn't have any allocated memory.

Parameters
aThe allocator that shall be used to allocate the buffer's storage.

◆ basic_linear_buffer() [3/5]

template<typename Allocator = std::allocator<uint8_t>>
asioext::basic_linear_buffer< Allocator >::basic_linear_buffer ( std::size_t  initial_size,
std::size_t  maximum_size = (std::numeric_limits<std::size_t>::max)() 
)
inline

Construct a dynamic buffer.

Parameters
initial_sizeThe initial size that the buffer starts with.
maximum_sizeSpecifies a maximum size for the buffer, in bytes.

◆ basic_linear_buffer() [4/5]

template<typename Allocator = std::allocator<uint8_t>>
asioext::basic_linear_buffer< Allocator >::basic_linear_buffer ( const Allocator &  a,
std::size_t  initial_size,
std::size_t  maximum_size = (std::numeric_limits<std::size_t>::max)() 
)
inline

Construct a dynamic buffer from an allocator.

Parameters
aThe allocator that shall be used to allocate the buffer's storage.
initial_sizeThe initial size that the buffer starts with.
maximum_sizeSpecifies a maximum size for the buffer, in bytes.

◆ basic_linear_buffer() [5/5]

template<typename Allocator = std::allocator<uint8_t>>
asioext::basic_linear_buffer< Allocator >::basic_linear_buffer ( basic_linear_buffer< Allocator > &&  other)
inlinenoexcept

Move-construct a dynamic buffer.

After the move, other is an empty buffer with no allocated memory (as-if just default-constructed).

◆ ~basic_linear_buffer()

template<typename Allocator = std::allocator<uint8_t>>
asioext::basic_linear_buffer< Allocator >::~basic_linear_buffer ( )
inline

Destroy the basic_linear_buffer.

Deallocates all owned data.

Member Function Documentation

◆ append()

template<typename Allocator = std::allocator<uint8_t>>
void asioext::basic_linear_buffer< Allocator >::append ( const void *  data,
std::size_t  n 
)

Append the given data to the buffer.

This function appends the given raw data to the buffer, resizing it as necessary.

If the buffer is resized, all iterators and references (including the end() iterator) are invalidated. Otherwise only the end() iterator is invalidated.

Parameters
dataThe raw bytes to append.
nNumber of raw bytes to append.
Exceptions
std::length_errorIf size() + n > max_size().

◆ begin() [1/2]

template<typename Allocator = std::allocator<uint8_t>>
iterator asioext::basic_linear_buffer< Allocator >::begin ( )
inlinenoexcept

Get an iterator pointing at the buffer data beginning.

◆ begin() [2/2]

template<typename Allocator = std::allocator<uint8_t>>
const_iterator asioext::basic_linear_buffer< Allocator >::begin ( ) const
inlinenoexcept

Get an iterator pointing at the buffer data beginning.

◆ capacity()

template<typename Allocator = std::allocator<uint8_t>>
std::size_t asioext::basic_linear_buffer< Allocator >::capacity ( ) const
inlinenoexcept

Get the current capacity of the dynamic buffer.

Returns
The current total capacity of the buffer, i.e. for both the input sequence and output sequence.

◆ clear()

template<typename Allocator = std::allocator<uint8_t>>
void asioext::basic_linear_buffer< Allocator >::clear ( )
inlinenoexcept

Clear the buffer.

Clears the buffer and resets it to zero size, without deallocating the memory.

◆ data() [1/2]

template<typename Allocator = std::allocator<uint8_t>>
uint8_t* asioext::basic_linear_buffer< Allocator >::data ( )
inlinenoexcept

Get a pointer to the buffer data beginning.

◆ data() [2/2]

template<typename Allocator = std::allocator<uint8_t>>
const uint8_t* asioext::basic_linear_buffer< Allocator >::data ( ) const
inlinenoexcept

Get a pointer to the buffer data beginning.

◆ end() [1/2]

template<typename Allocator = std::allocator<uint8_t>>
iterator asioext::basic_linear_buffer< Allocator >::end ( )
inlinenoexcept

Get an iterator pointing at the buffer data end.

◆ end() [2/2]

template<typename Allocator = std::allocator<uint8_t>>
const_iterator asioext::basic_linear_buffer< Allocator >::end ( ) const
inlinenoexcept

Get an iterator pointing at the buffer data end.

◆ erase() [1/4]

template<typename Allocator = std::allocator<uint8_t>>
void asioext::basic_linear_buffer< Allocator >::erase ( const_iterator  pos)
inline

Erase the single byte at the specified position.

This function erases the single byte at the given position.

All iterators and references after the erased byte are invalidated.

Parameters
posIterator to the byte to remove. Passing a pseudo-iterator such as end() is not allowed.

◆ erase() [2/4]

template<typename Allocator = std::allocator<uint8_t>>
void asioext::basic_linear_buffer< Allocator >::erase ( const_iterator  first,
const_iterator  last 
)
inline

Erase the bytes in the range [first, last).

This function erases the bytes in the range [first, last).

All iterators and references after the first erased byte are invalidated.

Parameters
firstIterator to the first byte to remove.
lastIterator to one past the last byte to remove.

◆ erase() [3/4]

template<typename Allocator = std::allocator<uint8_t>>
void asioext::basic_linear_buffer< Allocator >::erase ( std::size_t  pos)

Erase the single byte at the specified position.

This function erases the single byte at the given position.

All iterators and references after the erased byte are invalidated.

Parameters
posPosition of the byte to remove. Must be in [0, size()).

◆ erase() [4/4]

template<typename Allocator = std::allocator<uint8_t>>
void asioext::basic_linear_buffer< Allocator >::erase ( std::size_t  first,
std::size_t  last 
)

Erase the bytes in the range [first, last).

This function erases the bytes in the range [first, last).

All iterators and references after the first erased byte are invalidated.

Parameters
firstPosition of the first byte to remove.
lastPosition of one past the last byte to remove.

◆ insert() [1/2]

template<typename Allocator = std::allocator<uint8_t>>
void asioext::basic_linear_buffer< Allocator >::insert ( const_iterator  before_this,
const void *  data,
std::size_t  n 
)
inline

Insert the given data before the specified position.

This function inserts the given raw data before the buffer position pointed at by before_this. If the new buffer size would exceed the buffer's capacity, it is resized.

If the buffer is resized, all iterators and references (including the end() iterator) are invalidated. Otherwise only the iterators [before_this, end()] as well as their corresponding references are invalidated.

Parameters
before_thisIterator before which the content will be inserted. before_this may be the begin() or the end() iterator.
dataThe raw bytes to insert.
nNumber of raw bytes to insert.

◆ insert() [2/2]

template<typename Allocator = std::allocator<uint8_t>>
void asioext::basic_linear_buffer< Allocator >::insert ( std::size_t  before_this,
const void *  data,
std::size_t  n 
)

Insert the given data before the specified position.

This function inserts the given raw data before the buffer position pointed at by before_this. If the new buffer size would exceed the buffer's capacity, it is resized.

If the buffer is resized, all iterators and references (including the end() iterator) are invalidated. Otherwise only the iterators [before_this, end()] as well as their corresponding references are invalidated.

Parameters
before_thisPosition before which the content will be inserted. before_this may be 0 or the size().
dataThe raw bytes to insert.
nNumber of raw bytes to insert.

◆ max_size()

template<typename Allocator = std::allocator<uint8_t>>
std::size_t asioext::basic_linear_buffer< Allocator >::max_size ( ) const
inlinenoexcept

Get the maximum size of the dynamic buffer.

Returns
The allowed maximum of the sum of the sizes of the input sequence and output sequence.

◆ operator=()

template<typename Allocator = std::allocator<uint8_t>>
basic_linear_buffer& asioext::basic_linear_buffer< Allocator >::operator= ( basic_linear_buffer< Allocator > &&  other)
noexcept

Move-assign a dynamic buffer.

After the move, other is an empty buffer with no allocated memory (as-if just default-constructed).

◆ operator[]() [1/2]

template<typename Allocator = std::allocator<uint8_t>>
reference asioext::basic_linear_buffer< Allocator >::operator[] ( std::size_t  i)
inlinenoexcept

Get a reference to a specific byte inside the buffer.

Parameters
iOffset of the wanted byte.

◆ operator[]() [2/2]

template<typename Allocator = std::allocator<uint8_t>>
const_reference asioext::basic_linear_buffer< Allocator >::operator[] ( std::size_t  i) const
inlinenoexcept

Get a reference to a specific byte inside the buffer.

Parameters
iOffset of the wanted byte.

◆ resize()

template<typename Allocator = std::allocator<uint8_t>>
void asioext::basic_linear_buffer< Allocator >::resize ( std::size_t  new_size)

Resize the buffer.

This function changes the buffer's size to new_size.

If the buffer is resized, all iterators and references (including the end() iterator) are invalidated.

◆ size()

template<typename Allocator = std::allocator<uint8_t>>
std::size_t asioext::basic_linear_buffer< Allocator >::size ( ) const
inlinenoexcept

Get the size of the input sequence.