SofaBuffers C/C++ 0.12.0
API Documentation
Loading...
Searching...
No Matches
object.h File Reference

SofaBuffers C - Object encoder and decoder. More...

#include <stddef.h>
#include "sofab/istream.h"
#include "sofab/ostream.h"
Include dependency graph for object.h:

Go to the source code of this file.

Data Structures

struct  sofab_object_descr_field_t
 Description of a single field within a SofaBuffer object. More...
 
struct  sofab_object_descr
 Description of a SofaBuffer object structure. More...
 
struct  sofab_object_decoder_t
 Decoder state for a SofaBuffer object. More...
 

Macros

#define SOFAB_OBJECT_EXTERN   extern
 
#define SOFAB_OBJECT_FIELD(id, obj, field, type)
 Build a scalar field descriptor (sofab_object_descr_field_t).
 
#define SOFAB_OBJECT_FIELD_BLOB_SIZED(id, obj, dfield, lfield)
 Build a variable-length blob field descriptor (reuses the BLOB type).
 
#define SOFAB_OBJECT_ASSERT_FITS_PROFILE(obj, field, id)
 Compile-time check that a field fits the configured descriptor profile.
 
#define SOFAB_OBJECT_ASSERT_LEN_ADJACENT(obj, dfield, lfield)
 Compile-time check that lfield immediately precedes dfield.
 
#define SOFAB_OBJECT_ASSERT_LEN_FIRST(obj, lfield)
 Compile-time check that lfield is the first member of obj.
 
#define SOFAB_OBJECT_FIELD_SEQUENCE(id, obj, field, type, idx)
 Build a nested-object (sequence) field descriptor.
 
#define SOFAB_OBJECT_FIELD_ARRAY(id, obj, field, type)
 Build an array field descriptor.
 
#define SOFAB_OBJECT_FIELD_ARRAY_SIZED(id, obj, dfield, lfield, type)
 Build a length-carrying (sized) array field descriptor.
 
#define SOFAB_OBJECT_ASSERT_FIELD_COUNT(n)
 Compile-time limit on an OBJECT's field count (message, struct, union).
 
#define SOFAB_OBJECT_ASSERT_EXTENT(n)
 Compile-time limit on a PREFIX default image's byte extent.
 
#define SOFAB_OBJECT_ASSERT_SLOT_COUNT(n)
 Compile-time limit on a wrapper-array holder's element slot count.
 
#define SOFAB_OBJECT_DESCR_COUNT_WORD(field_count, extent)
 The sofab_object_descr_t::count_extent word of an object: the field count in the low byte, the default image's byte extent in the high byte.
 
#define SOFAB_OBJECT_DESCR(field_list, field_count, nested_list, nested_count)    { (field_list), (nested_list), NULL, SOFAB_OBJECT_DESCR_COUNT_WORD(field_count, 0u), (nested_count), 0 }
 Build an object descriptor (sofab_object_descr_t) without defaults.
 
#define SOFAB_OBJECT_DESCR_WITH_DEFAULTS(field_list, field_count, nested_list, nested_count, default_struct)
 Build an object descriptor with a default-values reference.
 
#define SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS(field_list, field_count, nested_list, nested_count, default_prefix)
 Build an object descriptor whose default image is a PREFIX of the object.
 
#define SOFAB_OBJECT_DESCR_SEQ(field_list, field_count, nested_list, nested_count)
 Build a fixed-capacity sequence-holder object descriptor.
 
#define SOFAB_OBJECT_DESCR_SEQ_SIZED(field_list, field_count, nested_list, nested_count, obj, lfield)
 Build a length-carrying (sized) sequence-holder object descriptor.
 
#define SOFAB_OBJECT_DESCR_UNION(field_list, field_count, nested_list, nested_count, default_image, obj, tfield)
 Build a tagged-union object descriptor.
 
Object field types

Field type tags stored in sofab_object_descr_field_t::type.

Each descriptor field carries one of these tags; it selects which encode/decode path the transcoder takes for that struct member. Pass the tag as the type argument of the SOFAB_OBJECT_FIELD family of macros.

#define SOFAB_OBJECT_FIELDTYPE_UNSIGNED   0x0
 
#define SOFAB_OBJECT_FIELDTYPE_SIGNED   0x1
 
#define SOFAB_OBJECT_FIELDTYPE_FP32   0x2
 
#define SOFAB_OBJECT_FIELDTYPE_FP64   0x3
 
#define SOFAB_OBJECT_FIELDTYPE_STRING   0x4
 
#define SOFAB_OBJECT_FIELDTYPE_BLOB   0x5
 
#define SOFAB_OBJECT_FIELDTYPE_ARRAY_UNSIGNED   0x6
 
#define SOFAB_OBJECT_FIELDTYPE_ARRAY_SIGNED   0x7
 
#define SOFAB_OBJECT_FIELDTYPE_ARRAY_FP32   0x8
 
#define SOFAB_OBJECT_FIELDTYPE_ARRAY_FP64   0x9
 
#define SOFAB_OBJECT_FIELDTYPE_ARRAY_BOOLEAN   0xA
 
#define SOFAB_OBJECT_FIELDTYPE_SEQUENCE   0xB
 
#define SOFAB_OBJECT_FIELDTYPE_BOOLEAN   0xC
 
Count-word ceilings (@ref sofab_object_descr_t::count_extent)

What the 16-bit count word can describe. Generated code can hold a schema against these at compile time the way it holds a field id against SOFAB_ID_MAX, instead of discovering the ceiling as an assertion failure inside this header – which is why they are macros and not just prose.

#define SOFAB_OBJECT_DESCR_FIELD_COUNT_MAX   255u
 Fields an object (message, struct, union) may declare.
 
#define SOFAB_OBJECT_DESCR_EXTENT_MAX   255u
 Bytes a PREFIX default image may describe (SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS).
 
#define SOFAB_OBJECT_DESCR_SLOT_COUNT_MAX   65535u
 Element slots a wrapper-array holder may declare (SOFAB_OBJECT_DESCR_SEQ).
 
#define SOFAB_OBJECT_DESCR_EXTENT_SHIFT   8
 Position of the extent inside the count word.
 
Wrapper-array holder marker (@ref sofab_object_descr_t::fixed_seq)

The fixed_seq slot packs two things: bit 0 flags the descriptor as a wrapper-array holder, and the remaining bits carry the byte width of the holder's companion element-count member (0 when it has none). It is the holder-level counterpart of the way nested_idx doubles as the "is sized" flag and the length width for SOFAB_OBJECT_FIELD_BLOB_SIZED / SOFAB_OBJECT_FIELD_ARRAY_SIZED — with the flag kept in bit 0 so that every plain "is this a holder?" test stays a truth test.

#define SOFAB_OBJECT_SEQ_HOLDER   0x01u
 
#define SOFAB_OBJECT_SEQ_LEN_SHIFT   1
 
#define SOFAB_OBJECT_UNION   0x80u
 

Typedefs

typedef struct sofab_object_descr sofab_object_descr_t
 Description of a SofaBuffer object structure.
 

Functions

sofab_ret_t sofab_object_init (const sofab_object_descr_t *info, void *obj)
 Initializes an object structure with default values.
 
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.
 

Detailed Description

SofaBuffers C - Object encoder and decoder.

This module implements a generic message encoder and decoder. Since programmatic transcoding of messages using the corelib API can generate a lot of program code under certain circumstances, this generic transcoder helps to keep the footprint small. Instead of API calls, a constant message description is used to serialize and deserialize messages. For large messages, these descriptions are smaller than the code required for API calls. Even in projects with many messages, program code can be saved by reusing the transcoder multiple times. For small embedded projects with few small messages, however, it may make sense to use the corelib without this transcoder.

Typically, this module is used from generated code that defines the message structures and their descriptors.

SPDX-License-Identifier: MIT

Definition in file object.h.