CARLA
Private Member Functions | Private Attributes | Friends | List of all members
carla::Buffer Class Reference

A piece of raw data. More...

#include <Buffer.h>

+ Collaboration diagram for carla::Buffer:

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. More...
 
 Buffer (size_type size)
 Create a buffer with size bytes allocated. More...
 
 Buffer (uint64_t size)
 Create a buffer with size bytes allocated. More...
 
template<typename T >
 Buffer (const T &source)
 Copy source into this buffer. Allocates the necessary memory. More...
 
 Buffer (const value_type *data, size_type size)
 
 Buffer (const value_type *data, uint64_t size)
 Create a buffer with size bytes allocated. More...
 
 Buffer (const Buffer &)=delete
 
 Buffer (Buffer &&rhs) noexcept
 
 ~Buffer ()
 
Assignment
Bufferoperator= (const Buffer &)=delete
 
Bufferoperator= (Buffer &&rhs) noexcept
 
Data access
const value_typeoperator[] (size_t i) const
 Access the byte at position i. More...
 
value_typeoperator[] (size_t i)
 Access the byte at position i. More...
 
const value_typedata () const noexcept
 Direct access to the allocated memory or nullptr if no memory is allocated. More...
 
value_typedata () noexcept
 Direct access to the allocated memory or nullptr if no memory is allocated. More...
 
boost::asio::const_buffer cbuffer () const noexcept
 Make a boost::asio::buffer from this buffer. More...
 
boost::asio::const_buffer buffer () const noexcept
 Make a boost::asio::buffer from this buffer. More...
 
boost::asio::mutable_buffer buffer () noexcept
 Make a boost::asio::buffer from this buffer. More...
 
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. More...
 
void reset (uint64_t size)
 Reset the size of this buffer. More...
 
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. More...
 
std::unique_ptr< value_type[]> pop () noexcept
 Release the contents of this buffer and set its size and capacity to zero. More...
 
void clear () noexcept
 Clear the contents of this buffer and set its size and capacity to zero. More...
 
copy_from
template<typename T >
void copy_from (const T &source)
 Copy source into this buffer. Allocates memory if necessary. More...
 
void copy_from (const value_type *data, size_type size)
 Copy size bytes of the memory pointed by data into this buffer. More...
 
void copy_from (size_type offset, const Buffer &rhs)
 Copy source into this buffer leaving at the front an offset of offset bytes uninitialized. More...
 
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. More...
 
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. More...
 
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. More...
 

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
 

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 39 of file carla/Buffer.h.

Member Typedef Documentation

◆ const_iterator

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

◆ iterator

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

◆ size_type

using carla::Buffer::size_type = uint32_t

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

◆ value_type

using carla::Buffer::value_type = unsigned char

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

Constructor & Destructor Documentation

◆ Buffer() [1/8]

carla::Buffer::Buffer ( )
default

Create an empty buffer.

Referenced by Buffer().

+ Here is the caller graph for this function:

◆ Buffer() [2/8]

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

Create a buffer with size bytes allocated.

Definition at line 68 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 74 of file carla/Buffer.h.

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

+ Here is the call graph for this function:

◆ 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 84 of file carla/Buffer.h.

References copy_from().

+ Here is the call graph for this function:

◆ Buffer() [5/8]

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

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

References copy_from().

+ Here is the call graph for this function:

◆ Buffer() [6/8]

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

Create a buffer with size bytes allocated.

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

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

+ Here is the call graph for this function:

◆ Buffer() [7/8]

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

◆ Buffer() [8/8]

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

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

◆ ~Buffer()

carla::Buffer::~Buffer ( )
inline

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

References _capacity, operator=(), and ReuseThisBuffer().

+ Here is the call graph for this function:

Member Function Documentation

◆ begin() [1/2]

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

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

References cbegin().

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

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ begin() [2/2]

iterator carla::Buffer::begin ( )
inlinenoexcept

Definition at line 222 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 173 of file carla/Buffer.h.

References cbuffer().

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

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ 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 178 of file carla/Buffer.h.

References data(), and size().

+ Here is the call graph for this function:

◆ capacity()

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

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

References _capacity.

◆ cbegin()

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

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

References _data.

Referenced by begin(), and cend().

+ Here is the caller graph for this function:

◆ 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 168 of file carla/Buffer.h.

References data(), and size().

Referenced by buffer().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ cend()

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

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

References cbegin(), and size().

Referenced by end().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ 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 288 of file carla/Buffer.h.

References pop().

+ Here is the call graph for this function:

◆ copy_from() [1/6]

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

Copy source into this buffer. Allocates memory if necessary.

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

Referenced by Buffer(), copy_from(), resize(), carla::sensor::s11n::RadarSerializer::Serialize(), carla::sensor::s11n::LidarSerializer::Serialize(), carla::sensor::s11n::SemanticLidarSerializer::Serialize(), TEST(), FPixelReader::WritePixelsToBuffer(), and WritePixelsToBuffer_Vulkan().

+ Here is the caller graph for this function:

◆ 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 308 of file carla/Buffer.h.

References copy_from().

+ Here is the call graph for this function:

◆ 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 314 of file carla/Buffer.h.

References buffer(), and copy_from().

+ Here is the call graph for this function:

◆ 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 321 of file carla/Buffer.h.

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

+ Here is the call graph for this function:

◆ 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 332 of file carla/Buffer.h.

References copy_from().

+ Here is the call graph for this function:

◆ 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 350 of file carla/Buffer.h.

References copy_from(), and ReuseThisBuffer().

+ Here is the call graph for this function:

◆ data() [1/2]

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

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

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

References _data.

Referenced by util::buffer::as_string(), buffer(), cbuffer(), carla::sensor::s11n::SensorHeaderSerializer::Deserialize(), resize(), TEST(), util::buffer::to_hex_string(), and carla::MsgPack::UnPack().

+ Here is the caller graph for this function:

◆ 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 159 of file carla/Buffer.h.

References _data.

◆ empty()

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

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

References _size.

◆ end() [1/2]

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

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

References cend().

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

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ end() [2/2]

iterator carla::Buffer::end ( )
inlinenoexcept

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

References begin(), and size().

+ Here is the call graph for this function:

◆ max_size()

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

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

Referenced by Buffer(), and reset().

+ Here is the caller graph for this function:

◆ operator=() [1/2]

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

Referenced by ~Buffer().

+ Here is the caller graph for this function:

◆ operator=() [2/2]

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

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

References _capacity, _data, _parent_pool, and _size.

◆ operator[]() [1/2]

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

Access the byte at position i.

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

References _data.

◆ operator[]() [2/2]

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

Access the byte at position i.

Definition at line 147 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 280 of file carla/Buffer.h.

References _capacity, _data, and _size.

Referenced by clear().

+ Here is the caller graph for this function:

◆ 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 249 of file carla/Buffer.h.

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

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

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ 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 259 of file carla/Buffer.h.

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

+ Here is the call graph for this function:

◆ 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 268 of file carla/Buffer.h.

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

+ Here is the call graph for this function:

◆ ReuseThisBuffer()

void carla::Buffer::ReuseThisBuffer ( )
private

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

References _parent_pool.

Referenced by copy_from(), and ~Buffer().

+ Here is the caller graph for this function:

◆ size()

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

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

References _size.

Referenced by util::buffer::as_string(), Buffer(), buffer(), cbuffer(), cend(), copy_from(), end(), make_special_message(), carla::operator<<(), carla::operator==(), reset(), resize(), TEST(), util::buffer::to_hex_string(), and carla::MsgPack::UnPack().

+ Here is the caller graph for this function:

Friends And Related Function Documentation

◆ BufferPool

friend class BufferPool
friend

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

Member Data Documentation

◆ _capacity

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

Definition at line 366 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 368 of file carla/Buffer.h.

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

◆ _parent_pool

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

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

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

◆ _size

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

Definition at line 364 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: