qi::Buffer, qi::BufferReader

Reference

qi::Buffer Class Reference

Introduction

Class to store buffer. . More...

#include <qi/buffer.hpp>

Public Functions

Buffer()
Buffer(const Buffer& buffer)
Buffer& operator=(const Buffer& buffer)
Buffer(Buffer&& buffer)
Buffer& operator=(Buffer&& buffer)
bool write(const void* data, size_t size)
size_t addSubBuffer(const Buffer& buffer)
bool hasSubBuffer(size_t offset) const
const Buffer& subBuffer(size_t offset) const
size_t size() const
size_t totalSize() const
const std::vector<std::pair<size_t, Buffer>>& subBuffers() const
void* reserve(size_t size)
void clear()
void* data()
const void* data() const
const void* read(size_t offset, size_t length) const
size_t read(void* buffer, size_t offset, size_t length) const

Detailed Description

This class can store buffer and sub-buffers. Here is a representation of internal management of sub-buffers.

digraph g {
    graph [ rankdir = "LR" ];
    node [ fontsize = "16", shape = "ellipse" ];

    subgraph cluster_buffer {
        mainbuffer;
        label = "Main buffer";
    }

    subgraph cluster_subbuffer1 {
        subbuffer1;
        label = "Sub-buffer 1";
    }

    subgraph cluster_subbuffer2 {
        subbuffer2;
        label = "Sub-buffer 2";
    }

    "mainbuffer" [
        label = "...| ...| ...| ...| ...| ...|<f0> uint32_t subBufferSize| ...| ...|<f1> uint32_t subBufferSize| ...| ...| ..."
        shape = "record"
    ];
    "subbuffer1" [
        label = "<f0> ...| ...|...| ...|  ...|  ...| ...| ...| ...| ..."
        shape = "record"
    ];
    "subbuffer2" [
        label = "<f0> ...| ...|...| ...|  ...|  ...| ...| ...| ...| ..."
        shape = "record"
    ];
    "mainbuffer":f0-> "subbuffer1":f0[
        id = 0
    ];
    "mainbuffer":f1-> "subbuffer2":f0[
        id = 0
    ];
}

Function Documentation

qi::Buffer::Buffer()

Default constructor.

qi::Buffer::Buffer(const Buffer& buffer)

Brief: Copy constructor.

Parameters:
  • buffer – The buffer to copy.

As data are store as a shared pointer, the different copy of the same buffer all handle the same data.

Buffer& qi::Buffer::operator=(const Buffer& buffer)

Brief: Assignment operator. As data are store as a shared pointer, the different copy of the same buffer all handle the same data.

Parameters:
  • buffer – The buffer to copy.
qi::Buffer::Buffer(Buffer&& buffer)

Brief: Move constructor. The moved-from buffer’s state is like a default constructed Buffer.

Parameters:
  • buffer – The buffer to move from.
Buffer& qi::Buffer::operator=(Buffer&& buffer)

Brief: Move assignment operator. The moved-from buffer’s state is like a default constructed Buffer.

Parameters:
  • buffer – The buffer to move from.
bool qi::Buffer::write(const void* data, size_t size)

Brief: Write data in the buffer.

Parameters:
  • data – The data to write
  • size – The size of the data to write
Returns:

true if operation succeeded, false otherwise.

size_t qi::Buffer::addSubBuffer(const Buffer& buffer)

Brief: Add a sub-buffer to the main buffer. This function add a uint32_t for the size of sub-buffers in main buffer and add the buffer to the list of sub-buffers.

Parameters:
  • buffer – The buffer to have as sub-buffer.
Returns:

return te offset at which sub-buffer have been added.

bool qi::Buffer::hasSubBuffer(size_t offset) const

Brief: Check if there is a sub-buffer at given offset.

Parameters:
  • offset – The offset to look at the presence of sub-buffer.
Returns:

true if there is a sub-buffer, false otherwise.

const Buffer& qi::Buffer::subBuffer(size_t offset) const

Brief: Return the sub-buffer at given offset. If there is no sub-buffer throw a std::runtime_error.

Parameters:
  • offset – The offset to look for sub-buffer.
Returns:

the sub-buffer.

size_t qi::Buffer::size() const

Brief: Return the content size of this buffer not counting sub-buffers.

Returns:the size.
size_t qi::Buffer::totalSize() const

Brief: Return the content size of this buffer and of all its sub-buffers.

Returns:the size.
const std::vector<std::pair<size_t, Buffer>>& qi::Buffer::subBuffers() const

Brief: Return a vector of sub-buffers of the current buffer.

Returns:a vector of pairs. The first value of the pair is the offset of the sub-buffer into the master buffer. The second value is the sub-buffer itself.
void* qi::Buffer::reserve(size_t size)

Brief: Reserve bytes at the end of current buffer.

Parameters:
  • size – number of new bytes to reserve at the end of buffer.
Returns:

a pointer to the data.

void qi::Buffer::clear()

Erase content of buffer and remove sub-buffers whithout clearing them.

void* qi::Buffer::data()

Brief: Return a pointer to the raw data storage of this buffer.

Returns:the pointer to the data.
const void* qi::Buffer::data() const

Brief: Return a const pointer to the raw data in this buffer.

Returns:the pointer to the data.
const void* qi::Buffer::read(size_t offset = 0, size_t length = 0) const

Brief: Read some data from the buffer.

Parameters:
  • offset – offset at which reading begin in the buffer.
  • length – length of the data to read.
Returns:

0 if the buffer is empty or if we try to read data after the end of the buffer. Otherwise return a pointer to the data. Only length bytes can be read in the returned buffer.

size_t qi::Buffer::read(void* buffer, size_t offset = 0, size_t length = 0) const

Brief: Read some data in the buffer and store it in a new pre-allocated buffer.

Parameters:
  • buffer – the pre-allocated buffer to store data.
  • offset – Offset in the current buffer to start copy.
  • length – Length of the data to be copied.
Returns:

-1 if there is no data in buffer or if offset is bigger than total data in buffer. Otherwise return the total length of copied data.

qi::BufferReader Class Reference

Introduction

Class to read const buffer. This class is intendeed to read buffer. It store an internal data cursor and an internal sub-buffer index. All offset are relative to the current position. More...

#include <qi/buffer.hpp>

Public Functions

BufferReader(const Buffer& buf)
~BufferReader()
size_t read(void* data, size_t length)
void* read(size_t offset)
bool seek(size_t offset)
void* peek(size_t offset) const
bool hasSubBuffer() const
const Buffer& subBuffer()
size_t position() const

Detailed Description

Class to read const buffer. This class is intendeed to read buffer. It store an internal data cursor and an internal sub-buffer index. All offset are relative to the current position.

Function Documentation

qi::BufferReader::BufferReader(const Buffer& buf)

Brief: Constructor.

Parameters:
  • buf – The buffer to copy.
qi::BufferReader::~BufferReader()

Default destructor.

size_t qi::BufferReader::read(void* data, size_t length)

Brief: read and store data from the buffer.

Parameters:
  • data – A pre-allocated pointer to store read data.
  • length – Size of the object pointed by data or size to read.
Returns:

size of really read and stored data.

void* qi::BufferReader::read(size_t offset)

Brief: read data from buffer.

Parameters:
  • offset – Number of bytes to read.
Returns:

a pointer to data at the given

bool qi::BufferReader::seek(size_t offset)

Brief: Move forward the buffer cursor by the given offset.

Parameters:
  • offset – Value for move forward the cursor.
Returns:

Return true if succeed, false otherwise.

void* qi::BufferReader::peek(size_t offset) const

Brief: Check if we can read from the actual position toward offset bytes.

Parameters:
  • offset – The relative offset.
Returns:

The pointer if it succeed. If actual position + offset exceed size of buffer return 0.

bool qi::BufferReader::hasSubBuffer() const

Brief: Check if there is sub-buffer at the actual position.

Returns:true if there is sub-buffer, false otherwise.
const Buffer& qi::BufferReader::subBuffer()

Brief: return the sub-buffer at the actual position. If there is no sub-buffer at actual position throw a std::runtime-error.

Returns:Return the sub-buffer if any.
size_t qi::BufferReader::position() const

Brief: Return the actual position in the buffer.

Returns:The current offset.