-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathc_test_objects.c
More file actions
104 lines (87 loc) · 2.85 KB
/
c_test_objects.c
File metadata and controls
104 lines (87 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "c_test_objects.h"
int create_foo(flatcc_builder_t* B, uint64_t id, char* text) {
int rc;
if ((rc = flatcc_builder_init(B))) return rc;
if ((rc = Foo_start_as_root(B))) return rc;
if ((rc = Foo_id_add(B, id))) return rc;
rc = Foo_text_create_str(B, text);
if (rc) return rc;
flatbuffers_buffer_ref_t root = Foo_end_as_root(B);
return 0;
}
Foo_table_t get_foo(OBX_cursor* cursor, uint64_t id) {
const void* data;
size_t size;
int rc = obx_cursor_get(cursor, id, &data, &size);
if (rc == 404) return NULL; // No special treatment at the moment if not found
if (rc) return NULL;
assert(data);
assert(size);
Foo_table_t table = Foo_as_root(data);
assert(table);
return table;
}
int put_foo(OBX_cursor* cursor, uint64_t* idInOut, char* text) {
flatcc_builder_t builder;
uint64_t id = *idInOut;
bool isNew = id == 0;
id = obx_cursor_id_for_put(cursor, id);
if (!id) { return -1; }
int rc = create_foo(&builder, id, text);
if (rc) goto err;
size_t size;
void* buffer = flatcc_builder_get_direct_buffer(&builder, &size);
if (!buffer) goto err;
rc = (isNew ? obx_cursor_put_new : obx_cursor_put)(cursor, id, buffer, size);
if (rc) goto err;
flatcc_builder_clear(&builder);
*idInOut = id;
return 0;
err:
flatcc_builder_clear(&builder);
if (rc == 0) return -1;
else return rc;
}
int create_bar(flatcc_builder_t* B, uint64_t id, char* text, uint64_t fooId) {
int rc;
if ((rc = flatcc_builder_init(B))) return rc;
if ((rc = Bar_start_as_root(B))) return rc;
if ((rc = Bar_id_add(B, id))) return rc;
if ((rc = Bar_fooId_add(B, fooId))) return rc;
if ((rc = Bar_text_create_str(B, text))) return rc;
flatbuffers_buffer_ref_t root = Bar_end_as_root(B);
return 0;
}
Bar_table_t get_bar(OBX_cursor* cursor, uint64_t id) {
const void* data;
size_t size;
int rc = obx_cursor_get(cursor, id, &data, &size);
if (rc == 404) return NULL; // No special treatment at the moment if not found
if (rc) return NULL;
assert(data);
assert(size);
Bar_table_t table = Bar_as_root(data);
assert(table);
return table;
}
int put_bar(OBX_cursor* cursor, uint64_t* idInOut, char* text, uint64_t fooId) {
flatcc_builder_t builder;
uint64_t id = *idInOut;
bool isNew = id == 0;
id = obx_cursor_id_for_put(cursor, id);
if (!id) return -1;
int rc = create_bar(&builder, id, text, fooId);
if (rc) goto err;
size_t size;
void* buffer = flatcc_builder_get_direct_buffer(&builder, &size);
if (!buffer) goto err;
rc = (isNew ? obx_cursor_put_new : obx_cursor_put)(cursor, id, buffer, size);
if (rc) goto err;
flatcc_builder_clear(&builder);
*idInOut = id;
return 0;
err:
flatcc_builder_clear(&builder);
if (rc == 0) return -1;
else return rc;
}