Skip to content

sgns::base

Namespaces

Name
sgns::base::detail

Classes

Name
class sgns::base::Blob
class sgns::base::Buffer
Class represents arbitrary (including empty) byte buffer.
struct sgns::base::Wrapper
Make strongly typed structures from different concepts of the equal types. E.g. block height and round number are both uint64_t, but better to be different types. Or, ID and Signature both vectors.

Types

Name
enum class BlobError
enum class UnhexError { NOT_ENOUGH_INPUT = 1, NON_HEX_INPUT, VALUE_OUT_OF_RANGE, MISSING_0X_PREFIX, UNKNOWN}
error codes for exceptions that may occur during unhexing
using Blob< 8 > Hash64
using Blob< 16 > Hash128
using Blob< 32 > Hash256
using Blob< 64 > Hash512
using std::shared_ptr< spdlog::logger > Logger

Functions

Name
template <class Stream ,size_t size,typename =std::enable_if_t>
Stream &
operator<<(Stream & s, const Blob< size > & blob)
scale-encodes blob instance to stream
template <class Stream ,size_t size,typename =std::enable_if_t>
Stream &
operator>>(Stream & s, Blob< size > & blob)
decodes blob instance from stream
template <size_t N>
std::ostream &
operator<<(std::ostream & os, const Blob< N > & blob)
std::ostream & operator<<(std::ostream & os, const Buffer & buffer)
template <class Stream ,typename =std::enable_if_t>
Stream &
operator<<(Stream & s, const Buffer & buffer)
override operator<< for all streams except std::ostream
template <class Stream ,typename =std::enable_if_t>
Stream &
operator>>(Stream & s, Buffer & buffer)
decodes buffer object from stream
std::string hex_upper(gsl::span< const uint8_t > bytes)
Converts bytes to uppercase hex representation.
std::string hex_lower(gsl::span< const uint8_t > bytes)
Converts bytes to hex representation.
outcome::result< std::vector< uint8_t > > unhex(std::string_view hex)
Converts hex representation to bytes.
outcome::result< std::vector< uint8_t > > unhexWith0x(std::string_view hex)
Unhex hex-string with 0x in the begining.
Logger createLogger(const std::string & tag, const std::string & basepath ="")
Create a logger instance.
std::array< uint8_t, 8 > uint64_t_to_bytes(uint64_t number)
uint64_t bytes_to_uint64_t(gsl::span< uint8_t, 8 > bytes)
std::array< uint8_t, 16 > uint128_t_to_bytes(const boost::multiprecision::uint128_t & i)
boost::multiprecision::uint128_t bytes_to_uint128_t(gsl::span< uint8_t, 16 > bytes)
std::array< uint8_t, 32 > uint256_t_to_bytes(const boost::multiprecision::uint256_t & i)
boost::multiprecision::uint256_t bytes_to_uint256_t(gsl::span< uint8_t, 32 > bytes)
template <typename T ,typename =std::enable_if_t>>
void
raise(T t)
throws outcome::result error as boost exception
template <typename T ,typename =std::enable_if_t<!std::is_enum_v>>
void
raise(const T & t)
throws outcome::result error made of error as boost exception
template <typename T ,typename Tag ,typename =std::enable_if_t>>
bool
operator<(const Wrapper< T, Tag > & a, const Wrapper< T, Tag > & b)

Types Documentation

enum BlobError

Enumerator Value Description
INCORRECT_LENGTH 1

Error codes for exceptions that may occur during blob initialization

enum UnhexError

Enumerator Value Description
NOT_ENOUGH_INPUT 1
NON_HEX_INPUT
VALUE_OUT_OF_RANGE
MISSING_0X_PREFIX
UNKNOWN

error codes for exceptions that may occur during unhexing

using Hash64

using sgns::base::Hash64 = Blob<8>;

using Hash128

using sgns::base::Hash128 = Blob<16>;

using Hash256

using sgns::base::Hash256 = Blob<32>;

using Hash512

using sgns::base::Hash512 = Blob<64>;

using Logger

using sgns::base::Logger = std::shared_ptr<spdlog::logger>;

Functions Documentation

function operator<<

template <class Stream ,
size_t size,
typename  =std::enable_if_t<Stream::is_encoder_stream>>
Stream & operator<<(
    Stream & s,
    const Blob< size > & blob
)

scale-encodes blob instance to stream

Parameters:

  • s output stream reference
  • blob value to encode

Template Parameters:

  • Stream output stream type
  • size blob size

Return: reference to stream

function operator>>

template <class Stream ,
size_t size,
typename  =std::enable_if_t<Stream::is_decoder_stream>>
Stream & operator>>(
    Stream & s,
    Blob< size > & blob
)

decodes blob instance from stream

Parameters:

  • s input stream reference
  • blob value to encode

Template Parameters:

  • Stream output stream type
  • size blob size

Return: reference to stream

function operator<<

template <size_t N>
inline std::ostream & operator<<(
    std::ostream & os,
    const Blob< N > & blob
)

function operator<<

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

function operator<<

template <class Stream ,
typename  =std::enable_if_t<Stream::is_encoder_stream>>
Stream & operator<<(
    Stream & s,
    const Buffer & buffer
)

override operator<< for all streams except std::ostream

Parameters:

  • s stream reference
  • buffer value to encode

Template Parameters:

  • Stream stream type

Return: reference to stream

function operator>>

template <class Stream ,
typename  =std::enable_if_t<Stream::is_decoder_stream>>
Stream & operator>>(
    Stream & s,
    Buffer & buffer
)

decodes buffer object from stream

Parameters:

  • s stream reference
  • buffer value to decode

Template Parameters:

  • Stream input stream type

Return: reference to stream

function hex_upper

std::string hex_upper(
    gsl::span< const uint8_t > bytes
)

Converts bytes to uppercase hex representation.

Parameters:

  • bytes input bytes

Return: hexstring

function hex_lower

std::string hex_lower(
    gsl::span< const uint8_t > bytes
)

Converts bytes to hex representation.

Parameters:

  • bytes input bytes

Return: hexstring

function unhex

outcome::result< std::vector< uint8_t > > unhex(
    std::string_view hex
)

Converts hex representation to bytes.

Parameters:

  • hex hex string input

See: https://www.boost.org/doc/libs/1_51_0/libs/algorithm/doc/html/the_boost_algorithm_library/Misc/hex.html

Return: result containing array of bytes if input string is hex encoded and has even length

Note: reads both uppercase and lowercase hexstrings

function unhexWith0x

outcome::result< std::vector< uint8_t > > unhexWith0x(
    std::string_view hex
)

Unhex hex-string with 0x in the begining.

Parameters:

  • hex hex string with 0x in the beginning

Return: unhexed buffer

function createLogger

Logger createLogger(
    const std::string & tag,
    const std::string & basepath =""
)

Create a logger instance.

Parameters:

  • tag Tagging name for identifying logger.
  • basepath Optional base path for log output (platform dependent).

Return: Logger object.

function uint64_t_to_bytes

std::array< uint8_t, 8 > uint64_t_to_bytes(
    uint64_t number
)

function bytes_to_uint64_t

uint64_t bytes_to_uint64_t(
    gsl::span< uint8_t, 8 > bytes
)

function uint128_t_to_bytes

std::array< uint8_t, 16 > uint128_t_to_bytes(
    const boost::multiprecision::uint128_t & i
)

function bytes_to_uint128_t

boost::multiprecision::uint128_t bytes_to_uint128_t(
    gsl::span< uint8_t, 16 > bytes
)

function uint256_t_to_bytes

std::array< uint8_t, 32 > uint256_t_to_bytes(
    const boost::multiprecision::uint256_t & i
)

function bytes_to_uint256_t

boost::multiprecision::uint256_t bytes_to_uint256_t(
    gsl::span< uint8_t, 32 > bytes
)

function raise

template <typename T ,
typename  =std::enable_if_t<std::is_enum_v<T>>>
void raise(
    T t
)

throws outcome::result error as boost exception

Parameters:

  • t error value

Template Parameters:

  • T enum error type, only outcome::result enums are allowed

function raise

template <typename T ,
typename  =std::enable_if_t<!std::is_enum_v<T>>>
void raise(
    const T & t
)

throws outcome::result error made of error as boost exception

Parameters:

  • t outcome error value

Template Parameters:

  • T outcome error type

function operator<

template <typename T ,
typename Tag ,
typename  =std::enable_if_t<std::is_arithmetic_v<T>>>
bool operator<(
    const Wrapper< T, Tag > & a,
    const Wrapper< T, Tag > & b
)

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