Add internal bt_ctf_field_type_id_string()/byte_order_string()
[babeltrace.git] / include / babeltrace / ctf-ir / field-types-internal.h
1 #ifndef BABELTRACE_CTF_IR_FIELD_TYPES_INTERNAL_H
2 #define BABELTRACE_CTF_IR_FIELD_TYPES_INTERNAL_H
3
4 /*
5 * BabelTrace - CTF IR: Event field types 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 <stdint.h>
31 #include <babeltrace/ctf-writer/event-types.h>
32 #include <babeltrace/ctf-writer/event-fields.h>
33 #include <babeltrace/ctf-writer/writer.h>
34 #include <babeltrace/ctf-ir/trace-internal.h>
35 #include <babeltrace/ctf-ir/clock-class.h>
36 #include <babeltrace/babeltrace-internal.h>
37 #include <babeltrace/object-internal.h>
38 #include <babeltrace/types.h>
39 #include <glib.h>
40
41 typedef void (*type_freeze_func)(struct bt_ctf_field_type *);
42 typedef int (*type_serialize_func)(struct bt_ctf_field_type *,
43 struct metadata_context *);
44
45 struct bt_ctf_field_type {
46 struct bt_object base;
47 enum bt_ctf_field_type_id id;
48 unsigned int alignment;
49 type_freeze_func freeze;
50 type_serialize_func serialize;
51 /*
52 * A type can't be modified once it is added to an event or after a
53 * a field has been instanciated from it.
54 */
55 int frozen;
56
57 /*
58 * This flag indicates if the field type is valid. A valid
59 * field type is _always_ frozen. All the nested field types of
60 * a valid field type are also valid (and thus frozen).
61 */
62 int valid;
63 };
64
65 struct bt_ctf_field_type_integer {
66 struct bt_ctf_field_type parent;
67 struct bt_ctf_clock_class *mapped_clock;
68 enum bt_ctf_byte_order user_byte_order;
69 bt_bool is_signed;
70 unsigned int size;
71 enum bt_ctf_integer_base base;
72 enum bt_ctf_string_encoding encoding;
73 };
74
75 struct enumeration_mapping {
76 union {
77 uint64_t _unsigned;
78 int64_t _signed;
79 } range_start;
80
81 union {
82 uint64_t _unsigned;
83 int64_t _signed;
84 } range_end;
85 GQuark string;
86 };
87
88 struct bt_ctf_field_type_enumeration {
89 struct bt_ctf_field_type parent;
90 struct bt_ctf_field_type *container;
91 GPtrArray *entries; /* Array of ptrs to struct enumeration_mapping */
92 /* Only set during validation. */
93 bt_bool has_overlapping_ranges;
94 };
95
96 enum bt_ctf_field_type_enumeration_mapping_iterator_type {
97 ITERATOR_BY_NAME,
98 ITERATOR_BY_SIGNED_VALUE,
99 ITERATOR_BY_UNSIGNED_VALUE,
100 };
101
102 struct bt_ctf_field_type_enumeration_mapping_iterator {
103 struct bt_object base;
104 struct bt_ctf_field_type_enumeration *enumeration_type;
105 enum bt_ctf_field_type_enumeration_mapping_iterator_type type;
106 int index;
107 union {
108 GQuark name_quark;
109 int64_t signed_value;
110 uint64_t unsigned_value;
111 } u;
112 };
113
114 struct bt_ctf_field_type_floating_point {
115 struct bt_ctf_field_type parent;
116 enum bt_ctf_byte_order user_byte_order;
117 unsigned int exp_dig;
118 unsigned int mant_dig;
119 };
120
121 struct structure_field {
122 GQuark name;
123 struct bt_ctf_field_type *type;
124 };
125
126 struct bt_ctf_field_type_structure {
127 struct bt_ctf_field_type parent;
128 GHashTable *field_name_to_index;
129 GPtrArray *fields; /* Array of pointers to struct structure_field */
130 };
131
132 struct bt_ctf_field_type_variant {
133 struct bt_ctf_field_type parent;
134 GString *tag_name;
135 struct bt_ctf_field_type_enumeration *tag;
136 struct bt_ctf_field_path *tag_field_path;
137 GHashTable *field_name_to_index;
138 GPtrArray *fields; /* Array of pointers to struct structure_field */
139 };
140
141 struct bt_ctf_field_type_array {
142 struct bt_ctf_field_type parent;
143 struct bt_ctf_field_type *element_type;
144 unsigned int length; /* Number of elements */
145 };
146
147 struct bt_ctf_field_type_sequence {
148 struct bt_ctf_field_type parent;
149 struct bt_ctf_field_type *element_type;
150 GString *length_field_name;
151 struct bt_ctf_field_path *length_field_path;
152 };
153
154 struct bt_ctf_field_type_string {
155 struct bt_ctf_field_type parent;
156 enum bt_ctf_string_encoding encoding;
157 };
158
159 BT_HIDDEN
160 void bt_ctf_field_type_freeze(struct bt_ctf_field_type *type);
161
162 BT_HIDDEN
163 struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_signed(
164 struct bt_ctf_field_type_variant *variant, int64_t tag_value);
165
166 BT_HIDDEN
167 struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_unsigned(
168 struct bt_ctf_field_type_variant *variant, uint64_t tag_value);
169
170 BT_HIDDEN
171 int bt_ctf_field_type_serialize(struct bt_ctf_field_type *type,
172 struct metadata_context *context);
173
174 BT_HIDDEN
175 int bt_ctf_field_type_validate(struct bt_ctf_field_type *type);
176
177 BT_HIDDEN
178 int bt_ctf_field_type_structure_get_field_name_index(
179 struct bt_ctf_field_type *structure, const char *name);
180
181 /* Replace an existing field's type in a structure */
182 BT_HIDDEN
183 int bt_ctf_field_type_structure_set_field_index(
184 struct bt_ctf_field_type *structure,
185 struct bt_ctf_field_type *field, int index);
186
187 BT_HIDDEN
188 int bt_ctf_field_type_variant_get_field_name_index(
189 struct bt_ctf_field_type *variant, const char *name);
190
191 BT_HIDDEN
192 int bt_ctf_field_type_sequence_set_length_field_path(
193 struct bt_ctf_field_type *type,
194 struct bt_ctf_field_path *path);
195
196 BT_HIDDEN
197 int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type *type,
198 struct bt_ctf_field_path *path);
199
200 BT_HIDDEN
201 int bt_ctf_field_type_variant_set_tag_field_type(struct bt_ctf_field_type *type,
202 struct bt_ctf_field_type *tag_type);
203
204 /* Replace an existing field's type in a variant */
205 BT_HIDDEN
206 int bt_ctf_field_type_variant_set_field_index(
207 struct bt_ctf_field_type *variant,
208 struct bt_ctf_field_type *field, int index);
209
210 BT_HIDDEN
211 int bt_ctf_field_type_array_set_element_type(struct bt_ctf_field_type *array,
212 struct bt_ctf_field_type *element_type);
213
214 BT_HIDDEN
215 int bt_ctf_field_type_sequence_set_element_type(struct bt_ctf_field_type *array,
216 struct bt_ctf_field_type *element_type);
217
218 BT_HIDDEN
219 int64_t bt_ctf_field_type_get_field_count(struct bt_ctf_field_type *type);
220
221 BT_HIDDEN
222 struct bt_ctf_field_type *bt_ctf_field_type_get_field_at_index(
223 struct bt_ctf_field_type *type, int index);
224
225 BT_HIDDEN
226 int bt_ctf_field_type_get_field_index(struct bt_ctf_field_type *type,
227 const char *name);
228
229 static inline
230 const char *bt_ctf_field_type_id_string(enum bt_ctf_field_type_id type_id)
231 {
232 switch (type_id) {
233 case BT_CTF_FIELD_TYPE_ID_UNKNOWN:
234 return "BT_CTF_FIELD_TYPE_ID_UNKNOWN";
235 case BT_CTF_FIELD_TYPE_ID_INTEGER:
236 return "BT_CTF_FIELD_TYPE_ID_INTEGER";
237 case BT_CTF_FIELD_TYPE_ID_FLOAT:
238 return "BT_CTF_FIELD_TYPE_ID_FLOAT";
239 case BT_CTF_FIELD_TYPE_ID_ENUM:
240 return "BT_CTF_FIELD_TYPE_ID_ENUM";
241 case BT_CTF_FIELD_TYPE_ID_STRING:
242 return "BT_CTF_FIELD_TYPE_ID_STRING";
243 case BT_CTF_FIELD_TYPE_ID_STRUCT:
244 return "BT_CTF_FIELD_TYPE_ID_STRUCT";
245 case BT_CTF_FIELD_TYPE_ID_ARRAY:
246 return "BT_CTF_FIELD_TYPE_ID_ARRAY";
247 case BT_CTF_FIELD_TYPE_ID_SEQUENCE:
248 return "BT_CTF_FIELD_TYPE_ID_SEQUENCE";
249 case BT_CTF_FIELD_TYPE_ID_VARIANT:
250 return "BT_CTF_FIELD_TYPE_ID_VARIANT";
251 default:
252 return "(unknown)";
253 }
254 };
255
256 static inline
257 const char *bt_ctf_field_type_byte_order_string(enum bt_ctf_byte_order bo)
258 {
259 switch (bo) {
260 case BT_CTF_BYTE_ORDER_UNKNOWN:
261 return "BT_CTF_BYTE_ORDER_UNKNOWN";
262 case BT_CTF_BYTE_ORDER_NATIVE:
263 return "BT_CTF_BYTE_ORDER_NATIVE";
264 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
265 return "BT_CTF_BYTE_ORDER_LITTLE_ENDIAN";
266 case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
267 return "BT_CTF_BYTE_ORDER_BIG_ENDIAN";
268 case BT_CTF_BYTE_ORDER_NETWORK:
269 return "BT_CTF_BYTE_ORDER_NETWORK";
270 default:
271 return "(unknown)";
272 }
273 };
274
275 #endif /* BABELTRACE_CTF_IR_FIELD_TYPES_INTERNAL_H */
This page took 0.035845 seconds and 5 git commands to generate.