| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
| 5 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 6 | */ |
| 7 | |
| 8 | #ifndef BABELTRACE_LIB_TRACE_IR_FIELD_H |
| 9 | #define BABELTRACE_LIB_TRACE_IR_FIELD_H |
| 10 | |
| 11 | #include "lib/object.h" |
| 12 | #include <babeltrace2/types.h> |
| 13 | #include <stdint.h> |
| 14 | #include <stdbool.h> |
| 15 | #include <glib.h> |
| 16 | |
| 17 | #include "field-class.h" |
| 18 | |
| 19 | struct bt_field; |
| 20 | |
| 21 | typedef struct bt_field *(* bt_field_create_func)(struct bt_field_class *); |
| 22 | typedef void (*bt_field_method_set_is_frozen)(struct bt_field *, bool); |
| 23 | typedef bool (*bt_field_method_is_set)(const struct bt_field *); |
| 24 | typedef void (*bt_field_method_reset)(struct bt_field *); |
| 25 | |
| 26 | struct bt_field_methods { |
| 27 | bt_field_method_set_is_frozen set_is_frozen; |
| 28 | bt_field_method_is_set is_set; |
| 29 | bt_field_method_reset reset; |
| 30 | }; |
| 31 | |
| 32 | struct bt_field { |
| 33 | struct bt_object base; |
| 34 | |
| 35 | /* Owned by this */ |
| 36 | struct bt_field_class *class; |
| 37 | |
| 38 | /* Virtual table for slow path (dev mode) operations */ |
| 39 | struct bt_field_methods *methods; |
| 40 | |
| 41 | bool is_set; |
| 42 | bool frozen; |
| 43 | }; |
| 44 | |
| 45 | struct bt_field_bool { |
| 46 | struct bt_field common; |
| 47 | bool value; |
| 48 | }; |
| 49 | |
| 50 | struct bt_field_bit_array { |
| 51 | struct bt_field common; |
| 52 | uint64_t value_as_int; |
| 53 | }; |
| 54 | |
| 55 | struct bt_field_integer { |
| 56 | struct bt_field common; |
| 57 | |
| 58 | union { |
| 59 | uint64_t u; |
| 60 | int64_t i; |
| 61 | } value; |
| 62 | }; |
| 63 | |
| 64 | struct bt_field_real { |
| 65 | struct bt_field common; |
| 66 | double value; |
| 67 | }; |
| 68 | |
| 69 | struct bt_field_structure { |
| 70 | struct bt_field common; |
| 71 | |
| 72 | /* Array of `struct bt_field *`, owned by this */ |
| 73 | GPtrArray *fields; |
| 74 | }; |
| 75 | |
| 76 | struct bt_field_option { |
| 77 | struct bt_field common; |
| 78 | |
| 79 | /* Owned by this */ |
| 80 | struct bt_field *content_field; |
| 81 | |
| 82 | /* Weak: equal to `content_field` above or `NULL` */ |
| 83 | struct bt_field *selected_field; |
| 84 | }; |
| 85 | |
| 86 | struct bt_field_variant { |
| 87 | struct bt_field common; |
| 88 | |
| 89 | /* Weak: belongs to `fields` below */ |
| 90 | struct bt_field *selected_field; |
| 91 | |
| 92 | /* Index of currently selected field */ |
| 93 | uint64_t selected_index; |
| 94 | |
| 95 | /* Array of `struct bt_field *`, owned by this */ |
| 96 | GPtrArray *fields; |
| 97 | }; |
| 98 | |
| 99 | struct bt_field_blob { |
| 100 | struct bt_field common; |
| 101 | |
| 102 | uint64_t length; |
| 103 | uint8_t *data; |
| 104 | }; |
| 105 | |
| 106 | struct bt_field_array { |
| 107 | struct bt_field common; |
| 108 | |
| 109 | /* Array of `struct bt_field *`, owned by this */ |
| 110 | GPtrArray *fields; |
| 111 | |
| 112 | /* Current effective length */ |
| 113 | uint64_t length; |
| 114 | }; |
| 115 | |
| 116 | struct bt_field_string { |
| 117 | struct bt_field common; |
| 118 | GArray *buf; |
| 119 | uint64_t length; |
| 120 | }; |
| 121 | |
| 122 | #ifdef BT_DEV_MODE |
| 123 | # define bt_field_set_is_frozen _bt_field_set_is_frozen |
| 124 | # define bt_field_is_set _bt_field_is_set |
| 125 | # define bt_field_reset _bt_field_reset |
| 126 | # define bt_field_set_single _bt_field_set_single |
| 127 | #else |
| 128 | # define bt_field_set_is_frozen(_field, _is_frozen) |
| 129 | # define bt_field_is_set(_field) (BT_FALSE) |
| 130 | # define bt_field_reset(_field) |
| 131 | # define bt_field_set_single(_field, _val) |
| 132 | #endif |
| 133 | |
| 134 | void _bt_field_set_is_frozen(const struct bt_field *field, bool is_frozen); |
| 135 | |
| 136 | static inline |
| 137 | void _bt_field_reset(const struct bt_field *field) |
| 138 | { |
| 139 | BT_ASSERT_DBG(field); |
| 140 | BT_ASSERT_DBG(field->methods->reset); |
| 141 | field->methods->reset((void *) field); |
| 142 | } |
| 143 | |
| 144 | static inline |
| 145 | void _bt_field_set_single(struct bt_field *field, bool value) |
| 146 | { |
| 147 | BT_ASSERT_DBG(field); |
| 148 | field->is_set = value; |
| 149 | } |
| 150 | |
| 151 | static inline |
| 152 | bt_bool _bt_field_is_set(const struct bt_field *field) |
| 153 | { |
| 154 | bt_bool is_set = BT_FALSE; |
| 155 | |
| 156 | if (!field) { |
| 157 | goto end; |
| 158 | } |
| 159 | |
| 160 | BT_ASSERT_DBG(field->methods->is_set); |
| 161 | is_set = field->methods->is_set(field); |
| 162 | |
| 163 | end: |
| 164 | return is_set; |
| 165 | } |
| 166 | |
| 167 | struct bt_field *bt_field_create(struct bt_field_class *class); |
| 168 | |
| 169 | void bt_field_destroy(struct bt_field *field); |
| 170 | |
| 171 | #endif /* BABELTRACE_LIB_TRACE_IR_FIELD_H */ |