lib: merge common CTF IR part with the remaining implementation
[babeltrace.git] / include / babeltrace / ctf-ir / fields-internal.h
CommitLineData
2e33ac5a
PP
1#ifndef BABELTRACE_CTF_IR_FIELDS_INTERNAL_H
2#define BABELTRACE_CTF_IR_FIELDS_INTERNAL_H
273b65be
JG
3
4/*
2e33ac5a 5 * Babeltrace - CTF IR: Event Fields internal
273b65be 6 *
de9dd397 7 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be
JG
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
3dca2276
PP
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>
83509119 34#include <babeltrace/object-internal.h>
273b65be 35#include <babeltrace/babeltrace-internal.h>
c55a9f58 36#include <babeltrace/types.h>
dc3fffef 37#include <stdint.h>
4d4b475d 38#include <string.h>
3dca2276 39#include <inttypes.h>
d990a4fb 40#include <stdbool.h>
273b65be
JG
41#include <glib.h>
42
cb6f1f7d 43#define BT_ASSERT_PRE_FIELD_HAS_TYPE_ID(_field, _type_id, _name) \
312c056a 44 BT_ASSERT_PRE((_field)->type->id == ((int) (_type_id)), \
3dca2276 45 _name " has the wrong type ID: expected-type-id=%s, " \
cb6f1f7d 46 "%![field-]+f", \
312c056a 47 bt_common_field_type_id_string((int) (_type_id)), (_field))
dc3fffef 48
cb6f1f7d
PP
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))
3dca2276 52
cb6f1f7d
PP
53#define BT_ASSERT_PRE_FIELD_HOT(_field, _name) \
54 BT_ASSERT_PRE_HOT((_field), (_name), ": %!+f", (_field))
3dca2276
PP
55
56struct bt_field;
3dca2276 57
cb6f1f7d 58typedef void (*bt_field_method_set_is_frozen)(struct bt_field *,
312c056a 59 bool);
cb6f1f7d
PP
60typedef int (*bt_field_method_validate)(struct bt_field *);
61typedef bt_bool (*bt_field_method_is_set)(struct bt_field *);
62typedef void (*bt_field_method_reset)(struct bt_field *);
63
64struct 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;
3dca2276
PP
69};
70
cb6f1f7d 71struct bt_field {
83509119 72 struct bt_object base;
cb6f1f7d
PP
73 struct bt_field_type *type;
74 struct bt_field_methods *methods;
d990a4fb
JG
75 bool payload_set;
76 bool frozen;
273b65be
JG
77};
78
cb6f1f7d
PP
79struct bt_field_integer {
80 struct bt_field common;
dc3fffef
PP
81 union {
82 int64_t signd;
83 uint64_t unsignd;
84 } payload;
273b65be
JG
85};
86
cb6f1f7d
PP
87struct bt_field_enumeration {
88 struct bt_field_integer common;
89};
90
91struct bt_field_floating_point {
92 struct bt_field common;
dc3fffef 93 double payload;
273b65be
JG
94};
95
cb6f1f7d
PP
96struct bt_field_structure {
97 struct bt_field common;
312c056a 98
cb6f1f7d 99 /* Array of `struct bt_field *`, owned by this */
312c056a 100 GPtrArray *fields;
273b65be
JG
101};
102
cb6f1f7d
PP
103struct bt_field_variant {
104 struct bt_field common;
312c056a
PP
105
106 union {
107 uint64_t u;
108 int64_t i;
109 } tag_value;
110
111 /* Weak: belongs to `choices` below */
cb6f1f7d 112 struct bt_field *current_field;
312c056a 113
cb6f1f7d 114 /* Array of `struct bt_field *`, owned by this */
312c056a 115 GPtrArray *fields;
273b65be
JG
116};
117
cb6f1f7d
PP
118struct bt_field_array {
119 struct bt_field common;
312c056a 120
cb6f1f7d 121 /* Array of `struct bt_field *`, owned by this */
312c056a 122 GPtrArray *elements;
273b65be
JG
123};
124
cb6f1f7d
PP
125struct bt_field_sequence {
126 struct bt_field common;
312c056a
PP
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
cb6f1f7d 135 /* Array of `struct bt_field *`, owned by this */
312c056a 136 GPtrArray *elements;
273b65be
JG
137};
138
cb6f1f7d
PP
139struct bt_field_string {
140 struct bt_field common;
4d4b475d
PP
141 GArray *buf;
142 size_t size;
273b65be
JG
143};
144
f6ccaed9 145#ifdef BT_DEV_MODE
312c056a
PP
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
f6ccaed9 151#else
3dca2276 152# define bt_field_validate_recursive(_field) (-1)
312c056a 153# define bt_field_set_is_frozen_recursive(_field, _is_frozen)
3dca2276 154# define bt_field_is_set_recursive(_field) (BT_FALSE)
565b6706 155# define bt_field_reset_recursive(_field)
f6ccaed9
PP
156# define bt_field_set(_field, _val)
157#endif
918be005 158
cb6f1f7d
PP
159BT_HIDDEN
160void _bt_field_set_is_frozen_recursive(struct bt_field *field,
161 bool is_frozen);
3dca2276
PP
162
163static inline
cb6f1f7d 164int _bt_field_validate_recursive(struct bt_field *field)
3dca2276
PP
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
cb6f1f7d 174 BT_ASSERT(bt_field_type_has_known_id(field->type));
3dca2276
PP
175
176 if (field->methods->validate) {
177 ret = field->methods->validate(field);
178 }
179
180end:
181 return ret;
182}
183
184static inline
cb6f1f7d 185void _bt_field_reset_recursive(struct bt_field *field)
3dca2276
PP
186{
187 BT_ASSERT(field);
188 BT_ASSERT(field->methods->reset);
189 field->methods->reset(field);
190}
191
192static inline
cb6f1f7d 193void _bt_field_set(struct bt_field *field, bool value)
3dca2276
PP
194{
195 BT_ASSERT(field);
196 field->payload_set = value;
197}
198
199static inline
cb6f1f7d 200bt_bool _bt_field_is_set_recursive(struct bt_field *field)
3dca2276
PP
201{
202 bt_bool is_set = BT_FALSE;
203
204 if (!field) {
205 goto end;
206 }
207
cb6f1f7d 208 BT_ASSERT(bt_field_type_has_known_id(field->type));
3dca2276
PP
209 BT_ASSERT(field->methods->is_set);
210 is_set = field->methods->is_set(field);
211
212end:
213 return is_set;
214}
215
312c056a
PP
216BT_HIDDEN
217struct bt_field *bt_field_create_recursive(struct bt_field_type *type);
218
219BT_HIDDEN
220void bt_field_destroy_recursive(struct bt_field *field);
221
2e33ac5a 222#endif /* BABELTRACE_CTF_IR_FIELDS_INTERNAL_H */
This page took 0.068494 seconds and 4 git commands to generate.