CARLA
Loading...
Searching...
No Matches
Private Member Functions | Private Attributes | Friends | List of all members
carla::Buffer Class Reference

A piece of raw data. More...

#include <Buffer.h>

Public Types

Member types
using value_type = unsigned char
 
using size_type = uint32_t
 
using iterator = value_type *
 
using const_iterator = const value_type *
 

Public Member Functions

Construction and destruction
 Buffer ()=default
 Create an empty buffer.
 
 Buffer (size_type size)
 Create a buffer with size bytes allocated.
 
 Buffer (uint64_t size)
 Create a buffer with size bytes allocated.
 
template<typename T >
 Buffer (const T &source)
 Copy source into this buffer. Allocates the necessary memory.
 
 Buffer (const value_type *data, size_type size)
 
 Buffer (const value_type *data, uint64_t size)
 Create a buffer with size bytes allocated.
 
 Buffer (const Buffer &)=delete
 
 Buffer (Buffer &&rhs) noexcept
 
 ~Buffer ()
 
Assignment
Buffer & operator= (const Buffer &)=delete
 
Buffer & operator= (Buffer &&rhs) noexcept
 
Data access
const value_type & operator[] (size_t i) const
 Access the byte at position i.
 
value_type & operator[] (size_t i)
 Access the byte at position i.
 
const value_type * data () const noexcept
 Direct access to the allocated memory or nullptr if no memory is allocated.
 
value_type * data () noexcept
 Direct access to the allocated memory or nullptr if no memory is allocated.
 
boost::asio::const_buffer cbuffer () const noexcept
 Make a boost::asio::buffer from this buffer.
 
boost::asio::const_buffer buffer () const noexcept
 Make a boost::asio::buffer from this buffer.
 
boost::asio::mutable_buffer buffer () noexcept
 Make a boost::asio::buffer from this buffer.
 
Iterators
const_iterator cbegin () const noexcept
 
const_iterator begin () const noexcept
 
iterator begin () noexcept
 
const_iterator cend () const noexcept
 
const_iterator end () const noexcept
 
iterator end () noexcept
 
Modifiers
void reset (size_type size)
 Reset the size of this buffer.
 
void reset (uint64_t size)
 Reset the size of this buffer.
 
void resize (uint64_t size)
 Resize the buffer, a new block of size size is allocated if the capacity is not enough and the data is copied.
 
std::unique_ptr< value_type[]> pop () noexcept
 Release the contents of this buffer and set its size and capacity to zero.
 
void clear () noexcept
 Clear the contents of this buffer and set its size and capacity to zero.
 
copy_from
template<typename T >
void copy_from (const T &source)
 Copy source into this buffer. Allocates memory if necessary.
 
void copy_from (const value_type *data, size_type size)
 Copy size bytes of the memory pointed by data into this buffer.
 
void copy_from (size_type offset, const Buffer &rhs)
 Copy source into this buffer leaving at the front an offset of offset bytes uninitialized.
 
template<typename T >
std::enable_if< boost::asio::is_const_buffer_sequence< T >::value >::type copy_from (size_type offset, const T &source)
 Copy source into this buffer leaving at the front an offset of offset bytes uninitialized.
 
template<typename T >
std::enable_if<!boost::asio::is_const_buffer_sequence< T >::value >::type copy_from (size_type offset, const T &source)
 Copy source into this buffer leaving at the front an offset of offset bytes uninitialized.
 
void copy_from (size_type offset, const value_type *data, size_type size)
 Copy size bytes of the memory pointed by data into this buffer, leaving at the front an offset of offset bytes uninitialized.
 

Private Member Functions

void ReuseThisBuffer ()
 

Private Attributes

size_type _capacity = 0u
 
std::unique_ptr< value_type[]> _data = nullptr
 
std::weak_ptr< BufferPool > _parent_pool
 
size_type _size = 0u
 

Friends

class BufferPool
 
class BufferView
 

Capacity

bool empty () const noexcept
 
size_type size () const noexcept
 
size_type capacity () const noexcept
 
static constexpr size_type max_size () noexcept
 

Detailed Description

A piece of raw data.

Note that if more capacity is needed, a new memory block is allocated and the old one is deleted. This means that by default the buffer can only grow. To release the memory use clear or pop.

This is a move-only type, meant to be cheap to pass by value. If the buffer is retrieved from a BufferPool, the memory is automatically pushed back to the pool on destruction.

Warning
Creating a buffer bigger than max_size() is undefined.

Definition at line 42 of file carla/Buffer.h.

Member Typedef Documentation

◆ const_iterator

Definition at line 57 of file carla/Buffer.h.

◆ iterator

Definition at line 55 of file carla/Buffer.h.

◆ size_type

using carla::Buffer::size_type = uint32_t

Definition at line 53 of file carla/Buffer.h.

◆ value_type

using carla::Buffer::value_type = unsigned char

Definition at line 51 of file carla/Buffer.h.

Constructor & Destructor Documentation

◆ Buffer() [1/8]

carla::Buffer::Buffer ( )
default

Create an empty buffer.

◆ Buffer() [2/8]

carla::Buffer::Buffer ( size_type  size)
inlineexplicit

Create a buffer with size bytes allocated.

Definition at line 71 of file carla/Buffer.h.

◆ Buffer() [3/8]

carla::Buffer::Buffer ( uint64_t  size)
inlineexplicit

Create a buffer with size bytes allocated.

Definition at line 77 of file carla/Buffer.h.

References max_size(), size(), and carla::throw_exception().

◆ Buffer() [4/8]

template<typename T >
carla::Buffer::Buffer ( const T &  source)
inlineexplicit

Copy source into this buffer. Allocates the necessary memory.

Definition at line 87 of file carla/Buffer.h.

References copy_from().

◆ Buffer() [5/8]

carla::Buffer::Buffer ( const value_type *  data,
size_type  size 
)
inlineexplicit

Definition at line 91 of file carla/Buffer.h.

References copy_from(), data(), and size().

◆ Buffer() [6/8]

carla::Buffer::Buffer ( const value_type *  data,
uint64_t  size 
)
inlineexplicit

Create a buffer with size bytes allocated.

Definition at line 96 of file carla/Buffer.h.

References max_size(), size(), and carla::throw_exception().

◆ Buffer() [7/8]

carla::Buffer::Buffer ( const Buffer &  )
delete

◆ Buffer() [8/8]

carla::Buffer::Buffer ( Buffer &&  rhs)
inlinenoexcept

Definition at line 106 of file carla/Buffer.h.

◆ ~Buffer()

carla::Buffer::~Buffer ( )
inline

Definition at line 112 of file carla/Buffer.h.

References _capacity, and ReuseThisBuffer().

Member Function Documentation

◆ begin() [1/2]

const_iterator carla::Buffer::begin ( ) const
inlinenoexcept

◆ begin() [2/2]

iterator carla::Buffer::begin ( )
inlinenoexcept

Definition at line 225 of file carla/Buffer.h.

References _data.

◆ buffer() [1/2]

boost::asio::const_buffer carla::Buffer::buffer ( ) const
inlinenoexcept

Make a boost::asio::buffer from this buffer.

Warning
Boost.Asio buffers do not own the data, it's up to the caller to not delete the memory that this buffer holds until the asio buffer is no longer used.

Definition at line 176 of file carla/Buffer.h.

References cbuffer().

Referenced by carla::multigpu::IncomingMessage::buffer(), carla::streaming::detail::tcp::IncomingMessage::buffer(), copy_from(), and copy_from().

◆ buffer() [2/2]

boost::asio::mutable_buffer carla::Buffer::buffer ( )
inlinenoexcept

Make a boost::asio::buffer from this buffer.

Warning
Boost.Asio buffers do not own the data, it's up to the caller to not delete the memory that this buffer holds until the asio buffer is no longer used.

Definition at line 181 of file carla/Buffer.h.

References data(), and size().

◆ capacity()

size_type carla::Buffer::capacity ( ) const
inlinenoexcept

Definition at line 205 of file carla/Buffer.h.

References _capacity.

Referenced by carla::BufferView::capacity().

◆ cbegin()

const_iterator carla::Buffer::cbegin ( ) const
inlinenoexcept

Definition at line 217 of file carla/Buffer.h.

References _data.

Referenced by begin(), carla::BufferView::begin(), cend(), and carla::BufferView::cend().

◆ cbuffer()

boost::asio::const_buffer carla::Buffer::cbuffer ( ) const
inlinenoexcept

Make a boost::asio::buffer from this buffer.

Warning
Boost.Asio buffers do not own the data, it's up to the caller to not delete the memory that this buffer holds until the asio buffer is no longer used.

Definition at line 171 of file carla/Buffer.h.

References data(), and size().

Referenced by buffer().

◆ cend()

const_iterator carla::Buffer::cend ( ) const
inlinenoexcept

Definition at line 229 of file carla/Buffer.h.

References cbegin(), and size().

Referenced by end(), and carla::BufferView::end().

◆ clear()

void carla::Buffer::clear ( )
inlinenoexcept

Clear the contents of this buffer and set its size and capacity to zero.

Deletes allocated memory.

Definition at line 291 of file carla/Buffer.h.

References pop().

◆ copy_from() [1/6]

template<typename T >
void carla::Buffer::copy_from ( const T &  source)
inline

◆ copy_from() [2/6]

void carla::Buffer::copy_from ( const value_type *  data,
size_type  size 
)
inline

Copy size bytes of the memory pointed by data into this buffer.

Allocates memory if necessary.

Definition at line 311 of file carla/Buffer.h.

References copy_from(), data(), and size().

◆ copy_from() [3/6]

void carla::Buffer::copy_from ( size_type  offset,
const Buffer &  rhs 
)
inline

Copy source into this buffer leaving at the front an offset of offset bytes uninitialized.

Allocates memory if necessary.

Definition at line 317 of file carla/Buffer.h.

References buffer(), and copy_from().

◆ copy_from() [4/6]

template<typename T >
std::enable_if< boost::asio::is_const_buffer_sequence< T >::value >::type carla::Buffer::copy_from ( size_type  offset,
const T &  source 
)
inline

Copy source into this buffer leaving at the front an offset of offset bytes uninitialized.

Allocates memory if necessary.

Definition at line 324 of file carla/Buffer.h.

References buffer(), DEBUG_ASSERT, DEBUG_ONLY, reset(), and size().

◆ copy_from() [5/6]

template<typename T >
std::enable_if<!boost::asio::is_const_buffer_sequence< T >::value >::type carla::Buffer::copy_from ( size_type  offset,
const T &  source 
)
inline

Copy source into this buffer leaving at the front an offset of offset bytes uninitialized.

Allocates memory if necessary.

Definition at line 335 of file carla/Buffer.h.

References copy_from().

◆ copy_from() [6/6]

void carla::Buffer::copy_from ( size_type  offset,
const value_type *  data,
size_type  size 
)
inline

Copy size bytes of the memory pointed by data into this buffer, leaving at the front an offset of offset bytes uninitialized.

Allocates memory if necessary.

Definition at line 353 of file carla/Buffer.h.

References copy_from(), data(), and size().

◆ data() [1/2]

const value_type * carla::Buffer::data ( ) const
inlinenoexcept

◆ data() [2/2]

value_type * carla::Buffer::data ( )
inlinenoexcept

Direct access to the allocated memory or nullptr if no memory is allocated.

Definition at line 162 of file carla/Buffer.h.

References _data.

◆ empty()

bool carla::Buffer::empty ( ) const
inlinenoexcept

Definition at line 193 of file carla/Buffer.h.

References _size.

◆ end() [1/2]

const_iterator carla::Buffer::end ( ) const
inlinenoexcept

Definition at line 233 of file carla/Buffer.h.

References cend().

Referenced by carla::sensor::RawData::end(), carla::sensor::RawData::end(), and carla::operator==().

◆ end() [2/2]

iterator carla::Buffer::end ( )
inlinenoexcept

Definition at line 237 of file carla/Buffer.h.

References begin(), and size().

◆ max_size()

static constexpr size_type carla::Buffer::max_size ( )
inlinestaticconstexprnoexcept

Definition at line 201 of file carla/Buffer.h.

Referenced by Buffer(), Buffer(), and reset().

◆ operator=() [1/2]

Buffer & carla::Buffer::operator= ( Buffer &&  rhs)
inlinenoexcept

Definition at line 128 of file carla/Buffer.h.

References _capacity, _data, _parent_pool, and _size.

◆ operator=() [2/2]

Buffer & carla::Buffer::operator= ( const Buffer &  )
delete

◆ operator[]() [1/2]

value_type & carla::Buffer::operator[] ( size_t  i)
inline

Access the byte at position i.

Definition at line 150 of file carla/Buffer.h.

References _data.

◆ operator[]() [2/2]

const value_type & carla::Buffer::operator[] ( size_t  i) const
inline

Access the byte at position i.

Definition at line 145 of file carla/Buffer.h.

References _data.

◆ pop()

std::unique_ptr< value_type[]> carla::Buffer::pop ( )
inlinenoexcept

Release the contents of this buffer and set its size and capacity to zero.

Definition at line 283 of file carla/Buffer.h.

References _capacity, _data, and _size.

Referenced by clear().

◆ reset() [1/2]

void carla::Buffer::reset ( size_type  size)
inline

Reset the size of this buffer.

If the capacity is not enough, the current memory is discarded and a new block of size size is allocated.

Definition at line 252 of file carla/Buffer.h.

References _capacity, _data, _size, carla::log_debug(), and size().

Referenced by carla::multigpu::IncomingMessage::buffer(), carla::streaming::detail::tcp::IncomingMessage::buffer(), copy_from(), reset(), resize(), and TEST().

◆ reset() [2/2]

void carla::Buffer::reset ( uint64_t  size)
inline

Reset the size of this buffer.

If the capacity is not enough, the current memory is discarded and a new block of size size is allocated.

Definition at line 262 of file carla/Buffer.h.

References max_size(), reset(), size(), and carla::throw_exception().

◆ resize()

void carla::Buffer::resize ( uint64_t  size)
inline

Resize the buffer, a new block of size size is allocated if the capacity is not enough and the data is copied.

Definition at line 271 of file carla/Buffer.h.

References _capacity, _data, _size, copy_from(), data(), reset(), and size().

◆ ReuseThisBuffer()

void carla::Buffer::ReuseThisBuffer ( )
private

Definition at line 7 of file carla/Buffer.cpp.

References _parent_pool.

Referenced by ~Buffer().

◆ size()

size_type carla::Buffer::size ( ) const
inlinenoexcept

Friends And Related Symbol Documentation

◆ BufferPool

friend class BufferPool
friend

Definition at line 363 of file carla/Buffer.h.

◆ BufferView

friend class BufferView
friend

Definition at line 364 of file carla/Buffer.h.

Member Data Documentation

◆ _capacity

size_type carla::Buffer::_capacity = 0u
private

Definition at line 370 of file carla/Buffer.h.

Referenced by capacity(), operator=(), pop(), reset(), resize(), and ~Buffer().

◆ _data

std::unique_ptr<value_type[]> carla::Buffer::_data = nullptr
private

Definition at line 372 of file carla/Buffer.h.

Referenced by begin(), cbegin(), data(), data(), operator=(), operator[](), operator[](), pop(), reset(), and resize().

◆ _parent_pool

std::weak_ptr<BufferPool> carla::Buffer::_parent_pool
private

Definition at line 366 of file carla/Buffer.h.

Referenced by operator=(), carla::BufferPool::Pop(), and ReuseThisBuffer().

◆ _size

size_type carla::Buffer::_size = 0u
private

Definition at line 368 of file carla/Buffer.h.

Referenced by empty(), operator=(), pop(), reset(), resize(), and size().


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