Skip to content

sgns::storage::Buffer

Class represents arbitrary (including empty) byte buffer.

#include <buffer.hpp>

Inherits from boost::equality_comparable< Buffer >, boost::equality_comparable< gsl::span< uint8_t > >, boost::equality_comparable< std::vector< uint8_t > >

Inherited by sgns::storage::trie::KeyNibbles

Public Types

Name
using std::vector< uint8_t >::iterator iterator
using std::vector< uint8_t >::reverse_iterator reverse_iterator
using std::vector< uint8_t >::const_reverse_iterator const_reverse_iterator
using std::vector< uint8_t >::const_iterator const_iterator
using uint8_t value_type
using typename std::vector< uint8_t >::pointer pointer
using typename std::vector< uint8_t >::const_pointer const_pointer

Public Functions

Name
Buffer(size_t size, uint8_t byte)
Allocates a buffer of the given size, filled with a byte value.
Buffer(std::vector< uint8_t > v)
lvalue construct buffer from a byte vector
Buffer(gsl::span< const uint8_t > s)
Buffer(const uint8_t * begin, const uint8_t * end)
Buffer() =default
Buffer(const Buffer & b) =default
Buffer(Buffer && b) =default
Buffer(std::initializer_list< uint8_t > b)
~Buffer() =default
Buffer & reserve(size_t size)
Buffer & resize(size_t size)
Buffer & operator=(const Buffer & other) =default
Buffer & operator=(Buffer && other) =default
Buffer & operator+=(const Buffer & other)
uint8_t operator[](size_t index) const
Accessor of byte elements given an index in the byte array.
uint8_t & operator[](size_t index)
Accessor of byte elements given an index in the byte array.
bool operator==(const Buffer & b) const
Lexicographical comparison of two buffers.
bool operator==(const std::vector< uint8_t > & b) const
Lexicographical comparison of buffer and vector of bytes.
bool operator==(gsl::span< const uint8_t > s) const
Lexicographical comparison of buffer and vector of bytes.
bool operator<(const Buffer & b) const
Lexicographical comparison of two buffers.
iterator begin()
Iterator, which points to begin of this buffer.
const_iterator begin() const
Iterator, which points to begin of this buffer.
iterator end()
Iterator, which points to the element next to the last in this buffer.
const_iterator end() const
Iterator, which points to the element next to the last in this buffer.
reverse_iterator rbegin()
Iterator, which points to last of this buffer.
const_reverse_iterator rbegin() const
Iterator, which points to last of this buffer.
reverse_iterator rend()
Iterator, which points to the element previous to first in this buffer.
const_reverse_iterator rend() const
Iterator, which points to the element previous to first in this buffer.
size_t size() const
Getter for size of this buffer.
Buffer & putUint8(uint8_t n)
Put an 8-bit value into this buffer.
Buffer & putUint32(uint32_t n)
Put a 32-bit value into this buffer.
Buffer & putUint64(uint64_t n)
Put a 64-bit value into this buffer.
Buffer & put(std::string_view str)
Put a string into the byte buffer.
Buffer & put(const std::vector< uint8_t > & v)
Put a vector of bytes into the byte buffer.
Buffer & put(gsl::span< const uint8_t > s)
Put a sequence of bytes into the byte buffer.
Buffer & putBytes(const uint8_t * begin, const uint8_t * end)
Put an array of bytes bounded by pointers into the byte buffer.
Buffer & putBuffer(const Buffer & buf)
Put another buffer content at the end of current one.
void clear()
const uint8_t * data() const
getter for raw array of bytes
uint8_t * data()
const std::vector< uint8_t > & toVector() const
getter for vector of bytes
std::vector< uint8_t > & toVector()
Buffer subbuffer(size_t offset =0, size_t length =-1) const
std::string toHex() const
encode bytearray as hex
bool empty() const
std::string_view toString() const
return content of bytearray as string
outcome::result< Buffer > fromHex(std::string_view hex)
Construct Buffer from hex string.

Public Types Documentation

using iterator

using sgns::base::Buffer::iterator = std::vector<uint8_t>::iterator;

using reverse_iterator

using sgns::base::Buffer::reverse_iterator = std::vector<uint8_t>::reverse_iterator;

using const_reverse_iterator

using sgns::base::Buffer::const_reverse_iterator = std::vector<uint8_t>::const_reverse_iterator;

using const_iterator

using sgns::base::Buffer::const_iterator = std::vector<uint8_t>::const_iterator;

using value_type

using sgns::base::Buffer::value_type = uint8_t;

using pointer

using sgns::base::Buffer::pointer = typename std::vector<uint8_t>::pointer;

using const_pointer

using sgns::base::Buffer::const_pointer = typename std::vector<uint8_t>::const_pointer;

Public Functions Documentation

function Buffer

Buffer(
    size_t size,
    uint8_t byte
)

Allocates a buffer of the given size, filled with a byte value.

Parameters:

  • size Buffer size in bytes.
  • byte Fill value for all bytes.

function Buffer

explicit Buffer(
    std::vector< uint8_t > v
)

lvalue construct buffer from a byte vector

function Buffer

explicit Buffer(
    gsl::span< const uint8_t > s
)

function Buffer

Buffer(
    const uint8_t * begin,
    const uint8_t * end
)

function Buffer

Buffer() =default

function Buffer

Buffer(
    const Buffer & b
) =default

function Buffer

Buffer(
    Buffer && b
) =default

function Buffer

Buffer(
    std::initializer_list< uint8_t > b
)

function ~Buffer

~Buffer() =default

function reserve

Buffer & reserve(
    size_t size
)

function resize

Buffer & resize(
    size_t size
)

function operator=

Buffer & operator=(
    const Buffer & other
) =default

function operator=

Buffer & operator=(
    Buffer && other
) =default

function operator+=

Buffer & operator+=(
    const Buffer & other
)

function operator[]

uint8_t operator[](
    size_t index
) const

Accessor of byte elements given an index in the byte array.

Parameters:

  • index Element index.

function operator[]

uint8_t & operator[](
    size_t index
)

Accessor of byte elements given an index in the byte array.

Parameters:

  • index Element index.

function operator==

bool operator==(
    const Buffer & b
) const

Lexicographical comparison of two buffers.

function operator==

bool operator==(
    const std::vector< uint8_t > & b
) const

Lexicographical comparison of buffer and vector of bytes.

function operator==

bool operator==(
    gsl::span< const uint8_t > s
) const

Lexicographical comparison of buffer and vector of bytes.

function operator<

bool operator<(
    const Buffer & b
) const

Lexicographical comparison of two buffers.

function begin

iterator begin()

Iterator, which points to begin of this buffer.

function begin

const_iterator begin() const

Iterator, which points to begin of this buffer.

function end

iterator end()

Iterator, which points to the element next to the last in this buffer.

function end

const_iterator end() const

Iterator, which points to the element next to the last in this buffer.

function rbegin

reverse_iterator rbegin()

Iterator, which points to last of this buffer.

function rbegin

const_reverse_iterator rbegin() const

Iterator, which points to last of this buffer.

function rend

reverse_iterator rend()

Iterator, which points to the element previous to first in this buffer.

function rend

const_reverse_iterator rend() const

Iterator, which points to the element previous to first in this buffer.

function size

size_t size() const

Getter for size of this buffer.

function putUint8

Buffer & putUint8(
    uint8_t n
)

Put an 8-bit value into this buffer.

Parameters:

  • n Value to append.

Return: This buffer, suitable for chaining.

function putUint32

Buffer & putUint32(
    uint32_t n
)

Put a 32-bit value into this buffer.

Parameters:

  • n Value to append (serialized as big-endian).

Return: This buffer, suitable for chaining.

function putUint64

Buffer & putUint64(
    uint64_t n
)

Put a 64-bit value into this buffer.

Parameters:

  • n Value to append (serialized as big-endian).

Return: This buffer, suitable for chaining.

function put

Buffer & put(
    std::string_view str
)

Put a string into the byte buffer.

Parameters:

  • str Arbitrary string.

Return: This buffer, suitable for chaining.

function put

Buffer & put(
    const std::vector< uint8_t > & v
)

Put a vector of bytes into the byte buffer.

Parameters:

  • v Arbitrary vector of bytes.

Return: This buffer, suitable for chaining.

function put

Buffer & put(
    gsl::span< const uint8_t > s
)

Put a sequence of bytes into the byte buffer.

Parameters:

  • s Arbitrary span of bytes.

Return: This buffer, suitable for chaining.

function putBytes

Buffer & putBytes(
    const uint8_t * begin,
    const uint8_t * end
)

Put an array of bytes bounded by pointers into the byte buffer.

Parameters:

  • begin Pointer to the array start.
  • end Pointer to the address after the last element.

Return: This buffer, suitable for chaining.

function putBuffer

Buffer & putBuffer(
    const Buffer & buf
)

Put another buffer content at the end of current one.

Parameters:

  • buf another buffer

Return: this buffer suitable for chaining.

function clear

void clear()

Clear the contents of the Buffer

function data

const uint8_t * data() const

getter for raw array of bytes

function data

uint8_t * data()

function toVector

const std::vector< uint8_t > & toVector() const

getter for vector of bytes

function toVector

std::vector< uint8_t > & toVector()

function subbuffer

Buffer subbuffer(
    size_t offset =0,
    size_t length =-1
) const

Returns a copy of a part of the buffer Works alike subspan() of gsl::span

function toHex

std::string toHex() const

encode bytearray as hex

Return: hex-encoded string

function empty

bool empty() const

Return: true, if buffer is empty, false otherwise

Check if this buffer is empty

function toString

std::string_view toString() const

return content of bytearray as string

Return: string

Note: Does not ensure correct encoding

function fromHex

static outcome::result< Buffer > fromHex(
    std::string_view hex
)

Construct Buffer from hex string.

Parameters:

  • hex hex-encoded string

Return: result containing constructed buffer if input string is hex-encoded string.


Updated on 2026-03-04 at 13:10:44 -0800