Commit | Line | Data |
---|---|---|
273b65be | 1 | /* |
f2b0325d | 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
de9dd397 | 3 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
273b65be JG |
4 | * |
5 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | * of this software and associated documentation files (the "Software"), to deal | |
7 | * in the Software without restriction, including without limitation the rights | |
8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | * copies of the Software, and to permit persons to whom the Software is | |
10 | * furnished to do so, subject to the following conditions: | |
11 | * | |
12 | * The above copyright notice and this permission notice shall be included in | |
13 | * all copies or substantial portions of the Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
21 | * SOFTWARE. | |
22 | */ | |
23 | ||
fc25abce PP |
24 | #define BT_LOG_TAG "FIELDS" |
25 | #include <babeltrace/lib-logging-internal.h> | |
26 | ||
8deee039 | 27 | #include <babeltrace/assert-pre-internal.h> |
0f15f666 PP |
28 | #include <babeltrace/trace-ir/field.h> |
29 | #include <babeltrace/trace-ir/field-const.h> | |
30 | #include <babeltrace/trace-ir/field-internal.h> | |
31 | #include <babeltrace/trace-ir/field-class-internal.h> | |
83509119 | 32 | #include <babeltrace/object-internal.h> |
8138bfe1 | 33 | #include <babeltrace/object.h> |
3d9990ac PP |
34 | #include <babeltrace/compiler-internal.h> |
35 | #include <babeltrace/compat/fcntl-internal.h> | |
36 | #include <babeltrace/align-internal.h> | |
8b45963b | 37 | #include <babeltrace/assert-internal.h> |
fc25abce | 38 | #include <inttypes.h> |
273b65be | 39 | |
18acc6f8 | 40 | static |
7b33a0e0 | 41 | void reset_single_field(struct bt_field *field); |
18acc6f8 PP |
42 | |
43 | static | |
7b33a0e0 | 44 | void reset_array_field(struct bt_field *field); |
18acc6f8 PP |
45 | |
46 | static | |
7b33a0e0 | 47 | void reset_structure_field(struct bt_field *field); |
18acc6f8 PP |
48 | |
49 | static | |
7b33a0e0 | 50 | void reset_variant_field(struct bt_field *field); |
18acc6f8 PP |
51 | |
52 | static | |
7b33a0e0 | 53 | void set_single_field_is_frozen(struct bt_field *field, bool is_frozen); |
18acc6f8 PP |
54 | |
55 | static | |
7b33a0e0 | 56 | void set_array_field_is_frozen(struct bt_field *field, bool is_frozen); |
18acc6f8 PP |
57 | |
58 | static | |
7b33a0e0 | 59 | void set_structure_field_is_frozen(struct bt_field *field, bool is_frozen); |
18acc6f8 PP |
60 | |
61 | static | |
7b33a0e0 | 62 | void set_variant_field_is_frozen(struct bt_field *field, bool is_frozen); |
18acc6f8 PP |
63 | |
64 | static | |
78cf9df6 | 65 | bool single_field_is_set(const struct bt_field *field); |
18acc6f8 PP |
66 | |
67 | static | |
78cf9df6 | 68 | bool array_field_is_set(const struct bt_field *field); |
18acc6f8 PP |
69 | |
70 | static | |
78cf9df6 | 71 | bool structure_field_is_set(const struct bt_field *field); |
18acc6f8 PP |
72 | |
73 | static | |
78cf9df6 | 74 | bool variant_field_is_set(const struct bt_field *field); |
18acc6f8 PP |
75 | |
76 | static | |
7b33a0e0 PP |
77 | struct bt_field_methods integer_field_methods = { |
78 | .set_is_frozen = set_single_field_is_frozen, | |
79 | .is_set = single_field_is_set, | |
80 | .reset = reset_single_field, | |
8deee039 | 81 | }; |
273b65be | 82 | |
7b33a0e0 PP |
83 | static |
84 | struct bt_field_methods real_field_methods = { | |
85 | .set_is_frozen = set_single_field_is_frozen, | |
86 | .is_set = single_field_is_set, | |
87 | .reset = reset_single_field, | |
12c8a1a3 JG |
88 | }; |
89 | ||
7b33a0e0 PP |
90 | static |
91 | struct bt_field_methods string_field_methods = { | |
92 | .set_is_frozen = set_single_field_is_frozen, | |
93 | .is_set = single_field_is_set, | |
94 | .reset = reset_single_field, | |
273b65be JG |
95 | }; |
96 | ||
7b33a0e0 PP |
97 | static |
98 | struct bt_field_methods structure_field_methods = { | |
99 | .set_is_frozen = set_structure_field_is_frozen, | |
100 | .is_set = structure_field_is_set, | |
101 | .reset = reset_structure_field, | |
87d43dc1 JG |
102 | }; |
103 | ||
7b33a0e0 PP |
104 | static |
105 | struct bt_field_methods array_field_methods = { | |
106 | .set_is_frozen = set_array_field_is_frozen, | |
107 | .is_set = array_field_is_set, | |
108 | .reset = reset_array_field, | |
918be005 PP |
109 | }; |
110 | ||
76f869ab | 111 | static |
7b33a0e0 PP |
112 | struct bt_field_methods variant_field_methods = { |
113 | .set_is_frozen = set_variant_field_is_frozen, | |
114 | .is_set = variant_field_is_set, | |
115 | .reset = reset_variant_field, | |
116 | }; | |
76f869ab | 117 | |
8deee039 | 118 | static |
939190b3 | 119 | struct bt_field *create_integer_field(struct bt_field_class *); |
8deee039 PP |
120 | |
121 | static | |
939190b3 | 122 | struct bt_field *create_real_field(struct bt_field_class *); |
8deee039 PP |
123 | |
124 | static | |
939190b3 | 125 | struct bt_field *create_string_field(struct bt_field_class *); |
8deee039 PP |
126 | |
127 | static | |
939190b3 | 128 | struct bt_field *create_structure_field(struct bt_field_class *); |
8deee039 PP |
129 | |
130 | static | |
939190b3 | 131 | struct bt_field *create_static_array_field(struct bt_field_class *); |
8b45963b | 132 | |
8deee039 | 133 | static |
939190b3 | 134 | struct bt_field *create_dynamic_array_field(struct bt_field_class *); |
8b45963b | 135 | |
8deee039 | 136 | static |
939190b3 | 137 | struct bt_field *create_variant_field(struct bt_field_class *); |
8b45963b | 138 | |
8deee039 | 139 | static |
939190b3 | 140 | struct bt_field *(* const field_create_funcs[])(struct bt_field_class *) = { |
af0c18e3 PP |
141 | [BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER] = create_integer_field, |
142 | [BT_FIELD_CLASS_TYPE_SIGNED_INTEGER] = create_integer_field, | |
143 | [BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION] = create_integer_field, | |
144 | [BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION] = create_integer_field, | |
145 | [BT_FIELD_CLASS_TYPE_REAL] = create_real_field, | |
146 | [BT_FIELD_CLASS_TYPE_STRING] = create_string_field, | |
147 | [BT_FIELD_CLASS_TYPE_STRUCTURE] = create_structure_field, | |
148 | [BT_FIELD_CLASS_TYPE_STATIC_ARRAY] = create_static_array_field, | |
149 | [BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY] = create_dynamic_array_field, | |
150 | [BT_FIELD_CLASS_TYPE_VARIANT] = create_variant_field, | |
8deee039 | 151 | }; |
8b45963b | 152 | |
a6918753 | 153 | static |
7b33a0e0 | 154 | void destroy_integer_field(struct bt_field *field); |
a6918753 PP |
155 | |
156 | static | |
7b33a0e0 | 157 | void destroy_real_field(struct bt_field *field); |
a6918753 PP |
158 | |
159 | static | |
7b33a0e0 | 160 | void destroy_string_field(struct bt_field *field); |
a6918753 PP |
161 | |
162 | static | |
7b33a0e0 | 163 | void destroy_structure_field(struct bt_field *field); |
a6918753 PP |
164 | |
165 | static | |
7b33a0e0 | 166 | void destroy_array_field(struct bt_field *field); |
a6918753 PP |
167 | |
168 | static | |
7b33a0e0 | 169 | void destroy_variant_field(struct bt_field *field); |
a6918753 PP |
170 | |
171 | static | |
172 | void (* const field_destroy_funcs[])(struct bt_field *) = { | |
af0c18e3 PP |
173 | [BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER] = destroy_integer_field, |
174 | [BT_FIELD_CLASS_TYPE_SIGNED_INTEGER] = destroy_integer_field, | |
175 | [BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION] = destroy_integer_field, | |
176 | [BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION] = destroy_integer_field, | |
177 | [BT_FIELD_CLASS_TYPE_REAL] = destroy_real_field, | |
178 | [BT_FIELD_CLASS_TYPE_STRING] = destroy_string_field, | |
179 | [BT_FIELD_CLASS_TYPE_STRUCTURE] = destroy_structure_field, | |
180 | [BT_FIELD_CLASS_TYPE_STATIC_ARRAY] = destroy_array_field, | |
181 | [BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY] = destroy_array_field, | |
182 | [BT_FIELD_CLASS_TYPE_VARIANT] = destroy_variant_field, | |
a6918753 PP |
183 | }; |
184 | ||
78cf9df6 | 185 | struct bt_field_class *bt_field_borrow_class(const struct bt_field *field) |
18acc6f8 | 186 | { |
7b33a0e0 | 187 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
939190b3 | 188 | return field->class; |
18acc6f8 PP |
189 | } |
190 | ||
78cf9df6 PP |
191 | const struct bt_field_class *bt_field_borrow_class_const( |
192 | const struct bt_field *field) | |
9e550e5f | 193 | { |
78cf9df6 PP |
194 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
195 | return field->class; | |
9e550e5f PP |
196 | } |
197 | ||
78cf9df6 | 198 | enum bt_field_class_type bt_field_get_class_type(const struct bt_field *field) |
18acc6f8 | 199 | { |
7b33a0e0 | 200 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
af0c18e3 | 201 | return field->class->type; |
18acc6f8 PP |
202 | } |
203 | ||
a6918753 | 204 | BT_HIDDEN |
939190b3 | 205 | struct bt_field *bt_field_create(struct bt_field_class *fc) |
273b65be | 206 | { |
839d52a5 | 207 | struct bt_field *field = NULL; |
7b33a0e0 | 208 | |
939190b3 | 209 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
af0c18e3 PP |
210 | BT_ASSERT(bt_field_class_has_known_type(fc)); |
211 | field = field_create_funcs[fc->type](fc); | |
273b65be | 212 | if (!field) { |
66fd07a5 | 213 | BT_LIB_LOGE("Cannot create field object from field class: " |
939190b3 | 214 | "%![fc-]+F", fc); |
8deee039 | 215 | goto end; |
273b65be JG |
216 | } |
217 | ||
8deee039 PP |
218 | end: |
219 | return field; | |
273b65be JG |
220 | } |
221 | ||
7b33a0e0 | 222 | static inline |
939190b3 | 223 | void init_field(struct bt_field *field, struct bt_field_class *fc, |
7b33a0e0 | 224 | struct bt_field_methods *methods) |
cd95e351 | 225 | { |
7b33a0e0 | 226 | BT_ASSERT(field); |
939190b3 | 227 | BT_ASSERT(fc); |
7b33a0e0 PP |
228 | bt_object_init_unique(&field->base); |
229 | field->methods = methods; | |
4b70020d PP |
230 | field->class = fc; |
231 | bt_object_get_no_null_check(fc); | |
cd95e351 JG |
232 | } |
233 | ||
7b33a0e0 | 234 | static |
939190b3 | 235 | struct bt_field *create_integer_field(struct bt_field_class *fc) |
4ebcc695 | 236 | { |
7b33a0e0 PP |
237 | struct bt_field_integer *int_field; |
238 | ||
939190b3 | 239 | BT_LIB_LOGD("Creating integer field object: %![fc-]+F", fc); |
7b33a0e0 PP |
240 | int_field = g_new0(struct bt_field_integer, 1); |
241 | if (!int_field) { | |
242 | BT_LOGE_STR("Failed to allocate one integer field."); | |
243 | goto end; | |
244 | } | |
245 | ||
939190b3 | 246 | init_field((void *) int_field, fc, &integer_field_methods); |
7b33a0e0 PP |
247 | BT_LIB_LOGD("Created integer field object: %!+f", int_field); |
248 | ||
249 | end: | |
250 | return (void *) int_field; | |
4ebcc695 PP |
251 | } |
252 | ||
7b33a0e0 | 253 | static |
939190b3 | 254 | struct bt_field *create_real_field(struct bt_field_class *fc) |
fea75608 | 255 | { |
7b33a0e0 | 256 | struct bt_field_real *real_field; |
18acc6f8 | 257 | |
939190b3 | 258 | BT_LIB_LOGD("Creating real field object: %![fc-]+F", fc); |
7b33a0e0 PP |
259 | real_field = g_new0(struct bt_field_real, 1); |
260 | if (!real_field) { | |
261 | BT_LOGE_STR("Failed to allocate one real field."); | |
262 | goto end; | |
263 | } | |
264 | ||
939190b3 | 265 | init_field((void *) real_field, fc, &real_field_methods); |
7b33a0e0 PP |
266 | BT_LIB_LOGD("Created real field object: %!+f", real_field); |
267 | ||
268 | end: | |
269 | return (void *) real_field; | |
fea75608 PP |
270 | } |
271 | ||
7b33a0e0 | 272 | static |
939190b3 | 273 | struct bt_field *create_string_field(struct bt_field_class *fc) |
273b65be | 274 | { |
7b33a0e0 | 275 | struct bt_field_string *string_field; |
18acc6f8 | 276 | |
939190b3 | 277 | BT_LIB_LOGD("Creating string field object: %![fc-]+F", fc); |
7b33a0e0 PP |
278 | string_field = g_new0(struct bt_field_string, 1); |
279 | if (!string_field) { | |
280 | BT_LOGE_STR("Failed to allocate one string field."); | |
281 | goto end; | |
282 | } | |
18acc6f8 | 283 | |
939190b3 | 284 | init_field((void *) string_field, fc, &string_field_methods); |
7b33a0e0 PP |
285 | string_field->buf = g_array_sized_new(FALSE, FALSE, |
286 | sizeof(char), 1); | |
287 | if (!string_field->buf) { | |
288 | BT_LOGE_STR("Failed to allocate a GArray."); | |
8138bfe1 | 289 | BT_OBJECT_PUT_REF_AND_RESET(string_field); |
7b33a0e0 PP |
290 | goto end; |
291 | } | |
18acc6f8 | 292 | |
7b33a0e0 PP |
293 | g_array_index(string_field->buf, char, 0) = '\0'; |
294 | BT_LIB_LOGD("Created string field object: %!+f", string_field); | |
18acc6f8 | 295 | |
7b33a0e0 PP |
296 | end: |
297 | return (void *) string_field; | |
298 | } | |
18acc6f8 | 299 | |
7b33a0e0 | 300 | static inline |
939190b3 PP |
301 | int create_fields_from_named_field_classes( |
302 | struct bt_field_class_named_field_class_container *fc, | |
7b33a0e0 PP |
303 | GPtrArray **fields) |
304 | { | |
305 | int ret = 0; | |
306 | uint64_t i; | |
18acc6f8 | 307 | |
7b33a0e0 PP |
308 | *fields = g_ptr_array_new_with_free_func( |
309 | (GDestroyNotify) bt_field_destroy); | |
310 | if (!*fields) { | |
311 | BT_LOGE_STR("Failed to allocate a GPtrArray."); | |
312 | ret = -1; | |
313 | goto end; | |
18acc6f8 PP |
314 | } |
315 | ||
939190b3 | 316 | g_ptr_array_set_size(*fields, fc->named_fcs->len); |
7b33a0e0 | 317 | |
939190b3 | 318 | for (i = 0; i < fc->named_fcs->len; i++) { |
7b33a0e0 | 319 | struct bt_field *field; |
939190b3 PP |
320 | struct bt_named_field_class *named_fc = |
321 | BT_FIELD_CLASS_NAMED_FC_AT_INDEX(fc, i); | |
7b33a0e0 | 322 | |
939190b3 | 323 | field = bt_field_create(named_fc->fc); |
7b33a0e0 PP |
324 | if (!field) { |
325 | BT_LIB_LOGE("Failed to create structure member or variant option field: " | |
939190b3 PP |
326 | "name=\"%s\", %![fc-]+F", |
327 | named_fc->name->str, named_fc->fc); | |
7b33a0e0 PP |
328 | ret = -1; |
329 | goto end; | |
330 | } | |
331 | ||
332 | g_ptr_array_index(*fields, i) = field; | |
333 | } | |
18acc6f8 PP |
334 | |
335 | end: | |
336 | return ret; | |
273b65be JG |
337 | } |
338 | ||
7b33a0e0 | 339 | static |
939190b3 | 340 | struct bt_field *create_structure_field(struct bt_field_class *fc) |
cd95e351 | 341 | { |
7b33a0e0 | 342 | struct bt_field_structure *struct_field; |
18acc6f8 | 343 | |
939190b3 | 344 | BT_LIB_LOGD("Creating structure field object: %![fc-]+F", fc); |
7b33a0e0 PP |
345 | struct_field = g_new0(struct bt_field_structure, 1); |
346 | if (!struct_field) { | |
347 | BT_LOGE_STR("Failed to allocate one structure field."); | |
348 | goto end; | |
349 | } | |
fc25abce | 350 | |
939190b3 | 351 | init_field((void *) struct_field, fc, &structure_field_methods); |
7b33a0e0 | 352 | |
939190b3 | 353 | if (create_fields_from_named_field_classes((void *) fc, |
7b33a0e0 PP |
354 | &struct_field->fields)) { |
355 | BT_LIB_LOGE("Cannot create structure member fields: " | |
939190b3 | 356 | "%![fc-]+F", fc); |
8138bfe1 | 357 | BT_OBJECT_PUT_REF_AND_RESET(struct_field); |
7b33a0e0 | 358 | goto end; |
18acc6f8 PP |
359 | } |
360 | ||
7b33a0e0 | 361 | BT_LIB_LOGD("Created structure field object: %!+f", struct_field); |
18acc6f8 | 362 | |
7b33a0e0 PP |
363 | end: |
364 | return (void *) struct_field; | |
cd95e351 JG |
365 | } |
366 | ||
7b33a0e0 | 367 | static |
939190b3 | 368 | struct bt_field *create_variant_field(struct bt_field_class *fc) |
273b65be | 369 | { |
7b33a0e0 | 370 | struct bt_field_variant *var_field; |
18acc6f8 | 371 | |
939190b3 | 372 | BT_LIB_LOGD("Creating variant field object: %![fc-]+F", fc); |
7b33a0e0 PP |
373 | var_field = g_new0(struct bt_field_variant, 1); |
374 | if (!var_field) { | |
375 | BT_LOGE_STR("Failed to allocate one variant field."); | |
376 | goto end; | |
377 | } | |
8b45963b | 378 | |
939190b3 | 379 | init_field((void *) var_field, fc, &variant_field_methods); |
18acc6f8 | 380 | |
939190b3 | 381 | if (create_fields_from_named_field_classes((void *) fc, |
7b33a0e0 PP |
382 | &var_field->fields)) { |
383 | BT_LIB_LOGE("Cannot create variant member fields: " | |
939190b3 | 384 | "%![fc-]+F", fc); |
8138bfe1 | 385 | BT_OBJECT_PUT_REF_AND_RESET(var_field); |
7b33a0e0 PP |
386 | goto end; |
387 | } | |
273b65be | 388 | |
7b33a0e0 | 389 | BT_LIB_LOGD("Created variant field object: %!+f", var_field); |
18acc6f8 | 390 | |
7b33a0e0 PP |
391 | end: |
392 | return (void *) var_field; | |
18acc6f8 PP |
393 | } |
394 | ||
395 | static inline | |
7b33a0e0 | 396 | int init_array_field_fields(struct bt_field_array *array_field) |
18acc6f8 PP |
397 | { |
398 | int ret = 0; | |
7b33a0e0 | 399 | uint64_t i; |
939190b3 | 400 | struct bt_field_class_array *array_fc; |
18acc6f8 | 401 | |
7b33a0e0 | 402 | BT_ASSERT(array_field); |
939190b3 | 403 | array_fc = (void *) array_field->common.class; |
7b33a0e0 PP |
404 | array_field->fields = g_ptr_array_sized_new(array_field->length); |
405 | if (!array_field->fields) { | |
406 | BT_LOGE_STR("Failed to allocate a GPtrArray."); | |
18acc6f8 PP |
407 | ret = -1; |
408 | goto end; | |
409 | } | |
410 | ||
7b33a0e0 PP |
411 | g_ptr_array_set_free_func(array_field->fields, |
412 | (GDestroyNotify) bt_field_destroy); | |
413 | g_ptr_array_set_size(array_field->fields, array_field->length); | |
414 | ||
415 | for (i = 0; i < array_field->length; i++) { | |
416 | array_field->fields->pdata[i] = bt_field_create( | |
939190b3 | 417 | array_fc->element_fc); |
7b33a0e0 PP |
418 | if (!array_field->fields->pdata[i]) { |
419 | BT_LIB_LOGE("Cannot create array field's element field: " | |
939190b3 | 420 | "index=%" PRIu64 ", %![fc-]+F", i, array_fc); |
7b33a0e0 PP |
421 | ret = -1; |
422 | goto end; | |
423 | } | |
424 | } | |
18acc6f8 PP |
425 | |
426 | end: | |
427 | return ret; | |
3f4a108d PP |
428 | } |
429 | ||
7b33a0e0 | 430 | static |
939190b3 | 431 | struct bt_field *create_static_array_field(struct bt_field_class *fc) |
f78d67fb | 432 | { |
939190b3 | 433 | struct bt_field_class_static_array *array_fc = (void *) fc; |
7b33a0e0 | 434 | struct bt_field_array *array_field; |
a6918753 | 435 | |
939190b3 | 436 | BT_LIB_LOGD("Creating static array field object: %![fc-]+F", fc); |
7b33a0e0 PP |
437 | array_field = g_new0(struct bt_field_array, 1); |
438 | if (!array_field) { | |
439 | BT_LOGE_STR("Failed to allocate one static array field."); | |
440 | goto end; | |
441 | } | |
f78d67fb | 442 | |
939190b3 PP |
443 | init_field((void *) array_field, fc, &array_field_methods); |
444 | array_field->length = array_fc->length; | |
18acc6f8 | 445 | |
7b33a0e0 PP |
446 | if (init_array_field_fields(array_field)) { |
447 | BT_LIB_LOGE("Cannot create static array fields: " | |
939190b3 | 448 | "%![fc-]+F", fc); |
8138bfe1 | 449 | BT_OBJECT_PUT_REF_AND_RESET(array_field); |
7b33a0e0 PP |
450 | goto end; |
451 | } | |
a6918753 | 452 | |
7b33a0e0 | 453 | BT_LIB_LOGD("Created static array field object: %!+f", array_field); |
18acc6f8 | 454 | |
7b33a0e0 PP |
455 | end: |
456 | return (void *) array_field; | |
273b65be JG |
457 | } |
458 | ||
7b33a0e0 | 459 | static |
939190b3 | 460 | struct bt_field *create_dynamic_array_field(struct bt_field_class *fc) |
cd95e351 | 461 | { |
7b33a0e0 | 462 | struct bt_field_array *array_field; |
a6918753 | 463 | |
939190b3 | 464 | BT_LIB_LOGD("Creating dynamic array field object: %![fc-]+F", fc); |
7b33a0e0 PP |
465 | array_field = g_new0(struct bt_field_array, 1); |
466 | if (!array_field) { | |
467 | BT_LOGE_STR("Failed to allocate one dynamic array field."); | |
468 | goto end; | |
469 | } | |
470 | ||
939190b3 | 471 | init_field((void *) array_field, fc, &array_field_methods); |
7b33a0e0 PP |
472 | |
473 | if (init_array_field_fields(array_field)) { | |
474 | BT_LIB_LOGE("Cannot create dynamic array fields: " | |
939190b3 | 475 | "%![fc-]+F", fc); |
8138bfe1 | 476 | BT_OBJECT_PUT_REF_AND_RESET(array_field); |
7b33a0e0 | 477 | goto end; |
18acc6f8 PP |
478 | } |
479 | ||
7b33a0e0 PP |
480 | BT_LIB_LOGD("Created dynamic array field object: %!+f", array_field); |
481 | ||
482 | end: | |
483 | return (void *) array_field; | |
a6918753 PP |
484 | } |
485 | ||
78cf9df6 | 486 | int64_t bt_field_signed_integer_get_value(const struct bt_field *field) |
a6918753 | 487 | { |
78cf9df6 | 488 | const struct bt_field_integer *int_field = (const void *) field; |
a6918753 | 489 | |
7b33a0e0 PP |
490 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
491 | BT_ASSERT_PRE_FIELD_IS_SET(field, "Field"); | |
492 | BT_ASSERT_PRE_FIELD_IS_SIGNED_INT(field, "Field"); | |
493 | return int_field->value.i; | |
cd95e351 JG |
494 | } |
495 | ||
78cf9df6 | 496 | void bt_field_signed_integer_set_value(struct bt_field *field, int64_t value) |
cd95e351 | 497 | { |
7b33a0e0 | 498 | struct bt_field_integer *int_field = (void *) field; |
a6918753 | 499 | |
7b33a0e0 PP |
500 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
501 | BT_ASSERT_PRE_FIELD_IS_SIGNED_INT(field, "Field"); | |
502 | BT_ASSERT_PRE_FIELD_HOT(field, "Field"); | |
503 | BT_ASSERT_PRE(bt_util_value_is_in_range_signed( | |
939190b3 | 504 | ((struct bt_field_class_integer *) field->class)->range, value), |
7b33a0e0 | 505 | "Value is out of bounds: value=%" PRId64 ", %![field-]+f, " |
939190b3 | 506 | "%![fc-]+F", value, field, field->class); |
7b33a0e0 PP |
507 | int_field->value.i = value; |
508 | bt_field_set_single(field, true); | |
cd95e351 JG |
509 | } |
510 | ||
78cf9df6 | 511 | uint64_t bt_field_unsigned_integer_get_value(const struct bt_field *field) |
273b65be | 512 | { |
78cf9df6 | 513 | const struct bt_field_integer *int_field = (const void *) field; |
7b33a0e0 PP |
514 | |
515 | BT_ASSERT_PRE_NON_NULL(field, "Field"); | |
516 | BT_ASSERT_PRE_FIELD_IS_SET(field, "Field"); | |
517 | BT_ASSERT_PRE_FIELD_IS_UNSIGNED_INT(field, "Field"); | |
518 | return int_field->value.u; | |
273b65be JG |
519 | } |
520 | ||
78cf9df6 | 521 | void bt_field_unsigned_integer_set_value(struct bt_field *field, uint64_t value) |
cd95e351 | 522 | { |
7b33a0e0 | 523 | struct bt_field_integer *int_field = (void *) field; |
a6918753 | 524 | |
7b33a0e0 PP |
525 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
526 | BT_ASSERT_PRE_FIELD_IS_UNSIGNED_INT(field, "Field"); | |
527 | BT_ASSERT_PRE_FIELD_HOT(field, "Field"); | |
528 | BT_ASSERT_PRE(bt_util_value_is_in_range_unsigned( | |
939190b3 | 529 | ((struct bt_field_class_integer *) field->class)->range, value), |
7b33a0e0 | 530 | "Value is out of bounds: value=%" PRIu64 ", %![field-]+f, " |
939190b3 | 531 | "%![fc-]+F", value, field, field->class); |
7b33a0e0 PP |
532 | int_field->value.u = value; |
533 | bt_field_set_single(field, true); | |
cd95e351 JG |
534 | } |
535 | ||
78cf9df6 | 536 | double bt_field_real_get_value(const struct bt_field *field) |
273b65be | 537 | { |
78cf9df6 | 538 | const struct bt_field_real *real_field = (const void *) field; |
7b33a0e0 PP |
539 | |
540 | BT_ASSERT_PRE_NON_NULL(field, "Field"); | |
541 | BT_ASSERT_PRE_FIELD_IS_SET(field, "Field"); | |
af0c18e3 | 542 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_REAL, "Field"); |
7b33a0e0 | 543 | return real_field->value; |
8b45963b PP |
544 | } |
545 | ||
78cf9df6 | 546 | void bt_field_real_set_value(struct bt_field *field, double value) |
8b45963b | 547 | { |
7b33a0e0 | 548 | struct bt_field_real *real_field = (void *) field; |
18acc6f8 | 549 | |
7b33a0e0 | 550 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
af0c18e3 | 551 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_REAL, "Field"); |
7b33a0e0 PP |
552 | BT_ASSERT_PRE_FIELD_HOT(field, "Field"); |
553 | BT_ASSERT_PRE( | |
939190b3 | 554 | !((struct bt_field_class_real *) field->class)->is_single_precision || |
7b33a0e0 PP |
555 | (double) (float) value == value, |
556 | "Invalid value for a single-precision real number: value=%f, " | |
939190b3 | 557 | "%![fc-]+F", value, field->class); |
7b33a0e0 PP |
558 | real_field->value = value; |
559 | bt_field_set_single(field, true); | |
560 | } | |
561 | ||
78cf9df6 PP |
562 | int bt_field_unsigned_enumeration_get_mapping_labels( |
563 | const struct bt_field *field, | |
939190b3 | 564 | bt_field_class_enumeration_mapping_label_array *label_array, |
7b33a0e0 PP |
565 | uint64_t *count) |
566 | { | |
78cf9df6 | 567 | const struct bt_field_integer *int_field = (const void *) field; |
7b33a0e0 PP |
568 | |
569 | BT_ASSERT_PRE_NON_NULL(field, "Field"); | |
570 | BT_ASSERT_PRE_NON_NULL(label_array, "Label array (output)"); | |
571 | BT_ASSERT_PRE_NON_NULL(label_array, "Count (output)"); | |
572 | BT_ASSERT_PRE_FIELD_IS_SET(field, "Field"); | |
af0c18e3 PP |
573 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, |
574 | BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, "Field"); | |
939190b3 PP |
575 | return bt_field_class_unsigned_enumeration_get_mapping_labels_by_value( |
576 | field->class, int_field->value.u, label_array, count); | |
273b65be JG |
577 | } |
578 | ||
78cf9df6 PP |
579 | int bt_field_signed_enumeration_get_mapping_labels( |
580 | const struct bt_field *field, | |
939190b3 | 581 | bt_field_class_enumeration_mapping_label_array *label_array, |
7b33a0e0 | 582 | uint64_t *count) |
cd95e351 | 583 | { |
78cf9df6 | 584 | const struct bt_field_integer *int_field = (const void *) field; |
18acc6f8 | 585 | |
7b33a0e0 PP |
586 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
587 | BT_ASSERT_PRE_NON_NULL(label_array, "Label array (output)"); | |
588 | BT_ASSERT_PRE_NON_NULL(label_array, "Count (output)"); | |
589 | BT_ASSERT_PRE_FIELD_IS_SET(field, "Field"); | |
af0c18e3 PP |
590 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, |
591 | BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, "Field"); | |
939190b3 PP |
592 | return bt_field_class_signed_enumeration_get_mapping_labels_by_value( |
593 | field->class, int_field->value.i, label_array, count); | |
8b45963b | 594 | } |
fc25abce | 595 | |
78cf9df6 | 596 | const char *bt_field_string_get_value(const struct bt_field *field) |
8b45963b | 597 | { |
78cf9df6 | 598 | const struct bt_field_string *string_field = (const void *) field; |
7b33a0e0 PP |
599 | |
600 | BT_ASSERT_PRE_NON_NULL(field, "Field"); | |
601 | BT_ASSERT_PRE_FIELD_IS_SET(field, "Field"); | |
af0c18e3 | 602 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_STRING, |
7b33a0e0 PP |
603 | "Field"); |
604 | return (const char *) string_field->buf->data; | |
605 | } | |
606 | ||
78cf9df6 | 607 | uint64_t bt_field_string_get_length(const struct bt_field *field) |
7b33a0e0 | 608 | { |
78cf9df6 | 609 | const struct bt_field_string *string_field = (const void *) field; |
18acc6f8 | 610 | |
7b33a0e0 PP |
611 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
612 | BT_ASSERT_PRE_FIELD_IS_SET(field, "Field"); | |
af0c18e3 | 613 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_STRING, |
7b33a0e0 PP |
614 | "Field"); |
615 | return string_field->length; | |
cd95e351 JG |
616 | } |
617 | ||
78cf9df6 | 618 | int bt_field_string_set_value(struct bt_field *field, const char *value) |
273b65be | 619 | { |
7b33a0e0 | 620 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
18acc6f8 | 621 | BT_ASSERT_PRE_NON_NULL(value, "Value"); |
7b33a0e0 | 622 | BT_ASSERT_PRE_FIELD_HOT(field, "Field"); |
af0c18e3 | 623 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, BT_FIELD_CLASS_TYPE_STRING, |
7b33a0e0 | 624 | "Field"); |
78cf9df6 PP |
625 | bt_field_string_clear(field); |
626 | return bt_field_string_append_with_length(field, value, | |
7b33a0e0 | 627 | (uint64_t) strlen(value)); |
273b65be JG |
628 | } |
629 | ||
78cf9df6 | 630 | int bt_field_string_append(struct bt_field *field, const char *value) |
cd95e351 | 631 | { |
78cf9df6 | 632 | return bt_field_string_append_with_length(field, |
9e550e5f | 633 | value, (uint64_t) strlen(value)); |
cd95e351 JG |
634 | } |
635 | ||
78cf9df6 | 636 | int bt_field_string_append_with_length(struct bt_field *field, |
7b33a0e0 | 637 | const char *value, uint64_t length) |
273b65be | 638 | { |
18acc6f8 PP |
639 | struct bt_field_string *string_field = (void *) field; |
640 | char *data; | |
7b33a0e0 | 641 | uint64_t new_length; |
273b65be | 642 | |
7b33a0e0 | 643 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
18acc6f8 | 644 | BT_ASSERT_PRE_NON_NULL(value, "Value"); |
7b33a0e0 | 645 | BT_ASSERT_PRE_FIELD_HOT(field, "Field"); |
af0c18e3 PP |
646 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, |
647 | BT_FIELD_CLASS_TYPE_STRING, "Field"); | |
a6918753 | 648 | |
18acc6f8 PP |
649 | /* Make sure no null bytes are appended */ |
650 | BT_ASSERT_PRE(memchr(value, '\0', length) == NULL, | |
651 | "String value to append contains a null character: " | |
7b33a0e0 | 652 | "partial-value=\"%.32s\", length=%" PRIu64, value, length); |
c6f9c5a3 | 653 | |
7b33a0e0 | 654 | new_length = length + string_field->length; |
18acc6f8 | 655 | |
7b33a0e0 PP |
656 | if (unlikely(new_length + 1 > string_field->buf->len)) { |
657 | g_array_set_size(string_field->buf, new_length + 1); | |
c6f9c5a3 PP |
658 | } |
659 | ||
18acc6f8 | 660 | data = string_field->buf->data; |
7b33a0e0 PP |
661 | memcpy(data + string_field->length, value, length); |
662 | ((char *) string_field->buf->data)[new_length] = '\0'; | |
663 | string_field->length = new_length; | |
664 | bt_field_set_single(field, true); | |
18acc6f8 PP |
665 | return 0; |
666 | } | |
8deee039 | 667 | |
78cf9df6 | 668 | int bt_field_string_clear(struct bt_field *field) |
18acc6f8 PP |
669 | { |
670 | struct bt_field_string *string_field = (void *) field; | |
671 | ||
7b33a0e0 PP |
672 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
673 | BT_ASSERT_PRE_FIELD_HOT(field, "Field"); | |
af0c18e3 PP |
674 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, |
675 | BT_FIELD_CLASS_TYPE_STRING, "Field"); | |
7b33a0e0 PP |
676 | string_field->length = 0; |
677 | bt_field_set_single(field, true); | |
18acc6f8 PP |
678 | return 0; |
679 | } | |
680 | ||
78cf9df6 | 681 | uint64_t bt_field_array_get_length(const struct bt_field *field) |
18acc6f8 | 682 | { |
78cf9df6 | 683 | const struct bt_field_array *array_field = (const void *) field; |
c6f9c5a3 | 684 | |
7b33a0e0 PP |
685 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
686 | BT_ASSERT_PRE_FIELD_IS_ARRAY(field, "Field"); | |
687 | return array_field->length; | |
8deee039 | 688 | } |
f98c6554 | 689 | |
78cf9df6 | 690 | int bt_field_dynamic_array_set_length(struct bt_field *field, uint64_t length) |
8deee039 | 691 | { |
7b33a0e0 PP |
692 | int ret = 0; |
693 | struct bt_field_array *array_field = (void *) field; | |
f98c6554 | 694 | |
7b33a0e0 | 695 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
af0c18e3 PP |
696 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, |
697 | BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY, "Field"); | |
7b33a0e0 | 698 | BT_ASSERT_PRE_FIELD_HOT(field, "Field"); |
273b65be | 699 | |
7b33a0e0 PP |
700 | if (unlikely(length > array_field->fields->len)) { |
701 | /* Make more room */ | |
939190b3 | 702 | struct bt_field_class_array *array_fc; |
7b33a0e0 PP |
703 | uint64_t cur_len = array_field->fields->len; |
704 | uint64_t i; | |
18acc6f8 | 705 | |
7b33a0e0 | 706 | g_ptr_array_set_size(array_field->fields, length); |
939190b3 | 707 | array_fc = (void *) field->class; |
18acc6f8 | 708 | |
7b33a0e0 PP |
709 | for (i = cur_len; i < array_field->fields->len; i++) { |
710 | struct bt_field *elem_field = bt_field_create( | |
939190b3 | 711 | array_fc->element_fc); |
273b65be | 712 | |
7b33a0e0 PP |
713 | if (!elem_field) { |
714 | BT_LIB_LOGE("Cannot create element field for " | |
715 | "dynamic array field: " | |
716 | "index=%" PRIu64 ", " | |
717 | "%![array-field-]+f", i, field); | |
718 | ret = -1; | |
719 | goto end; | |
720 | } | |
9be89173 | 721 | |
7b33a0e0 PP |
722 | BT_ASSERT(!array_field->fields->pdata[i]); |
723 | array_field->fields->pdata[i] = elem_field; | |
9be89173 | 724 | } |
9be89173 JG |
725 | } |
726 | ||
7b33a0e0 | 727 | array_field->length = length; |
8deee039 | 728 | |
273b65be | 729 | end: |
9be89173 | 730 | return ret; |
273b65be JG |
731 | } |
732 | ||
78cf9df6 PP |
733 | static inline |
734 | struct bt_field *borrow_array_field_element_field_by_index( | |
7b33a0e0 | 735 | struct bt_field *field, uint64_t index) |
a6918753 | 736 | { |
7b33a0e0 | 737 | struct bt_field_array *array_field = (void *) field; |
a6918753 | 738 | |
7b33a0e0 PP |
739 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
740 | BT_ASSERT_PRE_FIELD_IS_ARRAY(field, "Field"); | |
741 | BT_ASSERT_PRE_VALID_INDEX(index, array_field->length); | |
742 | return array_field->fields->pdata[index]; | |
a6918753 PP |
743 | } |
744 | ||
78cf9df6 PP |
745 | struct bt_field *bt_field_array_borrow_element_field_by_index( |
746 | struct bt_field *field, uint64_t index) | |
9e550e5f | 747 | { |
78cf9df6 | 748 | return borrow_array_field_element_field_by_index(field, index); |
9e550e5f PP |
749 | } |
750 | ||
78cf9df6 PP |
751 | const struct bt_field * |
752 | bt_field_array_borrow_element_field_by_index_const( | |
753 | const struct bt_field *field, uint64_t index) | |
754 | { | |
755 | return borrow_array_field_element_field_by_index((void *) field, index); | |
756 | } | |
757 | ||
758 | static inline | |
759 | struct bt_field *borrow_structure_field_member_field_by_index( | |
7b33a0e0 | 760 | struct bt_field *field, uint64_t index) |
e42dd763 | 761 | { |
7b33a0e0 | 762 | struct bt_field_structure *struct_field = (void *) field; |
e42dd763 | 763 | |
7b33a0e0 | 764 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
af0c18e3 PP |
765 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, |
766 | BT_FIELD_CLASS_TYPE_STRUCTURE, "Field"); | |
7b33a0e0 PP |
767 | BT_ASSERT_PRE_VALID_INDEX(index, struct_field->fields->len); |
768 | return struct_field->fields->pdata[index]; | |
e42dd763 PP |
769 | } |
770 | ||
78cf9df6 PP |
771 | struct bt_field *bt_field_structure_borrow_member_field_by_index( |
772 | struct bt_field *field, uint64_t index) | |
773 | { | |
774 | return borrow_structure_field_member_field_by_index(field, | |
775 | index); | |
776 | } | |
777 | ||
778 | const struct bt_field * | |
779 | bt_field_structure_borrow_member_field_by_index_const( | |
780 | const struct bt_field *field, uint64_t index) | |
9e550e5f | 781 | { |
78cf9df6 | 782 | return borrow_structure_field_member_field_by_index( |
9e550e5f PP |
783 | (void *) field, index); |
784 | } | |
785 | ||
78cf9df6 PP |
786 | static inline |
787 | struct bt_field *borrow_structure_field_member_field_by_name( | |
7b33a0e0 | 788 | struct bt_field *field, const char *name) |
273b65be | 789 | { |
7b33a0e0 | 790 | struct bt_field *ret_field = NULL; |
939190b3 | 791 | struct bt_field_class_structure *struct_fc; |
7b33a0e0 PP |
792 | struct bt_field_structure *struct_field = (void *) field; |
793 | gpointer orig_key; | |
794 | gpointer index; | |
fc25abce | 795 | |
7b33a0e0 PP |
796 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
797 | BT_ASSERT_PRE_NON_NULL(name, "Field name"); | |
af0c18e3 PP |
798 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, |
799 | BT_FIELD_CLASS_TYPE_STRUCTURE, "Field"); | |
939190b3 | 800 | struct_fc = (void *) field->class; |
a6918753 | 801 | |
939190b3 | 802 | if (!g_hash_table_lookup_extended(struct_fc->common.name_to_index, name, |
7b33a0e0 | 803 | &orig_key, &index)) { |
a6918753 | 804 | goto end; |
fc25abce PP |
805 | } |
806 | ||
7b33a0e0 PP |
807 | ret_field = struct_field->fields->pdata[GPOINTER_TO_UINT(index)]; |
808 | BT_ASSERT(ret_field); | |
a6918753 PP |
809 | |
810 | end: | |
7b33a0e0 | 811 | return ret_field; |
273b65be JG |
812 | } |
813 | ||
78cf9df6 PP |
814 | struct bt_field *bt_field_structure_borrow_member_field_by_name( |
815 | struct bt_field *field, const char *name) | |
816 | { | |
817 | return borrow_structure_field_member_field_by_name(field, name); | |
818 | } | |
819 | ||
820 | const struct bt_field *bt_field_structure_borrow_member_field_by_name_const( | |
821 | const struct bt_field *field, const char *name) | |
9e550e5f | 822 | { |
78cf9df6 | 823 | return borrow_structure_field_member_field_by_name( |
9e550e5f PP |
824 | (void *) field, name); |
825 | } | |
826 | ||
78cf9df6 PP |
827 | static inline |
828 | struct bt_field *borrow_variant_field_selected_option_field( | |
7b33a0e0 | 829 | struct bt_field *field) |
273b65be | 830 | { |
7b33a0e0 | 831 | struct bt_field_variant *var_field = (void *) field; |
273b65be | 832 | |
7b33a0e0 | 833 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
af0c18e3 PP |
834 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, |
835 | BT_FIELD_CLASS_TYPE_VARIANT, "Field"); | |
7b33a0e0 PP |
836 | BT_ASSERT_PRE(var_field->selected_field, |
837 | "Variant field has no selected field: %!+f", field); | |
838 | return var_field->selected_field; | |
273b65be JG |
839 | } |
840 | ||
78cf9df6 PP |
841 | struct bt_field *bt_field_variant_borrow_selected_option_field( |
842 | struct bt_field *field) | |
843 | { | |
844 | return borrow_variant_field_selected_option_field(field); | |
845 | } | |
846 | ||
847 | const struct bt_field *bt_field_variant_borrow_selected_option_field_const( | |
848 | const struct bt_field *field) | |
273b65be | 849 | { |
78cf9df6 | 850 | return borrow_variant_field_selected_option_field((void *) field); |
9e550e5f PP |
851 | } |
852 | ||
78cf9df6 PP |
853 | int bt_field_variant_select_option_field( |
854 | struct bt_field *field, uint64_t index) | |
9e550e5f | 855 | { |
7b33a0e0 | 856 | struct bt_field_variant *var_field = (void *) field; |
fc25abce | 857 | |
7b33a0e0 | 858 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
af0c18e3 PP |
859 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, |
860 | BT_FIELD_CLASS_TYPE_VARIANT, "Field"); | |
7b33a0e0 PP |
861 | BT_ASSERT_PRE_FIELD_HOT(field, "Field"); |
862 | BT_ASSERT_PRE_VALID_INDEX(index, var_field->fields->len); | |
863 | var_field->selected_field = var_field->fields->pdata[index]; | |
864 | var_field->selected_index = index; | |
865 | return 0; | |
273b65be JG |
866 | } |
867 | ||
7b33a0e0 | 868 | uint64_t bt_field_variant_get_selected_option_field_index( |
78cf9df6 | 869 | const struct bt_field *field) |
a6918753 | 870 | { |
78cf9df6 | 871 | const struct bt_field_variant *var_field = (const void *) field; |
a6918753 | 872 | |
7b33a0e0 | 873 | BT_ASSERT_PRE_NON_NULL(field, "Field"); |
af0c18e3 PP |
874 | BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(field, |
875 | BT_FIELD_CLASS_TYPE_VARIANT, "Field"); | |
7b33a0e0 PP |
876 | BT_ASSERT_PRE(var_field->selected_field, |
877 | "Variant field has no selected field: %!+f", field); | |
878 | return var_field->selected_index; | |
a6918753 PP |
879 | } |
880 | ||
7b33a0e0 PP |
881 | static inline |
882 | void bt_field_finalize(struct bt_field *field) | |
273b65be | 883 | { |
7b33a0e0 | 884 | BT_ASSERT(field); |
939190b3 | 885 | BT_LOGD_STR("Putting field's class."); |
1248f5ea | 886 | BT_OBJECT_PUT_REF_AND_RESET(field->class); |
273b65be JG |
887 | } |
888 | ||
889 | static | |
7b33a0e0 | 890 | void destroy_integer_field(struct bt_field *field) |
273b65be | 891 | { |
7b33a0e0 PP |
892 | BT_ASSERT(field); |
893 | BT_LIB_LOGD("Destroying integer field object: %!+f", field); | |
894 | bt_field_finalize(field); | |
895 | g_free(field); | |
273b65be JG |
896 | } |
897 | ||
18acc6f8 | 898 | static |
7b33a0e0 | 899 | void destroy_real_field(struct bt_field *field) |
273b65be | 900 | { |
7b33a0e0 PP |
901 | BT_ASSERT(field); |
902 | BT_LIB_LOGD("Destroying real field object: %!+f", field); | |
903 | bt_field_finalize(field); | |
904 | g_free(field); | |
273b65be JG |
905 | } |
906 | ||
18acc6f8 | 907 | static |
7b33a0e0 | 908 | void destroy_structure_field(struct bt_field *field) |
273b65be | 909 | { |
7b33a0e0 | 910 | struct bt_field_structure *struct_field = (void *) field; |
273b65be | 911 | |
8b45963b | 912 | BT_ASSERT(field); |
7b33a0e0 PP |
913 | BT_LIB_LOGD("Destroying structure field object: %!+f", field); |
914 | bt_field_finalize(field); | |
8b45963b | 915 | |
7b33a0e0 PP |
916 | if (struct_field->fields) { |
917 | g_ptr_array_free(struct_field->fields, TRUE); | |
1248f5ea | 918 | struct_field->fields = NULL; |
273b65be | 919 | } |
8b45963b | 920 | |
7b33a0e0 | 921 | g_free(field); |
273b65be JG |
922 | } |
923 | ||
18acc6f8 | 924 | static |
7b33a0e0 | 925 | void destroy_variant_field(struct bt_field *field) |
273b65be | 926 | { |
7b33a0e0 | 927 | struct bt_field_variant *var_field = (void *) field; |
273b65be | 928 | |
8b45963b | 929 | BT_ASSERT(field); |
7b33a0e0 PP |
930 | BT_LIB_LOGD("Destroying variant field object: %!+f", field); |
931 | bt_field_finalize(field); | |
a6918753 | 932 | |
7b33a0e0 PP |
933 | if (var_field->fields) { |
934 | g_ptr_array_free(var_field->fields, TRUE); | |
1248f5ea | 935 | var_field->fields = NULL; |
fc25abce | 936 | } |
8b45963b | 937 | |
7b33a0e0 | 938 | g_free(field); |
273b65be JG |
939 | } |
940 | ||
18acc6f8 | 941 | static |
7b33a0e0 | 942 | void destroy_array_field(struct bt_field *field) |
273b65be | 943 | { |
7b33a0e0 | 944 | struct bt_field_array *array_field = (void *) field; |
273b65be | 945 | |
8b45963b | 946 | BT_ASSERT(field); |
7b33a0e0 PP |
947 | BT_LIB_LOGD("Destroying array field object: %!+f", field); |
948 | bt_field_finalize(field); | |
8deee039 | 949 | |
7b33a0e0 PP |
950 | if (array_field->fields) { |
951 | g_ptr_array_free(array_field->fields, TRUE); | |
1248f5ea | 952 | array_field->fields = NULL; |
273b65be | 953 | } |
8b45963b | 954 | |
7b33a0e0 | 955 | g_free(field); |
273b65be JG |
956 | } |
957 | ||
18acc6f8 | 958 | static |
7b33a0e0 | 959 | void destroy_string_field(struct bt_field *field) |
273b65be | 960 | { |
7b33a0e0 | 961 | struct bt_field_string *string_field = (void *) field; |
273b65be | 962 | |
8b45963b | 963 | BT_ASSERT(field); |
7b33a0e0 PP |
964 | BT_LIB_LOGD("Destroying string field object: %!+f", field); |
965 | bt_field_finalize(field); | |
8deee039 | 966 | |
7b33a0e0 PP |
967 | if (string_field->buf) { |
968 | g_array_free(string_field->buf, TRUE); | |
1248f5ea | 969 | string_field->buf = NULL; |
273b65be | 970 | } |
7b33a0e0 PP |
971 | |
972 | g_free(field); | |
273b65be JG |
973 | } |
974 | ||
7b33a0e0 PP |
975 | BT_HIDDEN |
976 | void bt_field_destroy(struct bt_field *field) | |
12c8a1a3 | 977 | { |
8b45963b | 978 | BT_ASSERT(field); |
af0c18e3 PP |
979 | BT_ASSERT(bt_field_class_has_known_type(field->class)); |
980 | field_destroy_funcs[field->class->type](field); | |
12c8a1a3 JG |
981 | } |
982 | ||
18acc6f8 | 983 | static |
7b33a0e0 | 984 | void reset_single_field(struct bt_field *field) |
12c8a1a3 | 985 | { |
8b45963b | 986 | BT_ASSERT(field); |
7b33a0e0 | 987 | field->is_set = false; |
12c8a1a3 JG |
988 | } |
989 | ||
18acc6f8 | 990 | static |
7b33a0e0 | 991 | void reset_structure_field(struct bt_field *field) |
12c8a1a3 | 992 | { |
7b33a0e0 PP |
993 | uint64_t i; |
994 | struct bt_field_structure *struct_field = (void *) field; | |
12c8a1a3 | 995 | |
8b45963b | 996 | BT_ASSERT(field); |
7b33a0e0 PP |
997 | |
998 | for (i = 0; i < struct_field->fields->len; i++) { | |
999 | bt_field_reset(struct_field->fields->pdata[i]); | |
1000 | } | |
12c8a1a3 JG |
1001 | } |
1002 | ||
18acc6f8 | 1003 | static |
7b33a0e0 | 1004 | void reset_variant_field(struct bt_field *field) |
12c8a1a3 | 1005 | { |
7b33a0e0 PP |
1006 | uint64_t i; |
1007 | struct bt_field_variant *var_field = (void *) field; | |
12c8a1a3 | 1008 | |
8b45963b | 1009 | BT_ASSERT(field); |
8b45963b | 1010 | |
7b33a0e0 PP |
1011 | for (i = 0; i < var_field->fields->len; i++) { |
1012 | bt_field_reset(var_field->fields->pdata[i]); | |
12c8a1a3 | 1013 | } |
12c8a1a3 JG |
1014 | } |
1015 | ||
18acc6f8 | 1016 | static |
7b33a0e0 | 1017 | void reset_array_field(struct bt_field *field) |
12c8a1a3 | 1018 | { |
a6918753 | 1019 | uint64_t i; |
7b33a0e0 | 1020 | struct bt_field_array *array_field = (void *) field; |
12c8a1a3 | 1021 | |
8b45963b | 1022 | BT_ASSERT(field); |
8b45963b | 1023 | |
7b33a0e0 PP |
1024 | for (i = 0; i < array_field->fields->len; i++) { |
1025 | bt_field_reset(array_field->fields->pdata[i]); | |
12c8a1a3 | 1026 | } |
12c8a1a3 JG |
1027 | } |
1028 | ||
18acc6f8 | 1029 | static |
7b33a0e0 | 1030 | void set_single_field_is_frozen(struct bt_field *field, bool is_frozen) |
918be005 | 1031 | { |
a6918753 | 1032 | field->frozen = is_frozen; |
918be005 PP |
1033 | } |
1034 | ||
18acc6f8 | 1035 | static |
7b33a0e0 | 1036 | void set_structure_field_is_frozen(struct bt_field *field, bool is_frozen) |
918be005 | 1037 | { |
a6918753 | 1038 | uint64_t i; |
7b33a0e0 | 1039 | struct bt_field_structure *struct_field = (void *) field; |
918be005 | 1040 | |
7b33a0e0 PP |
1041 | BT_LIB_LOGD("Setting structure field's frozen state: " |
1042 | "%![field-]+f, is-frozen=%d", field, is_frozen); | |
fc25abce | 1043 | |
7b33a0e0 PP |
1044 | for (i = 0; i < struct_field->fields->len; i++) { |
1045 | struct bt_field *member_field = struct_field->fields->pdata[i]; | |
918be005 | 1046 | |
7b33a0e0 PP |
1047 | BT_LIB_LOGD("Setting structure field's member field's " |
1048 | "frozen state: %![field-]+f, index=%" PRIu64, | |
1049 | member_field, i); | |
1050 | bt_field_set_is_frozen(member_field, is_frozen); | |
918be005 PP |
1051 | } |
1052 | ||
7b33a0e0 | 1053 | set_single_field_is_frozen(field, is_frozen); |
918be005 PP |
1054 | } |
1055 | ||
18acc6f8 | 1056 | static |
7b33a0e0 | 1057 | void set_variant_field_is_frozen(struct bt_field *field, bool is_frozen) |
918be005 | 1058 | { |
a6918753 | 1059 | uint64_t i; |
7b33a0e0 | 1060 | struct bt_field_variant *var_field = (void *) field; |
918be005 | 1061 | |
7b33a0e0 PP |
1062 | BT_LIB_LOGD("Setting variant field's frozen state: " |
1063 | "%![field-]+f, is-frozen=%d", field, is_frozen); | |
a6918753 | 1064 | |
7b33a0e0 PP |
1065 | for (i = 0; i < var_field->fields->len; i++) { |
1066 | struct bt_field *option_field = var_field->fields->pdata[i]; | |
a6918753 | 1067 | |
7b33a0e0 PP |
1068 | BT_LIB_LOGD("Setting variant field's option field's " |
1069 | "frozen state: %![field-]+f, index=%" PRIu64, | |
1070 | option_field, i); | |
1071 | bt_field_set_is_frozen(option_field, is_frozen); | |
a6918753 PP |
1072 | } |
1073 | ||
7b33a0e0 | 1074 | set_single_field_is_frozen(field, is_frozen); |
918be005 PP |
1075 | } |
1076 | ||
18acc6f8 | 1077 | static |
7b33a0e0 | 1078 | void set_array_field_is_frozen(struct bt_field *field, bool is_frozen) |
918be005 | 1079 | { |
7b33a0e0 | 1080 | uint64_t i; |
18acc6f8 | 1081 | struct bt_field_array *array_field = (void *) field; |
918be005 | 1082 | |
7b33a0e0 PP |
1083 | BT_LIB_LOGD("Setting array field's frozen state: " |
1084 | "%![field-]+f, is-frozen=%d", field, is_frozen); | |
fc25abce | 1085 | |
7b33a0e0 PP |
1086 | for (i = 0; i < array_field->fields->len; i++) { |
1087 | struct bt_field *elem_field = array_field->fields->pdata[i]; | |
918be005 | 1088 | |
7b33a0e0 PP |
1089 | BT_LIB_LOGD("Setting array field's element field's " |
1090 | "frozen state: %![field-]+f, index=%" PRIu64, | |
fc25abce | 1091 | elem_field, i); |
7b33a0e0 | 1092 | bt_field_set_is_frozen(elem_field, is_frozen); |
918be005 PP |
1093 | } |
1094 | ||
7b33a0e0 | 1095 | set_single_field_is_frozen(field, is_frozen); |
918be005 PP |
1096 | } |
1097 | ||
1098 | BT_HIDDEN | |
78cf9df6 | 1099 | void _bt_field_set_is_frozen(const struct bt_field *field, |
a6918753 | 1100 | bool is_frozen) |
918be005 | 1101 | { |
7b33a0e0 PP |
1102 | BT_ASSERT(field); |
1103 | BT_LIB_LOGD("Setting field object's frozen state: %!+f, is-frozen=%d", | |
a6918753 | 1104 | field, is_frozen); |
a6918753 | 1105 | BT_ASSERT(field->methods->set_is_frozen); |
78cf9df6 | 1106 | field->methods->set_is_frozen((void *) field, is_frozen); |
918be005 | 1107 | } |
76f869ab | 1108 | |
18acc6f8 | 1109 | static |
78cf9df6 | 1110 | bool single_field_is_set(const struct bt_field *field) |
76f869ab | 1111 | { |
7b33a0e0 PP |
1112 | BT_ASSERT(field); |
1113 | return field->is_set; | |
76f869ab JG |
1114 | } |
1115 | ||
18acc6f8 | 1116 | static |
78cf9df6 | 1117 | bool structure_field_is_set(const struct bt_field *field) |
76f869ab | 1118 | { |
7b33a0e0 PP |
1119 | bool is_set = true; |
1120 | uint64_t i; | |
78cf9df6 | 1121 | const struct bt_field_structure *struct_field = (const void *) field; |
76f869ab | 1122 | |
8b45963b | 1123 | BT_ASSERT(field); |
8deee039 | 1124 | |
7b33a0e0 PP |
1125 | for (i = 0; i < struct_field->fields->len; i++) { |
1126 | is_set = bt_field_is_set(struct_field->fields->pdata[i]); | |
53d65c1d | 1127 | if (!is_set) { |
76f869ab JG |
1128 | goto end; |
1129 | } | |
1130 | } | |
8deee039 | 1131 | |
76f869ab | 1132 | end: |
53d65c1d | 1133 | return is_set; |
76f869ab JG |
1134 | } |
1135 | ||
18acc6f8 | 1136 | static |
78cf9df6 | 1137 | bool variant_field_is_set(const struct bt_field *field) |
76f869ab | 1138 | { |
78cf9df6 | 1139 | const struct bt_field_variant *var_field = (const void *) field; |
7b33a0e0 | 1140 | bool is_set = false; |
76f869ab | 1141 | |
8b45963b | 1142 | BT_ASSERT(field); |
8deee039 | 1143 | |
7b33a0e0 PP |
1144 | if (var_field->selected_field) { |
1145 | is_set = bt_field_is_set(var_field->selected_field); | |
76f869ab | 1146 | } |
8deee039 | 1147 | |
53d65c1d | 1148 | return is_set; |
76f869ab JG |
1149 | } |
1150 | ||
18acc6f8 | 1151 | static |
78cf9df6 | 1152 | bool array_field_is_set(const struct bt_field *field) |
76f869ab | 1153 | { |
7b33a0e0 PP |
1154 | bool is_set = true; |
1155 | uint64_t i; | |
78cf9df6 | 1156 | const struct bt_field_array *array_field = (const void *) field; |
76f869ab | 1157 | |
8b45963b | 1158 | BT_ASSERT(field); |
8deee039 | 1159 | |
7b33a0e0 PP |
1160 | for (i = 0; i < array_field->length; i++) { |
1161 | is_set = bt_field_is_set(array_field->fields->pdata[i]); | |
53d65c1d | 1162 | if (!is_set) { |
76f869ab JG |
1163 | goto end; |
1164 | } | |
1165 | } | |
8deee039 | 1166 | |
76f869ab | 1167 | end: |
53d65c1d | 1168 | return is_set; |
76f869ab | 1169 | } |