Base output stream: encodes fields into a caller-provided buffer.
More...
#include <sofab.hpp>
|
| class | Result |
| | Outcome of an encode operation, supporting fluent chaining. More...
|
| |
|
| | 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.
|
| |
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.
◆ 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.
◆ OStreamImpl() [1/3]
| sofab::OStreamImpl::OStreamImpl |
( |
| ) |
|
|
protecteddefaultnoexcept |
◆ OStreamImpl() [2/3]
Copy construction is deleted (the context owns raw buffer pointers).
◆ OStreamImpl() [3/3]
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.
◆ 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.
◆ data()
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.
◆ 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.
◆ 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.
◆ 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.
◆ operator=() [1/2]
Copy assignment is deleted (the context owns raw buffer pointers).
◆ operator=() [2/2]
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()
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
-
| id | Field identifier of the sequence. |
- Returns
- Result for fluent chaining and error inspection.
Definition at line 1516 of file sofab.hpp.
◆ 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.
◆ 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.
◆ static_flush_callback()
|
|
inlinestaticprotectednoexcept |
◆ write() [1/2]
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
-
| id | Field identifier. |
| value | Value to encode. |
- Returns
- Result for fluent chaining and error inspection.
Definition at line 1195 of file sofab.hpp.
◆ write() [2/2]
Encode a raw binary blob field.
- Parameters
-
| id | Field identifier. |
| value | Pointer to the bytes to write. |
| size | Number of bytes at value. |
- Returns
- Result for fluent chaining and error inspection.
Definition at line 1462 of file sofab.hpp.
◆ writeIf()
Encode a field only when condition is true.
- Parameters
-
| id | Field identifier. |
| value | Value to encode; the wire type is deduced from T. |
| condition | Write 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.
◆ writeLazy()
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
-
| id | Field identifier. |
| value | Nested message to encode. |
- Returns
- Result for fluent chaining and error inspection.
Definition at line 1437 of file sofab.hpp.
◆ buffer_
| uint8_t* sofab::OStreamImpl::buffer_ |
|
protected |
Pointer to the active encode buffer.
Definition at line 909 of file sofab.hpp.
◆ ctx_
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_
Optional user flush callback.
Definition at line 913 of file sofab.hpp.
The documentation for this class was generated from the following file: