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

Output stream over a buffer the caller already owns. More...

#include <sofab.hpp>

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

Public Member Functions

 OStreamView (uint8_t *buffer, size_t buflen, size_t offset=0) noexcept
 Construct over caller storage.
 
 OStreamView (flushCallback callback, uint8_t *buffer, size_t buflen, size_t offset=0) noexcept
 Construct over caller storage with a flush callback.
 
void setBuffer (uint8_t *buffer, size_t buflen, size_t offset=0) noexcept
 Install a different caller buffer, mid-stream.
 
- 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

Output stream over a buffer the caller already owns.

Allocates no buffer and copies no payload: encoding writes straight into buffer. The counterpart to OStreamInline (buffer inside the object) and OStream (buffer held by a shared_ptr) — this is the one for a destination that already exists, such as the dst of a generated encodeTo, or a DMA / transport region a taking sink hands on (§5.1.5). Like every stream it allocates nothing at all after construction.

The buffer must outlive the stream, and it is not restored if encoding fails: overflow leaves the bytes written so far in place and ok false.

Definition at line 2580 of file sofab.hpp.

Constructor & Destructor Documentation

◆ OStreamView() [1/2]

sofab::OStreamView::OStreamView ( uint8_t *  buffer,
size_t  buflen,
size_t  offset = 0 
)
inlinenoexcept

Construct over caller storage.

Parameters
bufferDestination; must outlive this stream.
buflenCapacity of buffer in bytes.
offsetNumber of leading bytes to leave untouched before the cursor.

Definition at line 2589 of file sofab.hpp.

◆ OStreamView() [2/2]

sofab::OStreamView::OStreamView ( flushCallback  callback,
uint8_t *  buffer,
size_t  buflen,
size_t  offset = 0 
)
inlinenoexcept

Construct over caller storage with a flush callback.

Parameters
callbackInvoked with finished bytes whenever the buffer fills.
bufferDestination; must outlive this stream.
buflenCapacity of buffer in bytes.
offsetNumber of leading bytes to leave untouched before the cursor.

Definition at line 2601 of file sofab.hpp.

Member Function Documentation

◆ setBuffer()

void sofab::OStreamView::setBuffer ( uint8_t *  buffer,
size_t  buflen,
size_t  offset = 0 
)
inlinenoexcept

Install a different caller buffer, mid-stream.

§5.1.1 lists "allow a new output buffer to be installed mid-stream" among a corelib's required capabilities, and §5.1.5 puts the duty on the sink: "A sink that takes the buffer MUST install a replacement before returning." This is the call it makes. Without it a taking sink could only be written against OStream — the shared_ptr flavour — which is the wrong one for the case a taking sink exists for: memory the caller already owns and hands on (A2-0020).

The stream keeps no reference to the buffer it gives up. The new one must outlive the stream, and the same MIN_OUTPUT_BUFFER floor applies as at construction: behind a sink, buflen - offset short of it is refused with Error::InvalidArgument rather than partway through a later field (§5.1.4).

Parameters
bufferNew destination; must outlive this stream.
buflenCapacity of buffer in bytes.
offsetNumber of leading bytes to leave untouched before the cursor.

Definition at line 2629 of file sofab.hpp.


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