Commit | Line | Data |
---|---|---|
273b65be | 1 | /* |
0235b0db MJ |
2 | * SPDX-License-Identifier: MIT |
3 | * | |
e2f7325d | 4 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
de9dd397 | 5 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
273b65be JG |
6 | */ |
7 | ||
0235b0db MJ |
8 | #ifndef BABELTRACE_TRACE_IR_FIELDS_INTERNAL_H |
9 | #define BABELTRACE_TRACE_IR_FIELDS_INTERNAL_H | |
10 | ||
578e048b MJ |
11 | #include "lib/assert-pre.h" |
12 | #include "common/common.h" | |
13 | #include "lib/object.h" | |
91d81473 | 14 | #include "common/macros.h" |
3fadfbc0 | 15 | #include <babeltrace2/types.h> |
dc3fffef | 16 | #include <stdint.h> |
4d4b475d | 17 | #include <string.h> |
3dca2276 | 18 | #include <inttypes.h> |
d990a4fb | 19 | #include <stdbool.h> |
273b65be JG |
20 | #include <glib.h> |
21 | ||
578e048b MJ |
22 | #include "field-class.h" |
23 | #include "utils.h" | |
24 | ||
bdb288b3 PP |
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), \ | |
864cad70 | 27 | _name " has the wrong class type: expected-class-type=%s, " \ |
44c440bc | 28 | "%![field-]+f", \ |
864cad70 | 29 | bt_common_field_class_type_string(_cls_type), (_field)) |
44c440bc | 30 | |
bdb288b3 PP |
31 | #define BT_ASSERT_PRE_DEV_FIELD_IS_UNSIGNED_INT(_field, _name) \ |
32 | BT_ASSERT_PRE_DEV( \ | |
40f4ba76 PP |
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, \ | |
44c440bc PP |
35 | _name " is not an unsigned integer field: %![field-]+f", \ |
36 | (_field)) | |
37 | ||
bdb288b3 PP |
38 | #define BT_ASSERT_PRE_DEV_FIELD_IS_SIGNED_INT(_field, _name) \ |
39 | BT_ASSERT_PRE_DEV( \ | |
40f4ba76 PP |
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, \ | |
bdb288b3 | 42 | _name " is not a signed integer field: %![field-]+f", \ |
44c440bc PP |
43 | (_field)) |
44 | ||
bdb288b3 PP |
45 | #define BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY(_field, _name) \ |
46 | BT_ASSERT_PRE_DEV( \ | |
40f4ba76 | 47 | ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_STATIC_ARRAY || \ |
81b8fa44 PP |
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, \ | |
44c440bc PP |
50 | _name " is not an array field: %![field-]+f", (_field)) |
51 | ||
81b8fa44 PP |
52 | #define BT_ASSERT_PRE_DEV_FIELD_IS_DYNAMIC_ARRAY(_field, _name) \ |
53 | BT_ASSERT_PRE_DEV( \ | |
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)) | |
57 | ||
0aa006b7 PP |
58 | #define BT_ASSERT_PRE_DEV_FIELD_IS_OPTION(_field, _name) \ |
59 | BT_ASSERT_PRE_DEV( \ | |
de821fe5 PP |
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, \ | |
0aa006b7 PP |
64 | _name " is not an option field: %![field-]+f", (_field)) |
65 | ||
45c51519 PP |
66 | #define BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT(_field, _name) \ |
67 | BT_ASSERT_PRE_DEV( \ | |
de821fe5 PP |
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, \ | |
45c51519 PP |
71 | _name " is not a variant field: %![field-]+f", (_field)) |
72 | ||
bdb288b3 PP |
73 | #define BT_ASSERT_PRE_DEV_FIELD_IS_SET(_field, _name) \ |
74 | BT_ASSERT_PRE_DEV(bt_field_is_set(_field), \ | |
cb6f1f7d | 75 | _name " is not set: %!+f", (_field)) |
3dca2276 | 76 | |
bdb288b3 PP |
77 | #define BT_ASSERT_PRE_DEV_FIELD_HOT(_field, _name) \ |
78 | BT_ASSERT_PRE_DEV_HOT((const struct bt_field *) (_field), (_name), \ | |
44c440bc | 79 | ": %!+f", (_field)) |
3dca2276 PP |
80 | |
81 | struct bt_field; | |
3dca2276 | 82 | |
5cd6d0e5 | 83 | typedef struct bt_field *(* bt_field_create_func)(struct bt_field_class *); |
44c440bc | 84 | typedef void (*bt_field_method_set_is_frozen)(struct bt_field *, bool); |
40f4ba76 | 85 | typedef bool (*bt_field_method_is_set)(const struct bt_field *); |
cb6f1f7d PP |
86 | typedef void (*bt_field_method_reset)(struct bt_field *); |
87 | ||
88 | struct bt_field_methods { | |
89 | bt_field_method_set_is_frozen set_is_frozen; | |
cb6f1f7d PP |
90 | bt_field_method_is_set is_set; |
91 | bt_field_method_reset reset; | |
3dca2276 PP |
92 | }; |
93 | ||
cb6f1f7d | 94 | struct bt_field { |
83509119 | 95 | struct bt_object base; |
44c440bc PP |
96 | |
97 | /* Owned by this */ | |
5cd6d0e5 | 98 | struct bt_field_class *class; |
44c440bc PP |
99 | |
100 | /* Virtual table for slow path (dev mode) operations */ | |
cb6f1f7d | 101 | struct bt_field_methods *methods; |
44c440bc PP |
102 | |
103 | bool is_set; | |
d990a4fb | 104 | bool frozen; |
273b65be JG |
105 | }; |
106 | ||
5cebbe7f PP |
107 | struct bt_field_bool { |
108 | struct bt_field common; | |
109 | bool value; | |
110 | }; | |
111 | ||
1094efa4 PP |
112 | struct bt_field_bit_array { |
113 | struct bt_field common; | |
114 | uint64_t value_as_int; | |
115 | }; | |
116 | ||
cb6f1f7d PP |
117 | struct bt_field_integer { |
118 | struct bt_field common; | |
273b65be | 119 | |
44c440bc PP |
120 | union { |
121 | uint64_t u; | |
122 | int64_t i; | |
123 | } value; | |
cb6f1f7d PP |
124 | }; |
125 | ||
44c440bc | 126 | struct bt_field_real { |
cb6f1f7d | 127 | struct bt_field common; |
44c440bc | 128 | double value; |
273b65be JG |
129 | }; |
130 | ||
cb6f1f7d PP |
131 | struct bt_field_structure { |
132 | struct bt_field common; | |
312c056a | 133 | |
cb6f1f7d | 134 | /* Array of `struct bt_field *`, owned by this */ |
312c056a | 135 | GPtrArray *fields; |
273b65be JG |
136 | }; |
137 | ||
b38aea74 PP |
138 | struct bt_field_option { |
139 | struct bt_field common; | |
140 | ||
141 | /* Owned by this */ | |
142 | struct bt_field *content_field; | |
143 | ||
144 | /* Weak: equal to `content_field` above or `NULL` */ | |
145 | struct bt_field *selected_field; | |
146 | }; | |
147 | ||
cb6f1f7d PP |
148 | struct bt_field_variant { |
149 | struct bt_field common; | |
312c056a | 150 | |
44c440bc PP |
151 | /* Weak: belongs to `fields` below */ |
152 | struct bt_field *selected_field; | |
312c056a | 153 | |
44c440bc PP |
154 | /* Index of currently selected field */ |
155 | uint64_t selected_index; | |
312c056a | 156 | |
cb6f1f7d | 157 | /* Array of `struct bt_field *`, owned by this */ |
312c056a | 158 | GPtrArray *fields; |
273b65be JG |
159 | }; |
160 | ||
cb6f1f7d PP |
161 | struct bt_field_array { |
162 | struct bt_field common; | |
312c056a | 163 | |
cb6f1f7d | 164 | /* Array of `struct bt_field *`, owned by this */ |
44c440bc | 165 | GPtrArray *fields; |
312c056a | 166 | |
44c440bc | 167 | /* Current effective length */ |
312c056a | 168 | uint64_t length; |
273b65be JG |
169 | }; |
170 | ||
cb6f1f7d PP |
171 | struct bt_field_string { |
172 | struct bt_field common; | |
4d4b475d | 173 | GArray *buf; |
44c440bc | 174 | uint64_t length; |
273b65be JG |
175 | }; |
176 | ||
f6ccaed9 | 177 | #ifdef BT_DEV_MODE |
44c440bc PP |
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 | |
f6ccaed9 | 182 | #else |
44c440bc PP |
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) | |
f6ccaed9 | 187 | #endif |
918be005 | 188 | |
cb6f1f7d | 189 | BT_HIDDEN |
40f4ba76 | 190 | void _bt_field_set_is_frozen(const struct bt_field *field, bool is_frozen); |
3dca2276 PP |
191 | |
192 | static inline | |
40f4ba76 | 193 | void _bt_field_reset(const struct bt_field *field) |
3dca2276 | 194 | { |
98b15851 PP |
195 | BT_ASSERT_DBG(field); |
196 | BT_ASSERT_DBG(field->methods->reset); | |
40f4ba76 | 197 | field->methods->reset((void *) field); |
3dca2276 PP |
198 | } |
199 | ||
200 | static inline | |
44c440bc | 201 | void _bt_field_set_single(struct bt_field *field, bool value) |
3dca2276 | 202 | { |
98b15851 | 203 | BT_ASSERT_DBG(field); |
44c440bc | 204 | field->is_set = value; |
3dca2276 PP |
205 | } |
206 | ||
207 | static inline | |
40f4ba76 | 208 | bt_bool _bt_field_is_set(const struct bt_field *field) |
3dca2276 PP |
209 | { |
210 | bt_bool is_set = BT_FALSE; | |
211 | ||
212 | if (!field) { | |
213 | goto end; | |
214 | } | |
215 | ||
98b15851 | 216 | BT_ASSERT_DBG(field->methods->is_set); |
3dca2276 PP |
217 | is_set = field->methods->is_set(field); |
218 | ||
219 | end: | |
220 | return is_set; | |
221 | } | |
222 | ||
312c056a | 223 | BT_HIDDEN |
5cd6d0e5 | 224 | struct bt_field *bt_field_create(struct bt_field_class *class); |
312c056a PP |
225 | |
226 | BT_HIDDEN | |
44c440bc | 227 | void bt_field_destroy(struct bt_field *field); |
312c056a | 228 | |
56e18c4c | 229 | #endif /* BABELTRACE_TRACE_IR_FIELDS_INTERNAL_H */ |