StormByte C++ Library 1.2.0
StormByte is a comprehensive, cross-platform C++ library aimed at easing system programming, configuration management, logging, and database handling tasks. This library provides a unified API that abstracts away the complexities and inconsistencies of different platforms (Windows, Linux).
Loading...
Searching...
No Matches
Static Public Member Functions | List of all members
StormByte::Detail::Codec< T > Struct Template Reference

Size / write / read for one type T. More...

#include <StormByte/serializable.hxx>

Static Public Member Functions

static std::size_t Size (const T &data) noexcept
 Serialized size of data.
 
static std::vector< std::byte > Write (const T &data) noexcept
 Encodes data to a little-endian blob.
 
static Expected< T, DeserializeErrorRead (std::span< const std::byte > data) noexcept
 Decodes T from the start of data.
 

Detailed Description

template<typename T>
struct StormByte::Detail::Codec< T >

Size / write / read for one type T.

Template Parameters
TType being encoded.

Primary template is intentionally unusable. Specialize the whole struct (all three members) for a type that is not optional, pair, container or trivially copyable.

template<>
struct StormByte::Detail::Codec<MyType> {
static std::size_t Size(const MyType& v) noexcept;
static std::vector<std::byte> Write(const MyType& v) noexcept;
static Expected<MyType, DeserializeError> Read(std::span<const std::byte>) noexcept;
};
std::conditional_t< Type::Reference< T >, std::expected< std::reference_wrapper< std::remove_reference_t< T > >, std::shared_ptr< E > >, std::expected< T, std::shared_ptr< E > > > Expected
std::expected alias with reference and shared-error handling.
Definition expected.hxx:45
Size / write / read for one type T.
Definition serializable.hxx:96
static std::size_t Size(const T &data) noexcept
Serialized size of data.
Definition serializable.hxx:102
static std::vector< std::byte > Write(const T &data) noexcept
Encodes data to a little-endian blob.
Definition serializable.hxx:114
static Expected< T, DeserializeError > Read(std::span< const std::byte > data) noexcept
Decodes T from the start of data.
Definition serializable.hxx:126
Note
The three members must agree with each other. Size(v) must equal Write(v).size(). Read consumes a prefix of that length; leftover bytes belong to the caller.

Member Function Documentation

◆ Read()

template<typename T >
static Expected< T, DeserializeError > StormByte::Detail::Codec< T >::Read ( std::span< const std::byte >  data)
inlinestaticnoexcept

Decodes T from the start of data.

Parameters
[in]dataInput span; may be longer than one value.
Returns
Value, or DeserializeError.

◆ Size()

template<typename T >
static std::size_t StormByte::Detail::Codec< T >::Size ( const T &  data)
inlinestaticnoexcept

Serialized size of data.

Parameters
[in]dataValue to measure.
Returns
Size in bytes.

◆ Write()

template<typename T >
static std::vector< std::byte > StormByte::Detail::Codec< T >::Write ( const T &  data)
inlinestaticnoexcept

Encodes data to a little-endian blob.

Parameters
[in]dataValue to encode.
Returns
Blob. No framing beyond what T itself needs.

The documentation for this struct was generated from the following file: