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