Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Buffer Class Reference

#include <elm/util/Buffer.h>

Public Member Functions

 Buffer (t::size size=default_size)
 
 Buffer (char *buf, t::size size)
 
 ~Buffer ()
 
t::size size () const
 
char operator[] (t::size i) const
 
char & operator[] (t::size i)
 
void copyTo (void *p, t::size size, t::size offset=0) const
 
void copyFrom (void *p, t::size size, t::size offset=0)
 
template<class F >
void read (F f, t::size size, t::size offset=0) const
 
template<class F >
void write (F f, t::size size, t::size offset=0)
 

Static Public Attributes

static const t::size default_size = 4096
 

Detailed Description

The intent of this class is to provide a simple way to prevent errors like buffer-overful by controlling precisely its accesses.

There are different to use a buffer:

  • byte access using array notation [...],
  • range access by copying ranges of bytes,
  • controlled read or write using an external function.

In case of an invalid access to the buffer, the exception @BufferException is thrown.

Constructor & Destructor Documentation

◆ Buffer() [1/2]

Buffer ( t::size  size = default_size)
inline

Build and allocate a buffer of the given size. The bytes are automatically allocated and released.

Parameters
sizeBuffer size (default to default_size).

◆ Buffer() [2/2]

Buffer ( char *  buf,
t::size  size 
)
inline

Build buffer around the given bytes.

Parameters
bufBytes to use.
sizeSize of the given byte buffer.

◆ ~Buffer()

~Buffer ( )
inline

Member Function Documentation

◆ copyFrom()

void copyFrom ( void p,
t::size  size,
t::size  offset = 0 
)
inline

Copy a range of bytes into the buffer.

Parameters
pPointer to bytes to copy into the buffer.
sizeSize of range to copy.
offsetOffset in the buffer to copy to (default to 0).

References elm::array::copy(), elm::io::p(), and Buffer::size().

◆ copyTo()

void copyTo ( void p,
t::size  size,
t::size  offset = 0 
) const
inline

Copy a range of bytes from the buffer to the given memory.

Parameters
pPointer to memory to copy to.
sizeSize of range to copy.
offsetOffset in the buffer of bytes to copy (default to 0).

References elm::array::copy(), elm::io::p(), and Buffer::size().

◆ operator[]() [1/2]

char& operator[] ( t::size  i)
inline

◆ operator[]() [2/2]

char operator[] ( t::size  i) const
inline

◆ read()

void read ( f,
t::size  size,
t::size  offset = 0 
) const
inline

Read bytes from the buffer in a controlled way using the given function. This supposes that the function f is not buggy.

Parameters
fFunction used to read from the buffer. Must be of type void(const char *, t::size).
sizeSize of bytes to read.
offsetOffset in the buffer to read from (default to 0).

References Buffer::size().

◆ size()

t::size size ( ) const
inline

Get the size of the buffer.

Returns
Buffer size.

Referenced by Buffer::copyFrom(), Buffer::copyTo(), Buffer::read(), and Buffer::write().

◆ write()

void write ( f,
t::size  size,
t::size  offset = 0 
)
inline

Write bytes in the buffer using a function in a controlled way. This supposes that the function f is not buggy.

Parameters
fFunction used to write to the buffer. Must be of type void(char *, t::size).
sizeSize of bytes to write.
offsetOffset in the buffer to write to (default to 0).

References Buffer::size().

Member Data Documentation

◆ default_size

const t::size default_size = 4096
static

Default buffer size.


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