Reduct  v4.0.5-1-g4851deb
A functional and immutable language.
Loading...
Searching...
No Matches
Schema

Detailed Description

Schemas provide a way to validate the structure of Reduct association lists and transform them into native C structures.

Data Structures

struct  reduct_schema_t
 Schema field structure. More...
 
struct  reduct_schema_internal_t
 Internal schema structure. More...
 
struct  reduct_schema_global_t
 Global schema-related state structure. More...
 

Macros

#define REDUCT_SCHEMA_INDEX_NONE   ((reduct_schema_index_t) - 1)
 Invalid schema index.
 
#define REDUCT_SCHEMA_FIELD(_key, _struct, _member, _type)
 Helper macro to define a schema field.
 
#define REDUCT_SCHEMA_FIELD_ARRAY(_key, _struct, _member, _subtype)
 Helper macro to define an array schema field.
 

Typedefs

typedef uint32_t reduct_schema_id_t
 Schema ID type.
 
typedef uint32_t reduct_schema_index_t
 Schema index type.
 

Enumerations

enum  reduct_schema_type_t {
  REDUCT_SCHEMA_TYPE_UINT , REDUCT_SCHEMA_TYPE_INT , REDUCT_SCHEMA_TYPE_FLOAT , REDUCT_SCHEMA_TYPE_BOOL ,
  REDUCT_SCHEMA_TYPE_STRING , REDUCT_SCHEMA_TYPE_HANDLE , REDUCT_SCHEMA_TYPE_ARRAY
}
 Schema type flags. More...
 

Functions

REDUCT_API void reduct_schema_global_init (reduct_schema_global_t *global)
 Initialize a global schema state.
 
REDUCT_API void reduct_schema_global_deinit (reduct_schema_global_t *global)
 Deinitialize a global schema state.
 
REDUCT_API reduct_schema_id_t reduct_schema_new (struct reduct *reduct, size_t count,...)
 Create a new schema.
 
REDUCT_API reduct_schema_id_t reduct_schema_new_fields (struct reduct *reduct, size_t count, const reduct_schema_t *fields)
 Create a new schema from an array of fields.
 
REDUCT_API void reduct_schema_apply (struct reduct *reduct, reduct_schema_id_t id, reduct_handle_t listH, void *out)
 Apply a schema to an association list and populate a C structure.
 
REDUCT_API size_t reduct_schema_get_count (struct reduct *reduct, reduct_schema_id_t id)
 Get the number of fields in a schema.
 
REDUCT_API reduct_handle_t reduct_schema_serialize (struct reduct *reduct, reduct_schema_id_t id, const void *in)
 Transform a C structure into an association list using a schema.
 

Macro Definition Documentation

◆ REDUCT_SCHEMA_INDEX_NONE

#define REDUCT_SCHEMA_INDEX_NONE   ((reduct_schema_index_t) - 1)

Invalid schema index.

Definition at line 65 of file schema.h.

◆ REDUCT_SCHEMA_FIELD

#define REDUCT_SCHEMA_FIELD (   _key,
  _struct,
  _member,
  _type 
)
Value:
(reduct_schema_t){(_key), offsetof(_struct, _member), sizeof(((_struct*)0)->_member), REDUCT_SCHEMA_TYPE_##_type, \
0, 0}
Schema field structure.
Definition schema.h:42

Helper macro to define a schema field.

Parameters
_keyThe key string in the association list.
_structThe C structure type.
_memberThe member name in the C structure.
_typeThe reduct_schema_type_t of the field, only the suffix is required, REDUCT_SCHEMA_TYPE_ is added automatically.

Definition at line 154 of file schema.h.

◆ REDUCT_SCHEMA_FIELD_ARRAY

#define REDUCT_SCHEMA_FIELD_ARRAY (   _key,
  _struct,
  _member,
  _subtype 
)
Value:
{ \
(_key), offsetof(_struct, _member), sizeof(((_struct*)0)->_member), REDUCT_SCHEMA_TYPE_ARRAY, \
REDUCT_SCHEMA_TYPE_##_subtype, sizeof(((_struct*)0)->_member[0]) \
}
@ REDUCT_SCHEMA_TYPE_ARRAY
A fixed-size array of primitives.
Definition schema.h:34

Helper macro to define an array schema field.

Parameters
_keyThe key string.
_structThe C structure type.
_memberThe array member name.
_typeThe reduct_schema_type_t of the field, only the suffix is required, REDUCT_SCHEMA_TYPE_ is added automatically.

Definition at line 167 of file schema.h.

Typedef Documentation

◆ reduct_schema_id_t

typedef uint32_t reduct_schema_id_t

Schema ID type.

Definition at line 61 of file schema.h.

◆ reduct_schema_index_t

typedef uint32_t reduct_schema_index_t

Schema index type.

Definition at line 63 of file schema.h.

Enumeration Type Documentation

◆ reduct_schema_type_t

Schema type flags.

Enumerator
REDUCT_SCHEMA_TYPE_UINT 

Unsigned integer.

REDUCT_SCHEMA_TYPE_INT 

Signed integer.

REDUCT_SCHEMA_TYPE_FLOAT 

Float or double.

REDUCT_SCHEMA_TYPE_BOOL 

A bool.

REDUCT_SCHEMA_TYPE_STRING 

An array of characters.

REDUCT_SCHEMA_TYPE_HANDLE 

A reduct_handle_t.

REDUCT_SCHEMA_TYPE_ARRAY 

A fixed-size array of primitives.

Definition at line 26 of file schema.h.

Function Documentation

◆ reduct_schema_global_init()

REDUCT_API void reduct_schema_global_init ( reduct_schema_global_t global)

Initialize a global schema state.

Parameters
globalPointer to the global schema state to initialize.

◆ reduct_schema_global_deinit()

REDUCT_API void reduct_schema_global_deinit ( reduct_schema_global_t global)

Deinitialize a global schema state.

Parameters
globalPointer to the global schema state to deinitialize.

◆ reduct_schema_new()

REDUCT_API reduct_schema_id_t reduct_schema_new ( struct reduct *  reduct,
size_t  count,
  ... 
)

Create a new schema.

Parameters
reductPointer to the Reduct structure.
countNumber of fields.
...Variadic arguments for specifying the fields.
Returns
The ID of the newly created schema.

◆ reduct_schema_new_fields()

REDUCT_API reduct_schema_id_t reduct_schema_new_fields ( struct reduct *  reduct,
size_t  count,
const reduct_schema_t fields 
)

Create a new schema from an array of fields.

Parameters
reductPointer to the Reduct structure.
countNumber of fields.
fieldsArray of field definitions.
Returns
The ID of the newly created schema.

◆ reduct_schema_apply()

REDUCT_API void reduct_schema_apply ( struct reduct *  reduct,
reduct_schema_id_t  id,
reduct_handle_t  listH,
void *  out 
)

Apply a schema to an association list and populate a C structure.

Any fields not explicitly set by the given list are guaranteed to be left untouched.

Parameters
reductPointer to the Reduct structure.
idThe ID of the schema to apply.
listHThe handle to the association list.
outPointer to the destination C structure.

◆ reduct_schema_get_count()

REDUCT_API size_t reduct_schema_get_count ( struct reduct *  reduct,
reduct_schema_id_t  id 
)

Get the number of fields in a schema.

Parameters
reductPointer to the Reduct structure.
idThe schema ID.
Returns
The number of fields, or 0 if the ID is invalid.

◆ reduct_schema_serialize()

REDUCT_API reduct_handle_t reduct_schema_serialize ( struct reduct *  reduct,
reduct_schema_id_t  id,
const void *  in 
)

Transform a C structure into an association list using a schema.

Parameters
reductPointer to the Reduct structure.
idThe ID of the schema to use.
inPointer to the source C structure.
Returns
A handle to the newly created association list.