|
SofaBuffers C++ 0.12.0
Pure-C++20 SofaBuffers — API Documentation
|
Implementation details of the wire format; not part of the public API. More...
Classes | |
| struct | FitDest |
| fitDest as the sizer a typed read runs between its checks and its delivery, so the free reads below state the §7.3 tag, the bound and the reach once — inside the read — instead of re-deriving them in front of it. More... | |
| struct | NoSizer |
| The "size nothing" sizer: what a typed read does with the caller's destination between its checks and its delivery when the caller did not ask for sizing (the destination is used as handed). More... | |
| struct | WireInt |
| The integer type an element travels as on the wire. More... | |
| struct | WireInt< E, true > |
| Enumeration case of WireInt: the declared underlying type. More... | |
Concepts | |
| concept | FloatElement |
float or double, the two element types of an fp array. | |
| concept | sizable |
Give dst room for want, if it is a destination the caller has to size. | |
Typedefs | |
| template<typename E > | |
| using | WireInt_t = typename WireInt< E >::type |
| Alias for WireInt. | |
Enumerations | |
| enum class | Wire : uint8_t { Unsigned = 0 , Signed = 1 , Fixlen = 2 , ArrayUnsigned = 3 , ArraySigned = 4 , ArrayFixlen = 5 , SequenceStart = 6 , SequenceEnd = 7 } |
| Wire type stored in the low 3 bits of every field header. More... | |
| enum class | Fix : uint8_t { Fp32 = 0 , Fp64 = 1 , String = 2 , Blob = 3 } |
Sub-type of a length-prefixed (Fixlen) payload, stored in the low 3 bits of its length word. More... | |
Functions | |
| template<FloatElement T> | |
| bool | bitsEqualSpan (std::span< const T > a, std::span< const T > b) noexcept |
| Length check, then one block compare of the object representation. | |
| void | storeLittle64 (uint8_t *out, uint64_t w) noexcept |
| Store a 64-bit word as eight little-endian bytes. | |
| constexpr uint64_t | spread7 (uint64_t v) noexcept |
Spread the low 56 bits of v into eight 7-bit groups, one per byte. | |
| constexpr uint64_t | gather7 (uint64_t w) noexcept |
| Inverse of spread7: pack eight bytes' low 7 bits into one value. | |
| template<std::unsigned_integral U> | |
| void | storeLittleBits (uint8_t *out, U bits) noexcept |
| Store the object representation of a 4- or 8-byte value as little-endian bytes. | |
| template<std::unsigned_integral U> | |
| U | loadLittleBits (const uint8_t *in) noexcept |
| Inverse of storeLittleBits. | |
| uint64_t | loadLittle64 (const uint8_t *in) noexcept |
| Load eight little-endian bytes as a 64-bit word (see storeLittle64). | |
| constexpr uint64_t | zigzagEncode (int64_t v) noexcept |
| Map a signed integer to an unsigned one with the zig-zag scheme. | |
| constexpr int64_t | zigzagDecode (uint64_t u) noexcept |
| Inverse of zigzagEncode. | |
| template<std::floating_point F> | |
| constexpr auto | floatBits (F v) noexcept |
| Reinterpret a float/double as the unsigned integer holding its bits. | |
| template<std::floating_point F, std::unsigned_integral U> | |
| constexpr F | bitsFloat (U bits) noexcept |
| Reconstruct a floating-point value from its raw bits. | |
| constexpr uint32_t | utf8Advance (uint32_t state, const uint8_t *d, size_t len) noexcept |
Step the UTF-8 DFA over len bytes and return the state. | |
| constexpr bool | utf8Valid (const char *data, size_t len) noexcept |
| template<typename T > | |
| constexpr long | destCapacity () noexcept |
The element ceiling a decode destination T publishes, or -1 when it publishes none. | |
| template<typename D > | |
| constexpr size_t | destRoom (const D &d) noexcept |
| Room a decode destination already holds — bytes for a payload, elements for an array (CORELIB_PLAN §6.6.3). | |
| template<typename D > | |
| constexpr void | destSetLen (D &d, size_t n) noexcept |
| Publish the decoded length in a destination that already had the room — the one container operation the codec performs. | |
| std::size_t | utf8Prefix (std::string_view sv, std::size_t n) noexcept |
Length of the longest prefix of sv that is at most n bytes and does not end inside a UTF-8 character; sv is longer than n. | |
| template<typename D > | |
| void | fitDest (D &dst, size_t want) noexcept |
| size_t | arrayReach (const IStreamImpl &is) noexcept |
| Elements of the announced array the bytes in hand could still carry, clamped to the count itself. | |
| template<typename Elem > | |
| bool | arrayTagMatches (const IStreamImpl &is) noexcept |
Does the delivered tag match the array kind Elem declares (§7.3)? | |
| bool | seqIndexAdmitted (IStreamImpl &is, sofab::id id, long cap, long dynCap, long destCap) noexcept |
| Admit or refuse a wrapper-array element index, in §6.3's categories, before the container it indexes into is extended. | |
| template<typename A > | |
| constexpr bool | namesSeqContainer () noexcept |
| Whether a MessageSeq template argument names the destination container rather than the element type. | |
Variables | |
| constexpr size_t | VARINT_MAX_BYTES = 10 |
| Widest base-128 varint: ceil(64/7) == 10 bytes (§4.1). | |
| constexpr uint8_t | utf8Dfa [] |
Validate that [data, data+len) is well-formed UTF-8. | |
| constexpr uint32_t | UTF8_ACCEPT = 0 |
| The DFA's ACCEPT and REJECT states (see utf8Dfa). | |
| constexpr uint32_t | UTF8_REJECT = 12 |
Implementation details of the wire format; not part of the public API.
| using sofab::detail::WireInt_t = typedef typename WireInt<E>::type |
|
strong |
Sub-type of a length-prefixed (Fixlen) payload, stored in the low 3 bits of its length word.
Internal, like Wire. §7.3 bounds the type check at wire type plus this subtype, since fp32/fp64/string/blob share Wire::Fixlen.
| Enumerator | |
|---|---|
| Fp32 | 32-bit IEEE-754 float. |
| Fp64 | 64-bit IEEE-754 double. |
| String | UTF-8 text. |
| Blob | Opaque byte string. |
|
strong |
Wire type stored in the low 3 bits of every field header.
Internal. The typed reads compare it themselves (§7.3), so neither generated nor hand-written code has to name it.
|
inlinenoexcept |
Elements of the announced array the bytes in hand could still carry, clamped to the count itself.
The same clamp IStreamImpl::readArray applies to its own refusal, so the helper never sizes a destination the codec would then refuse, and an announced 2^31 whose payload is absent or withheld sizes nothing.
|
inlinenoexcept |
|
inlinenoexcept |
Length check, then one block compare of the object representation.
Definition at line 44 of file bits_equal.hpp.
|
constexprnoexcept |
Reconstruct a floating-point value from its raw bits.
| F | Target floating-point type. |
| U | Unsigned integer type holding the bit pattern. |
| bits | Object representation produced by floatBits. |
|
constexprnoexcept |
The element ceiling a decode destination T publishes, or -1 when it publishes none.
The bound IStreamImpl::readArray compares the wire count against before it fills anything (MESSAGE_SPEC §3): a heap-free destination that cannot reach the count must reject it, never truncate into it.
Told apart by the destination's own capabilities rather than by name, so a caller's own container works too — a static capacity() is the ceiling (InlineVector and friends: resize exists but clamps to N), everything else is treated as growable. T::capacity() is deliberately spelled as an unqualified static call: std::vector's capacity() is a non-static member, so naming it this way is ill-formed and the requirement fails — exactly the growable / heap-free split IStreamImpl::readString already keys on.
A destination of fixed extent that publishes nothing (std::array, a bound span) is not covered here: that is IStreamImpl::read's low-level contract, where the leading elements land in the destination and the rest is parsed only to stay framed.
|
constexprnoexcept |
Room a decode destination already holds — bytes for a payload, elements for an array (CORELIB_PLAN §6.6.3).
§6.6 forbids the codec growing the caller's destination from a wire number: "the codec allocates nothing itself but **requires a growable destination** and grows it, from a wire count or otherwise — violates". §6.6.3 names the shape that replaces it — a value is delivered "into a destination the caller hands back after being told the announced count, <strong>with the codec refusing a destination too short rather than growing it</strong>" — so every typed read needs one number: how much room is there now.
Two kinds of destination answer it differently, and the difference is not a policy the codec picks but a fact the type publishes:
T::capacity() — sofab::FixedString, sofab::FixedBytes, sofab::InlineVector) is storage the caller already owns in full. Setting a length inside it allocates nothing, so the room is the capacity;std::string, std::vector, std::array, a bound span) offers exactly size(). For a heap-growable container that is the point: the codec writes into what the caller sized and never creates room, so no allocator call can reach the wire.| D | Destination type. |
| d | The destination. |
|
constexprnoexcept |
Publish the decoded length in a destination that already had the room — the one container operation the codec performs.
Never grows: sofab::detail::destRoom has already been compared against the announced size, so n is at most what the destination holds and this is a length store (sofab::FixedString::set_len) or a shrink. A shrink cannot allocate — std::string / std::vector never reallocate for a size below the current one — so §6.6's question, "can a sender make this
allocation bigger by sending different bytes?", has no allocation to ask it of.
A destination of fixed extent that publishes neither (std::array, a bound span) keeps its extent; the value's length is the announced count the caller was told.
| d | Destination. |
| n | Decoded length, already known to fit. |
|
noexcept |
|
constexprnoexcept |
|
constexprnoexcept |
Inverse of spread7: pack eight bytes' low 7 bits into one value.
Byte i of w contributes bits [7i+6 : 7i] of the result; bit 7 of each byte (the varint continuation flag) is discarded by the masks, so the caller need not clear it. The three rounds mirror spread7's, run in the opposite order.
|
inlinenoexcept |
Load eight little-endian bytes as a 64-bit word (see storeLittle64).
|
inlinenoexcept |
Inverse of storeLittleBits.
|
constexprnoexcept |
Whether a MessageSeq template argument names the destination container rather than the element type.
MessageSeq is templated on the container, exactly as StringSeq is, which is what lets it collect into an InlineVector as well as a std::vector. It used to be templated on the ELEMENT, with std::vector<T> hard-wired as the destination, and that spelling keeps working: the two readings are told apart here, from the argument alone.
An argument names a container when its value_type is something this collector reads as an element — a message, or a row container. The two readings can only collide for an argument whose value_type is itself one of those two, i.e. an element that is a sequence of messages or a row of rows; under the element reading such an element is handed to IStreamImpl::read, which has no overload for it, so what the container reading takes over is code that never compiled.
|
inlinenoexcept |
Admit or refuse a wrapper-array element index, in §6.3's categories, before the container it indexes into is extended.
MESSAGE_SPEC §5.1 makes the array's length highest present id + 1, so the index is the length: "for a <strong>sequence array</strong> it surfaces the <strong>index</strong> of the element in hand … there being no count header to check", and "A limit <strong>MUST</strong> be enforced … before the container it indexes into is extended" (§6.2.1).
The three bounds do not share a verdict, so they are consulted in §6.3's order. A schema count shuts the receiver cap out entirely — a receiver limit "**MUST NOT** be applied to a field the schema already
bounds" (§6.2.1).
The stream applies the same three bounds one step earlier, at the element header (IStreamImpl::rejectElementIndex), which is where a truncated element cannot outrun them. This is the second gate, for a deserialize reached directly — it is a public entry point — and for a collector that publishes no element type for the stream to key on.
| is | Stream to record the refusal on. |
| id | The element index in hand. |
| cap | Schema count N, or negative. |
| dynCap | The caller's max_dyn_array_count (§6.2.1), or negative when the caller supplied none — in which case no index cap applies, because there is none to fall back on. |
| destCap | The destination container's capacity, or negative. |
true when the index may be placed.count nor a receiver cap grows to whatever index the sender picks — the amplification §6.2.1 exists to stop, from a ~9-byte field. There is nothing to fall back on (§6.2.1 forbids this codec a limit of its own), so the call is refused with Error::InvalidArgument. A destination that cannot grow is the one exemption: its capacity already bounds the allocation, so no cap is needed for the sender not to choose it.
|
constexprnoexcept |
Spread the low 56 bits of v into eight 7-bit groups, one per byte.
Byte i of the result holds bits [7i+6 : 7i] of v, with bit 7 of each byte clear — exactly the payload layout of the first eight bytes of a base-128 varint. Three halve-and-shift rounds do it in a handful of ALU ops, which is what lets the encoder emit eight varint bytes as one store instead of eight.
|
inlinenoexcept |
Store a 64-bit word as eight little-endian bytes.
The wire is little-endian (§4), so on a little-endian host this is the native representation and compiles to a single unaligned store; on a big-endian one the word is reversed first. std::byteswap is C++23, so the reversal is spelled out.
|
inlinenoexcept |
Store the object representation of a 4- or 8-byte value as little-endian bytes.
The wire is little-endian (§4), so on a little-endian host this is one unaligned store; elsewhere the bytes are laid down individually. The hand-rolled shift loop this replaces did not fold into a store on GCC and cost ten instructions per fp32 field.
| U | Unsigned type holding the bits (uint32_t or uint64_t). |
| out | Destination, with at least sizeof(U) writable bytes. |
| bits | Value to lay down. |
|
constexprnoexcept |
Step the UTF-8 DFA over len bytes and return the state.
The resumable half of utf8Valid, for a string payload that arrives across more than one feed: §6.4.4 requires that "a chunk boundary MUST
NOT affect the outcome" and permits exactly this — "A decoder MAY validate
incrementally provided it carries validator state across <tt>feed</tt> calls; no
assembly buffer is required."
UTF8_REJECT is absorbing, which is what lets the verdict be held back: §6.4.4's last row makes a byte that can begin no sequence INVALID "reported at payload completion, not before", and a decoder "MUST NOT report INVALID mid-payload … that input is INCOMPLETE until the
payload ends". The caller therefore steps the state per chunk and consults it once, when the declared length is reached — where a state other than UTF8_ACCEPT covers both a rejected byte and a sequence the payload ended in the middle of.
| state | State from the previous piece (UTF8_ACCEPT to start). |
| d | Bytes of this piece. |
| len | Their count. |
|
inlinenoexcept |
Length of the longest prefix of sv that is at most n bytes and does not end inside a UTF-8 character; sv is longer than n.
The over-long half of FixedString's assignment-time clamp, one copy for every capacity: the cut moves back while the first byte left out is a continuation byte (10xxxxxx), so a valid UTF-8 sv keeps a valid UTF-8 prefix. The common case – the value fits – stays inline in FixedString::assign, where a literal's length folds away.
|
constexprnoexcept |
|
constexprnoexcept |
Inverse of zigzagEncode.
| u | Zig-zag-encoded unsigned value. |
|
constexprnoexcept |
|
inlineconstexpr |
|
inlineconstexpr |
|
inlineconstexpr |
Validate that [data, data+len) is well-formed UTF-8.
A real UTF-8 validator, not a byte-range shortcut — this is a security surface (CORELIB_PLAN §6.4). It rejects:
C0 80 (Java "Modified UTF-8" NUL), C1-lead 2-byte forms, and the overlong 3-/4-byte E0 80.., F0 80.. forms;U+D800–U+DFFF (ED A0..ED BF);U+10FFFF (F4 90.. and any F5–FF lead);U+0000 (a single 00 byte): NUL is valid UTF-8 and representable in the length-framed payload (§6.4).constexpr and free of reinterpret_cast, so it can be evaluated at compile time and unit-tested directly.
| data | Pointer to the candidate bytes (may be nullptr iff len is 0). |
| len | Number of bytes to validate. |
true iff every byte forms part of a well-formed UTF-8 sequence.
|
inlineconstexpr |
Widest base-128 varint: ceil(64/7) == 10 bytes (§4.1).
The encoder and decoder both use it as the "one more varint definitely fits" window, so a capacity check can be hoisted out of an element loop and amortised over a whole chunk instead of paid per element.