| 1 | /* |
| 2 | * The MIT License (MIT) |
| 3 | * |
| 4 | * Copyright (c) 2016 Philippe Proulx <pproulx@efficios.com> |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | /* From value-const.h */ |
| 26 | |
| 27 | typedef enum bt_value_status { |
| 28 | /// Operation canceled. |
| 29 | BT_VALUE_STATUS_CANCELED = 125, |
| 30 | |
| 31 | /// Cannot allocate memory. |
| 32 | BT_VALUE_STATUS_NOMEM = -12, |
| 33 | |
| 34 | /// Okay, no error. |
| 35 | BT_VALUE_STATUS_OK = 0, |
| 36 | } bt_value_status; |
| 37 | |
| 38 | typedef enum bt_value_type { |
| 39 | /// Null value object. |
| 40 | BT_VALUE_TYPE_NULL = 0, |
| 41 | |
| 42 | /// Boolean value object (holds #BT_TRUE or #BT_FALSE). |
| 43 | BT_VALUE_TYPE_BOOL = 1, |
| 44 | |
| 45 | /// Integer value object (holds a signed 64-bit integer raw value). |
| 46 | BT_VALUE_TYPE_INTEGER = 2, |
| 47 | |
| 48 | /// Floating point number value object (holds a \c double raw value). |
| 49 | BT_VALUE_TYPE_REAL = 3, |
| 50 | |
| 51 | /// String value object. |
| 52 | BT_VALUE_TYPE_STRING = 4, |
| 53 | |
| 54 | /// Array value object. |
| 55 | BT_VALUE_TYPE_ARRAY = 5, |
| 56 | |
| 57 | /// Map value object. |
| 58 | BT_VALUE_TYPE_MAP = 6, |
| 59 | } bt_value_type; |
| 60 | |
| 61 | extern bt_value_type bt_value_get_type(const bt_value *object); |
| 62 | |
| 63 | bt_bool bt_value_is_null(const bt_value *object); |
| 64 | |
| 65 | bt_bool bt_value_is_bool(const bt_value *object); |
| 66 | |
| 67 | bt_bool bt_value_is_integer(const bt_value *object); |
| 68 | |
| 69 | bt_bool bt_value_is_real(const bt_value *object); |
| 70 | |
| 71 | bt_bool bt_value_is_string(const bt_value *object); |
| 72 | |
| 73 | bt_bool bt_value_is_array(const bt_value *object); |
| 74 | |
| 75 | bt_bool bt_value_is_map(const bt_value *object); |
| 76 | |
| 77 | extern bt_value_status bt_value_copy(const bt_value *object, |
| 78 | bt_value **OUT); |
| 79 | |
| 80 | extern bt_bool bt_value_compare(const bt_value *object_a, |
| 81 | const bt_value *object_b); |
| 82 | |
| 83 | extern bt_bool bt_value_bool_get(const bt_value *bool_obj); |
| 84 | |
| 85 | extern int64_t bt_value_integer_get(const bt_value *integer_obj); |
| 86 | |
| 87 | extern double bt_value_real_get(const bt_value *real_obj); |
| 88 | |
| 89 | extern const char *bt_value_string_get(const bt_value *string_obj); |
| 90 | |
| 91 | extern uint64_t bt_value_array_get_size(const bt_value *array_obj); |
| 92 | |
| 93 | bt_bool bt_value_array_is_empty(const bt_value *array_obj); |
| 94 | |
| 95 | extern const bt_value *bt_value_array_borrow_element_by_index_const( |
| 96 | const bt_value *array_obj, uint64_t index); |
| 97 | |
| 98 | extern uint64_t bt_value_map_get_size(const bt_value *map_obj); |
| 99 | |
| 100 | bt_bool bt_value_map_is_empty(const bt_value *map_obj); |
| 101 | |
| 102 | extern const bt_value *bt_value_map_borrow_entry_value_const( |
| 103 | const bt_value *map_obj, const char *key); |
| 104 | |
| 105 | typedef bt_bool (* bt_value_map_foreach_entry_const_func)(const char *key, |
| 106 | const bt_value *object, void *data); |
| 107 | |
| 108 | extern bt_value_status bt_value_map_foreach_entry_const( |
| 109 | const bt_value *map_obj, |
| 110 | bt_value_map_foreach_entry_const_func func, void *data); |
| 111 | |
| 112 | extern bt_bool bt_value_map_has_entry(const bt_value *map_obj, |
| 113 | const char *key); |
| 114 | |
| 115 | extern bt_value_status bt_value_map_extend( |
| 116 | const bt_value *base_map_obj, |
| 117 | const bt_value *extension_map_obj, |
| 118 | bt_value **extended_map_obj); |
| 119 | |
| 120 | extern void bt_value_get_ref(const bt_value *value); |
| 121 | |
| 122 | extern void bt_value_put_ref(const bt_value *value); |
| 123 | |
| 124 | /* From value.h */ |
| 125 | |
| 126 | extern bt_value *const bt_value_null; |
| 127 | |
| 128 | extern bt_value *bt_value_bool_create(void); |
| 129 | |
| 130 | extern bt_value *bt_value_bool_create_init(bt_bool val); |
| 131 | |
| 132 | extern void bt_value_bool_set(bt_value *bool_obj, bt_bool val); |
| 133 | |
| 134 | extern bt_value *bt_value_integer_create(void); |
| 135 | |
| 136 | extern bt_value *bt_value_integer_create_init( |
| 137 | int64_t val); |
| 138 | |
| 139 | extern void bt_value_integer_set(bt_value *integer_obj, int64_t val); |
| 140 | |
| 141 | extern bt_value *bt_value_real_create(void); |
| 142 | |
| 143 | extern bt_value *bt_value_real_create_init(double val); |
| 144 | |
| 145 | extern void bt_value_real_set(bt_value *real_obj, double val); |
| 146 | |
| 147 | extern bt_value *bt_value_string_create(void); |
| 148 | |
| 149 | extern bt_value *bt_value_string_create_init(const char *val); |
| 150 | |
| 151 | extern bt_value_status bt_value_string_set(bt_value *string_obj, |
| 152 | const char *val); |
| 153 | |
| 154 | extern bt_value *bt_value_array_create(void); |
| 155 | |
| 156 | extern bt_value *bt_value_array_borrow_element_by_index( |
| 157 | bt_value *array_obj, uint64_t index); |
| 158 | |
| 159 | extern bt_value_status bt_value_array_append_element( |
| 160 | bt_value *array_obj, |
| 161 | bt_value *element_obj); |
| 162 | |
| 163 | extern bt_value_status bt_value_array_append_bool_element( |
| 164 | bt_value *array_obj, bt_bool val); |
| 165 | |
| 166 | extern bt_value_status bt_value_array_append_integer_element( |
| 167 | bt_value *array_obj, int64_t val); |
| 168 | |
| 169 | extern bt_value_status bt_value_array_append_real_element( |
| 170 | bt_value *array_obj, double val); |
| 171 | |
| 172 | extern bt_value_status bt_value_array_append_string_element( |
| 173 | bt_value *array_obj, const char *val); |
| 174 | |
| 175 | extern bt_value_status bt_value_array_append_empty_array_element( |
| 176 | bt_value *array_obj); |
| 177 | |
| 178 | extern bt_value_status bt_value_array_append_empty_map_element( |
| 179 | bt_value *array_obj); |
| 180 | |
| 181 | extern bt_value_status bt_value_array_set_element_by_index( |
| 182 | bt_value *array_obj, uint64_t index, |
| 183 | bt_value *element_obj); |
| 184 | |
| 185 | extern bt_value *bt_value_map_create(void); |
| 186 | |
| 187 | extern bt_value *bt_value_map_borrow_entry_value( |
| 188 | bt_value *map_obj, const char *key); |
| 189 | |
| 190 | typedef bt_bool (* bt_value_map_foreach_entry_func)(const char *key, |
| 191 | bt_value *object, void *data); |
| 192 | |
| 193 | extern bt_value_status bt_value_map_foreach_entry( |
| 194 | bt_value *map_obj, |
| 195 | bt_value_map_foreach_entry_func func, void *data); |
| 196 | |
| 197 | extern bt_value_status bt_value_map_insert_entry( |
| 198 | bt_value *map_obj, const char *key, |
| 199 | bt_value *element_obj); |
| 200 | |
| 201 | extern bt_value_status bt_value_map_insert_bool_entry( |
| 202 | bt_value *map_obj, const char *key, bt_bool val); |
| 203 | |
| 204 | extern bt_value_status bt_value_map_insert_integer_entry( |
| 205 | bt_value *map_obj, const char *key, int64_t val); |
| 206 | |
| 207 | extern bt_value_status bt_value_map_insert_real_entry( |
| 208 | bt_value *map_obj, const char *key, double val); |
| 209 | |
| 210 | extern bt_value_status bt_value_map_insert_string_entry( |
| 211 | bt_value *map_obj, const char *key, |
| 212 | const char *val); |
| 213 | |
| 214 | extern bt_value_status bt_value_map_insert_empty_array_entry( |
| 215 | bt_value *map_obj, const char *key); |
| 216 | |
| 217 | extern bt_value_status bt_value_map_insert_empty_map_entry( |
| 218 | bt_value *map_obj, const char *key); |
| 219 | |
| 220 | %{ |
| 221 | struct bt_value_map_get_keys_data { |
| 222 | struct bt_value *keys; |
| 223 | }; |
| 224 | |
| 225 | static int bt_value_map_get_keys_cb(const char *key, const struct bt_value *object, void *data) |
| 226 | { |
| 227 | enum bt_value_status status; |
| 228 | struct bt_value_map_get_keys_data *priv_data = data; |
| 229 | |
| 230 | status = bt_value_array_append_string_element(priv_data->keys, key); |
| 231 | if (status != BT_VALUE_STATUS_OK) { |
| 232 | return BT_FALSE; |
| 233 | } |
| 234 | |
| 235 | return BT_TRUE; |
| 236 | } |
| 237 | |
| 238 | static struct bt_value *bt_value_map_get_keys(const struct bt_value *map_obj) |
| 239 | { |
| 240 | enum bt_value_status status; |
| 241 | struct bt_value_map_get_keys_data data; |
| 242 | |
| 243 | data.keys = bt_value_array_create(); |
| 244 | if (!data.keys) { |
| 245 | return NULL; |
| 246 | } |
| 247 | |
| 248 | status = bt_value_map_foreach_entry_const(map_obj, bt_value_map_get_keys_cb, |
| 249 | &data); |
| 250 | if (status != BT_VALUE_STATUS_OK) { |
| 251 | goto error; |
| 252 | } |
| 253 | |
| 254 | goto end; |
| 255 | |
| 256 | error: |
| 257 | if (data.keys) { |
| 258 | BT_VALUE_PUT_REF_AND_RESET(data.keys); |
| 259 | } |
| 260 | |
| 261 | end: |
| 262 | return data.keys; |
| 263 | } |
| 264 | %} |
| 265 | |
| 266 | struct bt_value *bt_value_map_get_keys(const struct bt_value *map_obj); |