2 * The MIT License (MIT)
4 * Copyright (c) 2016 Philippe Proulx <pproulx@efficios.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 /* From value-const.h */
27 typedef enum bt_value_status {
28 /// Operation canceled.
29 BT_VALUE_STATUS_CANCELED = 125,
31 /// Cannot allocate memory.
32 BT_VALUE_STATUS_NOMEM = -12,
35 BT_VALUE_STATUS_OK = 0,
38 typedef enum bt_value_type {
39 /// Null value object.
40 BT_VALUE_TYPE_NULL = 0,
42 /// Boolean value object (holds #BT_TRUE or #BT_FALSE).
43 BT_VALUE_TYPE_BOOL = 1,
45 /// Integer value object (holds a signed 64-bit integer raw value).
46 BT_VALUE_TYPE_INTEGER = 2,
48 /// Floating point number value object (holds a \c double raw value).
49 BT_VALUE_TYPE_REAL = 3,
51 /// String value object.
52 BT_VALUE_TYPE_STRING = 4,
54 /// Array value object.
55 BT_VALUE_TYPE_ARRAY = 5,
58 BT_VALUE_TYPE_MAP = 6,
61 extern bt_value_type bt_value_get_type(const bt_value *object);
63 bt_bool bt_value_is_null(const bt_value *object);
65 bt_bool bt_value_is_bool(const bt_value *object);
67 bt_bool bt_value_is_integer(const bt_value *object);
69 bt_bool bt_value_is_real(const bt_value *object);
71 bt_bool bt_value_is_string(const bt_value *object);
73 bt_bool bt_value_is_array(const bt_value *object);
75 bt_bool bt_value_is_map(const bt_value *object);
77 extern bt_value_status bt_value_copy(const bt_value *object,
80 extern bt_bool bt_value_compare(const bt_value *object_a,
81 const bt_value *object_b);
83 extern bt_bool bt_value_bool_get(const bt_value *bool_obj);
85 extern int64_t bt_value_integer_get(const bt_value *integer_obj);
87 extern double bt_value_real_get(const bt_value *real_obj);
89 extern const char *bt_value_string_get(const bt_value *string_obj);
91 extern uint64_t bt_value_array_get_size(const bt_value *array_obj);
93 bt_bool bt_value_array_is_empty(const bt_value *array_obj);
95 extern const bt_value *bt_value_array_borrow_element_by_index_const(
96 const bt_value *array_obj, uint64_t index);
98 extern uint64_t bt_value_map_get_size(const bt_value *map_obj);
100 bt_bool bt_value_map_is_empty(const bt_value *map_obj);
102 extern const bt_value *bt_value_map_borrow_entry_value_const(
103 const bt_value *map_obj, const char *key);
105 typedef bt_bool (* bt_value_map_foreach_entry_const_func)(const char *key,
106 const bt_value *object, void *data);
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);
112 extern bt_bool bt_value_map_has_entry(const bt_value *map_obj,
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);
120 extern void bt_value_get_ref(const bt_value *value);
122 extern void bt_value_put_ref(const bt_value *value);
126 extern bt_value *const bt_value_null;
128 extern bt_value *bt_value_bool_create(void);
130 extern bt_value *bt_value_bool_create_init(bt_bool val);
132 extern void bt_value_bool_set(bt_value *bool_obj, bt_bool val);
134 extern bt_value *bt_value_integer_create(void);
136 extern bt_value *bt_value_integer_create_init(
139 extern void bt_value_integer_set(bt_value *integer_obj, int64_t val);
141 extern bt_value *bt_value_real_create(void);
143 extern bt_value *bt_value_real_create_init(double val);
145 extern void bt_value_real_set(bt_value *real_obj, double val);
147 extern bt_value *bt_value_string_create(void);
149 extern bt_value *bt_value_string_create_init(const char *val);
151 extern bt_value_status bt_value_string_set(bt_value *string_obj,
154 extern bt_value *bt_value_array_create(void);
156 extern bt_value *bt_value_array_borrow_element_by_index(
157 bt_value *array_obj, uint64_t index);
159 extern bt_value_status bt_value_array_append_element(
161 bt_value *element_obj);
163 extern bt_value_status bt_value_array_append_bool_element(
164 bt_value *array_obj, bt_bool val);
166 extern bt_value_status bt_value_array_append_integer_element(
167 bt_value *array_obj, int64_t val);
169 extern bt_value_status bt_value_array_append_real_element(
170 bt_value *array_obj, double val);
172 extern bt_value_status bt_value_array_append_string_element(
173 bt_value *array_obj, const char *val);
175 extern bt_value_status bt_value_array_append_empty_array_element(
176 bt_value *array_obj);
178 extern bt_value_status bt_value_array_append_empty_map_element(
179 bt_value *array_obj);
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);
185 extern bt_value *bt_value_map_create(void);
187 extern bt_value *bt_value_map_borrow_entry_value(
188 bt_value *map_obj, const char *key);
190 typedef bt_bool (* bt_value_map_foreach_entry_func)(const char *key,
191 bt_value *object, void *data);
193 extern bt_value_status bt_value_map_foreach_entry(
195 bt_value_map_foreach_entry_func func, void *data);
197 extern bt_value_status bt_value_map_insert_entry(
198 bt_value *map_obj, const char *key,
199 bt_value *element_obj);
201 extern bt_value_status bt_value_map_insert_bool_entry(
202 bt_value *map_obj, const char *key, bt_bool val);
204 extern bt_value_status bt_value_map_insert_integer_entry(
205 bt_value *map_obj, const char *key, int64_t val);
207 extern bt_value_status bt_value_map_insert_real_entry(
208 bt_value *map_obj, const char *key, double val);
210 extern bt_value_status bt_value_map_insert_string_entry(
211 bt_value *map_obj, const char *key,
214 extern bt_value_status bt_value_map_insert_empty_array_entry(
215 bt_value *map_obj, const char *key);
217 extern bt_value_status bt_value_map_insert_empty_map_entry(
218 bt_value *map_obj, const char *key);
221 struct bt_value_map_get_keys_data {
222 struct bt_value *keys;
225 static int bt_value_map_get_keys_cb(const char *key, const struct bt_value *object, void *data)
227 enum bt_value_status status;
228 struct bt_value_map_get_keys_data *priv_data = data;
230 status = bt_value_array_append_string_element(priv_data->keys, key);
231 if (status != BT_VALUE_STATUS_OK) {
238 static struct bt_value *bt_value_map_get_keys(const struct bt_value *map_obj)
240 enum bt_value_status status;
241 struct bt_value_map_get_keys_data data;
243 data.keys = bt_value_array_create();
248 status = bt_value_map_foreach_entry_const(map_obj, bt_value_map_get_keys_cb,
250 if (status != BT_VALUE_STATUS_OK) {
258 BT_VALUE_PUT_REF_AND_RESET(data.keys);
266 struct bt_value *bt_value_map_get_keys(const struct bt_value *map_obj);