SofaBuffers C++ 0.12.0
Pure-C++20 SofaBuffers — API Documentation
Loading...
Searching...
No Matches
sofab::OStreamInline< N, Offset > Class Template Reference

Output stream whose buffer is stored inline (the buffer costs no heap allocation). More...

#include <sofab.hpp>

Inheritance diagram for sofab::OStreamInline< N, Offset >:
[legend]
Collaboration diagram for sofab::OStreamInline< N, Offset >:
[legend]

Public Member Functions

 OStreamInline () noexcept
 Construct with no flush callback; overflow returns Error::BufferFull.
 
 OStreamInline (flushCallback callback) noexcept
 Construct with a flush callback.
 
- Public Member Functions inherited from sofab::OStreamImpl
 OStreamImpl (const OStreamImpl &)=delete
 
OStreamImpl & operator= (const OStreamImpl &)=delete
 
 OStreamImpl (OStreamImpl &&) noexcept=default
 
OStreamImpl & operator= (OStreamImpl &&) noexcept=default
 
virtual ~OStreamImpl () noexcept
 Flushes any buffered bytes through the callback before destruction.
 
size_t flush () noexcept
 Hand any buffered bytes to the flush callback and rewind the cursor.
 
size_t bytesUsed () const noexcept
 
const uint8_t * data () const noexcept
 
bool ok () const noexcept
 Whether every write on this stream has succeeded.
 
Error error () const noexcept
 The code behind ok, latched first-failure-wins.
 
Result rejectArgument () noexcept
 Refuse the value being encoded: latch Error::InvalidArgument.
 
template<typename T >
Result write (sofab::id fieldId, const T &value) noexcept
 Write a field, dispatching on the value's type.
 
template<typename T >
requires std::is_base_of_v<OStreamMessage, T>
Result writeLazy (sofab::id fieldId, const T &value) noexcept
 Write a nested message field, omitting it when it is all-default.
 
Result write (sofab::id fieldId, const void *value, int32_t size) noexcept
 Write a raw byte blob field.
 
template<typename T >
Result writeIf (sofab::id fieldId, const T &value, bool condition) noexcept
 Write a field only when condition holds.
 
Result sequenceBeginLazy (sofab::id fieldId) noexcept
 Open a nested sub-message whose header is held back until the sub-message turns out to have content.
 
Result sequenceEnd () noexcept
 Close the most recently opened sub-message, letting it vanish if it received no content.
 
Result sequenceEndKeep () noexcept
 Close the most recently opened sub-message, keeping its frame even when it received no content.
 

Additional Inherited Members

- Public Types inherited from sofab::OStreamImpl
using flushCallback = std::function< void(std::span< const uint8_t >)>
 Callback invoked with a span of finished bytes whenever the buffer flushes.
 
- Protected Member Functions inherited from sofab::OStreamImpl
 OStreamImpl () noexcept=default
 Construct an unattached stream; a derived class must call initBuffer.
 
Error latch (Error e) noexcept
 Record a failure in the sticky verdict and hand it straight back.
 
void initBuffer (uint8_t *buffer, size_t buflen, size_t offset) noexcept
 Point the stream at a buffer and position the write cursor.
 
Error pushByte (uint8_t b) noexcept
 Append a single byte, flushing first if the buffer is full.
 
Error pushBytes (const uint8_t *data, size_t len) noexcept
 Append a run of bytes.
 
Error putVarint (uint64_t v) noexcept
 Encode a value as a varint and append it to the buffer.
 
Error putSequenceEnd () noexcept
 Write the sequence-end marker (§4.9) — id 0, wire type 7.
 
Error commitPending () noexcept
 Write out the held-back sequence headers, outermost first.
 
Error beforeContent () noexcept
 Commit any held-back sequence run before writing content.
 
Error writeScalar (sofab::id fieldId, Wire type, uint64_t value) noexcept
 Write a scalar field: header varint plus one value varint, in a single bulk write.
 
Error writeFixlen (sofab::id fieldId, Fix ft, const uint8_t *data, size_t len) noexcept
 Write a length-prefixed field (string, blob or other fixlen payload).
 
template<std::floating_point F>
Error writeFloatScalar (sofab::id fieldId, F value) noexcept
 Write a single float or double as a little-endian fixlen field.
 
template<typename E >
requires (std::integral<E> || std::is_enum_v<E>)
Error writeIntArray (sofab::id fieldId, std::span< const E > elems) noexcept
 Write an array of integers as a count-prefixed run of varints.
 
template<std::floating_point F>
Error writeFloatArray (sofab::id fieldId, std::span< const F > elems) noexcept
 Write an array of floats or doubles as a count-prefixed fixlen array.
 
- Static Protected Member Functions inherited from sofab::OStreamImpl
static size_t encodeVarint (uint8_t *out, uint64_t v) noexcept
 Encode an unsigned value as a base-128 varint.
 
static size_t encodeVarintPadded (uint8_t *out, uint64_t v) noexcept
 Encode a varint into a destination with a full varint window.
 
static constexpr uint64_t headerWord (sofab::id fieldId, Wire w) noexcept
 Compose a field header word: the id in the high bits, the wire type in the low three (§4.1).
 
static constexpr uint64_t fixlenWord (uint64_t len, Fix ft) noexcept
 Compose a fixlen word: the payload length (or element size) in the high bits, the subtype in the low three (§4.6, §4.8).
 
- Protected Attributes inherited from sofab::OStreamImpl
uint8_t * buffer_ = nullptr
 Start of the active buffer.
 
uint8_t * cursor_ = nullptr
 Current write position.
 
uint8_t * end_ = nullptr
 One past the end of the buffer.
 
size_t offset_ = 0
 Start offset of the current installation (§5.1).
 
flushCallback flushCallback_
 Invoked when the buffer fills; may be empty.
 
size_t seqDepth_ = 0
 Number of currently-open nested sequences (§4.9 MAX_DEPTH).
 
PendingRun pending_
 
Error failure_ = Error::None
 Sticky first failure of this stream (see ok, error).
 
bool installed_ = false
 Set by initBuffer, cleared right before a flush callback runs.
 

Detailed Description

template<size_t N, size_t Offset = 0>
class sofab::OStreamInline< N, Offset >

Output stream whose buffer is stored inline (the buffer costs no heap allocation).

The encoded bytes never leave the inline array, and the stream allocates nothing at all — the held-back sequence run is fixed MAX_DEPTH state sized at construction (§6.6.2).

Because the buffer is the object, this flavour admits only a copying sink: there is nothing to hand over, so a sink must copy the span it is given and return without installing anything (§5.1.5's other half). A taking sink wants OStreamView (caller memory, with OStreamView::setBuffer) or OStream (shared_ptr memory).

Template Parameters
NBuffer capacity in bytes; must be greater than zero.
OffsetNumber of leading bytes to reserve before the cursor; must be less than N.

Definition at line 2653 of file sofab.hpp.

Constructor & Destructor Documentation

◆ OStreamInline() [1/2]

template<size_t N, size_t Offset = 0>
sofab::OStreamInline< N, Offset >::OStreamInline ( )
inlinenoexcept

Construct with no flush callback; overflow returns Error::BufferFull.

Definition at line 2661 of file sofab.hpp.

◆ OStreamInline() [2/2]

template<size_t N, size_t Offset = 0>
sofab::OStreamInline< N, Offset >::OStreamInline ( flushCallback  callback)
inlineexplicitnoexcept

Construct with a flush callback.

Parameters
callbackInvoked with finished bytes whenever the buffer fills.

Definition at line 2666 of file sofab.hpp.


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