SofaBuffers C/C++ 0.12.0
API Documentation
Loading...
Searching...
No Matches
sofab::OStreamImpl Class Reference

Base output stream: encodes fields into a caller-provided buffer. More...

#include <sofab.hpp>

Inheritance diagram for sofab::OStreamImpl:
[legend]
Collaboration diagram for sofab::OStreamImpl:
[legend]

Data Structures

class  Result
 Outcome of an encode operation, supporting fluent chaining. More...
 

Public Types

using flushCallback = void(*)(std::span< const uint8_t >)
 Callback invoked with the bytes to flush (buffer full or on flush()).
 

Public Member Functions

 OStreamImpl (const OStreamImpl &)=delete
 Copy construction is deleted (the context owns raw buffer pointers).
 
OStreamImpl & operator= (const OStreamImpl &)=delete
 Copy assignment is deleted (the context owns raw buffer pointers).
 
 OStreamImpl (OStreamImpl &&) noexcept=default
 Move construction (transfers the underlying context).
 
OStreamImpl & operator= (OStreamImpl &&) noexcept=default
 Move assignment (transfers the underlying context).
 
virtual ~OStreamImpl () noexcept
 Destructor; flushes any buffered bytes.
 
size_t flush () noexcept
 Flush buffered bytes through the flush callback (if any).
 
size_t bytesUsed () noexcept
 Number of bytes written to the active buffer since the last flush.
 
const uint8_t * data () const noexcept
 Pointer to the start of the active encode buffer.
 
template<typename T >
Result write (sofab_id_t id, const T &value) noexcept
 Encode a single field, deducing the wire type from T.
 
template<typename T >
Result writeLazy (sofab_id_t id, const T &value) noexcept
 Encode a nested message field, omitting it when all-default.
 
Result write (sofab_id_t id, const void *value, int32_t size) noexcept
 Encode a raw binary blob field.
 
template<typename T >
Result writeIf (sofab_id_t id, const T &value, bool condition) noexcept
 Encode a field only when condition is true.
 
Result sequenceBeginLazy (sofab_id_t id) noexcept
 Open a nested sequence whose header is held back until it turns out to have content.
 
Result sequenceEnd () noexcept
 Close the current nested sequence, letting it vanish if it got no content (MESSAGE_SPEC §2).
 
Result sequenceEndKeep () noexcept
 Close the current nested sequence, keeping its frame even without content.
 
bool ok () const noexcept
 Whether every write on this stream has succeeded.
 
Error error () const noexcept
 The first failure this stream saw, or Error::None.
 
Result rejectArgument () noexcept
 Refuse the value being encoded: latch Error::InvalidArgument.
 

Protected Member Functions

void onFlushCallback (size_t len) noexcept
 Invoke the user flush callback (if any) with len buffered bytes.
 
 OStreamImpl () noexcept=default
 

Static Protected Member Functions

static void static_flush_callback (sofab_ostream_t *ctx, const uint8_t *data, size_t len, void *usrptr) noexcept
 C-ABI flush trampoline: forwards to onFlushCallback() via usrptr.
 

Protected Attributes

sofab_ostream_t ctx_
 Underlying C output stream context.
 
uint8_t * buffer_
 Pointer to the active encode buffer.
 
flushCallback flushCallback_ {}
 Optional user flush callback.
 
uint8_t failed_ = 0
 Sticky: first failing write's sofab_ret_t (0 = none).
 

Detailed Description

Base output stream: encodes fields into a caller-provided buffer.

Thin C++ facade over sofab_ostream_t. The write() overloads deduce the wire encoding from the C++ type, and each returns a Result that can be chained fluently. Concrete buffer ownership is added by the derived OStream / OStreamInline classes; this base is not constructed directly.

Definition at line 890 of file sofab.hpp.

Member Typedef Documentation

◆ flushCallback

Callback invoked with the bytes to flush (buffer full or on flush()).

A std::function hosted, a plain function pointer freestanding (where std::function is not available — it type-erases onto the heap). Both spellings are contextually convertible to bool and callable, which is all this class asks of the type. The freestanding form accepts a captureless lambda or a free function; carry state through a global or through the object the stream lives in.

Definition at line 904 of file sofab.hpp.

Constructor & Destructor Documentation

◆ OStreamImpl() [1/3]

sofab::OStreamImpl::OStreamImpl ( )
protecteddefaultnoexcept

◆ OStreamImpl() [2/3]

sofab::OStreamImpl::OStreamImpl ( const OStreamImpl &  )
delete

Copy construction is deleted (the context owns raw buffer pointers).

◆ OStreamImpl() [3/3]

sofab::OStreamImpl::OStreamImpl ( OStreamImpl &&  )
defaultnoexcept

Move construction (transfers the underlying context).

◆ ~OStreamImpl()

virtual sofab::OStreamImpl::~OStreamImpl ( )
inlinevirtualnoexcept

Destructor; flushes any buffered bytes.

Definition at line 1146 of file sofab.hpp.

Here is the call graph for this function:

Member Function Documentation

◆ bytesUsed()

size_t sofab::OStreamImpl::bytesUsed ( )
inlinenoexcept

Number of bytes written to the active buffer since the last flush.

Returns
Bytes currently used in the buffer.

Definition at line 1164 of file sofab.hpp.

Here is the call graph for this function:

◆ data()

const uint8_t * sofab::OStreamImpl::data ( ) const
inlinenoexcept

Pointer to the start of the active encode buffer.

Returns
Read-only pointer to the buffer (valid for bytesUsed bytes).

Definition at line 1173 of file sofab.hpp.

Here is the caller graph for this function:

◆ error()

Error sofab::OStreamImpl::error ( ) const
inlinenoexcept

The first failure this stream saw, or Error::None.

Returns
The error code of the first failing write.

Definition at line 1583 of file sofab.hpp.

◆ flush()

size_t sofab::OStreamImpl::flush ( )
inlinenoexcept

Flush buffered bytes through the flush callback (if any).

Returns
Number of bytes flushed.

Definition at line 1155 of file sofab.hpp.

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

◆ ok()

bool sofab::OStreamImpl::ok ( ) const
inlinenoexcept

Whether every write on this stream has succeeded.

Sticky and independent of how the writes were issued — chained or one at a time with the result discarded. The usual reason it turns false is Error::BufferFull on a stream over caller storage (OStreamView), where the destination can be smaller than the message.

Returns
true while no write has failed.

Definition at line 1577 of file sofab.hpp.

Here is the caller graph for this function:

◆ onFlushCallback()

void sofab::OStreamImpl::onFlushCallback ( size_t  len)
inlineprotectednoexcept

Invoke the user flush callback (if any) with len buffered bytes.

Definition at line 918 of file sofab.hpp.

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

◆ operator=() [1/2]

OStreamImpl & sofab::OStreamImpl::operator= ( const OStreamImpl &  )
delete

Copy assignment is deleted (the context owns raw buffer pointers).

◆ operator=() [2/2]

OStreamImpl & sofab::OStreamImpl::operator= ( OStreamImpl &&  )
defaultnoexcept

Move assignment (transfers the underlying context).

◆ rejectArgument()

Result sofab::OStreamImpl::rejectArgument ( )
inlinenoexcept

Refuse the value being encoded: latch Error::InvalidArgument.

The hook a generated serialize() calls when a field's value is past a bound only the schema knows – a string or blob longer than its maxlen, an array with more elements than its count. The corelib holds no schema bound; it records the refusal (first failure wins), so that ok and error report it like any other failed write and no byte of the message is returned as a success. Writes nothing itself. Generated code tests ENCODE_BOUNDS first, so SOFAB_DISABLE_ENCODE_BOUNDS compiles every such check away.

Cold and out of line: every guard in a generated serialize() then shares one call instead of its own inlined latch (measured on ARMv6-m: 202 B instead of 300 B of .text for the 18 guards of the bench schema), and the rare path stays out of the hot one.

Returns
A Result carrying Error::InvalidArgument.

Definition at line 1607 of file sofab.hpp.

◆ sequenceBeginLazy()

Result sofab::OStreamImpl::sequenceBeginLazy ( sofab_id_t  id)
inlinenoexcept

Open a nested sequence whose header is held back until it turns out to have content.

MESSAGE_SPEC §2 omits a sequence-typed field whose value equals its declared default, and "not one child was written" is exactly that condition – evaluated per child field, recursively, for free. Closing such a sequence with nothing in it emits nothing instead of a two-byte empty frame, so an all-default message becomes the empty byte string. The predicate never touches a byte image, so struct padding cannot influence it.

Where an empty frame carries meaning, close with sequenceEndKeep instead: the last element of a wrapper array (§5.1 — the interior is sparse and drops like any default field), the §4.9 empty-sequence primitive itself, and the explicitly empty array of a field that declares a non-empty default (§2, §3). The wrapper deliberately exposes no eager opener — the frame-or-not decision belongs to the closer, which is the one call site that knows the position in the schema. (The C core keeps sofab_ostream_write_sequence_begin() for sofab_object_encode().)

Bounded: at most SOFAB_LAZY_SEQ_DEPTH (default 8) headers are held back at once; a sequence opened deeper than that is framed eagerly and keeps an empty frame it could have dropped — well-formed and value-identical, but not canonical. See SOFAB_LAZY_SEQ_DEPTH in sofab/ostream.h.

Parameters
idField identifier of the sequence.
Returns
Result for fluent chaining and error inspection.

Definition at line 1516 of file sofab.hpp.

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

◆ sequenceEnd()

Result sofab::OStreamImpl::sequenceEnd ( )
inlinenoexcept

Close the current nested sequence, letting it vanish if it got no content (MESSAGE_SPEC §2).

Returns
Result for fluent chaining and error inspection.

Definition at line 1526 of file sofab.hpp.

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

◆ sequenceEndKeep()

Result sofab::OStreamImpl::sequenceEndKeep ( )
inlinenoexcept

Close the current nested sequence, keeping its frame even without content.

Emits the held-back headers – this frame's and every enclosing one's – and then the end marker, so an empty sequence reaches the wire as begin + end. Required wherever the frame carries information beyond its contents: a wrapper-array ELEMENT (presence carries a dynamic array's length, MESSAGE_SPEC §5.1), and an array field already known to differ from a non-empty declared default (§2, §3).

Returns
Result for fluent chaining and error inspection.

Definition at line 1544 of file sofab.hpp.

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

◆ static_flush_callback()

static void sofab::OStreamImpl::static_flush_callback ( sofab_ostream_t *  ctx,
const uint8_t *  data,
size_t  len,
void *  usrptr 
)
inlinestaticprotectednoexcept

C-ABI flush trampoline: forwards to onFlushCallback() via usrptr.

Definition at line 927 of file sofab.hpp.

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

◆ write() [1/2]

template<typename T >
Result sofab::OStreamImpl::write ( sofab_id_t  id,
const T &  value 
)
inlinenoexcept

Encode a single field, deducing the wire type from T.

Supports integers, bool, float, double, string-like types (std::string / std::string_view / C strings), contiguous ranges (arrays/vectors/spans) of integers, enumerations or floats, and nested OStreamMessage objects. An enumeration element travels as its declared underlying type, so a container of scoped enums needs no converted copy. Using a type whose capability was compiled out of the C core is a compile-time error (see the SOFAB_DISABLE_* notes at the top).

Parameters
idField identifier.
valueValue to encode.
Returns
Result for fluent chaining and error inspection.

Definition at line 1195 of file sofab.hpp.

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

◆ write() [2/2]

Result sofab::OStreamImpl::write ( sofab_id_t  id,
const void *  value,
int32_t  size 
)
inlinenoexcept

Encode a raw binary blob field.

Parameters
idField identifier.
valuePointer to the bytes to write.
sizeNumber of bytes at value.
Returns
Result for fluent chaining and error inspection.

Definition at line 1462 of file sofab.hpp.

Here is the call graph for this function:

◆ writeIf()

template<typename T >
Result sofab::OStreamImpl::writeIf ( sofab_id_t  id,
const T &  value,
bool  condition 
)
inlinenoexcept

Encode a field only when condition is true.

Parameters
idField identifier.
valueValue to encode; the wire type is deduced from T.
conditionWrite the field only when true; otherwise a success no-op.
Returns
Result for fluent chaining and error inspection.

Definition at line 1475 of file sofab.hpp.

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

◆ writeLazy()

template<typename T >
Result sofab::OStreamImpl::writeLazy ( sofab_id_t  id,
const T &  value 
)
inlinenoexcept

Encode a nested message field, omitting it when all-default.

Same as write for an OStreamMessage, except it closes with sequenceEnd rather than sequenceEndKeep: the nested serialize omits every child that equals its default, so "not one child was written" is exactly "the object equals its declared default" – evaluated per child field, recursively – and the field is then dropped instead of emitted as an empty frame (MESSAGE_SPEC §2).

Use it for a struct / union field. Keep plain write for an array element: element presence carries a dynamic array's length (§5.1), so an all-default element stays framed.

As in write, the closer runs even when the nested serialize fails (a strict-UTF-8 rejection does that on an otherwise healthy stream), so the sequence never stays open and the hold-back window never strands a slot. The first failure is the one returned.

Parameters
idField identifier.
valueNested message to encode.
Returns
Result for fluent chaining and error inspection.

Definition at line 1437 of file sofab.hpp.

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

Field Documentation

◆ buffer_

uint8_t* sofab::OStreamImpl::buffer_
protected

Pointer to the active encode buffer.

Definition at line 909 of file sofab.hpp.

◆ ctx_

sofab_ostream_t sofab::OStreamImpl::ctx_
protected

Underlying C output stream context.

Definition at line 908 of file sofab.hpp.

◆ failed_

uint8_t sofab::OStreamImpl::failed_ = 0
protected

Sticky: first failing write's sofab_ret_t (0 = none).

See ok.

Definition at line 914 of file sofab.hpp.

◆ flushCallback_

flushCallback sofab::OStreamImpl::flushCallback_ {}
protected

Optional user flush callback.

Definition at line 913 of file sofab.hpp.


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