SofaBuffers C++ 0.12.0
Pure-C++20 SofaBuffers — API Documentation
Loading...
Searching...
No Matches
sofab Namespace Reference

Namespaces

namespace  detail
 Implementation details of the wire format; not part of the public API.
 

Classes

struct  BlobSeq
 The blob counterpart of StringSeq; see it for the placement and bound rules. More...
 
struct  ElemBound
 The value range a declared integer type admits (MESSAGE_SPEC §7.1). More...
 
class  FixedBytes
 Fixed-capacity, heap-free byte blob of up to N bytes. More...
 
class  FixedString
 Fixed-capacity, heap-free string of up to N characters. More...
 
class  InlineVector
 Fixed-capacity, heap-free sequence of up to N elements of type T. More...
 
class  IStreamImpl
 Base of the input streams: decodes fields from fed bytes. More...
 
class  IStreamInline
 Input stream that delivers each top-level field to a user callback. More...
 
class  IStreamInto
 Decoder that decodes into a message instance the CALLER owns. More...
 
class  IStreamMessage
 Base class for user-defined messages that can deserialise themselves. More...
 
class  IStreamObject
 Holds a message and routes decoded top-level fields into it. More...
 
struct  Limits
 The one stream-scoped receiver cap: how many bytes a single top-level field may span. More...
 
struct  Message
 A message that both encodes and decodes: exactly OStreamMessage + IStreamMessage. More...
 
struct  MessageSeq
 Collects a struct/union or nested-array wrapper sequence into the container it is handed. More...
 
class  OStream
 Output stream over a caller-supplied buffer whose lifetime is shared. More...
 
class  OStreamImpl
 Base of the output streams: encodes values into a caller-provided buffer. More...
 
class  OStreamInline
 Output stream whose buffer is stored inline (the buffer costs no heap allocation). More...
 
class  OStreamMessage
 Base class for user-defined messages that can serialise themselves. More...
 
class  OStreamObject
 Bundles a message with an inline output buffer sized for it. More...
 
class  OStreamView
 Output stream over a buffer the caller already owns. More...
 
struct  RowSeq
 Collects a wrapper sequence whose elements are rows the stream cannot read on its own — a row that is itself a wrapper sequence (array<array<string>>, array<array<struct>>, deeper), or a row that must bind through a view of its elements (an enum or boolean row). More...
 
struct  StringSeq
 Collects the elements of a string wrapper-sequence array. More...
 

Concepts

concept  OutputMessage
 Constrains a serialisable message: derives from sofab::OStreamMessage and exposes a static constexpr std::size_t _maxSize.
 
concept  InputMessage
 Constrains a deserialisable message: must derive from sofab::IStreamMessage.
 

Typedefs

using id = uint32_t
 Field identifier on the wire.
 

Enumerations

enum class  Error {
  None = 0 , BufferFull = 3 , InvalidArgument = 1 , InvalidMessage = 4 ,
  Incomplete = 5 , LimitExceeded = 6
}
 Status codes returned by the encode/decode API; None signals success. More...
 
enum class  DecodeStatus {
  Complete = 0 , Incomplete = 1 , Invalid = 2 , LimitExceeded = 3 ,
  InvalidArgument = 4
}
 Three-valued decode outcome of a sofab::IStreamImpl::feed call (spec §7). More...
 

Functions

template<std::ranges::contiguous_range A, std::ranges::contiguous_range B>
requires std::ranges::sized_range<A> && std::ranges::sized_range<B> && detail::FloatElement<std::remove_cv_t<std::ranges::range_value_t<A>>> && std::same_as<std::remove_cv_t<std::ranges::range_value_t<A>>, std::remove_cv_t<std::ranges::range_value_t<B>>>
bool bitsEqual (const A &a, const B &b) noexcept
 True iff a and b have the same length and every element pair has the same IEEE-754 bit pattern.
 
template<detail::FloatElement T, std::ranges::contiguous_range A>
requires std::ranges::sized_range<A> && std::same_as<std::remove_cv_t<std::ranges::range_value_t<A>>, T>
bool bitsEqual (const A &a, std::initializer_list< T > b) noexcept
 Braced-list default: bitsEqual(field, {0.0f, 1.5f}).
 
constexpr bool utf8_valid (std::string_view bytes) noexcept
 Public UTF-8 validity primitive (spec CORELIB_PLAN §6.4).
 
template<typename S >
bool readString (IStreamImpl &is, S &dst, long maxlen) noexcept
 Read the current field as a string into a destination this call sizes — the generated layer's IStreamImpl::readString.
 
template<typename S >
bool readStringCapped (IStreamImpl &is, S &dst, long dynCap) noexcept
 The schema-unbounded sofab::readString: sizes dst only once the §6.2.1 receiver cap has admitted the field.
 
template<typename B >
bool readBlob (IStreamImpl &is, B &dst, long maxlen) noexcept
 The blob counterpart of sofab::readString.
 
template<typename B >
bool readBlobCapped (IStreamImpl &is, B &dst, long dynCap) noexcept
 The blob counterpart of sofab::readStringCapped.
 
template<typename T >
bool readArray (IStreamImpl &is, T &dst, long schemaCount, ElemBound elem={}) noexcept
 Read the current count-prefixed array into a destination this call sizes — the generated layer's IStreamImpl::readArray.
 
template<typename T >
bool readArrayCapped (IStreamImpl &is, T &dst, long dynCap, ElemBound elem={}) noexcept
 The schema-unbounded sofab::readArray: sizes dst only once the §6.2.1 receiver cap has admitted the count.
 
template<typename T >
bool read (IStreamImpl &is, T &dst) noexcept
 IStreamImpl::read with the destination sized first — the generated layer's untyped read.
 

Variables

constexpr int API_VERSION = 1
 Version of the SofaBuffers public API implemented by this header.
 
constexpr uint32_t ID_MAX = 0x7fffffffu
 Largest valid field id (INT32_MAX).
 
constexpr uint32_t FIXLEN_MAX = 0x7fffffffu
 Largest fixlen payload byte-length (INT32_MAX).
 
constexpr uint32_t ARRAY_MAX = 0x7fffffffu
 Largest array element count (INT32_MAX).
 
constexpr int MAX_DEPTH = 255
 Maximum nested-sequence depth (§4.9).
 
constexpr size_t MIN_OUTPUT_BUFFER = 1
 Smallest output buffer this port accepts for streaming (§5.1).
 
template<typename >
constexpr bool always_false_v = false
 Always-false trait used to trigger static_assert in the otherwise-unreachable branch of an if constexpr chain.
 

Typedef Documentation

◆ id

using sofab::id = typedef uint32_t

Field identifier on the wire.

Valid range is [0, INT32_MAX].

Definition at line 247 of file sofab.hpp.

Enumeration Type Documentation

◆ DecodeStatus

enum class sofab::DecodeStatus
strong

Three-valued decode outcome of a sofab::IStreamImpl::feed call (spec §7).

The decoder reports where the consumed bytes ended, with no separate finish/finalize step (§7.1): the same three outcomes apply to a one-shot buffer and to chunked streaming. Whether an Incomplete result is acceptable is the caller's decision — a streaming caller reads it as "feed me the next chunk", a whole-message caller reads it as a truncated message.

Enumerator
Complete 

Consumed bytes end exactly at a field boundary — a valid message.

Incomplete 

Consumed bytes end inside a field or with an open sequence.

Not an error.

Invalid 

Bytes are malformed regardless of what follows.

Terminal.

LimitExceeded 

A configured receiver-side limit (§6.2.1) was exceeded on a schema-**unbounded** field.

The bytes are well-formed — the same message decodes under a looser limit — so this is a policy rejection and not DecodeStatus::Invalid — §6.3 requires an implementation to "keep the two distinguishable to the caller" and forbids reporting it as InvalidMessage. Terminal.

§6.3 offers two ways to surface it, "either a <strong>fourth decode outcome</strong>, or a terminal failure carrying the <tt>LimitExceeded</tt> code on the error channel"; this port does both, so a caller switching on sofab::IStreamImpl::Result::status alone cannot mistake it for malformation.

InvalidArgument 

The value broke neither the schema bound nor a receiver cap, but does not fit the destination this caller handed over (§6.6.3, §6.3's third refusal tier).

A mistake in the call, not in the message: the same bytes decode for a caller who passes a larger destination. Terminal.

Definition at line 216 of file sofab.hpp.

◆ Error

enum class sofab::Error
strong

Status codes returned by the encode/decode API; None signals success.

Enumerator
None 

Operation succeeded (decode: COMPLETE, §7).

BufferFull 

Encode-only: the encoder ran out of room.

Either the output buffer filled and no flush callback was set, or — the rare second case — OStreamImpl::sequenceBeginLazy could not allocate room to hold one more sequence header back (see OStreamImpl::sequenceBeginLazy).

InvalidArgument 

An argument was out of range (e.g.

a field id above the limit) — and, on the decode side, §6.3's third refusal tier: a value that broke neither the schema bound nor a receiver cap but does not fit the destination the caller handed over (§6.6.3).

"The third is a mistake in the <strong>call</strong>, not a property of the message or of the deployment, and the other two codes each say something untrue about it. <tt>InvalidMessage</tt> would mark a well-formed message malformed — the same bytes decode for a caller who passes a larger destination, and no §5.2.2 condition is present. <tt>LimitExceeded</tt> would promise a limit to raise that was never configured." (§6.3)

InvalidMessage 

The input bytes are malformed (decode: INVALID, §7).

Incomplete 

Decode-only: the fed bytes end inside a field — a partial varint (§4.1), a fixlen/array payload shorter than declared (§4.6/§4.8), or an open sequence (§4.9).

This is INCOMPLETE (§7) and not an error: the caller owns end-of-input and may feed more bytes. Distinct from both None (COMPLETE) and InvalidMessage (INVALID).

LimitExceeded 

Decode-only: a receiver-configured cap (§6.2.1) was exceeded on a field the schema leaves unbounded — the field-span budget Limits::max_buffered_field, or the dynCap handed to IStreamImpl::readStringCapped, IStreamImpl::readBlobCapped, IStreamImpl::readArrayCapped or a wrapper-array collector.

Policy, not malformation — deliberately distinct from InvalidMessage, so a differential fuzzer never reads a local buffering limit as a conformance divergence. The bytes are never clamped or truncated; IStreamImpl::feed simply fails with this code.

Never raised for a field the schema bounds (§6.2.1/§6.3). A declared maxlen/count is a statement about validity and outranks any receiver-side statement about capacity: an over-bound claim is InvalidMessage no matter how the cap is configured, so the same bytes cannot come out as a policy refusal on a receiver that happens to buffer less. Only a schema-**unbounded** field reaches this code.

Definition at line 150 of file sofab.hpp.

Function Documentation

◆ bitsEqual() [1/2]

template<std::ranges::contiguous_range A, std::ranges::contiguous_range B>
requires std::ranges::sized_range<A> && std::ranges::sized_range<B> && detail::FloatElement<std::remove_cv_t<std::ranges::range_value_t<A>>> && std::same_as<std::remove_cv_t<std::ranges::range_value_t<A>>, std::remove_cv_t<std::ranges::range_value_t<B>>>
bool sofab::bitsEqual ( const A &  a,
const B &  b 
)
inlinenoexcept

True iff a and b have the same length and every element pair has the same IEEE-754 bit pattern.

Accepts anything contiguous and sized whose element type is float or double, on both sides: std::vector, sofab::InlineVector, std::array, std::span, a C array, or a braced list (bitsEqual(v, {0.0f, 1.5f}) binds a std::initializer_list<float>). Both sides must have the same element type. The length is compared first; nothing is allocated or modified.

Returns
false on a length mismatch or any differing bit pattern.

Definition at line 72 of file bits_equal.hpp.

◆ bitsEqual() [2/2]

template<detail::FloatElement T, std::ranges::contiguous_range A>
requires std::ranges::sized_range<A> && std::same_as<std::remove_cv_t<std::ranges::range_value_t<A>>, T>
bool sofab::bitsEqual ( const A &  a,
std::initializer_list< T >  b 
)
inlinenoexcept

Braced-list default: bitsEqual(field, {0.0f, 1.5f}).

Definition at line 84 of file bits_equal.hpp.

◆ read()

template<typename T >
bool sofab::read ( IStreamImpl &  is,
T &  dst 
)
noexcept

IStreamImpl::read with the destination sized first — the generated layer's untyped read.

Dispatches like IStreamImpl::read and adds nothing for a scalar or a sub-message, whose destinations hold no room to size. A std::string payload destination and a contiguous element range are sized here, so the codec never has to grow one.

Definition at line 6565 of file sofab.hpp.

◆ readArray()

template<typename T >
bool sofab::readArray ( IStreamImpl &  is,
T &  dst,
long  schemaCount,
ElemBound  elem = {} 
)
noexcept

Read the current count-prefixed array into a destination this call sizes — the generated layer's IStreamImpl::readArray.

Sizes dst for the announced element count, clamped to what the bytes in hand could fill, and only once the tag (§7.3), the schema count (§7.1) and the configured receiver cap (§6.2.1) all admit the field — so neither a mistyped occurrence nor a hostile count reaches an allocator. The codec then fills what it was given room for and refuses a destination shorter than the array actually is (§6.6.3).

Two entry points, neither defaulted, exactly as for sofab::readString.

Parameters
isStream delivering the field.
dstDestination range.
schemaCountDeclared count: N. Required and non-negative.
elemDeclared element range (ElemBound).
Returns
true when the array was read.

Definition at line 6533 of file sofab.hpp.

◆ readArrayCapped()

template<typename T >
bool sofab::readArrayCapped ( IStreamImpl &  is,
T &  dst,
long  dynCap,
ElemBound  elem = {} 
)
noexcept

The schema-unbounded sofab::readArray: sizes dst only once the §6.2.1 receiver cap has admitted the count.

Parameters
isStream delivering the field.
dstDestination range.
dynCapThe caller's max_dyn_array_count. Required and non-negative.
elemDeclared element range (ElemBound).

Definition at line 6549 of file sofab.hpp.

◆ readBlob()

template<typename B >
bool sofab::readBlob ( IStreamImpl &  is,
B &  dst,
long  maxlen 
)
noexcept

The blob counterpart of sofab::readString.

Sizes dst for the announced payload and then hands it to the codec, which refuses a destination too short rather than growing one (§6.6.3). For a destination with static room (FixedString) it adds nothing and compiles away; for a std::string it is the one place the allocation happens.

The sizing runs only once every gate IStreamImpl::readString applies has admitted the field — the tag (§7.3), the declared maxlen (§7.1) and the configured receiver cap dynCap (§6.2.1) — so a mistyped, an over-long or an over-cap occurrence leaves dst exactly as it was (§7.4). Sizing on a subset of them would defeat the point of a receiver cap: §6.2.1 puts the check "before the allocation it is meant to prevent", and this call is that allocation. A schema-unbounded field sized here from the wire-announced length is precisely the sender dictating the receiver's allocation.

No number can be left out
This helper holds no limit and invents none (§6.2.1: a codec "MUST NOT supply a default for one it was not given, MUST NOT read an omitted argument as <em>unlimited</em>"), so it does not offer an argument that may be omitted at all. There are two entry points, one per bound that can apply, neither defaulted:
sofab::readString(is, name, 32); // maxlen: 32
sofab::readStringCapped(is, name, SOFAB_MAX_DYN_STRING_LEN); // no maxlen
bool readString(IStreamImpl &is, S &dst, long maxlen) noexcept
Read the current field as a string into a destination this call sizes — the generated layer's IStream...
Definition sofab.hpp:6470
bool readStringCapped(IStreamImpl &is, S &dst, long dynCap) noexcept
The schema-unbounded sofab::readString: sizes dst only once the §6.2.1 receiver cap has admitted the ...
Definition sofab.hpp:6485
Sizing dst from the wire with neither number stated is precisely the sender dictating the receiver's allocation, so a negative one is refused with Error::InvalidArgument rather than obeyed.
Parameters
isStream delivering the field.
dstDestination for the decoded text.
maxlenDeclared maxlen. Required and non-negative.
Returns
true when the value was read.

Definition at line 6496 of file sofab.hpp.

◆ readBlobCapped()

template<typename B >
bool sofab::readBlobCapped ( IStreamImpl &  is,
B &  dst,
long  dynCap 
)
noexcept

The blob counterpart of sofab::readStringCapped.

Parameters
isStream delivering the field.
dstDestination for the decoded text.
dynCapThe caller's max_dyn_string_len. Required and non-negative.

Definition at line 6507 of file sofab.hpp.

◆ readString()

template<typename S >
bool sofab::readString ( IStreamImpl &  is,
S &  dst,
long  maxlen 
)
noexcept

Read the current field as a string into a destination this call sizes — the generated layer's IStreamImpl::readString.

Sizes dst for the announced payload and then hands it to the codec, which refuses a destination too short rather than growing one (§6.6.3). For a destination with static room (FixedString) it adds nothing and compiles away; for a std::string it is the one place the allocation happens.

The sizing runs only once every gate IStreamImpl::readString applies has admitted the field — the tag (§7.3), the declared maxlen (§7.1) and the configured receiver cap dynCap (§6.2.1) — so a mistyped, an over-long or an over-cap occurrence leaves dst exactly as it was (§7.4). Sizing on a subset of them would defeat the point of a receiver cap: §6.2.1 puts the check "before the allocation it is meant to prevent", and this call is that allocation. A schema-unbounded field sized here from the wire-announced length is precisely the sender dictating the receiver's allocation.

No number can be left out
This helper holds no limit and invents none (§6.2.1: a codec "MUST NOT supply a default for one it was not given, MUST NOT read an omitted argument as <em>unlimited</em>"), so it does not offer an argument that may be omitted at all. There are two entry points, one per bound that can apply, neither defaulted:
sofab::readString(is, name, 32); // maxlen: 32
sofab::readStringCapped(is, name, SOFAB_MAX_DYN_STRING_LEN); // no maxlen
Sizing dst from the wire with neither number stated is precisely the sender dictating the receiver's allocation, so a negative one is refused with Error::InvalidArgument rather than obeyed.
Parameters
isStream delivering the field.
dstDestination for the decoded text.
maxlenDeclared maxlen. Required and non-negative.
Returns
true when the value was read.

Definition at line 6470 of file sofab.hpp.

◆ readStringCapped()

template<typename S >
bool sofab::readStringCapped ( IStreamImpl &  is,
S &  dst,
long  dynCap 
)
noexcept

The schema-unbounded sofab::readString: sizes dst only once the §6.2.1 receiver cap has admitted the field.

Parameters
isStream delivering the field.
dstDestination for the decoded text.
dynCapThe caller's max_dyn_string_len. Required and non-negative.

Definition at line 6485 of file sofab.hpp.

◆ utf8_valid()

constexpr bool sofab::utf8_valid ( std::string_view  bytes)
constexprnoexcept

Public UTF-8 validity primitive (spec CORELIB_PLAN §6.4).

Returns true iff bytes is well-formed UTF-8 by detail::utf8Valid (rejecting overlong forms, surrogates and out-of-range code points; accepting embedded NUL). Independent of SOFAB_STRICT_UTF8 — this is the validator itself, always available; the compile-time flag only decides whether the encode/decode paths invoke it.

Parameters
bytesCandidate string payload.
Returns
true iff bytes is valid UTF-8.

Definition at line 722 of file sofab.hpp.

Variable Documentation

◆ always_false_v

template<typename >
constexpr bool sofab::always_false_v = false
inlineconstexpr

Always-false trait used to trigger static_assert in the otherwise-unreachable branch of an if constexpr chain.

Because the value depends on the template parameter, the assertion only fires when that branch is actually instantiated, which is the idiomatic way to reject unsupported types at compile time.

Template Parameters
TThe type the dependent assertion is bound to.

Definition at line 147 of file sofab.hpp.

◆ API_VERSION

constexpr int sofab::API_VERSION = 1
inlineconstexpr

Version of the SofaBuffers public API implemented by this header.

Definition at line 79 of file sofab.hpp.

◆ ARRAY_MAX

constexpr uint32_t sofab::ARRAY_MAX = 0x7fffffffu
inlineconstexpr

Largest array element count (INT32_MAX).

Encoding a longer array is Error::InvalidArgument; decoding one is Error::InvalidMessage.

Definition at line 105 of file sofab.hpp.

◆ FIXLEN_MAX

constexpr uint32_t sofab::FIXLEN_MAX = 0x7fffffffu
inlineconstexpr

Largest fixlen payload byte-length (INT32_MAX).

Encoding a longer payload is Error::InvalidArgument; decoding one is Error::InvalidMessage.

Definition at line 100 of file sofab.hpp.

◆ ID_MAX

constexpr uint32_t sofab::ID_MAX = 0x7fffffffu
inlineconstexpr

Largest valid field id (INT32_MAX).

Encoding a larger id is Error::InvalidArgument; decoding one is Error::InvalidMessage.

Definition at line 94 of file sofab.hpp.

◆ MAX_DEPTH

constexpr int sofab::MAX_DEPTH = 255
inlineconstexpr

Maximum nested-sequence depth (§4.9).

Deeper nesting is rejected (encode: Error::InvalidArgument; decode: Error::InvalidMessage).

Definition at line 110 of file sofab.hpp.

◆ MIN_OUTPUT_BUFFER

constexpr size_t sofab::MIN_OUTPUT_BUFFER = 1
inlineconstexpr

Smallest output buffer this port accepts for streaming (§5.1).

This implementation writes byte-at-a-time whenever the buffer is tight — every write funnels through OStreamImpl::pushBytes, which falls back to a per-byte push that flushes across the boundary — so it splits atomic units too and declares the smallest value §5.1 admits, 1. A caller therefore never has to reserve headroom: any non-empty buffer streams a message of any size, and the bytes are identical to the one-shot path.

It binds a buffer installed together with a flush sink, and only such a buffer: buflen - offset >= MIN_OUTPUT_BUFFER is checked where the buffer is handed over — a constructor or OStream::setBuffer — and never partway through a message. A buffer installed without a sink is subject to no minimum at all: no flush can occur, so it either holds the message or reports Error::BufferFull, and a two-byte message still encodes into two bytes.

A rejected installation leaves the stream inert and latched at Error::InvalidArgument (OStreamImpl::ok is false, OStreamImpl::error reports it); no write it is then asked for touches the buffer.

Definition at line 134 of file sofab.hpp.