Reduct  v4.1.3-1-gd06c383
A functional and immutable language.
Loading...
Searching...
No Matches
schema.h
Go to the documentation of this file.
1#ifndef REDUCT_SCHEMA_H
2#define REDUCT_SCHEMA_H 1
3
4#include <reduct/defs.h>
5#include <reduct/sync.h>
6
7#include <stdbool.h>
8
9struct reduct;
10
11/**
12 * @file schema.h
13 * @brief Schema transformation.
14 * @defgroup schema Schema
15 *
16 * Schemas provide a way to validate the structure of Reduct association lists and transform them
17 * into native C structures.
18 *
19 * ## Applying a Schema
20 *
21 * The `reduct_schema_apply()` function expects an association list where each key corresponds to a field in the schema
22 * and the value is the data for that field.
23 *
24 * Arrays are an exception to the above and can be specified in two ways:
25 * - `("my-key" ("first" "second" "third"))`
26 * - `("my-key" "first" "second" "third")`
27 *
28 * @{
29 */
30
31/**
32 * @brief Schema type flags.
33 * @enum reduct_schema_type_t
34 */
35typedef enum reduct_schema_type
36{
37 REDUCT_SCHEMA_TYPE_UINT, ///< Unsigned integer.
38 REDUCT_SCHEMA_TYPE_INT, ///< Signed integer.
39 REDUCT_SCHEMA_TYPE_FLOAT, ///< Float or double.
41 REDUCT_SCHEMA_TYPE_STRING, ///< An array of characters.
42 REDUCT_SCHEMA_TYPE_HANDLE, ///< A `reduct_handle_t`.
43 REDUCT_SCHEMA_TYPE_ARRAY, ///< A fixed-size array of primitives.
44 REDUCT_SCHEMA_TYPE_ATOM, ///< A pointer to a `reduct_atom_t`.
45 REDUCT_SCHEMA_TYPE_LIST, ///< A pointer to a `reduct_list_t`.
47
48/**
49 * @brief Schema field structure.
50 * @struct reduct_schema_t
51 */
52typedef struct reduct_schema
53{
54 const char* key;
55 size_t offset;
56 size_t size;
61
62/**
63 * @brief Internal schema structure.
64 * @struct reduct_schema_internal_t
65 */
66typedef struct reduct_schema_internal
67{
68 size_t count;
71
72typedef uint32_t reduct_schema_id_t; ///< Schema ID type.
73
74typedef uint32_t reduct_schema_index_t; ///< Schema index type.
75
76#define REDUCT_SCHEMA_INDEX_NONE ((reduct_schema_index_t) - 1) ///< Invalid schema index.
77
78/**
79 * @brief Global schema-related state structure.
80 * @struct reduct_schema_global_t
81 */
82typedef struct
83{
84 struct reduct_schema_internal** schemas;
85 size_t count;
86 size_t capacity;
89
90/**
91 * @brief Initialize a global schema state.
92 *
93 * @param global Pointer to the global schema state to initialize.
94 */
96
97/**
98 * @brief Deinitialize a global schema state.
99 *
100 * @param global Pointer to the global schema state to deinitialize.
101 */
103
104/**
105 * @brief Create a new schema from an array of fields.
106 *
107 * @param reduct Pointer to the Reduct structure.
108 * @param count Number of fields.
109 * @param fields Array of field definitions.
110 * @return The ID of the newly created schema.
111 */
112REDUCT_API reduct_schema_id_t reduct_schema_new(struct reduct* reduct, size_t count, const reduct_schema_t* fields);
113
114/**
115 * @brief Apply a schema to an association list and populate a C structure.
116 *
117 * Any fields not explicitly set by the given list are guaranteed to be left untouched.
118 *
119 * @param reduct Pointer to the Reduct structure.
120 * @param id The ID of the schema to apply.
121 * @param listH The handle to the association list.
122 * @param out Pointer to the destination C structure.
123 */
124REDUCT_API void reduct_schema_apply(struct reduct* reduct, reduct_schema_id_t id, reduct_handle_t listH, void* out);
125
126/**
127 * @brief Get the number of fields in a schema.
128 *
129 * @param reduct Pointer to the Reduct structure.
130 * @param id The schema ID.
131 * @return The number of fields, or 0 if the ID is invalid.
132 */
133REDUCT_API size_t reduct_schema_get_count(struct reduct* reduct, reduct_schema_id_t id);
134
135/**
136 * @brief Transform a C structure into an association list using a schema.
137 *
138 * @param reduct Pointer to the Reduct structure.
139 * @param id The ID of the schema to use.
140 * @param in Pointer to the source C structure.
141 * @return A handle to the newly created association list.
142 */
143REDUCT_API reduct_handle_t reduct_schema_serialize(struct reduct* reduct, reduct_schema_id_t id, const void* in);
144
145/**
146 * @brief Helper macro to define a schema field.
147 *
148 * @param _key The key string in the association list.
149 * @param _struct The C structure type.
150 * @param _member The member name in the C structure.
151 * @param _type The `reduct_schema_type_t` of the field, only the suffix is required, `REDUCT_SCHEMA_TYPE_` is added
152 * automatically.
153 */
154#define REDUCT_SCHEMA_FIELD(_key, _struct, _member, _type) \
155 (reduct_schema_t){(_key), offsetof(_struct, _member), sizeof(((_struct*)0)->_member), REDUCT_SCHEMA_TYPE_##_type, \
156 0, 0}
157
158/**
159 * @brief Helper macro to define an array schema field.
160 *
161 * @param _key The key string.
162 * @param _struct The C structure type.
163 * @param _member The array member name.
164 * @param _type The `reduct_schema_type_t` of the field, only the suffix is required, `REDUCT_SCHEMA_TYPE_` is added
165 * automatically.
166 */
167#define REDUCT_SCHEMA_FIELD_ARRAY(_key, _struct, _member, _subtype) \
168 (reduct_schema_t) \
169 { \
170 (_key), offsetof(_struct, _member), sizeof(((_struct*)0)->_member), REDUCT_SCHEMA_TYPE_ARRAY, \
171 REDUCT_SCHEMA_TYPE_##_subtype, sizeof(((_struct*)0)->_member[0]) \
172 }
173
174/** @} */
175
176#endif
#define REDUCT_API
Definition defs.h:24
REDUCT_API reduct_schema_id_t reduct_schema_new(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_global_deinit(reduct_schema_global_t *global)
Deinitialize a global schema state.
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 void reduct_schema_global_init(reduct_schema_global_t *global)
Initialize a global schema state.
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.
uint32_t reduct_schema_index_t
Schema index type.
Definition schema.h:74
uint32_t reduct_schema_id_t
Schema ID type.
Definition schema.h:72
reduct_schema_type_t
Schema type flags.
Definition schema.h:36
@ REDUCT_SCHEMA_TYPE_HANDLE
A reduct_handle_t.
Definition schema.h:42
@ REDUCT_SCHEMA_TYPE_INT
Signed integer.
Definition schema.h:38
@ REDUCT_SCHEMA_TYPE_LIST
A pointer to a reduct_list_t.
Definition schema.h:45
@ REDUCT_SCHEMA_TYPE_BOOL
A bool.
Definition schema.h:40
@ REDUCT_SCHEMA_TYPE_ARRAY
A fixed-size array of primitives.
Definition schema.h:43
@ REDUCT_SCHEMA_TYPE_FLOAT
Float or double.
Definition schema.h:39
@ REDUCT_SCHEMA_TYPE_ATOM
A pointer to a reduct_atom_t.
Definition schema.h:44
@ REDUCT_SCHEMA_TYPE_STRING
An array of characters.
Definition schema.h:41
@ REDUCT_SCHEMA_TYPE_UINT
Unsigned integer.
Definition schema.h:37
Handle type.
Definition defs.h:121
Read-Write Mutex structure.
Definition sync.h:23
Global schema-related state structure.
Definition schema.h:83
reduct_rwmutex_t mutex
Definition schema.h:87
struct reduct_schema_internal ** schemas
Definition schema.h:84
Internal schema structure.
Definition schema.h:67
Schema field structure.
Definition schema.h:53
reduct_schema_type_t type
Definition schema.h:57
size_t offset
Definition schema.h:55
size_t size
Definition schema.h:56
reduct_schema_type_t subtype
Definition schema.h:58
const char * key
Definition schema.h:54
size_t elementSize
Definition schema.h:59
Syncronization primitives.