lib: merge `assert-pre.h` and `assert-post.h` into `assert-cond.h`
[babeltrace.git] / src / lib / trace-ir / field.h
CommitLineData
273b65be 1/*
0235b0db
MJ
2 * SPDX-License-Identifier: MIT
3 *
e2f7325d 4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
de9dd397 5 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be
JG
6 */
7
0235b0db
MJ
8#ifndef BABELTRACE_TRACE_IR_FIELDS_INTERNAL_H
9#define BABELTRACE_TRACE_IR_FIELDS_INTERNAL_H
10
d98421f2 11#include "lib/assert-cond.h"
578e048b
MJ
12#include "common/common.h"
13#include "lib/object.h"
91d81473 14#include "common/macros.h"
3fadfbc0 15#include <babeltrace2/types.h>
dc3fffef 16#include <stdint.h>
4d4b475d 17#include <string.h>
3dca2276 18#include <inttypes.h>
d990a4fb 19#include <stdbool.h>
273b65be
JG
20#include <glib.h>
21
578e048b
MJ
22#include "field-class.h"
23#include "utils.h"
24
bdb288b3
PP
25#define BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(_field, _cls_type, _name) \
26 BT_ASSERT_PRE_DEV(((const struct bt_field *) (_field))->class->type == (_cls_type), \
864cad70 27 _name " has the wrong class type: expected-class-type=%s, " \
44c440bc 28 "%![field-]+f", \
864cad70 29 bt_common_field_class_type_string(_cls_type), (_field))
44c440bc 30
bdb288b3
PP
31#define BT_ASSERT_PRE_DEV_FIELD_IS_UNSIGNED_INT(_field, _name) \
32 BT_ASSERT_PRE_DEV( \
40f4ba76
PP
33 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER || \
34 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, \
44c440bc
PP
35 _name " is not an unsigned integer field: %![field-]+f", \
36 (_field))
37
bdb288b3
PP
38#define BT_ASSERT_PRE_DEV_FIELD_IS_SIGNED_INT(_field, _name) \
39 BT_ASSERT_PRE_DEV( \
40f4ba76
PP
40 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_SIGNED_INTEGER || \
41 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, \
bdb288b3 42 _name " is not a signed integer field: %![field-]+f", \
44c440bc
PP
43 (_field))
44
bdb288b3
PP
45#define BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY(_field, _name) \
46 BT_ASSERT_PRE_DEV( \
40f4ba76 47 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_STATIC_ARRAY || \
81b8fa44
PP
48 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD || \
49 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD, \
44c440bc
PP
50 _name " is not an array field: %![field-]+f", (_field))
51
81b8fa44
PP
52#define BT_ASSERT_PRE_DEV_FIELD_IS_DYNAMIC_ARRAY(_field, _name) \
53 BT_ASSERT_PRE_DEV( \
54 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD || \
55 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD, \
56 _name " is not a dynamic array field: %![field-]+f", (_field))
57
0aa006b7
PP
58#define BT_ASSERT_PRE_DEV_FIELD_IS_OPTION(_field, _name) \
59 BT_ASSERT_PRE_DEV( \
de821fe5
PP
60 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD || \
61 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD || \
62 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD || \
63 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD, \
0aa006b7
PP
64 _name " is not an option field: %![field-]+f", (_field))
65
45c51519
PP
66#define BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT(_field, _name) \
67 BT_ASSERT_PRE_DEV( \
de821fe5
PP
68 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD || \
69 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD || \
70 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD, \
45c51519
PP
71 _name " is not a variant field: %![field-]+f", (_field))
72
bdb288b3
PP
73#define BT_ASSERT_PRE_DEV_FIELD_IS_SET(_field, _name) \
74 BT_ASSERT_PRE_DEV(bt_field_is_set(_field), \
cb6f1f7d 75 _name " is not set: %!+f", (_field))
3dca2276 76
bdb288b3
PP
77#define BT_ASSERT_PRE_DEV_FIELD_HOT(_field, _name) \
78 BT_ASSERT_PRE_DEV_HOT((const struct bt_field *) (_field), (_name), \
44c440bc 79 ": %!+f", (_field))
3dca2276
PP
80
81struct bt_field;
3dca2276 82
5cd6d0e5 83typedef struct bt_field *(* bt_field_create_func)(struct bt_field_class *);
44c440bc 84typedef void (*bt_field_method_set_is_frozen)(struct bt_field *, bool);
40f4ba76 85typedef bool (*bt_field_method_is_set)(const struct bt_field *);
cb6f1f7d
PP
86typedef void (*bt_field_method_reset)(struct bt_field *);
87
88struct bt_field_methods {
89 bt_field_method_set_is_frozen set_is_frozen;
cb6f1f7d
PP
90 bt_field_method_is_set is_set;
91 bt_field_method_reset reset;
3dca2276
PP
92};
93
cb6f1f7d 94struct bt_field {
83509119 95 struct bt_object base;
44c440bc
PP
96
97 /* Owned by this */
5cd6d0e5 98 struct bt_field_class *class;
44c440bc
PP
99
100 /* Virtual table for slow path (dev mode) operations */
cb6f1f7d 101 struct bt_field_methods *methods;
44c440bc
PP
102
103 bool is_set;
d990a4fb 104 bool frozen;
273b65be
JG
105};
106
5cebbe7f
PP
107struct bt_field_bool {
108 struct bt_field common;
109 bool value;
110};
111
1094efa4
PP
112struct bt_field_bit_array {
113 struct bt_field common;
114 uint64_t value_as_int;
115};
116
cb6f1f7d
PP
117struct bt_field_integer {
118 struct bt_field common;
273b65be 119
44c440bc
PP
120 union {
121 uint64_t u;
122 int64_t i;
123 } value;
cb6f1f7d
PP
124};
125
44c440bc 126struct bt_field_real {
cb6f1f7d 127 struct bt_field common;
44c440bc 128 double value;
273b65be
JG
129};
130
cb6f1f7d
PP
131struct bt_field_structure {
132 struct bt_field common;
312c056a 133
cb6f1f7d 134 /* Array of `struct bt_field *`, owned by this */
312c056a 135 GPtrArray *fields;
273b65be
JG
136};
137
b38aea74
PP
138struct bt_field_option {
139 struct bt_field common;
140
141 /* Owned by this */
142 struct bt_field *content_field;
143
144 /* Weak: equal to `content_field` above or `NULL` */
145 struct bt_field *selected_field;
146};
147
cb6f1f7d
PP
148struct bt_field_variant {
149 struct bt_field common;
312c056a 150
44c440bc
PP
151 /* Weak: belongs to `fields` below */
152 struct bt_field *selected_field;
312c056a 153
44c440bc
PP
154 /* Index of currently selected field */
155 uint64_t selected_index;
312c056a 156
cb6f1f7d 157 /* Array of `struct bt_field *`, owned by this */
312c056a 158 GPtrArray *fields;
273b65be
JG
159};
160
cb6f1f7d
PP
161struct bt_field_array {
162 struct bt_field common;
312c056a 163
cb6f1f7d 164 /* Array of `struct bt_field *`, owned by this */
44c440bc 165 GPtrArray *fields;
312c056a 166
44c440bc 167 /* Current effective length */
312c056a 168 uint64_t length;
273b65be
JG
169};
170
cb6f1f7d
PP
171struct bt_field_string {
172 struct bt_field common;
4d4b475d 173 GArray *buf;
44c440bc 174 uint64_t length;
273b65be
JG
175};
176
f6ccaed9 177#ifdef BT_DEV_MODE
44c440bc
PP
178# define bt_field_set_is_frozen _bt_field_set_is_frozen
179# define bt_field_is_set _bt_field_is_set
180# define bt_field_reset _bt_field_reset
181# define bt_field_set_single _bt_field_set_single
f6ccaed9 182#else
44c440bc
PP
183# define bt_field_set_is_frozen(_field, _is_frozen)
184# define bt_field_is_set(_field) (BT_FALSE)
185# define bt_field_reset(_field)
186# define bt_field_set_single(_field, _val)
f6ccaed9 187#endif
918be005 188
cb6f1f7d 189BT_HIDDEN
40f4ba76 190void _bt_field_set_is_frozen(const struct bt_field *field, bool is_frozen);
3dca2276
PP
191
192static inline
40f4ba76 193void _bt_field_reset(const struct bt_field *field)
3dca2276 194{
98b15851
PP
195 BT_ASSERT_DBG(field);
196 BT_ASSERT_DBG(field->methods->reset);
40f4ba76 197 field->methods->reset((void *) field);
3dca2276
PP
198}
199
200static inline
44c440bc 201void _bt_field_set_single(struct bt_field *field, bool value)
3dca2276 202{
98b15851 203 BT_ASSERT_DBG(field);
44c440bc 204 field->is_set = value;
3dca2276
PP
205}
206
207static inline
40f4ba76 208bt_bool _bt_field_is_set(const struct bt_field *field)
3dca2276
PP
209{
210 bt_bool is_set = BT_FALSE;
211
212 if (!field) {
213 goto end;
214 }
215
98b15851 216 BT_ASSERT_DBG(field->methods->is_set);
3dca2276
PP
217 is_set = field->methods->is_set(field);
218
219end:
220 return is_set;
221}
222
312c056a 223BT_HIDDEN
5cd6d0e5 224struct bt_field *bt_field_create(struct bt_field_class *class);
312c056a
PP
225
226BT_HIDDEN
44c440bc 227void bt_field_destroy(struct bt_field *field);
312c056a 228
56e18c4c 229#endif /* BABELTRACE_TRACE_IR_FIELDS_INTERNAL_H */
This page took 0.10293 seconds and 4 git commands to generate.