Cleanup: lib: field-class.c: fix logging message for variant FC
[babeltrace.git] / src / lib / trace-ir / field-class.c
CommitLineData
939190b3 1/*
f2b0325d 2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
939190b3
PP
3 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
939190b3
PP
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-CLASS"
1633ef46 25#include "lib/logging.h"
939190b3 26
57952005 27#include "lib/assert-pre.h"
71c5da58
MJ
28#include <babeltrace2/trace-ir/field-class.h>
29#include <babeltrace2/trace-ir/field-class-const.h>
71c5da58
MJ
30#include <babeltrace2/trace-ir/field-const.h>
31#include <babeltrace2/trace-ir/field.h>
71c5da58 32#include <babeltrace2/trace-ir/clock-class.h>
57952005
MJ
33#include "lib/object.h"
34#include "compat/compiler.h"
35#include "compat/endian.h"
36#include "common/assert.h"
37#include "compat/glib.h"
939190b3
PP
38#include <float.h>
39#include <inttypes.h>
40#include <stdlib.h>
41
57952005
MJ
42#include "clock-class.h"
43#include "field-class.h"
44#include "field.h"
45#include "field-path.h"
46#include "utils.h"
fb25b9e3 47#include "lib/func-status.h"
02b61fe0 48#include "lib/integer-range-set.h"
af90138b 49#include "lib/value.h"
57952005 50
78cf9df6
PP
51enum bt_field_class_type bt_field_class_get_type(
52 const struct bt_field_class *fc)
939190b3 53{
fa6cfec3 54 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
af0c18e3 55 return fc->type;
939190b3
PP
56}
57
58static
af90138b 59int init_field_class(struct bt_field_class *fc, enum bt_field_class_type type,
939190b3
PP
60 bt_object_release_func release_func)
61{
af90138b
PP
62 int ret = 0;
63
939190b3 64 BT_ASSERT(fc);
939190b3
PP
65 BT_ASSERT(release_func);
66 bt_object_init_shared(&fc->base, release_func);
af0c18e3 67 fc->type = type;
af90138b
PP
68 fc->user_attributes = bt_value_map_create();
69 if (!fc->user_attributes) {
70 BT_LIB_LOGE_APPEND_CAUSE(
71 "Failed to create a map value object.");
72 ret = -1;
73 goto end;
74 }
75
76end:
77 return ret;
78}
79
80static
81void finalize_field_class(struct bt_field_class *fc)
82{
83 BT_OBJECT_PUT_REF_AND_RESET(fc->user_attributes);
939190b3
PP
84}
85
6943da6a
PP
86static
87void destroy_bit_array_field_class(struct bt_object *obj)
88{
89 BT_ASSERT(obj);
90 BT_LIB_LOGD("Destroying bit array field class object: %!+F", obj);
af90138b 91 finalize_field_class((void *) obj);
6943da6a
PP
92 g_free(obj);
93}
94
95struct bt_field_class *bt_field_class_bit_array_create(
96 struct bt_trace_class *trace_class, uint64_t length)
97{
98 struct bt_field_class_bit_array *ba_fc = NULL;
99
100 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
101 BT_ASSERT_PRE(length > 0 && length <= 64,
102 "Unsupported length for bit array field class "
103 "(minimum is 1, maximum is 64): length=%" PRIu64, length);
104 BT_LOGD("Creating default bit array field class object.");
105 ba_fc = g_new0(struct bt_field_class_bit_array, 1);
106 if (!ba_fc) {
107 BT_LIB_LOGE_APPEND_CAUSE(
108 "Failed to allocate one bit array field class.");
109 goto error;
110 }
111
af90138b
PP
112 if (init_field_class((void *) ba_fc, BT_FIELD_CLASS_TYPE_BIT_ARRAY,
113 destroy_bit_array_field_class)) {
114 goto error;
115 }
116
6943da6a
PP
117 ba_fc->length = length;
118 BT_LIB_LOGD("Created bit array field class object: %!+F", ba_fc);
119 goto end;
120
121error:
122 BT_OBJECT_PUT_REF_AND_RESET(ba_fc);
123
124end:
125 return (void *) ba_fc;
126}
127
128uint64_t bt_field_class_bit_array_get_length(const struct bt_field_class *fc)
129{
130 const struct bt_field_class_bit_array *ba_fc = (const void *) fc;
131
132 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
133 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_BIT_ARRAY,
134 "Field class");
135 return ba_fc->length;
136}
137
9414b439
PP
138static
139void destroy_bool_field_class(struct bt_object *obj)
140{
141 BT_ASSERT(obj);
142 BT_LIB_LOGD("Destroying boolean field class object: %!+F", obj);
af90138b 143 finalize_field_class((void *) obj);
9414b439
PP
144 g_free(obj);
145}
146
147struct bt_field_class *bt_field_class_bool_create(
148 bt_trace_class *trace_class)
149{
150 struct bt_field_class_bool *bool_fc = NULL;
151
152 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
153 BT_LOGD("Creating default boolean field class object.");
154 bool_fc = g_new0(struct bt_field_class_bool, 1);
155 if (!bool_fc) {
156 BT_LIB_LOGE_APPEND_CAUSE(
157 "Failed to allocate one boolean field class.");
158 goto error;
159 }
160
af90138b
PP
161 if (init_field_class((void *) bool_fc, BT_FIELD_CLASS_TYPE_BOOL,
162 destroy_bool_field_class)) {
163 goto error;
164 }
165
9414b439
PP
166 BT_LIB_LOGD("Created boolean field class object: %!+F", bool_fc);
167 goto end;
168
169error:
170 BT_OBJECT_PUT_REF_AND_RESET(bool_fc);
171
172end:
173 return (void *) bool_fc;
174}
175
939190b3 176static
af90138b 177int init_integer_field_class(struct bt_field_class_integer *fc,
af0c18e3 178 enum bt_field_class_type type,
939190b3
PP
179 bt_object_release_func release_func)
180{
af90138b
PP
181 int ret;
182
183 ret = init_field_class((void *) fc, type, release_func);
184 if (ret) {
185 goto end;
186 }
187
939190b3
PP
188 fc->range = 64;
189 fc->base = BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL;
af90138b
PP
190
191end:
192 return ret;
939190b3
PP
193}
194
195static
196void destroy_integer_field_class(struct bt_object *obj)
197{
198 BT_ASSERT(obj);
9e550e5f 199 BT_LIB_LOGD("Destroying integer field class object: %!+F", obj);
af90138b 200 finalize_field_class((void *) obj);
939190b3
PP
201 g_free(obj);
202}
203
204static inline
b7396828
PP
205struct bt_field_class *create_integer_field_class(bt_trace_class *trace_class,
206 enum bt_field_class_type type)
939190b3
PP
207{
208 struct bt_field_class_integer *int_fc = NULL;
209
b7396828 210 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
9e550e5f 211 BT_LOGD("Creating default integer field class object: type=%s",
af0c18e3 212 bt_common_field_class_type_string(type));
939190b3
PP
213 int_fc = g_new0(struct bt_field_class_integer, 1);
214 if (!int_fc) {
a8f90e5d
PP
215 BT_LIB_LOGE_APPEND_CAUSE(
216 "Failed to allocate one integer field class.");
939190b3
PP
217 goto error;
218 }
219
af90138b
PP
220 if (init_integer_field_class(int_fc, type,
221 destroy_integer_field_class)) {
222 goto error;
223 }
224
9e550e5f 225 BT_LIB_LOGD("Created integer field class object: %!+F", int_fc);
939190b3
PP
226 goto end;
227
228error:
8138bfe1 229 BT_OBJECT_PUT_REF_AND_RESET(int_fc);
939190b3
PP
230
231end:
232 return (void *) int_fc;
233}
234
60bbfc7c 235struct bt_field_class *bt_field_class_integer_unsigned_create(
b7396828 236 bt_trace_class *trace_class)
939190b3 237{
b7396828 238 return create_integer_field_class(trace_class,
9e550e5f 239 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER);
939190b3
PP
240}
241
60bbfc7c 242struct bt_field_class *bt_field_class_integer_signed_create(
b7396828 243 bt_trace_class *trace_class)
939190b3 244{
b7396828 245 return create_integer_field_class(trace_class,
9e550e5f 246 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER);
939190b3
PP
247}
248
249uint64_t bt_field_class_integer_get_field_value_range(
78cf9df6 250 const struct bt_field_class *fc)
939190b3 251{
78cf9df6 252 const struct bt_field_class_integer *int_fc = (const void *) fc;
939190b3 253
fa6cfec3
PP
254 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
255 BT_ASSERT_PRE_DEV_FC_IS_INT(fc, "Field class");
939190b3
PP
256 return int_fc->range;
257}
258
939190b3
PP
259static
260bool size_is_valid_for_enumeration_field_class(struct bt_field_class *fc,
261 uint64_t size)
262{
263 // TODO
264 return true;
265}
266
78cf9df6
PP
267void bt_field_class_integer_set_field_value_range(
268 struct bt_field_class *fc, uint64_t size)
939190b3
PP
269{
270 struct bt_field_class_integer *int_fc = (void *) fc;
271
272 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
273 BT_ASSERT_PRE_FC_IS_INT(fc, "Field class");
fa6cfec3 274 BT_ASSERT_PRE_DEV_FC_HOT(fc, "Field class");
939190b3 275 BT_ASSERT_PRE(size <= 64,
9e550e5f 276 "Unsupported size for integer field class's field value range "
939190b3 277 "(maximum is 64): size=%" PRIu64, size);
af0c18e3
PP
278 BT_ASSERT_PRE(
279 int_fc->common.type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER ||
280 int_fc->common.type == BT_FIELD_CLASS_TYPE_SIGNED_INTEGER ||
939190b3 281 size_is_valid_for_enumeration_field_class(fc, size),
9e550e5f 282 "Invalid field value range for enumeration field class: "
939190b3
PP
283 "at least one of the current mapping ranges contains values "
284 "which are outside this range: %!+F, size=%" PRIu64, fc, size);
285 int_fc->range = size;
a684a357 286 BT_LIB_LOGD("Set integer field class's field value range: %!+F", fc);
939190b3
PP
287}
288
289enum bt_field_class_integer_preferred_display_base
78cf9df6 290bt_field_class_integer_get_preferred_display_base(const struct bt_field_class *fc)
939190b3 291{
78cf9df6 292 const struct bt_field_class_integer *int_fc = (const void *) fc;
939190b3 293
fa6cfec3
PP
294 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
295 BT_ASSERT_PRE_DEV_FC_IS_INT(fc, "Field class");
939190b3
PP
296 return int_fc->base;
297}
298
78cf9df6
PP
299void bt_field_class_integer_set_preferred_display_base(
300 struct bt_field_class *fc,
939190b3
PP
301 enum bt_field_class_integer_preferred_display_base base)
302{
303 struct bt_field_class_integer *int_fc = (void *) fc;
304
305 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
306 BT_ASSERT_PRE_FC_IS_INT(fc, "Field class");
fa6cfec3 307 BT_ASSERT_PRE_DEV_FC_HOT(fc, "Field class");
939190b3 308 int_fc->base = base;
a684a357 309 BT_LIB_LOGD("Set integer field class's preferred display base: %!+F", fc);
939190b3
PP
310}
311
312static
313void finalize_enumeration_field_class_mapping(
314 struct bt_field_class_enumeration_mapping *mapping)
315{
316 BT_ASSERT(mapping);
317
318 if (mapping->label) {
319 g_string_free(mapping->label, TRUE);
02b61fe0 320 mapping->label = NULL;
939190b3
PP
321 }
322
02b61fe0 323 BT_OBJECT_PUT_REF_AND_RESET(mapping->range_set);
939190b3
PP
324}
325
326static
327void destroy_enumeration_field_class(struct bt_object *obj)
328{
329 struct bt_field_class_enumeration *fc = (void *) obj;
330
331 BT_ASSERT(fc);
9e550e5f 332 BT_LIB_LOGD("Destroying enumeration field class object: %!+F", fc);
af90138b 333 finalize_field_class((void *) obj);
939190b3
PP
334
335 if (fc->mappings) {
336 uint64_t i;
337
338 for (i = 0; i < fc->mappings->len; i++) {
339 finalize_enumeration_field_class_mapping(
340 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, i));
341 }
342
343 g_array_free(fc->mappings, TRUE);
1248f5ea 344 fc->mappings = NULL;
939190b3
PP
345 }
346
347 if (fc->label_buf) {
348 g_ptr_array_free(fc->label_buf, TRUE);
1248f5ea 349 fc->label_buf = NULL;
939190b3
PP
350 }
351
352 g_free(fc);
353}
354
355static
78cf9df6 356struct bt_field_class *create_enumeration_field_class(
b7396828 357 bt_trace_class *trace_class, enum bt_field_class_type type)
939190b3
PP
358{
359 struct bt_field_class_enumeration *enum_fc = NULL;
360
b7396828 361 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
9e550e5f 362 BT_LOGD("Creating default enumeration field class object: type=%s",
af0c18e3 363 bt_common_field_class_type_string(type));
939190b3
PP
364 enum_fc = g_new0(struct bt_field_class_enumeration, 1);
365 if (!enum_fc) {
a8f90e5d
PP
366 BT_LIB_LOGE_APPEND_CAUSE(
367 "Failed to allocate one enumeration field class.");
939190b3
PP
368 goto error;
369 }
370
af90138b
PP
371 if (init_integer_field_class((void *) enum_fc, type,
372 destroy_enumeration_field_class)) {
373 goto error;
374 }
375
939190b3
PP
376 enum_fc->mappings = g_array_new(FALSE, TRUE,
377 sizeof(struct bt_field_class_enumeration_mapping));
378 if (!enum_fc->mappings) {
a8f90e5d 379 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray.");
939190b3
PP
380 goto error;
381 }
382
383 enum_fc->label_buf = g_ptr_array_new();
384 if (!enum_fc->label_buf) {
a8f90e5d 385 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray.");
939190b3
PP
386 goto error;
387 }
388
9e550e5f 389 BT_LIB_LOGD("Created enumeration field class object: %!+F", enum_fc);
939190b3
PP
390 goto end;
391
392error:
8138bfe1 393 BT_OBJECT_PUT_REF_AND_RESET(enum_fc);
939190b3
PP
394
395end:
396 return (void *) enum_fc;
397}
398
60bbfc7c 399struct bt_field_class *bt_field_class_enumeration_unsigned_create(
b7396828 400 bt_trace_class *trace_class)
939190b3 401{
b7396828 402 return create_enumeration_field_class(trace_class,
af0c18e3 403 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION);
939190b3
PP
404}
405
60bbfc7c 406struct bt_field_class *bt_field_class_enumeration_signed_create(
b7396828 407 bt_trace_class *trace_class)
939190b3 408{
b7396828 409 return create_enumeration_field_class(trace_class,
af0c18e3 410 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION);
939190b3
PP
411}
412
78cf9df6
PP
413uint64_t bt_field_class_enumeration_get_mapping_count(
414 const struct bt_field_class *fc)
939190b3 415{
78cf9df6 416 const struct bt_field_class_enumeration *enum_fc = (const void *) fc;
939190b3 417
fa6cfec3
PP
418 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
419 BT_ASSERT_PRE_DEV_FC_IS_ENUM(fc, "Field class");
939190b3
PP
420 return (uint64_t) enum_fc->mappings->len;
421}
422
60bbfc7c
PP
423const struct bt_field_class_enumeration_unsigned_mapping *
424bt_field_class_enumeration_unsigned_borrow_mapping_by_index_const(
b6baf3bb 425 const struct bt_field_class *fc, uint64_t index)
939190b3 426{
78cf9df6 427 const struct bt_field_class_enumeration *enum_fc = (const void *) fc;
939190b3 428
fa6cfec3
PP
429 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
430 BT_ASSERT_PRE_DEV_VALID_INDEX(index, enum_fc->mappings->len);
431 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION,
939190b3 432 "Field class");
b6baf3bb 433 return (const void *) BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, index);
939190b3
PP
434}
435
60bbfc7c
PP
436const struct bt_field_class_enumeration_signed_mapping *
437bt_field_class_enumeration_signed_borrow_mapping_by_index_const(
b6baf3bb 438 const struct bt_field_class *fc, uint64_t index)
939190b3 439{
78cf9df6 440 const struct bt_field_class_enumeration *enum_fc = (const void *) fc;
939190b3 441
fa6cfec3
PP
442 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
443 BT_ASSERT_PRE_DEV_VALID_INDEX(index, enum_fc->mappings->len);
444 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION,
939190b3 445 "Field class");
b6baf3bb 446 return (const void *) BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, index);
939190b3
PP
447}
448
02b61fe0
PP
449static
450const struct bt_field_class_enumeration_mapping *
451borrow_enumeration_field_class_mapping_by_label(
452 const struct bt_field_class_enumeration *fc, const char *label)
939190b3 453{
02b61fe0
PP
454 struct bt_field_class_enumeration_mapping *mapping = NULL;
455 uint64_t i;
456
457 BT_ASSERT(fc);
458 BT_ASSERT_PRE_DEV_NON_NULL(label, "Label");
459
460 for (i = 0; i < fc->mappings->len; i++) {
461 struct bt_field_class_enumeration_mapping *this_mapping =
462 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, i);
463
464 if (strcmp(this_mapping->label->str, label) == 0) {
465 mapping = this_mapping;
466 goto end;
467 }
468 }
469
470end:
471 return mapping;
939190b3
PP
472}
473
60bbfc7c
PP
474const struct bt_field_class_enumeration_signed_mapping *
475bt_field_class_enumeration_signed_borrow_mapping_by_label_const(
02b61fe0 476 const struct bt_field_class *fc, const char *label)
939190b3 477{
02b61fe0
PP
478 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
479 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION,
480 "Field class");
481 return (const void *) borrow_enumeration_field_class_mapping_by_label(
482 (const void *) fc, label);
939190b3
PP
483}
484
60bbfc7c
PP
485const struct bt_field_class_enumeration_unsigned_mapping *
486bt_field_class_enumeration_unsigned_borrow_mapping_by_label_const(
02b61fe0 487 const struct bt_field_class *fc, const char *label)
939190b3 488{
02b61fe0
PP
489 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
490 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc,
491 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, "Field class");
492 return (const void *) borrow_enumeration_field_class_mapping_by_label(
493 (const void *) fc, label);
494}
939190b3 495
02b61fe0
PP
496const char *bt_field_class_enumeration_mapping_get_label(
497 const struct bt_field_class_enumeration_mapping *mapping)
498{
499 BT_ASSERT_PRE_DEV_NON_NULL(mapping, "Enumeration field class mapping");
500 return mapping->label->str;
939190b3
PP
501}
502
02b61fe0 503const struct bt_integer_range_set_unsigned *
60bbfc7c
PP
504bt_field_class_enumeration_unsigned_mapping_borrow_ranges_const(
505 const struct bt_field_class_enumeration_unsigned_mapping *u_mapping)
939190b3 506{
02b61fe0
PP
507 const struct bt_field_class_enumeration_mapping *mapping =
508 (const void *) u_mapping;
509
510 BT_ASSERT_PRE_DEV_NON_NULL(mapping, "Enumeration field class mapping");
511 return (const void *) mapping->range_set;
939190b3
PP
512}
513
02b61fe0 514const struct bt_integer_range_set_signed *
60bbfc7c
PP
515bt_field_class_enumeration_signed_mapping_borrow_ranges_const(
516 const struct bt_field_class_enumeration_signed_mapping *s_mapping)
939190b3 517{
02b61fe0
PP
518 const struct bt_field_class_enumeration_mapping *mapping =
519 (const void *) s_mapping;
520
521 BT_ASSERT_PRE_DEV_NON_NULL(mapping, "Enumeration field class mapping");
522 return (const void *) mapping->range_set;
939190b3
PP
523}
524
1091bb60 525enum bt_field_class_enumeration_get_mapping_labels_for_value_status
60bbfc7c 526bt_field_class_enumeration_unsigned_get_mapping_labels_for_value(
78cf9df6 527 const struct bt_field_class *fc, uint64_t value,
939190b3
PP
528 bt_field_class_enumeration_mapping_label_array *label_array,
529 uint64_t *count)
530{
78cf9df6 531 const struct bt_field_class_enumeration *enum_fc = (const void *) fc;
939190b3
PP
532 uint64_t i;
533
fa6cfec3
PP
534 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
535 BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Label array (output)");
536 BT_ASSERT_PRE_DEV_NON_NULL(count, "Count (output)");
537 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION,
939190b3
PP
538 "Field class");
539 g_ptr_array_set_size(enum_fc->label_buf, 0);
540
541 for (i = 0; i < enum_fc->mappings->len; i++) {
542 uint64_t j;
78cf9df6 543 const struct bt_field_class_enumeration_mapping *mapping =
939190b3
PP
544 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i);
545
02b61fe0
PP
546 for (j = 0; j < mapping->range_set->ranges->len; j++) {
547 const struct bt_integer_range *range = (const void *)
548 BT_INTEGER_RANGE_SET_RANGE_AT_INDEX(
549 mapping->range_set, j);
939190b3
PP
550
551 if (value >= range->lower.u &&
552 value <= range->upper.u) {
553 g_ptr_array_add(enum_fc->label_buf,
554 mapping->label->str);
555 break;
556 }
557 }
558 }
559
560 *label_array = (void *) enum_fc->label_buf->pdata;
561 *count = (uint64_t) enum_fc->label_buf->len;
fb25b9e3 562 return BT_FUNC_STATUS_OK;
939190b3
PP
563}
564
1091bb60 565enum bt_field_class_enumeration_get_mapping_labels_for_value_status
60bbfc7c 566bt_field_class_enumeration_signed_get_mapping_labels_for_value(
78cf9df6 567 const struct bt_field_class *fc, int64_t value,
939190b3
PP
568 bt_field_class_enumeration_mapping_label_array *label_array,
569 uint64_t *count)
570{
78cf9df6 571 const struct bt_field_class_enumeration *enum_fc = (const void *) fc;
939190b3
PP
572 uint64_t i;
573
fa6cfec3
PP
574 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
575 BT_ASSERT_PRE_DEV_NON_NULL(label_array, "Label array (output)");
576 BT_ASSERT_PRE_DEV_NON_NULL(count, "Count (output)");
577 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION,
939190b3
PP
578 "Field class");
579 g_ptr_array_set_size(enum_fc->label_buf, 0);
580
581 for (i = 0; i < enum_fc->mappings->len; i++) {
582 uint64_t j;
78cf9df6 583 const struct bt_field_class_enumeration_mapping *mapping =
939190b3
PP
584 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i);
585
02b61fe0
PP
586 for (j = 0; j < mapping->range_set->ranges->len; j++) {
587 const struct bt_integer_range *range = (const void *)
588 BT_INTEGER_RANGE_SET_RANGE_AT_INDEX(
589 mapping->range_set, j);
939190b3
PP
590
591 if (value >= range->lower.i &&
592 value <= range->upper.i) {
593 g_ptr_array_add(enum_fc->label_buf,
594 mapping->label->str);
595 break;
596 }
597 }
598 }
599
600 *label_array = (void *) enum_fc->label_buf->pdata;
601 *count = (uint64_t) enum_fc->label_buf->len;
fb25b9e3 602 return BT_FUNC_STATUS_OK;
939190b3
PP
603}
604
02b61fe0
PP
605static
606bool enumeration_field_class_has_mapping_with_label(
607 const struct bt_field_class_enumeration *enum_fc,
608 const char *label)
939190b3 609{
939190b3 610 uint64_t i;
02b61fe0 611 bool exists = false;
939190b3 612
02b61fe0
PP
613 BT_ASSERT(enum_fc);
614 BT_ASSERT(label);
939190b3 615
939190b3
PP
616 for (i = 0; i < enum_fc->mappings->len; i++) {
617 struct bt_field_class_enumeration_mapping *mapping_candidate =
618 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i);
619
620 if (strcmp(mapping_candidate->label->str, label) == 0) {
02b61fe0
PP
621 exists = true;
622 goto end;
939190b3
PP
623 }
624 }
625
02b61fe0
PP
626end:
627 return exists;
628}
939190b3 629
02b61fe0
PP
630static inline
631enum bt_field_class_enumeration_add_mapping_status
632add_mapping_to_enumeration_field_class(struct bt_field_class *fc,
633 const char *label, const struct bt_integer_range_set *range_set)
634{
635 enum bt_field_class_enumeration_add_mapping_status status =
636 BT_FUNC_STATUS_OK;
637 struct bt_field_class_enumeration *enum_fc = (void *) fc;
638 struct bt_field_class_enumeration_mapping mapping = { 0 };
639
640 BT_ASSERT(fc);
641 BT_ASSERT_PRE_NON_NULL(label, "Label");
88efd0a7 642 BT_ASSERT_PRE_NON_NULL(range_set, "Integer range set");
02b61fe0
PP
643 BT_ASSERT_PRE(!enumeration_field_class_has_mapping_with_label(
644 enum_fc, label),
645 "Duplicate mapping name in enumeration field class: "
646 "%![enum-fc-]+F, label=\"%s\"", fc, label);
647 mapping.range_set = range_set;
648 bt_object_get_ref(mapping.range_set);
649 mapping.label = g_string_new(label);
650 if (!mapping.label) {
651 finalize_enumeration_field_class_mapping(&mapping);
652 status = BT_FUNC_STATUS_MEMORY_ERROR;
653 goto end;
939190b3
PP
654 }
655
02b61fe0 656 g_array_append_val(enum_fc->mappings, mapping);
a684a357 657 BT_LIB_LOGD("Added mapping to enumeration field class: "
02b61fe0 658 "%![fc-]+F, label=\"%s\"", fc, label);
939190b3
PP
659
660end:
02b61fe0 661 return status;
939190b3
PP
662}
663
02b61fe0 664enum bt_field_class_enumeration_add_mapping_status
60bbfc7c 665bt_field_class_enumeration_unsigned_add_mapping(
78cf9df6 666 struct bt_field_class *fc, const char *label,
02b61fe0 667 const struct bt_integer_range_set_unsigned *range_set)
939190b3 668{
939190b3 669 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
af0c18e3 670 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION,
939190b3 671 "Field class");
02b61fe0
PP
672 return add_mapping_to_enumeration_field_class(fc, label,
673 (const void *) range_set);
674}
675
676enum bt_field_class_enumeration_add_mapping_status
60bbfc7c 677bt_field_class_enumeration_signed_add_mapping(
78cf9df6 678 struct bt_field_class *fc, const char *label,
02b61fe0 679 const struct bt_integer_range_set_signed *range_set)
939190b3 680{
939190b3 681 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
af0c18e3 682 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION,
939190b3 683 "Field class");
02b61fe0
PP
684 return add_mapping_to_enumeration_field_class(fc, label,
685 (const void *) range_set);
939190b3
PP
686}
687
688static
689void destroy_real_field_class(struct bt_object *obj)
690{
691 BT_ASSERT(obj);
9e550e5f 692 BT_LIB_LOGD("Destroying real field class object: %!+F", obj);
af90138b 693 finalize_field_class((void *) obj);
939190b3
PP
694 g_free(obj);
695}
696
76276a81
FD
697static
698struct bt_field_class *create_real_field_class(bt_trace_class *trace_class,
699 enum bt_field_class_type type)
939190b3
PP
700{
701 struct bt_field_class_real *real_fc = NULL;
702
b7396828 703 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
76276a81
FD
704 BT_LOGD("Creating default real field class object: type=%s",
705 bt_common_field_class_type_string(type));
939190b3
PP
706 real_fc = g_new0(struct bt_field_class_real, 1);
707 if (!real_fc) {
a8f90e5d 708 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one real field class.");
939190b3
PP
709 goto error;
710 }
711
76276a81 712 if (init_field_class((void *) real_fc, type, destroy_real_field_class)) {
af90138b
PP
713 goto error;
714 }
715
9e550e5f 716 BT_LIB_LOGD("Created real field class object: %!+F", real_fc);
939190b3
PP
717 goto end;
718
719error:
8138bfe1 720 BT_OBJECT_PUT_REF_AND_RESET(real_fc);
939190b3
PP
721
722end:
723 return (void *) real_fc;
724}
725
76276a81
FD
726struct bt_field_class *bt_field_class_real_single_precision_create(
727 bt_trace_class *trace_class)
939190b3 728{
76276a81
FD
729 return create_real_field_class(trace_class,
730 BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL);
939190b3
PP
731}
732
76276a81
FD
733struct bt_field_class *bt_field_class_real_double_precision_create(
734 bt_trace_class *trace_class)
939190b3 735{
76276a81
FD
736 return create_real_field_class(trace_class,
737 BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL);
939190b3
PP
738}
739
740static
741int init_named_field_classes_container(
742 struct bt_field_class_named_field_class_container *fc,
78cf9df6 743 enum bt_field_class_type type,
02b61fe0
PP
744 bt_object_release_func fc_release_func,
745 GDestroyNotify named_fc_destroy_func)
939190b3
PP
746{
747 int ret = 0;
748
af90138b
PP
749 ret = init_field_class((void *) fc, type, fc_release_func);
750 if (ret) {
751 goto end;
752 }
753
02b61fe0 754 fc->named_fcs = g_ptr_array_new_with_free_func(named_fc_destroy_func);
939190b3 755 if (!fc->named_fcs) {
02b61fe0 756 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
939190b3
PP
757 ret = -1;
758 goto end;
759 }
760
761 fc->name_to_index = g_hash_table_new(g_str_hash, g_str_equal);
762 if (!fc->name_to_index) {
a8f90e5d 763 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GHashTable.");
939190b3
PP
764 ret = -1;
765 goto end;
766 }
767
768end:
769 return ret;
770}
771
772static
773void finalize_named_field_class(struct bt_named_field_class *named_fc)
774{
775 BT_ASSERT(named_fc);
9e550e5f 776 BT_LIB_LOGD("Finalizing named field class: "
939190b3
PP
777 "addr=%p, name=\"%s\", %![fc-]+F",
778 named_fc, named_fc->name ? named_fc->name->str : NULL,
779 named_fc->fc);
af90138b 780 BT_OBJECT_PUT_REF_AND_RESET(named_fc->user_attributes);
939190b3
PP
781
782 if (named_fc->name) {
783 g_string_free(named_fc->name, TRUE);
02b61fe0 784 named_fc->name = NULL;
939190b3
PP
785 }
786
9e550e5f 787 BT_LOGD_STR("Putting named field class's field class.");
1248f5ea 788 BT_OBJECT_PUT_REF_AND_RESET(named_fc->fc);
939190b3
PP
789}
790
02b61fe0
PP
791static
792void destroy_named_field_class(gpointer ptr)
793{
af90138b
PP
794 struct bt_named_field_class *named_fc = ptr;
795
02b61fe0 796 if (ptr) {
560d197d 797 BT_OBJECT_PUT_REF_AND_RESET(named_fc->user_attributes);
02b61fe0
PP
798 finalize_named_field_class(ptr);
799 g_free(ptr);
800 }
801}
802
803static
804void destroy_variant_with_selector_option(gpointer ptr)
805{
806 struct bt_field_class_variant_with_selector_option *opt = ptr;
807
808 if (ptr) {
809 finalize_named_field_class(&opt->common);
810 BT_OBJECT_PUT_REF_AND_RESET(opt->range_set);
811 g_free(ptr);
812 }
813}
814
939190b3
PP
815static
816void finalize_named_field_classes_container(
817 struct bt_field_class_named_field_class_container *fc)
818{
939190b3
PP
819 BT_ASSERT(fc);
820
821 if (fc->named_fcs) {
02b61fe0
PP
822 g_ptr_array_free(fc->named_fcs, TRUE);
823 fc->named_fcs = NULL;
939190b3 824
939190b3
PP
825 }
826
827 if (fc->name_to_index) {
828 g_hash_table_destroy(fc->name_to_index);
02b61fe0 829 fc->name_to_index = NULL;
939190b3
PP
830 }
831}
832
833static
834void destroy_structure_field_class(struct bt_object *obj)
835{
836 BT_ASSERT(obj);
2d95225e 837 BT_LIB_LOGD("Destroying structure field class object: %!+F", obj);
af90138b 838 finalize_field_class((void *) obj);
939190b3
PP
839 finalize_named_field_classes_container((void *) obj);
840 g_free(obj);
841}
842
b7396828
PP
843struct bt_field_class *bt_field_class_structure_create(
844 bt_trace_class *trace_class)
939190b3
PP
845{
846 int ret;
847 struct bt_field_class_structure *struct_fc = NULL;
848
b7396828 849 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
9e550e5f 850 BT_LOGD_STR("Creating default structure field class object.");
939190b3
PP
851 struct_fc = g_new0(struct bt_field_class_structure, 1);
852 if (!struct_fc) {
a8f90e5d
PP
853 BT_LIB_LOGE_APPEND_CAUSE(
854 "Failed to allocate one structure field class.");
939190b3
PP
855 goto error;
856 }
857
858 ret = init_named_field_classes_container((void *) struct_fc,
02b61fe0
PP
859 BT_FIELD_CLASS_TYPE_STRUCTURE, destroy_structure_field_class,
860 destroy_named_field_class);
939190b3 861 if (ret) {
02b61fe0 862 /* init_named_field_classes_container() logs errors */
939190b3
PP
863 goto error;
864 }
865
9e550e5f 866 BT_LIB_LOGD("Created structure field class object: %!+F", struct_fc);
939190b3
PP
867 goto end;
868
869error:
8138bfe1 870 BT_OBJECT_PUT_REF_AND_RESET(struct_fc);
939190b3
PP
871
872end:
873 return (void *) struct_fc;
874}
875
876static
02b61fe0 877int init_named_field_class(struct bt_named_field_class *named_fc,
939190b3
PP
878 const char *name, struct bt_field_class *fc)
879{
02b61fe0 880 int status = BT_FUNC_STATUS_OK;
939190b3 881
02b61fe0
PP
882 BT_ASSERT(named_fc);
883 BT_ASSERT(name);
884 BT_ASSERT(fc);
885 named_fc->name = g_string_new(name);
886 if (!named_fc->name) {
a8f90e5d 887 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
02b61fe0 888 status = BT_FUNC_STATUS_MEMORY_ERROR;
939190b3
PP
889 goto end;
890 }
891
af90138b
PP
892 named_fc->user_attributes = bt_value_map_create();
893 if (!named_fc->user_attributes) {
894 BT_LIB_LOGE_APPEND_CAUSE(
895 "Failed to create a map value object.");
896 status = BT_FUNC_STATUS_MEMORY_ERROR;
897 goto end;
898 }
899
4b70020d 900 named_fc->fc = fc;
864aa43f 901 bt_object_get_ref_no_null_check(named_fc->fc);
e24f271a 902
02b61fe0
PP
903end:
904 return status;
905}
906
907static
908struct bt_named_field_class *create_named_field_class(const char *name,
909 struct bt_field_class *fc)
910{
911 struct bt_named_field_class *named_fc = g_new0(
912 struct bt_named_field_class, 1);
913
914 if (!named_fc) {
915 BT_LIB_LOGE_APPEND_CAUSE(
916 "Failed to allocate a named field class.");
917 goto error;
918 }
919
920 if (init_named_field_class(named_fc, name, fc)) {
921 /* init_named_field_class() logs errors */
922 goto error;
923 }
924
925 goto end;
926
927error:
928 destroy_named_field_class(named_fc);
929 named_fc = NULL;
939190b3
PP
930
931end:
02b61fe0
PP
932 return named_fc;
933}
934
935static
936struct bt_field_class_variant_with_selector_option *
937create_variant_with_selector_option(
938 const char *name, struct bt_field_class *fc,
939 const struct bt_integer_range_set *range_set)
940{
941 struct bt_field_class_variant_with_selector_option *opt = g_new0(
942 struct bt_field_class_variant_with_selector_option, 1);
943
944 BT_ASSERT(range_set);
945
946 if (!opt) {
947 BT_LIB_LOGE_APPEND_CAUSE(
948 "Failed to allocate a named field class.");
949 goto error;
950 }
951
952 if (init_named_field_class(&opt->common, name, fc)) {
953 goto error;
954 }
955
956 opt->range_set = range_set;
864aa43f 957 bt_object_get_ref_no_null_check(opt->range_set);
02b61fe0
PP
958 bt_integer_range_set_freeze(range_set);
959 goto end;
960
961error:
962 destroy_variant_with_selector_option(opt);
963 opt = NULL;
964
965end:
966 return opt;
967}
968
969static
970int append_named_field_class_to_container_field_class(
971 struct bt_field_class_named_field_class_container *container_fc,
972 struct bt_named_field_class *named_fc)
973{
974 BT_ASSERT(container_fc);
975 BT_ASSERT(named_fc);
976 BT_ASSERT_PRE_DEV_FC_HOT(container_fc, "Field class");
977 BT_ASSERT_PRE(!bt_g_hash_table_contains(container_fc->name_to_index,
978 named_fc->name->str),
979 "Duplicate member/option name in structure/variant field class: "
980 "%![container-fc-]+F, name=\"%s\"", container_fc,
981 named_fc->name->str);
af90138b
PP
982
983 /*
984 * Freeze the contained field class, but not the named field
985 * class itself, as it's still possible afterwards to modify
986 * properties of the member/option object.
987 */
988 bt_field_class_freeze(named_fc->fc);
02b61fe0
PP
989 g_ptr_array_add(container_fc->named_fcs, named_fc);
990 g_hash_table_insert(container_fc->name_to_index, named_fc->name->str,
991 GUINT_TO_POINTER(container_fc->named_fcs->len - 1));
992 return BT_FUNC_STATUS_OK;
939190b3
PP
993}
994
fb25b9e3
PP
995enum bt_field_class_structure_append_member_status
996bt_field_class_structure_append_member(
4b514ab3
PP
997 struct bt_field_class *fc, const char *name,
998 struct bt_field_class *member_fc)
939190b3 999{
02b61fe0
PP
1000 enum bt_field_class_structure_append_member_status status;
1001 struct bt_named_field_class *named_fc = NULL;
9e550e5f 1002
939190b3 1003 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
909d0a89
PP
1004 BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE,
1005 "Field class");
02b61fe0
PP
1006 named_fc = create_named_field_class(name, member_fc);
1007 if (!named_fc) {
1008 /* create_named_field_class() logs errors */
1009 status = BT_FUNC_STATUS_MEMORY_ERROR;
1010 goto end;
1011 }
1012
1013 status = append_named_field_class_to_container_field_class((void *) fc,
1014 named_fc);
1015 if (status == BT_FUNC_STATUS_OK) {
1016 /* Moved to the container */
1017 named_fc = NULL;
1018 }
1019
1020end:
1021 return status;
939190b3
PP
1022}
1023
78cf9df6
PP
1024uint64_t bt_field_class_structure_get_member_count(
1025 const struct bt_field_class *fc)
939190b3
PP
1026{
1027 struct bt_field_class_structure *struct_fc = (void *) fc;
1028
fa6cfec3
PP
1029 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1030 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE,
909d0a89 1031 "Field class");
939190b3
PP
1032 return (uint64_t) struct_fc->common.named_fcs->len;
1033}
1034
1035static
e24f271a
PP
1036struct bt_named_field_class *
1037borrow_named_field_class_from_container_field_class_at_index(
909d0a89 1038 struct bt_field_class_named_field_class_container *fc,
e24f271a 1039 uint64_t index)
939190b3 1040{
939190b3 1041 BT_ASSERT(fc);
fa6cfec3 1042 BT_ASSERT_PRE_DEV_VALID_INDEX(index, fc->named_fcs->len);
02b61fe0 1043 return fc->named_fcs->pdata[index];
939190b3
PP
1044}
1045
e24f271a
PP
1046const struct bt_field_class_structure_member *
1047bt_field_class_structure_borrow_member_by_index_const(
1048 const struct bt_field_class *fc, uint64_t index)
939190b3 1049{
fa6cfec3
PP
1050 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1051 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE,
909d0a89 1052 "Field class");
e24f271a
PP
1053 return (const void *)
1054 borrow_named_field_class_from_container_field_class_at_index(
1055 (void *) fc, index);
909d0a89
PP
1056}
1057
e24f271a
PP
1058struct bt_field_class_structure_member *
1059bt_field_class_structure_borrow_member_by_index(
1060 struct bt_field_class *fc, uint64_t index)
909d0a89 1061{
fa6cfec3
PP
1062 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1063 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE,
909d0a89 1064 "Field class");
e24f271a
PP
1065 return (void *)
1066 borrow_named_field_class_from_container_field_class_at_index(
1067 (void *) fc, index);
9e550e5f
PP
1068}
1069
939190b3 1070static
e24f271a
PP
1071struct bt_named_field_class *
1072borrow_named_field_class_from_container_field_class_by_name(
909d0a89 1073 struct bt_field_class_named_field_class_container *fc,
939190b3
PP
1074 const char *name)
1075{
e24f271a 1076 struct bt_named_field_class *named_fc = NULL;
939190b3
PP
1077 gpointer orig_key;
1078 gpointer value;
1079
1080 BT_ASSERT(fc);
fa6cfec3 1081 BT_ASSERT_PRE_DEV_NON_NULL(name, "Name");
939190b3
PP
1082 if (!g_hash_table_lookup_extended(fc->name_to_index, name, &orig_key,
1083 &value)) {
1084 goto end;
1085 }
1086
02b61fe0 1087 named_fc = fc->named_fcs->pdata[GPOINTER_TO_UINT(value)];
939190b3
PP
1088
1089end:
e24f271a 1090 return named_fc;
939190b3
PP
1091}
1092
e24f271a 1093const struct bt_field_class_structure_member *
c0db9072 1094bt_field_class_structure_borrow_member_by_name_const(
78cf9df6 1095 const struct bt_field_class *fc, const char *name)
939190b3 1096{
fa6cfec3
PP
1097 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1098 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE,
909d0a89 1099 "Field class");
e24f271a
PP
1100 return (const void *)
1101 borrow_named_field_class_from_container_field_class_by_name(
909d0a89
PP
1102 (void *) fc, name);
1103}
1104
e24f271a 1105struct bt_field_class_structure_member *
fe65b1fe 1106bt_field_class_structure_borrow_member_by_name(
909d0a89
PP
1107 struct bt_field_class *fc, const char *name)
1108{
fa6cfec3
PP
1109 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1110 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE,
909d0a89 1111 "Field class");
e24f271a
PP
1112 return (void *)
1113 borrow_named_field_class_from_container_field_class_by_name(
78cf9df6 1114 (void *) fc, name);
9e550e5f
PP
1115}
1116
e24f271a
PP
1117const char *bt_field_class_structure_member_get_name(
1118 const struct bt_field_class_structure_member *member)
1119{
1120 const struct bt_named_field_class *named_fc = (const void *) member;
1121
fa6cfec3 1122 BT_ASSERT_PRE_DEV_NON_NULL(member, "Structure field class member");
e24f271a
PP
1123 return named_fc->name->str;
1124}
1125
1126const struct bt_field_class *
1127bt_field_class_structure_member_borrow_field_class_const(
1128 const struct bt_field_class_structure_member *member)
1129{
1130 const struct bt_named_field_class *named_fc = (const void *) member;
1131
fa6cfec3 1132 BT_ASSERT_PRE_DEV_NON_NULL(member, "Structure field class member");
e24f271a
PP
1133 return named_fc->fc;
1134}
1135
87040955
FD
1136struct bt_field_class *
1137bt_field_class_structure_member_borrow_field_class(
1138 struct bt_field_class_structure_member *member)
1139{
1140 struct bt_named_field_class *named_fc = (void *) member;
1141
1142 BT_ASSERT_PRE_DEV_NON_NULL(member, "Structure field class member");
1143 return named_fc->fc;
1144}
1145
247fa9e3
PP
1146static
1147void destroy_option_field_class(struct bt_object *obj)
1148{
1149 struct bt_field_class_option *fc = (void *) obj;
1150
1151 BT_ASSERT(fc);
1152 BT_LIB_LOGD("Destroying option field class object: %!+F", fc);
af90138b 1153 finalize_field_class((void *) obj);
247fa9e3
PP
1154 BT_LOGD_STR("Putting content field class.");
1155 BT_OBJECT_PUT_REF_AND_RESET(fc->content_fc);
467673c1
PP
1156
1157 if (fc->common.type != BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR) {
1158 struct bt_field_class_option_with_selector *with_sel_fc =
1159 (void *) obj;
1160
1161 BT_LOGD_STR("Putting selector field path.");
1162 BT_OBJECT_PUT_REF_AND_RESET(with_sel_fc->selector_field_path);
1163 BT_LOGD_STR("Putting selector field class.");
1164 BT_OBJECT_PUT_REF_AND_RESET(with_sel_fc->selector_fc);
1165
1166 if (fc->common.type != BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR) {
1167 struct bt_field_class_option_with_selector_integer *with_int_sel_fc =
1168 (void *) obj;
1169
1170 BT_LOGD_STR("Putting integer range set.");
1171 BT_OBJECT_PUT_REF_AND_RESET(with_int_sel_fc->range_set);
1172 }
1173 }
1174
247fa9e3
PP
1175 g_free(fc);
1176}
1177
467673c1
PP
1178static
1179struct bt_field_class *create_option_field_class(
1180 struct bt_trace_class *trace_class,
1181 enum bt_field_class_type fc_type,
1182 struct bt_field_class *content_fc,
1183 struct bt_field_class *selector_fc)
247fa9e3
PP
1184{
1185 struct bt_field_class_option *opt_fc = NULL;
1186
1187 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
1188 BT_ASSERT_PRE_NON_NULL(content_fc, "Content field class");
1189 BT_LIB_LOGD("Creating option field class: "
467673c1
PP
1190 "type=%s, %![content-fc-]+F, %![sel-fc-]+F",
1191 bt_common_field_class_type_string(fc_type),
1192 content_fc, selector_fc);
1193
1194 if (fc_type != BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR) {
1195 struct bt_field_class_option_with_selector *opt_with_sel_fc = NULL;
1196
1197 BT_ASSERT_PRE_NON_NULL(selector_fc, "Selector field class");
1198
1199 if (fc_type == BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR) {
1200 BT_ASSERT_PRE_FC_HAS_ID(selector_fc,
1201 BT_FIELD_CLASS_TYPE_BOOL,
1202 "Selector field class");
1203 opt_with_sel_fc = (void *) g_new0(
1204 struct bt_field_class_option_with_selector_bool, 1);
1205 } else {
1206 BT_ASSERT_PRE_FC_IS_INT(selector_fc,
1207 "Selector field class");
1208 opt_with_sel_fc = (void *) g_new0(
1209 struct bt_field_class_option_with_selector_integer, 1);
1210 }
1211
1212 if (!opt_with_sel_fc) {
1213 BT_LIB_LOGE_APPEND_CAUSE(
1214 "Failed to allocate one option with selector field class.");
1215 goto error;
1216 }
1217
1218 opt_with_sel_fc->selector_fc = selector_fc;
864aa43f 1219 bt_object_get_ref_no_null_check(opt_with_sel_fc->selector_fc);
467673c1
PP
1220 opt_fc = (void *) opt_with_sel_fc;
1221 } else {
1222 opt_fc = g_new0(struct bt_field_class_option, 1);
1223 if (!opt_fc) {
1224 BT_LIB_LOGE_APPEND_CAUSE(
1225 "Failed to allocate one option field class.");
1226 goto error;
1227 }
247fa9e3
PP
1228 }
1229
467673c1
PP
1230 BT_ASSERT(opt_fc);
1231
1232 if (init_field_class((void *) opt_fc, fc_type,
af90138b
PP
1233 destroy_option_field_class)) {
1234 goto error;
1235 }
1236
247fa9e3 1237 opt_fc->content_fc = content_fc;
864aa43f 1238 bt_object_get_ref_no_null_check(opt_fc->content_fc);
247fa9e3
PP
1239 bt_field_class_freeze(opt_fc->content_fc);
1240
1241 if (selector_fc) {
247fa9e3
PP
1242 bt_field_class_freeze(selector_fc);
1243 }
1244
1245 BT_LIB_LOGD("Created option field class object: "
1246 "%![opt-fc-]+F, %![sel-fc-]+F", opt_fc, selector_fc);
1247 goto end;
1248
1249error:
1250 BT_OBJECT_PUT_REF_AND_RESET(opt_fc);
1251
1252end:
1253 return (void *) opt_fc;
1254}
1255
467673c1
PP
1256struct bt_field_class *bt_field_class_option_without_selector_create(
1257 struct bt_trace_class *trace_class,
1258 struct bt_field_class *content_fc)
1259{
1260 return create_option_field_class(trace_class,
1261 BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR,
1262 content_fc, NULL);
1263}
1264
1265struct bt_field_class *bt_field_class_option_with_selector_bool_create(
1266 struct bt_trace_class *trace_class,
1267 struct bt_field_class *content_fc,
1268 struct bt_field_class *selector_fc)
1269{
1270 struct bt_field_class_option_with_selector_bool *fc =
1271 (void *) create_option_field_class(trace_class,
1272 BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR,
1273 content_fc, selector_fc);
1274
1275 if (!fc) {
1276 goto end;
1277 }
1278
1279end:
1280 return (void *) fc;
1281}
1282
1283struct bt_field_class *
1284bt_field_class_option_with_selector_integer_unsigned_create(
1285 struct bt_trace_class *trace_class,
1286 struct bt_field_class *content_fc,
1287 struct bt_field_class *selector_fc,
1288 const struct bt_integer_range_set_unsigned *u_range_set)
1289{
1290 struct bt_field_class_option_with_selector_integer *fc;
1291 const struct bt_integer_range_set *range_set =
1292 (const void *) u_range_set;
1293
1294 BT_ASSERT_PRE_NON_NULL(range_set, "Integer range set");
1295 BT_ASSERT_PRE(range_set->ranges->len > 0,
1296 "Integer range set is empty: %!+R", range_set);
1297 fc = (void *) create_option_field_class(trace_class,
1298 BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR,
1299 content_fc, selector_fc);
1300
1301 if (!fc) {
1302 goto end;
1303 }
1304
1305 fc->range_set = range_set;
864aa43f 1306 bt_object_get_ref_no_null_check(fc->range_set);
467673c1
PP
1307 bt_integer_range_set_freeze(range_set);
1308
1309end:
1310 return (void *) fc;
1311}
1312
1313struct bt_field_class *
1314bt_field_class_option_with_selector_integer_signed_create(
1315 struct bt_trace_class *trace_class,
1316 struct bt_field_class *content_fc,
1317 struct bt_field_class *selector_fc,
1318 const struct bt_integer_range_set_signed *i_range_set)
1319{
1320 struct bt_field_class_option_with_selector_integer *fc;
1321 const struct bt_integer_range_set *range_set =
1322 (const void *) i_range_set;
1323
1324 BT_ASSERT_PRE_NON_NULL(range_set, "Integer range set");
1325 BT_ASSERT_PRE(range_set->ranges->len > 0,
1326 "Integer range set is empty: %!+R", range_set);
1327 fc = (void *) create_option_field_class(trace_class,
1328 BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR,
1329 content_fc, selector_fc);
1330
1331 if (!fc) {
1332 goto end;
1333 }
1334
1335 fc->range_set = range_set;
864aa43f 1336 bt_object_get_ref_no_null_check(fc->range_set);
467673c1
PP
1337 bt_integer_range_set_freeze(range_set);
1338
1339end:
1340 return (void *) fc;
1341}
1342
247fa9e3
PP
1343const struct bt_field_class *bt_field_class_option_borrow_field_class_const(
1344 const struct bt_field_class *fc)
1345{
1346 struct bt_field_class_option *opt_fc = (void *) fc;
1347
1348 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
467673c1 1349 BT_ASSERT_PRE_FC_IS_OPTION(fc, "Field class");
247fa9e3
PP
1350 return opt_fc->content_fc;
1351}
1352
87040955
FD
1353struct bt_field_class *bt_field_class_option_borrow_field_class(
1354 struct bt_field_class *fc)
1355{
1356 struct bt_field_class_option *opt_fc = (void *) fc;
1357
1358 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
467673c1 1359 BT_ASSERT_PRE_FC_IS_OPTION(fc, "Field class");
87040955
FD
1360 return opt_fc->content_fc;
1361}
1362
247fa9e3 1363const struct bt_field_path *
467673c1 1364bt_field_class_option_with_selector_borrow_selector_field_path_const(
247fa9e3
PP
1365 const struct bt_field_class *fc)
1366{
467673c1
PP
1367 const struct bt_field_class_option_with_selector *opt_fc =
1368 (const void *) fc;
247fa9e3
PP
1369
1370 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
467673c1 1371 BT_ASSERT_PRE_FC_IS_OPTION_WITH_SEL(fc, "Field class");
247fa9e3
PP
1372 return opt_fc->selector_field_path;
1373}
1374
467673c1
PP
1375void bt_field_class_option_with_selector_bool_set_selector_is_reversed(
1376 struct bt_field_class *fc, bt_bool sel_is_reversed)
1377{
1378 struct bt_field_class_option_with_selector_bool *opt_fc = (void *) fc;
1379
1380 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
1381 BT_ASSERT_PRE_FC_HAS_ID(fc,
1382 BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR, "Field class");
1383 BT_ASSERT_PRE_DEV_FC_HOT(fc, "Field class");
1384 opt_fc->sel_is_reversed = sel_is_reversed;
1385}
1386
1387bt_bool bt_field_class_option_with_selector_bool_selector_is_reversed(
1388 const struct bt_field_class *fc)
1389{
1390 struct bt_field_class_option_with_selector_bool *opt_fc = (void *) fc;
1391
1392 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
1393 BT_ASSERT_PRE_FC_HAS_ID(fc,
1394 BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR, "Field class");
1395 return opt_fc->sel_is_reversed;
1396}
1397
1398const struct bt_integer_range_set_unsigned *
1399bt_field_class_option_with_selector_integer_unsigned_borrow_selector_ranges_const(
1400 const struct bt_field_class *fc)
1401{
1402 struct bt_field_class_option_with_selector_integer *opt_fc =
1403 (void *) fc;
1404
1405 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
1406 BT_ASSERT_PRE_FC_IS_OPTION_WITH_INT_SEL(fc, "Field class");
1407 return (const void *) opt_fc->range_set;
1408}
1409
1410const struct bt_integer_range_set_signed *
1411bt_field_class_option_with_selector_integer_signed_borrow_selector_ranges_const(
1412 const struct bt_field_class *fc)
1413{
1414 struct bt_field_class_option_with_selector_integer *opt_fc =
1415 (void *) fc;
1416
1417 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
1418 BT_ASSERT_PRE_FC_IS_OPTION_WITH_INT_SEL(fc, "Field class");
1419 return (const void *) opt_fc->range_set;
1420}
1421
02b61fe0
PP
1422static
1423void finalize_variant_field_class(struct bt_field_class_variant *var_fc)
e24f271a 1424{
02b61fe0
PP
1425 BT_ASSERT(var_fc);
1426 BT_LIB_LOGD("Finalizing variant field class object: %!+F", var_fc);
af90138b 1427 finalize_field_class((void *) var_fc);
02b61fe0 1428 finalize_named_field_classes_container((void *) var_fc);
e24f271a
PP
1429}
1430
939190b3
PP
1431static
1432void destroy_variant_field_class(struct bt_object *obj)
1433{
1434 struct bt_field_class_variant *fc = (void *) obj;
1435
1436 BT_ASSERT(fc);
02b61fe0
PP
1437 finalize_variant_field_class(fc);
1438 g_free(fc);
1439}
1440
1441static
1442void destroy_variant_with_selector_field_class(struct bt_object *obj)
1443{
1444 struct bt_field_class_variant_with_selector *fc = (void *) obj;
1445
1446 BT_ASSERT(fc);
1447 finalize_variant_field_class(&fc->common);
939190b3 1448 BT_LOGD_STR("Putting selector field path.");
1248f5ea
PP
1449 BT_OBJECT_PUT_REF_AND_RESET(fc->selector_field_path);
1450 BT_LOGD_STR("Putting selector field class.");
1451 BT_OBJECT_PUT_REF_AND_RESET(fc->selector_fc);
939190b3
PP
1452 g_free(fc);
1453}
1454
b7396828 1455struct bt_field_class *bt_field_class_variant_create(
02b61fe0 1456 bt_trace_class *trace_class, bt_field_class *selector_fc)
939190b3
PP
1457{
1458 int ret;
1459 struct bt_field_class_variant *var_fc = NULL;
02b61fe0
PP
1460 struct bt_field_class_variant_with_selector *var_with_sel_fc = NULL;
1461 enum bt_field_class_type fc_type;
939190b3 1462
b7396828 1463 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
02b61fe0
PP
1464
1465 if (selector_fc) {
1466 BT_ASSERT_PRE_FC_IS_INT(selector_fc, "Selector field class");
939190b3
PP
1467 }
1468
02b61fe0
PP
1469 BT_LIB_LOGD("Creating default variant field class: %![sel-fc-]+F",
1470 selector_fc);
1471
1472 if (selector_fc) {
1473 var_with_sel_fc = g_new0(
1474 struct bt_field_class_variant_with_selector, 1);
1475 if (!var_with_sel_fc) {
1476 BT_LIB_LOGE_APPEND_CAUSE(
1477 "Failed to allocate one variant field class with selector.");
1478 goto error;
1479 }
1480
1481 if (selector_fc->type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER ||
1482 selector_fc->type == BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION) {
0822832b 1483 fc_type = BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR;
02b61fe0 1484 } else {
0822832b 1485 fc_type = BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR;
02b61fe0
PP
1486 }
1487
1488 ret = init_named_field_classes_container(
1489 (void *) var_with_sel_fc, fc_type,
1490 destroy_variant_with_selector_field_class,
1491 destroy_variant_with_selector_option);
1492 if (ret) {
1493 /* init_named_field_classes_container() logs errors */
1494 goto error;
1495 }
1496
1497 var_with_sel_fc->selector_fc = selector_fc;
864aa43f 1498 bt_object_get_ref_no_null_check(var_with_sel_fc->selector_fc);
02b61fe0
PP
1499 bt_field_class_freeze(selector_fc);
1500 var_fc = (void *) var_with_sel_fc;
ed47d321
FD
1501 BT_LIB_LOGD("Created default variant field class with selector object: "
1502 "%![var-fc-]+F, %![sel-fc-]+F", var_fc, selector_fc);
02b61fe0
PP
1503 } else {
1504 var_fc = g_new0(struct bt_field_class_variant, 1);
1505 if (!var_fc) {
1506 BT_LIB_LOGE_APPEND_CAUSE(
1507 "Failed to allocate one variant field class without selector.");
1508 goto error;
1509 }
1510
1511 ret = init_named_field_classes_container((void *) var_fc,
1512 BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR,
1513 destroy_variant_field_class, destroy_named_field_class);
1514 if (ret) {
1515 /* init_named_field_classes_container() logs errors */
1516 goto error;
1517 }
ed47d321
FD
1518 BT_LIB_LOGD("Created default variant field class without selector object: "
1519 "%![var-fc-]+F", var_fc);
939190b3
PP
1520 }
1521
02b61fe0 1522 BT_ASSERT(var_fc);
939190b3
PP
1523 goto end;
1524
1525error:
8138bfe1 1526 BT_OBJECT_PUT_REF_AND_RESET(var_fc);
939190b3
PP
1527
1528end:
1529 return (void *) var_fc;
1530}
1531
02b61fe0
PP
1532enum bt_field_class_variant_without_selector_append_option_status
1533bt_field_class_variant_without_selector_append_option(struct bt_field_class *fc,
1534 const char *name, struct bt_field_class *option_fc)
1535{
1536 enum bt_field_class_variant_without_selector_append_option_status status;
1537 struct bt_named_field_class *named_fc = NULL;
1538
1539 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
1540 BT_ASSERT_PRE_NON_NULL(name, "Name");
1541 BT_ASSERT_PRE_NON_NULL(option_fc, "Option field class");
1542 BT_ASSERT_PRE_FC_HAS_ID(fc,
1543 BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR, "Field class");
1544 named_fc = create_named_field_class(name, option_fc);
1545 if (!named_fc) {
1546 /* create_named_field_class() logs errors */
1547 status = BT_FUNC_STATUS_MEMORY_ERROR;
1548 goto end;
1549 }
1550
1551 status = append_named_field_class_to_container_field_class((void *) fc,
1552 named_fc);
1553 if (status == BT_FUNC_STATUS_OK) {
1554 /* Moved to the container */
1555 named_fc = NULL;
1556 }
1557
1558end:
1559 if (named_fc) {
1560 destroy_named_field_class(named_fc);
1561 }
1562
1563 return status;
939190b3
PP
1564}
1565
02b61fe0
PP
1566static
1567int ranges_overlap(GPtrArray *var_fc_opts, const struct bt_integer_range_set *range_set,
1568 bool is_signed, bool *has_overlap)
1569{
1570 int status = BT_FUNC_STATUS_OK;
1571 struct bt_integer_range_set *full_range_set;
1572 uint64_t i;
1573
1574 *has_overlap = false;
1575
1576 /*
1577 * Build a single range set with all the ranges and test for
1578 * overlaps.
1579 */
1580 if (is_signed) {
1581 full_range_set = (void *) bt_integer_range_set_signed_create();
1582 } else {
1583 full_range_set = (void *) bt_integer_range_set_unsigned_create();
1584 }
1585
1586 if (!full_range_set) {
1587 BT_LOGE_STR("Failed to create a range set.");
1588 status = BT_FUNC_STATUS_MEMORY_ERROR;
1589 goto end;
1590 }
1591
1592 /* Add existing option ranges */
1593 for (i = 0; i < var_fc_opts->len; i++) {
1594 struct bt_field_class_variant_with_selector_option *opt =
1595 var_fc_opts->pdata[i];
1596 uint64_t j;
1597
1598 for (j = 0; j < opt->range_set->ranges->len; j++) {
1599 struct bt_integer_range *range = BT_INTEGER_RANGE_SET_RANGE_AT_INDEX(
1600 opt->range_set, j);
1601
1602 if (is_signed) {
1603 status = bt_integer_range_set_signed_add_range(
1604 (void *) full_range_set, range->lower.i,
1605 range->upper.i);
1606 } else {
1607 status = bt_integer_range_set_unsigned_add_range(
1608 (void *) full_range_set, range->lower.u,
1609 range->upper.u);
1610 }
1611
1612 if (status) {
1613 goto end;
1614 }
1615 }
1616 }
1617
1618 /* Add new ranges */
1619 for (i = 0; i < range_set->ranges->len; i++) {
1620 struct bt_integer_range *range = BT_INTEGER_RANGE_SET_RANGE_AT_INDEX(
1621 range_set, i);
1622
1623 if (is_signed) {
1624 status = bt_integer_range_set_signed_add_range(
1625 (void *) full_range_set, range->lower.i,
1626 range->upper.i);
1627 } else {
1628 status = bt_integer_range_set_unsigned_add_range(
1629 (void *) full_range_set, range->lower.u,
1630 range->upper.u);
1631 }
1632
1633 if (status) {
1634 goto end;
1635 }
1636 }
1637
1638 /* Check overlaps */
1639 if (is_signed) {
1640 *has_overlap = bt_integer_range_set_signed_has_overlaps(full_range_set);
1641 } else {
1642 *has_overlap = bt_integer_range_set_unsigned_has_overlaps(
1643 full_range_set);
1644 }
1645
1646end:
1647 bt_object_put_ref(full_range_set);
1648 return status;
1649}
1650
1651static
1652int append_option_to_variant_with_selector_field_class(
1653 struct bt_field_class *fc, const char *name,
1654 struct bt_field_class *option_fc,
1655 const struct bt_integer_range_set *range_set,
1656 enum bt_field_class_type expected_type)
939190b3 1657{
02b61fe0
PP
1658 int status;
1659 struct bt_field_class_variant_with_selector *var_fc = (void *) fc;
1660 struct bt_field_class_variant_with_selector_option *opt = NULL;
1661 bool has_overlap;
9e550e5f 1662
939190b3 1663 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
02b61fe0
PP
1664 BT_ASSERT_PRE_NON_NULL(name, "Name");
1665 BT_ASSERT_PRE_NON_NULL(option_fc, "Option field class");
88efd0a7 1666 BT_ASSERT_PRE_NON_NULL(range_set, "Integer range set");
02b61fe0
PP
1667 BT_ASSERT_PRE_FC_HAS_ID(fc, expected_type, "Field class");
1668 BT_ASSERT_PRE(range_set->ranges->len > 0,
88efd0a7 1669 "Integer range set is empty: %!+R", range_set);
02b61fe0 1670 status = ranges_overlap(var_fc->common.common.named_fcs, range_set,
0822832b 1671 expected_type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR,
02b61fe0
PP
1672 &has_overlap);
1673 if (status) {
1674 /* ranges_overlap() logs errors */
1675 goto end;
1676 }
1677
1678 BT_ASSERT_PRE(!has_overlap,
88efd0a7
PP
1679 "Integer range set's ranges and existing ranges have an overlap: "
1680 "%!+R", range_set);
02b61fe0
PP
1681 opt = create_variant_with_selector_option(name, option_fc, range_set);
1682 if (!opt) {
1683 /* create_variant_with_selector_option() logs errors */
1684 status = BT_FUNC_STATUS_MEMORY_ERROR;
1685 goto end;
1686 }
1687
1688 status = append_named_field_class_to_container_field_class((void *) fc,
1689 &opt->common);
1690 if (status == BT_FUNC_STATUS_OK) {
1691 /* Moved to the container */
1692 opt = NULL;
1693 }
1694
1695end:
1696 if (opt) {
1697 destroy_variant_with_selector_option(opt);
1698 }
1699
1700 return status;
1701}
1702
0822832b
PP
1703enum bt_field_class_variant_with_selector_integer_append_option_status
1704bt_field_class_variant_with_selector_integer_unsigned_append_option(
02b61fe0
PP
1705 struct bt_field_class *fc, const char *name,
1706 struct bt_field_class *option_fc,
1707 const struct bt_integer_range_set_unsigned *range_set)
1708{
1709 return append_option_to_variant_with_selector_field_class(fc,
1710 name, option_fc, (const void *) range_set,
0822832b 1711 BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR);
02b61fe0
PP
1712}
1713
0822832b
PP
1714enum bt_field_class_variant_with_selector_integer_append_option_status
1715bt_field_class_variant_with_selector_integer_signed_append_option(
02b61fe0
PP
1716 struct bt_field_class *fc, const char *name,
1717 struct bt_field_class *option_fc,
1718 const struct bt_integer_range_set_signed *range_set)
1719{
1720 return append_option_to_variant_with_selector_field_class(fc,
1721 name, option_fc, (const void *) range_set,
0822832b 1722 BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR);
02b61fe0
PP
1723}
1724
1725uint64_t bt_field_class_variant_get_option_count(const struct bt_field_class *fc)
1726{
1727 const struct bt_field_class_variant *var_fc = (const void *) fc;
1728
1729 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1730 BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc, "Field class");
1731 return (uint64_t) var_fc->common.named_fcs->len;
939190b3
PP
1732}
1733
e24f271a 1734const struct bt_field_class_variant_option *
c0db9072 1735bt_field_class_variant_borrow_option_by_name_const(
78cf9df6 1736 const struct bt_field_class *fc, const char *name)
939190b3 1737{
fa6cfec3 1738 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
02b61fe0 1739 BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc, "Field class");
e24f271a
PP
1740 return (const void *)
1741 borrow_named_field_class_from_container_field_class_by_name(
1742 (void *) fc, name);
909d0a89
PP
1743}
1744
02b61fe0
PP
1745const struct bt_field_class_variant_option *
1746bt_field_class_variant_borrow_option_by_index_const(
1747 const struct bt_field_class *fc, uint64_t index)
909d0a89 1748{
fa6cfec3 1749 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
02b61fe0
PP
1750 BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc, "Field class");
1751 return (const void *)
1752 borrow_named_field_class_from_container_field_class_at_index(
34dd2c30
PP
1753 (void *) fc, index);
1754}
1755
1756struct bt_field_class_variant_option *
1757bt_field_class_variant_borrow_option_by_name(
1758 struct bt_field_class *fc, const char *name)
1759{
1760 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1761 BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc, "Field class");
1762 return (void *)
1763 borrow_named_field_class_from_container_field_class_by_name(
1764 (void *) fc, name);
1765}
1766
1767struct bt_field_class_variant_option *
1768bt_field_class_variant_borrow_option_by_index(
1769 struct bt_field_class *fc, uint64_t index)
1770{
1771 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1772 BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc, "Field class");
1773 return (void *)
1774 borrow_named_field_class_from_container_field_class_at_index(
02b61fe0 1775 (void *) fc, index);
9e550e5f
PP
1776}
1777
0822832b
PP
1778const struct bt_field_class_variant_with_selector_integer_unsigned_option *
1779bt_field_class_variant_with_selector_integer_unsigned_borrow_option_by_name_const(
02b61fe0 1780 const struct bt_field_class *fc, const char *name)
939190b3 1781{
fa6cfec3 1782 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
02b61fe0 1783 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc,
0822832b 1784 BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR,
02b61fe0
PP
1785 "Field class");
1786 return (const void *)
1787 borrow_named_field_class_from_container_field_class_by_name(
1788 (void *) fc, name);
939190b3
PP
1789}
1790
0822832b
PP
1791const struct bt_field_class_variant_with_selector_integer_unsigned_option *
1792bt_field_class_variant_with_selector_integer_unsigned_borrow_option_by_index_const(
e24f271a 1793 const struct bt_field_class *fc, uint64_t index)
939190b3 1794{
fa6cfec3 1795 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
02b61fe0 1796 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc,
0822832b 1797 BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR,
02b61fe0 1798 "Field class");
e24f271a
PP
1799 return (const void *)
1800 borrow_named_field_class_from_container_field_class_at_index(
1801 (void *) fc, index);
909d0a89
PP
1802}
1803
0822832b
PP
1804const struct bt_field_class_variant_with_selector_integer_signed_option *
1805bt_field_class_variant_with_selector_integer_signed_borrow_option_by_name_const(
02b61fe0 1806 const struct bt_field_class *fc, const char *name)
909d0a89 1807{
fa6cfec3 1808 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
02b61fe0 1809 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc,
0822832b 1810 BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR,
02b61fe0
PP
1811 "Field class");
1812 return (const void *)
1813 borrow_named_field_class_from_container_field_class_by_name(
1814 (void *) fc, name);
1815}
1816
0822832b
PP
1817const struct bt_field_class_variant_with_selector_integer_signed_option *
1818bt_field_class_variant_with_selector_integer_signed_borrow_option_by_index_const(
02b61fe0
PP
1819 const struct bt_field_class *fc, uint64_t index)
1820{
1821 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1822 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc,
0822832b 1823 BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR,
02b61fe0
PP
1824 "Field class");
1825 return (const void *)
e24f271a
PP
1826 borrow_named_field_class_from_container_field_class_at_index(
1827 (void *) fc, index);
1828}
1829
1830const char *bt_field_class_variant_option_get_name(
1831 const struct bt_field_class_variant_option *option)
1832{
1833 const struct bt_named_field_class *named_fc = (const void *) option;
1834
fa6cfec3 1835 BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option");
e24f271a
PP
1836 return named_fc->name->str;
1837}
1838
1839const struct bt_field_class *
1840bt_field_class_variant_option_borrow_field_class_const(
1841 const struct bt_field_class_variant_option *option)
1842{
1843 const struct bt_named_field_class *named_fc = (const void *) option;
1844
fa6cfec3 1845 BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option");
e24f271a
PP
1846 return named_fc->fc;
1847}
1848
87040955
FD
1849struct bt_field_class *
1850bt_field_class_variant_option_borrow_field_class(
1851 struct bt_field_class_variant_option *option)
1852{
1853 struct bt_named_field_class *named_fc = (void *) option;
1854
1855 BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option");
1856 return named_fc->fc;
1857}
1858
02b61fe0 1859const struct bt_integer_range_set_unsigned *
0822832b
PP
1860bt_field_class_variant_with_selector_integer_unsigned_option_borrow_ranges_const(
1861 const struct bt_field_class_variant_with_selector_integer_unsigned_option *option)
e24f271a 1862{
02b61fe0
PP
1863 const struct bt_field_class_variant_with_selector_option *opt =
1864 (const void *) option;
e24f271a 1865
fa6cfec3 1866 BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option");
02b61fe0
PP
1867 return (const void *) opt->range_set;
1868}
1869
1870const struct bt_integer_range_set_signed *
0822832b
PP
1871bt_field_class_variant_with_selector_integer_signed_option_borrow_ranges_const(
1872 const struct bt_field_class_variant_with_selector_integer_signed_option *option)
02b61fe0
PP
1873{
1874 const struct bt_field_class_variant_with_selector_option *opt =
1875 (const void *) option;
1876
1877 BT_ASSERT_PRE_DEV_NON_NULL(option, "Variant field class option");
1878 return (const void *) opt->range_set;
939190b3
PP
1879}
1880
78cf9df6 1881const struct bt_field_path *
02b61fe0 1882bt_field_class_variant_with_selector_borrow_selector_field_path_const(
78cf9df6 1883 const struct bt_field_class *fc)
9e550e5f 1884{
02b61fe0
PP
1885 const struct bt_field_class_variant_with_selector *var_fc =
1886 (const void *) fc;
939190b3 1887
fa6cfec3 1888 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
02b61fe0 1889 BT_ASSERT_PRE_DEV_FC_IS_VARIANT_WITH_SEL(fc, "Field class");
939190b3
PP
1890 return var_fc->selector_field_path;
1891}
1892
1893static
af90138b 1894int init_array_field_class(struct bt_field_class_array *fc,
af0c18e3 1895 enum bt_field_class_type type, bt_object_release_func release_func,
939190b3
PP
1896 struct bt_field_class *element_fc)
1897{
af90138b
PP
1898 int ret;
1899
939190b3 1900 BT_ASSERT(element_fc);
af90138b
PP
1901 ret = init_field_class((void *) fc, type, release_func);
1902 if (ret) {
1903 goto end;
1904 }
1905
4b70020d 1906 fc->element_fc = element_fc;
864aa43f 1907 bt_object_get_ref_no_null_check(fc->element_fc);
939190b3 1908 bt_field_class_freeze(element_fc);
af90138b
PP
1909
1910end:
1911 return ret;
939190b3
PP
1912}
1913
1914static
1915void finalize_array_field_class(struct bt_field_class_array *array_fc)
1916{
1917 BT_ASSERT(array_fc);
9e550e5f 1918 BT_LOGD_STR("Putting element field class.");
af90138b 1919 finalize_field_class((void *) array_fc);
1248f5ea 1920 BT_OBJECT_PUT_REF_AND_RESET(array_fc->element_fc);
939190b3
PP
1921}
1922
1923static
1924void destroy_static_array_field_class(struct bt_object *obj)
1925{
1926 BT_ASSERT(obj);
9e550e5f 1927 BT_LIB_LOGD("Destroying static array field class object: %!+F", obj);
939190b3
PP
1928 finalize_array_field_class((void *) obj);
1929 g_free(obj);
1930}
1931
78cf9df6 1932struct bt_field_class *
60bbfc7c 1933bt_field_class_array_static_create(bt_trace_class *trace_class,
b7396828 1934 struct bt_field_class *element_fc, uint64_t length)
939190b3 1935{
60bbfc7c 1936 struct bt_field_class_array_static *array_fc = NULL;
939190b3 1937
b7396828 1938 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
9e550e5f
PP
1939 BT_ASSERT_PRE_NON_NULL(element_fc, "Element field class");
1940 BT_LOGD_STR("Creating default static array field class object.");
60bbfc7c 1941 array_fc = g_new0(struct bt_field_class_array_static, 1);
939190b3 1942 if (!array_fc) {
a8f90e5d
PP
1943 BT_LIB_LOGE_APPEND_CAUSE(
1944 "Failed to allocate one static array field class.");
939190b3
PP
1945 goto error;
1946 }
1947
af90138b
PP
1948 if (init_array_field_class((void *) array_fc,
1949 BT_FIELD_CLASS_TYPE_STATIC_ARRAY,
1950 destroy_static_array_field_class, element_fc)) {
1951 goto error;
1952 }
1953
939190b3 1954 array_fc->length = length;
9e550e5f 1955 BT_LIB_LOGD("Created static array field class object: %!+F", array_fc);
939190b3
PP
1956 goto end;
1957
1958error:
8138bfe1 1959 BT_OBJECT_PUT_REF_AND_RESET(array_fc);
939190b3
PP
1960
1961end:
1962 return (void *) array_fc;
1963}
1964
78cf9df6
PP
1965const struct bt_field_class *
1966bt_field_class_array_borrow_element_field_class_const(
1967 const struct bt_field_class *fc)
939190b3 1968{
78cf9df6 1969 const struct bt_field_class_array *array_fc = (const void *) fc;
939190b3 1970
fa6cfec3
PP
1971 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1972 BT_ASSERT_PRE_DEV_FC_IS_ARRAY(fc, "Field class");
939190b3
PP
1973 return array_fc->element_fc;
1974}
1975
909d0a89
PP
1976struct bt_field_class *
1977bt_field_class_array_borrow_element_field_class(struct bt_field_class *fc)
1978{
1979 struct bt_field_class_array *array_fc = (void *) fc;
1980
fa6cfec3
PP
1981 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1982 BT_ASSERT_PRE_DEV_FC_IS_ARRAY(fc, "Field class");
909d0a89
PP
1983 return array_fc->element_fc;
1984}
1985
60bbfc7c 1986uint64_t bt_field_class_array_static_get_length(const struct bt_field_class *fc)
9e550e5f 1987{
60bbfc7c 1988 const struct bt_field_class_array_static *array_fc = (const void *) fc;
939190b3 1989
fa6cfec3
PP
1990 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
1991 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STATIC_ARRAY,
939190b3
PP
1992 "Field class");
1993 return (uint64_t) array_fc->length;
1994}
1995
1996static
1997void destroy_dynamic_array_field_class(struct bt_object *obj)
1998{
60bbfc7c 1999 struct bt_field_class_array_dynamic *fc = (void *) obj;
939190b3
PP
2000
2001 BT_ASSERT(fc);
9e550e5f 2002 BT_LIB_LOGD("Destroying dynamic array field class object: %!+F", fc);
939190b3
PP
2003 finalize_array_field_class((void *) fc);
2004 BT_LOGD_STR("Putting length field path.");
1248f5ea
PP
2005 BT_OBJECT_PUT_REF_AND_RESET(fc->length_field_path);
2006 BT_LOGD_STR("Putting length field class.");
2007 BT_OBJECT_PUT_REF_AND_RESET(fc->length_fc);
939190b3
PP
2008 g_free(fc);
2009}
2010
60bbfc7c 2011struct bt_field_class *bt_field_class_array_dynamic_create(
93d7d5d1
PP
2012 struct bt_trace_class *trace_class,
2013 struct bt_field_class *element_fc,
2014 struct bt_field_class *length_fc)
939190b3 2015{
60bbfc7c 2016 struct bt_field_class_array_dynamic *array_fc = NULL;
939190b3 2017
b7396828 2018 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
9e550e5f
PP
2019 BT_ASSERT_PRE_NON_NULL(element_fc, "Element field class");
2020 BT_LOGD_STR("Creating default dynamic array field class object.");
60bbfc7c 2021 array_fc = g_new0(struct bt_field_class_array_dynamic, 1);
939190b3 2022 if (!array_fc) {
a8f90e5d
PP
2023 BT_LIB_LOGE_APPEND_CAUSE(
2024 "Failed to allocate one dynamic array field class.");
939190b3
PP
2025 goto error;
2026 }
2027
af90138b
PP
2028 if (init_array_field_class((void *) array_fc,
2029 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY,
2030 destroy_dynamic_array_field_class, element_fc)) {
2031 goto error;
2032 }
93d7d5d1
PP
2033
2034 if (length_fc) {
2035 BT_ASSERT_PRE_FC_IS_UNSIGNED_INT(length_fc,
2036 "Length field class");
2037 array_fc->length_fc = length_fc;
864aa43f 2038 bt_object_get_ref_no_null_check(array_fc->length_fc);
93d7d5d1
PP
2039 bt_field_class_freeze(length_fc);
2040 }
2041
9e550e5f 2042 BT_LIB_LOGD("Created dynamic array field class object: %!+F", array_fc);
939190b3
PP
2043 goto end;
2044
2045error:
8138bfe1 2046 BT_OBJECT_PUT_REF_AND_RESET(array_fc);
939190b3
PP
2047
2048end:
2049 return (void *) array_fc;
2050}
2051
78cf9df6 2052const struct bt_field_path *
60bbfc7c 2053bt_field_class_array_dynamic_borrow_length_field_path_const(
78cf9df6 2054 const struct bt_field_class *fc)
939190b3 2055{
60bbfc7c 2056 const struct bt_field_class_array_dynamic *seq_fc = (const void *) fc;
939190b3 2057
fa6cfec3
PP
2058 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
2059 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY,
939190b3
PP
2060 "Field class");
2061 return seq_fc->length_field_path;
2062}
2063
2064static
2065void destroy_string_field_class(struct bt_object *obj)
2066{
2067 BT_ASSERT(obj);
9e550e5f 2068 BT_LIB_LOGD("Destroying string field class object: %!+F", obj);
af90138b 2069 finalize_field_class((void *) obj);
939190b3
PP
2070 g_free(obj);
2071}
2072
b7396828 2073struct bt_field_class *bt_field_class_string_create(bt_trace_class *trace_class)
939190b3
PP
2074{
2075 struct bt_field_class_string *string_fc = NULL;
2076
b7396828 2077 BT_ASSERT_PRE_NON_NULL(trace_class, "Trace class");
9e550e5f 2078 BT_LOGD_STR("Creating default string field class object.");
939190b3
PP
2079 string_fc = g_new0(struct bt_field_class_string, 1);
2080 if (!string_fc) {
a8f90e5d
PP
2081 BT_LIB_LOGE_APPEND_CAUSE(
2082 "Failed to allocate one string field class.");
939190b3
PP
2083 goto error;
2084 }
2085
af90138b
PP
2086 if (init_field_class((void *) string_fc, BT_FIELD_CLASS_TYPE_STRING,
2087 destroy_string_field_class)) {
2088 goto error;
2089 }
2090
9e550e5f 2091 BT_LIB_LOGD("Created string field class object: %!+F", string_fc);
939190b3
PP
2092 goto end;
2093
2094error:
8138bfe1 2095 BT_OBJECT_PUT_REF_AND_RESET(string_fc);
939190b3
PP
2096
2097end:
2098 return (void *) string_fc;
2099}
2100
2101BT_HIDDEN
e24f271a 2102void _bt_field_class_freeze(const struct bt_field_class *c_fc)
939190b3 2103{
e24f271a
PP
2104 struct bt_field_class *fc = (void *) c_fc;
2105
939190b3
PP
2106 /*
2107 * Element/member/option field classes are frozen when added to
2108 * their owner.
2109 */
2110 BT_ASSERT(fc);
af90138b 2111 bt_value_freeze(fc->user_attributes);
e24f271a
PP
2112 fc->frozen = true;
2113
2114 switch (fc->type) {
2115 case BT_FIELD_CLASS_TYPE_STRUCTURE:
02b61fe0 2116 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR:
0822832b
PP
2117 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR:
2118 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR:
e24f271a
PP
2119 {
2120 struct bt_field_class_named_field_class_container *container_fc =
2121 (void *) fc;
2122 uint64_t i;
2123
2124 for (i = 0; i < container_fc->named_fcs->len; i++) {
02b61fe0
PP
2125 bt_named_field_class_freeze(
2126 container_fc->named_fcs->pdata[i]);
e24f271a
PP
2127 }
2128
2129 break;
2130 }
2131 default:
2132 break;
2133 }
2134}
2135
2136BT_HIDDEN
2137void _bt_named_field_class_freeze(const struct bt_named_field_class *named_fc)
2138{
2139 BT_ASSERT(named_fc);
af90138b
PP
2140 BT_ASSERT(named_fc->fc->frozen);
2141 BT_LIB_LOGD("Freezing named field class's user attributes: %!+v",
2142 named_fc->user_attributes);
2143 bt_value_freeze(named_fc->user_attributes);
e24f271a 2144 ((struct bt_named_field_class *) named_fc)->frozen = true;
939190b3
PP
2145}
2146
2147BT_HIDDEN
fa6cfec3 2148void bt_field_class_make_part_of_trace_class(const struct bt_field_class *c_fc)
939190b3 2149{
78cf9df6
PP
2150 struct bt_field_class *fc = (void *) c_fc;
2151
939190b3 2152 BT_ASSERT(fc);
10b7a2e4 2153 BT_ASSERT_PRE(!fc->part_of_trace_class,
939190b3 2154 "Field class is already part of a trace: %!+F", fc);
10b7a2e4 2155 fc->part_of_trace_class = true;
939190b3 2156
af0c18e3
PP
2157 switch (fc->type) {
2158 case BT_FIELD_CLASS_TYPE_STRUCTURE:
02b61fe0 2159 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR:
0822832b
PP
2160 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR:
2161 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR:
939190b3
PP
2162 {
2163 struct bt_field_class_named_field_class_container *container_fc =
2164 (void *) fc;
2165 uint64_t i;
2166
2167 for (i = 0; i < container_fc->named_fcs->len; i++) {
2168 struct bt_named_field_class *named_fc =
02b61fe0 2169 container_fc->named_fcs->pdata[i];
939190b3 2170
10b7a2e4 2171 bt_field_class_make_part_of_trace_class(named_fc->fc);
939190b3
PP
2172 }
2173
2174 break;
2175 }
af0c18e3
PP
2176 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
2177 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
939190b3
PP
2178 {
2179 struct bt_field_class_array *array_fc = (void *) fc;
2180
10b7a2e4 2181 bt_field_class_make_part_of_trace_class(array_fc->element_fc);
939190b3
PP
2182 break;
2183 }
2184 default:
2185 break;
2186 }
2187}
8c6884d9 2188
af90138b
PP
2189const struct bt_value *bt_field_class_borrow_user_attributes_const(
2190 const struct bt_field_class *fc)
2191{
2192 BT_ASSERT_PRE_DEV_NON_NULL(fc, "Field class");
2193 return fc->user_attributes;
2194}
2195
2196struct bt_value *bt_field_class_borrow_user_attributes(
2197 struct bt_field_class *field_class)
2198{
2199 return (void *) bt_field_class_borrow_user_attributes_const(
2200 (void *) field_class);
2201}
2202
2203
2204void bt_field_class_set_user_attributes(
2205 struct bt_field_class *fc,
2206 const struct bt_value *user_attributes)
2207{
2208 BT_ASSERT_PRE_NON_NULL(fc, "Field class");
2209 BT_ASSERT_PRE_NON_NULL(user_attributes, "User attributes");
2210 BT_ASSERT_PRE(user_attributes->type == BT_VALUE_TYPE_MAP,
2211 "User attributes object is not a map value object.");
2212 BT_ASSERT_PRE_DEV_FC_HOT(fc, "Field class");
864aa43f 2213 bt_object_put_ref_no_null_check(fc->user_attributes);
af90138b 2214 fc->user_attributes = (void *) user_attributes;
864aa43f 2215 bt_object_get_ref_no_null_check(fc->user_attributes);
af90138b
PP
2216}
2217
2218static
2219const struct bt_value *bt_named_field_class_borrow_user_attributes_const(
2220 const struct bt_named_field_class *named_fc)
2221{
2222 return named_fc->user_attributes;
2223}
2224
2225static
2226void bt_named_field_class_set_user_attributes(
2227 struct bt_named_field_class *named_fc,
2228 const struct bt_value *user_attributes)
2229{
2230 BT_ASSERT_PRE_NON_NULL(user_attributes, "User attributes");
2231 BT_ASSERT_PRE(user_attributes->type == BT_VALUE_TYPE_MAP,
2232 "User attributes object is not a map value object.");
2233 BT_ASSERT_PRE_DEV_HOT(named_fc,
2234 "Structure field class member or variant field class option",
2235 ".");
864aa43f 2236 bt_object_put_ref_no_null_check(named_fc->user_attributes);
af90138b 2237 named_fc->user_attributes = (void *) user_attributes;
864aa43f 2238 bt_object_get_ref_no_null_check(named_fc->user_attributes);
af90138b
PP
2239}
2240
2241const struct bt_value *
2242bt_field_class_structure_member_borrow_user_attributes_const(
2243 const struct bt_field_class_structure_member *member)
2244{
2245 BT_ASSERT_PRE_NON_NULL(member, "Structure field class member");
2246 return bt_named_field_class_borrow_user_attributes_const(
2247 (const void *) member);
2248}
2249
2250struct bt_value *
2251bt_field_class_structure_member_borrow_user_attributes(
2252 struct bt_field_class_structure_member *member)
2253{
2254 BT_ASSERT_PRE_NON_NULL(member, "Structure field class member");
2255 return (void *) bt_named_field_class_borrow_user_attributes_const(
2256 (void *) member);
2257}
2258
2259void bt_field_class_structure_member_set_user_attributes(
2260 struct bt_field_class_structure_member *member,
2261 const struct bt_value *user_attributes)
2262{
2263 BT_ASSERT_PRE_NON_NULL(member, "Structure field class member");
2264 bt_named_field_class_set_user_attributes((void *) member,
2265 user_attributes);
2266}
2267
2268const struct bt_value *bt_field_class_variant_option_borrow_user_attributes_const(
2269 const struct bt_field_class_variant_option *option)
2270{
2271 BT_ASSERT_PRE_NON_NULL(option, "Variant field class option");
2272 return bt_named_field_class_borrow_user_attributes_const(
2273 (const void *) option);
2274}
2275
2276struct bt_value *bt_field_class_variant_option_borrow_user_attributes(
2277 struct bt_field_class_variant_option *option)
2278{
2279 BT_ASSERT_PRE_NON_NULL(option, "Variant field class option");
2280 return (void *) bt_named_field_class_borrow_user_attributes_const(
2281 (void *) option);
2282}
2283
2284void bt_field_class_variant_option_set_user_attributes(
2285 struct bt_field_class_variant_option *option,
2286 const struct bt_value *user_attributes)
2287{
2288 BT_ASSERT_PRE_NON_NULL(option, "Variant field class option");
2289 bt_named_field_class_set_user_attributes((void *) option,
2290 user_attributes);
2291}
2292
8c6884d9
PP
2293void bt_field_class_get_ref(const struct bt_field_class *field_class)
2294{
2295 bt_object_get_ref(field_class);
2296}
2297
2298void bt_field_class_put_ref(const struct bt_field_class *field_class)
2299{
2300 bt_object_put_ref(field_class);
2301}
This page took 0.147861 seconds and 4 git commands to generate.