| 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 | struct bt_field; |
| 26 | |
| 27 | typedef struct bt_field *(* bt_field_create_func)(struct bt_field_class *); |
| 28 | typedef void (*bt_field_method_set_is_frozen)(struct bt_field *, bool); |
| 29 | typedef bool (*bt_field_method_is_set)(const struct bt_field *); |
| 30 | typedef void (*bt_field_method_reset)(struct bt_field *); |
| 31 | |
| 32 | struct bt_field_methods { |
| 33 | bt_field_method_set_is_frozen set_is_frozen; |
| 34 | bt_field_method_is_set is_set; |
| 35 | bt_field_method_reset reset; |
| 36 | }; |
| 37 | |
| 38 | struct bt_field { |
| 39 | struct bt_object base; |
| 40 | |
| 41 | /* Owned by this */ |
| 42 | struct bt_field_class *class; |
| 43 | |
| 44 | /* Virtual table for slow path (dev mode) operations */ |
| 45 | struct bt_field_methods *methods; |
| 46 | |
| 47 | bool is_set; |
| 48 | bool frozen; |
| 49 | }; |
| 50 | |
| 51 | struct bt_field_bool { |
| 52 | struct bt_field common; |
| 53 | bool value; |
| 54 | }; |
| 55 | |
| 56 | struct bt_field_bit_array { |
| 57 | struct bt_field common; |
| 58 | uint64_t value_as_int; |
| 59 | }; |
| 60 | |
| 61 | struct bt_field_integer { |
| 62 | struct bt_field common; |
| 63 | |
| 64 | union { |
| 65 | uint64_t u; |
| 66 | int64_t i; |
| 67 | } value; |
| 68 | }; |
| 69 | |
| 70 | struct bt_field_real { |
| 71 | struct bt_field common; |
| 72 | double value; |
| 73 | }; |
| 74 | |
| 75 | struct bt_field_structure { |
| 76 | struct bt_field common; |
| 77 | |
| 78 | /* Array of `struct bt_field *`, owned by this */ |
| 79 | GPtrArray *fields; |
| 80 | }; |
| 81 | |
| 82 | struct bt_field_option { |
| 83 | struct bt_field common; |
| 84 | |
| 85 | /* Owned by this */ |
| 86 | struct bt_field *content_field; |
| 87 | |
| 88 | /* Weak: equal to `content_field` above or `NULL` */ |
| 89 | struct bt_field *selected_field; |
| 90 | }; |
| 91 | |
| 92 | struct bt_field_variant { |
| 93 | struct bt_field common; |
| 94 | |
| 95 | /* Weak: belongs to `fields` below */ |
| 96 | struct bt_field *selected_field; |
| 97 | |
| 98 | /* Index of currently selected field */ |
| 99 | uint64_t selected_index; |
| 100 | |
| 101 | /* Array of `struct bt_field *`, owned by this */ |
| 102 | GPtrArray *fields; |
| 103 | }; |
| 104 | |
| 105 | struct bt_field_array { |
| 106 | struct bt_field common; |
| 107 | |
| 108 | /* Array of `struct bt_field *`, owned by this */ |
| 109 | GPtrArray *fields; |
| 110 | |
| 111 | /* Current effective length */ |
| 112 | uint64_t length; |
| 113 | }; |
| 114 | |
| 115 | struct bt_field_string { |
| 116 | struct bt_field common; |
| 117 | GArray *buf; |
| 118 | uint64_t length; |
| 119 | }; |
| 120 | |
| 121 | #ifdef BT_DEV_MODE |
| 122 | # define bt_field_set_is_frozen _bt_field_set_is_frozen |
| 123 | # define bt_field_is_set _bt_field_is_set |
| 124 | # define bt_field_reset _bt_field_reset |
| 125 | # define bt_field_set_single _bt_field_set_single |
| 126 | #else |
| 127 | # define bt_field_set_is_frozen(_field, _is_frozen) |
| 128 | # define bt_field_is_set(_field) (BT_FALSE) |
| 129 | # define bt_field_reset(_field) |
| 130 | # define bt_field_set_single(_field, _val) |
| 131 | #endif |
| 132 | |
| 133 | BT_HIDDEN |
| 134 | void _bt_field_set_is_frozen(const struct bt_field *field, bool is_frozen); |
| 135 | |
| 136 | static inline |
| 137 | void _bt_field_reset(const struct bt_field *field) |
| 138 | { |
| 139 | BT_ASSERT_DBG(field); |
| 140 | BT_ASSERT_DBG(field->methods->reset); |
| 141 | field->methods->reset((void *) field); |
| 142 | } |
| 143 | |
| 144 | static inline |
| 145 | void _bt_field_set_single(struct bt_field *field, bool value) |
| 146 | { |
| 147 | BT_ASSERT_DBG(field); |
| 148 | field->is_set = value; |
| 149 | } |
| 150 | |
| 151 | static inline |
| 152 | bt_bool _bt_field_is_set(const struct bt_field *field) |
| 153 | { |
| 154 | bt_bool is_set = BT_FALSE; |
| 155 | |
| 156 | if (!field) { |
| 157 | goto end; |
| 158 | } |
| 159 | |
| 160 | BT_ASSERT_DBG(field->methods->is_set); |
| 161 | is_set = field->methods->is_set(field); |
| 162 | |
| 163 | end: |
| 164 | return is_set; |
| 165 | } |
| 166 | |
| 167 | BT_HIDDEN |
| 168 | struct bt_field *bt_field_create(struct bt_field_class *class); |
| 169 | |
| 170 | BT_HIDDEN |
| 171 | void bt_field_destroy(struct bt_field *field); |
| 172 | |
| 173 | #endif /* BABELTRACE_TRACE_IR_FIELDS_INTERNAL_H */ |