SofaBuffers C++ 0.12.0
Pure-C++20 SofaBuffers — API Documentation
Loading...
Searching...
No Matches
sofab::OStream Class Reference

Output stream over a caller-supplied buffer whose lifetime is shared. More...

#include <sofab.hpp>

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

Public Member Functions

 OStream (std::shared_ptr< uint8_t[]> buffer, size_t buflen, size_t offset=0) noexcept
 Construct over a caller-supplied buffer.
 
 OStream (flushCallback callback, std::shared_ptr< uint8_t[]> buffer, size_t buflen, size_t offset=0) noexcept
 Construct over a caller-supplied buffer with a flush callback.
 
void setBuffer (std::shared_ptr< uint8_t[]> buffer, size_t buflen, size_t offset=0) noexcept
 Install a buffer, mid-stream if need be (§5.1's buffer-set).
 
std::shared_ptr< uint8_t[]> getBuffer () noexcept
 
- 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.
 

Protected Member Functions

 OStream () noexcept=default
 Construct without a buffer; one must be set via setBuffer.
 
- 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.
 

Protected Attributes

std::shared_ptr< uint8_t[]> bufferOwner_
 Caller storage, kept alive.
 
- 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.
 

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.
 
- 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).
 

Detailed Description

Output stream over a caller-supplied buffer whose lifetime is shared.

The buffer is adopted from the caller, swappable at runtime via setBuffer, and retrievable with getBuffer so it may be shared with whatever consumes the encoded bytes.

Note
This stream does not allocate the buffer, and no stream in this library does: §5.1 puts every output buffer on the caller's side, so that there is one buffer-ownership model rather than two and a heap-less profile is the plain reading of it rather than a special case. Sizing the storage is the job of the layer that knows the schema — the generated code, which allocates MAX_SIZE and installs it without a sink for a bounded schema, or a scratch buffer with an appending sink for an unbounded one. A caller that simply wants a heap buffer writes sofab::OStream os{std::make_shared<uint8_t[]>(n), n};.

Definition at line 2502 of file sofab.hpp.

Constructor & Destructor Documentation

◆ OStream() [1/3]

sofab::OStream::OStream ( )
protecteddefaultnoexcept

Construct without a buffer; one must be set via setBuffer.

◆ OStream() [2/3]

sofab::OStream::OStream ( std::shared_ptr< uint8_t[]>  buffer,
size_t  buflen,
size_t  offset = 0 
)
inlinenoexcept

Construct over a caller-supplied buffer.

Parameters
bufferBacking storage to adopt.
buflenCapacity of buffer in bytes.
offsetNumber of leading bytes to reserve before the write cursor.

Definition at line 2516 of file sofab.hpp.

◆ OStream() [3/3]

sofab::OStream::OStream ( flushCallback  callback,
std::shared_ptr< uint8_t[]>  buffer,
size_t  buflen,
size_t  offset = 0 
)
inlinenoexcept

Construct over a caller-supplied buffer with a flush callback.

Parameters
callbackInvoked with finished bytes whenever the buffer fills.
bufferBacking storage to adopt.
buflenCapacity of buffer in bytes.
offsetNumber of leading bytes to reserve before the write cursor.

Definition at line 2528 of file sofab.hpp.

Member Function Documentation

◆ getBuffer()

std::shared_ptr< uint8_t[]> sofab::OStream::getBuffer ( )
inlinenoexcept
Returns
A shared handle to the backing buffer.

Definition at line 2563 of file sofab.hpp.

◆ setBuffer()

void sofab::OStream::setBuffer ( std::shared_ptr< uint8_t[]>  buffer,
size_t  buflen,
size_t  offset = 0 
)
inlinenoexcept

Install a buffer, mid-stream if need be (§5.1's buffer-set).

This is what a flush sink that takes the buffer — hands it to a transport, queues it for an asynchronous write, gives it to DMA — must call before returning; returning without calling it means the sink copied, and the encoder reuses the same buffer from offset 0.

The start offset belongs to this installation, not to the buffer, and it is consumed: passing the same buffer again is a new installation like any other, which is how a sink re-arms header room in every flushed packet where a bare return would not.

With a sink installed the buffer must leave at least MIN_OUTPUT_BUFFER writable bytes (buflen - offset); an undersized one — or an offset past buflen — is rejected here rather than partway through a message, leaving the stream inert with ok false and error reporting Error::InvalidArgument.

Parameters
bufferNew backing storage to adopt.
buflenCapacity of buffer in bytes.
offsetNumber of leading bytes to reserve before the write cursor.

Definition at line 2557 of file sofab.hpp.

Member Data Documentation

◆ bufferOwner_

std::shared_ptr<uint8_t[]> sofab::OStream::bufferOwner_
protected

Caller storage, kept alive.

Definition at line 2505 of file sofab.hpp.


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