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