StormByte 2.0.0
C++26 foundation of the StormByte suite
 
Loading...
Searching...
No Matches
StormByte::Detail::Codec< T > Struct Template Reference

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

#include <StormByte/serializable.hxx>

Static Public Member Functions

static ByteSize Size (const T &data) noexcept
 Serialized size of data.
 
static BinaryData Write (const T &data) noexcept
 Encodes data to a little-endian blob.
 
static Expected< T, DeserializeError > Read (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 ByteSize Size(const MyType& v) noexcept;
static BinaryData Write(const MyType& v) noexcept;
static Expected<MyType, DeserializeError> Read(std::span<const std::byte>) noexcept;
};
DLL-boundary-safe owned contiguous sequence of std::byte.
Definition binary_data.hxx:106
Count of octets (uint64_t storage).
Definition byte_size.hxx:103
Count of abstract units (uint64_t storage).
Definition size.hxx:100
std::conditional_t< Type::Reference< T >, std::expected< std::reference_wrapper< std::remove_reference_t< T > >, Safe::Shared< E > >, std::expected< T, Safe::Shared< E > > > Expected
std::expected alias with reference and shared-error handling.
Definition expected.hxx:66
Size / write / read for one type T.
Definition serializable.hxx:107
static BinaryData Write(const T &data) noexcept
Encodes data to a little-endian blob.
Definition serializable.hxx:125
static Expected< T, DeserializeError > Read(std::span< const std::byte > data) noexcept
Decodes T from the start of data.
Definition serializable.hxx:137
Note
The three members must agree with each other. Size(v) must equal Write(v).size() as a byte count. 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 StormByte::DeserializeError.

◆ Size()

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

Serialized size of data.

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

◆ Write()

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

Encodes data to a little-endian blob.

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

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