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

SofaBuffers — pure C++20 implementation (no C backend). More...

#include <array>
#include <bit>
#include <concepts>
#include <cstdint>
#include <cstring>
#include <functional>
#include <limits>
#include <memory>
#include <span>
#include <string>
#include <string_view>
#include <type_traits>
#include <vector>
#include "sofab/bits_equal.hpp"
Include dependency graph for sofab.hpp:

Go to the source code of this file.

Classes

struct  sofab::detail::WireInt< E, bool >
 The integer type an element travels as on the wire. More...
 
struct  sofab::detail::WireInt< E, true >
 Enumeration case of WireInt: the declared underlying type. More...
 
struct  sofab::detail::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...
 
class  sofab::FixedString< N >
 Fixed-capacity, heap-free string of up to N characters. More...
 
class  sofab::FixedBytes< N >
 Fixed-capacity, heap-free byte blob of up to N bytes. More...
 
class  sofab::InlineVector< T, N >
 Fixed-capacity, heap-free sequence of up to N elements of type T. More...
 
class  sofab::OStreamImpl
 Base of the output streams: encodes values into a caller-provided buffer. More...
 
class  sofab::OStreamImpl::PendingRun
 The held-back sequence run: ids of the innermost open sequences whose header has not been written yet (sequenceBeginLazy). More...
 
class  sofab::OStreamImpl::Result
 Chainable result of a write operation. More...
 
class  sofab::OStream
 Output stream over a caller-supplied buffer whose lifetime is shared. More...
 
class  sofab::OStreamView
 Output stream over a buffer the caller already owns. More...
 
class  sofab::OStreamInline< N, Offset >
 Output stream whose buffer is stored inline (the buffer costs no heap allocation). More...
 
class  sofab::OStreamMessage
 Base class for user-defined messages that can serialise themselves. More...
 
class  sofab::OStreamObject< MessageType, N, Offset >
 Bundles a message with an inline output buffer sized for it. More...
 
struct  sofab::Limits
 The one stream-scoped receiver cap: how many bytes a single top-level field may span. More...
 
struct  sofab::ElemBound
 The value range a declared integer type admits (MESSAGE_SPEC §7.1). More...
 
class  sofab::IStreamImpl
 Base of the input streams: decodes fields from fed bytes. More...
 
class  sofab::IStreamImpl::Result
 Three-valued outcome of a feed call (spec §7). More...
 
struct  sofab::IStreamImpl::FieldCb
 Decode fields at the current nesting level, firing cb for each. More...
 
class  sofab::IStreamInline
 Input stream that delivers each top-level field to a user callback. More...
 
class  sofab::IStreamMessage
 Base class for user-defined messages that can deserialise themselves. More...
 
struct  sofab::Message
 A message that both encodes and decodes: exactly OStreamMessage + IStreamMessage. More...
 
class  sofab::IStreamObject< MessageType >
 Holds a message and routes decoded top-level fields into it. More...
 
class  sofab::IStreamInto< MessageType >
 Decoder that decodes into a message instance the CALLER owns. More...
 
struct  sofab::detail::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  sofab::StringSeq< C >
 Collects the elements of a string wrapper-sequence array. More...
 
struct  sofab::BlobSeq< C >
 The blob counterpart of StringSeq; see it for the placement and bound rules. More...
 
struct  sofab::MessageSeq< A >
 Collects a struct/union or nested-array wrapper sequence into the container it is handed. More...
 
struct  sofab::RowSeq< C, Reader >
 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...
 

Namespaces

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

Concepts

concept  sofab::OutputMessage
 Constrains a serialisable message: derives from sofab::OStreamMessage and exposes a static constexpr std::size_t _maxSize.
 
concept  sofab::InputMessage
 Constrains a deserialisable message: must derive from sofab::IStreamMessage.
 
concept  sofab::detail::sizable
 Give dst room for want, if it is a destination the caller has to size.
 

Macros

#define SOFAB_STRICT_UTF8   1
 Compile-time gate for strict UTF-8 validation of string payloads (spec MESSAGE_SPEC §8, CORELIB_PLAN §6.4).
 

Typedefs

using sofab::id = uint32_t
 Field identifier on the wire.
 
template<typename E >
using sofab::detail::WireInt_t = typename WireInt< E >::type
 Alias for WireInt.
 

Enumerations

enum class  sofab::Error {
  sofab::None = 0 , sofab::BufferFull = 3 , sofab::InvalidArgument = 1 , sofab::InvalidMessage = 4 ,
  sofab::Incomplete = 5 , sofab::LimitExceeded = 6
}
 Status codes returned by the encode/decode API; None signals success. More...
 
enum class  sofab::DecodeStatus {
  sofab::Complete = 0 , sofab::Incomplete = 1 , sofab::Invalid = 2 , sofab::LimitExceeded = 3 ,
  sofab::InvalidArgument = 4
}
 Three-valued decode outcome of a sofab::IStreamImpl::feed call (spec §7). More...
 
enum class  sofab::detail::Wire : uint8_t {
  sofab::detail::Unsigned = 0 , sofab::detail::Signed = 1 , sofab::detail::Fixlen = 2 , sofab::detail::ArrayUnsigned = 3 ,
  sofab::detail::ArraySigned = 4 , sofab::detail::ArrayFixlen = 5 , sofab::detail::SequenceStart = 6 , sofab::detail::SequenceEnd = 7
}
 Wire type stored in the low 3 bits of every field header. More...
 
enum class  sofab::detail::Fix : uint8_t { sofab::detail::Fp32 = 0 , sofab::detail::Fp64 = 1 , sofab::detail::String = 2 , sofab::detail::Blob = 3 }
 Sub-type of a length-prefixed (Fixlen) payload, stored in the low 3 bits of its length word. More...
 

Functions

void sofab::detail::storeLittle64 (uint8_t *out, uint64_t w) noexcept
 Store a 64-bit word as eight little-endian bytes.
 
constexpr uint64_t sofab::detail::spread7 (uint64_t v) noexcept
 Spread the low 56 bits of v into eight 7-bit groups, one per byte.
 
constexpr uint64_t sofab::detail::gather7 (uint64_t w) noexcept
 Inverse of spread7: pack eight bytes' low 7 bits into one value.
 
template<std::unsigned_integral U>
void sofab::detail::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 sofab::detail::loadLittleBits (const uint8_t *in) noexcept
 Inverse of storeLittleBits.
 
uint64_t sofab::detail::loadLittle64 (const uint8_t *in) noexcept
 Load eight little-endian bytes as a 64-bit word (see storeLittle64).
 
constexpr uint64_t sofab::detail::zigzagEncode (int64_t v) noexcept
 Map a signed integer to an unsigned one with the zig-zag scheme.
 
constexpr int64_t sofab::detail::zigzagDecode (uint64_t u) noexcept
 Inverse of zigzagEncode.
 
template<std::floating_point F>
constexpr auto sofab::detail::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 sofab::detail::bitsFloat (U bits) noexcept
 Reconstruct a floating-point value from its raw bits.
 
constexpr uint32_t sofab::detail::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 sofab::detail::utf8Valid (const char *data, size_t len) noexcept
 
template<typename T >
constexpr long sofab::detail::destCapacity () noexcept
 The element ceiling a decode destination T publishes, or -1 when it publishes none.
 
template<typename D >
constexpr size_t sofab::detail::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 sofab::detail::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.
 
constexpr bool sofab::utf8_valid (std::string_view bytes) noexcept
 Public UTF-8 validity primitive (spec CORELIB_PLAN §6.4).
 
std::size_t sofab::detail::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 sofab::detail::fitDest (D &dst, size_t want) noexcept
 
size_t sofab::detail::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 sofab::detail::arrayTagMatches (const IStreamImpl &is) noexcept
 Does the delivered tag match the array kind Elem declares (§7.3)?
 
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.
 
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.
 
template<typename B >
bool sofab::readBlob (IStreamImpl &is, B &dst, long maxlen) noexcept
 The blob counterpart of sofab::readString.
 
template<typename B >
bool sofab::readBlobCapped (IStreamImpl &is, B &dst, long dynCap) noexcept
 The blob counterpart of sofab::readStringCapped.
 
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.
 
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.
 
template<typename T >
bool sofab::read (IStreamImpl &is, T &dst) noexcept
 IStreamImpl::read with the destination sized first — the generated layer's untyped read.
 
bool sofab::detail::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 sofab::detail::namesSeqContainer () noexcept
 Whether a MessageSeq template argument names the destination container rather than the element type.
 

Variables

constexpr int sofab::API_VERSION = 1
 Version of the SofaBuffers public API implemented by this header.
 
constexpr uint32_t sofab::ID_MAX = 0x7fffffffu
 Largest valid field id (INT32_MAX).
 
constexpr uint32_t sofab::FIXLEN_MAX = 0x7fffffffu
 Largest fixlen payload byte-length (INT32_MAX).
 
constexpr uint32_t sofab::ARRAY_MAX = 0x7fffffffu
 Largest array element count (INT32_MAX).
 
constexpr int sofab::MAX_DEPTH = 255
 Maximum nested-sequence depth (§4.9).
 
constexpr size_t sofab::MIN_OUTPUT_BUFFER = 1
 Smallest output buffer this port accepts for streaming (§5.1).
 
template<typename >
constexpr bool sofab::always_false_v = false
 Always-false trait used to trigger static_assert in the otherwise-unreachable branch of an if constexpr chain.
 
constexpr size_t sofab::detail::VARINT_MAX_BYTES = 10
 Widest base-128 varint: ceil(64/7) == 10 bytes (§4.1).
 
constexpr uint8_t sofab::detail::utf8Dfa []
 Validate that [data, data+len) is well-formed UTF-8.
 
constexpr uint32_t sofab::detail::UTF8_ACCEPT = 0
 The DFA's ACCEPT and REJECT states (see utf8Dfa).
 
constexpr uint32_t sofab::detail::UTF8_REJECT = 12
 

Detailed Description

SofaBuffers — pure C++20 implementation (no C backend).

A from-scratch, header-only implementation of the SofaBuffers wire format in modern C++20. It mirrors the public API of the C-backed sofab/sofab.hpp (same sofab::OStream / sofab::IStream* surface) but shares no code with the C library — encoding and decoding are implemented directly here.

Design:

  • Encoding stays fully streamable: an OStream writes into a caller buffer and invokes a flush callback when it fills, so a message can exceed RAM.
  • Decoding is optimised for the common case where the whole message is already in contiguous memory: a protobuf-style cursor advances a pointer over the buffer (no per-byte state machine). The streaming feed() API is retained — bytes are accumulated and complete top-level fields are dispatched as they become available — so chunked input still works.
  • Modern techniques: std::span, std::bit_cast, concepts, if constexpr, [[nodiscard]]. Endianness is handled explicitly (LE on the wire) so no host-endian branching is needed.

SPDX-License-Identifier: MIT

Definition in file sofab.hpp.