Commit | Line | Data |
---|---|---|
56e18c4c PP |
1 | #ifndef BABELTRACE_TRACE_IR_FIELDS_INTERNAL_H |
2 | #define BABELTRACE_TRACE_IR_FIELDS_INTERNAL_H | |
273b65be JG |
3 | |
4 | /* | |
e2f7325d | 5 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
de9dd397 | 6 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
273b65be | 7 | * |
273b65be JG |
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
9 | * of this software and associated documentation files (the "Software"), to deal | |
10 | * in the Software without restriction, including without limitation the rights | |
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
12 | * copies of the Software, and to permit persons to whom the Software is | |
13 | * furnished to do so, subject to the following conditions: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be included in | |
16 | * all copies or substantial portions of the Software. | |
17 | * | |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
24 | * SOFTWARE. | |
25 | */ | |
26 | ||
578e048b MJ |
27 | #include "lib/assert-pre.h" |
28 | #include "common/common.h" | |
29 | #include "lib/object.h" | |
91d81473 | 30 | #include "common/macros.h" |
3fadfbc0 | 31 | #include <babeltrace2/types.h> |
dc3fffef | 32 | #include <stdint.h> |
4d4b475d | 33 | #include <string.h> |
3dca2276 | 34 | #include <inttypes.h> |
d990a4fb | 35 | #include <stdbool.h> |
273b65be JG |
36 | #include <glib.h> |
37 | ||
578e048b MJ |
38 | #include "field-class.h" |
39 | #include "utils.h" | |
40 | ||
bdb288b3 PP |
41 | #define BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(_field, _cls_type, _name) \ |
42 | BT_ASSERT_PRE_DEV(((const struct bt_field *) (_field))->class->type == (_cls_type), \ | |
864cad70 | 43 | _name " has the wrong class type: expected-class-type=%s, " \ |
44c440bc | 44 | "%![field-]+f", \ |
864cad70 | 45 | bt_common_field_class_type_string(_cls_type), (_field)) |
44c440bc | 46 | |
bdb288b3 PP |
47 | #define BT_ASSERT_PRE_DEV_FIELD_IS_UNSIGNED_INT(_field, _name) \ |
48 | BT_ASSERT_PRE_DEV( \ | |
40f4ba76 PP |
49 | ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER || \ |
50 | ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, \ | |
44c440bc PP |
51 | _name " is not an unsigned integer field: %![field-]+f", \ |
52 | (_field)) | |
53 | ||
bdb288b3 PP |
54 | #define BT_ASSERT_PRE_DEV_FIELD_IS_SIGNED_INT(_field, _name) \ |
55 | BT_ASSERT_PRE_DEV( \ | |
40f4ba76 PP |
56 | ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_SIGNED_INTEGER || \ |
57 | ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, \ | |
bdb288b3 | 58 | _name " is not a signed integer field: %![field-]+f", \ |
44c440bc PP |
59 | (_field)) |
60 | ||
bdb288b3 PP |
61 | #define BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY(_field, _name) \ |
62 | BT_ASSERT_PRE_DEV( \ | |
40f4ba76 | 63 | ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_STATIC_ARRAY || \ |
81b8fa44 PP |
64 | ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD || \ |
65 | ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD, \ | |
44c440bc PP |
66 | _name " is not an array field: %![field-]+f", (_field)) |
67 | ||
81b8fa44 PP |
68 | #define BT_ASSERT_PRE_DEV_FIELD_IS_DYNAMIC_ARRAY(_field, _name) \ |
69 | BT_ASSERT_PRE_DEV( \ | |
70 | ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD || \ | |
71 | ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD, \ | |
72 | _name " is not a dynamic array field: %![field-]+f", (_field)) | |
73 | ||
0aa006b7 PP |
74 | #define BT_ASSERT_PRE_DEV_FIELD_IS_OPTION(_field, _name) \ |
75 | BT_ASSERT_PRE_DEV( \ | |
de821fe5 PP |
76 | ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD || \ |
77 | ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD || \ | |
78 | ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD || \ | |
79 | ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD, \ | |
0aa006b7 PP |
80 | _name " is not an option field: %![field-]+f", (_field)) |
81 | ||
45c51519 PP |
82 | #define BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT(_field, _name) \ |
83 | BT_ASSERT_PRE_DEV( \ | |
de821fe5 PP |
84 | ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD || \ |
85 | ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD || \ | |
86 | ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD, \ | |
45c51519 PP |
87 | _name " is not a variant field: %![field-]+f", (_field)) |
88 | ||
bdb288b3 PP |
89 | #define BT_ASSERT_PRE_DEV_FIELD_IS_SET(_field, _name) \ |
90 | BT_ASSERT_PRE_DEV(bt_field_is_set(_field), \ | |
cb6f1f7d | 91 | _name " is not set: %!+f", (_field)) |
3dca2276 | 92 | |
bdb288b3 PP |
93 | #define BT_ASSERT_PRE_DEV_FIELD_HOT(_field, _name) \ |
94 | BT_ASSERT_PRE_DEV_HOT((const struct bt_field *) (_field), (_name), \ | |
44c440bc | 95 | ": %!+f", (_field)) |
3dca2276 PP |
96 | |
97 | struct bt_field; | |
3dca2276 | 98 | |
5cd6d0e5 | 99 | typedef struct bt_field *(* bt_field_create_func)(struct bt_field_class *); |
44c440bc | 100 | typedef void (*bt_field_method_set_is_frozen)(struct bt_field *, bool); |
40f4ba76 | 101 | typedef bool (*bt_field_method_is_set)(const struct bt_field *); |
cb6f1f7d PP |
102 | typedef void (*bt_field_method_reset)(struct bt_field *); |
103 | ||
104 | struct bt_field_methods { | |
105 | bt_field_method_set_is_frozen set_is_frozen; | |
cb6f1f7d PP |
106 | bt_field_method_is_set is_set; |
107 | bt_field_method_reset reset; | |
3dca2276 PP |
108 | }; |
109 | ||
cb6f1f7d | 110 | struct bt_field { |
83509119 | 111 | struct bt_object base; |
44c440bc PP |
112 | |
113 | /* Owned by this */ | |
5cd6d0e5 | 114 | struct bt_field_class *class; |
44c440bc PP |
115 | |
116 | /* Virtual table for slow path (dev mode) operations */ | |
cb6f1f7d | 117 | struct bt_field_methods *methods; |
44c440bc PP |
118 | |
119 | bool is_set; | |
d990a4fb | 120 | bool frozen; |
273b65be JG |
121 | }; |
122 | ||
5cebbe7f PP |
123 | struct bt_field_bool { |
124 | struct bt_field common; | |
125 | bool value; | |
126 | }; | |
127 | ||
1094efa4 PP |
128 | struct bt_field_bit_array { |
129 | struct bt_field common; | |
130 | uint64_t value_as_int; | |
131 | }; | |
132 | ||
cb6f1f7d PP |
133 | struct bt_field_integer { |
134 | struct bt_field common; | |
273b65be | 135 | |
44c440bc PP |
136 | union { |
137 | uint64_t u; | |
138 | int64_t i; | |
139 | } value; | |
cb6f1f7d PP |
140 | }; |
141 | ||
44c440bc | 142 | struct bt_field_real { |
cb6f1f7d | 143 | struct bt_field common; |
44c440bc | 144 | double value; |
273b65be JG |
145 | }; |
146 | ||
cb6f1f7d PP |
147 | struct bt_field_structure { |
148 | struct bt_field common; | |
312c056a | 149 | |
cb6f1f7d | 150 | /* Array of `struct bt_field *`, owned by this */ |
312c056a | 151 | GPtrArray *fields; |
273b65be JG |
152 | }; |
153 | ||
b38aea74 PP |
154 | struct bt_field_option { |
155 | struct bt_field common; | |
156 | ||
157 | /* Owned by this */ | |
158 | struct bt_field *content_field; | |
159 | ||
160 | /* Weak: equal to `content_field` above or `NULL` */ | |
161 | struct bt_field *selected_field; | |
162 | }; | |
163 | ||
cb6f1f7d PP |
164 | struct bt_field_variant { |
165 | struct bt_field common; | |
312c056a | 166 | |
44c440bc PP |
167 | /* Weak: belongs to `fields` below */ |
168 | struct bt_field *selected_field; | |
312c056a | 169 | |
44c440bc PP |
170 | /* Index of currently selected field */ |
171 | uint64_t selected_index; | |
312c056a | 172 | |
cb6f1f7d | 173 | /* Array of `struct bt_field *`, owned by this */ |
312c056a | 174 | GPtrArray *fields; |
273b65be JG |
175 | }; |
176 | ||
cb6f1f7d PP |
177 | struct bt_field_array { |
178 | struct bt_field common; | |
312c056a | 179 | |
cb6f1f7d | 180 | /* Array of `struct bt_field *`, owned by this */ |
44c440bc | 181 | GPtrArray *fields; |
312c056a | 182 | |
44c440bc | 183 | /* Current effective length */ |
312c056a | 184 | uint64_t length; |
273b65be JG |
185 | }; |
186 | ||
cb6f1f7d PP |
187 | struct bt_field_string { |
188 | struct bt_field common; | |
4d4b475d | 189 | GArray *buf; |
44c440bc | 190 | uint64_t length; |
273b65be JG |
191 | }; |
192 | ||
f6ccaed9 | 193 | #ifdef BT_DEV_MODE |
44c440bc PP |
194 | # define bt_field_set_is_frozen _bt_field_set_is_frozen |
195 | # define bt_field_is_set _bt_field_is_set | |
196 | # define bt_field_reset _bt_field_reset | |
197 | # define bt_field_set_single _bt_field_set_single | |
f6ccaed9 | 198 | #else |
44c440bc PP |
199 | # define bt_field_set_is_frozen(_field, _is_frozen) |
200 | # define bt_field_is_set(_field) (BT_FALSE) | |
201 | # define bt_field_reset(_field) | |
202 | # define bt_field_set_single(_field, _val) | |
f6ccaed9 | 203 | #endif |
918be005 | 204 | |
cb6f1f7d | 205 | BT_HIDDEN |
40f4ba76 | 206 | void _bt_field_set_is_frozen(const struct bt_field *field, bool is_frozen); |
3dca2276 PP |
207 | |
208 | static inline | |
40f4ba76 | 209 | void _bt_field_reset(const struct bt_field *field) |
3dca2276 | 210 | { |
98b15851 PP |
211 | BT_ASSERT_DBG(field); |
212 | BT_ASSERT_DBG(field->methods->reset); | |
40f4ba76 | 213 | field->methods->reset((void *) field); |
3dca2276 PP |
214 | } |
215 | ||
216 | static inline | |
44c440bc | 217 | void _bt_field_set_single(struct bt_field *field, bool value) |
3dca2276 | 218 | { |
98b15851 | 219 | BT_ASSERT_DBG(field); |
44c440bc | 220 | field->is_set = value; |
3dca2276 PP |
221 | } |
222 | ||
223 | static inline | |
40f4ba76 | 224 | bt_bool _bt_field_is_set(const struct bt_field *field) |
3dca2276 PP |
225 | { |
226 | bt_bool is_set = BT_FALSE; | |
227 | ||
228 | if (!field) { | |
229 | goto end; | |
230 | } | |
231 | ||
98b15851 | 232 | BT_ASSERT_DBG(field->methods->is_set); |
3dca2276 PP |
233 | is_set = field->methods->is_set(field); |
234 | ||
235 | end: | |
236 | return is_set; | |
237 | } | |
238 | ||
312c056a | 239 | BT_HIDDEN |
5cd6d0e5 | 240 | struct bt_field *bt_field_create(struct bt_field_class *class); |
312c056a PP |
241 | |
242 | BT_HIDDEN | |
44c440bc | 243 | void bt_field_destroy(struct bt_field *field); |
312c056a | 244 | |
56e18c4c | 245 | #endif /* BABELTRACE_TRACE_IR_FIELDS_INTERNAL_H */ |