Commit | Line | Data |
---|---|---|
108b91d0 PP |
1 | #ifndef BABELTRACE_TRACE_IR_FIELDS_INTERNAL_H |
2 | #define BABELTRACE_TRACE_IR_FIELDS_INTERNAL_H | |
273b65be JG |
3 | |
4 | /* | |
f2b0325d | 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 | ||
57952005 MJ |
27 | #include "lib/assert-pre.h" |
28 | #include "common/common.h" | |
29 | #include "lib/object.h" | |
85e7137b | 30 | #include "common/macros.h" |
71c5da58 | 31 | #include <babeltrace2/types.h> |
dc3fffef | 32 | #include <stdint.h> |
e42dd763 | 33 | #include <string.h> |
8deee039 | 34 | #include <inttypes.h> |
a745c331 | 35 | #include <stdbool.h> |
273b65be JG |
36 | #include <glib.h> |
37 | ||
57952005 MJ |
38 | #include "field-class.h" |
39 | #include "utils.h" | |
40 | ||
fa6cfec3 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), \ | |
af0c18e3 | 43 | _name " has the wrong class type: expected-class-type=%s, " \ |
7b33a0e0 | 44 | "%![field-]+f", \ |
af0c18e3 | 45 | bt_common_field_class_type_string(_cls_type), (_field)) |
7b33a0e0 | 46 | |
fa6cfec3 PP |
47 | #define BT_ASSERT_PRE_DEV_FIELD_IS_UNSIGNED_INT(_field, _name) \ |
48 | BT_ASSERT_PRE_DEV( \ | |
78cf9df6 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, \ | |
7b33a0e0 PP |
51 | _name " is not an unsigned integer field: %![field-]+f", \ |
52 | (_field)) | |
53 | ||
fa6cfec3 PP |
54 | #define BT_ASSERT_PRE_DEV_FIELD_IS_SIGNED_INT(_field, _name) \ |
55 | BT_ASSERT_PRE_DEV( \ | |
78cf9df6 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, \ | |
fa6cfec3 | 58 | _name " is not a signed integer field: %![field-]+f", \ |
7b33a0e0 PP |
59 | (_field)) |
60 | ||
fa6cfec3 PP |
61 | #define BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY(_field, _name) \ |
62 | BT_ASSERT_PRE_DEV( \ | |
78cf9df6 PP |
63 | ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_STATIC_ARRAY || \ |
64 | ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY, \ | |
7b33a0e0 PP |
65 | _name " is not an array field: %![field-]+f", (_field)) |
66 | ||
02b61fe0 PP |
67 | #define BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT(_field, _name) \ |
68 | BT_ASSERT_PRE_DEV( \ | |
69 | ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR || \ | |
70 | ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR || \ | |
71 | ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR, \ | |
72 | _name " is not a variant field: %![field-]+f", (_field)) | |
73 | ||
fa6cfec3 PP |
74 | #define BT_ASSERT_PRE_DEV_FIELD_IS_SET(_field, _name) \ |
75 | BT_ASSERT_PRE_DEV(bt_field_is_set(_field), \ | |
18acc6f8 | 76 | _name " is not set: %!+f", (_field)) |
8deee039 | 77 | |
fa6cfec3 PP |
78 | #define BT_ASSERT_PRE_DEV_FIELD_HOT(_field, _name) \ |
79 | BT_ASSERT_PRE_DEV_HOT((const struct bt_field *) (_field), (_name), \ | |
7b33a0e0 | 80 | ": %!+f", (_field)) |
8deee039 PP |
81 | |
82 | struct bt_field; | |
8deee039 | 83 | |
939190b3 | 84 | typedef struct bt_field *(* bt_field_create_func)(struct bt_field_class *); |
7b33a0e0 | 85 | typedef void (*bt_field_method_set_is_frozen)(struct bt_field *, bool); |
78cf9df6 | 86 | typedef bool (*bt_field_method_is_set)(const struct bt_field *); |
18acc6f8 PP |
87 | typedef void (*bt_field_method_reset)(struct bt_field *); |
88 | ||
89 | struct bt_field_methods { | |
90 | bt_field_method_set_is_frozen set_is_frozen; | |
18acc6f8 PP |
91 | bt_field_method_is_set is_set; |
92 | bt_field_method_reset reset; | |
8deee039 PP |
93 | }; |
94 | ||
18acc6f8 | 95 | struct bt_field { |
83509119 | 96 | struct bt_object base; |
7b33a0e0 PP |
97 | |
98 | /* Owned by this */ | |
939190b3 | 99 | struct bt_field_class *class; |
7b33a0e0 PP |
100 | |
101 | /* Virtual table for slow path (dev mode) operations */ | |
18acc6f8 | 102 | struct bt_field_methods *methods; |
7b33a0e0 PP |
103 | |
104 | bool is_set; | |
a745c331 | 105 | bool frozen; |
273b65be JG |
106 | }; |
107 | ||
9414b439 PP |
108 | struct bt_field_bool { |
109 | struct bt_field common; | |
110 | bool value; | |
111 | }; | |
112 | ||
6943da6a PP |
113 | struct bt_field_bit_array { |
114 | struct bt_field common; | |
115 | uint64_t value_as_int; | |
116 | }; | |
117 | ||
18acc6f8 PP |
118 | struct bt_field_integer { |
119 | struct bt_field common; | |
273b65be | 120 | |
7b33a0e0 PP |
121 | union { |
122 | uint64_t u; | |
123 | int64_t i; | |
124 | } value; | |
18acc6f8 PP |
125 | }; |
126 | ||
7b33a0e0 | 127 | struct bt_field_real { |
18acc6f8 | 128 | struct bt_field common; |
7b33a0e0 | 129 | double value; |
273b65be JG |
130 | }; |
131 | ||
18acc6f8 PP |
132 | struct bt_field_structure { |
133 | struct bt_field common; | |
a6918753 | 134 | |
18acc6f8 | 135 | /* Array of `struct bt_field *`, owned by this */ |
a6918753 | 136 | GPtrArray *fields; |
273b65be JG |
137 | }; |
138 | ||
247fa9e3 PP |
139 | struct bt_field_option { |
140 | struct bt_field common; | |
141 | ||
142 | /* Owned by this */ | |
143 | struct bt_field *content_field; | |
144 | ||
145 | /* Weak: equal to `content_field` above or `NULL` */ | |
146 | struct bt_field *selected_field; | |
147 | }; | |
148 | ||
18acc6f8 PP |
149 | struct bt_field_variant { |
150 | struct bt_field common; | |
a6918753 | 151 | |
7b33a0e0 PP |
152 | /* Weak: belongs to `fields` below */ |
153 | struct bt_field *selected_field; | |
a6918753 | 154 | |
7b33a0e0 PP |
155 | /* Index of currently selected field */ |
156 | uint64_t selected_index; | |
a6918753 | 157 | |
18acc6f8 | 158 | /* Array of `struct bt_field *`, owned by this */ |
a6918753 | 159 | GPtrArray *fields; |
273b65be JG |
160 | }; |
161 | ||
18acc6f8 PP |
162 | struct bt_field_array { |
163 | struct bt_field common; | |
a6918753 | 164 | |
18acc6f8 | 165 | /* Array of `struct bt_field *`, owned by this */ |
7b33a0e0 | 166 | GPtrArray *fields; |
a6918753 | 167 | |
7b33a0e0 | 168 | /* Current effective length */ |
a6918753 | 169 | uint64_t length; |
273b65be JG |
170 | }; |
171 | ||
18acc6f8 PP |
172 | struct bt_field_string { |
173 | struct bt_field common; | |
e42dd763 | 174 | GArray *buf; |
7b33a0e0 | 175 | uint64_t length; |
273b65be JG |
176 | }; |
177 | ||
8b45963b | 178 | #ifdef BT_DEV_MODE |
7b33a0e0 PP |
179 | # define bt_field_set_is_frozen _bt_field_set_is_frozen |
180 | # define bt_field_is_set _bt_field_is_set | |
181 | # define bt_field_reset _bt_field_reset | |
182 | # define bt_field_set_single _bt_field_set_single | |
8b45963b | 183 | #else |
7b33a0e0 PP |
184 | # define bt_field_set_is_frozen(_field, _is_frozen) |
185 | # define bt_field_is_set(_field) (BT_FALSE) | |
186 | # define bt_field_reset(_field) | |
187 | # define bt_field_set_single(_field, _val) | |
8b45963b | 188 | #endif |
918be005 | 189 | |
18acc6f8 | 190 | BT_HIDDEN |
78cf9df6 | 191 | void _bt_field_set_is_frozen(const struct bt_field *field, bool is_frozen); |
8deee039 PP |
192 | |
193 | static inline | |
78cf9df6 | 194 | void _bt_field_reset(const struct bt_field *field) |
8deee039 PP |
195 | { |
196 | BT_ASSERT(field); | |
197 | BT_ASSERT(field->methods->reset); | |
78cf9df6 | 198 | field->methods->reset((void *) field); |
8deee039 PP |
199 | } |
200 | ||
201 | static inline | |
7b33a0e0 | 202 | void _bt_field_set_single(struct bt_field *field, bool value) |
8deee039 PP |
203 | { |
204 | BT_ASSERT(field); | |
7b33a0e0 | 205 | field->is_set = value; |
8deee039 PP |
206 | } |
207 | ||
208 | static inline | |
78cf9df6 | 209 | bt_bool _bt_field_is_set(const struct bt_field *field) |
8deee039 PP |
210 | { |
211 | bt_bool is_set = BT_FALSE; | |
212 | ||
213 | if (!field) { | |
214 | goto end; | |
215 | } | |
216 | ||
8deee039 PP |
217 | BT_ASSERT(field->methods->is_set); |
218 | is_set = field->methods->is_set(field); | |
219 | ||
220 | end: | |
221 | return is_set; | |
222 | } | |
223 | ||
a6918753 | 224 | BT_HIDDEN |
939190b3 | 225 | struct bt_field *bt_field_create(struct bt_field_class *class); |
a6918753 PP |
226 | |
227 | BT_HIDDEN | |
7b33a0e0 | 228 | void bt_field_destroy(struct bt_field *field); |
a6918753 | 229 | |
108b91d0 | 230 | #endif /* BABELTRACE_TRACE_IR_FIELDS_INTERNAL_H */ |