| 1 | #ifndef BABELTRACE2_VALUE_CONST_H |
| 2 | #define BABELTRACE2_VALUE_CONST_H |
| 3 | |
| 4 | /* |
| 5 | * Copyright (c) 2010-2019 EfficiOS Inc. and Linux Foundation |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
| 24 | */ |
| 25 | |
| 26 | #ifndef __BT_IN_BABELTRACE_H |
| 27 | # error "Please include <babeltrace2/babeltrace.h> instead." |
| 28 | #endif |
| 29 | |
| 30 | #include <stdint.h> |
| 31 | #include <stddef.h> |
| 32 | |
| 33 | #include <babeltrace2/types.h> |
| 34 | |
| 35 | #ifdef __cplusplus |
| 36 | extern "C" { |
| 37 | #endif |
| 38 | |
| 39 | typedef enum bt_value_type { |
| 40 | /// Null value object. |
| 41 | BT_VALUE_TYPE_NULL = 0, |
| 42 | |
| 43 | /// Boolean value object (holds #BT_TRUE or #BT_FALSE). |
| 44 | BT_VALUE_TYPE_BOOL = 1, |
| 45 | |
| 46 | /// Unsigned integer value object (holds an unsigned 64-bit integer raw value). |
| 47 | BT_VALUE_TYPE_UNSIGNED_INTEGER = 2, |
| 48 | |
| 49 | /// Signed integer value object (holds a signed 64-bit integer raw value). |
| 50 | BT_VALUE_TYPE_SIGNED_INTEGER = 3, |
| 51 | |
| 52 | /// Floating point number value object (holds a \c double raw value). |
| 53 | BT_VALUE_TYPE_REAL = 4, |
| 54 | |
| 55 | /// String value object. |
| 56 | BT_VALUE_TYPE_STRING = 5, |
| 57 | |
| 58 | /// Array value object. |
| 59 | BT_VALUE_TYPE_ARRAY = 6, |
| 60 | |
| 61 | /// Map value object. |
| 62 | BT_VALUE_TYPE_MAP = 7, |
| 63 | } bt_value_type; |
| 64 | |
| 65 | extern bt_value_type bt_value_get_type(const bt_value *object); |
| 66 | |
| 67 | static inline |
| 68 | bt_bool bt_value_is_null(const bt_value *object) |
| 69 | { |
| 70 | return bt_value_get_type(object) == BT_VALUE_TYPE_NULL; |
| 71 | } |
| 72 | |
| 73 | static inline |
| 74 | bt_bool bt_value_is_bool(const bt_value *object) |
| 75 | { |
| 76 | return bt_value_get_type(object) == BT_VALUE_TYPE_BOOL; |
| 77 | } |
| 78 | |
| 79 | static inline |
| 80 | bt_bool bt_value_is_unsigned_integer(const bt_value *object) |
| 81 | { |
| 82 | return bt_value_get_type(object) == BT_VALUE_TYPE_UNSIGNED_INTEGER; |
| 83 | } |
| 84 | |
| 85 | static inline |
| 86 | bt_bool bt_value_is_signed_integer(const bt_value *object) |
| 87 | { |
| 88 | return bt_value_get_type(object) == BT_VALUE_TYPE_SIGNED_INTEGER; |
| 89 | } |
| 90 | |
| 91 | static inline |
| 92 | bt_bool bt_value_is_real(const bt_value *object) |
| 93 | { |
| 94 | return bt_value_get_type(object) == BT_VALUE_TYPE_REAL; |
| 95 | } |
| 96 | |
| 97 | static inline |
| 98 | bt_bool bt_value_is_string(const bt_value *object) |
| 99 | { |
| 100 | return bt_value_get_type(object) == BT_VALUE_TYPE_STRING; |
| 101 | } |
| 102 | |
| 103 | static inline |
| 104 | bt_bool bt_value_is_array(const bt_value *object) |
| 105 | { |
| 106 | return bt_value_get_type(object) == BT_VALUE_TYPE_ARRAY; |
| 107 | } |
| 108 | |
| 109 | static inline |
| 110 | bt_bool bt_value_is_map(const bt_value *object) |
| 111 | { |
| 112 | return bt_value_get_type(object) == BT_VALUE_TYPE_MAP; |
| 113 | } |
| 114 | |
| 115 | typedef enum bt_value_copy_status { |
| 116 | BT_VALUE_COPY_STATUS_MEMORY_ERROR = __BT_FUNC_STATUS_MEMORY_ERROR, |
| 117 | BT_VALUE_COPY_STATUS_OK = __BT_FUNC_STATUS_OK, |
| 118 | } bt_value_copy_status; |
| 119 | |
| 120 | extern bt_value_copy_status bt_value_copy(const bt_value *object, |
| 121 | bt_value **copy); |
| 122 | |
| 123 | extern bt_bool bt_value_is_equal(const bt_value *object_a, |
| 124 | const bt_value *object_b); |
| 125 | |
| 126 | extern bt_bool bt_value_bool_get(const bt_value *bool_obj); |
| 127 | |
| 128 | extern uint64_t bt_value_integer_unsigned_get(const bt_value *integer_obj); |
| 129 | |
| 130 | extern int64_t bt_value_integer_signed_get(const bt_value *integer_obj); |
| 131 | |
| 132 | extern double bt_value_real_get(const bt_value *real_obj); |
| 133 | |
| 134 | extern const char *bt_value_string_get(const bt_value *string_obj); |
| 135 | |
| 136 | extern uint64_t bt_value_array_get_length(const bt_value *array_obj); |
| 137 | |
| 138 | static inline |
| 139 | bt_bool bt_value_array_is_empty(const bt_value *array_obj) |
| 140 | { |
| 141 | return bt_value_array_get_length(array_obj) == 0; |
| 142 | } |
| 143 | |
| 144 | extern const bt_value *bt_value_array_borrow_element_by_index_const( |
| 145 | const bt_value *array_obj, uint64_t index); |
| 146 | |
| 147 | extern uint64_t bt_value_map_get_size(const bt_value *map_obj); |
| 148 | |
| 149 | static inline |
| 150 | bt_bool bt_value_map_is_empty(const bt_value *map_obj) |
| 151 | { |
| 152 | return bt_value_map_get_size(map_obj) == 0; |
| 153 | } |
| 154 | |
| 155 | extern const bt_value *bt_value_map_borrow_entry_value_const( |
| 156 | const bt_value *map_obj, const char *key); |
| 157 | |
| 158 | typedef bt_bool (* bt_value_map_foreach_entry_const_func)(const char *key, |
| 159 | const bt_value *object, void *data); |
| 160 | |
| 161 | typedef enum bt_value_map_foreach_entry_const_status { |
| 162 | BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_OK = __BT_FUNC_STATUS_OK, |
| 163 | BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_INTERRUPTED = __BT_FUNC_STATUS_INTERRUPTED, |
| 164 | } bt_value_map_foreach_entry_const_status; |
| 165 | |
| 166 | extern bt_value_map_foreach_entry_const_status bt_value_map_foreach_entry_const( |
| 167 | const bt_value *map_obj, |
| 168 | bt_value_map_foreach_entry_const_func func, void *data); |
| 169 | |
| 170 | extern bt_bool bt_value_map_has_entry(const bt_value *map_obj, |
| 171 | const char *key); |
| 172 | |
| 173 | extern void bt_value_get_ref(const bt_value *value); |
| 174 | |
| 175 | extern void bt_value_put_ref(const bt_value *value); |
| 176 | |
| 177 | #define BT_VALUE_PUT_REF_AND_RESET(_var) \ |
| 178 | do { \ |
| 179 | bt_value_put_ref(_var); \ |
| 180 | (_var) = NULL; \ |
| 181 | } while (0) |
| 182 | |
| 183 | #define BT_VALUE_MOVE_REF(_var_dst, _var_src) \ |
| 184 | do { \ |
| 185 | bt_value_put_ref(_var_dst); \ |
| 186 | (_var_dst) = (_var_src); \ |
| 187 | (_var_src) = NULL; \ |
| 188 | } while (0) |
| 189 | |
| 190 | #ifdef __cplusplus |
| 191 | } |
| 192 | #endif |
| 193 | |
| 194 | #endif /* BABELTRACE2_VALUE_CONST_H */ |