class ugcs::vsm::Io_buffer

Overview

Generic I/O buffer. More…

#include <io_buffer.h>

class Io_buffer: public std::enable_shared_from_this< Io_buffer >
{
public:
    // typedefs

    typedef std::shared_ptr<Io_buffer> Ptr;
    typedef std::weak_ptr<Io_buffer> Weak_ptr;

    // fields

    static const size_t END;

    // construction

    Io_buffer(const Io_buffer& buf, size_t offset = 0, size_t len = END);
    Io_buffer(Io_buffer&& buf);

    Io_buffer(
        std::shared_ptr<const std::vector<uint8_t>>&& data_vec,
        size_t offset = 0,
        size_t len = END
        );

    Io_buffer(std::vector<uint8_t>&& data_vec, size_t offset = 0, size_t len = END);
    Io_buffer();
    Io_buffer(const std::string& data_str);
    Io_buffer(const void* data, size_t len);

    // methods

    template <typename... Args>
    static Ptr Create(Args&&... args);

    Ptr Concatenate(Io_buffer::Ptr buf);
    Ptr Slice(size_t offset, size_t len = END) const;
    size_t Get_length() const;
    const void* Get_data() const;
    std::string Get_string() const;
    std::string Get_ascii() const;
    std::string Get_hex() const;
};

Detailed Documentation

Generic I/O buffer.

Used for all I/O operations with network, filesystem, peripheral devices. Should always be passed by Io_buffer::Ptr smart pointer type. This allows anyone to hold reference to the buffer until he needs to access data and release the buffer when it last reference released.

Important: the buffer object is immutable - once it is created its data cannot be modified. It is required not confuse other reference holders. If modified buffer is required it can be easily created based on existing one via different constructors, operators and methods. Create() method should be used for obtaining Io_buffer instance.

Typedefs

typedef std::shared_ptr<Io_buffer> Ptr

Pointer type.

typedef std::weak_ptr<Io_buffer> Weak_ptr

Pointer type.

Fields

static const size_t END

Special value which references data end.

Construction

Io_buffer(const Io_buffer& buf, size_t offset = 0, size_t len = END)

Copy constructor.

Parameters:

buf

Buffer to copy from.

offset

Offset in bytes to reference data in the source buffer.

len

Length in bytes of the referenced data. Value END indicates that all remaining data are taken.

Invalid_param_exception

if the specified offset or length exceeds the source buffer boundary.

Io_buffer(Io_buffer&& buf)

Move constructor.

Io_buffer(
    std::shared_ptr<const std::vector<uint8_t>>&& data_vec,
    size_t offset = 0,
    size_t len = END
    )

Construct from data provided in vector.

Typical usage is for data which are firstly received from some I/O call.

Parameters:

data_vec

Vector with data. Should be dynamically allocated by “std::make_shared()”. Keep in mind that it can be passed somewhere else only by shared pointer.

offset

Offset in data vector where this buffer data start from.

len

Length of the data referenced in the provided vector. Offset and length should not exceed vector boundary. Value END indicates that all remaining data in vector should be used.

Invalid_param_exception

if the specified offset or length exceeds the source buffer boundary.

Io_buffer(std::vector<uint8_t>&& data_vec, size_t offset = 0, size_t len = END)

Construct from data provided in vector.

See constructor which accepts “std::shared_ptr<const std::vector<uint8_t>> &&” for more details.

Io_buffer()

Construct empty buffer.

Io_buffer(const std::string& data_str)

Construct buffer from string.

Null terminator is not included in the data. Data are copied.

Io_buffer(const void* data, size_t len)

Construct buffer from provided bytes array.

Data are copied.

Parameters:

data

Bytes array with data.

len

Number of bytes to take from data argument.

Methods

template <typename... Args>
static Ptr Create(Args&&... args)

Create an instance.

Ptr Concatenate(Io_buffer::Ptr buf)

Concatenate this buffer data with another buffer data and return new buffer object which contains resulted data.

Note, that method complexity is linear if lengths of both buffers are not zero.

Parameters:

buf

Buffer to concatenate with.

Returns:

New buffer with data from this buffer and the specified one.

Ptr Slice(size_t offset, size_t len = END) const

Take slice from buffer data.

Parameters:

offset

Offset to start slice from.

len

Length of the slice data. Value END indicates that all remaining data should be taken.

Invalid_param_exception

if the specified offset or length exceeds the source buffer boundary.

Returns:

New buffer with sliced data.

size_t Get_length() const

Get length of the contained data.

const void* Get_data() const

Get pointer to raw data stored in the buffer.

Parameters:

Invalid_op_exception

if the buffer is empty.

std::string Get_string() const

Get buffer content as string.

std::string Get_ascii() const

Get buffer data as string of characters.

Non-printable chars are substituted with ‘.’

std::string Get_hex() const

Get buffer data as hex string.