SofaBuffers C/C++ 0.12.0
API Documentation
Loading...
Searching...
No Matches
ostream.h
Go to the documentation of this file.
1/*!
2 * @file ostream.h
3 * @brief SofaBuffers C - Output stream encoder for Sofab messages.
4 *
5 * This module implements a streaming encoder for SofaBuffers (Sofab), a compact,
6 * TLV-like (Type-Length-Value) binary message format. The encoder writes
7 * fields into a user-provided buffer and calls a user-supplied flush callback
8 * when the buffer is full or when the user explicitly requests a flush.
9 *
10 * Typical usage:
11 * - Initialize an sofab_ostream_t with sofab_ostream_init(), providing a buffer
12 * and optionally a flush callback.
13 * - Use the sofab_ostream_write_*() functions to append fields (unsigned,
14 * signed, fixed-length, arrays, nested sequences).
15 * - If a flush callback is provided, it will be invoked automatically when
16 * the internal buffer needs space; the callback is responsible for handling
17 * (sending, persisting) the buffered bytes and usually calls
18 * sofab_ostream_buffer_set() to provide a fresh buffer.
19 * - Call sofab_ostream_flush() to flush any remaining bytes before teardown.
20 *
21 * Ownership and semantics (brief):
22 * - The sofab_ostream_t holds pointers into a buffer supplied by the caller.
23 * The stream never allocates or frees that buffer. The caller owns the
24 * memory and must ensure it remains valid until replaced with
25 * sofab_ostream_buffer_set() or the stream is destroyed.
26 * - The flush callback is optional (may be NULL). If NULL and the buffer
27 * becomes full, write calls will return an appropriate error code.
28 *
29 * SPDX-License-Identifier: MIT
30 */
31
32#ifndef SOFAB_OSTREAM_H
33#define SOFAB_OSTREAM_H
34
35/**
36 * @defgroup c_api C API
37 * @{
38 */
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44#ifndef SOFAB_OSTREAM_C
45# define SOFAB_OSTREAM_EXTERN extern
46#else
47# define SOFAB_OSTREAM_EXTERN
48#endif
49
50/* includes *******************************************************************/
51#include <stdlib.h>
52#include <stdint.h>
53#include <stdbool.h>
54#include <string.h>
55
56#include "sofab/sofab.h"
57
58/* types **********************************************************************/
59
60/*!
61 * @brief Opaque Sofab output stream context.
62 *
63 * This structure holds all state related to the incremental encoding of
64 * Sofab-encoded data. Users should not access its fields directly; instead,
65 * they interact with it through the provided API.
66 */
68
69/*!
70 * @brief Output stream flush callback type.
71 *
72 * The flush callback is invoked when the output stream needs to free or hand
73 * off previously written bytes (for example, when the internal buffer is full
74 * or when sofab_ostream_flush() is called). The callback implementation is
75 * responsible for processing the bytes (e.g. sending them over a transport,
76 * writing to storage) and for providing, if desired, a new buffer through
77 * sofab_ostream_buffer_set().
78 *
79 * The callback must not block the stream reentrantly (the library does not
80 * perform internal locking). If required, the callback should copy or queue
81 * data and return quickly.
82 *
83 * @param usrptr User-provided pointer.
84 * @param data Pointer to the buffer that contains bytes to flush.
85 * @param len Number of bytes available at @p data to flush.
86 */
87typedef void (*sofab_ostream_flush_cb_t) (
88 sofab_ostream_t *ctx, const uint8_t *data, size_t len, void *usrptr);
89
90/*!
91 * @brief Sofab output stream context.
92 *
93 * This structure holds the state required to encode Sofab fields into a
94 * contiguous buffer. The user initializes it with sofab_ostream_init(), then
95 * repeatedly calls writer functions (sofab_ostream_write_*). The application
96 * is responsible for supplying and owning the buffer memory; the stream only
97 * holds pointers into that memory.
98 */
99#if !defined(SOFAB_DISABLE_SEQUENCE_SUPPORT) && !defined(SOFAB_DISABLE_LAZY_SEQ_SUPPORT)
100/*!
101 * @brief How many nested sequence headers sofab_ostream_write_sequence_begin_lazy()
102 * can hold back at once. Default 8.
103 *
104 * **The documented bound of this heap-free profile** (CORELIB_PLAN §6, "How deep
105 * the hold-back reaches"). An implementation that can allocate MUST hold back to
106 * the full SOFAB_MAX_DEPTH and is canonical at every depth; a heap-free profile
107 * cannot, so it MAY bound the run — and MUST say where the bound is, because two
108 * encoders that disagree about it disagree about *bytes*.
109 *
110 * What the bound costs, concretely:
111 *
112 * - At nesting depth <= SOFAB_LAZY_SEQ_DEPTH the encoder is **canonical**: an
113 * all-default sequence field is omitted entirely (MESSAGE_SPEC §2).
114 * - Deeper than that, the run is committed and the sequence is framed
115 * **eagerly**. If it then turns out to be all-default, the two-byte empty
116 * frame §2 would have omitted stays on the wire. That output is
117 * **well-formed and decodes to the same value** — it is the non-canonical
118 * form §2 already requires every decoder to accept and normalize — so a
119 * bounded and an unbounded encoder interoperate. What it is not is
120 * byte-identical to an unbounded encoder's output.
121 *
122 * This is the same constrained-profile allowance SOFAB_FIXLEN_MAX / SOFAB_ARRAY_MAX
123 * already carry, for the same reason: a bound that costs RAM per stream is a real
124 * cost on a target that has none to spare. Raise it (-DSOFAB_LAZY_SEQ_DEPTH=N) if
125 * a schema nests sequence *fields* deeper than 8 and canonical output matters;
126 * every extra level costs 4 bytes of RAM per output stream.
127 *
128 * **Ceiling: 255.** The run counter (@c sofab_ostream::npending) is a @c uint8_t,
129 * so a larger window could not be counted; the preprocessor check below rejects such a
130 * build rather than let the counter wrap and silently lose a pending run. 255 is
131 * already past SOFAB_MAX_DEPTH-deep sequence *fields* being useful, and a profile
132 * that wants an unbounded window needs a heap, which this corelib does not have.
133 *
134 * Not a wire-format limit and not a nesting limit: nesting itself is bounded by
135 * SOFAB_MAX_DEPTH (255) and that check is unaffected — exceeding
136 * SOFAB_LAZY_SEQ_DEPTH is never an error, only a loss of canonicity.
137 */
138# if !defined(SOFAB_LAZY_SEQ_DEPTH)
139# define SOFAB_LAZY_SEQ_DEPTH 8
140# endif
141# if SOFAB_LAZY_SEQ_DEPTH < 1 || SOFAB_LAZY_SEQ_DEPTH > 255
142# error "SOFAB_LAZY_SEQ_DEPTH must be 1..255 (npending is a uint8_t)"
143# endif
144#endif /* SEQUENCE && LAZY_SEQ */
145
147{
148 uint8_t *buffer; /*!< Pointer to the start of the active buffer. */
149 uint8_t *bufend; /*!< Pointer one-past-the-end of the active buffer. */
150 uint8_t *offset; /*!< Current write cursor within the buffer. */
151 sofab_ostream_flush_cb_t flush; /*!< Optional flush callback invoked when buffer data must be handled. */
152 void *usrptr; /*!< User-provided pointer passed to the flush callback. */
153#if !defined(SOFAB_DISABLE_SEQUENCE_SUPPORT) && !defined(SOFAB_DISABLE_LAZY_SEQ_SUPPORT)
154 /*! Ids of the innermost open sequences whose header is still held back
155 * (see sofab_ostream_write_sequence_begin_lazy). Always a contiguous suffix
156 * of the open sequences: writing any field, opening an eager frame, or
157 * overflowing the window commits the whole run.
158 *
159 * Costs 4 * SOFAB_LAZY_SEQ_DEPTH bytes plus the counter -- 36 B per stream at
160 * the default depth of 8 on a 32-bit target. A pure-C consumer that
161 * encodes through sofab_object_encode() never needs it -- the descriptor
162 * decides omission per field before opening anything -- so a footprint build
163 * can compile the whole mechanism out with SOFAB_DISABLE_LAZY_SEQ_SUPPORT.
164 * The macro changes this struct's layout, so it MUST be defined identically
165 * for the library and every one of its users. */
167 uint8_t npending; /*!< Valid entries in @c pending. */
168#endif /* SEQUENCE && LAZY_SEQ */
169};
170
171/* prototypes *****************************************************************/
172
173/*!
174 * @brief Initialize an Sofab output stream context.
175 *
176 * Prepares the sofab_ostream_t for usage. The supplied buffer will be used for
177 * subsequent writes. If @p flush is non-NULL it will be called whenever the
178 * implementation needs to hand off buffered bytes (for example when the buffer
179 * is full or when sofab_ostream_flush() is invoked). The user pointer @p usrptr
180 * is passed unchanged to that callback.
181 *
182 * The function does not copy the provided buffer; the caller retains ownership
183 * and must ensure the memory remains valid until replaced by
184 * sofab_ostream_buffer_set() or the stream is destroyed.
185 *
186 * @param ctx Pointer to the output stream context to initialize.
187 * @param buffer Pointer to a writable buffer used to accumulate encoded bytes.
188 * @param buflen Size of @p buffer in bytes.
189 * @param offset Initial offset (0..buflen) to start writing from. The offset
190 * is applied relative to @p buffer; e.g. offset==0 writes at
191 * buffer[0], offset==buflen makes the buffer effectively full.
192 * @param flush Optional flush callback invoked when the buffer data must be handled.
193 * @param usrptr Optional user pointer passed to @p flush when invoked.
194 *
195 * @note When @p flush is non-NULL the buffer is installed **for streaming** and
196 * must satisfy @c buflen @c - @c offset @c >= @ref SOFAB_MIN_OUTPUT_BUFFER
197 * (CORELIB_PLAN §5.1). With @p flush NULL no flush can occur and no
198 * minimum applies: @p buflen may be 0 and @p offset may equal @p buflen,
199 * and the buffer either holds the message or reports
200 * @ref SOFAB_RET_E_BUFFER_FULL. The all-default message is the empty byte
201 * string (MESSAGE_SPEC §2), so the zero-length case is a real one.
202 *
203 * @note Both preconditions are checked with @c assert(), which is this port's
204 * mechanism for an out-of-range argument on a @c void entry point. A
205 * release build (@c NDEBUG) does not reject; an installation that leaves
206 * no room then surfaces as @ref SOFAB_RET_E_BUFFER_FULL on the first
207 * write that needs space, never as a write past the buffer.
208 */
210 sofab_ostream_t *ctx, uint8_t *buffer, size_t buflen, size_t offset,
211 sofab_ostream_flush_cb_t flush, void *usrptr);
212
213/*!
214 * @brief Flush any pending bytes from the output stream.
215 *
216 * If a flush callback was registered during initialization, it is invoked with
217 * any pending bytes currently present in the active buffer. After the call,
218 * the internal buffer is considered empty; offset is reset to the start of the
219 * buffer (unless the flush callback calls sofab_ostream_buffer_set() to provide
220 * a different buffer).
221 *
222 * @param ctx Pointer to the output stream context.
223 * @return Number of bytes flushed.
224 */
226
227/*!
228 * @brief Get the number of bytes used in the current buffer.
229 *
230 * This function returns the number of bytes that have been written into the
231 * active buffer since the last flush or initialization.
232 *
233 * @param ctx Pointer to the output stream context.
234 * @return Number of bytes used in the current buffer.
235 */
237
238/*!
239 * @brief Replace the active buffer used by the output stream.
240 *
241 * This function updates the stream to use a new buffer region. Typical usage
242 * is inside a flush callback: after handing off the old buffer bytes the
243 * callback calls sofab_ostream_buffer_set() to present a fresh buffer and resume
244 * encoding without losing data.
245 *
246 * The buffer is not copied; the stream simply adjusts pointers to the new
247 * region. The caller retains ownership of the memory.
248 *
249 * @param ctx Pointer to the output stream context.
250 * @param buffer Pointer to the new buffer to use.
251 * @param buflen Size of @p buffer in bytes.
252 * @param offset Initial offset within the new buffer (0..buflen). The offset
253 * belongs to *this* installation (CORELIB_PLAN §5.1): the cursor
254 * starts at it, and it is consumed, so a later flush the callback
255 * returns from without installing anything resumes at 0. Passing
256 * the same buffer again is a new installation like any other --
257 * that is how a sink re-arms header room in every flushed unit,
258 * one framing header per packet.
259 */
261 sofab_ostream_t *ctx, uint8_t *buffer, size_t buflen, size_t offset);
262
263/* write functions ************************************************************/
264
265/*!
266 * @brief Write an unsigned integer field.
267 *
268 * Encodes a field header for the given @p id with an unsigned varint payload
269 * and writes the varint-encoded @p value into the stream buffer.
270 *
271 * @param ctx Pointer to the output stream context.
272 * @param id Field identifier (sofab_id_t).
273 * @param value Unsigned integer value to encode.
274 *
275 * @return SOFAB_RET_OK on success, otherwise an sofab_ret_t error code will be returned.
276 */
279
280/*!
281 * @brief Write a signed integer field.
282 *
283 * Encodes a field header for @p id and writes a signed integer value. The
284 * value encoding typically uses zigzag transformation followed by varint
285 * encoding.
286 *
287 * @param ctx Pointer to the output stream context.
288 * @param id Field identifier.
289 * @param value Signed integer value to encode.
290 *
291 * @return SOFAB_RET_OK on success, otherwise an error code.
292 */
295
296#if !defined(SOFAB_DISABLE_FIXLEN_SUPPORT)
297/*!
298 * @brief Write a fixed-length field.
299 *
300 * Encodes a fixed-length field header for @p id and copies @p datalen bytes
301 * from @p data into the output stream. The @p type parameter specifies the
302 * semantic type of the fixed-length data.
303 *
304 * @param ctx Pointer to the output stream context.
305 * @param id Field identifier.
306 * @param data Pointer to bytes to write (may be NULL if datalen is 0).
307 * @param datalen Number of bytes to write from @p data.
308 * @param type Semantic fixed-length type (sofab_fixlentype_t).
309 *
310 * @return SOFAB_RET_OK on success, otherwise an sofab_ret_t error code.
311 */
314 const void *data, int32_t datalen,
315 sofab_fixlentype_t type);
316#endif /* !defined(SOFAB_DISABLE_FIXLEN_SUPPORT) */
317
318/* inline convenience functions ***********************************************/
319
320/*!
321 * @brief Write a boolean value (1 byte).
322 *
323 * Convenience wrapper that encodes a boolean as an unsigned value (0 or 1).
324 *
325 * @param ctx Pointer to the output stream context.
326 * @param id Field identifier.
327 * @param value Boolean value to encode.
328 *
329 * @return See sofab_ostream_write_unsigned().
330 */
332 sofab_ostream_t *ctx, sofab_id_t id, bool value)
333{
334 return sofab_ostream_write_unsigned(ctx, id, value ? 1 : 0);
335}
336
337#if !defined(SOFAB_DISABLE_FIXLEN_SUPPORT)
338/*!
339 * @brief Write a 32-bit floating-point value.
340 *
341 * @param ctx Pointer to the output stream context.
342 * @param id Field identifier.
343 * @param value Float value to encode.
344 *
345 * @return See sofab_ostream_write_fixlen().
346 */
348 sofab_ostream_t *ctx, sofab_id_t id, float value)
349{
350 return sofab_ostream_write_fixlen(ctx, id, &value, sizeof(value), SOFAB_FIXLENTYPE_FP32);
351}
352
353/*!
354 * @brief Write a 64-bit floating-point value.
355 *
356 * @param ctx Pointer to the output stream context.
357 * @param id Field identifier.
358 * @param value Double value to encode.
359 *
360 * @return See sofab_ostream_write_fixlen().
361 */
362#if !defined(SOFAB_DISABLE_FP64_SUPPORT)
364 sofab_ostream_t *ctx, sofab_id_t id, double value)
365{
366 return sofab_ostream_write_fixlen(ctx, id, &value, sizeof(value), SOFAB_FIXLENTYPE_FP64);
367}
368#endif /* !defined(SOFAB_DISABLE_FP64_SUPPORT) */
369
370/*!
371 * @brief Write a null-terminated string.
372 *
373 * Convenience wrapper that writes a string field by using strlen()
374 * to determine length.
375 *
376 * @param ctx Pointer to the output stream context.
377 * @param id Field identifier.
378 * @param text Null-terminated C string to write.
379 *
380 * @return See sofab_ostream_write_fixlen().
381 */
383 sofab_ostream_t *ctx, sofab_id_t id, const char *text)
384{
385 return sofab_ostream_write_fixlen(ctx, id, text, strlen(text), SOFAB_FIXLENTYPE_STRING);
386}
387
388/*!
389 * @brief Write a binary blob field.
390 *
391 * Convenience wrapper that writes raw bytes as a blob fixed-length field.
392 *
393 * @param ctx Pointer to the output stream context.
394 * @param id Field identifier.
395 * @param data Pointer to binary data.
396 * @param size Number of bytes to write.
397 *
398 * @return See sofab_ostream_write_fixlen().
399 */
401 sofab_ostream_t *ctx, sofab_id_t id, const void *data, size_t size)
402{
403 return sofab_ostream_write_fixlen(ctx, id, data, size, SOFAB_FIXLENTYPE_BLOB);
404}
405#endif /* !defined(SOFAB_DISABLE_FIXLEN_SUPPORT) */
406
407/* write array functions ******************************************************/
408
409#if !defined(SOFAB_DISABLE_ARRAY_SUPPORT)
410/*!
411 * @brief Write an array of unsigned integers.
412 *
413 * Encodes an array field composed of unsigned integer elements. Each array
414 * element is encoded according to the element size and unsigned varint rules.
415 * Supported types: uint8_t, uint16_t, uint32_t, uint64_t.
416 *
417 * @param ctx Pointer to the output stream context.
418 * @param id Field identifier.
419 * @param data Pointer to the array data.
420 * @param element_count Number of elements in @p data.
421 * @param element_size Size of each element in bytes.
422 *
423 * @return SOFAB_RET_OK on success, otherwise an error code.
424 */
427 const void *data, int32_t element_count, int32_t element_size);
428
429/*!
430 * @brief Write an array of signed integers.
431 *
432 * Encodes an array field composed of signed integer elements. Each array
433 * element is encoded according to the element size and signed varint rules.
434 * Supported types: int8_t, int16_t, int32_t, int64_t.
435 *
436 * @param ctx Pointer to the output stream context.
437 * @param id Field identifier.
438 * @param data Pointer to the array data.
439 * @param element_count Number of elements in @p data.
440 * @param element_size Size of each element in bytes.
441 *
442 * @return SOFAB_RET_OK on success, otherwise an error code.
443 */
446 const void *data, int32_t element_count, int32_t element_size);
447
448#if !defined(SOFAB_DISABLE_FIXLEN_SUPPORT)
449/*!
450 * @brief Write an array of fixed-length elements.
451 *
452 * Writes a contiguous block representing @p element_count elements of fixed
453 * size @p element_size. The @p type describes the semantic fixed-length type
454 * for the array elements.
455 * Only floating-point types (FP32, FP64) are supported for fixed-length arrays.
456 *
457 * @param ctx Pointer to the output stream context.
458 * @param id Field identifier.
459 * @param data Pointer to element data.
460 * @param element_count Number of elements.
461 * @param element_size Size of each element in bytes.
462 * @param type Semantic fixed-length type of elements.
463 *
464 * @return SOFAB_RET_OK on success, otherwise an error code.
465 */
468 const void *data, int32_t element_count, int32_t element_size,
469 sofab_fixlentype_t type);
470#endif /* !defined(SOFAB_DISABLE_FIXLEN_SUPPORT) */
471#endif /* !defined(SOFAB_DISABLE_ARRAY_SUPPORT) */
472
473/* inline convenience array functions *****************************************/
474
475#if !defined(SOFAB_DISABLE_ARRAY_SUPPORT)
476/*!
477 * @brief Writes an array of 8-bit signed integers.
478 *
479 * @param ctx Output stream context.
480 * @param id Field identifier.
481 * @param var Destination array.
482 * @param element_count Number of elements to write.
483 *
484 * @return See sofab_ostream_write_array_of_signed().
485 */
487 sofab_ostream_t *ctx, sofab_id_t id, const int8_t *var, int32_t element_count)
488{
489 return sofab_ostream_write_array_of_signed(ctx, id, var, element_count, sizeof(int8_t));
490}
491
492/*!
493 * @brief Writes an array of 8-bit unsigned integers.
494 *
495 * @param ctx Output stream context.
496 * @param id Field identifier.
497 * @param var Destination array.
498 * @param element_count Number of elements to write.
499 *
500 * @return See sofab_ostream_write_array_of_unsigned().
501 */
503 sofab_ostream_t *ctx, sofab_id_t id, const uint8_t *var, int32_t element_count)
504{
505 return sofab_ostream_write_array_of_unsigned(ctx, id, var, element_count, sizeof(uint8_t));
506}
507
508/*!
509 * @brief Writes an array of 16-bit signed integers.
510 *
511 * @param ctx Output stream context.
512 * @param id Field identifier.
513 * @param var Destination array.
514 * @param element_count Number of elements to write.
515 *
516 * @return See sofab_ostream_write_array_of_signed().
517 */
519 sofab_ostream_t *ctx, sofab_id_t id, const int16_t *var, int32_t element_count)
520{
521 return sofab_ostream_write_array_of_signed(ctx, id, var, element_count, sizeof(int16_t));
522}
523
524/*!
525 * @brief Writes an array of 16-bit unsigned integers.
526 *
527 * @param ctx Output stream context.
528 * @param id Field identifier.
529 * @param var Destination array.
530 * @param element_count Number of elements to write.
531 *
532 * @return See sofab_ostream_write_array_of_unsigned().
533 */
535 sofab_ostream_t *ctx, sofab_id_t id, const uint16_t *var, int32_t element_count)
536{
537 return sofab_ostream_write_array_of_unsigned(ctx, id, var, element_count, sizeof(uint16_t));
538}
539
540/*!
541 * @brief Writes an array of 32-bit signed integers.
542 *
543 * @param ctx Output stream context.
544 * @param id Field identifier.
545 * @param var Destination array.
546 * @param element_count Number of elements to write.
547 *
548 * @return See sofab_ostream_write_array_of_signed().
549 */
551 sofab_ostream_t *ctx, sofab_id_t id, const int32_t *var, int32_t element_count)
552{
553 return sofab_ostream_write_array_of_signed(ctx, id, var, element_count, sizeof(int32_t));
554}
555
556/*!
557 * @brief Writes an array of 32-bit unsigned integers.
558 *
559 * @param ctx Output stream context.
560 * @param id Field identifier.
561 * @param var Destination array.
562 * @param element_count Number of elements to write.
563 *
564 * @return See sofab_ostream_write_array_of_unsigned().
565 */
567 sofab_ostream_t *ctx, sofab_id_t id, const uint32_t *var, int32_t element_count)
568{
569 return sofab_ostream_write_array_of_unsigned(ctx, id, var, element_count, sizeof(uint32_t));
570}
571
572/*!
573 * @brief Writes an array of 64-bit signed integers.
574 *
575 * @param ctx Output stream context.
576 * @param id Field identifier.
577 * @param var Destination array.
578 * @param element_count Number of elements to write.
579 *
580 * @return See sofab_ostream_write_array_of_signed().
581 */
582#if !defined(SOFAB_DISABLE_INT64_SUPPORT)
584 sofab_ostream_t *ctx, sofab_id_t id, const int64_t *var, int32_t element_count)
585{
586 return sofab_ostream_write_array_of_signed(ctx, id, var, element_count, sizeof(int64_t));
587}
588
589/*!
590 * @brief Writes an array of 64-bit unsigned integers.
591 *
592 * @param ctx Output stream context.
593 * @param id Field identifier.
594 * @param var Destination array.
595 * @param element_count Number of elements to write.
596 *
597 * @return See sofab_ostream_write_array_of_unsigned().
598 */
600 sofab_ostream_t *ctx, sofab_id_t id, const uint64_t *var, int32_t element_count)
601{
602 return sofab_ostream_write_array_of_unsigned(ctx, id, var, element_count, sizeof(uint64_t));
603}
604#endif /* !defined(SOFAB_DISABLE_INT64_SUPPORT) */
605
606#if !defined(SOFAB_DISABLE_FIXLEN_SUPPORT)
607/*!
608 * @brief Write an array of 32-bit floating-point values.
609 *
610 * @param ctx Pointer to the output stream context.
611 * @param id Field identifier.
612 * @param data Pointer to float array.
613 * @param element_count Number of float elements.
614 *
615 * @return See sofab_ostream_write_array_of_fixlen().
616 */
618 sofab_ostream_t *ctx, sofab_id_t id, const float *data, int32_t element_count)
619{
621 ctx, id, data, element_count, sizeof(float), SOFAB_FIXLENTYPE_FP32);
622}
623
624/*!
625 * @brief Write an array of 64-bit floating-point values.
626 *
627 * @param ctx Pointer to the output stream context.
628 * @param id Field identifier.
629 * @param data Pointer to double array.
630 * @param element_count Number of double elements.
631 *
632 * @return See sofab_ostream_write_array_of_fixlen().
633 */
634#if !defined(SOFAB_DISABLE_FP64_SUPPORT)
636 sofab_ostream_t *ctx, sofab_id_t id, const double *data, int32_t element_count)
637{
639 ctx, id, data, element_count, sizeof(double), SOFAB_FIXLENTYPE_FP64);
640}
641#endif /* !defined(SOFAB_DISABLE_FP64_SUPPORT) */
642#endif /* !defined(SOFAB_DISABLE_FIXLEN_SUPPORT) */
643#endif /* !defined(SOFAB_DISABLE_ARRAY_SUPPORT) */
644
645/* nested sequence functions **************************************************/
646
647#if !defined(SOFAB_DISABLE_SEQUENCE_SUPPORT)
648/*!
649 * @brief Begin encoding a nested sequence (start marker).
650 *
651 * Writes the header that begins a nested sequence for field @p id. After this
652 * call, subsequent writes belong to the nested sequence until
653 * sofab_ostream_write_sequence_end() is called. Nested sequences allow
654 * hierarchical message structures with a new field identifier space.
655 *
656 * This opener frames unconditionally: the header is on the wire before the
657 * content is known, so the sequence reaches the wire even if nothing is written
658 * into it. That is what a hand-written encoder wants (an empty sequence is a
659 * legal wire primitive, §4.9) and what an element position needs (§5.1). A
660 * sequence-typed *field* that may equal its declared default belongs to
661 * sofab_ostream_write_sequence_begin_lazy() instead, which omits it
662 * (MESSAGE_SPEC §2).
663 *
664 * @param ctx Pointer to the output stream context.
665 * @param id Field identifier representing the sequence.
666 *
667 * @return SOFAB_RET_OK on success, otherwise an sofab_ret_t error code.
668 */
670
671/*!
672 * @brief Begin a nested sequence whose header is held back until it has content.
673 *
674 * MESSAGE_SPEC §2 omits a sequence-typed field whose value equals its declared
675 * default, and "not one child field was written" is exactly that condition --
676 * evaluated per child field, recursively, for free. A sequence opened with this
677 * function and closed without a single field in it emits **nothing** instead of a
678 * two-byte empty frame, so an all-default message becomes the empty byte string.
679 *
680 * The predicate never touches a byte image of the object, so struct padding
681 * cannot influence it and a non-zero nested default is handled by the caller's
682 * ordinary per-field test.
683 *
684 * This is the message-layer primitive, for generated code and for the C++
685 * wrapper. Two callers keep using the eager sofab_ostream_write_sequence_begin():
686 * - a direct (hand-written) encoder, for which an empty sequence is a legal
687 * wire primitive in its own right (§4.9);
688 * - an array wrapper whose field declares a **non-empty** default, where the
689 * empty frame is the only encoding of "explicitly empty" (§2, §3).
690 *
691 * The descriptor-driven sofab_object_encode() needs neither: it decides omission
692 * per field, before opening anything.
693 *
694 * **Bounded**: at most @ref SOFAB_LAZY_SEQ_DEPTH headers are held back at once.
695 * A sequence opened deeper than that is framed eagerly and therefore keeps its
696 * empty frame when it turns out to be all-default — well-formed and
697 * value-identical, but not canonical. See @ref SOFAB_LAZY_SEQ_DEPTH for why this
698 * heap-free profile takes that allowance and how to raise the bound.
699 *
700 * **On a full buffer**: whatever commits the run (a field write, an eager
701 * sequence_begin, sofab_ostream_write_sequence_end_keep(), or overflowing the
702 * window) can hit SOFAB_RET_E_BUFFER_FULL partway through it. The ids that did
703 * not reach the buffer stay held back, so a caller that supplies a fresh buffer
704 * with sofab_ostream_buffer_set() and retries emits them then — the pending run
705 * never silently loses a sequence whose end marker is still coming. As everywhere
706 * in this encoder, the *bytes* are not rolled back: the header that hit the wall
707 * may have left a partial varint behind.
708 *
709 * @param ctx Pointer to the output stream context.
710 * @param id Field identifier representing the sequence.
711 *
712 * @return SOFAB_RET_OK on success, otherwise an sofab_ret_t error code.
713 */
714#if !defined(SOFAB_DISABLE_LAZY_SEQ_SUPPORT)
716
717/*!
718 * @brief End a nested sequence, keeping its frame even without content.
719 *
720 * The counterpart of sofab_ostream_write_sequence_end() for a sequence opened
721 * with sofab_ostream_write_sequence_begin_lazy(): it first emits the held-back
722 * headers -- this frame's and every enclosing one's -- and then the end marker,
723 * so an empty sequence reaches the wire as begin + end.
724 *
725 * Required wherever the frame carries information beyond its contents: a
726 * wrapper-array ELEMENT (element presence carries a dynamic array's length,
727 * MESSAGE_SPEC §5.1), and an array field already known to differ from a
728 * non-empty declared default (§2, §3).
729 *
730 * @param ctx Pointer to the output stream context.
731 *
732 * @return SOFAB_RET_OK on success, otherwise an sofab_ret_t error code.
733 */
735#endif /* !defined(SOFAB_DISABLE_LAZY_SEQ_SUPPORT) */
736
737/*!
738 * @brief End encoding a nested sequence (end marker).
739 *
740 * Writes the termination marker for the current nested sequence started via
741 * sofab_ostream_write_sequence_begin().
742 *
743 * @param ctx Pointer to the output stream context.
744 *
745 * @return SOFAB_RET_OK on success, otherwise an sofab_ret_t error code.
746 */
748#endif /* !defined(SOFAB_DISABLE_SEQUENCE_SUPPORT) */
749
750#ifdef __cplusplus
751}
752#endif
753
754/** @} */ // end of defgroup
755
756#endif /* SOFAB_OSTREAM_H */
static sofab_ret_t sofab_ostream_write_array_of_i64(sofab_ostream_t *ctx, sofab_id_t id, const int64_t *var, int32_t element_count)
Writes an array of 64-bit signed integers.
Definition ostream.h:583
sofab_ret_t sofab_ostream_write_sequence_end(sofab_ostream_t *ctx)
End encoding a nested sequence (end marker).
size_t sofab_ostream_flush(sofab_ostream_t *ctx)
Flush any pending bytes from the output stream.
sofab_fixlentype_t
SofaBuffers 3bit fixed-length data types.
Definition sofab.h:98
sofab_ret_t sofab_ostream_write_array_of_unsigned(sofab_ostream_t *ctx, sofab_id_t id, const void *data, int32_t element_count, int32_t element_size)
Write an array of unsigned integers.
sofab_ret_t sofab_ostream_write_fixlen(sofab_ostream_t *ctx, sofab_id_t id, const void *data, int32_t datalen, sofab_fixlentype_t type)
Write a fixed-length field.
#define SOFAB_LAZY_SEQ_DEPTH
Sofab output stream context.
Definition ostream.h:139
sofab_ret_t sofab_ostream_write_sequence_begin_lazy(sofab_ostream_t *ctx, sofab_id_t id)
Begin a nested sequence whose header is held back until it has content.
static sofab_ret_t sofab_ostream_write_array_of_u16(sofab_ostream_t *ctx, sofab_id_t id, const uint16_t *var, int32_t element_count)
Writes an array of 16-bit unsigned integers.
Definition ostream.h:534
static sofab_ret_t sofab_ostream_write_array_of_i16(sofab_ostream_t *ctx, sofab_id_t id, const int16_t *var, int32_t element_count)
Writes an array of 16-bit signed integers.
Definition ostream.h:518
static sofab_ret_t sofab_ostream_write_array_of_u8(sofab_ostream_t *ctx, sofab_id_t id, const uint8_t *var, int32_t element_count)
Writes an array of 8-bit unsigned integers.
Definition ostream.h:502
static sofab_ret_t sofab_ostream_write_array_of_fp32(sofab_ostream_t *ctx, sofab_id_t id, const float *data, int32_t element_count)
Write an array of 32-bit floating-point values.
Definition ostream.h:617
sofab_ret_t sofab_ostream_write_signed(sofab_ostream_t *ctx, sofab_id_t id, sofab_signed_t value)
Write a signed integer field.
static sofab_ret_t sofab_ostream_write_array_of_u64(sofab_ostream_t *ctx, sofab_id_t id, const uint64_t *var, int32_t element_count)
Writes an array of 64-bit unsigned integers.
Definition ostream.h:599
void(* sofab_ostream_flush_cb_t)(sofab_ostream_t *ctx, const uint8_t *data, size_t len, void *usrptr)
Output stream flush callback type.
Definition ostream.h:87
uint32_t sofab_id_t
Field identifier type.
Definition sofab.h:106
static sofab_ret_t sofab_ostream_write_fp64(sofab_ostream_t *ctx, sofab_id_t id, double value)
Write a 64-bit floating-point value.
Definition ostream.h:363
sofab_ret_t sofab_ostream_write_array_of_signed(sofab_ostream_t *ctx, sofab_id_t id, const void *data, int32_t element_count, int32_t element_size)
Write an array of signed integers.
uint64_t sofab_unsigned_t
Scalar varint value types.
Definition sofab.h:118
static sofab_ret_t sofab_ostream_write_fp32(sofab_ostream_t *ctx, sofab_id_t id, float value)
Write a 32-bit floating-point value.
Definition ostream.h:347
sofab_ret_t sofab_ostream_write_array_of_fixlen(sofab_ostream_t *ctx, sofab_id_t id, const void *data, int32_t element_count, int32_t element_size, sofab_fixlentype_t type)
Write an array of fixed-length elements.
void sofab_ostream_buffer_set(sofab_ostream_t *ctx, uint8_t *buffer, size_t buflen, size_t offset)
Replace the active buffer used by the output stream.
size_t sofab_ostream_bytes_used(sofab_ostream_t *ctx)
Get the number of bytes used in the current buffer.
static sofab_ret_t sofab_ostream_write_array_of_i8(sofab_ostream_t *ctx, sofab_id_t id, const int8_t *var, int32_t element_count)
Writes an array of 8-bit signed integers.
Definition ostream.h:486
sofab_ret_t sofab_ostream_write_sequence_end_keep(sofab_ostream_t *ctx)
End a nested sequence, keeping its frame even without content.
void sofab_ostream_init(sofab_ostream_t *ctx, uint8_t *buffer, size_t buflen, size_t offset, sofab_ostream_flush_cb_t flush, void *usrptr)
Initialize an Sofab output stream context.
static sofab_ret_t sofab_ostream_write_string(sofab_ostream_t *ctx, sofab_id_t id, const char *text)
Write a null-terminated string.
Definition ostream.h:382
static sofab_ret_t sofab_ostream_write_array_of_fp64(sofab_ostream_t *ctx, sofab_id_t id, const double *data, int32_t element_count)
Write an array of 64-bit floating-point values.
Definition ostream.h:635
sofab_ret_t sofab_ostream_write_unsigned(sofab_ostream_t *ctx, sofab_id_t id, sofab_unsigned_t value)
Write an unsigned integer field.
sofab_ret_t
Return status codes.
Definition sofab.h:39
static sofab_ret_t sofab_ostream_write_array_of_u32(sofab_ostream_t *ctx, sofab_id_t id, const uint32_t *var, int32_t element_count)
Writes an array of 32-bit unsigned integers.
Definition ostream.h:566
int64_t sofab_signed_t
Signed value type.
Definition sofab.h:122
static sofab_ret_t sofab_ostream_write_boolean(sofab_ostream_t *ctx, sofab_id_t id, bool value)
Write a boolean value (1 byte).
Definition ostream.h:331
static sofab_ret_t sofab_ostream_write_array_of_i32(sofab_ostream_t *ctx, sofab_id_t id, const int32_t *var, int32_t element_count)
Writes an array of 32-bit signed integers.
Definition ostream.h:550
sofab_ret_t sofab_ostream_write_sequence_begin(sofab_ostream_t *ctx, sofab_id_t id)
Begin encoding a nested sequence (start marker).
static sofab_ret_t sofab_ostream_write_blob(sofab_ostream_t *ctx, sofab_id_t id, const void *data, size_t size)
Write a binary blob field.
Definition ostream.h:400
@ SOFAB_FIXLENTYPE_FP32
Definition sofab.h:99
@ SOFAB_FIXLENTYPE_STRING
Definition sofab.h:101
@ SOFAB_FIXLENTYPE_FP64
Definition sofab.h:100
@ SOFAB_FIXLENTYPE_BLOB
Definition sofab.h:102
SofaBuffers C - Type definitions and constants.
sofab_ostream_flush_cb_t flush
Definition ostream.h:151
uint8_t * offset
Definition ostream.h:150
sofab_id_t pending[SOFAB_LAZY_SEQ_DEPTH]
Definition ostream.h:166
uint8_t * buffer
Definition ostream.h:148
void * usrptr
Definition ostream.h:152
uint8_t * bufend
Definition ostream.h:149
uint8_t npending
Definition ostream.h:167