-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtasklist.obx.h
240 lines (196 loc) · 9.21 KB
/
tasklist.obx.h
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Code generated by ObjectBox; DO NOT EDIT.
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "flatcc/flatcc.h"
#include "flatcc/flatcc_builder.h"
#include "objectbox.h"
/// Internal function used in other generated functions to put (write) explicitly typed objects.
static obx_id tasklist_obx_h_put_object(OBX_box* box, void* object,
bool (*to_flatbuffer)(flatcc_builder_t*, const void*, void**, size_t*), OBXPutMode mode);
/// Internal function used in other generated functions to get (read) explicitly typed objects.
static void* tasklist_obx_h_get_object(OBX_box* box, obx_id id, void* (*from_flatbuffer)(const void*, size_t));
/// Internal function used in other generated functions to get a vTable offset for a given field.
static flatbuffers_voffset_t tasklist_obx_h_fb_field_offset(flatbuffers_voffset_t vs, const flatbuffers_voffset_t* vt, size_t field);
typedef struct Task {
obx_id id;
char* text;
uint64_t date_created;
uint64_t date_finished;
} Task;
enum Task_ {
Task_ENTITY_ID = 1,
Task_PROP_ID_id = 1,
Task_PROP_ID_text = 2,
Task_PROP_ID_date_created = 3,
Task_PROP_ID_date_finished = 4,
};
/// Write given object to the FlatBufferBuilder
static bool Task_to_flatbuffer(flatcc_builder_t* B, const Task* object, void** out_buffer, size_t* out_size);
/// Read an object from a valid FlatBuffer.
/// If the read object contains vectors or strings, those are allocated on heap and must be freed after use by calling Task_free_pointers().
/// Thus, when calling this function multiple times on the same object, ensure to call Task_free_pointers() before subsequent calls to avoid leaks.
/// @returns true if the object was deserialized successfully or false on (allocation) error in which case any memory
/// allocated by this function will also be freed before returning, allowing you to retry.
static bool Task_from_flatbuffer(const void* data, size_t size, Task* out_object);
/// Read an object from a valid FlatBuffer, allocating the object on heap.
/// The object must be freed after use by calling Task_free();
static Task* Task_new_from_flatbuffer(const void* data, size_t size);
/// Free memory allocated for vector and string properties, setting the freed pointers to NULL.
static void Task_free_pointers(Task* object);
/// Free Task* object pointer and all its property pointers (vectors and strings).
/// Equivalent to calling Task_free_pointers() followed by free();
static void Task_free(Task* object);
static bool Task_to_flatbuffer(flatcc_builder_t* B, const Task* object, void** out_buffer, size_t* out_size) {
assert(B);
assert(object);
assert(out_buffer);
assert(out_size);
flatcc_builder_reset(B);
flatcc_builder_start_buffer(B, 0, 0, 0);
flatcc_builder_ref_t offset_text = !object->text ? 0 : flatcc_builder_create_string_str(B, object->text);
if (flatcc_builder_start_table(B, 4) != 0) return false;
void* p;
flatcc_builder_ref_t* _p;
{
if (!(p = flatcc_builder_table_add(B, 0, 8, 8))) return false;
flatbuffers_uint64_write_to_pe(p, object->id);
}
if (offset_text) {
if (!(_p = flatcc_builder_table_add_offset(B, 1))) return false;
*_p = offset_text;
}
{
if (!(p = flatcc_builder_table_add(B, 2, 8, 8))) return false;
flatbuffers_uint64_write_to_pe(p, object->date_created);
}
{
if (!(p = flatcc_builder_table_add(B, 3, 8, 8))) return false;
flatbuffers_uint64_write_to_pe(p, object->date_finished);
}
flatcc_builder_ref_t ref;
if (!(ref = flatcc_builder_end_table(B))) return false;
if (!flatcc_builder_end_buffer(B, ref)) return false;
return (*out_buffer = flatcc_builder_finalize_aligned_buffer(B, out_size)) != NULL;
}
static bool Task_from_flatbuffer(const void* data, size_t size, Task* out_object) {
assert(data);
assert(size > 0);
assert(out_object);
const uint8_t* table = (const uint8_t*) data + __flatbuffers_uoffset_read_from_pe(data);
assert(table);
const flatbuffers_voffset_t* vt = (const flatbuffers_voffset_t*) (table - __flatbuffers_soffset_read_from_pe(table));
flatbuffers_voffset_t vs = __flatbuffers_voffset_read_from_pe(vt);
// variables reused when reading strings and vectors
flatbuffers_voffset_t offset;
const flatbuffers_uoffset_t* val;
size_t len;
// reset so that dangling pointers are freed properly on malloc() failures
#ifdef __cplusplus
*out_object = {};
#else
*out_object = (Task){0};
#endif
if ((offset = tasklist_obx_h_fb_field_offset(vs, vt, 0))) {
out_object->id = flatbuffers_uint64_read_from_pe(table + offset);
}
if ((offset = tasklist_obx_h_fb_field_offset(vs, vt, 1))) {
val = (const flatbuffers_uoffset_t*)(table + offset + sizeof(flatbuffers_uoffset_t) + __flatbuffers_uoffset_read_from_pe(table + offset));
len = (size_t) __flatbuffers_uoffset_read_from_pe(val - 1);
out_object->text = (char*) malloc((len+1) * sizeof(char));
if (out_object->text == NULL) {
Task_free_pointers(out_object);
return false;
}
memcpy((void*)out_object->text, (const void*)val, len+1);
} else {
out_object->text = NULL;
}
if ((offset = tasklist_obx_h_fb_field_offset(vs, vt, 2))) {
out_object->date_created = flatbuffers_uint64_read_from_pe(table + offset);
}
if ((offset = tasklist_obx_h_fb_field_offset(vs, vt, 3))) {
out_object->date_finished = flatbuffers_uint64_read_from_pe(table + offset);
}
return true;
}
static Task* Task_new_from_flatbuffer(const void* data, size_t size) {
Task* object = (Task*) malloc(sizeof(Task));
if (object) {
if (!Task_from_flatbuffer(data, size, object)) {
free(object);
object = NULL;
}
}
return object;
}
static void Task_free_pointers(Task* object) {
if (object == NULL) return;
if (object->text) {
free(object->text);
object->text = NULL;
}
}
static void Task_free(Task* object) {
Task_free_pointers(object);
free(object);
}
/// Insert or update the given object in the database.
/// @param object (in & out) will be updated with a newly inserted ID if the one specified previously was zero. If an ID
/// was already specified (non-zero), it will remain unchanged.
/// @return object ID from the object param (see object param docs) or a zero on error. If a zero was returned, you can
/// check obx_last_error_*() to get the error details. In an unlikely event that those functions return no error
/// code/message, the error occurred in FlatBuffers serialization, e.g. due to memory allocation issues.
static obx_id Task_put(OBX_box* box, Task* object) {
obx_id id = tasklist_obx_h_put_object(box, object,
(bool (*)(flatcc_builder_t*, const void*, void**, size_t*)) Task_to_flatbuffer,
OBXPutMode_PUT);
if (id != 0) {
object->id = id; // update the ID property on new objects for convenience
}
return id;
}
/// Read an object from the database, returning a pointer.
/// @return an object pointer or NULL if an object with the given ID doesn't exist or any other error occurred. You can
/// check obx_last_error_*() if NULL is returned to get the error details. In an unlikely event that those functions
/// return no error code/message, the error occurred in FlatBuffers serialization, e.g. due to memory allocation issues.
/// @note: The returned object must be freed after use by calling Task_free();
static Task* Task_get(OBX_box* box, obx_id id) {
return (Task*) tasklist_obx_h_get_object(box, id, (void* (*) (const void*, size_t)) Task_new_from_flatbuffer);
}
static obx_id tasklist_obx_h_put_object(OBX_box* box, void* object,
bool (*to_flatbuffer)(flatcc_builder_t*, const void*, void**, size_t*), OBXPutMode mode) {
flatcc_builder_t builder;
flatcc_builder_init(&builder);
obx_id id = 0;
size_t size = 0;
void* buffer = NULL;
if (!to_flatbuffer(&builder, object, &buffer, &size)) {
obx_last_error_set(OBX_ERROR_STD_OTHER, 0, "FlatBuffer serialization failed");
} else {
id = obx_box_put_object4(box, buffer, size, mode); // 0 on error
}
flatcc_builder_clear(&builder);
if (buffer) flatcc_builder_aligned_free(buffer);
return id;
}
static void* tasklist_obx_h_get_object(OBX_box* box, obx_id id, void* (*from_flatbuffer)(const void*, size_t)) {
// We need an explicit TX - read data lifecycle is bound to the open TX.
OBX_txn* tx = obx_txn_read(obx_box_store(box));
if (!tx) return NULL;
void* result = NULL;
const void* data;
size_t size;
if (obx_box_get(box, id, &data, &size) == OBX_SUCCESS) {
result = from_flatbuffer(data, size);
if (result == NULL) {
obx_last_error_set(OBX_ERROR_STD_OTHER, 0, "FlatBuffer deserialization failed");
}
}
obx_txn_close(tx);
return result;
}
static flatbuffers_voffset_t tasklist_obx_h_fb_field_offset(flatbuffers_voffset_t vs, const flatbuffers_voffset_t* vt, size_t field) {
return (vs < sizeof(vt[0]) * (field + 3)) ? 0 : __flatbuffers_voffset_read_from_pe(vt + field + 2);
}