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