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