2 * SPDX-License-Identifier: MIT
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 #ifndef BABELTRACE_TRACE_IR_FIELDS_INTERNAL_H
9 #define BABELTRACE_TRACE_IR_FIELDS_INTERNAL_H
11 #include "lib/assert-cond.h"
12 #include "common/common.h"
13 #include "lib/object.h"
14 #include "common/macros.h"
15 #include <babeltrace2/types.h>
22 #include "field-class.h"
25 #define BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(_field, _cls_type, _name) \
26 BT_ASSERT_PRE_DEV(((const struct bt_field *) (_field))->class->type == (_cls_type), \
27 _name " has the wrong class type: expected-class-type=%s, " \
29 bt_common_field_class_type_string(_cls_type), (_field))
31 #define BT_ASSERT_PRE_DEV_FIELD_IS_UNSIGNED_INT(_field, _name) \
33 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER || \
34 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, \
35 _name " is not an unsigned integer field: %![field-]+f", \
38 #define BT_ASSERT_PRE_DEV_FIELD_IS_SIGNED_INT(_field, _name) \
40 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_SIGNED_INTEGER || \
41 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, \
42 _name " is not a signed integer field: %![field-]+f", \
45 #define BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY(_field, _name) \
47 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_STATIC_ARRAY || \
48 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD || \
49 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD, \
50 _name " is not an array field: %![field-]+f", (_field))
52 #define BT_ASSERT_PRE_DEV_FIELD_IS_DYNAMIC_ARRAY(_field, _name) \
54 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD || \
55 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD, \
56 _name " is not a dynamic array field: %![field-]+f", (_field))
58 #define BT_ASSERT_PRE_DEV_FIELD_IS_OPTION(_field, _name) \
60 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD || \
61 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD || \
62 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD || \
63 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD, \
64 _name " is not an option field: %![field-]+f", (_field))
66 #define BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT(_field, _name) \
68 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD || \
69 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD || \
70 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD, \
71 _name " is not a variant field: %![field-]+f", (_field))
73 #define BT_ASSERT_PRE_DEV_FIELD_IS_SET(_field, _name) \
74 BT_ASSERT_PRE_DEV(bt_field_is_set(_field), \
75 _name " is not set: %!+f", (_field))
77 #define BT_ASSERT_PRE_DEV_FIELD_HOT(_field, _name) \
78 BT_ASSERT_PRE_DEV_HOT((const struct bt_field *) (_field), (_name), \
83 typedef struct bt_field
*(* bt_field_create_func
)(struct bt_field_class
*);
84 typedef void (*bt_field_method_set_is_frozen
)(struct bt_field
*, bool);
85 typedef bool (*bt_field_method_is_set
)(const struct bt_field
*);
86 typedef void (*bt_field_method_reset
)(struct bt_field
*);
88 struct bt_field_methods
{
89 bt_field_method_set_is_frozen set_is_frozen
;
90 bt_field_method_is_set is_set
;
91 bt_field_method_reset reset
;
95 struct bt_object base
;
98 struct bt_field_class
*class;
100 /* Virtual table for slow path (dev mode) operations */
101 struct bt_field_methods
*methods
;
107 struct bt_field_bool
{
108 struct bt_field common
;
112 struct bt_field_bit_array
{
113 struct bt_field common
;
114 uint64_t value_as_int
;
117 struct bt_field_integer
{
118 struct bt_field common
;
126 struct bt_field_real
{
127 struct bt_field common
;
131 struct bt_field_structure
{
132 struct bt_field common
;
134 /* Array of `struct bt_field *`, owned by this */
138 struct bt_field_option
{
139 struct bt_field common
;
142 struct bt_field
*content_field
;
144 /* Weak: equal to `content_field` above or `NULL` */
145 struct bt_field
*selected_field
;
148 struct bt_field_variant
{
149 struct bt_field common
;
151 /* Weak: belongs to `fields` below */
152 struct bt_field
*selected_field
;
154 /* Index of currently selected field */
155 uint64_t selected_index
;
157 /* Array of `struct bt_field *`, owned by this */
161 struct bt_field_array
{
162 struct bt_field common
;
164 /* Array of `struct bt_field *`, owned by this */
167 /* Current effective length */
171 struct bt_field_string
{
172 struct bt_field common
;
178 # define bt_field_set_is_frozen _bt_field_set_is_frozen
179 # define bt_field_is_set _bt_field_is_set
180 # define bt_field_reset _bt_field_reset
181 # define bt_field_set_single _bt_field_set_single
183 # define bt_field_set_is_frozen(_field, _is_frozen)
184 # define bt_field_is_set(_field) (BT_FALSE)
185 # define bt_field_reset(_field)
186 # define bt_field_set_single(_field, _val)
190 void _bt_field_set_is_frozen(const struct bt_field
*field
, bool is_frozen
);
193 void _bt_field_reset(const struct bt_field
*field
)
195 BT_ASSERT_DBG(field
);
196 BT_ASSERT_DBG(field
->methods
->reset
);
197 field
->methods
->reset((void *) field
);
201 void _bt_field_set_single(struct bt_field
*field
, bool value
)
203 BT_ASSERT_DBG(field
);
204 field
->is_set
= value
;
208 bt_bool
_bt_field_is_set(const struct bt_field
*field
)
210 bt_bool is_set
= BT_FALSE
;
216 BT_ASSERT_DBG(field
->methods
->is_set
);
217 is_set
= field
->methods
->is_set(field
);
224 struct bt_field
*bt_field_create(struct bt_field_class
*class);
227 void bt_field_destroy(struct bt_field
*field
);
229 #endif /* BABELTRACE_TRACE_IR_FIELDS_INTERNAL_H */