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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/structs.h"
#include "gate/results.h"
#include "gate/objects.h"
#include "gate/arrays.h"
#include "gate/guids.h"
#include "gate/times.h"
#include "gate/blobs.h"
#include "gate/utilities.h"
#include "gate/properties.h"
gate_result_t gate_struct_item_init(gate_uint16_t type, void* ptr)
{
gate_result_t ret = GATE_RESULT_OK;
gate_size_t type_length = gate_type_length(type);
switch (type)
{
case GATE_TYPE_ARRAYLIST_BOOL: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_bool_t), NULL, 0, NULL, NULL); break;
case GATE_TYPE_ARRAYLIST_I8: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_int8_t), NULL, 0, NULL, NULL); break;
case GATE_TYPE_ARRAYLIST_UI8: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_uint8_t), NULL, 0, NULL, NULL); break;
case GATE_TYPE_ARRAYLIST_I16: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_int16_t), NULL, 0, NULL, NULL); break;
case GATE_TYPE_ARRAYLIST_UI16: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_uint16_t), NULL, 0, NULL, NULL); break;
case GATE_TYPE_ARRAYLIST_I32: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_int32_t), NULL, 0, NULL, NULL); break;
case GATE_TYPE_ARRAYLIST_UI32: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_uint32_t), NULL, 0, NULL, NULL); break;
case GATE_TYPE_ARRAYLIST_I64: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_int64_t), NULL, 0, NULL, NULL); break;
case GATE_TYPE_ARRAYLIST_UI64: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_uint64_t), NULL, 0, NULL, NULL); break;
case GATE_TYPE_ARRAYLIST_R32: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_real32_t), NULL, 0, NULL, NULL); break;
case GATE_TYPE_ARRAYLIST_R64: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_real64_t), NULL, 0, NULL, NULL); break;
case GATE_TYPE_ARRAYLIST_ADDRESS: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_uintptr_t), NULL, 0, NULL, NULL); break;
/* arraylist of basic gate structures (POD) */
case GATE_TYPE_ARRAYLIST_DATAPTR: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_dataptr_t), NULL, 0, NULL, NULL); break;
case GATE_TYPE_ARRAYLIST_FUNCPTR: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_funcptr_t), NULL, 0, NULL, NULL); break;
case GATE_TYPE_ARRAYLIST_CSTR: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(char const*), NULL, 0, NULL, NULL); break;
case GATE_TYPE_ARRAYLIST_WSTR: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(wchar_t const*), NULL, 0, NULL, NULL); break;
case GATE_TYPE_ARRAYLIST_GUID: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_guid_t), NULL, 0, NULL, NULL); break;
case GATE_TYPE_ARRAYLIST_DATE: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_date_t), NULL, 0, NULL, NULL); break;
case GATE_TYPE_ARRAYLIST_DAYTIME: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_daytime_t), NULL, 0, NULL, NULL); break;
case GATE_TYPE_ARRAYLIST_DATETIME: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_datetime_t), NULL, 0, NULL, NULL); break;
case GATE_TYPE_ARRAYLIST_TIME: *((gate_arraylist_t*)(ptr)) = gate_arraylist_create(sizeof(gate_time_t), NULL, 0, NULL, NULL); break;
/* arraylist of basic generic types */
case GATE_TYPE_ARRAYLIST_STRING: *((gate_arraylist_t*)(ptr)) = gate_util_stringarray_create(); break;
case GATE_TYPE_ARRAYLIST_ARRAY: *((gate_arraylist_t*)(ptr)) = NULL; break; /* must be initialized individually */
case GATE_TYPE_ARRAYLIST_BLOB: *((gate_arraylist_t*)(ptr)) = gate_util_blobarray_create(); break;
/* arraylist of basic reference types */
case GATE_TYPE_ARRAYLIST_STRUCT: *((gate_arraylist_t*)(ptr)) = NULL; break; /* must be initialized individually */
case GATE_TYPE_ARRAYLIST_OBJECT: *((gate_object_ptr_t*)(ptr)) = NULL; break; /* must be initialized individually */
case GATE_TYPE_ARRAYLIST_PROPERTY: gate_property_create_empty((gate_property_t*)(ptr)); break;
case GATE_TYPE_STRUCT: ((gate_struct_t*)(ptr))->struct_descriptor = NULL; break; /* must be initialized individually */
default:
{
gate_mem_clear(ptr, type_length);
break;
}
}
return ret;
}
gate_result_t gate_struct_item_release(gate_uint16_t type, void* ptr)
{
gate_result_t ret = GATE_RESULT_OK;
gate_size_t type_length = gate_type_length(type);
gate_struct_ptr_t struct_ptr;
switch (type)
{
case GATE_TYPE_STRUCT:
{
struct_ptr = *((gate_struct_ptr_t*)ptr);
ret = gate_struct_release(struct_ptr);
break;
}
case GATE_TYPE_STRING:
{
gate_string_release((gate_string_t*)ptr);
break;
}
case GATE_TYPE_BLOB:
{
gate_blob_release((gate_blob_t*)ptr);
break;
}
case GATE_TYPE_OBJECT:
{
gate_object_release(*((gate_object_ptr_t*)ptr));
break;
}
case GATE_TYPE_PROPERTY:
{
gate_property_destroy((gate_property_t*)ptr);
break;
}
case GATE_TYPE_ARRAY:
{
gate_array_release((gate_array_t*)ptr);
break;
}
case GATE_TYPE_EMPTY:
{
break;
}
default:
{
if (GATE_FLAG_ENABLED(type, GATE_TYPE_ARRAYLIST))
{
gate_arraylist_release(*(gate_arraylist_t*)ptr);
*((gate_arraylist_t*)ptr) = NULL;
}
else if (type_length == 0)
{
ret = GATE_RESULT_FAILED;
}
else
{
gate_mem_clear(ptr, type_length);
}
break;
}
}
return ret;
}
gate_result_t gate_struct_init(gate_struct_t* obj, gate_struct_descriptor_t const* descriptor)
{
gate_result_t ret = GATE_RESULT_FAILED;
char* obj_ptr = (char*)obj;
char* data_ptr;
size_t index;
do
{
if (obj == NULL)
{
ret = GATE_RESULT_INVALIDARG;
break;
}
if (descriptor != NULL)
{
obj->struct_descriptor = descriptor;
}
else
{
descriptor = obj->struct_descriptor;
}
if (obj->struct_descriptor == NULL)
{
ret = GATE_RESULT_INVALIDARG;
break;
}
ret = GATE_RESULT_OK;
for (index = 0; index != descriptor->item_count; ++index)
{
data_ptr = obj_ptr + descriptor->items[index].offset;
ret = gate_struct_item_init(descriptor->items[index].type, data_ptr);
if (GATE_FAILED(ret))
{
/* release all previous created data */
while (index != 0)
{
--index;
data_ptr = obj_ptr + descriptor->items[index].offset;
gate_struct_item_release(descriptor->items[index].type, data_ptr);
}
break;
}
}
} while (0);
return ret;
}
gate_result_t gate_struct_copy(gate_struct_t* dst, gate_struct_t const* src)
{
gate_result_t ret = GATE_RESULT_FAILED;
char* ptr_dst = (char*)dst;
char const* ptr_src = (char const*)src;
gate_size_t index;
if (dst->struct_descriptor == NULL)
{
dst->struct_descriptor = src->struct_descriptor;
}
do
{
if (dst->struct_descriptor->item_count != src->struct_descriptor->item_count)
{
ret = GATE_RESULT_INCORRECTTYPE;
break;
}
ret = GATE_RESULT_OK;
for (index = 0; index != src->struct_descriptor->item_count; ++index)
{
if (dst->struct_descriptor->items[index].type == src->struct_descriptor->items[index].type)
{
ret = gate_type_copy(dst->struct_descriptor->items[index].type,
ptr_dst + dst->struct_descriptor->items[index].offset,
ptr_src + src->struct_descriptor->items[index].offset);
if (GATE_FAILED(ret))
{
while (index-- != 0)
{
gate_type_release(dst->struct_descriptor->items[index].type, ptr_dst + dst->struct_descriptor->items[index].offset);
}
break;
}
}
else
{
ret = GATE_RESULT_INCORRECTTYPE;
break;
}
}
} while (0);
return ret;
}
gate_size_t gate_struct_length(gate_struct_t const* src)
{
return src->struct_descriptor->struct_length;
}
char const* gate_struct_get_name(gate_struct_t const* src)
{
return src->struct_descriptor->name;
}
gate_size_t gate_struct_get_member_count(gate_struct_t const* src)
{
return src->struct_descriptor->item_count;
}
gate_uint16_t gate_struct_get_member_type(gate_struct_t const* src, gate_size_t index)
{
if (index >= src->struct_descriptor->item_count)
{
return 0;
}
return src->struct_descriptor->items[index].type;
}
gate_uint16_t gate_struct_get_member_type_by_string(gate_struct_t const* src, gate_string_t const* name)
{
gate_size_t index;
gate_size_t len;
for (index = 0; index != src->struct_descriptor->item_count; ++index)
{
len = gate_str_length(src->struct_descriptor->items[index].name);
if (0 == gate_str_compare(src->struct_descriptor->items[index].name, len, name->str, name->length))
{
return src->struct_descriptor->items[index].type;
}
}
return GATE_TYPE_EMPTY;
}
gate_uint16_t gate_struct_get_member_type_by_name(gate_struct_t const* src, char const* name)
{
gate_uint16_t type = GATE_TYPE_EMPTY;
gate_string_t str_name = GATE_STRING_INIT_EMPTY;
gate_string_create_static(&str_name, name);
type = gate_struct_get_member_type_by_string(src, &str_name);
gate_string_release(&str_name);
return type;
}
char const* gate_struct_get_member_name(gate_struct_t const* src, gate_size_t index)
{
if (index >= src->struct_descriptor->item_count)
{
return NULL;
}
return src->struct_descriptor->items[index].name;
}
void const* gate_struct_get_member(gate_struct_t const* src, gate_size_t index)
{
if (index >= src->struct_descriptor->item_count)
{
return NULL;
}
return (void const*)(((char const*)src) + src->struct_descriptor->items[index].offset);
}
void const* gate_struct_get_member_by_string(gate_struct_t const* src, gate_string_t const* name)
{
gate_size_t index;
gate_size_t len;
for (index = 0; index != src->struct_descriptor->item_count; ++index)
{
len = gate_str_length(src->struct_descriptor->items[index].name);
if (0 == gate_str_compare(src->struct_descriptor->items[index].name, len, name->str, name->length))
{
return (void const*)(((char const*)src) + src->struct_descriptor->items[index].offset);
}
}
return NULL;
}
void const* gate_struct_get_member_by_name(gate_struct_t const* src, char const* name)
{
void const* ret = NULL;
gate_string_t str_name = GATE_STRING_INIT_EMPTY;
if (NULL != gate_string_create_static(&str_name, name))
{
ret = gate_struct_get_member_by_string(src, &str_name);
gate_string_release(&str_name);
}
return ret;
}
void* gate_struct_get_mutable_member(gate_struct_t* src, gate_size_t index)
{
if (index >= src->struct_descriptor->item_count)
{
return NULL;
}
return (void*)(((char*)src) + src->struct_descriptor->items[index].offset);
}
gate_result_t gate_struct_release(gate_struct_ptr_t obj)
{
gate_result_t ret = GATE_RESULT_OK;
char* data_ptr;<--- The scope of the variable 'data_ptr' can be reduced. [+]The scope of the variable 'data_ptr' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
char* obj_ptr = (char*)obj;
gate_size_t index = 0;
if (obj->struct_descriptor)
{
for (index = 0; index != obj->struct_descriptor->item_count; ++index)
{
data_ptr = obj_ptr + obj->struct_descriptor->items[index].offset;
gate_struct_item_release(obj->struct_descriptor->items[index].type, data_ptr);
}
}
return ret;
}
gate_result_t gate_struct_copy_constructor(void* dest, void const* src)
{
gate_result_t result = gate_struct_copy((gate_struct_t*)dest, (gate_struct_t const*)src);
return result;
}
void gate_struct_destructor(void* dest)
{
gate_struct_release((gate_struct_ptr_t)dest);
}
|