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