lib: merge common CTF IR part with the remaining implementation
[babeltrace.git] / include / babeltrace / ctf-ir / fields-internal.h
1 #ifndef BABELTRACE_CTF_IR_FIELDS_INTERNAL_H
2 #define BABELTRACE_CTF_IR_FIELDS_INTERNAL_H
3
4 /*
5 * Babeltrace - CTF IR: Event Fields internal
6 *
7 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 *
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30 #include <babeltrace/assert-pre-internal.h>
31 #include <babeltrace/common-internal.h>
32 #include <babeltrace/ctf-ir/field-types-internal.h>
33 #include <babeltrace/ctf-ir/utils-internal.h>
34 #include <babeltrace/object-internal.h>
35 #include <babeltrace/babeltrace-internal.h>
36 #include <babeltrace/types.h>
37 #include <stdint.h>
38 #include <string.h>
39 #include <inttypes.h>
40 #include <stdbool.h>
41 #include <glib.h>
42
43 #define BT_ASSERT_PRE_FIELD_HAS_TYPE_ID(_field, _type_id, _name) \
44 BT_ASSERT_PRE((_field)->type->id == ((int) (_type_id)), \
45 _name " has the wrong type ID: expected-type-id=%s, " \
46 "%![field-]+f", \
47 bt_common_field_type_id_string((int) (_type_id)), (_field))
48
49 #define BT_ASSERT_PRE_FIELD_IS_SET(_field, _name) \
50 BT_ASSERT_PRE(bt_field_is_set_recursive(_field), \
51 _name " is not set: %!+f", (_field))
52
53 #define BT_ASSERT_PRE_FIELD_HOT(_field, _name) \
54 BT_ASSERT_PRE_HOT((_field), (_name), ": %!+f", (_field))
55
56 struct bt_field;
57
58 typedef void (*bt_field_method_set_is_frozen)(struct bt_field *,
59 bool);
60 typedef int (*bt_field_method_validate)(struct bt_field *);
61 typedef bt_bool (*bt_field_method_is_set)(struct bt_field *);
62 typedef void (*bt_field_method_reset)(struct bt_field *);
63
64 struct bt_field_methods {
65 bt_field_method_set_is_frozen set_is_frozen;
66 bt_field_method_validate validate;
67 bt_field_method_is_set is_set;
68 bt_field_method_reset reset;
69 };
70
71 struct bt_field {
72 struct bt_object base;
73 struct bt_field_type *type;
74 struct bt_field_methods *methods;
75 bool payload_set;
76 bool frozen;
77 };
78
79 struct bt_field_integer {
80 struct bt_field common;
81 union {
82 int64_t signd;
83 uint64_t unsignd;
84 } payload;
85 };
86
87 struct bt_field_enumeration {
88 struct bt_field_integer common;
89 };
90
91 struct bt_field_floating_point {
92 struct bt_field common;
93 double payload;
94 };
95
96 struct bt_field_structure {
97 struct bt_field common;
98
99 /* Array of `struct bt_field *`, owned by this */
100 GPtrArray *fields;
101 };
102
103 struct bt_field_variant {
104 struct bt_field common;
105
106 union {
107 uint64_t u;
108 int64_t i;
109 } tag_value;
110
111 /* Weak: belongs to `choices` below */
112 struct bt_field *current_field;
113
114 /* Array of `struct bt_field *`, owned by this */
115 GPtrArray *fields;
116 };
117
118 struct bt_field_array {
119 struct bt_field common;
120
121 /* Array of `struct bt_field *`, owned by this */
122 GPtrArray *elements;
123 };
124
125 struct bt_field_sequence {
126 struct bt_field common;
127
128 /*
129 * This is the true sequence field's length: its value can be
130 * less than `elements->len` below because we never shrink the
131 * array of elements to avoid reallocation.
132 */
133 uint64_t length;
134
135 /* Array of `struct bt_field *`, owned by this */
136 GPtrArray *elements;
137 };
138
139 struct bt_field_string {
140 struct bt_field common;
141 GArray *buf;
142 size_t size;
143 };
144
145 #ifdef BT_DEV_MODE
146 # define bt_field_validate_recursive _bt_field_validate_recursive
147 # define bt_field_set_is_frozen_recursive _bt_field_set_is_frozen_recursive
148 # define bt_field_is_set_recursive _bt_field_is_set_recursive
149 # define bt_field_reset_recursive _bt_field_reset_recursive
150 # define bt_field_set _bt_field_set
151 #else
152 # define bt_field_validate_recursive(_field) (-1)
153 # define bt_field_set_is_frozen_recursive(_field, _is_frozen)
154 # define bt_field_is_set_recursive(_field) (BT_FALSE)
155 # define bt_field_reset_recursive(_field)
156 # define bt_field_set(_field, _val)
157 #endif
158
159 BT_HIDDEN
160 void _bt_field_set_is_frozen_recursive(struct bt_field *field,
161 bool is_frozen);
162
163 static inline
164 int _bt_field_validate_recursive(struct bt_field *field)
165 {
166 int ret = 0;
167
168 if (!field) {
169 BT_ASSERT_PRE_MSG("%s", "Invalid field: field is NULL.");
170 ret = -1;
171 goto end;
172 }
173
174 BT_ASSERT(bt_field_type_has_known_id(field->type));
175
176 if (field->methods->validate) {
177 ret = field->methods->validate(field);
178 }
179
180 end:
181 return ret;
182 }
183
184 static inline
185 void _bt_field_reset_recursive(struct bt_field *field)
186 {
187 BT_ASSERT(field);
188 BT_ASSERT(field->methods->reset);
189 field->methods->reset(field);
190 }
191
192 static inline
193 void _bt_field_set(struct bt_field *field, bool value)
194 {
195 BT_ASSERT(field);
196 field->payload_set = value;
197 }
198
199 static inline
200 bt_bool _bt_field_is_set_recursive(struct bt_field *field)
201 {
202 bt_bool is_set = BT_FALSE;
203
204 if (!field) {
205 goto end;
206 }
207
208 BT_ASSERT(bt_field_type_has_known_id(field->type));
209 BT_ASSERT(field->methods->is_set);
210 is_set = field->methods->is_set(field);
211
212 end:
213 return is_set;
214 }
215
216 BT_HIDDEN
217 struct bt_field *bt_field_create_recursive(struct bt_field_type *type);
218
219 BT_HIDDEN
220 void bt_field_destroy_recursive(struct bt_field *field);
221
222 #endif /* BABELTRACE_CTF_IR_FIELDS_INTERNAL_H */
This page took 0.033592 seconds and 4 git commands to generate.