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