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