lib: merge `assert-pre.h` and `assert-post.h` into `assert-cond.h`
[babeltrace.git] / src / lib / trace-ir / field.h
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 */
7
8 #ifndef BABELTRACE_TRACE_IR_FIELDS_INTERNAL_H
9 #define BABELTRACE_TRACE_IR_FIELDS_INTERNAL_H
10
11 #include "lib/assert-cond.h"
12 #include "common/common.h"
13 #include "lib/object.h"
14 #include "common/macros.h"
15 #include <babeltrace2/types.h>
16 #include <stdint.h>
17 #include <string.h>
18 #include <inttypes.h>
19 #include <stdbool.h>
20 #include <glib.h>
21
22 #include "field-class.h"
23 #include "utils.h"
24
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), \
27 _name " has the wrong class type: expected-class-type=%s, " \
28 "%![field-]+f", \
29 bt_common_field_class_type_string(_cls_type), (_field))
30
31 #define BT_ASSERT_PRE_DEV_FIELD_IS_UNSIGNED_INT(_field, _name) \
32 BT_ASSERT_PRE_DEV( \
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, \
35 _name " is not an unsigned integer field: %![field-]+f", \
36 (_field))
37
38 #define BT_ASSERT_PRE_DEV_FIELD_IS_SIGNED_INT(_field, _name) \
39 BT_ASSERT_PRE_DEV( \
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, \
42 _name " is not a signed integer field: %![field-]+f", \
43 (_field))
44
45 #define BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY(_field, _name) \
46 BT_ASSERT_PRE_DEV( \
47 ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_STATIC_ARRAY || \
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, \
50 _name " is not an array field: %![field-]+f", (_field))
51
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
58 #define BT_ASSERT_PRE_DEV_FIELD_IS_OPTION(_field, _name) \
59 BT_ASSERT_PRE_DEV( \
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, \
64 _name " is not an option field: %![field-]+f", (_field))
65
66 #define BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT(_field, _name) \
67 BT_ASSERT_PRE_DEV( \
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, \
71 _name " is not a variant field: %![field-]+f", (_field))
72
73 #define BT_ASSERT_PRE_DEV_FIELD_IS_SET(_field, _name) \
74 BT_ASSERT_PRE_DEV(bt_field_is_set(_field), \
75 _name " is not set: %!+f", (_field))
76
77 #define BT_ASSERT_PRE_DEV_FIELD_HOT(_field, _name) \
78 BT_ASSERT_PRE_DEV_HOT((const struct bt_field *) (_field), (_name), \
79 ": %!+f", (_field))
80
81 struct bt_field;
82
83 typedef struct bt_field *(* bt_field_create_func)(struct bt_field_class *);
84 typedef void (*bt_field_method_set_is_frozen)(struct bt_field *, bool);
85 typedef bool (*bt_field_method_is_set)(const struct bt_field *);
86 typedef void (*bt_field_method_reset)(struct bt_field *);
87
88 struct bt_field_methods {
89 bt_field_method_set_is_frozen set_is_frozen;
90 bt_field_method_is_set is_set;
91 bt_field_method_reset reset;
92 };
93
94 struct bt_field {
95 struct bt_object base;
96
97 /* Owned by this */
98 struct bt_field_class *class;
99
100 /* Virtual table for slow path (dev mode) operations */
101 struct bt_field_methods *methods;
102
103 bool is_set;
104 bool frozen;
105 };
106
107 struct bt_field_bool {
108 struct bt_field common;
109 bool value;
110 };
111
112 struct bt_field_bit_array {
113 struct bt_field common;
114 uint64_t value_as_int;
115 };
116
117 struct bt_field_integer {
118 struct bt_field common;
119
120 union {
121 uint64_t u;
122 int64_t i;
123 } value;
124 };
125
126 struct bt_field_real {
127 struct bt_field common;
128 double value;
129 };
130
131 struct bt_field_structure {
132 struct bt_field common;
133
134 /* Array of `struct bt_field *`, owned by this */
135 GPtrArray *fields;
136 };
137
138 struct 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
148 struct bt_field_variant {
149 struct bt_field common;
150
151 /* Weak: belongs to `fields` below */
152 struct bt_field *selected_field;
153
154 /* Index of currently selected field */
155 uint64_t selected_index;
156
157 /* Array of `struct bt_field *`, owned by this */
158 GPtrArray *fields;
159 };
160
161 struct bt_field_array {
162 struct bt_field common;
163
164 /* Array of `struct bt_field *`, owned by this */
165 GPtrArray *fields;
166
167 /* Current effective length */
168 uint64_t length;
169 };
170
171 struct bt_field_string {
172 struct bt_field common;
173 GArray *buf;
174 uint64_t length;
175 };
176
177 #ifdef BT_DEV_MODE
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
182 #else
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)
187 #endif
188
189 BT_HIDDEN
190 void _bt_field_set_is_frozen(const struct bt_field *field, bool is_frozen);
191
192 static inline
193 void _bt_field_reset(const struct bt_field *field)
194 {
195 BT_ASSERT_DBG(field);
196 BT_ASSERT_DBG(field->methods->reset);
197 field->methods->reset((void *) field);
198 }
199
200 static inline
201 void _bt_field_set_single(struct bt_field *field, bool value)
202 {
203 BT_ASSERT_DBG(field);
204 field->is_set = value;
205 }
206
207 static inline
208 bt_bool _bt_field_is_set(const struct bt_field *field)
209 {
210 bt_bool is_set = BT_FALSE;
211
212 if (!field) {
213 goto end;
214 }
215
216 BT_ASSERT_DBG(field->methods->is_set);
217 is_set = field->methods->is_set(field);
218
219 end:
220 return is_set;
221 }
222
223 BT_HIDDEN
224 struct bt_field *bt_field_create(struct bt_field_class *class);
225
226 BT_HIDDEN
227 void bt_field_destroy(struct bt_field *field);
228
229 #endif /* BABELTRACE_TRACE_IR_FIELDS_INTERNAL_H */
This page took 0.03395 seconds and 4 git commands to generate.