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