lib: make trace IR API const-correct
[babeltrace.git] / include / babeltrace / trace-ir / fields-internal.h
CommitLineData
56e18c4c
PP
1#ifndef BABELTRACE_TRACE_IR_FIELDS_INTERNAL_H
2#define BABELTRACE_TRACE_IR_FIELDS_INTERNAL_H
273b65be
JG
3
4/*
de9dd397 5 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be
JG
6 *
7 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
3dca2276
PP
28#include <babeltrace/assert-pre-internal.h>
29#include <babeltrace/common-internal.h>
5cd6d0e5 30#include <babeltrace/trace-ir/field-classes-internal.h>
56e18c4c 31#include <babeltrace/trace-ir/utils-internal.h>
83509119 32#include <babeltrace/object-internal.h>
273b65be 33#include <babeltrace/babeltrace-internal.h>
c55a9f58 34#include <babeltrace/types.h>
dc3fffef 35#include <stdint.h>
4d4b475d 36#include <string.h>
3dca2276 37#include <inttypes.h>
d990a4fb 38#include <stdbool.h>
273b65be
JG
39#include <glib.h>
40
864cad70 41#define BT_ASSERT_PRE_FIELD_HAS_CLASS_TYPE(_field, _cls_type, _name) \
40f4ba76 42 BT_ASSERT_PRE(((const struct bt_field *) (_field))->class->type == (_cls_type), \
864cad70 43 _name " has the wrong class type: expected-class-type=%s, " \
44c440bc 44 "%![field-]+f", \
864cad70 45 bt_common_field_class_type_string(_cls_type), (_field))
44c440bc
PP
46
47#define BT_ASSERT_PRE_FIELD_IS_UNSIGNED_INT(_field, _name) \
48 BT_ASSERT_PRE( \
40f4ba76
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, \
44c440bc
PP
51 _name " is not an unsigned integer field: %![field-]+f", \
52 (_field))
53
54#define BT_ASSERT_PRE_FIELD_IS_SIGNED_INT(_field, _name) \
55 BT_ASSERT_PRE( \
40f4ba76
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, \
44c440bc
PP
58 _name " is not a signed integer field: %![field-]+f", \
59 (_field))
60
61#define BT_ASSERT_PRE_FIELD_IS_ARRAY(_field, _name) \
62 BT_ASSERT_PRE( \
40f4ba76
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, \
44c440bc
PP
65 _name " is not an array field: %![field-]+f", (_field))
66
67#define BT_ASSERT_PRE_FIELD_IS_SET(_field, _name) \
68 BT_ASSERT_PRE(bt_field_is_set(_field), \
cb6f1f7d 69 _name " is not set: %!+f", (_field))
3dca2276 70
44c440bc 71#define BT_ASSERT_PRE_FIELD_HOT(_field, _name) \
40f4ba76 72 BT_ASSERT_PRE_HOT((const struct bt_field *) (_field), (_name), \
44c440bc 73 ": %!+f", (_field))
3dca2276
PP
74
75struct bt_field;
3dca2276 76
5cd6d0e5 77typedef struct bt_field *(* bt_field_create_func)(struct bt_field_class *);
44c440bc 78typedef void (*bt_field_method_set_is_frozen)(struct bt_field *, bool);
40f4ba76 79typedef bool (*bt_field_method_is_set)(const struct bt_field *);
cb6f1f7d
PP
80typedef void (*bt_field_method_reset)(struct bt_field *);
81
82struct bt_field_methods {
83 bt_field_method_set_is_frozen set_is_frozen;
cb6f1f7d
PP
84 bt_field_method_is_set is_set;
85 bt_field_method_reset reset;
3dca2276
PP
86};
87
cb6f1f7d 88struct bt_field {
83509119 89 struct bt_object base;
44c440bc
PP
90
91 /* Owned by this */
5cd6d0e5 92 struct bt_field_class *class;
44c440bc
PP
93
94 /* Virtual table for slow path (dev mode) operations */
cb6f1f7d 95 struct bt_field_methods *methods;
44c440bc
PP
96
97 bool is_set;
d990a4fb 98 bool frozen;
273b65be
JG
99};
100
cb6f1f7d
PP
101struct bt_field_integer {
102 struct bt_field common;
273b65be 103
44c440bc
PP
104 union {
105 uint64_t u;
106 int64_t i;
107 } value;
cb6f1f7d
PP
108};
109
44c440bc 110struct bt_field_real {
cb6f1f7d 111 struct bt_field common;
44c440bc 112 double value;
273b65be
JG
113};
114
cb6f1f7d
PP
115struct bt_field_structure {
116 struct bt_field common;
312c056a 117
cb6f1f7d 118 /* Array of `struct bt_field *`, owned by this */
312c056a 119 GPtrArray *fields;
273b65be
JG
120};
121
cb6f1f7d
PP
122struct bt_field_variant {
123 struct bt_field common;
312c056a 124
44c440bc
PP
125 /* Weak: belongs to `fields` below */
126 struct bt_field *selected_field;
312c056a 127
44c440bc
PP
128 /* Index of currently selected field */
129 uint64_t selected_index;
312c056a 130
cb6f1f7d 131 /* Array of `struct bt_field *`, owned by this */
312c056a 132 GPtrArray *fields;
273b65be
JG
133};
134
cb6f1f7d
PP
135struct bt_field_array {
136 struct bt_field common;
312c056a 137
cb6f1f7d 138 /* Array of `struct bt_field *`, owned by this */
44c440bc 139 GPtrArray *fields;
312c056a 140
44c440bc 141 /* Current effective length */
312c056a 142 uint64_t length;
273b65be
JG
143};
144
cb6f1f7d
PP
145struct bt_field_string {
146 struct bt_field common;
4d4b475d 147 GArray *buf;
44c440bc 148 uint64_t length;
273b65be
JG
149};
150
f6ccaed9 151#ifdef BT_DEV_MODE
44c440bc
PP
152# define bt_field_set_is_frozen _bt_field_set_is_frozen
153# define bt_field_is_set _bt_field_is_set
154# define bt_field_reset _bt_field_reset
155# define bt_field_set_single _bt_field_set_single
f6ccaed9 156#else
44c440bc
PP
157# define bt_field_set_is_frozen(_field, _is_frozen)
158# define bt_field_is_set(_field) (BT_FALSE)
159# define bt_field_reset(_field)
160# define bt_field_set_single(_field, _val)
f6ccaed9 161#endif
918be005 162
cb6f1f7d 163BT_HIDDEN
40f4ba76 164void _bt_field_set_is_frozen(const struct bt_field *field, bool is_frozen);
3dca2276
PP
165
166static inline
40f4ba76 167void _bt_field_reset(const struct bt_field *field)
3dca2276
PP
168{
169 BT_ASSERT(field);
170 BT_ASSERT(field->methods->reset);
40f4ba76 171 field->methods->reset((void *) field);
3dca2276
PP
172}
173
174static inline
44c440bc 175void _bt_field_set_single(struct bt_field *field, bool value)
3dca2276
PP
176{
177 BT_ASSERT(field);
44c440bc 178 field->is_set = value;
3dca2276
PP
179}
180
181static inline
40f4ba76 182bt_bool _bt_field_is_set(const struct bt_field *field)
3dca2276
PP
183{
184 bt_bool is_set = BT_FALSE;
185
186 if (!field) {
187 goto end;
188 }
189
864cad70 190 BT_ASSERT(bt_field_class_has_known_type(field->class));
3dca2276
PP
191 BT_ASSERT(field->methods->is_set);
192 is_set = field->methods->is_set(field);
193
194end:
195 return is_set;
196}
197
312c056a 198BT_HIDDEN
5cd6d0e5 199struct bt_field *bt_field_create(struct bt_field_class *class);
312c056a
PP
200
201BT_HIDDEN
44c440bc 202void bt_field_destroy(struct bt_field *field);
312c056a 203
56e18c4c 204#endif /* BABELTRACE_TRACE_IR_FIELDS_INTERNAL_H */
This page took 0.055219 seconds and 4 git commands to generate.