StormByte 3.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 Safe::Binary 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 Safe::Binary Write(const MyType& v) noexcept;
static Expected<MyType, DeserializeError> Read(std::span<const std::byte>) noexcept;
};
Count of octets (uint64_t storage).
Definition byte_size.hxx:105
Owned contiguous sequence of std::byte, stored in a Vector.
Definition binary.hxx:79
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:62
Size / write / read for one type T.
Definition serializable.hxx:108
static ByteSize Size(const T &data) noexcept
Serialized size of data.
Definition serializable.hxx:114
static Safe::Binary Write(const T &data) noexcept
Encodes data to a little-endian blob.
Definition serializable.hxx:126
static Expected< T, DeserializeError > Read(std::span< const std::byte > data) noexcept
Decodes T from the start of data.
Definition serializable.hxx:138
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>
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>
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>
Safe::Binary 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: