diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-11-29 18:09:12 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-11-29 18:09:12 -0500 |
| commit | f66f8ad7119207b99f00ea2ea389550ee65db5b3 (patch) | |
| tree | 5b5a7c887b311e3de2f2cb293b1228598c5b9eb1 /stdlib/tables.c | |
| parent | 4b5e4cd1f21582f5e5fa682ab4e4bff252963468 (diff) | |
Add serialization and deserialization
Diffstat (limited to 'stdlib/tables.c')
| -rw-r--r-- | stdlib/tables.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/stdlib/tables.c b/stdlib/tables.c index 97fee8af..449f37ca 100644 --- a/stdlib/tables.c +++ b/stdlib/tables.c @@ -647,4 +647,41 @@ PUREFUNC public bool Table$is_none(const void *obj, const TypeInfo_t*) return ((Table_t*)obj)->entries.length < 0; } +public void Table$serialize(const void *obj, FILE *out, Table_t *pointers, const TypeInfo_t *type) +{ + Table_t *t = (Table_t*)obj; + int64_t len = t->entries.length; + Int64$serialize(&len, out, pointers, &Int64$info); + + size_t offset = value_offset(type); + for (int64_t i = 0; i < len; i++) { + _serialize(t->entries.data + i*t->entries.stride, out, pointers, type->TableInfo.key); + _serialize(t->entries.data + i*t->entries.stride + offset, out, pointers, type->TableInfo.value); + } + + Optional$serialize(&t->fallback, out, pointers, Optional$info(sizeof(void*), __alignof__(void*), Pointer$info("&", type))); +} + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wstack-protector" +public void Table$deserialize(FILE *in, void *outval, Array_t *pointers, const TypeInfo_t *type) +{ + int64_t len; + Int64$deserialize(in, &len, pointers, &Int$info); + + Table_t t = {}; + for (int64_t i = 0; i < len; i++) { + char key[type->TableInfo.key->size]; + _deserialize(in, key, pointers, type->TableInfo.key); + char value[type->TableInfo.value->size]; + _deserialize(in, value, pointers, type->TableInfo.value); + Table$set(&t, key, value, type); + } + + Optional$deserialize(in, &t.fallback, pointers, Optional$info(sizeof(void*), __alignof__(void*), Pointer$info("&", type))); + + *(Table_t*)outval = t; +} +#pragma GCC diagnostic pop + // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1 |
