Commit | Line | Data |
---|---|---|
dac5c838 | 1 | /* |
e2f7325d | 2 | * Copyright (c) 2015-2018 Philippe Proulx <pproulx@efficios.com> |
dac5c838 PP |
3 | * |
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 | * of this software and associated documentation files (the "Software"), to deal | |
6 | * in the Software without restriction, including without limitation the rights | |
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 | * copies of the Software, and to permit persons to whom the Software is | |
9 | * furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | * | |
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
20 | * SOFTWARE. | |
21 | */ | |
22 | ||
0f5e83e5 | 23 | #define BT_LOG_TAG "VALUES" |
40547d22 | 24 | #include <babeltrace/lib-logging-internal.h> |
0f5e83e5 | 25 | |
dac5c838 PP |
26 | #include <stdlib.h> |
27 | #include <string.h> | |
dac5c838 | 28 | #include <string.h> |
2f42aa0a | 29 | #include <inttypes.h> |
3d9990ac | 30 | #include <babeltrace/compiler-internal.h> |
da91b29a | 31 | #include <babeltrace/common-internal.h> |
c6bd8523 PP |
32 | #include <babeltrace/value-const.h> |
33 | #include <babeltrace/value.h> | |
3d9990ac | 34 | #include <babeltrace/compat/glib-internal.h> |
c55a9f58 | 35 | #include <babeltrace/types.h> |
c5b9b441 | 36 | #include <babeltrace/assert-pre-internal.h> |
c6bd8523 | 37 | #include <babeltrace/value-internal.h> |
f6ccaed9 | 38 | #include <babeltrace/assert-internal.h> |
2f42aa0a | 39 | |
dac5c838 PP |
40 | #define BT_VALUE_FROM_CONCRETE(_concrete) ((struct bt_value *) (_concrete)) |
41 | #define BT_VALUE_TO_BOOL(_base) ((struct bt_value_bool *) (_base)) | |
42 | #define BT_VALUE_TO_INTEGER(_base) ((struct bt_value_integer *) (_base)) | |
a373bf69 | 43 | #define BT_VALUE_TO_REAL(_base) ((struct bt_value_real *) (_base)) |
dac5c838 PP |
44 | #define BT_VALUE_TO_STRING(_base) ((struct bt_value_string *) (_base)) |
45 | #define BT_VALUE_TO_ARRAY(_base) ((struct bt_value_array *) (_base)) | |
46 | #define BT_VALUE_TO_MAP(_base) ((struct bt_value_map *) (_base)) | |
47 | ||
f6ccaed9 | 48 | #define BT_ASSERT_PRE_VALUE_IS_TYPE(_value, _type) \ |
da91b29a | 49 | BT_ASSERT_PRE(((struct bt_value *) (_value))->type == (_type), \ |
f6ccaed9 | 50 | "Value has the wrong type ID: expected-type=%s, " \ |
da91b29a | 51 | "%![value-]+v", bt_common_value_type_string(_type), \ |
f6ccaed9 PP |
52 | (_value)) |
53 | ||
54 | #define BT_ASSERT_PRE_VALUE_HOT(_value, _name) \ | |
da91b29a PP |
55 | BT_ASSERT_PRE_HOT(((struct bt_value *) (_value)), (_name), \ |
56 | ": %!+v", (_value)) | |
f6ccaed9 PP |
57 | |
58 | #define BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(_index, _count) \ | |
59 | BT_ASSERT_PRE((_index) < (_count), \ | |
60 | "Index is out of bound: " \ | |
61 | "index=%" PRIu64 ", count=%u", (_index), (_count)); | |
62 | ||
3fea54f6 PP |
63 | static |
64 | void bt_value_null_instance_release_func(struct bt_object *obj) | |
65 | { | |
66 | BT_LOGW("Releasing the null value singleton: addr=%p", obj); | |
67 | } | |
68 | ||
dac5c838 PP |
69 | static |
70 | struct bt_value bt_value_null_instance = { | |
f685129c | 71 | .base = { |
312c056a | 72 | .is_shared = true, |
3fea54f6 PP |
73 | .ref_count = 1, |
74 | .release_func = bt_value_null_instance_release_func, | |
75 | .spec_release_func = NULL, | |
76 | .parent_is_owner_listener_func = NULL, | |
77 | .parent = NULL, | |
f685129c | 78 | }, |
dac5c838 | 79 | .type = BT_VALUE_TYPE_NULL, |
f6ccaed9 | 80 | .frozen = BT_TRUE, |
dac5c838 PP |
81 | }; |
82 | ||
211796dc | 83 | struct bt_value *const bt_value_null = &bt_value_null_instance; |
dac5c838 | 84 | |
dac5c838 | 85 | static |
83509119 | 86 | void bt_value_destroy(struct bt_object *obj); |
dac5c838 PP |
87 | |
88 | static | |
89 | void bt_value_string_destroy(struct bt_value *object) | |
90 | { | |
91 | g_string_free(BT_VALUE_TO_STRING(object)->gstr, TRUE); | |
238b7404 | 92 | BT_VALUE_TO_STRING(object)->gstr = NULL; |
dac5c838 PP |
93 | } |
94 | ||
95 | static | |
96 | void bt_value_array_destroy(struct bt_value *object) | |
97 | { | |
98 | /* | |
99 | * Pointer array's registered value destructor will take care | |
100 | * of putting each contained object. | |
101 | */ | |
102 | g_ptr_array_free(BT_VALUE_TO_ARRAY(object)->garray, TRUE); | |
238b7404 | 103 | BT_VALUE_TO_ARRAY(object)->garray = NULL; |
dac5c838 PP |
104 | } |
105 | ||
106 | static | |
107 | void bt_value_map_destroy(struct bt_value *object) | |
108 | { | |
109 | /* | |
110 | * Hash table's registered value destructor will take care of | |
111 | * putting each contained object. Keys are GQuarks and cannot | |
112 | * be destroyed anyway. | |
113 | */ | |
114 | g_hash_table_destroy(BT_VALUE_TO_MAP(object)->ght); | |
238b7404 | 115 | BT_VALUE_TO_MAP(object)->ght = NULL; |
dac5c838 PP |
116 | } |
117 | ||
118 | static | |
119 | void (* const destroy_funcs[])(struct bt_value *) = { | |
120 | [BT_VALUE_TYPE_NULL] = NULL, | |
121 | [BT_VALUE_TYPE_BOOL] = NULL, | |
122 | [BT_VALUE_TYPE_INTEGER] = NULL, | |
a373bf69 | 123 | [BT_VALUE_TYPE_REAL] = NULL, |
dac5c838 PP |
124 | [BT_VALUE_TYPE_STRING] = bt_value_string_destroy, |
125 | [BT_VALUE_TYPE_ARRAY] = bt_value_array_destroy, | |
126 | [BT_VALUE_TYPE_MAP] = bt_value_map_destroy, | |
127 | }; | |
128 | ||
129 | static | |
05e21286 | 130 | struct bt_value *bt_value_null_copy(const struct bt_value *null_obj) |
dac5c838 | 131 | { |
da91b29a | 132 | return (void *) bt_value_null; |
dac5c838 PP |
133 | } |
134 | ||
135 | static | |
05e21286 | 136 | struct bt_value *bt_value_bool_copy(const struct bt_value *bool_obj) |
dac5c838 | 137 | { |
05e21286 | 138 | return bt_value_bool_create_init( |
601b0d3c | 139 | BT_VALUE_TO_BOOL(bool_obj)->value); |
dac5c838 PP |
140 | } |
141 | ||
142 | static | |
05e21286 | 143 | struct bt_value *bt_value_integer_copy( |
da91b29a | 144 | const struct bt_value *integer_obj) |
dac5c838 | 145 | { |
05e21286 | 146 | return bt_value_integer_create_init( |
dac5c838 PP |
147 | BT_VALUE_TO_INTEGER(integer_obj)->value); |
148 | } | |
149 | ||
150 | static | |
05e21286 | 151 | struct bt_value *bt_value_real_copy(const struct bt_value *real_obj) |
dac5c838 | 152 | { |
05e21286 | 153 | return bt_value_real_create_init( |
a373bf69 | 154 | BT_VALUE_TO_REAL(real_obj)->value); |
dac5c838 PP |
155 | } |
156 | ||
157 | static | |
05e21286 | 158 | struct bt_value *bt_value_string_copy(const struct bt_value *string_obj) |
dac5c838 | 159 | { |
05e21286 | 160 | return bt_value_string_create_init( |
dac5c838 PP |
161 | BT_VALUE_TO_STRING(string_obj)->gstr->str); |
162 | } | |
163 | ||
164 | static | |
05e21286 | 165 | struct bt_value *bt_value_array_copy(const struct bt_value *array_obj) |
dac5c838 PP |
166 | { |
167 | int i; | |
168 | int ret; | |
05e21286 | 169 | struct bt_value *copy_obj; |
dac5c838 PP |
170 | struct bt_value_array *typed_array_obj; |
171 | ||
2f42aa0a | 172 | BT_LOGD("Copying array value: addr=%p", array_obj); |
dac5c838 | 173 | typed_array_obj = BT_VALUE_TO_ARRAY(array_obj); |
05e21286 | 174 | copy_obj = bt_value_array_create(); |
dac5c838 | 175 | if (!copy_obj) { |
2f42aa0a | 176 | BT_LOGE_STR("Cannot create empty array value."); |
dac5c838 PP |
177 | goto end; |
178 | } | |
179 | ||
180 | for (i = 0; i < typed_array_obj->garray->len; ++i) { | |
05e21286 PP |
181 | struct bt_value *element_obj_copy = NULL; |
182 | const struct bt_value *element_obj = | |
183 | bt_value_array_borrow_element_by_index_const( | |
601b0d3c | 184 | array_obj, i); |
dac5c838 | 185 | |
f6ccaed9 | 186 | BT_ASSERT(element_obj); |
40b59ed9 PP |
187 | BT_LOGD("Copying array value's element: element-addr=%p, " |
188 | "index=%d", element_obj, i); | |
6be5a99e | 189 | ret = bt_value_copy(element_obj, &element_obj_copy); |
601b0d3c | 190 | if (ret) { |
2f42aa0a PP |
191 | BT_LOGE("Cannot copy array value's element: " |
192 | "array-addr=%p, index=%d", | |
193 | array_obj, i); | |
65300d60 | 194 | BT_OBJECT_PUT_REF_AND_RESET(copy_obj); |
dac5c838 PP |
195 | goto end; |
196 | } | |
197 | ||
601b0d3c | 198 | BT_ASSERT(element_obj_copy); |
05e21286 | 199 | ret = bt_value_array_append_element(copy_obj, |
da91b29a | 200 | (void *) element_obj_copy); |
65300d60 | 201 | BT_OBJECT_PUT_REF_AND_RESET(element_obj_copy); |
dac5c838 | 202 | if (ret) { |
2f42aa0a PP |
203 | BT_LOGE("Cannot append to array value: addr=%p", |
204 | array_obj); | |
65300d60 | 205 | BT_OBJECT_PUT_REF_AND_RESET(copy_obj); |
dac5c838 PP |
206 | goto end; |
207 | } | |
208 | } | |
209 | ||
a704fb0b PP |
210 | BT_LOGD("Copied array value: original-addr=%p, copy-addr=%p", |
211 | array_obj, copy_obj); | |
2f42aa0a | 212 | |
dac5c838 PP |
213 | end: |
214 | return copy_obj; | |
215 | } | |
216 | ||
217 | static | |
05e21286 | 218 | struct bt_value *bt_value_map_copy(const struct bt_value *map_obj) |
dac5c838 PP |
219 | { |
220 | int ret; | |
221 | GHashTableIter iter; | |
222 | gpointer key, element_obj; | |
05e21286 PP |
223 | struct bt_value *copy_obj; |
224 | struct bt_value *element_obj_copy = NULL; | |
dac5c838 PP |
225 | struct bt_value_map *typed_map_obj; |
226 | ||
2f42aa0a | 227 | BT_LOGD("Copying map value: addr=%p", map_obj); |
dac5c838 | 228 | typed_map_obj = BT_VALUE_TO_MAP(map_obj); |
05e21286 | 229 | copy_obj = bt_value_map_create(); |
dac5c838 PP |
230 | if (!copy_obj) { |
231 | goto end; | |
232 | } | |
233 | ||
234 | g_hash_table_iter_init(&iter, typed_map_obj->ght); | |
235 | ||
236 | while (g_hash_table_iter_next(&iter, &key, &element_obj)) { | |
5b44aff2 | 237 | const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key)); |
dac5c838 | 238 | |
f6ccaed9 | 239 | BT_ASSERT(key_str); |
40b59ed9 PP |
240 | BT_LOGD("Copying map value's element: element-addr=%p, " |
241 | "key=\"%s\"", element_obj, key_str); | |
6be5a99e | 242 | ret = bt_value_copy(element_obj, &element_obj_copy); |
601b0d3c | 243 | if (ret) { |
2f42aa0a PP |
244 | BT_LOGE("Cannot copy map value's element: " |
245 | "map-addr=%p, key=\"%s\"", | |
246 | map_obj, key_str); | |
65300d60 | 247 | BT_OBJECT_PUT_REF_AND_RESET(copy_obj); |
dac5c838 PP |
248 | goto end; |
249 | } | |
250 | ||
601b0d3c | 251 | BT_ASSERT(element_obj_copy); |
05e21286 | 252 | ret = bt_value_map_insert_entry(copy_obj, key_str, |
da91b29a | 253 | (void *) element_obj_copy); |
65300d60 | 254 | BT_OBJECT_PUT_REF_AND_RESET(element_obj_copy); |
dac5c838 | 255 | if (ret) { |
2f42aa0a PP |
256 | BT_LOGE("Cannot insert into map value: addr=%p, key=\"%s\"", |
257 | map_obj, key_str); | |
65300d60 | 258 | BT_OBJECT_PUT_REF_AND_RESET(copy_obj); |
dac5c838 PP |
259 | goto end; |
260 | } | |
261 | } | |
262 | ||
2f42aa0a PP |
263 | BT_LOGD("Copied map value: addr=%p", map_obj); |
264 | ||
dac5c838 PP |
265 | end: |
266 | return copy_obj; | |
267 | } | |
268 | ||
269 | static | |
05e21286 | 270 | struct bt_value *(* const copy_funcs[])(const struct bt_value *) = { |
dac5c838 PP |
271 | [BT_VALUE_TYPE_NULL] = bt_value_null_copy, |
272 | [BT_VALUE_TYPE_BOOL] = bt_value_bool_copy, | |
273 | [BT_VALUE_TYPE_INTEGER] = bt_value_integer_copy, | |
a373bf69 | 274 | [BT_VALUE_TYPE_REAL] = bt_value_real_copy, |
dac5c838 PP |
275 | [BT_VALUE_TYPE_STRING] = bt_value_string_copy, |
276 | [BT_VALUE_TYPE_ARRAY] = bt_value_array_copy, | |
277 | [BT_VALUE_TYPE_MAP] = bt_value_map_copy, | |
278 | }; | |
279 | ||
280 | static | |
c55a9f58 | 281 | bt_bool bt_value_null_compare(const struct bt_value *object_a, |
dac5c838 PP |
282 | const struct bt_value *object_b) |
283 | { | |
284 | /* | |
c55a9f58 | 285 | * Always BT_TRUE since bt_value_compare() already checks if both |
dac5c838 PP |
286 | * object_a and object_b have the same type, and in the case of |
287 | * null value objects, they're always the same if it is so. | |
288 | */ | |
c55a9f58 | 289 | return BT_TRUE; |
dac5c838 PP |
290 | } |
291 | ||
292 | static | |
c55a9f58 | 293 | bt_bool bt_value_bool_compare(const struct bt_value *object_a, |
dac5c838 PP |
294 | const struct bt_value *object_b) |
295 | { | |
40b59ed9 PP |
296 | if (BT_VALUE_TO_BOOL(object_a)->value != |
297 | BT_VALUE_TO_BOOL(object_b)->value) { | |
298 | BT_LOGV("Boolean value objects are different: " | |
299 | "bool-a-val=%d, bool-b-val=%d", | |
300 | BT_VALUE_TO_BOOL(object_a)->value, | |
301 | BT_VALUE_TO_BOOL(object_b)->value); | |
302 | return BT_FALSE; | |
303 | } | |
304 | ||
305 | return BT_TRUE; | |
dac5c838 PP |
306 | } |
307 | ||
308 | static | |
c55a9f58 | 309 | bt_bool bt_value_integer_compare(const struct bt_value *object_a, |
dac5c838 PP |
310 | const struct bt_value *object_b) |
311 | { | |
40b59ed9 PP |
312 | if (BT_VALUE_TO_INTEGER(object_a)->value != |
313 | BT_VALUE_TO_INTEGER(object_b)->value) { | |
314 | BT_LOGV("Integer value objects are different: " | |
315 | "int-a-val=%" PRId64 ", int-b-val=%" PRId64, | |
316 | BT_VALUE_TO_INTEGER(object_a)->value, | |
317 | BT_VALUE_TO_INTEGER(object_b)->value); | |
318 | return BT_FALSE; | |
319 | } | |
320 | ||
321 | return BT_TRUE; | |
dac5c838 PP |
322 | } |
323 | ||
324 | static | |
a373bf69 | 325 | bt_bool bt_value_real_compare(const struct bt_value *object_a, |
dac5c838 PP |
326 | const struct bt_value *object_b) |
327 | { | |
a373bf69 PP |
328 | if (BT_VALUE_TO_REAL(object_a)->value != |
329 | BT_VALUE_TO_REAL(object_b)->value) { | |
330 | BT_LOGV("Real number value objects are different: " | |
331 | "real-a-val=%f, real-b-val=%f", | |
332 | BT_VALUE_TO_REAL(object_a)->value, | |
333 | BT_VALUE_TO_REAL(object_b)->value); | |
40b59ed9 PP |
334 | return BT_FALSE; |
335 | } | |
336 | ||
337 | return BT_TRUE; | |
dac5c838 PP |
338 | } |
339 | ||
340 | static | |
c55a9f58 | 341 | bt_bool bt_value_string_compare(const struct bt_value *object_a, |
dac5c838 PP |
342 | const struct bt_value *object_b) |
343 | { | |
40b59ed9 PP |
344 | if (strcmp(BT_VALUE_TO_STRING(object_a)->gstr->str, |
345 | BT_VALUE_TO_STRING(object_b)->gstr->str) != 0) { | |
346 | BT_LOGV("String value objects are different: " | |
347 | "string-a-val=\"%s\", string-b-val=\"%s\"", | |
348 | BT_VALUE_TO_STRING(object_a)->gstr->str, | |
349 | BT_VALUE_TO_STRING(object_b)->gstr->str); | |
350 | return BT_FALSE; | |
351 | } | |
352 | ||
353 | return BT_TRUE; | |
dac5c838 PP |
354 | } |
355 | ||
356 | static | |
c55a9f58 | 357 | bt_bool bt_value_array_compare(const struct bt_value *object_a, |
dac5c838 PP |
358 | const struct bt_value *object_b) |
359 | { | |
360 | int i; | |
c55a9f58 | 361 | bt_bool ret = BT_TRUE; |
dac5c838 PP |
362 | const struct bt_value_array *array_obj_a = |
363 | BT_VALUE_TO_ARRAY(object_a); | |
364 | ||
da91b29a PP |
365 | if (bt_value_array_get_size(object_a) != |
366 | bt_value_array_get_size(object_b)) { | |
2f42aa0a PP |
367 | BT_LOGV("Array values are different: size mismatch " |
368 | "value-a-addr=%p, value-b-addr=%p, " | |
369 | "value-a-size=%" PRId64 ", value-b-size=%" PRId64, | |
370 | object_a, object_b, | |
07208d85 PP |
371 | bt_value_array_get_size(object_a), |
372 | bt_value_array_get_size(object_b)); | |
c55a9f58 | 373 | ret = BT_FALSE; |
dac5c838 PP |
374 | goto end; |
375 | } | |
376 | ||
377 | for (i = 0; i < array_obj_a->garray->len; ++i) { | |
05e21286 PP |
378 | const struct bt_value *element_obj_a; |
379 | const struct bt_value *element_obj_b; | |
dac5c838 | 380 | |
05e21286 | 381 | element_obj_a = bt_value_array_borrow_element_by_index_const( |
da91b29a | 382 | object_a, i); |
05e21286 | 383 | element_obj_b = bt_value_array_borrow_element_by_index_const( |
da91b29a | 384 | object_b, i); |
dac5c838 PP |
385 | |
386 | if (!bt_value_compare(element_obj_a, element_obj_b)) { | |
2f42aa0a PP |
387 | BT_LOGV("Array values's elements are different: " |
388 | "value-a-addr=%p, value-b-addr=%p, index=%d", | |
32e87ceb | 389 | element_obj_a, element_obj_b, i); |
c55a9f58 | 390 | ret = BT_FALSE; |
dac5c838 PP |
391 | goto end; |
392 | } | |
dac5c838 PP |
393 | } |
394 | ||
395 | end: | |
396 | return ret; | |
397 | } | |
398 | ||
399 | static | |
c55a9f58 | 400 | bt_bool bt_value_map_compare(const struct bt_value *object_a, |
dac5c838 PP |
401 | const struct bt_value *object_b) |
402 | { | |
c55a9f58 | 403 | bt_bool ret = BT_TRUE; |
dac5c838 PP |
404 | GHashTableIter iter; |
405 | gpointer key, element_obj_a; | |
406 | const struct bt_value_map *map_obj_a = BT_VALUE_TO_MAP(object_a); | |
407 | ||
601b0d3c PP |
408 | if (bt_value_map_get_size(object_a) != |
409 | bt_value_map_get_size(object_b)) { | |
2f42aa0a PP |
410 | BT_LOGV("Map values are different: size mismatch " |
411 | "value-a-addr=%p, value-b-addr=%p, " | |
412 | "value-a-size=%" PRId64 ", value-b-size=%" PRId64, | |
413 | object_a, object_b, | |
07208d85 PP |
414 | bt_value_map_get_size(object_a), |
415 | bt_value_map_get_size(object_b)); | |
c55a9f58 | 416 | ret = BT_FALSE; |
dac5c838 PP |
417 | goto end; |
418 | } | |
419 | ||
420 | g_hash_table_iter_init(&iter, map_obj_a->ght); | |
421 | ||
422 | while (g_hash_table_iter_next(&iter, &key, &element_obj_a)) { | |
05e21286 | 423 | const struct bt_value *element_obj_b; |
5b44aff2 | 424 | const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key)); |
dac5c838 | 425 | |
05e21286 | 426 | element_obj_b = bt_value_map_borrow_entry_value_const(object_b, |
da91b29a | 427 | key_str); |
dac5c838 PP |
428 | |
429 | if (!bt_value_compare(element_obj_a, element_obj_b)) { | |
2f42aa0a PP |
430 | BT_LOGV("Map values's elements are different: " |
431 | "value-a-addr=%p, value-b-addr=%p, key=\"%s\"", | |
432 | element_obj_a, element_obj_b, key_str); | |
c55a9f58 | 433 | ret = BT_FALSE; |
dac5c838 PP |
434 | goto end; |
435 | } | |
dac5c838 PP |
436 | } |
437 | ||
438 | end: | |
439 | return ret; | |
440 | } | |
441 | ||
442 | static | |
c55a9f58 | 443 | bt_bool (* const compare_funcs[])(const struct bt_value *, |
dac5c838 PP |
444 | const struct bt_value *) = { |
445 | [BT_VALUE_TYPE_NULL] = bt_value_null_compare, | |
446 | [BT_VALUE_TYPE_BOOL] = bt_value_bool_compare, | |
447 | [BT_VALUE_TYPE_INTEGER] = bt_value_integer_compare, | |
a373bf69 | 448 | [BT_VALUE_TYPE_REAL] = bt_value_real_compare, |
dac5c838 | 449 | [BT_VALUE_TYPE_STRING] = bt_value_string_compare, |
40b59ed9 | 450 | [BT_VALUE_TYPE_ARRAY] = bt_value_array_compare, |
dac5c838 PP |
451 | [BT_VALUE_TYPE_MAP] = bt_value_map_compare, |
452 | }; | |
453 | ||
f6ccaed9 | 454 | static |
dac5c838 PP |
455 | void bt_value_null_freeze(struct bt_value *object) |
456 | { | |
457 | } | |
458 | ||
f6ccaed9 | 459 | static |
dac5c838 PP |
460 | void bt_value_generic_freeze(struct bt_value *object) |
461 | { | |
f6ccaed9 | 462 | object->frozen = BT_TRUE; |
dac5c838 PP |
463 | } |
464 | ||
f6ccaed9 | 465 | static |
dac5c838 PP |
466 | void bt_value_array_freeze(struct bt_value *object) |
467 | { | |
468 | int i; | |
469 | struct bt_value_array *typed_array_obj = | |
470 | BT_VALUE_TO_ARRAY(object); | |
471 | ||
472 | for (i = 0; i < typed_array_obj->garray->len; ++i) { | |
f6ccaed9 | 473 | bt_value_freeze(g_ptr_array_index(typed_array_obj->garray, i)); |
dac5c838 PP |
474 | } |
475 | ||
476 | bt_value_generic_freeze(object); | |
477 | } | |
478 | ||
f6ccaed9 | 479 | static |
dac5c838 PP |
480 | void bt_value_map_freeze(struct bt_value *object) |
481 | { | |
482 | GHashTableIter iter; | |
483 | gpointer key, element_obj; | |
484 | const struct bt_value_map *map_obj = BT_VALUE_TO_MAP(object); | |
485 | ||
486 | g_hash_table_iter_init(&iter, map_obj->ght); | |
487 | ||
488 | while (g_hash_table_iter_next(&iter, &key, &element_obj)) { | |
489 | bt_value_freeze(element_obj); | |
490 | } | |
491 | ||
492 | bt_value_generic_freeze(object); | |
493 | } | |
494 | ||
495 | static | |
496 | void (* const freeze_funcs[])(struct bt_value *) = { | |
497 | [BT_VALUE_TYPE_NULL] = bt_value_null_freeze, | |
498 | [BT_VALUE_TYPE_BOOL] = bt_value_generic_freeze, | |
499 | [BT_VALUE_TYPE_INTEGER] = bt_value_generic_freeze, | |
a373bf69 | 500 | [BT_VALUE_TYPE_REAL] = bt_value_generic_freeze, |
dac5c838 PP |
501 | [BT_VALUE_TYPE_STRING] = bt_value_generic_freeze, |
502 | [BT_VALUE_TYPE_ARRAY] = bt_value_array_freeze, | |
503 | [BT_VALUE_TYPE_MAP] = bt_value_map_freeze, | |
504 | }; | |
505 | ||
506 | static | |
83509119 | 507 | void bt_value_destroy(struct bt_object *obj) |
dac5c838 | 508 | { |
83509119 | 509 | struct bt_value *value; |
dac5c838 | 510 | |
83509119 | 511 | value = container_of(obj, struct bt_value, base); |
2f42aa0a PP |
512 | BT_LOGD("Destroying value: addr=%p", value); |
513 | ||
83509119 | 514 | if (bt_value_is_null(value)) { |
2f42aa0a | 515 | BT_LOGD_STR("Not destroying the null value singleton."); |
dac5c838 PP |
516 | return; |
517 | } | |
518 | ||
83509119 JG |
519 | if (destroy_funcs[value->type]) { |
520 | destroy_funcs[value->type](value); | |
dac5c838 PP |
521 | } |
522 | ||
83509119 | 523 | g_free(value); |
dac5c838 PP |
524 | } |
525 | ||
f6ccaed9 | 526 | BT_HIDDEN |
05e21286 | 527 | enum bt_value_status _bt_value_freeze(const struct bt_value *c_object) |
dac5c838 | 528 | { |
05e21286 | 529 | const struct bt_value *object = (void *) c_object; |
dac5c838 PP |
530 | enum bt_value_status ret = BT_VALUE_STATUS_OK; |
531 | ||
f6ccaed9 | 532 | BT_ASSERT(object); |
dac5c838 | 533 | |
f6ccaed9 | 534 | if (object->frozen) { |
2f42aa0a PP |
535 | goto end; |
536 | } | |
537 | ||
538 | BT_LOGD("Freezing value: addr=%p", object); | |
05e21286 | 539 | freeze_funcs[object->type]((void *) object); |
dac5c838 PP |
540 | |
541 | end: | |
542 | return ret; | |
543 | } | |
544 | ||
dac5c838 PP |
545 | enum bt_value_type bt_value_get_type(const struct bt_value *object) |
546 | { | |
f6ccaed9 | 547 | BT_ASSERT_PRE_NON_NULL(object, "Value object"); |
dac5c838 PP |
548 | return object->type; |
549 | } | |
550 | ||
551 | static | |
552 | struct bt_value bt_value_create_base(enum bt_value_type type) | |
553 | { | |
3fea54f6 | 554 | struct bt_value value; |
dac5c838 | 555 | |
3fea54f6 PP |
556 | value.type = type; |
557 | value.frozen = BT_FALSE; | |
558 | bt_object_init_shared(&value.base, bt_value_destroy); | |
559 | return value; | |
dac5c838 PP |
560 | } |
561 | ||
05e21286 | 562 | struct bt_value *bt_value_bool_create_init(bt_bool val) |
dac5c838 PP |
563 | { |
564 | struct bt_value_bool *bool_obj; | |
565 | ||
2f42aa0a | 566 | BT_LOGD("Creating boolean value object: val=%d", val); |
dac5c838 | 567 | bool_obj = g_new0(struct bt_value_bool, 1); |
dac5c838 | 568 | if (!bool_obj) { |
2f42aa0a | 569 | BT_LOGE_STR("Failed to allocate one boolean value object."); |
dac5c838 PP |
570 | goto end; |
571 | } | |
572 | ||
573 | bool_obj->base = bt_value_create_base(BT_VALUE_TYPE_BOOL); | |
574 | bool_obj->value = val; | |
2f42aa0a | 575 | BT_LOGD("Created boolean value object: addr=%p", bool_obj); |
dac5c838 PP |
576 | |
577 | end: | |
da91b29a | 578 | return (void *) BT_VALUE_FROM_CONCRETE(bool_obj); |
dac5c838 PP |
579 | } |
580 | ||
05e21286 | 581 | struct bt_value *bt_value_bool_create(void) |
dac5c838 | 582 | { |
05e21286 | 583 | return bt_value_bool_create_init(BT_FALSE); |
dac5c838 PP |
584 | } |
585 | ||
05e21286 | 586 | struct bt_value *bt_value_integer_create_init(int64_t val) |
dac5c838 PP |
587 | { |
588 | struct bt_value_integer *integer_obj; | |
589 | ||
2f42aa0a | 590 | BT_LOGD("Creating integer value object: val=%" PRId64, val); |
dac5c838 | 591 | integer_obj = g_new0(struct bt_value_integer, 1); |
dac5c838 | 592 | if (!integer_obj) { |
2f42aa0a | 593 | BT_LOGE_STR("Failed to allocate one integer value object."); |
dac5c838 PP |
594 | goto end; |
595 | } | |
596 | ||
597 | integer_obj->base = bt_value_create_base(BT_VALUE_TYPE_INTEGER); | |
598 | integer_obj->value = val; | |
2f42aa0a PP |
599 | BT_LOGD("Created integer value object: addr=%p", |
600 | integer_obj); | |
dac5c838 PP |
601 | |
602 | end: | |
da91b29a | 603 | return (void *) BT_VALUE_FROM_CONCRETE(integer_obj); |
dac5c838 PP |
604 | } |
605 | ||
05e21286 | 606 | struct bt_value *bt_value_integer_create(void) |
dac5c838 | 607 | { |
05e21286 | 608 | return bt_value_integer_create_init(0); |
dac5c838 PP |
609 | } |
610 | ||
05e21286 | 611 | struct bt_value *bt_value_real_create_init(double val) |
dac5c838 | 612 | { |
a373bf69 | 613 | struct bt_value_real *real_obj; |
dac5c838 | 614 | |
a373bf69 PP |
615 | BT_LOGD("Creating real number value object: val=%f", val); |
616 | real_obj = g_new0(struct bt_value_real, 1); | |
617 | if (!real_obj) { | |
618 | BT_LOGE_STR("Failed to allocate one real number value object."); | |
dac5c838 PP |
619 | goto end; |
620 | } | |
621 | ||
a373bf69 PP |
622 | real_obj->base = bt_value_create_base(BT_VALUE_TYPE_REAL); |
623 | real_obj->value = val; | |
624 | BT_LOGD("Created real number value object: addr=%p", | |
625 | real_obj); | |
dac5c838 PP |
626 | |
627 | end: | |
da91b29a | 628 | return (void *) BT_VALUE_FROM_CONCRETE(real_obj); |
dac5c838 PP |
629 | } |
630 | ||
05e21286 | 631 | struct bt_value *bt_value_real_create(void) |
dac5c838 | 632 | { |
05e21286 | 633 | return bt_value_real_create_init(0.); |
dac5c838 PP |
634 | } |
635 | ||
05e21286 | 636 | struct bt_value *bt_value_string_create_init(const char *val) |
dac5c838 PP |
637 | { |
638 | struct bt_value_string *string_obj = NULL; | |
639 | ||
640 | if (!val) { | |
2f42aa0a | 641 | BT_LOGW_STR("Invalid parameter: value is NULL."); |
dac5c838 PP |
642 | goto end; |
643 | } | |
644 | ||
003c7749 | 645 | BT_LOGD("Creating string value object: val-len=%zu", strlen(val)); |
dac5c838 | 646 | string_obj = g_new0(struct bt_value_string, 1); |
dac5c838 | 647 | if (!string_obj) { |
2f42aa0a | 648 | BT_LOGE_STR("Failed to allocate one string object."); |
dac5c838 PP |
649 | goto end; |
650 | } | |
651 | ||
652 | string_obj->base = bt_value_create_base(BT_VALUE_TYPE_STRING); | |
653 | string_obj->gstr = g_string_new(val); | |
dac5c838 | 654 | if (!string_obj->gstr) { |
2f42aa0a | 655 | BT_LOGE_STR("Failed to allocate a GString."); |
dac5c838 PP |
656 | g_free(string_obj); |
657 | string_obj = NULL; | |
658 | goto end; | |
659 | } | |
660 | ||
2f42aa0a PP |
661 | BT_LOGD("Created string value object: addr=%p", |
662 | string_obj); | |
663 | ||
dac5c838 | 664 | end: |
da91b29a | 665 | return (void *) BT_VALUE_FROM_CONCRETE(string_obj); |
dac5c838 PP |
666 | } |
667 | ||
05e21286 | 668 | struct bt_value *bt_value_string_create(void) |
dac5c838 | 669 | { |
05e21286 | 670 | return bt_value_string_create_init(""); |
dac5c838 PP |
671 | } |
672 | ||
05e21286 | 673 | struct bt_value *bt_value_array_create(void) |
dac5c838 PP |
674 | { |
675 | struct bt_value_array *array_obj; | |
676 | ||
2f42aa0a | 677 | BT_LOGD_STR("Creating empty array value object."); |
dac5c838 | 678 | array_obj = g_new0(struct bt_value_array, 1); |
dac5c838 | 679 | if (!array_obj) { |
2f42aa0a | 680 | BT_LOGE_STR("Failed to allocate one array object."); |
dac5c838 PP |
681 | goto end; |
682 | } | |
683 | ||
684 | array_obj->base = bt_value_create_base(BT_VALUE_TYPE_ARRAY); | |
5d5982ab | 685 | array_obj->garray = bt_g_ptr_array_new_full(0, |
65300d60 | 686 | (GDestroyNotify) bt_object_put_ref); |
dac5c838 | 687 | if (!array_obj->garray) { |
2f42aa0a | 688 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
dac5c838 PP |
689 | g_free(array_obj); |
690 | array_obj = NULL; | |
691 | goto end; | |
692 | } | |
693 | ||
2f42aa0a PP |
694 | BT_LOGD("Created array value object: addr=%p", |
695 | array_obj); | |
696 | ||
dac5c838 | 697 | end: |
da91b29a | 698 | return (void *) BT_VALUE_FROM_CONCRETE(array_obj); |
dac5c838 PP |
699 | } |
700 | ||
05e21286 | 701 | struct bt_value *bt_value_map_create(void) |
dac5c838 PP |
702 | { |
703 | struct bt_value_map *map_obj; | |
704 | ||
2f42aa0a | 705 | BT_LOGD_STR("Creating empty map value object."); |
dac5c838 | 706 | map_obj = g_new0(struct bt_value_map, 1); |
dac5c838 | 707 | if (!map_obj) { |
2f42aa0a | 708 | BT_LOGE_STR("Failed to allocate one map object."); |
dac5c838 PP |
709 | goto end; |
710 | } | |
711 | ||
712 | map_obj->base = bt_value_create_base(BT_VALUE_TYPE_MAP); | |
713 | map_obj->ght = g_hash_table_new_full(g_direct_hash, g_direct_equal, | |
65300d60 | 714 | NULL, (GDestroyNotify) bt_object_put_ref); |
dac5c838 | 715 | if (!map_obj->ght) { |
2f42aa0a | 716 | BT_LOGE_STR("Failed to allocate a GHashTable."); |
dac5c838 PP |
717 | g_free(map_obj); |
718 | map_obj = NULL; | |
719 | goto end; | |
720 | } | |
721 | ||
2f42aa0a PP |
722 | BT_LOGD("Created map value object: addr=%p", |
723 | map_obj); | |
724 | ||
dac5c838 | 725 | end: |
da91b29a | 726 | return (void *) BT_VALUE_FROM_CONCRETE(map_obj); |
dac5c838 PP |
727 | } |
728 | ||
601b0d3c | 729 | bt_bool bt_value_bool_get(const struct bt_value *bool_obj) |
dac5c838 | 730 | { |
f6ccaed9 | 731 | BT_ASSERT_PRE_NON_NULL(bool_obj, "Value object"); |
f6ccaed9 | 732 | BT_ASSERT_PRE_VALUE_IS_TYPE(bool_obj, BT_VALUE_TYPE_BOOL); |
601b0d3c | 733 | return BT_VALUE_TO_BOOL(bool_obj)->value; |
dac5c838 PP |
734 | } |
735 | ||
05e21286 | 736 | void bt_value_bool_set(struct bt_value *bool_obj, bt_bool val) |
dac5c838 | 737 | { |
f6ccaed9 PP |
738 | BT_ASSERT_PRE_NON_NULL(bool_obj, "Value object"); |
739 | BT_ASSERT_PRE_VALUE_IS_TYPE(bool_obj, BT_VALUE_TYPE_BOOL); | |
740 | BT_ASSERT_PRE_VALUE_HOT(bool_obj, "Value object"); | |
741 | BT_VALUE_TO_BOOL(bool_obj)->value = val; | |
2f42aa0a PP |
742 | BT_LOGV("Set boolean value's raw value: value-addr=%p, value=%d", |
743 | bool_obj, val); | |
dac5c838 PP |
744 | } |
745 | ||
601b0d3c | 746 | int64_t bt_value_integer_get(const struct bt_value *integer_obj) |
dac5c838 | 747 | { |
f6ccaed9 | 748 | BT_ASSERT_PRE_NON_NULL(integer_obj, "Value object"); |
f6ccaed9 | 749 | BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj, BT_VALUE_TYPE_INTEGER); |
601b0d3c | 750 | return BT_VALUE_TO_INTEGER(integer_obj)->value; |
dac5c838 PP |
751 | } |
752 | ||
05e21286 | 753 | void bt_value_integer_set(struct bt_value *integer_obj, |
364747d6 | 754 | int64_t val) |
dac5c838 | 755 | { |
f6ccaed9 PP |
756 | BT_ASSERT_PRE_NON_NULL(integer_obj, "Value object"); |
757 | BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj, BT_VALUE_TYPE_INTEGER); | |
758 | BT_ASSERT_PRE_VALUE_HOT(integer_obj, "Value object"); | |
759 | BT_VALUE_TO_INTEGER(integer_obj)->value = val; | |
2f42aa0a PP |
760 | BT_LOGV("Set integer value's raw value: value-addr=%p, value=%" PRId64, |
761 | integer_obj, val); | |
dac5c838 PP |
762 | } |
763 | ||
601b0d3c | 764 | double bt_value_real_get(const struct bt_value *real_obj) |
dac5c838 | 765 | { |
a373bf69 | 766 | BT_ASSERT_PRE_NON_NULL(real_obj, "Value object"); |
a373bf69 | 767 | BT_ASSERT_PRE_VALUE_IS_TYPE(real_obj, BT_VALUE_TYPE_REAL); |
601b0d3c | 768 | return BT_VALUE_TO_REAL(real_obj)->value; |
dac5c838 PP |
769 | } |
770 | ||
05e21286 | 771 | void bt_value_real_set(struct bt_value *real_obj, double val) |
dac5c838 | 772 | { |
a373bf69 PP |
773 | BT_ASSERT_PRE_NON_NULL(real_obj, "Value object"); |
774 | BT_ASSERT_PRE_VALUE_IS_TYPE(real_obj, BT_VALUE_TYPE_REAL); | |
775 | BT_ASSERT_PRE_VALUE_HOT(real_obj, "Value object"); | |
776 | BT_VALUE_TO_REAL(real_obj)->value = val; | |
777 | BT_LOGV("Set real number value's raw value: value-addr=%p, value=%f", | |
778 | real_obj, val); | |
dac5c838 PP |
779 | } |
780 | ||
601b0d3c | 781 | const char *bt_value_string_get(const struct bt_value *string_obj) |
dac5c838 | 782 | { |
f6ccaed9 | 783 | BT_ASSERT_PRE_NON_NULL(string_obj, "Value object"); |
f6ccaed9 | 784 | BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj, BT_VALUE_TYPE_STRING); |
601b0d3c | 785 | return BT_VALUE_TO_STRING(string_obj)->gstr->str; |
dac5c838 PP |
786 | } |
787 | ||
05e21286 PP |
788 | enum bt_value_status bt_value_string_set( |
789 | struct bt_value *string_obj, const char *val) | |
dac5c838 | 790 | { |
f6ccaed9 | 791 | BT_ASSERT_PRE_NON_NULL(string_obj, "Value object"); |
f6ccaed9 PP |
792 | BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj, BT_VALUE_TYPE_STRING); |
793 | BT_ASSERT_PRE_VALUE_HOT(string_obj, "Value object"); | |
794 | g_string_assign(BT_VALUE_TO_STRING(string_obj)->gstr, val); | |
2f42aa0a PP |
795 | BT_LOGV("Set string value's raw value: value-addr=%p, raw-value-addr=%p", |
796 | string_obj, val); | |
f6ccaed9 | 797 | return BT_VALUE_STATUS_OK; |
dac5c838 PP |
798 | } |
799 | ||
601b0d3c | 800 | uint64_t bt_value_array_get_size(const struct bt_value *array_obj) |
dac5c838 | 801 | { |
f6ccaed9 PP |
802 | BT_ASSERT_PRE_NON_NULL(array_obj, "Value object"); |
803 | BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj, BT_VALUE_TYPE_ARRAY); | |
601b0d3c | 804 | return (uint64_t) BT_VALUE_TO_ARRAY(array_obj)->garray->len; |
dac5c838 PP |
805 | } |
806 | ||
da91b29a | 807 | struct bt_value *bt_value_array_borrow_element_by_index( |
05e21286 | 808 | struct bt_value *array_obj, uint64_t index) |
dac5c838 | 809 | { |
dac5c838 PP |
810 | struct bt_value_array *typed_array_obj = |
811 | BT_VALUE_TO_ARRAY(array_obj); | |
812 | ||
f6ccaed9 PP |
813 | BT_ASSERT_PRE_NON_NULL(array_obj, "Value object"); |
814 | BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj, BT_VALUE_TYPE_ARRAY); | |
815 | BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index, | |
816 | typed_array_obj->garray->len); | |
094ff7c0 | 817 | return g_ptr_array_index(typed_array_obj->garray, index); |
dac5c838 PP |
818 | } |
819 | ||
05e21286 PP |
820 | const struct bt_value *bt_value_array_borrow_element_by_index_const( |
821 | const struct bt_value *array_obj, | |
da91b29a PP |
822 | uint64_t index) |
823 | { | |
05e21286 | 824 | return bt_value_array_borrow_element_by_index( |
da91b29a PP |
825 | (void *) array_obj, index); |
826 | } | |
827 | ||
05e21286 PP |
828 | enum bt_value_status bt_value_array_append_element( |
829 | struct bt_value *array_obj, | |
364747d6 | 830 | struct bt_value *element_obj) |
dac5c838 | 831 | { |
dac5c838 PP |
832 | struct bt_value_array *typed_array_obj = |
833 | BT_VALUE_TO_ARRAY(array_obj); | |
834 | ||
f6ccaed9 PP |
835 | BT_ASSERT_PRE_NON_NULL(array_obj, "Array value object"); |
836 | BT_ASSERT_PRE_NON_NULL(element_obj, "Element value object"); | |
837 | BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj, BT_VALUE_TYPE_ARRAY); | |
838 | BT_ASSERT_PRE_VALUE_HOT(array_obj, "Array value object"); | |
dac5c838 | 839 | g_ptr_array_add(typed_array_obj->garray, element_obj); |
65300d60 | 840 | bt_object_get_ref(element_obj); |
2f42aa0a PP |
841 | BT_LOGV("Appended element to array value: array-value-addr=%p, " |
842 | "element-value-addr=%p, new-size=%u", | |
843 | array_obj, element_obj, typed_array_obj->garray->len); | |
f6ccaed9 | 844 | return BT_VALUE_STATUS_OK; |
dac5c838 PP |
845 | } |
846 | ||
05e21286 PP |
847 | enum bt_value_status bt_value_array_append_bool_element( |
848 | struct bt_value *array_obj, bt_bool val) | |
dac5c838 PP |
849 | { |
850 | enum bt_value_status ret; | |
05e21286 | 851 | struct bt_value *bool_obj = NULL; |
dac5c838 | 852 | |
05e21286 PP |
853 | bool_obj = bt_value_bool_create_init(val); |
854 | ret = bt_value_array_append_element(array_obj, | |
da91b29a | 855 | (void *) bool_obj); |
65300d60 | 856 | bt_object_put_ref(bool_obj); |
dac5c838 PP |
857 | return ret; |
858 | } | |
859 | ||
05e21286 PP |
860 | enum bt_value_status bt_value_array_append_integer_element( |
861 | struct bt_value *array_obj, int64_t val) | |
dac5c838 PP |
862 | { |
863 | enum bt_value_status ret; | |
05e21286 | 864 | struct bt_value *integer_obj = NULL; |
dac5c838 | 865 | |
05e21286 PP |
866 | integer_obj = bt_value_integer_create_init(val); |
867 | ret = bt_value_array_append_element(array_obj, | |
da91b29a | 868 | (void *) integer_obj); |
65300d60 | 869 | bt_object_put_ref(integer_obj); |
dac5c838 PP |
870 | return ret; |
871 | } | |
872 | ||
05e21286 PP |
873 | enum bt_value_status bt_value_array_append_real_element( |
874 | struct bt_value *array_obj, double val) | |
dac5c838 PP |
875 | { |
876 | enum bt_value_status ret; | |
05e21286 | 877 | struct bt_value *real_obj = NULL; |
dac5c838 | 878 | |
05e21286 PP |
879 | real_obj = bt_value_real_create_init(val); |
880 | ret = bt_value_array_append_element(array_obj, | |
da91b29a | 881 | (void *) real_obj); |
65300d60 | 882 | bt_object_put_ref(real_obj); |
dac5c838 PP |
883 | return ret; |
884 | } | |
885 | ||
05e21286 PP |
886 | enum bt_value_status bt_value_array_append_string_element( |
887 | struct bt_value *array_obj, const char *val) | |
dac5c838 PP |
888 | { |
889 | enum bt_value_status ret; | |
05e21286 | 890 | struct bt_value *string_obj = NULL; |
dac5c838 | 891 | |
05e21286 PP |
892 | string_obj = bt_value_string_create_init(val); |
893 | ret = bt_value_array_append_element(array_obj, | |
da91b29a | 894 | (void *) string_obj); |
65300d60 | 895 | bt_object_put_ref(string_obj); |
dac5c838 PP |
896 | return ret; |
897 | } | |
898 | ||
05e21286 PP |
899 | enum bt_value_status bt_value_array_append_empty_array_element( |
900 | struct bt_value *array_obj) | |
dac5c838 PP |
901 | { |
902 | enum bt_value_status ret; | |
05e21286 | 903 | struct bt_value *empty_array_obj = NULL; |
dac5c838 | 904 | |
05e21286 PP |
905 | empty_array_obj = bt_value_array_create(); |
906 | ret = bt_value_array_append_element(array_obj, | |
da91b29a | 907 | (void *) empty_array_obj); |
65300d60 | 908 | bt_object_put_ref(empty_array_obj); |
dac5c838 PP |
909 | return ret; |
910 | } | |
911 | ||
05e21286 PP |
912 | enum bt_value_status bt_value_array_append_empty_map_element( |
913 | struct bt_value *array_obj) | |
dac5c838 PP |
914 | { |
915 | enum bt_value_status ret; | |
05e21286 | 916 | struct bt_value *map_obj = NULL; |
dac5c838 | 917 | |
05e21286 PP |
918 | map_obj = bt_value_map_create(); |
919 | ret = bt_value_array_append_element(array_obj, | |
da91b29a | 920 | (void *) map_obj); |
65300d60 | 921 | bt_object_put_ref(map_obj); |
dac5c838 PP |
922 | return ret; |
923 | } | |
924 | ||
05e21286 PP |
925 | enum bt_value_status bt_value_array_set_element_by_index( |
926 | struct bt_value *array_obj, uint64_t index, | |
da91b29a | 927 | struct bt_value *element_obj) |
dac5c838 | 928 | { |
dac5c838 PP |
929 | struct bt_value_array *typed_array_obj = |
930 | BT_VALUE_TO_ARRAY(array_obj); | |
931 | ||
f6ccaed9 PP |
932 | BT_ASSERT_PRE_NON_NULL(array_obj, "Array value object"); |
933 | BT_ASSERT_PRE_NON_NULL(element_obj, "Element value object"); | |
934 | BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj, BT_VALUE_TYPE_ARRAY); | |
935 | BT_ASSERT_PRE_VALUE_HOT(array_obj, "Array value object"); | |
936 | BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index, | |
937 | typed_array_obj->garray->len); | |
65300d60 | 938 | bt_object_put_ref(g_ptr_array_index(typed_array_obj->garray, index)); |
dac5c838 | 939 | g_ptr_array_index(typed_array_obj->garray, index) = element_obj; |
65300d60 | 940 | bt_object_get_ref(element_obj); |
2f42aa0a PP |
941 | BT_LOGV("Set array value's element: array-value-addr=%p, " |
942 | "index=%" PRIu64 ", element-value-addr=%p", | |
943 | array_obj, index, element_obj); | |
f6ccaed9 | 944 | return BT_VALUE_STATUS_OK; |
dac5c838 PP |
945 | } |
946 | ||
601b0d3c | 947 | uint64_t bt_value_map_get_size(const struct bt_value *map_obj) |
dac5c838 | 948 | { |
f6ccaed9 PP |
949 | BT_ASSERT_PRE_NON_NULL(map_obj, "Value object"); |
950 | BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP); | |
601b0d3c | 951 | return (uint64_t) g_hash_table_size(BT_VALUE_TO_MAP(map_obj)->ght); |
dac5c838 PP |
952 | } |
953 | ||
05e21286 | 954 | struct bt_value *bt_value_map_borrow_entry_value(struct bt_value *map_obj, |
364747d6 | 955 | const char *key) |
dac5c838 | 956 | { |
f6ccaed9 PP |
957 | BT_ASSERT_PRE_NON_NULL(map_obj, "Value object"); |
958 | BT_ASSERT_PRE_NON_NULL(key, "Key"); | |
959 | BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP); | |
094ff7c0 PP |
960 | return g_hash_table_lookup(BT_VALUE_TO_MAP(map_obj)->ght, |
961 | GUINT_TO_POINTER(g_quark_from_string(key))); | |
dac5c838 PP |
962 | } |
963 | ||
05e21286 PP |
964 | const struct bt_value *bt_value_map_borrow_entry_value_const( |
965 | const struct bt_value *map_obj, const char *key) | |
da91b29a | 966 | { |
05e21286 | 967 | return bt_value_map_borrow_entry_value((void *) map_obj, key); |
da91b29a PP |
968 | } |
969 | ||
07208d85 | 970 | bt_bool bt_value_map_has_entry(const struct bt_value *map_obj, const char *key) |
dac5c838 | 971 | { |
f6ccaed9 PP |
972 | BT_ASSERT_PRE_NON_NULL(map_obj, "Value object"); |
973 | BT_ASSERT_PRE_NON_NULL(key, "Key"); | |
974 | BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP); | |
975 | return bt_g_hash_table_contains(BT_VALUE_TO_MAP(map_obj)->ght, | |
976 | GUINT_TO_POINTER(g_quark_from_string(key))); | |
dac5c838 PP |
977 | } |
978 | ||
05e21286 PP |
979 | enum bt_value_status bt_value_map_insert_entry( |
980 | struct bt_value *map_obj, | |
364747d6 | 981 | const char *key, struct bt_value *element_obj) |
dac5c838 | 982 | { |
f6ccaed9 PP |
983 | BT_ASSERT_PRE_NON_NULL(map_obj, "Map value object"); |
984 | BT_ASSERT_PRE_NON_NULL(key, "Key"); | |
985 | BT_ASSERT_PRE_NON_NULL(element_obj, "Element value object"); | |
986 | BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP); | |
987 | BT_ASSERT_PRE_VALUE_HOT(map_obj, "Map value object"); | |
988 | g_hash_table_insert(BT_VALUE_TO_MAP(map_obj)->ght, | |
989 | GUINT_TO_POINTER(g_quark_from_string(key)), element_obj); | |
65300d60 | 990 | bt_object_get_ref(element_obj); |
2f42aa0a PP |
991 | BT_LOGV("Inserted value into map value: map-value-addr=%p, " |
992 | "key=\"%s\", element-value-addr=%p", | |
993 | map_obj, key, element_obj); | |
f6ccaed9 | 994 | return BT_VALUE_STATUS_OK; |
dac5c838 PP |
995 | } |
996 | ||
05e21286 PP |
997 | enum bt_value_status bt_value_map_insert_bool_entry( |
998 | struct bt_value *map_obj, const char *key, bt_bool val) | |
dac5c838 PP |
999 | { |
1000 | enum bt_value_status ret; | |
05e21286 | 1001 | struct bt_value *bool_obj = NULL; |
dac5c838 | 1002 | |
05e21286 PP |
1003 | bool_obj = bt_value_bool_create_init(val); |
1004 | ret = bt_value_map_insert_entry(map_obj, key, | |
da91b29a | 1005 | (void *) bool_obj); |
65300d60 | 1006 | bt_object_put_ref(bool_obj); |
dac5c838 PP |
1007 | return ret; |
1008 | } | |
1009 | ||
05e21286 PP |
1010 | enum bt_value_status bt_value_map_insert_integer_entry( |
1011 | struct bt_value *map_obj, const char *key, int64_t val) | |
dac5c838 PP |
1012 | { |
1013 | enum bt_value_status ret; | |
05e21286 | 1014 | struct bt_value *integer_obj = NULL; |
dac5c838 | 1015 | |
05e21286 PP |
1016 | integer_obj = bt_value_integer_create_init(val); |
1017 | ret = bt_value_map_insert_entry(map_obj, key, | |
da91b29a | 1018 | (void *) integer_obj); |
65300d60 | 1019 | bt_object_put_ref(integer_obj); |
dac5c838 PP |
1020 | return ret; |
1021 | } | |
1022 | ||
05e21286 PP |
1023 | enum bt_value_status bt_value_map_insert_real_entry( |
1024 | struct bt_value *map_obj, const char *key, double val) | |
dac5c838 PP |
1025 | { |
1026 | enum bt_value_status ret; | |
05e21286 | 1027 | struct bt_value *real_obj = NULL; |
dac5c838 | 1028 | |
05e21286 PP |
1029 | real_obj = bt_value_real_create_init(val); |
1030 | ret = bt_value_map_insert_entry(map_obj, key, | |
da91b29a | 1031 | (void *) real_obj); |
65300d60 | 1032 | bt_object_put_ref(real_obj); |
dac5c838 PP |
1033 | return ret; |
1034 | } | |
1035 | ||
05e21286 PP |
1036 | enum bt_value_status bt_value_map_insert_string_entry( |
1037 | struct bt_value *map_obj, const char *key, | |
601b0d3c | 1038 | const char *val) |
dac5c838 PP |
1039 | { |
1040 | enum bt_value_status ret; | |
05e21286 | 1041 | struct bt_value *string_obj = NULL; |
dac5c838 | 1042 | |
05e21286 PP |
1043 | string_obj = bt_value_string_create_init(val); |
1044 | ret = bt_value_map_insert_entry(map_obj, key, | |
da91b29a | 1045 | (void *) string_obj); |
65300d60 | 1046 | bt_object_put_ref(string_obj); |
dac5c838 PP |
1047 | return ret; |
1048 | } | |
1049 | ||
05e21286 PP |
1050 | enum bt_value_status bt_value_map_insert_empty_array_entry( |
1051 | struct bt_value *map_obj, const char *key) | |
dac5c838 PP |
1052 | { |
1053 | enum bt_value_status ret; | |
05e21286 | 1054 | struct bt_value *array_obj = NULL; |
dac5c838 | 1055 | |
05e21286 PP |
1056 | array_obj = bt_value_array_create(); |
1057 | ret = bt_value_map_insert_entry(map_obj, key, | |
da91b29a | 1058 | (void *) array_obj); |
65300d60 | 1059 | bt_object_put_ref(array_obj); |
dac5c838 PP |
1060 | return ret; |
1061 | } | |
1062 | ||
05e21286 PP |
1063 | enum bt_value_status bt_value_map_insert_empty_map_entry( |
1064 | struct bt_value *map_obj, const char *key) | |
dac5c838 PP |
1065 | { |
1066 | enum bt_value_status ret; | |
05e21286 | 1067 | struct bt_value *empty_map_obj = NULL; |
dac5c838 | 1068 | |
05e21286 PP |
1069 | empty_map_obj = bt_value_map_create(); |
1070 | ret = bt_value_map_insert_entry(map_obj, key, | |
da91b29a | 1071 | (void *) empty_map_obj); |
65300d60 | 1072 | bt_object_put_ref(empty_map_obj); |
dac5c838 PP |
1073 | return ret; |
1074 | } | |
1075 | ||
05e21286 | 1076 | enum bt_value_status bt_value_map_foreach_entry(struct bt_value *map_obj, |
40f4ba76 | 1077 | bt_value_map_foreach_entry_func func, void *data) |
dac5c838 PP |
1078 | { |
1079 | enum bt_value_status ret = BT_VALUE_STATUS_OK; | |
1080 | gpointer key, element_obj; | |
1081 | GHashTableIter iter; | |
1082 | struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj); | |
1083 | ||
f6ccaed9 | 1084 | BT_ASSERT_PRE_NON_NULL(map_obj, "Value object"); |
40f4ba76 | 1085 | BT_ASSERT_PRE_NON_NULL(func, "Callback"); |
f6ccaed9 | 1086 | BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP); |
dac5c838 PP |
1087 | g_hash_table_iter_init(&iter, typed_map_obj->ght); |
1088 | ||
1089 | while (g_hash_table_iter_next(&iter, &key, &element_obj)) { | |
5b44aff2 | 1090 | const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key)); |
dac5c838 | 1091 | |
40f4ba76 | 1092 | if (!func(key_str, element_obj, data)) { |
f6ccaed9 | 1093 | BT_LOGV("User canceled the loop: key=\"%s\", " |
2f42aa0a PP |
1094 | "value-addr=%p, data=%p", |
1095 | key_str, element_obj, data); | |
f6ccaed9 | 1096 | ret = BT_VALUE_STATUS_CANCELED; |
dac5c838 PP |
1097 | break; |
1098 | } | |
1099 | } | |
1100 | ||
dac5c838 PP |
1101 | return ret; |
1102 | } | |
1103 | ||
05e21286 PP |
1104 | enum bt_value_status bt_value_map_foreach_entry_const( |
1105 | const struct bt_value *map_obj, | |
40f4ba76 | 1106 | bt_value_map_foreach_entry_const_func func, void *data) |
da91b29a PP |
1107 | { |
1108 | return bt_value_map_foreach_entry((void *) map_obj, | |
40f4ba76 | 1109 | (bt_value_map_foreach_entry_func) func, data); |
da91b29a PP |
1110 | } |
1111 | ||
770750d3 | 1112 | struct extend_map_element_data { |
05e21286 | 1113 | struct bt_value *extended_obj; |
601b0d3c | 1114 | enum bt_value_status status; |
770750d3 PP |
1115 | }; |
1116 | ||
1117 | static | |
c55a9f58 | 1118 | bt_bool extend_map_element(const char *key, |
05e21286 | 1119 | const struct bt_value *extension_obj_elem, void *data) |
770750d3 | 1120 | { |
c55a9f58 | 1121 | bt_bool ret = BT_TRUE; |
770750d3 | 1122 | struct extend_map_element_data *extend_data = data; |
05e21286 | 1123 | struct bt_value *extension_obj_elem_copy = NULL; |
770750d3 PP |
1124 | |
1125 | /* Copy object which is to replace the current one */ | |
6be5a99e PP |
1126 | extend_data->status = bt_value_copy(extension_obj_elem, |
1127 | &extension_obj_elem_copy); | |
601b0d3c PP |
1128 | if (extend_data->status) { |
1129 | BT_LOGE("Cannot copy map element: addr=%p", | |
1130 | extension_obj_elem); | |
1131 | goto error; | |
1132 | } | |
1133 | ||
1134 | BT_ASSERT(extension_obj_elem_copy); | |
770750d3 PP |
1135 | |
1136 | /* Replace in extended object */ | |
05e21286 | 1137 | extend_data->status = bt_value_map_insert_entry( |
601b0d3c PP |
1138 | extend_data->extended_obj, key, |
1139 | (void *) extension_obj_elem_copy); | |
1140 | if (extend_data->status) { | |
2f42aa0a PP |
1141 | BT_LOGE("Cannot replace value in extended value: key=\"%s\", " |
1142 | "extended-value-addr=%p, element-value-addr=%p", | |
1143 | key, extend_data->extended_obj, | |
1144 | extension_obj_elem_copy); | |
770750d3 PP |
1145 | goto error; |
1146 | } | |
1147 | ||
1148 | goto end; | |
1149 | ||
1150 | error: | |
601b0d3c | 1151 | BT_ASSERT(extend_data->status != BT_VALUE_STATUS_OK); |
c55a9f58 | 1152 | ret = BT_FALSE; |
770750d3 PP |
1153 | |
1154 | end: | |
65300d60 | 1155 | BT_OBJECT_PUT_REF_AND_RESET(extension_obj_elem_copy); |
770750d3 PP |
1156 | return ret; |
1157 | } | |
1158 | ||
601b0d3c | 1159 | enum bt_value_status bt_value_map_extend( |
70991d9f | 1160 | const struct bt_value *base_map_obj, |
35294e83 PP |
1161 | const struct bt_value *extension_obj, |
1162 | struct bt_value **extended_map_obj) | |
770750d3 | 1163 | { |
601b0d3c PP |
1164 | struct extend_map_element_data extend_data = { |
1165 | .extended_obj = NULL, | |
1166 | .status = BT_VALUE_STATUS_OK, | |
1167 | }; | |
770750d3 | 1168 | |
f6ccaed9 PP |
1169 | BT_ASSERT_PRE_NON_NULL(base_map_obj, "Base value object"); |
1170 | BT_ASSERT_PRE_NON_NULL(extension_obj, "Extension value object"); | |
601b0d3c PP |
1171 | BT_ASSERT_PRE_NON_NULL(extended_map_obj, |
1172 | "Extended value object (output)"); | |
f6ccaed9 PP |
1173 | BT_ASSERT_PRE_VALUE_IS_TYPE(base_map_obj, BT_VALUE_TYPE_MAP); |
1174 | BT_ASSERT_PRE_VALUE_IS_TYPE(extension_obj, BT_VALUE_TYPE_MAP); | |
2f42aa0a PP |
1175 | BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p", |
1176 | base_map_obj, extension_obj); | |
601b0d3c | 1177 | *extended_map_obj = NULL; |
2f42aa0a | 1178 | |
770750d3 | 1179 | /* Create copy of base map object to start with */ |
6be5a99e | 1180 | extend_data.status = bt_value_copy(base_map_obj, extended_map_obj); |
601b0d3c | 1181 | if (extend_data.status) { |
2f42aa0a PP |
1182 | BT_LOGE("Cannot copy base value: base-value-addr=%p", |
1183 | base_map_obj); | |
770750d3 PP |
1184 | goto error; |
1185 | } | |
1186 | ||
601b0d3c PP |
1187 | BT_ASSERT(extended_map_obj); |
1188 | ||
770750d3 PP |
1189 | /* |
1190 | * For each key in the extension map object, replace this key | |
1191 | * in the copied map object. | |
1192 | */ | |
601b0d3c | 1193 | extend_data.extended_obj = *extended_map_obj; |
770750d3 | 1194 | |
05e21286 | 1195 | if (bt_value_map_foreach_entry_const(extension_obj, extend_map_element, |
770750d3 | 1196 | &extend_data)) { |
32e87ceb | 1197 | BT_LOGE("Cannot iterate on the extension object's elements: " |
2f42aa0a | 1198 | "extension-value-addr=%p", extension_obj); |
770750d3 PP |
1199 | goto error; |
1200 | } | |
1201 | ||
601b0d3c | 1202 | if (extend_data.status) { |
32e87ceb | 1203 | BT_LOGE("Failed to successfully iterate on the extension object's elements: " |
2f42aa0a | 1204 | "extension-value-addr=%p", extension_obj); |
770750d3 PP |
1205 | goto error; |
1206 | } | |
1207 | ||
2f42aa0a | 1208 | BT_LOGD("Extended map value: extended-value-addr=%p", |
601b0d3c | 1209 | *extended_map_obj); |
770750d3 PP |
1210 | goto end; |
1211 | ||
1212 | error: | |
601b0d3c PP |
1213 | BT_OBJECT_PUT_REF_AND_RESET(*extended_map_obj); |
1214 | *extended_map_obj = NULL; | |
770750d3 PP |
1215 | |
1216 | end: | |
601b0d3c | 1217 | return extend_data.status; |
770750d3 PP |
1218 | } |
1219 | ||
6be5a99e PP |
1220 | enum bt_value_status bt_value_copy(const struct bt_value *object, |
1221 | struct bt_value **copy_obj) | |
dac5c838 | 1222 | { |
601b0d3c | 1223 | enum bt_value_status status = BT_VALUE_STATUS_OK; |
dac5c838 | 1224 | |
f6ccaed9 | 1225 | BT_ASSERT_PRE_NON_NULL(object, "Value object"); |
601b0d3c | 1226 | BT_ASSERT_PRE_NON_NULL(copy_obj, "Value object copy (output)"); |
2f42aa0a | 1227 | BT_LOGD("Copying value object: addr=%p", object); |
601b0d3c PP |
1228 | *copy_obj = copy_funcs[object->type](object); |
1229 | if (*copy_obj) { | |
2f42aa0a PP |
1230 | BT_LOGD("Copied value object: copy-value-addr=%p", |
1231 | copy_obj); | |
1232 | } else { | |
601b0d3c PP |
1233 | status = BT_VALUE_STATUS_NOMEM; |
1234 | *copy_obj = NULL; | |
2f42aa0a PP |
1235 | BT_LOGE_STR("Failed to copy value object."); |
1236 | } | |
dac5c838 | 1237 | |
601b0d3c | 1238 | return status; |
dac5c838 PP |
1239 | } |
1240 | ||
c55a9f58 | 1241 | bt_bool bt_value_compare(const struct bt_value *object_a, |
dac5c838 PP |
1242 | const struct bt_value *object_b) |
1243 | { | |
c55a9f58 | 1244 | bt_bool ret = BT_FALSE; |
dac5c838 | 1245 | |
f6ccaed9 PP |
1246 | BT_ASSERT_PRE_NON_NULL(object_a, "Value object A"); |
1247 | BT_ASSERT_PRE_NON_NULL(object_b, "Value object B"); | |
dac5c838 PP |
1248 | |
1249 | if (object_a->type != object_b->type) { | |
2f42aa0a PP |
1250 | BT_LOGV("Values are different: type mismatch: " |
1251 | "value-a-addr=%p, value-b-addr=%p, " | |
c4628760 | 1252 | "value-a-type=%s, value-b-type=%s", |
2f42aa0a | 1253 | object_a, object_b, |
da91b29a PP |
1254 | bt_common_value_type_string(object_a->type), |
1255 | bt_common_value_type_string(object_b->type)); | |
dac5c838 PP |
1256 | goto end; |
1257 | } | |
1258 | ||
1259 | ret = compare_funcs[object_a->type](object_a, object_b); | |
1260 | ||
1261 | end: | |
1262 | return ret; | |
1263 | } | |
c5b9b441 PP |
1264 | |
1265 | void bt_value_get_ref(const struct bt_value *value) | |
1266 | { | |
1267 | bt_object_get_ref(value); | |
1268 | } | |
1269 | ||
1270 | void bt_value_put_ref(const struct bt_value *value) | |
1271 | { | |
1272 | bt_object_put_ref(value); | |
1273 | } |