Thrill  0.1
Buffer Class Reference

Detailed Description

Simple buffer of characters without initialization or growing functionality.

We use Buffer instead of std::string for handling untyped byte arrays. The advantage of Buffer is that we have guaranteed direct byte access, and that it does not initialize memory (faster). A Buffer object is also non-copyable, which makes sure that we use zero-copy overhead as much as possible.

This Buffer allows memory using malloc()/realloc()/free().

Definition at line 40 of file buffer.hpp.

#include <buffer.hpp>

Public Types

using const_iterator = const value_type *
 simple pointer iterators More...
 
using const_reference = const value_type &
 simple pointer references More...
 
using iterator = value_type *
 simple pointer iterators More...
 
using reference = value_type &
 simple pointer references More...
 
using size_type = size_t
 size and offset type of buffer More...
 
using value_type = unsigned char
 value type stored in the buffer More...
 

Public Member Functions

Data Access
iterator data () noexcept
 return iterator to beginning of Buffer More...
 
const_iterator data () const noexcept
 return iterator to beginning of Buffer More...
 
size_type size () const noexcept
 return number of items in Buffer More...
 
reference operator[] (size_type i)
 return the i-th position of the Buffer More...
 
const_reference operator[] (size_type i) const
 return constant reference to the i-th position of the Buffer More...
 
Iterator Access
iterator begin () noexcept
 return mutable iterator to first element More...
 
const_iterator begin () const noexcept
 return constant iterator to first element More...
 
const_iterator cbegin () const noexcept
 return constant iterator to first element More...
 
iterator end () noexcept
 return mutable iterator beyond last element More...
 
const_iterator end () const noexcept
 return constant iterator beyond last element More...
 
const_iterator cend () const noexcept
 return constant iterator beyond last element More...
 
Resize and Filling
void Zero ()
 Zero the whole array content. More...
 
void Resize (size_type new_size)
 

Private Member Functions

 Buffer (bool, void *data, size_type size)
 protected constructor used to acquire ownership of a buffer More...
 

Private Attributes

value_typedata_
 the buffer, typed as character data More...
 
size_type size_
 size of the buffer. More...
 

Construction, Moving, Destruction

 Buffer () noexcept
 allocate empty buffer More...
 
 Buffer (size_type n) noexcept
 allocate buffer containing n bytes More...
 
 Buffer (const void *data, size_type size)
 allocate buffer and COPY data into it. More...
 
 Buffer (const Buffer &)=delete
 non-copyable: delete copy-constructor More...
 
Bufferoperator= (const Buffer &)=delete
 non-copyable: delete assignment operator More...
 
 Buffer (Buffer &&other) noexcept
 move-construct other buffer into this one More...
 
Bufferoperator= (Buffer &&other) noexcept
 move-assignment of other buffer into this one More...
 
 ~Buffer ()
 delete buffer More...
 
bool IsValid () const noexcept
 Check for Buffer contents is valid. More...
 
static Buffer Acquire (void *data, size_type size)
 
void swap (Buffer &a, Buffer &b) noexcept
 swap buffer with another one More...
 

Output

std::string ToString () const
 copy contents into std::string More...
 
std::string PartialToString (size_t begin, size_t length) const
 copy part of contents into std::string More...
 
std::ostream & operator<< (std::ostream &os, const Buffer &b)
 make ostream-able More...
 

Member Typedef Documentation

◆ const_iterator

using const_iterator = const value_type *

simple pointer iterators

Definition at line 51 of file buffer.hpp.

◆ const_reference

using const_reference = const value_type&

simple pointer references

Definition at line 55 of file buffer.hpp.

◆ iterator

using iterator = value_type *

simple pointer iterators

Definition at line 49 of file buffer.hpp.

◆ reference

simple pointer references

Definition at line 53 of file buffer.hpp.

◆ size_type

using size_type = size_t

size and offset type of buffer

Definition at line 46 of file buffer.hpp.

◆ value_type

using value_type = unsigned char

value type stored in the buffer

Definition at line 44 of file buffer.hpp.

Constructor & Destructor Documentation

◆ Buffer() [1/6]

Buffer ( bool  ,
void *  data,
size_type  size 
)
inlineprivate

protected constructor used to acquire ownership of a buffer

Definition at line 59 of file buffer.hpp.

◆ Buffer() [2/6]

Buffer ( )
inlinenoexcept

allocate empty buffer

Definition at line 68 of file buffer.hpp.

Referenced by Buffer::Acquire(), and Buffer::Buffer().

◆ Buffer() [3/6]

Buffer ( size_type  n)
inlineexplicitnoexcept

allocate buffer containing n bytes

Definition at line 73 of file buffer.hpp.

◆ Buffer() [4/6]

Buffer ( const void *  data,
size_type  size 
)
inlineexplicit

allocate buffer and COPY data into it.

Definition at line 78 of file buffer.hpp.

References Buffer::Buffer(), Buffer::data(), Buffer::data_, and Buffer::operator=().

◆ Buffer() [5/6]

Buffer ( const Buffer )
delete

non-copyable: delete copy-constructor

◆ Buffer() [6/6]

Buffer ( Buffer &&  other)
inlinenoexcept

move-construct other buffer into this one

Definition at line 90 of file buffer.hpp.

◆ ~Buffer()

~Buffer ( )
inline

delete buffer

Definition at line 114 of file buffer.hpp.

References Buffer::data_, and free().

Member Function Documentation

◆ Acquire()

static Buffer Acquire ( void *  data,
size_type  size 
)
inlinestatic

construct Buffer by acquiring ownership of a memory buffer. The memory buffer will be FREE()ed (not delete[]-ed).

Definition at line 110 of file buffer.hpp.

References Buffer::Buffer().

Referenced by BufferRef::ToBuffer(), and BufferBuilder::ToBuffer().

◆ begin() [1/2]

iterator begin ( )
inlinenoexcept

return mutable iterator to first element

Definition at line 160 of file buffer.hpp.

References Buffer::data_.

Referenced by Buffer::cbegin(), Buffer::operator[](), and Connection::SyncRecv().

◆ begin() [2/2]

const_iterator begin ( ) const
inlinenoexcept

return constant iterator to first element

Definition at line 163 of file buffer.hpp.

References Buffer::data_.

◆ cbegin()

const_iterator cbegin ( ) const
inlinenoexcept

return constant iterator to first element

Definition at line 166 of file buffer.hpp.

References Buffer::begin().

◆ cend()

const_iterator cend ( ) const
inlinenoexcept

return constant iterator beyond last element

Definition at line 176 of file buffer.hpp.

References Buffer::end().

◆ data() [1/2]

◆ data() [2/2]

const_iterator data ( ) const
inlinenoexcept

return iterator to beginning of Buffer

Definition at line 137 of file buffer.hpp.

References Buffer::data_.

◆ end() [1/2]

iterator end ( )
inlinenoexcept

return mutable iterator beyond last element

Definition at line 170 of file buffer.hpp.

References Buffer::data_, and Buffer::size_.

Referenced by Buffer::cend(), and Connection::SyncRecv().

◆ end() [2/2]

const_iterator end ( ) const
inlinenoexcept

return constant iterator beyond last element

Definition at line 173 of file buffer.hpp.

References Buffer::data_, and Buffer::size_.

◆ IsValid()

bool IsValid ( ) const
inlinenoexcept

Check for Buffer contents is valid.

Definition at line 126 of file buffer.hpp.

References Buffer::data_.

◆ operator=() [1/2]

Buffer& operator= ( const Buffer )
delete

non-copyable: delete assignment operator

Referenced by Buffer::Buffer().

◆ operator=() [2/2]

Buffer& operator= ( Buffer &&  other)
inlinenoexcept

move-assignment of other buffer into this one

Definition at line 97 of file buffer.hpp.

References Buffer::data_, free(), and Buffer::size_.

◆ operator[]() [1/2]

reference operator[] ( size_type  i)
inline

return the i-th position of the Buffer

Definition at line 145 of file buffer.hpp.

References Buffer::begin(), and Buffer::size_.

◆ operator[]() [2/2]

const_reference operator[] ( size_type  i) const
inline

return constant reference to the i-th position of the Buffer

Definition at line 150 of file buffer.hpp.

References Buffer::begin(), and Buffer::size_.

◆ PartialToString()

std::string PartialToString ( size_t  begin,
size_t  length 
) const
inline

copy part of contents into std::string

Definition at line 218 of file buffer.hpp.

References Buffer::data_, and Buffer::size_.

◆ Resize()

void Resize ( size_type  new_size)
inline

resize the array to contain exactly new_size items. This should only be used if the Buffer was default constructed containing an empty array. It should NOT be used to resizing it, since this requires it to copy data.

Definition at line 192 of file buffer.hpp.

References Buffer::data_, LOG1, malloc(), realloc(), and Buffer::size_.

◆ size()

◆ ToString()

std::string ToString ( ) const
inline

copy contents into std::string

Definition at line 212 of file buffer.hpp.

References Buffer::data_, and Buffer::size_.

◆ Zero()

void Zero ( )
inline

Zero the whole array content.

Definition at line 185 of file buffer.hpp.

References Buffer::data_, and Buffer::size_.

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const Buffer b 
)
friend

make ostream-able

Definition at line 224 of file buffer.hpp.

◆ swap

void swap ( Buffer a,
Buffer b 
)
friend

swap buffer with another one

Definition at line 119 of file buffer.hpp.

Member Data Documentation

◆ data_

◆ size_


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