13#ifndef CSILK_REFLECT_H
14#define CSILK_REFLECT_H
195#ifndef CSILK_USER_TYPE_MAP
196#define CSILK_USER_TYPE_MAP
208#define csilk_type_name(x) \
211 signed char: "int8", \
212 unsigned char: "uint8", \
214 unsigned short: "uint16", \
216 unsigned int: "uint32", \
218 unsigned long: "uint64", \
219 long long: "int64", \
220 unsigned long long: "uint64", \
224 const char*: "string" CSILK_USER_TYPE_MAP, \
235#define csilk_marshal(ptr) csilk_json_marshal(csilk_type_name(*(ptr)), ptr)
247#define csilk_unmarshal(json, ptr) csilk_json_unmarshal(csilk_type_name(*(ptr)), json, ptr)
267#define CSILK_META_EXPAND(struct_type, field, type_enum, size, arr_len, is_ptr, nested_name) \
268 {#field, type_enum, offsetof(struct_type, field), size, arr_len, is_ptr, nested_name},
299#define CSILK_REGISTER_REFLECT(struct_type, map_macro) \
300 static csilk_field_desc_t struct_type##_meta[] = { \
301 map_macro(CSILK_META_EXPAND){NULL, 0, 0, 0, 0, false, NULL}}; \
302 static void __attribute__((constructor)) auto_reg_##struct_type(void) \
304 size_t count = (sizeof(struct_type##_meta) / sizeof(csilk_field_desc_t)) - 1; \
305 csilk_reflect_register(#struct_type, struct_type##_meta, count); \
void csilk_reflect_init(void)
Initialise the reflection subsystem.
Definition reflect.c:63
csilk_field_type_t type
Definition reflect.h:68
bool is_pointer
Definition reflect.h:75
const char * json_key
Definition reflect.h:66
int csilk_json_unmarshal(const char *type_name, const char *json_str, void *ptr)
Deserialise a JSON string into a reflected struct.
Definition reflect.c:610
size_t count
Definition reflect.h:92
const csilk_field_desc_t * fields
Definition reflect.h:91
csilk_field_type_t
Supported C data types for struct field reflection.
Definition reflect.h:28
@ CSILK_TYPE_STRUCT
Definition reflect.h:44
@ CSILK_TYPE_INT64
Definition reflect.h:36
@ CSILK_TYPE_INT16
Definition reflect.h:31
@ CSILK_TYPE_INT8
Definition reflect.h:29
@ CSILK_TYPE_UINT8
Definition reflect.h:30
@ CSILK_TYPE_UINT32
Definition reflect.h:35
@ CSILK_TYPE_UINT16
Definition reflect.h:32
@ CSILK_TYPE_INT32
Definition reflect.h:34
@ CSILK_TYPE_FLOAT
Definition reflect.h:39
@ CSILK_TYPE_STRING
Definition reflect.h:42
@ CSILK_TYPE_BOOL
Definition reflect.h:41
@ CSILK_TYPE_UINT64
Definition reflect.h:37
@ CSILK_TYPE_DOUBLE
Definition reflect.h:40
size_t size
Definition reflect.h:71
size_t offset
Definition reflect.h:69
void csilk_reflect_foreach(csilk_reflect_foreach_cb cb, void *user_data)
Iterate over all registered reflection types.
Definition reflect.c:158
void csilk_reflect_register(const char *name, const csilk_field_desc_t *fields, size_t count)
Manually register a struct type for reflection.
Definition reflect.c:108
const char * name
Definition reflect.h:90
const char * nested_type_name
Definition reflect.h:77
const csilk_reflect_entry_t * csilk_reflect_find(const char *name)
Look up a registered type by name.
Definition reflect.c:129
char * csilk_json_marshal(const char *type_name, const void *ptr)
Serialise a reflected struct to a JSON string.
Definition reflect.c:567
size_t array_length
Definition reflect.h:73
void(* csilk_reflect_foreach_cb)(const char *name, const csilk_reflect_entry_t *entry, void *user_data)
Callback type for csilk_reflect_foreach.
Definition reflect.h:135
Descriptor for a single field in a reflected struct.
Definition reflect.h:65
Registration entry for a reflected type.
Definition reflect.h:89