Reduct  v4.0.5-1-g4851deb
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 * @{
20 */
21
22/**
23 * @brief Schema type flags.
24 * @enum reduct_schema_type_t
25 */
26typedef enum reduct_schema_type
27{
28 REDUCT_SCHEMA_TYPE_UINT, ///< Unsigned integer.
29 REDUCT_SCHEMA_TYPE_INT, ///< Signed integer.
30 REDUCT_SCHEMA_TYPE_FLOAT, ///< Float or double.
32 REDUCT_SCHEMA_TYPE_STRING, ///< An array of characters.
33 REDUCT_SCHEMA_TYPE_HANDLE, ///< A `reduct_handle_t`.
34 REDUCT_SCHEMA_TYPE_ARRAY, ///< A fixed-size array of primitives.
36
37/**
38 * @brief Schema field structure.
39 * @struct reduct_schema_t
40 */
41typedef struct reduct_schema
42{
43 const char* key;
44 size_t offset;
45 size_t size;
50
51/**
52 * @brief Internal schema structure.
53 * @struct reduct_schema_internal_t
54 */
55typedef struct reduct_schema_internal
56{
57 size_t count;
60
61typedef uint32_t reduct_schema_id_t; ///< Schema ID type.
62
63typedef uint32_t reduct_schema_index_t; ///< Schema index type.
64
65#define REDUCT_SCHEMA_INDEX_NONE ((reduct_schema_index_t) - 1) ///< Invalid schema index.
66
67/**
68 * @brief Global schema-related state structure.
69 * @struct reduct_schema_global_t
70 */
71typedef struct
72{
73 struct reduct_schema_internal** schemas;
74 size_t count;
75 size_t capacity;
78
79/**
80 * @brief Initialize a global schema state.
81 *
82 * @param global Pointer to the global schema state to initialize.
83 */
85
86/**
87 * @brief Deinitialize a global schema state.
88 *
89 * @param global Pointer to the global schema state to deinitialize.
90 */
92
93/**
94 * @brief Create a new schema.
95 *
96 * @param reduct Pointer to the Reduct structure.
97 * @param count Number of fields.
98 * @param ... Variadic arguments for specifying the fields.
99 * @return The ID of the newly created schema.
100 */
101REDUCT_API reduct_schema_id_t reduct_schema_new(struct reduct* reduct, size_t count, ...);
102
103/**
104 * @brief Create a new schema from an array of fields.
105 *
106 * @param reduct Pointer to the Reduct structure.
107 * @param count Number of fields.
108 * @param fields Array of field definitions.
109 * @return The ID of the newly created schema.
110 */
111REDUCT_API reduct_schema_id_t reduct_schema_new_fields(struct reduct* reduct, size_t count,
112 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,...)
Create a new schema.
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:63
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.
uint32_t reduct_schema_id_t
Schema ID type.
Definition schema.h:61
reduct_schema_type_t
Schema type flags.
Definition schema.h:27
@ REDUCT_SCHEMA_TYPE_HANDLE
A reduct_handle_t.
Definition schema.h:33
@ REDUCT_SCHEMA_TYPE_INT
Signed integer.
Definition schema.h:29
@ REDUCT_SCHEMA_TYPE_BOOL
A bool.
Definition schema.h:31
@ REDUCT_SCHEMA_TYPE_ARRAY
A fixed-size array of primitives.
Definition schema.h:34
@ REDUCT_SCHEMA_TYPE_FLOAT
Float or double.
Definition schema.h:30
@ REDUCT_SCHEMA_TYPE_STRING
An array of characters.
Definition schema.h:32
@ REDUCT_SCHEMA_TYPE_UINT
Unsigned integer.
Definition schema.h:28
Handle type.
Definition defs.h:119
Read-Write Mutex structure.
Definition sync.h:23
Global schema-related state structure.
Definition schema.h:72
reduct_rwmutex_t mutex
Definition schema.h:76
struct reduct_schema_internal ** schemas
Definition schema.h:73
Internal schema structure.
Definition schema.h:56
Schema field structure.
Definition schema.h:42
reduct_schema_type_t type
Definition schema.h:46
size_t offset
Definition schema.h:44
size_t size
Definition schema.h:45
reduct_schema_type_t subtype
Definition schema.h:47
const char * key
Definition schema.h:43
size_t elementSize
Definition schema.h:48
Syncronization primitives.