SofaBuffers C/C++ 0.12.0
API Documentation
Loading...
Searching...
No Matches
sofab Namespace Reference

Namespaces

namespace  detail
 

Data Structures

class  DynCap
 A CORELIB_PLAN §6.2.1 receiver cap, exactly as the caller states it. More...
 
struct  FixedBlobSeq
 
class  FixedBytes
 Fixed-capacity, heap-free byte blob of up to N bytes. More...
 
struct  FixedMessageSeq
 Collects a struct/union or nested-array wrapper sequence into inline storage. More...
 
struct  FixedRowSeq
 Collects a wrapper sequence of rows the stream cannot read on its own, into inline storage. More...
 
class  FixedString
 Fixed-capacity, heap-free string of up to N characters. More...
 
struct  FixedStringSeq
 Collects a string wrapper sequence into inline storage. More...
 
class  InlineVector
 Fixed-capacity, heap-free sequence of up to N elements of type T. More...
 
struct  is_fixed_bytes
 Trait: true only for FixedBytes specializations. More...
 
struct  is_fixed_bytes< FixedBytes< N > >
 
struct  is_fixed_string
 Trait: true only for FixedString specializations. More...
 
struct  is_fixed_string< FixedString< N > >
 
class  IStreamImpl
 Base input stream: incrementally decodes fed bytes into bound targets. More...
 
class  IStreamInline
 Input stream driven by a user-supplied per-field callback. More...
 
class  IStreamInto
 Decoder that decodes into a message instance the CALLER owns. More...
 
class  IStreamMessage
 Base class for decodable message objects. More...
 
class  IStreamObject
 Self-contained decoder that owns a message instance. 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 a growable container. More...
 
class  OStreamImpl
 Base output stream: encodes fields into a caller-provided buffer. More...
 
class  OStreamInline
 Output stream backed by an inline, fixed-size buffer. More...
 
class  OStreamMessage
 Base class for serializable message objects. More...
 
class  OStreamObject
 Self-contained encoder that owns a message and an inline buffer. More...
 
class  OStreamObjectOffset
 OStreamObject variant parameterized by offset only. More...
 
class  OStreamView
 Output stream over a buffer the caller already owns. More...
 
struct  RowSeq
 Collects a wrapper sequence of rows the stream cannot read on its own, into a growable container. More...
 
struct  RowSeqBounds
 The index bounds of a RowSeq whose schema count is not a template argument: the schema count and the §6.2.1 receiver cap, as runtime members, exactly as MessageSeq carries them. More...
 
struct  RowSeqBounds<-1 >
 The index bounds of a RowSeq whose schema count is not a template argument: the schema count and the §6.2.1 receiver cap, as runtime members, exactly as MessageSeq carries them. More...
 
struct  wire_int
 The integer type an element travels as on the wire: identity for an integer, the declared underlying type for an enumeration. More...
 
struct  wire_int< E, true >
 Enumeration case of wire_int: the declared underlying type. More...
 

Concepts

concept  HasMaxSize
 Detects the CORELIB_PLAN §6.1.1 spelling of the worst-case size.
 
concept  HasLegacyMaxSize
 Detects the pre-0.11 spelling. Deprecated; see OutputMessage.
 
concept  OutputMessage
 Concept: a serializable message type usable with OStreamObject.
 
concept  InputMessage
 Concept: a decodable message type usable with IStreamObject and the nested-message IStreamImpl::read() overload.
 
concept  HasFixedCapacity
 Detects a container that states its own fixed capacity: the condition behind fixed_capacity_v.
 

Typedefs

template<typename E >
using wire_int_t = typename wire_int< E >::type
 Alias for wire_int.
 
using id = sofab_id_t
 Field identifier type (alias of sofab_id_t).
 

Enumerations

enum class  Error {
  None = SOFAB_RET_OK , Incomplete = SOFAB_RET_INCOMPLETE , BufferFull = SOFAB_RET_E_BUFFER_FULL , InvalidArgument = SOFAB_RET_E_ARGUMENT ,
  InvalidMessage = SOFAB_RET_E_INVALID_MSG , LimitExceeded = SOFAB_RET_E_LIMIT_EXCEEDED
}
 Result/error code returned by the stream APIs (wraps sofab_ret_t). More...
 
enum class  Wire : uint8_t {
  Unsigned = SOFAB_TYPE_VARINT_UNSIGNED , Signed = SOFAB_TYPE_VARINT_SIGNED , Fixlen = SOFAB_TYPE_FIXLEN , ArrayUnsigned = SOFAB_TYPE_VARINTARRAY_UNSIGNED ,
  ArraySigned = SOFAB_TYPE_VARINTARRAY_SIGNED , ArrayFixlen = SOFAB_TYPE_FIXLENARRAY , SequenceStart = SOFAB_TYPE_SEQUENCE_START , SequenceEnd = SOFAB_TYPE_SEQUENCE_END
}
 Wire type of a decoded field (the 3-bit type tag of its header). More...
 
enum class  Fix : uint8_t { Fp32 = SOFAB_FIXLENTYPE_FP32 , Fp64 = SOFAB_FIXLENTYPE_FP64 , String = SOFAB_FIXLENTYPE_STRING , Blob = SOFAB_FIXLENTYPE_BLOB }
 Sub-type of a length-prefixed (Wire::Fixlen) payload. More...
 

Functions

template<typename T >
bool bitsEqual (const T *a, std::size_t na, const T *b, std::size_t nb) noexcept
 True iff both float arrays have the same length and the same bit pattern at every index.
 
template<typename A , typename B >
bool bitsEqual (const A &a, const B &b) noexcept
 Container form of bitsEqual: anything contiguous that std::data / std::size accept and that holds float or double.
 
bool seqRefuse (IStreamImpl &is, size_t n, long schemaBound, DynCap dynCap) noexcept
 Apply a collector's message-side bounds and report the verdict.
 
template<typename C >
std::span< const typename C::value_type > trimTail (const C &a) noexcept
 Narrow a fixed-count array to its non-default prefix, for encode.
 

Variables

template<typename Elem >
constexpr int seq_elem_wire_v
 Wire type an element of type Elem arrives with, or -1 for none.
 
template<typename Elem >
constexpr int seq_elem_fix_v
 Fixlen sub-type an element of type Elem arrives with, or -1.
 
constexpr int API_VERSION = 1
 SofaBuffers C++ API version (mirrors SOFAB_API_VERSION).
 
constexpr bool ENCODE_BOUNDS = SOFAB_CPP_HAVE_ENCODE_BOUNDS != 0
 Whether generated encode refuses an over-bound value (SOFAB_CPP_HAVE_ENCODE_BOUNDS; off with SOFAB_DISABLE_ENCODE_BOUNDS).
 
template<typename >
constexpr bool always_false_v = false
 Always-false trait used to trigger a dependent static_assert in a discarded if constexpr branch (so it only fires when instantiated).
 
template<typename T >
constexpr bool is_fixed_string_v
 Convenience value for is_fixed_string.
 
template<typename T >
constexpr bool is_fixed_bytes_v
 Convenience value for is_fixed_bytes.
 
template<class T >
constexpr size_t max_size_v
 The worst-case encoded size of a generated message, whichever spelling it declares. Prefers MAX_SIZE.
 
template<typename C >
constexpr long fixed_capacity_v = -1
 Compile-time capacity a heap-free container publishes, or -1.
 
template<HasFixedCapacity C>
constexpr long fixed_capacity_v< C > = static_cast<long>(C::capacity())
 

Typedef Documentation

◆ id

Field identifier type (alias of sofab_id_t).

Definition at line 213 of file sofab.hpp.

◆ wire_int_t

Alias for wire_int.

Definition at line 166 of file sofab.hpp.

Enumeration Type Documentation

◆ Error

Result/error code returned by the stream APIs (wraps sofab_ret_t).

Enumerator
None 

Success (a complete message boundary).

Incomplete 

Consumed bytes end inside a field or with an open sequence: a valid but partial decode.

NOT an error — the caller owns end-of-input and may feed more bytes. Distinct from None (complete) and InvalidMessage (malformed).

BufferFull 

Output buffer overflowed during encoding.

InvalidArgument 

A field id out of range, a scalar width that is not 1/2/4/8 — or a destination too short for the value it was handed (§6.6.3): the third of the three ways a value can be refused, and the one that is a mistake in the call.

See IStreamImpl::readString.

InvalidMessage 

Malformed message encountered while decoding.

LimitExceeded 

A configured receiver limit (§6.2.1) was.

exceeded on a schema-**unbounded** field. The bytes are well-formed and decode under a looser limit, so this is deliberately not InvalidMessage: it means "raise my limit, or the sender must send less", where InvalidMessage means "these bytes are broken". Terminal, like InvalidMessage.

This corelib never decides it. §6.2.1 gives the limits to generated code — "the visitor decides. The codec never invents a limit of its own and never clamps to one" — and leaves the codec "the report and the category". This enumerator is the category; the report is the size / count the field callback already carries. A handler that finds an announced length past its configured cap calls IStreamImpl::exceedLimit and returns without binding a destination.

Definition at line 169 of file sofab.hpp.

◆ Fix

Sub-type of a length-prefixed (Wire::Fixlen) payload.

Part of the public API: returned by IStreamImpl::fixType. MESSAGE_SPEC §7.3 bounds the decode-side type check at wire type plus this subtype, since fp32 / fp64 / string / blob all share the Wire::Fixlen wire type. Mirrors sofab_fixlentype_t and matches corelib-cpp's sofab::Fix.

Enumerator
Fp32 

32-bit IEEE-754 float.

Fp64 

64-bit IEEE-754 double.

String 

UTF-8 text.

Blob 

Opaque byte string.

Definition at line 244 of file sofab.hpp.

◆ Wire

Wire type of a decoded field (the 3-bit type tag of its header).

Part of the public API: returned by IStreamImpl::wire. The enumerator values are the wire tags themselves, so this mirrors sofab_type_t while exposing the same sofab::Wire surface as the sibling corelib-cpp — a generator emits one IStreamImpl::wire guard shape for both.

Enumerator
Unsigned 

Unsigned integer encoded as a varint.

Signed 

Signed integer, zig-zag encoded as a varint.

Fixlen 

Length-prefixed payload (float, string or blob).

ArrayUnsigned 

Count-prefixed array of unsigned varints.

ArraySigned 

Count-prefixed array of zig-zag varints.

ArrayFixlen 

Count-prefixed array of fixed-size elements.

SequenceStart 

Opens a nested sub-message.

SequenceEnd 

Closes the most recently opened sub-message.

Definition at line 223 of file sofab.hpp.

Function Documentation

◆ bitsEqual() [1/2]

template<typename A , typename B >
bool sofab::bitsEqual ( const A &  a,
const B &  b 
)
inlinenoexcept

Container form of bitsEqual: anything contiguous that std::data / std::size accept and that holds float or double.

Takes a mutable field (std::vector, InlineVector, std::array) and a constant default (a temporary of the same type, a std::span, a std::initializer_list) alike, and the two need not be the same container type — only the same element type.

Definition at line 81 of file floats.hpp.

Here is the call graph for this function:

◆ bitsEqual() [2/2]

template<typename T >
bool sofab::bitsEqual ( const T *  a,
std::size_t  na,
const T *  b,
std::size_t  nb 
)
inlinenoexcept

True iff both float arrays have the same length and the same bit pattern at every index.

The length is compared first (early exit); the elements are then compared with one block memcmp over n * sizeof(T) bytes, so there is no IEEE == anywhere. Nothing is allocated or modified.

Template Parameters
Tfloat (32-bit pattern) or double (64-bit pattern).
Parameters
aFirst array, or any pointer when na is 0.
naLength of a in elements.
bSecond array, or any pointer when nb is 0.
nbLength of b in elements.

Definition at line 58 of file floats.hpp.

◆ seqRefuse()

bool sofab::seqRefuse ( IStreamImpl &  is,
size_t  n,
long  schemaBound,
DynCap  dynCap 
)
inlinenoexcept

Apply a collector's message-side bounds and report the verdict.

The collectors below are the static helper layer (§6.6.1): they ship here for reuse, the generated layer owns them, and no codec path calls them — IStreamImpl::readSequence hands control to the caller's collector exactly as it hands control to any other handler. So a bound applied here is generated code's bound, not the codec's, and §6.2.1's "the codec never invents a limit of its own" is kept: every number below arrives in a field the generated layer sets.

A wrapper array is the one shape that has nowhere else to put it. It announces no count and fires no callback per element, so the caller has no point at which to check, and §6.2.1 puts the enforcement point on the element index, before the container is extended.

Parameters
isThe stream, for the verdict.
nThe index + 1, or the element length.
schemaBoundThe schema's count / maxlen, or -1. Exceeding it is Error::InvalidMessage.
dynCapThe §6.2.1 receiver cap. Consulted only where schemaBound is -1. Exceeding it is Error::LimitExceeded; leaving it unstated there is Error::InvalidArgument, because §6.2.1 has no unlimited mode for it to mean instead.
Returns
true when the element was refused.

Definition at line 3845 of file sofab.hpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ trimTail()

template<typename C >
std::span< const typename C::value_type > sofab::trimTail ( const C &  a)
noexcept

Narrow a fixed-count array to its non-default prefix, for encode.

Deprecated:
Superseded and non-conformant. MESSAGE_SPEC §3 now defines count: N as a capacity and the wire count M as the array's length, so nothing may be elided: [1,2,3,0,0] and [1,2,3] are different values, and trimming the tail silently shortens the array. There is no fill-back on decode either. This helper is retained only so generated code emitted before the change still compiles; new code must not call it, and it goes away with the generator that emits it. (Its C counterpart, _array_trim_count in object.c, is already gone.)

The superseded contract it implements: a count: N array's canonical encoding carries M = one past the last element that differs from the element default, and the decoder refills [M, N).

Elements compare by byte image, never operator==: -0.0 == 0.0 holds in C++, but -0.0 is a distinct value that must survive the round-trip, and a NaN payload likewise never matches the default.

Parameters
aContiguous container of trivially-copyable elements.
Returns
A span over [0, M).

Definition at line 3967 of file sofab.hpp.

Here is the call graph for this function:

Variable Documentation

◆ always_false_v

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

Always-false trait used to trigger a dependent static_assert in a discarded if constexpr branch (so it only fires when instantiated).

Definition at line 143 of file sofab.hpp.

◆ API_VERSION

constexpr int sofab::API_VERSION = 1
inlineconstexpr

SofaBuffers C++ API version (mirrors SOFAB_API_VERSION).

Definition at line 134 of file sofab.hpp.

◆ ENCODE_BOUNDS

constexpr bool sofab::ENCODE_BOUNDS = SOFAB_CPP_HAVE_ENCODE_BOUNDS != 0
inlineconstexpr

Whether generated encode refuses an over-bound value (SOFAB_CPP_HAVE_ENCODE_BOUNDS; off with SOFAB_DISABLE_ENCODE_BOUNDS).

Definition at line 138 of file sofab.hpp.

◆ fixed_capacity_v

template<typename C >
constexpr long sofab::fixed_capacity_v = -1
inlineconstexpr

Compile-time capacity a heap-free container publishes, or -1.

InlineVector, FixedString and FixedBytes all declare capacity() static constexpr, which is what tells them apart from std::vector — whose capacity() is a per-object, runtime quantity and so cannot be called on the type.

Two readings, and which one applies is decided by the caller
A heap-free container is usually generated for a schema count / maxlen, so its capacity is numerically that bound. Whether it is read as that bound depends on whether the caller was told the bound some other way, and the two callers in this header differ:
  • the reads — IStreamImpl::readString, IStreamImpl::readBlob and IStreamImpl::readArray take the schema bound as a parameter, so the capacity is not what states it to them. What the capacity means to a read is how much storage the caller offered — §6.3's third tier, and a value the schema and the cap both admit but the destination cannot hold is Error::InvalidArgument, never Error::InvalidMessage. A read passed no bound is told nothing about the schema and can only apply that third tier — which is why a capacity of -1 there, with no bound and no cap, leaves the read with no ceiling at all and is refused outright (IStreamImpl::refuseUnbounded);
  • the fixed-storage collectors — FixedMessageSeq and the fixed string/blob sequences take no cap at all, by design: their static_assert says the container's capacity is the schema bound they were generated for, because there is no other channel to state it on. For them it is §6.3's first tier, and an element past it is Error::InvalidMessage (MESSAGE_SPEC §7.1).

Both are correct, and the difference is not about the number — it is about which channel states it. A collector generated for a bound carries it in the only place it has; a read is told it outright, and applies it through IStreamImpl::refuseSchema (or IStreamImpl::refuseCap, on the …Capped entry point) before it sizes anything.

Template Parameters
CContainer type.

Definition at line 1991 of file sofab.hpp.

◆ fixed_capacity_v< C >

template<HasFixedCapacity C>
constexpr long sofab::fixed_capacity_v< C > = static_cast<long>(C::capacity())
inlineconstexpr

Definition at line 1994 of file sofab.hpp.

◆ is_fixed_bytes_v

template<typename T >
constexpr bool sofab::is_fixed_bytes_v
inlineconstexpr
Initial value:
=
is_fixed_bytes<std::remove_cv_t<T>>::value

Convenience value for is_fixed_bytes.

Definition at line 669 of file sofab.hpp.

◆ is_fixed_string_v

template<typename T >
constexpr bool sofab::is_fixed_string_v
inlineconstexpr
Initial value:
=
is_fixed_string<std::remove_cv_t<T>>::value

Convenience value for is_fixed_string.

Definition at line 505 of file sofab.hpp.

◆ max_size_v

template<class T >
constexpr size_t sofab::max_size_v
inlineconstexpr
Initial value:
= [] () constexpr -> size_t
{
if constexpr (HasMaxSize<T>) { return T::MAX_SIZE; }
else { return T::_maxSize; }
}()
Detects the CORELIB_PLAN §6.1.1 spelling of the worst-case size.
Definition sofab.hpp:1793

The worst-case encoded size of a generated message, whichever spelling it declares. Prefers MAX_SIZE.

Definition at line 1808 of file sofab.hpp.

◆ seq_elem_fix_v

template<typename Elem >
constexpr int sofab::seq_elem_fix_v
inlineconstexpr
Initial value:
=
[] () constexpr -> int
{
if constexpr (!std::is_base_of_v<IStreamMessage, Elem>
&& requires { typename Elem::value_type; })
{
using RowElem = typename Elem::value_type;
if constexpr (std::is_same_v<RowElem, float>)
{
return static_cast<int>(Fix::Fp32);
}
else if constexpr (std::is_same_v<RowElem, double>)
{
return static_cast<int>(Fix::Fp64);
}
else
{
return -1;
}
}
else
{
return -1;
}
}()

Fixlen sub-type an element of type Elem arrives with, or -1.

The companion to seq_elem_wire_v for the one wire type that is not self-describing: fp32 and fp64 rows share Wire::ArrayFixlen, so §7.3 only separates them at the sub-type. -1 wherever the wire type already settles the element kind.

Template Parameters
ElemElement type (an IStreamMessage, or a row container).

Definition at line 103 of file seq.hpp.

◆ seq_elem_wire_v

template<typename Elem >
constexpr int sofab::seq_elem_wire_v
inlineconstexpr
Initial value:
=
[] () constexpr -> int
{
if constexpr (std::is_base_of_v<IStreamMessage, Elem>)
{
return static_cast<int>(Wire::SequenceStart);
}
else if constexpr (requires { typename Elem::value_type; })
{
using RowElem = typename Elem::value_type;
if constexpr (std::is_same_v<RowElem, float> || std::is_same_v<RowElem, double>)
{
return static_cast<int>(Wire::ArrayFixlen);
}
else if constexpr (std::is_integral_v<RowElem>)
{
return static_cast<int>(
std::is_signed_v<RowElem> ? Wire::ArraySigned : Wire::ArrayUnsigned);
}
else
{
return -1;
}
}
else
{
return -1;
}
}()

Wire type an element of type Elem arrives with, or -1 for none.

MESSAGE_SPEC §7.3 bounds an element by the wire type its declared type implies, and the collectors below must apply that check before they touch the destination — one step earlier than IStreamImpl::read or IStreamImpl::readArray would settle it for themselves. The two must not be able to disagree, so the choice made here mirrors readArray's exactly: a struct/union element is a nested sequence, and a native-scalar row is the array wire type its element kind selects.

-1 means the element type implies no single wire type; the check is then left to the read, which still refuses to bind a contradicting field.

Template Parameters
ElemElement type (an IStreamMessage, or a row container).

Definition at line 62 of file seq.hpp.