Make API CTF-agnostic
[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) \
44c440bc 44 BT_ASSERT_PRE(((struct bt_field *) (_field))->type->id == (_type_id), \
3dca2276 45 _name " has the wrong type ID: expected-type-id=%s, " \
44c440bc
PP
46 "%![field-]+f", \
47 bt_common_field_type_id_string(_type_id), (_field))
48
49#define BT_ASSERT_PRE_FIELD_IS_UNSIGNED_INT(_field, _name) \
50 BT_ASSERT_PRE( \
51 ((struct bt_field *) (_field))->type->id == BT_FIELD_TYPE_ID_UNSIGNED_INTEGER || \
52 ((struct bt_field *) (_field))->type->id == BT_FIELD_TYPE_ID_UNSIGNED_ENUMERATION, \
53 _name " is not an unsigned integer field: %![field-]+f", \
54 (_field))
55
56#define BT_ASSERT_PRE_FIELD_IS_SIGNED_INT(_field, _name) \
57 BT_ASSERT_PRE( \
58 ((struct bt_field *) (_field))->type->id == BT_FIELD_TYPE_ID_SIGNED_INTEGER || \
59 ((struct bt_field *) (_field))->type->id == BT_FIELD_TYPE_ID_SIGNED_ENUMERATION, \
60 _name " is not a signed integer field: %![field-]+f", \
61 (_field))
62
63#define BT_ASSERT_PRE_FIELD_IS_ARRAY(_field, _name) \
64 BT_ASSERT_PRE( \
65 ((struct bt_field *) (_field))->type->id == BT_FIELD_TYPE_ID_STATIC_ARRAY || \
66 ((struct bt_field *) (_field))->type->id == BT_FIELD_TYPE_ID_DYNAMIC_ARRAY, \
67 _name " is not an array field: %![field-]+f", (_field))
68
69#define BT_ASSERT_PRE_FIELD_IS_SET(_field, _name) \
70 BT_ASSERT_PRE(bt_field_is_set(_field), \
cb6f1f7d 71 _name " is not set: %!+f", (_field))
3dca2276 72
44c440bc
PP
73#define BT_ASSERT_PRE_FIELD_HOT(_field, _name) \
74 BT_ASSERT_PRE_HOT((struct bt_field *) (_field), (_name), \
75 ": %!+f", (_field))
3dca2276
PP
76
77struct bt_field;
3dca2276 78
44c440bc
PP
79typedef struct bt_field *(* bt_field_create_func)(struct bt_field_type *);
80typedef void (*bt_field_method_set_is_frozen)(struct bt_field *, bool);
81typedef bool (*bt_field_method_is_set)(struct bt_field *);
cb6f1f7d
PP
82typedef void (*bt_field_method_reset)(struct bt_field *);
83
84struct bt_field_methods {
85 bt_field_method_set_is_frozen set_is_frozen;
cb6f1f7d
PP
86 bt_field_method_is_set is_set;
87 bt_field_method_reset reset;
3dca2276
PP
88};
89
cb6f1f7d 90struct bt_field {
83509119 91 struct bt_object base;
44c440bc
PP
92
93 /* Owned by this */
cb6f1f7d 94 struct bt_field_type *type;
44c440bc
PP
95
96 /* Virtual table for slow path (dev mode) operations */
cb6f1f7d 97 struct bt_field_methods *methods;
44c440bc
PP
98
99 bool is_set;
d990a4fb 100 bool frozen;
273b65be
JG
101};
102
cb6f1f7d
PP
103struct bt_field_integer {
104 struct bt_field common;
273b65be 105
44c440bc
PP
106 union {
107 uint64_t u;
108 int64_t i;
109 } value;
cb6f1f7d
PP
110};
111
44c440bc 112struct bt_field_real {
cb6f1f7d 113 struct bt_field common;
44c440bc 114 double value;
273b65be
JG
115};
116
cb6f1f7d
PP
117struct bt_field_structure {
118 struct bt_field common;
312c056a 119
cb6f1f7d 120 /* Array of `struct bt_field *`, owned by this */
312c056a 121 GPtrArray *fields;
273b65be
JG
122};
123
cb6f1f7d
PP
124struct bt_field_variant {
125 struct bt_field common;
312c056a 126
44c440bc
PP
127 /* Weak: belongs to `fields` below */
128 struct bt_field *selected_field;
312c056a 129
44c440bc
PP
130 /* Index of currently selected field */
131 uint64_t selected_index;
312c056a 132
cb6f1f7d 133 /* Array of `struct bt_field *`, owned by this */
312c056a 134 GPtrArray *fields;
273b65be
JG
135};
136
cb6f1f7d
PP
137struct bt_field_array {
138 struct bt_field common;
312c056a 139
cb6f1f7d 140 /* Array of `struct bt_field *`, owned by this */
44c440bc 141 GPtrArray *fields;
312c056a 142
44c440bc 143 /* Current effective length */
312c056a 144 uint64_t length;
273b65be
JG
145};
146
cb6f1f7d
PP
147struct bt_field_string {
148 struct bt_field common;
4d4b475d 149 GArray *buf;
44c440bc 150 uint64_t length;
273b65be
JG
151};
152
f6ccaed9 153#ifdef BT_DEV_MODE
44c440bc
PP
154# define bt_field_set_is_frozen _bt_field_set_is_frozen
155# define bt_field_is_set _bt_field_is_set
156# define bt_field_reset _bt_field_reset
157# define bt_field_set_single _bt_field_set_single
f6ccaed9 158#else
44c440bc
PP
159# define bt_field_set_is_frozen(_field, _is_frozen)
160# define bt_field_is_set(_field) (BT_FALSE)
161# define bt_field_reset(_field)
162# define bt_field_set_single(_field, _val)
f6ccaed9 163#endif
918be005 164
cb6f1f7d 165BT_HIDDEN
44c440bc 166void _bt_field_set_is_frozen(struct bt_field *field, bool is_frozen);
3dca2276
PP
167
168static inline
44c440bc 169void _bt_field_reset(struct bt_field *field)
3dca2276
PP
170{
171 BT_ASSERT(field);
172 BT_ASSERT(field->methods->reset);
173 field->methods->reset(field);
174}
175
176static inline
44c440bc 177void _bt_field_set_single(struct bt_field *field, bool value)
3dca2276
PP
178{
179 BT_ASSERT(field);
44c440bc 180 field->is_set = value;
3dca2276
PP
181}
182
183static inline
44c440bc 184bt_bool _bt_field_is_set(struct bt_field *field)
3dca2276
PP
185{
186 bt_bool is_set = BT_FALSE;
187
188 if (!field) {
189 goto end;
190 }
191
cb6f1f7d 192 BT_ASSERT(bt_field_type_has_known_id(field->type));
3dca2276
PP
193 BT_ASSERT(field->methods->is_set);
194 is_set = field->methods->is_set(field);
195
196end:
197 return is_set;
198}
199
312c056a 200BT_HIDDEN
44c440bc 201struct bt_field *bt_field_create(struct bt_field_type *type);
312c056a
PP
202
203BT_HIDDEN
44c440bc 204void bt_field_destroy(struct bt_field *field);
312c056a 205
2e33ac5a 206#endif /* BABELTRACE_CTF_IR_FIELDS_INTERNAL_H */
This page took 0.05653 seconds and 4 git commands to generate.