| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright (c) 2015-2017 EfficiOS Inc. and Linux Foundation |
| 5 | * Copyright (c) 2015-2017 Philippe Proulx <pproulx@efficios.com> |
| 6 | */ |
| 7 | |
| 8 | #ifndef BABELTRACE_CTF_WRITER_VALUES_INTERNAL_H |
| 9 | #define BABELTRACE_CTF_WRITER_VALUES_INTERNAL_H |
| 10 | |
| 11 | #include <babeltrace2-ctf-writer/types.h> |
| 12 | |
| 13 | #include "common/macros.h" |
| 14 | |
| 15 | struct bt_ctf_value; |
| 16 | struct bt_ctf_private_value; |
| 17 | |
| 18 | /** |
| 19 | @brief Status codes. |
| 20 | */ |
| 21 | enum bt_ctf_value_status { |
| 22 | /// Operation canceled. |
| 23 | BT_CTF_VALUE_STATUS_CANCELED = 125, |
| 24 | |
| 25 | /// Cannot allocate memory. |
| 26 | BT_CTF_VALUE_STATUS_NOMEM = -12, |
| 27 | |
| 28 | /// Okay, no error. |
| 29 | BT_CTF_VALUE_STATUS_OK = 0, |
| 30 | }; |
| 31 | |
| 32 | enum bt_ctf_value_status _bt_ctf_value_freeze(struct bt_ctf_value *object); |
| 33 | |
| 34 | #ifdef BT_DEV_MODE |
| 35 | # define bt_ctf_value_freeze _bt_ctf_value_freeze |
| 36 | #else |
| 37 | # define bt_ctf_value_freeze(_value) |
| 38 | #endif /* BT_DEV_MODE */ |
| 39 | |
| 40 | extern struct bt_ctf_value *const bt_ctf_value_null; |
| 41 | |
| 42 | enum bt_ctf_value_type { |
| 43 | /// Null value object. |
| 44 | BT_CTF_VALUE_TYPE_NULL = 0, |
| 45 | |
| 46 | /// Boolean value object (holds #BT_CTF_TRUE or #BT_CTF_FALSE). |
| 47 | BT_CTF_VALUE_TYPE_BOOL = 1, |
| 48 | |
| 49 | /// Integer value object (holds a signed 64-bit integer raw value). |
| 50 | BT_CTF_VALUE_TYPE_INTEGER = 2, |
| 51 | |
| 52 | /// Floating point number value object (holds a \c double raw value). |
| 53 | BT_CTF_VALUE_TYPE_REAL = 3, |
| 54 | |
| 55 | /// String value object. |
| 56 | BT_CTF_VALUE_TYPE_STRING = 4, |
| 57 | |
| 58 | /// Array value object. |
| 59 | BT_CTF_VALUE_TYPE_ARRAY = 5, |
| 60 | |
| 61 | /// Map value object. |
| 62 | BT_CTF_VALUE_TYPE_MAP = 6, |
| 63 | }; |
| 64 | |
| 65 | enum bt_ctf_value_type bt_ctf_value_get_type(const struct bt_ctf_value *object); |
| 66 | |
| 67 | static inline |
| 68 | bt_ctf_bool bt_ctf_value_is_null(const struct bt_ctf_value *object) |
| 69 | { |
| 70 | return bt_ctf_value_get_type(object) == BT_CTF_VALUE_TYPE_NULL; |
| 71 | } |
| 72 | |
| 73 | static inline |
| 74 | bt_ctf_bool bt_ctf_value_is_bool(const struct bt_ctf_value *object) |
| 75 | { |
| 76 | return bt_ctf_value_get_type(object) == BT_CTF_VALUE_TYPE_BOOL; |
| 77 | } |
| 78 | |
| 79 | static inline |
| 80 | bt_ctf_bool bt_ctf_value_is_integer(const struct bt_ctf_value *object) |
| 81 | { |
| 82 | return bt_ctf_value_get_type(object) == BT_CTF_VALUE_TYPE_INTEGER; |
| 83 | } |
| 84 | |
| 85 | static inline |
| 86 | bt_ctf_bool bt_ctf_value_is_real(const struct bt_ctf_value *object) |
| 87 | { |
| 88 | return bt_ctf_value_get_type(object) == BT_CTF_VALUE_TYPE_REAL; |
| 89 | } |
| 90 | |
| 91 | static inline |
| 92 | bt_ctf_bool bt_ctf_value_is_string(const struct bt_ctf_value *object) |
| 93 | { |
| 94 | return bt_ctf_value_get_type(object) == BT_CTF_VALUE_TYPE_STRING; |
| 95 | } |
| 96 | |
| 97 | static inline |
| 98 | bt_ctf_bool bt_ctf_value_is_array(const struct bt_ctf_value *object) |
| 99 | { |
| 100 | return bt_ctf_value_get_type(object) == BT_CTF_VALUE_TYPE_ARRAY; |
| 101 | } |
| 102 | |
| 103 | static inline |
| 104 | bt_ctf_bool bt_ctf_value_is_map(const struct bt_ctf_value *object) |
| 105 | { |
| 106 | return bt_ctf_value_get_type(object) == BT_CTF_VALUE_TYPE_MAP; |
| 107 | } |
| 108 | |
| 109 | enum bt_ctf_value_status bt_ctf_value_copy(struct bt_ctf_private_value **copy, |
| 110 | const struct bt_ctf_value *object); |
| 111 | |
| 112 | bt_ctf_bool bt_ctf_value_compare(const struct bt_ctf_value *object_a, |
| 113 | const struct bt_ctf_value *object_b); |
| 114 | |
| 115 | bt_ctf_bool bt_ctf_value_bool_get(const struct bt_ctf_value *bool_obj); |
| 116 | |
| 117 | int64_t bt_ctf_value_integer_get(const struct bt_ctf_value *integer_obj); |
| 118 | |
| 119 | double bt_ctf_value_real_get(const struct bt_ctf_value *real_obj); |
| 120 | |
| 121 | const char *bt_ctf_value_string_get(const struct bt_ctf_value *string_obj); |
| 122 | |
| 123 | uint64_t bt_ctf_value_array_get_length(const struct bt_ctf_value *array_obj); |
| 124 | |
| 125 | static inline |
| 126 | bt_ctf_bool bt_ctf_value_array_is_empty(const struct bt_ctf_value *array_obj) |
| 127 | { |
| 128 | return bt_ctf_value_array_get_length(array_obj) == 0; |
| 129 | } |
| 130 | |
| 131 | struct bt_ctf_value *bt_ctf_value_array_borrow_element_by_index( |
| 132 | const struct bt_ctf_value *array_obj, uint64_t index); |
| 133 | |
| 134 | uint64_t bt_ctf_value_map_get_size(const struct bt_ctf_value *map_obj); |
| 135 | |
| 136 | static inline |
| 137 | bt_ctf_bool bt_ctf_value_map_is_empty(const struct bt_ctf_value *map_obj) |
| 138 | { |
| 139 | return bt_ctf_value_map_get_size(map_obj) == 0; |
| 140 | } |
| 141 | |
| 142 | struct bt_ctf_value *bt_ctf_value_map_borrow_entry_value( |
| 143 | const struct bt_ctf_value *map_obj, const char *key); |
| 144 | |
| 145 | typedef bt_ctf_bool (* bt_ctf_value_map_foreach_entry_cb)(const char *key, |
| 146 | struct bt_ctf_value *object, void *data); |
| 147 | |
| 148 | enum bt_ctf_value_status bt_ctf_value_map_foreach_entry( |
| 149 | const struct bt_ctf_value *map_obj, |
| 150 | bt_ctf_value_map_foreach_entry_cb cb, void *data); |
| 151 | |
| 152 | bt_ctf_bool bt_ctf_value_map_has_entry(const struct bt_ctf_value *map_obj, |
| 153 | const char *key); |
| 154 | |
| 155 | enum bt_ctf_value_status bt_ctf_value_map_extend( |
| 156 | struct bt_ctf_private_value **extended_map_obj, |
| 157 | const struct bt_ctf_value *base_map_obj, |
| 158 | const struct bt_ctf_value *extension_map_obj); |
| 159 | |
| 160 | |
| 161 | struct bt_ctf_value; |
| 162 | struct bt_ctf_private_value; |
| 163 | |
| 164 | extern struct bt_ctf_private_value *const bt_ctf_private_value_null; |
| 165 | |
| 166 | static inline |
| 167 | struct bt_ctf_value *bt_ctf_private_value_as_value( |
| 168 | struct bt_ctf_private_value *priv_value) |
| 169 | { |
| 170 | return (void *) priv_value; |
| 171 | } |
| 172 | |
| 173 | struct bt_ctf_private_value *bt_ctf_private_value_bool_create(void); |
| 174 | |
| 175 | struct bt_ctf_private_value *bt_ctf_private_value_bool_create_init(bt_ctf_bool val); |
| 176 | |
| 177 | void bt_ctf_private_value_bool_set(struct bt_ctf_private_value *bool_obj, |
| 178 | bt_ctf_bool val); |
| 179 | |
| 180 | struct bt_ctf_private_value *bt_ctf_private_value_integer_create(void); |
| 181 | |
| 182 | struct bt_ctf_private_value *bt_ctf_private_value_integer_create_init( |
| 183 | int64_t val); |
| 184 | |
| 185 | void bt_ctf_private_value_integer_set( |
| 186 | struct bt_ctf_private_value *integer_obj, int64_t val); |
| 187 | |
| 188 | struct bt_ctf_private_value *bt_ctf_private_value_real_create(void); |
| 189 | |
| 190 | struct bt_ctf_private_value *bt_ctf_private_value_real_create_init(double val); |
| 191 | |
| 192 | void bt_ctf_private_value_real_set( |
| 193 | struct bt_ctf_private_value *real_obj, double val); |
| 194 | |
| 195 | struct bt_ctf_private_value *bt_ctf_private_value_string_create(void); |
| 196 | |
| 197 | struct bt_ctf_private_value *bt_ctf_private_value_string_create_init( |
| 198 | const char *val); |
| 199 | |
| 200 | enum bt_ctf_value_status bt_ctf_private_value_string_set( |
| 201 | struct bt_ctf_private_value *string_obj, |
| 202 | const char *val); |
| 203 | |
| 204 | struct bt_ctf_private_value *bt_ctf_private_value_array_create(void); |
| 205 | |
| 206 | struct bt_ctf_private_value *bt_ctf_private_value_array_borrow_element_by_index( |
| 207 | const struct bt_ctf_private_value *array_obj, uint64_t index); |
| 208 | |
| 209 | enum bt_ctf_value_status bt_ctf_private_value_array_append_element( |
| 210 | struct bt_ctf_private_value *array_obj, |
| 211 | struct bt_ctf_value *element_obj); |
| 212 | |
| 213 | enum bt_ctf_value_status bt_ctf_private_value_array_append_bool_element( |
| 214 | struct bt_ctf_private_value *array_obj, |
| 215 | bt_ctf_bool val); |
| 216 | |
| 217 | enum bt_ctf_value_status bt_ctf_private_value_array_append_integer_element( |
| 218 | struct bt_ctf_private_value *array_obj, |
| 219 | int64_t val); |
| 220 | |
| 221 | enum bt_ctf_value_status bt_ctf_private_value_array_append_real_element( |
| 222 | struct bt_ctf_private_value *array_obj, |
| 223 | double val); |
| 224 | |
| 225 | enum bt_ctf_value_status bt_ctf_private_value_array_append_string_element( |
| 226 | struct bt_ctf_private_value *array_obj, const char *val); |
| 227 | |
| 228 | enum bt_ctf_value_status bt_ctf_private_value_array_append_empty_array_element( |
| 229 | struct bt_ctf_private_value *array_obj); |
| 230 | |
| 231 | enum bt_ctf_value_status bt_ctf_private_value_array_append_empty_map_element( |
| 232 | struct bt_ctf_private_value *array_obj); |
| 233 | |
| 234 | enum bt_ctf_value_status bt_ctf_private_value_array_set_element_by_index( |
| 235 | struct bt_ctf_private_value *array_obj, uint64_t index, |
| 236 | struct bt_ctf_value *element_obj); |
| 237 | |
| 238 | struct bt_ctf_private_value *bt_ctf_private_value_map_create(void); |
| 239 | |
| 240 | struct bt_ctf_private_value *bt_ctf_private_value_map_borrow_entry_value( |
| 241 | const struct bt_ctf_private_value *map_obj, const char *key); |
| 242 | |
| 243 | typedef bt_ctf_bool (* bt_ctf_private_value_map_foreach_entry_cb)(const char *key, |
| 244 | struct bt_ctf_private_value *object, void *data); |
| 245 | |
| 246 | enum bt_ctf_value_status bt_ctf_private_value_map_foreach_entry( |
| 247 | const struct bt_ctf_private_value *map_obj, |
| 248 | bt_ctf_private_value_map_foreach_entry_cb cb, void *data); |
| 249 | |
| 250 | enum bt_ctf_value_status bt_ctf_private_value_map_insert_entry( |
| 251 | struct bt_ctf_private_value *map_obj, const char *key, |
| 252 | struct bt_ctf_value *element_obj); |
| 253 | |
| 254 | enum bt_ctf_value_status bt_ctf_private_value_map_insert_bool_entry( |
| 255 | struct bt_ctf_private_value *map_obj, const char *key, bt_ctf_bool val); |
| 256 | |
| 257 | enum bt_ctf_value_status bt_ctf_private_value_map_insert_integer_entry( |
| 258 | struct bt_ctf_private_value *map_obj, const char *key, int64_t val); |
| 259 | |
| 260 | enum bt_ctf_value_status bt_ctf_private_value_map_insert_real_entry( |
| 261 | struct bt_ctf_private_value *map_obj, const char *key, double val); |
| 262 | |
| 263 | enum bt_ctf_value_status bt_ctf_private_value_map_insert_string_entry( |
| 264 | struct bt_ctf_private_value *map_obj, const char *key, |
| 265 | const char *val); |
| 266 | |
| 267 | enum bt_ctf_value_status bt_ctf_private_value_map_insert_empty_array_entry( |
| 268 | struct bt_ctf_private_value *map_obj, const char *key); |
| 269 | |
| 270 | enum bt_ctf_value_status bt_ctf_private_value_map_insert_empty_map_entry( |
| 271 | struct bt_ctf_private_value *map_obj, const char *key); |
| 272 | |
| 273 | static inline |
| 274 | const char *bt_ctf_value_type_string(enum bt_ctf_value_type type) |
| 275 | { |
| 276 | switch (type) { |
| 277 | case BT_CTF_VALUE_TYPE_NULL: |
| 278 | return "BT_CTF_VALUE_TYPE_NULL"; |
| 279 | case BT_CTF_VALUE_TYPE_BOOL: |
| 280 | return "BT_CTF_VALUE_TYPE_BOOL"; |
| 281 | case BT_CTF_VALUE_TYPE_INTEGER: |
| 282 | return "BT_CTF_VALUE_TYPE_INTEGER"; |
| 283 | case BT_CTF_VALUE_TYPE_REAL: |
| 284 | return "BT_CTF_VALUE_TYPE_REAL"; |
| 285 | case BT_CTF_VALUE_TYPE_STRING: |
| 286 | return "BT_CTF_VALUE_TYPE_STRING"; |
| 287 | case BT_CTF_VALUE_TYPE_ARRAY: |
| 288 | return "BT_CTF_VALUE_TYPE_ARRAY"; |
| 289 | case BT_CTF_VALUE_TYPE_MAP: |
| 290 | return "BT_CTF_VALUE_TYPE_MAP"; |
| 291 | default: |
| 292 | return "(unknown)"; |
| 293 | } |
| 294 | }; |
| 295 | |
| 296 | #endif /* BABELTRACE_CTF_WRITER_VALUES_INTERNAL_H */ |