SofaBuffers C++ 0.12.0
Pure-C++20 SofaBuffers — API Documentation
Loading...
Searching...
No Matches
sofab::IStreamInto< MessageType > Class Template Reference

Decoder that decodes into a message instance the CALLER owns. More...

#include <sofab.hpp>

Inheritance diagram for sofab::IStreamInto< MessageType >:
[legend]
Collaboration diagram for sofab::IStreamInto< MessageType >:
[legend]

Public Member Functions

 IStreamInto (MessageType &message, Limits limits) noexcept
 Bind message and route each top-level field into it.
 
- Public Member Functions inherited from sofab::IStreamImpl
 IStreamImpl (const IStreamImpl &)=delete
 
IStreamImpl & operator= (const IStreamImpl &)=delete
 
 IStreamImpl (IStreamImpl &&) noexcept=default
 
IStreamImpl & operator= (IStreamImpl &&) noexcept=default
 
virtual ~IStreamImpl ()=default
 
Result feed (const uint8_t *buffer, size_t buflen) noexcept
 Feed bytes into the decoder, delivering every complete top-level field.
 
virtual void reset () noexcept
 Drop every byte of decoder state, so the next feed starts a brand-new message.
 
void invalidate () noexcept
 Mark the running decode INVALID from inside a deliver callback.
 
void exceedLimit () noexcept
 Report a receiver-side decode-limit violation from inside a deliver callback.
 
void rejectDestination () noexcept
 Refuse the current field because the caller's destination is too short for it (§6.3's third refusal tier, §6.6.3).
 
void rejectUnbounded () noexcept
 Refuse the current field because the call stated no bound for it at all (§6.3's InvalidArgument).
 
void rejectElementIndex (sofab::id bad) noexcept
 Refuse a wrapper-array element whose index is at or past a bound, in the category §6.3 gives that bound.
 
size_t skipped () const noexcept
 Number of fields skipped this stream because their wire tag contradicted the type the callback asked for (MESSAGE_SPEC §7.3).
 
template<typename T >
bool read (T &value) noexcept
 Read the current field's value, dispatching on value's type.
 
template<typename S , typename Sizer = detail::NoSizer>
bool readString (S &value, long bound, Sizer sizer={}) noexcept
 Read the current field as a string, or skip it (§7.3).
 
template<typename S , typename Sizer = detail::NoSizer>
bool readStringCapped (S &value, long dynCap, Sizer sizer={}) noexcept
 Read the current field as a schema-**unbounded** string under the receiver's max_dyn_string_len (§6.2.1).
 
template<typename B , typename Sizer = detail::NoSizer>
bool readBlob (B &value, long bound, Sizer sizer={}) noexcept
 Read the current field as a blob, or skip it (§7.3).
 
template<typename B , typename Sizer = detail::NoSizer>
bool readBlobCapped (B &value, long dynCap, Sizer sizer={}) noexcept
 Read the current field as a schema-**unbounded** blob under the receiver's max_dyn_blob_len (§6.2.1).
 
template<typename T , typename Sizer = detail::NoSizer>
bool readArray (T &dst, long schemaCount, ElemBound elem={}, Sizer sizer={}) noexcept
 Read a count-prefixed native array, applying every receiver-side decision at the one word that decides it.
 
template<typename T , typename Sizer = detail::NoSizer>
bool readArrayCapped (T &dst, long dynCap, ElemBound elem={}, Sizer sizer={}) noexcept
 Read a schema-**unbounded** count-prefixed array under the receiver's max_dyn_array_count (§6.2.1).
 
size_t read (void *dst, size_t maxlen) noexcept
 Read the current blob field into a caller buffer.
 
bool consumed () const noexcept
 
Wire wire () const noexcept
 Wire type of the field currently being delivered.
 
Fix fixType () const noexcept
 Fixlen sub-type of the field currently being delivered.
 
size_t announcedSize () const noexcept
 The payload length the current field's header announces — a string/blob byte count, or a fixlen array's element width.
 
size_t announcedCount () const noexcept
 The element count the current array field's header announces.
 
size_t available () const noexcept
 Bytes of this field readable right now, in the chunk being parsed.
 
size_t progress () const noexcept
 How much of the current field earlier chunks already delivered — payload bytes written, or array elements decoded.
 
bool resumed () const noexcept
 true when this delivery re-enters a sequence an earlier feed left open, rather than announcing a new field.
 
bool acceptsString (long bound) noexcept
 The §7.3 and §7.1 decisions readString makes, taken before a destination exists.
 
bool acceptsStringCapped (long dynCap) noexcept
 acceptsString for a schema-unbounded element, under the receiver's max_dyn_string_len (§6.2.1).
 
bool acceptsBlob (long bound) noexcept
 The §7.3 and §7.1 decisions readString makes, taken before a destination exists.
 
bool acceptsBlobCapped (long dynCap) noexcept
 acceptsString for a schema-unbounded element, under the receiver's max_dyn_string_len (§6.2.1).
 

Additional Inherited Members

- Protected Types inherited from sofab::IStreamImpl
enum class  Entry : uint8_t { Fresh , Descend , Resume }
 How a level is being entered (see levelEntry). More...
 
enum class  Field : uint8_t { Ready , SkipElem , Stop }
 What beginField found at this level's next field. More...
 
- Protected Member Functions inherited from sofab::IStreamImpl
 IStreamImpl (Limits limits) noexcept
 Construct with the receiver's Limits; a derived class installs topCallback_.
 
bool tagMatches (Wire wantWire) noexcept
 The §7.3 seam: does the delivered field's wire tag match the one the caller's read declares?
 
bool tagMatches (Wire wantWire, Fix wantFix) noexcept
 
bool tagMatches (Wire wantWire, Fix wantFix, Fix wantFix2) noexcept
 
template<typename D >
bool readPayload (D &value, bool validateUtf8) noexcept
 Deliver the current fixlen payload into the destination the caller handed over (§6.6.3, second shape).
 
template<typename D >
bool roomFor (D &value, size_t need) noexcept
 Is there room for need bytes in the destination the caller handed over (§6.6.3)?
 
template<typename D >
bool readPayloadPiece (D &value, bool validateUtf8, size_t avail) noexcept
 The chunk-straddling half of readPayload — deliver the piece that has arrived and remember how far the value got.
 
bool capCannotFire (size_t n) const noexcept
 Hoisted form of exceedsBuffer for a whole window of n bytes: true when no field of it can cross the budget.
 
bool exceedsBuffer (size_t consumed, uint64_t need) const noexcept
 Would buffering this field cross maxBufferedField_?
 
Entry levelEntry () noexcept
 Decide how this level is being entered, and clear the replay state once the suspended level is reached.
 
template<typename Cb >
void dispatchLevel (Cb &&cb, bool stopAtEnd) noexcept
 
Field beginField (bool stopAtEnd) noexcept
 Parse the next field's header and metadata at a nested level, and apply every rule that is decided before the value.
 
bool finishField (const uint8_t *fieldStart, const uint8_t *payload, sofab::id fieldId) noexcept
 Settle a field the callback has been offered: skip it if the callback did not want it, suspend it if its bytes ran out.
 
void closeLevel (bool stopAtEnd) noexcept
 The bytes ran out with this sequence still open — INCOMPLETE, not malformed (§7).
 
bool reenterLevel (FieldCb cb, Entry entry) noexcept
 Take the Entry::Descend or Entry::Resume path.
 
void suspendHere (sofab::id fieldId, bool declined) noexcept
 Record that this level's current field is the suspension point.
 
void suspendAtBoundary () noexcept
 Claim the suspension for this level at a field boundary — nothing half-delivered, only a header cut in two or a sequence still open.
 
void restorePending () noexcept
 Put the suspended field's header back before continuing it.
 
bool deliverPending (FieldCb cb, bool declined) noexcept
 Re-deliver the half-delivered field at this level.
 
bool descendResume (FieldCb cb) noexcept
 Re-descend one recorded level of pathId_ towards suspendDepth_.
 
void skipPayload () noexcept
 Step over the payload of the current field, resumably.
 
void skipSequence () noexcept
 Walk a sub-sequence nobody asked for, delivering nothing.
 
size_t spanned () const noexcept
 The bytes of the current top-level field seen so far, across every chunk it spans (#26, Limits::max_buffered_field).
 
size_t spannedLive () const noexcept
 spanned less what this field spent on skipped subtrees — the number the field-span cap is measured against (§6.2.1).
 
void discountSkipped (const uint8_t *from) noexcept
 Discount a skipped field's bytes from the live span (§6.2.1).
 
Result latchTerminal () noexcept
 Latch the refusal this feed produced, in §6.3's order of tiers.
 
void stash (const uint8_t *p, size_t n) noexcept
 Hold the ≤ CARRY_CAP bytes of a cut item until the next feed.
 
size_t parseWindow (const uint8_t *p, size_t n) noexcept
 Parse one window of bytes, resuming whatever the last one left open, and report how much of it was consumed.
 
void parseTopLevel () noexcept
 Deliver every complete top-level field in the current window, leaving p_ at the first byte not consumed.
 
bool parseFieldTag () noexcept
 Parse a field header word into fieldId_ and type_ (§4.3).
 
bool parseFieldMeta () noexcept
 Parse the word(s) between a field header and its payload — the fixlen word (§4.6), the array count (§4.7) or both (§4.8).
 
bool parseFieldHeader () noexcept
 Parse one top-level field header plus the metadata that precedes its payload, into the current-field members.
 
bool exceedsBufferAtHeader () noexcept
 Would this field cross maxBufferedField_, judged from its header alone (#26)?
 
template<bool Bounded, typename Elem >
bool readIntElements (std::span< Elem > sp, ElemBound b={}) noexcept
 Decode the current integer-array payload: count_ varint elements, the first sp.size() of them into sp.
 
template<typename S , typename Sizer >
bool readStringGated (S &value, long bound, long dynCap, Sizer sizer) noexcept
 The body both string reads share, so the two cannot answer differently.
 
template<typename B , typename Sizer >
bool readBlobGated (B &value, long bound, long dynCap, Sizer sizer) noexcept
 The body both blob reads share.
 
template<typename T , typename Sizer >
bool readArrayGated (T &dst, long schemaCount, long dynCap, ElemBound elem, Sizer sizer) noexcept
 The body both array reads share.
 
bool acceptsStringGated (long bound, long dynCap) noexcept
 The body both string accepts share.
 
bool acceptsBlobGated (long bound, long dynCap) noexcept
 The body both blob accepts share.
 
- Static Protected Member Functions inherited from sofab::IStreamImpl
static bool getVarintWindowed (const uint8_t *&p, uint64_t &out, bool *overflow) noexcept
 Read one varint, given that a full varint window is in the buffer.
 
static bool skipVarintWindowed (const uint8_t *&p, bool *overflow) noexcept
 getVarintWindowed without building the value — see skipVarint.
 
static bool getVarint (const uint8_t *&p, const uint8_t *end, uint64_t &out, bool *overflow=nullptr) noexcept
 Read one base-128 varint, advancing the cursor (bounds-checked).
 
static bool skipVarint (const uint8_t *&p, const uint8_t *end, bool *overflow=nullptr) noexcept
 Advance the cursor past one varint without decoding it (bounds-checked).
 
static bool fixlenWordValid (uint64_t word) noexcept
 Validate a single Fixlen field's length-and-subtype word (§4.6, §6.2).
 
static bool arrayFixlenWordValid (uint64_t word) noexcept
 Validate a ArrayFixlen element word (§4.8, §6.2).
 
template<std::floating_point F>
static F loadFloat (const uint8_t *p) noexcept
 Load a little-endian float or double from raw bytes.
 
template<typename Cb >
static FieldCb wrapCb (Cb &cb) noexcept
 Wrap a level's callback as a FieldCb.
 
static constexpr bool schemaBoundsHeader (Wire w) noexcept
 Does this wire type's header state a size the schema can bound?
 
- Protected Attributes inherited from sofab::IStreamImpl
uint8_t carry_ [CARRY_CAP]
 The prefix of one cut item, carried to the next feed.
 
size_t carryLen_ = 0
 Live bytes of carry_.
 
int suspendDepth_ = -1
 Depth at which the parse is suspended, or -1 when nothing is.
 
bool replay_ = false
 Re-descending pathId_ to reach suspendDepth_.
 
sofab::id pathId_ [MAX_DEPTH+1]
 Id of the sequence field entered at each open level (§4.9).
 
bool pathSkip_ [MAX_DEPTH+1]
 That level was entered by skipPayload, not by a handler.
 
bool pend_ = false
 A field at suspendDepth_ is half-delivered.
 
sofab::id pendId_ = 0
 That field's header, kept apart from the live type_ / fixLen_ / count_.
 
Wire pendType_ {}
 That field's header, kept apart from the live type_ / fixLen_ / count_.
 
Fix pendFix_ {}
 That field's header, kept apart from the live type_ / fixLen_ / count_.
 
size_t pendLen_ = 0
 That field's header, kept apart from the live type_ / fixLen_ / count_.
 
size_t pendCount_ = 0
 That field's header, kept apart from the live type_ / fixLen_ / count_.
 
bool pendSkip_ = false
 ...and it was declined, so skipPayload resumes it rather than the handler.
 
size_t pendDone_ = 0
 Progress through the pending field: payload bytes already written into the caller's destination, or array elements already decoded.
 
uint32_t utf8State_ = 0
 Incremental UTF-8 DFA state for the string payload in progress (§6.4.4).
 
size_t spanCarry_ = 0
 Bytes of the current top-level field consumed in earlier chunks (#26).
 
const uint8_t * spanBase_ = nullptr
 Where that field's span continues in the window being parsed (#26).
 
bool spanSkip_ = false
 Is the parse currently inside a skipped sequence subtree (§6.2.1, #129)?
 
bool capInert_ = false
 Can the field-span cap provably not fire anywhere in the window being parsed (§6.2.1, #26)? Set once per parseWindow.
 
size_t spanSkipped_ = 0
 Bytes of the current top-level field spent on SKIPPED subtrees (§6.2.1, #129 one level down).
 
const uint8_t * p_ = nullptr
 Read cursor.
 
const uint8_t * end_ = nullptr
 One past the last readable byte.
 
Wire type_ {}
 Wire type of the field being delivered.
 
Fix fixType_ {}
 Sub-type of the current fixlen field.
 
size_t fixLen_ = 0
 Payload length (fixlen) or element size (fixlen array), in bytes.
 
size_t count_ = 0
 Element count of the current array field.
 
bool consumed_ = false
 Set once the callback has read the current field's value.
 
bool error_ = false
 Sticky "this feed was refused" flag, and the only one the parse loops test.
 
bool limitExceeded_ = false
 Category: a receiver-side limit was crossed (§6.2.1).
 
bool argError_ = false
 Category: a well-formed value did not fit the destination the caller handed over (§6.3's third refusal tier, §6.6.3).
 
Error terminal_ = Error::None
 The stream's latched terminal verdict, or Error::None while the stream is still usable (§5.2, §6.3).
 
int seqDepth_ = 0
 Current nested-sequence depth during dispatch (§4.9 MAX_DEPTH).
 
size_t skipped_ = 0
 §7.3 type-mismatch skips seen so far (skipped).
 
bool incomplete_ = false
 The field being delivered needs more bytes (§7 INCOMPLETE, not malformed).
 
long elemBound_ = -1
 The tightest armed element-index bound of the wrapper sequence being read (§5.1); -1 = none.
 
long elemSchema_ = -1
 Schema count N: an id at or past it is InvalidMessage (§7.1).
 
long elemDyn_ = -1
 Configured max_dyn_array_count: LimitExceeded (§6.2.1).
 
long elemDest_ = -1
 The destination container's own capacity: InvalidArgument (§6.6.3).
 
int elemWire_ = -1
 §7.3 wire type its elements must carry (a Wire as int); -1 = the collector decides the bound itself.
 
int elemFix_ = -1
 §7.3 fixlen subtype for that element type (a Fix as int); -1 = the element type has none.
 
sofab::id fieldId_ = 0
 Id of the field being delivered.
 
size_t maxBufferedField_
 The receiver's byte budget for one top-level field (Limits::max_buffered_field), as this decode was told it.
 
std::function< void(sofab::id, size_t, size_t)> topCallback_
 Delivers each top-level field.
 
- Static Protected Attributes inherited from sofab::IStreamImpl
static constexpr size_t CARRY_CAP = 3 * detail::VARINT_MAX_BYTES
 Widest prefix of a wire item that can outlive a chunk boundary, derived from this format's own constants (§6.6).
 
static constexpr uint64_t CAP_NEED_MAX
 Upper bound of any header-declared need (§6.2 ceilings).
 

Detailed Description

template<InputMessage MessageType>
class sofab::IStreamInto< MessageType >

Decoder that decodes into a message instance the CALLER owns.

IStreamObject with the instance taken out: it routes each top-level field into the message handed to the constructor instead of one it holds itself. That is the whole difference — same callback, same dispatch — and it is what a caller who already has a destination needs, which is every generated try_decode (sofa-buffers/generator#744). The alternative, a hand-written IStreamInline whose callback forwards to the destination, needs the stream inside its own callback and so cannot capture it; this class is that shape without the knot.

Warning
One message per stream, unless the destination is put back. Everything IStreamObject warns about applies here, for the same reason — the message is populated in place and MESSAGE_SPEC §2 leaves an all-default field out of the bytes, so a field the next message does not carry keeps the previous one's value. The difference is who clears it: the destination is the caller's, so reset here resets the DECODER only and the caller puts the message back (generated code calls the message's own reset(), which keeps the buffers it has already paid for — the reason to hand a destination in at all). A rejected input likewise leaves the fields decoded before the error in it.

The message must outlive the stream.

Telemetry msg; // kept across messages
msg.reset();
auto r = in.feed(bytes, len);
Result feed(const uint8_t *buffer, size_t buflen) noexcept
Feed bytes into the decoder, delivering every complete top-level field.
Definition sofab.hpp:4331
Decoder that decodes into a message instance the CALLER owns.
Definition sofab.hpp:6304
Template Parameters
MessageTypeA type satisfying sofab::InputMessage.

Definition at line 6303 of file sofab.hpp.

Constructor & Destructor Documentation

◆ IStreamInto()

template<InputMessage MessageType>
sofab::IStreamInto< MessageType >::IStreamInto ( MessageType &  message,
Limits  limits 
)
inlineexplicitnoexcept

Bind message and route each top-level field into it.

Parameters
messageThe destination; must outlive the stream.
limitsThe receiver's field-span budget (see Limits). Required: §6.2.1 gives this library no number to default to.

Definition at line 6314 of file sofab.hpp.


The documentation for this class was generated from the following file: