SofaBuffers C/C++ 0.12.0
API Documentation
Loading...
Searching...
No Matches
object.h
Go to the documentation of this file.
1/*!
2 * @file object.h
3 * @brief SofaBuffers C - Object encoder and decoder.
4 *
5 * This module implements a generic message encoder and decoder.
6 * Since programmatic transcoding of messages using the corelib API can
7 * generate a lot of program code under certain circumstances,
8 * this generic transcoder helps to keep the footprint small.
9 * Instead of API calls, a constant message description is used to
10 * serialize and deserialize messages.
11 * For large messages, these descriptions are smaller than the code
12 * required for API calls. Even in projects with many messages,
13 * program code can be saved by reusing the transcoder multiple times.
14 * For small embedded projects with few small messages, however,
15 * it may make sense to use the corelib without this transcoder.
16 *
17 * Typically, this module is used from generated code that defines
18 * the message structures and their descriptors.
19 *
20 * SPDX-License-Identifier: MIT
21 */
22
23#ifndef SOFAB_OBJECT_H
24#define SOFAB_OBJECT_H
25
26/**
27 * @defgroup c_api C API
28 * @{
29 */
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
35#ifndef SOFAB_OBJECT_C
36# define SOFAB_OBJECT_EXTERN extern
37#else
38# define SOFAB_OBJECT_EXTERN
39#endif
40
41/* includes *******************************************************************/
42#include <stddef.h>
43
44#include "sofab/istream.h"
45#include "sofab/ostream.h"
46
47/* constants ******************************************************************/
48/*!
49 * @name Object field types
50 * @brief Field type tags stored in @ref sofab_object_descr_field_t::type.
51 *
52 * Each descriptor field carries one of these tags; it selects which
53 * encode/decode path the transcoder takes for that struct member. Pass the tag
54 * as the @c type argument of the @ref SOFAB_OBJECT_FIELD family of macros.
55 * @{
56 */
57#define SOFAB_OBJECT_FIELDTYPE_UNSIGNED 0x0 /*!< Unsigned integer (uint8/16/32/64). */
58#define SOFAB_OBJECT_FIELDTYPE_SIGNED 0x1 /*!< Signed integer (int8/16/32/64). */
59#define SOFAB_OBJECT_FIELDTYPE_FP32 0x2 /*!< 32-bit floating point (float). */
60#define SOFAB_OBJECT_FIELDTYPE_FP64 0x3 /*!< 64-bit floating point (double). */
61#define SOFAB_OBJECT_FIELDTYPE_STRING 0x4 /*!< Null-terminated string. */
62#define SOFAB_OBJECT_FIELDTYPE_BLOB 0x5 /*!< Raw binary blob. Fixed full-capacity by default; a variable used-length variant is built with @ref SOFAB_OBJECT_FIELD_BLOB_SIZED (flagged via a non-zero @c nested_idx). */
63#define SOFAB_OBJECT_FIELDTYPE_ARRAY_UNSIGNED 0x6 /*!< Array of unsigned integers. Fixed full-capacity by default; a length-carrying variant is built with @ref SOFAB_OBJECT_FIELD_ARRAY_SIZED (flagged via a non-zero @c nested_idx). */
64#define SOFAB_OBJECT_FIELDTYPE_ARRAY_SIGNED 0x7 /*!< Array of signed integers (sized variant as above). */
65#define SOFAB_OBJECT_FIELDTYPE_ARRAY_FP32 0x8 /*!< Array of 32-bit floats (sized variant as above). */
66#define SOFAB_OBJECT_FIELDTYPE_ARRAY_FP64 0x9 /*!< Array of 64-bit doubles (sized variant as above). */
67#define SOFAB_OBJECT_FIELDTYPE_ARRAY_BOOLEAN 0xA /*!< Array of booleans (sized variant as above); each element follows the §4.4 rule. */
68#define SOFAB_OBJECT_FIELDTYPE_SEQUENCE 0xB /*!< Nested object (encoded as a sequence). */
69#define SOFAB_OBJECT_FIELDTYPE_BOOLEAN 0xC /*!< Boolean. Rides the unsigned varint wire type, but every non-zero value decodes to @c true and is normalized (CORELIB_PLAN §4.4). */
70/*! @} */
71
72/*
73 * A note for whoever adds the fourteenth tag: no *semantics* hang off a tag's
74 * value. Nothing derives "this is an array" from a tag being numerically high —
75 * the transcoder asks @ref _expected_opt what wire form the type takes and reads
76 * the answer off that. The boolean tags are why: a scalar cannot be appended
77 * after the array tags while a numeric test decides the two apart.
78 *
79 * Their *order*, though, is not free. The tags carrying a companion length in
80 * @c nested_idx — BLOB and every ARRAY_* — are deliberately contiguous (0x5..0xA)
81 * so @ref _sized_width is one range check. Scatter them and it becomes a compare
82 * chain that -Os declines to inline at its five call sites, which costs ~600
83 * instructions per message on a 434 B one. So: a sized tag goes at the end of
84 * that run, an unsized one after it. Measured, not assumed — see the PR that
85 * moved ARRAY_BOOLEAN out of 0xC.
86 */
87
88/* macros *********************************************************************/
89/*!
90 * @brief Build a scalar field descriptor (@ref sofab_object_descr_field_t).
91 *
92 * Derives the field offset and size from the struct type, so the descriptor
93 * stays in sync with the C declaration. The element size is taken as the size
94 * of the whole field (scalars are a single element).
95 *
96 * @param id Field ID on the wire.
97 * @param obj Enclosing struct type (e.g. @c struct my_msg).
98 * @param field Member name within @p obj.
99 * @param type Field type tag (one of the scalar @ref SOFAB_OBJECT_FIELDTYPE_UNSIGNED "SOFAB_OBJECT_FIELDTYPE_*").
100 */
101#define SOFAB_OBJECT_FIELD(id, obj, field, type) \
102 { id, offsetof(obj, field) + SOFAB_OBJECT_ASSERT_FITS_PROFILE(obj, field, id), \
103 sizeof(((obj *)0)->field), 0, type, (sizeof(((obj *)0)->field) & 0xF) }
104
105/*!
106 * @brief Build a variable-length blob field descriptor (reuses the BLOB type).
107 *
108 * A sized blob pairs a fixed-capacity buffer @p dfield (@c sizeof(dfield) bytes)
109 * with a companion length member @p lfield holding how many bytes are actually
110 * used (@c 0..sizeof(dfield)). On encode only @p lfield bytes reach the wire; on
111 * decode the received length is stored back into @p lfield. The buffer capacity
112 * never reaches the wire. This is the C counterpart of C++ @c sofab::FixedBytes,
113 * and it produces byte-identical wire to a plain blob of the same actual length.
114 *
115 * @warning @p lfield @b must immediately precede @p dfield in @p obj, i.e.
116 * @c offsetof(obj,dfield) @c == @c offsetof(obj,lfield)+sizeof(lfield); declare
117 * them adjacently as @c { uintX lfield; uint8_t dfield[N]; }. Placing the length
118 * @b before the buffer is alignment-robust: a byte buffer (alignment 1) always
119 * abuts the length with no padding, for any @p lfield width and any @c N (a
120 * length placed after the buffer could be padded away from @c offset+size). The
121 * companion width @c sizeof(lfield) (one of 1/2/4/8) is stored in the
122 * descriptor's @c nested_idx slot, which also flags the blob as sized: a plain
123 * @ref SOFAB_OBJECT_FIELD blob keeps @c nested_idx @c == @c 0 and its original
124 * fixed full-capacity behaviour.
125 *
126 * @param id Field ID on the wire.
127 * @param obj Enclosing struct type.
128 * @param dfield Blob buffer member within @p obj (its size is the capacity).
129 * @param lfield Used-length member, declared immediately before @p dfield.
130 */
131#define SOFAB_OBJECT_FIELD_BLOB_SIZED(id, obj, dfield, lfield) \
132 { id, offsetof(obj, dfield) + SOFAB_OBJECT_ASSERT_FITS_PROFILE(obj, dfield, id), \
133 sizeof(((obj *)0)->dfield), \
134 (uint8_t)(sizeof(((obj *)0)->lfield) \
135 + SOFAB_OBJECT_ASSERT_LEN_ADJACENT(obj, dfield, lfield)), \
136 SOFAB_OBJECT_FIELDTYPE_BLOB, \
137 (sizeof(((obj *)0)->dfield) & 0xF) }
138
139/*!
140 * @brief Compile-time check that a field fits the configured descriptor profile.
141 *
142 * @ref SOFAB_OBJECT_DESCR_PROFILE narrows the descriptor's @c id, @c offset and
143 * @c size members (SMALL: 8 bit, MEDIUM: 16, BIG: 32). A field past the
144 * resulting ceiling does not merely fail to be described -- its offset is
145 * silently truncated, so the walk reads and writes the wrong member. The
146 * compiler's own @c -Woverflow catches the narrowing only as a warning, and only
147 * where it is enabled; this makes it an error that names the cause, at no cost
148 * to the descriptor.
149 *
150 * The test is the narrowing itself -- does the value survive the round trip
151 * through the profile's type? -- not a comparison against the ceiling. A range
152 * test is tautological wherever @c size_t is no wider than the member (BIG on a
153 * 32-bit target), which @c -Wtype-limits rejects; the round trip is the same
154 * question and is never trivially true.
155 */
156#define SOFAB_OBJECT_ASSERT_FITS_PROFILE(obj, field, id) \
157 (0u * sizeof(struct { int field_exceeds_the_SOFAB_OBJECT_DESCR_PROFILE_id_offset_or_size_ceiling \
158 : ((size_t)(sofab_object_descr_offset_t)(offsetof(obj, field)) \
159 == (size_t)(offsetof(obj, field)) \
160 && (size_t)(sofab_object_descr_size_t)(sizeof(((obj *)0)->field)) \
161 == (size_t)(sizeof(((obj *)0)->field)) \
162 && (size_t)(sofab_object_descr_id_t)(id) == (size_t)(id)) ? 1 : -1; }))
163
164/*!
165 * @brief Compile-time check that @p lfield immediately precedes @p dfield.
166 *
167 * Evaluates to 0, or fails to compile (negative array bound) when the two
168 * members are not adjacent — i.e. when the compiler inserted padding between
169 * them.
170 *
171 * Used by the @b field-level @c *_SIZED macros (@ref SOFAB_OBJECT_FIELD_BLOB_SIZED,
172 * @ref SOFAB_OBJECT_FIELD_ARRAY_SIZED), and only by them. A field descriptor
173 * knows one address — the field's own @c offset inside the enclosing object — so
174 * "immediately before the storage" is the only anchor available to it, and the
175 * adjacency has to hold. An @b object descriptor is not in that position: it
176 * describes the whole object, so it can anchor at the object's start instead, and
177 * @ref SOFAB_OBJECT_DESCR_SEQ_SIZED does exactly that
178 * (@ref SOFAB_OBJECT_ASSERT_LEN_FIRST).
179 */
180#define SOFAB_OBJECT_ASSERT_LEN_ADJACENT(obj, dfield, lfield) \
181 (0 * sizeof(struct { int length_member_must_sit_immediately_before_the_data_member \
182 : (offsetof(obj, dfield) \
183 == offsetof(obj, lfield) + sizeof(((obj *)0)->lfield)) ? 1 : -1; }))
184
185/*!
186 * @brief Compile-time check that @p lfield is the @b first member of @p obj.
187 *
188 * Evaluates to 0, or fails to compile (negative array bound) when @p lfield does
189 * not sit at offset 0. Used by @ref SOFAB_OBJECT_DESCR_SEQ_SIZED, whose
190 * descriptor records only the count's @e width and locates it at offset 0 of the
191 * holder.
192 */
193#define SOFAB_OBJECT_ASSERT_LEN_FIRST(obj, lfield) \
194 (0 * sizeof(struct { int length_member_must_be_the_first_member_of_the_object \
195 : (offsetof(obj, lfield) == 0) ? 1 : -1; }))
196
197/*!
198 * @brief Build a nested-object (sequence) field descriptor.
199 *
200 * Like @ref SOFAB_OBJECT_FIELD but for a nested struct serialized as a
201 * sequence. @p idx selects the child descriptor from the enclosing object's
202 * @c nested_list.
203 *
204 * @param id Field ID on the wire.
205 * @param obj Enclosing struct type.
206 * @param field Nested struct member within @p obj.
207 * @param type Field type tag (typically @ref SOFAB_OBJECT_FIELDTYPE_SEQUENCE).
208 * @param idx Index of the nested descriptor in the @c nested_list.
209 */
210#define SOFAB_OBJECT_FIELD_SEQUENCE(id, obj, field, type, idx) \
211 { id, offsetof(obj, field) + SOFAB_OBJECT_ASSERT_FITS_PROFILE(obj, field, id), \
212 sizeof(((obj *)0)->field), idx, type, (sizeof(((obj *)0)->field) & 0xF) }
213
214/*!
215 * @brief Build an array field descriptor.
216 *
217 * Like @ref SOFAB_OBJECT_FIELD but the element size is taken from a single
218 * array element (@c field[0]); the element count is derived at run time from
219 * the total field size divided by the element size.
220 *
221 * @param id Field ID on the wire.
222 * @param obj Enclosing struct type.
223 * @param field Array member within @p obj.
224 * @param type Field type tag (one of the array @ref SOFAB_OBJECT_FIELDTYPE_ARRAY_UNSIGNED "SOFAB_OBJECT_FIELDTYPE_ARRAY_*").
225 */
226#define SOFAB_OBJECT_FIELD_ARRAY(id, obj, field, type) \
227 { id, offsetof(obj, field) + SOFAB_OBJECT_ASSERT_FITS_PROFILE(obj, field, id), \
228 sizeof(((obj *)0)->field), 0, type, (sizeof(((obj *)0)->field[0]) & 0xF) }
229
230/*!
231 * @brief Build a length-carrying (sized) array field descriptor.
232 *
233 * A plain @ref SOFAB_OBJECT_FIELD_ARRAY has nowhere to keep a length: it derives
234 * the element count from @c sizeof(field)/sizeof(field[0]), which is the array's
235 * @b capacity. MESSAGE_SPEC §3 makes the schema @c count a capacity too and the
236 * wire count @c M the array's @b length, with no element elided — so a capacity-
237 * only object can never hold an array shorter than @c N, and a decode of
238 * @c M @c < @c N followed by a re-encode silently lengthens the value back to
239 * @c N. A sized array fixes that the same way @ref SOFAB_OBJECT_FIELD_BLOB_SIZED
240 * fixes it for a blob: it pairs the fixed-capacity array @p dfield with a
241 * companion length member @p lfield holding how many elements are actually used
242 * (@c 0..N). On encode exactly @p lfield elements reach the wire — a trailing
243 * element equal to the element default included, because @c M is the length; on
244 * decode the received count is stored back into @p lfield. The capacity never
245 * reaches the wire.
246 *
247 * @warning @p lfield @b must immediately precede @p dfield in @p obj, and the
248 * two must be @b adjacent — @c offsetof(obj,dfield) @c == @c
249 * offsetof(obj,lfield)+sizeof(lfield). For a sized @e blob that is automatic: a
250 * byte buffer has alignment 1, so it abuts any length width. It is @b not
251 * automatic here. An element wider than a byte carries a stricter alignment, so a
252 * @e narrower length in front of it is padded away — @c { @c uint8_t @c len; @c
253 * uint32_t @c vals[4]; @c } places @c vals at offset 4, three bytes past the
254 * length, and the descriptor (which stores only the length's @e width and reads
255 * it at @c offset @c - @c width) would address the padding instead. This macro
256 * therefore does not assume the invariant, it @b asserts it: the adjacency is
257 * checked at compile time (@ref SOFAB_OBJECT_ASSERT_LEN_ADJACENT), so a padded
258 * pair is a build error rather than a silent misread. Declare the length at least
259 * as wide as one element — @c { @c uint32_t @c len; @c uint32_t @c vals[4]; @c }
260 * — or otherwise arrange the two members to leave no gap.
261 *
262 * The companion width @c sizeof(lfield) (one of 1/2/4/8) is stored in the
263 * descriptor's @c nested_idx slot, which also flags the array as sized: a plain
264 * @ref SOFAB_OBJECT_FIELD_ARRAY keeps @c nested_idx @c == @c 0 and encodes its
265 * full capacity.
266 *
267 * @param id Field ID on the wire.
268 * @param obj Enclosing struct type.
269 * @param dfield Array member within @p obj (its size is the capacity).
270 * @param lfield Used-length member (in elements), declared immediately before
271 * @p dfield.
272 * @param type Field type tag (one of the array @ref SOFAB_OBJECT_FIELDTYPE_ARRAY_UNSIGNED "SOFAB_OBJECT_FIELDTYPE_ARRAY_*").
273 */
274#define SOFAB_OBJECT_FIELD_ARRAY_SIZED(id, obj, dfield, lfield, type) \
275 { id, offsetof(obj, dfield) + SOFAB_OBJECT_ASSERT_FITS_PROFILE(obj, dfield, id), \
276 sizeof(((obj *)0)->dfield), \
277 (uint8_t)(sizeof(((obj *)0)->lfield) \
278 + SOFAB_OBJECT_ASSERT_LEN_ADJACENT(obj, dfield, lfield)), \
279 type, (sizeof(((obj *)0)->dfield[0]) & 0xF) }
280
281/*!
282 * @name Count-word ceilings (@ref sofab_object_descr_t::count_extent)
283 *
284 * What the 16-bit count word can describe. Generated code can hold a schema
285 * against these at compile time the way it holds a field id against
286 * @ref SOFAB_ID_MAX, instead of discovering the ceiling as an assertion failure
287 * inside this header -- which is why they are macros and not just prose.
288 * @{
289 */
290/*! @brief Fields an object (message, struct, union) may declare. */
291#define SOFAB_OBJECT_DESCR_FIELD_COUNT_MAX 255u
292/*! @brief Bytes a PREFIX default image may describe (@ref SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS). */
293#define SOFAB_OBJECT_DESCR_EXTENT_MAX 255u
294/*! @brief Element slots a wrapper-array holder may declare (@ref SOFAB_OBJECT_DESCR_SEQ). */
295#define SOFAB_OBJECT_DESCR_SLOT_COUNT_MAX 65535u
296/*! @brief Position of the extent inside the count word. */
297#define SOFAB_OBJECT_DESCR_EXTENT_SHIFT 8
298/*! @} */
299
300/*!
301 * @brief Compile-time limit on an OBJECT's field count (message, struct, union).
302 *
303 * The low byte of @ref sofab_object_descr_t::count_extent holds it, so
304 * @ref SOFAB_OBJECT_DESCR_FIELD_COUNT_MAX is the ceiling and a 256-field object
305 * fails to compile here instead of wrapping silently. A wrapper-array HOLDER is
306 * not affected: its slot count uses all 16 bits
307 * (@ref SOFAB_OBJECT_ASSERT_SLOT_COUNT).
308 */
309#define SOFAB_OBJECT_ASSERT_FIELD_COUNT(n) \
310 (0u * sizeof(struct { int an_object_may_not_declare_more_than_255_fields \
311 : ((n) <= SOFAB_OBJECT_DESCR_FIELD_COUNT_MAX) ? 1 : -1; }))
312
313/*!
314 * @brief Compile-time limit on a PREFIX default image's byte extent.
315 *
316 * The high byte of @ref sofab_object_descr_t::count_extent holds the extent, and
317 * an extent of 0 reads as "the image covers the whole object". A prefix longer
318 * than @ref SOFAB_OBJECT_DESCR_EXTENT_MAX therefore has no extent that describes
319 * it: recorded as 0, every field past the prefix would be seeded and compared
320 * through the image, reading past its end. @ref SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS
321 * refuses it here instead. A whole-object image of any length is not affected --
322 * @ref SOFAB_OBJECT_DESCR_WITH_DEFAULTS records 0 for it, which is exactly right.
323 */
324#define SOFAB_OBJECT_ASSERT_EXTENT(n) \
325 (0u * sizeof(struct { int a_prefix_default_image_may_not_exceed_255_bytes \
326 : ((n) <= SOFAB_OBJECT_DESCR_EXTENT_MAX) ? 1 : -1; }))
327
328/*!
329 * @brief Compile-time limit on a wrapper-array holder's element slot count.
330 *
331 * A holder spends all 16 bits of @ref sofab_object_descr_t::count_extent on its
332 * slot count, so @ref SOFAB_OBJECT_DESCR_SLOT_COUNT_MAX is the ceiling. Named
333 * here rather than left to the narrowing in the initialiser: @c -Woverflow
334 * reports that only where it is enabled, and a holder whose count wrapped walks
335 * a fraction of its slots and rejects every element id above them as an
336 * over-index (MESSAGE_SPEC §7.1) -- a silent change of meaning, not a build
337 * failure.
338 */
339#define SOFAB_OBJECT_ASSERT_SLOT_COUNT(n) \
340 (0u * sizeof(struct { int a_wrapper_array_may_not_declare_more_than_65535_slots \
341 : ((n) <= SOFAB_OBJECT_DESCR_SLOT_COUNT_MAX) ? 1 : -1; }))
342
343/*!
344 * @brief The @ref sofab_object_descr_t::count_extent word of an object: the
345 * field count in the low byte, the default image's byte extent in the
346 * high byte.
347 *
348 * An extent of 0 reads as "the image covers the whole object": what every
349 * descriptor written before the extent existed carries, what a union image uses,
350 * and what an image too long for the high byte is recorded as. That last one is
351 * right for a whole-object image -- the form every generated file before the
352 * extent hands over, at any size -- and wrong for a prefix, which is why a
353 * prefix goes through @ref SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS and is
354 * refused there (@ref SOFAB_OBJECT_ASSERT_EXTENT) rather than clamped here.
355 *
356 * Generated code tests for @ref SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS, the
357 * macro it calls, to tell a corelib that reads prefix images from one that does
358 * not -- never for this helper.
359 */
360#define SOFAB_OBJECT_DESCR_COUNT_WORD(field_count, extent) \
361 ((uint16_t)(((field_count) + SOFAB_OBJECT_ASSERT_FIELD_COUNT(field_count)) \
362 | (uint16_t)(((extent) <= SOFAB_OBJECT_DESCR_EXTENT_MAX ? (extent) : 0u) \
363 << SOFAB_OBJECT_DESCR_EXTENT_SHIFT)))
364
365/*!
366 * @brief Build an object descriptor (@ref sofab_object_descr_t) without defaults.
367 *
368 * Fields whose value is all-zero are omitted from the encoding (see
369 * @ref sofab_object_encode).
370 *
371 * @param field_list Array of @ref sofab_object_descr_field_t for this object.
372 * @param field_count Number of entries in @p field_list.
373 * @param nested_list Array of pointers to nested @ref sofab_object_descr_t (may be NULL).
374 * @param nested_count Number of entries in @p nested_list.
375 */
376#define SOFAB_OBJECT_DESCR(field_list, field_count, nested_list, nested_count) \
377 { (field_list), (nested_list), NULL, SOFAB_OBJECT_DESCR_COUNT_WORD(field_count, 0u), (nested_count), 0 }
378
379/*!
380 * @brief Build an object descriptor with a default-values reference.
381 *
382 * As @ref SOFAB_OBJECT_DESCR, but fields equal to the corresponding member in
383 * @p default_struct are omitted from the encoding (instead of comparing against
384 * zero), and @ref sofab_object_init seeds objects from it.
385 *
386 * @param field_list Array of @ref sofab_object_descr_field_t for this object.
387 * @param field_count Number of entries in @p field_list.
388 * @param nested_list Array of pointers to nested @ref sofab_object_descr_t (may be NULL).
389 * @param nested_count Number of entries in @p nested_list.
390 * @param default_struct Pointer to a fully-populated object holding the field
391 * defaults. @c sizeof(*default_struct) is recorded as the
392 * extent, or 0 ("the whole object") when it is longer than
393 * @ref SOFAB_OBJECT_DESCR_EXTENT_MAX -- both mean the same
394 * for a whole-object image, so this form takes one of any
395 * size, as it always has. A PREFIX of the object goes
396 * through @ref SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS: here,
397 * one longer than the extent byte would be read as the whole
398 * object and past its own end.
399 */
400#define SOFAB_OBJECT_DESCR_WITH_DEFAULTS(field_list, field_count,nested_list, nested_count, default_struct) \
401 { (field_list), (nested_list), (default_struct), \
402 SOFAB_OBJECT_DESCR_COUNT_WORD(field_count, sizeof(*(default_struct))), (nested_count), 0 }
403
404/*!
405 * @brief Build an object descriptor whose default image is a PREFIX of the object.
406 *
407 * As @ref SOFAB_OBJECT_DESCR_WITH_DEFAULTS, but @p default_prefix need not be a
408 * whole object: any prefix of one will do, as long as every field it describes
409 * sits at the same offset as in the object itself, because that is how the
410 * fields are addressed. @c sizeof(*default_prefix) is recorded as the extent,
411 * and a field at or past it is seeded with and compared against zero -- which is
412 * what "no declared default" means. This is what the SofaBuffers generator emits
413 * (the defaulted members first): a message that defaults one byte costs one byte
414 * of @c .rodata instead of a copy of the whole object.
415 *
416 * The prefix may be at most @ref SOFAB_OBJECT_DESCR_EXTENT_MAX bytes; a longer
417 * one is a build error (@ref SOFAB_OBJECT_ASSERT_EXTENT), because the extent has
418 * no value that says how far it reaches. Hand over the whole object through
419 * @ref SOFAB_OBJECT_DESCR_WITH_DEFAULTS instead.
420 *
421 * @param field_list Array of @ref sofab_object_descr_field_t for this object.
422 * @param field_count Number of entries in @p field_list.
423 * @param nested_list Array of pointers to nested @ref sofab_object_descr_t (may be NULL).
424 * @param nested_count Number of entries in @p nested_list.
425 * @param default_prefix Pointer to a prefix of the object holding the field defaults.
426 */
427#define SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS(field_list, field_count, nested_list, nested_count, default_prefix) \
428 { (field_list), (nested_list), (default_prefix), \
429 SOFAB_OBJECT_DESCR_COUNT_WORD(field_count, sizeof(*(default_prefix)) \
430 + SOFAB_OBJECT_ASSERT_EXTENT(sizeof(*(default_prefix)))), \
431 (nested_count), 0 }
432
433/*!
434 * @name Wrapper-array holder marker (@ref sofab_object_descr_t::fixed_seq)
435 *
436 * The @c fixed_seq slot packs two things: bit 0 flags the descriptor as a
437 * wrapper-array holder, and the remaining bits carry the byte width of the
438 * holder's companion element-count member (0 when it has none). It is the
439 * holder-level counterpart of the way @c nested_idx doubles as the "is sized"
440 * flag and the length width for @ref SOFAB_OBJECT_FIELD_BLOB_SIZED /
441 * @ref SOFAB_OBJECT_FIELD_ARRAY_SIZED — with the flag kept in bit 0 so that every
442 * plain "is this a holder?" test stays a truth test.
443 * @{
444 */
445#define SOFAB_OBJECT_SEQ_HOLDER 0x01u /*!< Bit 0: this descriptor is a wrapper-array holder. */
446#define SOFAB_OBJECT_SEQ_LEN_SHIFT 1 /*!< Shift of the length member's byte width (0 = unsized). */
447#define SOFAB_OBJECT_UNION 0x80u /*!< Bit 7: a union; its tag is a sofab_object_descr_id_t at offset 0. */
448/*! @} */
449
450/*!
451 * @brief Build a fixed-capacity sequence-holder object descriptor.
452 *
453 * Like @ref SOFAB_OBJECT_DESCR, but marks the descriptor as a wrapper-array
454 * holder: one whose fields are exactly the element slots
455 * @c 0 … @p field_count - 1 of a bounded @c string / @c blob / @c struct /
456 * @c union array (which lowers to a wrapper sequence). For such a descriptor a
457 * wire element id outside that range is an over-index element and is rejected as
458 * @ref SOFAB_RET_E_INVALID_MSG on decode (MESSAGE_SPEC §7/§7.1), rather than
459 * silently skipped the way a message ignores an unknown forward-compatible id.
460 *
461 * That bound applies only to a field that survives MESSAGE_SPEC §7.3: an element
462 * header whose wire type — or, for a @c fixlen element type, whose fixlen subtype
463 * — contradicts the declared element type is skipped exactly as an unknown id is,
464 * so its id is not an array index at all and nothing is left for the bound to
465 * reject ("against a schema bound, this clause wins"). The element type tested
466 * against is the one all slots share, taken from slot 0.
467 *
468 * The flag also selects the **positional** sparse rule of MESSAGE_SPEC §2/§5.1
469 * on encode: inside a holder an element at an @e interior index equal to its
470 * default is omitted (leaving an id gap) whatever its kind, and the element at
471 * the @e last index is always written, because a wrapper carries no length and
472 * *highest present id + 1* is what recovers it.
473 *
474 * This macro builds the **un-sized** holder: it has no length member, so its
475 * @p field_count slots are all materialized, the last index is
476 * @c field_count @c - @c 1 unconditionally, and the only two lengths it can
477 * express are @c 0 (every slot default — the enclosing object omits the whole
478 * field) and @c field_count. Use @ref SOFAB_OBJECT_DESCR_SEQ_SIZED to express
479 * @c 0 … @c N like every other target.
480 *
481 * @param field_list Array of @ref sofab_object_descr_field_t (the element slots).
482 * @param field_count Number of element slots (the array capacity N).
483 * @param nested_list Array of pointers to nested @ref sofab_object_descr_t (may be NULL).
484 * @param nested_count Number of entries in @p nested_list.
485 */
486#define SOFAB_OBJECT_DESCR_SEQ(field_list, field_count, nested_list, nested_count) \
487 { (field_list), (nested_list), NULL, \
488 (uint16_t)((field_count) + SOFAB_OBJECT_ASSERT_SLOT_COUNT(field_count)), \
489 (nested_count), SOFAB_OBJECT_SEQ_HOLDER }
490
491/*!
492 * @brief Build a length-carrying (sized) sequence-holder object descriptor.
493 *
494 * The wrapper-array counterpart of @ref SOFAB_OBJECT_FIELD_ARRAY_SIZED, and it
495 * closes the same gap one level up. An un-sized holder
496 * (@ref SOFAB_OBJECT_DESCR_SEQ) materializes all @p field_count element slots and
497 * carries no length, so it can express only two lengths — @c 0 and @c N — while
498 * MESSAGE_SPEC §5.1 gives a wrapper array the length *highest present id + 1*,
499 * i.e. any of @c 0 … @c N. A sized holder adds the missing member: @p lfield holds
500 * how many elements are actually used, exactly as a sized blob's companion holds
501 * how many bytes are.
502 *
503 * What it buys, per MESSAGE_SPEC §2/§5.1:
504 * - **encode** walks the slots @c [0, @p lfield) only — the last index is
505 * @c lfield @c - @c 1, not @c field_count @c - @c 1. An interior slot equal to
506 * its element default is omitted (id gap) whatever its kind; the slot at
507 * @c lfield @c - @c 1 is always written (a leaf as its value, a sequence element
508 * as an empty frame), because that is what carries the length. @c lfield @c ==
509 * @c 0 is the empty array and the enclosing object omits the field entirely.
510 * - **decode** stores the received length — *highest present element id + 1* —
511 * back into @p lfield, so a received @c [{k:1}] re-encodes as one element
512 * instead of silently growing back to @c N. "Present" is an element that was
513 * actually bound: one skipped because its wire type contradicts the declared
514 * element type is skipped like an unknown id (§7.3) and counts for nothing, an
515 * empty one (empty frame, empty string) is present and counts.
516 *
517 * @warning @p lfield @b must be the @b first member of @p obj — @c
518 * offsetof(obj,lfield) @c == @c 0. The descriptor stores only the count's
519 * @e width and reads it at <b>offset 0 of the holder</b>, which is where this
520 * macro requires it to be; the check is made at compile time
521 * (@ref SOFAB_OBJECT_ASSERT_LEN_FIRST) rather than assumed.
522 *
523 * Anchoring at the holder's start — instead of at <em>first slot offset −
524 * width</em>, the convention the @b field-level
525 * @ref SOFAB_OBJECT_FIELD_BLOB_SIZED / @ref SOFAB_OBJECT_FIELD_ARRAY_SIZED must
526 * keep — is what makes the count work for @b every element kind. The byte before
527 * slot 0 is not always free: a blob element and a native inner-array row are
528 * themselves SIZED, so each slot @e starts with its own used-length, and a count
529 * placed in front of the slots is read as that element's length instead. Offset 0
530 * of the holder has no such competition and needs no padding argument either: the
531 * count may be narrower than the slots' alignment, the padding simply sits between
532 * them. That also drops the old "declare the count at least as wide as the slot's
533 * alignment" requirement.
534 *
535 * The width @c sizeof(lfield) (one of 1/2/4/8) is stored in the descriptor's
536 * @c fixed_seq slot above the holder flag (@ref SOFAB_OBJECT_SEQ_LEN_SHIFT); a
537 * plain @ref SOFAB_OBJECT_DESCR_SEQ keeps that width @c 0 and its original
538 * full-capacity behaviour. Like @ref SOFAB_OBJECT_DESCR_SEQ this descriptor
539 * carries no default image, so the holder's declared default is the @b empty
540 * array: @ref sofab_object_init clears @p lfield to 0, and the enclosing
541 * ≠-default test reads "is default" as @c lfield @c == @c 0 (never a scan of the
542 * slots, whose content past the used length is indeterminate — the same rule a
543 * sized blob and a sized array follow).
544 *
545 * @param field_list Array of @ref sofab_object_descr_field_t (the element slots).
546 * @param field_count Number of element slots (the array capacity N).
547 * @param nested_list Array of pointers to nested @ref sofab_object_descr_t (may be NULL).
548 * @param nested_count Number of entries in @p nested_list.
549 * @param obj The holder struct type.
550 * @param lfield Used-length member (in elements), declared @b first in
551 * @p obj (at offset 0), ahead of the element slots.
552 */
553#define SOFAB_OBJECT_DESCR_SEQ_SIZED(field_list, field_count, nested_list, nested_count, obj, lfield) \
554 { (field_list), (nested_list), NULL, \
555 (uint16_t)((field_count) + SOFAB_OBJECT_ASSERT_SLOT_COUNT(field_count)), (nested_count), \
556 (uint8_t)(SOFAB_OBJECT_SEQ_HOLDER \
557 | (sizeof(((obj *)0)->lfield) << SOFAB_OBJECT_SEQ_LEN_SHIFT) \
558 | 0u * sizeof(struct { int length_member_is_too_wide_to_share_fixed_seq_with_the_union_bit \
559 : ((sizeof(((obj *)0)->lfield) << SOFAB_OBJECT_SEQ_LEN_SHIFT) \
560 < SOFAB_OBJECT_UNION) ? 1 : -1; }) \
561 | SOFAB_OBJECT_ASSERT_LEN_FIRST(obj, lfield)) }
562
563/*!
564 * @brief Build a tagged-union object descriptor.
565 *
566 * Like @ref SOFAB_OBJECT_DESCR — one field descriptor per `oneof` option — but
567 * every option overlays the same storage (a C `union` inside @p obj), and the
568 * tag member @p tfield, a @c sofab_object_descr_id_t at offset 0 of @p obj,
569 * holds the id of the option the object currently holds. Only that option is
570 * initialised, compared against its default and encoded (MESSAGE_SPEC §4.2: a
571 * union frame carries at most one child); on decode the option received last
572 * wins, and switching to a sequence option starts it from its default.
573 *
574 * **Encode** (MESSAGE_SPEC §2/§4.2). The held option is the only child the
575 * union frame can carry. When it is the `default_id` option it is written like
576 * an ordinary field — omitted iff it equals its default, so a union holding
577 * `default_id` at its default is itself default and is omitted by its parent.
578 * Any other held option is written @b even @b at @b its @b own @b default: a
579 * scalar as its value, a string or sized blob as the empty value, a sized array
580 * as count 0 (a fixlen array keeps its @c fixlen_word), and a sequence option
581 * (struct, union or wrapper-array holder) as a present, possibly empty, frame.
582 * Otherwise the receiver, whose fresh union holds `default_id`, could not tell
583 * that another option was selected. A union holding a non-`default_id` option is
584 * therefore never default, one level up included.
585 *
586 * **Decode** (MESSAGE_SPEC §7.4.1). The tag switches at the point where a field
587 * is known to be bound — past the MESSAGE_SPEC §7.3 wire-type/subtype test — so a
588 * skipped child or an unknown id never switches the union. A switch to a
589 * sequence option starts it from its default (its own descriptor); the same
590 * option received again continues its scope (§7.4 merge); a leaf option is
591 * replaced whole by its payload — a scalar and a sized blob or array by their
592 * value and length, a string by its payload and terminator, an array's slots past
593 * the wire count by the element default the istream clears them to. A
594 * capacity-only blob option is the one form that could NOT be replaced whole,
595 * which is why it is not allowed (see below). Several children and re-opened
596 * frames are legal: the option received last wins.
597 *
598 * @p default_image is the object's default image, or @c NULL. It covers the tag
599 * and the `default_id` option @b only — a prefix of @p obj, never a full image:
600 * the corelib reads the tag (the `default_id`) and, for a leaf `default_id`
601 * option, that option's own bytes and companion length, and nothing else. A
602 * non-`default_id` option is never compared (it is forced) and a sequence option
603 * is always seeded through its own descriptor. So an image type is
604 * `struct { sofab_object_descr_id_t which; }` when `default_id` names a sequence
605 * option, or `struct { sofab_object_descr_id_t which; union { D; A align; } u; }`
606 * for a leaf one, with @c offsetof(image, u) @c == @c offsetof(obj, u). @c NULL
607 * means tag 0 with an all-zero `default_id` option — exactly what an image
608 * carrying nothing but zeros would say, at no @c .rodata cost.
609 *
610 * Why one image is enough (MESSAGE_SPEC §4.2 / §6): a union option of type
611 * string, blob or array may not declare a non-empty default. Init seeds only
612 * `default_id`; the ≠-default test only ever compares a held `default_id`
613 * option (any other held option is forced); a decode that switches to a
614 * sequence option re-initialises it through its own descriptor, and a leaf
615 * option is overwritten whole by its payload. No other option's default is ever
616 * needed, so the options' overlay costs nothing in correctness.
617 *
618 * @warning A @c BLOB option @b must carry its length
619 * (@ref SOFAB_OBJECT_FIELD_BLOB_SIZED), never the capacity-only form
620 * @ref SOFAB_OBJECT_FIELDTYPE_BLOB "SOFAB_OBJECT_FIELD(…, BLOB)". A
621 * capacity-only blob has nowhere to record the length it received, so its value
622 * is always its full capacity: a peer sending fewer bytes than that (a different
623 * schema revision, or a hostile one) leaves the rest at whatever the object held,
624 * which in a union is the previously held option's payload — and encode would put
625 * it back on the wire. The sized form has no such gap, because the length says
626 * what the value is. @ref sofab_object_init asserts it.
627 *
628 * @warning Selecting an option by hand is the caller writing the tag @b and the
629 * value — @b always both, for every option kind. The tag alone is never enough,
630 * not even for an option "at its default": the options overlay each other and
631 * @ref sofab_object_init seeds only the one held, so the storage of any other
632 * option is indeterminate until the caller writes it. Encoding a string option
633 * whose buffer was never written reads past it (the encoder measures a string
634 * with @c strlen). For a sequence option at its default, "the value" is
635 * @ref sofab_object_init on that option's own descriptor.
636 *
637 * A union frame is an ordinary sequence, so this descriptor needs sequence
638 * support and carries no switch of its own: under
639 * @c SOFAB_DISABLE_SEQUENCE_SUPPORT the macro fails to compile rather than
640 * degrade to a plain struct, which no union survives (every overlaid option
641 * would be initialised, compared and encoded).
642 *
643 * @param field_list Array of @ref sofab_object_descr_field_t (one per option).
644 * @param field_count Number of options.
645 * @param nested_list Array of pointers to nested @ref sofab_object_descr_t (may be NULL).
646 * @param nested_count Number of entries in @p nested_list.
647 * @param default_image Pointer to the prefix default image, or @c NULL (tag 0,
648 * `default_id` option all-zero or a sequence).
649 * @param obj The union object type.
650 * @param tfield The tag member, a @c sofab_object_descr_id_t declared
651 * @b first in @p obj (at offset 0).
652 */
653/*! @cond INTERNAL */
654/* A union is a sequence on the wire, so the walk that reads it needs sequence
655 * support and has no switch of its own: where sequences are compiled out a union
656 * descriptor cannot be walked at all, and building one is a mistake the macro
657 * reports here instead of silently degrading to a plain struct -- which no union
658 * survives, since every overlaid option would be initialised, compared and
659 * encoded. */
660#if defined(SOFAB_DISABLE_SEQUENCE_SUPPORT)
661# define _SOFAB_OBJECT_UNION_BIT (0u * sizeof(char[ \
662 SOFAB_OBJECT_DESCR_UNION_needs_sequence_support_but_SOFAB_DISABLE_SEQUENCE_SUPPORT_is_defined \
663 ? 1 : -1]))
664#else
665# define _SOFAB_OBJECT_UNION_BIT SOFAB_OBJECT_UNION
666#endif
667/*! @endcond */
668#define SOFAB_OBJECT_DESCR_UNION(field_list, field_count, nested_list, nested_count, default_image, obj, tfield) \
669 { (field_list), (nested_list), (const void *)(default_image), \
670 SOFAB_OBJECT_DESCR_COUNT_WORD(field_count, 0u), (nested_count), \
671 (uint8_t)(_SOFAB_OBJECT_UNION_BIT \
672 | 0u * sizeof(struct { int tag_member_must_have_the_width_of_sofab_object_descr_id_t \
673 : sizeof(((obj *)0)->tfield) == sizeof(sofab_object_descr_id_t) ? 1 : -1; }) \
674 | SOFAB_OBJECT_ASSERT_LEN_FIRST(obj, tfield)) }
675
676/* types **********************************************************************/
677/*!
678 * @brief Description of a single field within a SofaBuffer object.
679 */
680typedef struct
681{
682 const sofab_object_descr_id_t id; /*!< Field ID (width per SOFAB_OBJECT_DESCR_PROFILE) */
683 const sofab_object_descr_offset_t offset; /*!< Offset within the object structure (width per profile) */
684 const sofab_object_descr_size_t size; /*!< Size of the field in bytes (width per profile) */
685 const uint8_t nested_idx; /*!< SEQUENCE: index into the nested object descriptor list. BLOB/ARRAY: byte width of the companion length member (0 = not sized), see @ref SOFAB_OBJECT_FIELD_BLOB_SIZED / @ref SOFAB_OBJECT_FIELD_ARRAY_SIZED */
686 const uint8_t type : 4; /*!< Field type (4bit for types: 0x0..0xC) */
687 const uint8_t element_size : 4; /*!< Size of individual elements for arrays (4bit for type length: 1..8)*/
689
690/*!
691 * @brief Description of a SofaBuffer object structure.
692 */
693typedef struct sofab_object_descr
694{
695 const sofab_object_descr_field_t *const field_list; /*!< Pointer to list of field descriptors */
696 const struct sofab_object_descr *const *nested_list; /*!< Pointer to list of nested object descriptors */
697 const void *const default_values; /*!< Pointer to default values for fields (optional, may be NULL) */
698 /*!
699 * Field count and, for an object that is not a wrapper-array holder, the
700 * byte extent of @ref default_values -- two things in one 16-bit slot,
701 * which is what keeps this descriptor 16 bytes wide on a 32-bit target.
702 *
703 * - **Holder** (@ref SOFAB_OBJECT_SEQ_HOLDER): all 16 bits are the element
704 * slot count, so a wrapper array keeps its full capacity (a
705 * 300-element `array<string>` is 300 slots). A holder never carries a
706 * default image, so there is no extent to store for it.
707 * - **Object** (message, struct, union): the low 8 bits are the field count
708 * (@ref SOFAB_OBJECT_DESCR_FIELD_COUNT_MAX) and the high 8 bits are how
709 * many bytes of the object @ref default_values describes, counted from
710 * offset 0 (@ref SOFAB_OBJECT_DESCR_EXTENT_MAX, 0 = the whole object).
711 * Both ceilings are asserted by the descriptor macros, as is a holder's
712 * slot count (@ref SOFAB_OBJECT_DESCR_SLOT_COUNT_MAX).
713 *
714 * Read it through @c _field_count() and @c _default_extent(), never
715 * directly.
716 */
717 const uint16_t count_extent;
718 const uint8_t nested_count; /*!< Number of nested objects */
719 const uint8_t fixed_seq; /*!< Wrapper-array holder marker: bit 0 (@ref SOFAB_OBJECT_SEQ_HOLDER) flags the holder — reject an unmatched (over-index) element id instead of skipping it, unless §7.3 already skips it on a contradicting wire type/subtype; the bits above @ref SOFAB_OBJECT_SEQ_LEN_SHIFT carry the byte width of the companion element-count member (0 = un-sized, see @ref SOFAB_OBJECT_DESCR_SEQ_SIZED) */
721
722/*!
723 * @brief Decoder state for a SofaBuffer object.
724 */
725typedef struct
726{
727 const sofab_object_descr_t *info; /*!< Pointer to object descriptor */
728 uint8_t *dst; /*!< Destination buffer for decoded data */
729 sofab_istream_decoder_t decoder; /*!< Decoder state */
730 uint8_t depth; /*!< Decoder depth */
732
733/* prototypes *****************************************************************/
734
735/*!
736 * @brief Initializes an object structure with default values.
737 *
738 * This function populates the provided object structure with default values
739 * as specified in the object descriptor. If no default values are provided,
740 * the object is zero-initialized.
741 *
742 * @param info Pointer to the object descriptor.
743 * @param obj Pointer to the object structure to initialize.
744 * @return SOFAB_RET_OK on success, or an error code on failure.
745 */
747 const sofab_object_descr_t *info,
748 void *obj);
749
750/*!
751 * @brief Encodes an object with the given descriptor into the output stream.
752 *
753 * The output stream context must be initialized prior to calling this function.
754 * Fields equal to their default (the matching member of the descriptor's
755 * default-values object, or zero when none is set) are skipped. Nested objects
756 * are written as sequences.
757 *
758 * The skip rule covers a nested-object **field** too (MESSAGE_SPEC §2): one whose
759 * every child equals its default is omitted entirely rather than framed as an
760 * empty sequence, so an all-default object encodes to zero bytes. The comparison
761 * is per child field, recursively, against the nested descriptor's declared
762 * defaults — never a raw byte image — so struct padding cannot influence it and a
763 * non-zero nested default is handled like any other. Absence reconstructs exactly
764 * that default (@ref sofab_object_init), so the omission is value-preserving.
765 *
766 * Inside a **wrapper-array holder** (@ref SOFAB_OBJECT_DESCR_SEQ) the fields are
767 * the array's element slots and the rule becomes **positional** (MESSAGE_SPEC
768 * §2/§5.1): a wrapper carries no length, so the decoded length is *highest present
769 * id + 1* and nothing that carries it may be elided.
770 * - An element at an **interior** index equal to its default is **omitted**,
771 * leaving an id gap — a leaf is not written and a sequence-form element is not
772 * framed either. Both kinds obey one rule; there is no trailing-run elision and
773 * no fill-to-N.
774 * - The element at the **last** index is **always written**: a leaf as its
775 * (default) value, a sequence element as an empty frame.
776 *
777 * **What "last index" means here.** It is the array's `length - 1`, and where the
778 * length comes from is the holder descriptor's business:
779 * - @ref SOFAB_OBJECT_DESCR_SEQ_SIZED carries an element-count member, so the
780 * length is that member (clamped to the capacity). Slots at or past it are not
781 * walked at all; the slot at `length - 1` is the one that is always written; a
782 * length of 0 writes nothing, and the field-level ≠-default test above omits the
783 * whole wrapper (the canonical encoding of the empty array, §2). All of
784 * @c 0 … @c N are expressible, as on every other target.
785 * - a plain @ref SOFAB_OBJECT_DESCR_SEQ has no length member, so the value it
786 * holds occupies every slot and the length is @c field_count: the last index is
787 * @c field_count @c - @c 1 unconditionally. An all-default holder is the one
788 * exception — indistinguishable from the empty array there — and is omitted
789 * whole by the same field-level test. Lengths @c 1 … @c N-1 are not
790 * representable in that form.
791 *
792 * A compact scalar array encodes **every** element it holds (§3): its element
793 * count is the length member of @ref SOFAB_OBJECT_FIELD_ARRAY_SIZED, or the full
794 * capacity for a plain @ref SOFAB_OBJECT_FIELD_ARRAY. A trailing element equal to
795 * the element default is *not* dropped — `[1,2,3,0,0]` and `[1,2,3]` are different
796 * values.
797 *
798 * This descriptor-driven encoder uses none of the output stream's hold-back
799 * framing (@c sofab_ostream_write_sequence_begin_lazy): it tests a field against
800 * its default *before* opening anything, so it is canonical at every nesting
801 * depth and no @c SOFAB_LAZY_SEQ_DEPTH window applies to it.
802 *
803 * @param ctx Pointer to the output stream context.
804 * @param info Pointer to the object descriptor.
805 * @param src Pointer to the source object to serialize.
806 *
807 * @return SOFAB_RET_OK on success, otherwise an sofab_ret_t error code
808 * (e.g. SOFAB_RET_E_ARGUMENT for an unsupported descriptor field type,
809 * or a write error propagated from the output stream).
810 */
812 sofab_ostream_t *ctx,
813 const sofab_object_descr_t *info,
814 const void *src);
815
816 /*!
817 * @brief Field callback invoked during object decoding.
818 *
819 * Use this function as the field callback when initializing an input stream
820 * for decoding objects with @ref sofab_istream_init. The @p usrptr must point
821 * to a @ref sofab_object_decoder_t whose @c info and @c dst describe the target
822 * object; for nested objects the decoder must be the first element of an array
823 * with one slot per supported nesting level (see @c depth). The callback binds
824 * the appropriate read for each known field ID. An unknown field ID is ignored
825 * (skipped) for a normal message descriptor, but for a fixed-capacity
826 * sequence-holder descriptor (see @ref SOFAB_OBJECT_DESCR_SEQ) it is an
827 * over-index element and rejects the message via @ref sofab_istream_invalidate —
828 * unless its header contradicts the declared element type, in which case §7.3
829 * skips it first and no element, and therefore no element index, ever exists.
830 *
831 * Inside a **sized** holder (@ref SOFAB_OBJECT_DESCR_SEQ_SIZED) a matched element
832 * id also raises the companion element-count member to @c id @c + @c 1, so that
833 * when the wrapper closes it holds *highest present id + 1* — the array's length
834 * per MESSAGE_SPEC §5.1, stored back exactly as a sized blob stores its received
835 * byte length. The §7.4 reset below re-zeroes the member whenever the wrapper
836 * re-opens, so a replaced array reports its own length and not the previous one's.
837 *
838 * **Only a bound element counts.** An element whose header wire type contradicts
839 * the declared one is skipped "exactly as a field with an unknown id is skipped"
840 * (§7.3) — and an unknown id leaves nothing behind, so it does **not** occupy its
841 * id and does **not** count toward the length: the slot is reconstructed from the
842 * element default and the array is byte-for-byte what it would have been had the
843 * element never arrived. The ids §5.1 counts are the ones consumed as elements,
844 * not the ones that merely appeared on the wire. A well-typed but **empty**
845 * element — an empty frame, an empty string — is bound and does count; that is a
846 * present element and not the same thing at all.
847 *
848 * @warning **Initialize @c dst with @ref sofab_object_init before every message.**
849 * Decoding writes only the fields the wire actually carries; a field the sender
850 * omitted because it equals its default is not written, so whatever the
851 * destination already held stays. Re-using one object for a second message
852 * therefore keeps stale values unless it is re-initialized first. This applies to
853 * a @b sequence field as much as to a leaf one: MESSAGE_SPEC §2 omits an
854 * all-default sequence field outright, and the §7.4 wrapper-replace reset inside
855 * this callback only runs when a wrapper actually opens on the wire.
856 *
857 * @param ctx Pointer to the active input stream context.
858 * @param id Field ID reported by the decoder.
859 * @param size Size of the field value in bytes (unused; kept for the callback signature).
860 * @param count Number of array elements (unused; kept for the callback signature).
861 * @param usrptr Pointer to the @ref sofab_object_decoder_t driving this object.
862 */
864 sofab_istream_t *ctx,
865 sofab_id_t id,
866 size_t size,
867 size_t count,
868 void *usrptr);
869
870#ifdef __cplusplus
871}
872#endif
873
874/** @} */ // end of defgroup
875
876#endif /* SOFAB_OBJECT_H */
uint16_t sofab_object_descr_offset_t
Definition sofab.h:462
sofab_ret_t sofab_object_init(const sofab_object_descr_t *info, void *obj)
Initializes an object structure with default values.
uint16_t sofab_object_descr_size_t
Definition sofab.h:463
uint32_t sofab_id_t
Field identifier type.
Definition sofab.h:106
uint16_t sofab_object_descr_id_t
Definition sofab.h:461
struct sofab_object_descr sofab_object_descr_t
Description of a SofaBuffer object structure.
sofab_ret_t
Return status codes.
Definition sofab.h:39
sofab_ret_t sofab_object_encode(sofab_ostream_t *ctx, const sofab_object_descr_t *info, const void *src)
Encodes an object with the given descriptor into the output stream.
void sofab_object_field_cb(sofab_istream_t *ctx, sofab_id_t id, size_t size, size_t count, void *usrptr)
Field callback invoked during object decoding.
SofaBuffers C - Input stream decoder for Sofab messages.
SofaBuffers C - Output stream encoder for Sofab messages.
Decoder context used for Sofab sequences.
Definition istream.h:123
Internal state of the Sofab input stream.
Definition istream.h:135
Decoder state for a SofaBuffer object.
Definition object.h:726
sofab_istream_decoder_t decoder
Definition object.h:729
const sofab_object_descr_t * info
Definition object.h:727
Description of a single field within a SofaBuffer object.
Definition object.h:681
const uint8_t element_size
Definition object.h:687
const uint8_t nested_idx
Definition object.h:685
const sofab_object_descr_id_t id
Definition object.h:682
const sofab_object_descr_offset_t offset
Definition object.h:683
const sofab_object_descr_size_t size
Definition object.h:684
Description of a SofaBuffer object structure.
Definition object.h:694
const uint8_t nested_count
Definition object.h:718
const uint8_t fixed_seq
Definition object.h:719
const struct sofab_object_descr *const * nested_list
Definition object.h:696
const void *const default_values
Definition object.h:697
const uint16_t count_extent
Definition object.h:717
const sofab_object_descr_field_t *const field_list
Definition object.h:695