2 * Copyright (c) 2015-2018 Philippe Proulx <pproulx@efficios.com>
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
23 #define BT_LOG_TAG "LIB/VALUE"
24 #include "lib/logging.h"
30 #include <babeltrace2/babeltrace.h>
32 #include "compat/compiler.h"
33 #include "common/common.h"
34 #include "compat/glib.h"
35 #include "lib/assert-pre.h"
36 #include "lib/assert-post.h"
37 #include "lib/value.h"
38 #include "common/assert.h"
39 #include "func-status.h"
41 #define BT_VALUE_TO_BOOL(_base) ((struct bt_value_bool *) (_base))
42 #define BT_VALUE_TO_INTEGER(_base) ((struct bt_value_integer *) (_base))
43 #define BT_VALUE_TO_REAL(_base) ((struct bt_value_real *) (_base))
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))
48 #define _BT_ASSERT_PRE_VALUE_IS_TYPE_COND(_value, _type) \
49 (((struct bt_value *) (_value))->type == (_type))
51 #define _BT_ASSERT_PRE_VALUE_IS_TYPE_FMT \
52 "Value has the wrong type ID: expected-type=%s, %![value-]+v"
54 #define BT_ASSERT_PRE_VALUE_IS_TYPE(_value, _type) \
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))
60 #define BT_ASSERT_PRE_DEV_VALUE_IS_TYPE(_value, _type) \
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))
66 #define BT_ASSERT_PRE_DEV_VALUE_HOT(_value, _name) \
67 BT_ASSERT_PRE_DEV_HOT(((struct bt_value *) (_value)), (_name), \
71 void bt_value_null_instance_release_func(struct bt_object
*obj
)
73 BT_LOGW("Releasing the null value singleton: addr=%p", obj
);
77 struct bt_value bt_value_null_instance
= {
81 .release_func
= bt_value_null_instance_release_func
,
82 .spec_release_func
= NULL
,
83 .parent_is_owner_listener_func
= NULL
,
86 .type
= BT_VALUE_TYPE_NULL
,
90 struct bt_value
*const bt_value_null
= &bt_value_null_instance
;
93 void bt_value_destroy(struct bt_object
*obj
);
96 void bt_value_string_destroy(struct bt_value
*object
)
98 g_string_free(BT_VALUE_TO_STRING(object
)->gstr
, TRUE
);
99 BT_VALUE_TO_STRING(object
)->gstr
= NULL
;
103 void bt_value_array_destroy(struct bt_value
*object
)
106 * Pointer array's registered value destructor will take care
107 * of putting each contained object.
109 g_ptr_array_free(BT_VALUE_TO_ARRAY(object
)->garray
, TRUE
);
110 BT_VALUE_TO_ARRAY(object
)->garray
= NULL
;
114 void bt_value_map_destroy(struct bt_value
*object
)
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.
121 g_hash_table_destroy(BT_VALUE_TO_MAP(object
)->ght
);
122 BT_VALUE_TO_MAP(object
)->ght
= NULL
;
126 void (* const destroy_funcs
[])(struct bt_value
*) = {
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
,
138 struct bt_value
*bt_value_null_copy(const struct bt_value
*null_obj
)
140 BT_ASSERT(null_obj
== bt_value_null
);
142 bt_object_get_ref_no_null_check(bt_value_null
);
143 return (void *) bt_value_null
;
147 struct bt_value
*bt_value_bool_copy(const struct bt_value
*bool_obj
)
149 return bt_value_bool_create_init(
150 BT_VALUE_TO_BOOL(bool_obj
)->value
);
154 struct bt_value
*bt_value_integer_create_init(enum bt_value_type type
,
158 struct bt_value
*bt_value_integer_copy(
159 const struct bt_value
*integer_obj
)
161 return bt_value_integer_create_init(integer_obj
->type
,
162 BT_VALUE_TO_INTEGER(integer_obj
)->value
.u
);
166 struct bt_value
*bt_value_real_copy(const struct bt_value
*real_obj
)
168 return bt_value_real_create_init(
169 BT_VALUE_TO_REAL(real_obj
)->value
);
173 struct bt_value
*bt_value_string_copy(const struct bt_value
*string_obj
)
175 return bt_value_string_create_init(
176 BT_VALUE_TO_STRING(string_obj
)->gstr
->str
);
180 struct bt_value
*bt_value_array_copy(const struct bt_value
*array_obj
)
184 struct bt_value
*copy_obj
;
185 struct bt_value_array
*typed_array_obj
;
187 BT_LOGD("Copying array value: addr=%p", array_obj
);
188 typed_array_obj
= BT_VALUE_TO_ARRAY(array_obj
);
189 copy_obj
= bt_value_array_create();
191 BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty array value.");
195 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
196 struct bt_value
*element_obj_copy
= NULL
;
197 const struct bt_value
*element_obj
=
198 bt_value_array_borrow_element_by_index_const(
201 BT_LOGD("Copying array value's element: element-addr=%p, "
202 "index=%d", element_obj
, i
);
203 ret
= bt_value_copy(element_obj
, &element_obj_copy
);
205 BT_LIB_LOGE_APPEND_CAUSE(
206 "Cannot copy array value's element: "
207 "array-addr=%p, index=%d",
209 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
213 BT_ASSERT(element_obj_copy
);
214 ret
= bt_value_array_append_element(copy_obj
,
215 (void *) element_obj_copy
);
216 BT_OBJECT_PUT_REF_AND_RESET(element_obj_copy
);
218 BT_LIB_LOGE_APPEND_CAUSE(
219 "Cannot append to array value: addr=%p",
221 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
226 BT_LOGD("Copied array value: original-addr=%p, copy-addr=%p",
227 array_obj
, copy_obj
);
234 struct bt_value
*bt_value_map_copy(const struct bt_value
*map_obj
)
238 gpointer key
, element_obj
;
239 struct bt_value
*copy_obj
;
240 struct bt_value
*element_obj_copy
= NULL
;
241 struct bt_value_map
*typed_map_obj
;
243 BT_LOGD("Copying map value: addr=%p", map_obj
);
244 typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
245 copy_obj
= bt_value_map_create();
250 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
252 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
253 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
256 BT_LOGD("Copying map value's element: element-addr=%p, "
257 "key=\"%s\"", element_obj
, key_str
);
258 ret
= bt_value_copy(element_obj
, &element_obj_copy
);
260 BT_LIB_LOGE_APPEND_CAUSE(
261 "Cannot copy map value's element: "
262 "map-addr=%p, key=\"%s\"",
264 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
268 BT_ASSERT(element_obj_copy
);
269 ret
= bt_value_map_insert_entry(copy_obj
, key_str
,
270 (void *) element_obj_copy
);
271 BT_OBJECT_PUT_REF_AND_RESET(element_obj_copy
);
273 BT_LIB_LOGE_APPEND_CAUSE(
274 "Cannot insert into map value: addr=%p, key=\"%s\"",
276 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
281 BT_LOGD("Copied map value: addr=%p", map_obj
);
288 struct bt_value
*(* const copy_funcs
[])(const struct bt_value
*) = {
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
,
300 bt_bool
bt_value_null_is_equal(const struct bt_value
*object_a
,
301 const struct bt_value
*object_b
)
304 * Always BT_TRUE since bt_value_is_equal() already checks if both
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.
312 bt_bool
bt_value_bool_is_equal(const struct bt_value
*object_a
,
313 const struct bt_value
*object_b
)
315 if (BT_VALUE_TO_BOOL(object_a
)->value
!=
316 BT_VALUE_TO_BOOL(object_b
)->value
) {
317 BT_LOGT("Boolean value objects are different: "
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
);
328 bt_bool
bt_value_integer_is_equal(const struct bt_value
*object_a
,
329 const struct bt_value
*object_b
)
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
) {
334 BT_LOGT("Unsigned integer value objects are different: "
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
);
339 BT_LOGT("Signed integer value objects are different: "
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
);
352 bt_bool
bt_value_real_is_equal(const struct bt_value
*object_a
,
353 const struct bt_value
*object_b
)
355 if (BT_VALUE_TO_REAL(object_a
)->value
!=
356 BT_VALUE_TO_REAL(object_b
)->value
) {
357 BT_LOGT("Real number value objects are different: "
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
);
368 bt_bool
bt_value_string_is_equal(const struct bt_value
*object_a
,
369 const struct bt_value
*object_b
)
371 if (strcmp(BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
372 BT_VALUE_TO_STRING(object_b
)->gstr
->str
) != 0) {
373 BT_LOGT("String value objects are different: "
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
);
384 bt_bool
bt_value_array_is_equal(const struct bt_value
*object_a
,
385 const struct bt_value
*object_b
)
388 bt_bool ret
= BT_TRUE
;
389 const struct bt_value_array
*array_obj_a
=
390 BT_VALUE_TO_ARRAY(object_a
);
392 if (bt_value_array_get_length(object_a
) !=
393 bt_value_array_get_length(object_b
)) {
394 BT_LOGT("Array values are different: size mismatch "
395 "value-a-addr=%p, value-b-addr=%p, "
396 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
398 bt_value_array_get_length(object_a
),
399 bt_value_array_get_length(object_b
));
404 for (i
= 0; i
< array_obj_a
->garray
->len
; ++i
) {
405 const struct bt_value
*element_obj_a
;
406 const struct bt_value
*element_obj_b
;
408 element_obj_a
= bt_value_array_borrow_element_by_index_const(
410 element_obj_b
= bt_value_array_borrow_element_by_index_const(
413 if (!bt_value_is_equal(element_obj_a
, element_obj_b
)) {
414 BT_LOGT("Array values's elements are different: "
415 "value-a-addr=%p, value-b-addr=%p, index=%d",
416 element_obj_a
, element_obj_b
, i
);
427 bt_bool
bt_value_map_is_equal(const struct bt_value
*object_a
,
428 const struct bt_value
*object_b
)
430 bt_bool ret
= BT_TRUE
;
432 gpointer key
, element_obj_a
;
433 const struct bt_value_map
*map_obj_a
= BT_VALUE_TO_MAP(object_a
);
435 if (bt_value_map_get_size(object_a
) !=
436 bt_value_map_get_size(object_b
)) {
437 BT_LOGT("Map values are different: size mismatch "
438 "value-a-addr=%p, value-b-addr=%p, "
439 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
441 bt_value_map_get_size(object_a
),
442 bt_value_map_get_size(object_b
));
447 g_hash_table_iter_init(&iter
, map_obj_a
->ght
);
449 while (g_hash_table_iter_next(&iter
, &key
, &element_obj_a
)) {
450 const struct bt_value
*element_obj_b
;
451 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
453 element_obj_b
= bt_value_map_borrow_entry_value_const(object_b
,
456 if (!bt_value_is_equal(element_obj_a
, element_obj_b
)) {
457 BT_LOGT("Map values's elements are different: "
458 "value-a-addr=%p, value-b-addr=%p, key=\"%s\"",
459 element_obj_a
, element_obj_b
, key_str
);
470 bt_bool (* const is_equal_funcs
[])(const struct bt_value
*,
471 const struct bt_value
*) = {
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
,
483 void bt_value_null_freeze(struct bt_value
*object
)
488 void bt_value_generic_freeze(struct bt_value
*object
)
490 object
->frozen
= BT_TRUE
;
494 void bt_value_array_freeze(struct bt_value
*object
)
497 struct bt_value_array
*typed_array_obj
=
498 BT_VALUE_TO_ARRAY(object
);
500 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
501 bt_value_freeze(g_ptr_array_index(typed_array_obj
->garray
, i
));
504 bt_value_generic_freeze(object
);
508 void bt_value_map_freeze(struct bt_value
*object
)
511 gpointer key
, element_obj
;
512 const struct bt_value_map
*map_obj
= BT_VALUE_TO_MAP(object
);
514 g_hash_table_iter_init(&iter
, map_obj
->ght
);
516 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
517 bt_value_freeze(element_obj
);
520 bt_value_generic_freeze(object
);
524 void (* const freeze_funcs
[])(struct bt_value
*) = {
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
,
536 void bt_value_destroy(struct bt_object
*obj
)
538 struct bt_value
*value
;
540 value
= container_of(obj
, struct bt_value
, base
);
541 BT_LOGD("Destroying value: addr=%p", value
);
543 if (bt_value_is_null(value
)) {
544 BT_LOGD_STR("Not destroying the null value singleton.");
548 if (destroy_funcs
[value
->type
]) {
549 destroy_funcs
[value
->type
](value
);
556 void _bt_value_freeze(const struct bt_value
*c_object
)
558 const struct bt_value
*object
= (void *) c_object
;
562 if (object
->frozen
) {
566 BT_LOGD("Freezing value: addr=%p", object
);
567 freeze_funcs
[object
->type
]((void *) object
);
573 enum bt_value_type
bt_value_get_type(const struct bt_value
*object
)
575 BT_ASSERT_PRE_DEV_NON_NULL(object
, "Value object");
580 struct bt_value
bt_value_create_base(enum bt_value_type type
)
582 struct bt_value value
;
585 value
.frozen
= BT_FALSE
;
586 bt_object_init_shared(&value
.base
, bt_value_destroy
);
590 struct bt_value
*bt_value_bool_create_init(bt_bool val
)
592 struct bt_value_bool
*bool_obj
;
594 BT_ASSERT_PRE_NO_ERROR();
596 BT_LOGD("Creating boolean value object: val=%d", val
);
597 bool_obj
= g_new0(struct bt_value_bool
, 1);
599 BT_LIB_LOGE_APPEND_CAUSE(
600 "Failed to allocate one boolean value object.");
604 bool_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_BOOL
);
605 bool_obj
->value
= val
;
606 BT_LOGD("Created boolean value object: addr=%p", bool_obj
);
609 return (void *) bool_obj
;
612 struct bt_value
*bt_value_bool_create(void)
614 BT_ASSERT_PRE_NO_ERROR();
616 return bt_value_bool_create_init(BT_FALSE
);
620 struct bt_value
*bt_value_integer_create_init(enum bt_value_type type
,
623 struct bt_value_integer
*integer_obj
;
625 BT_ASSERT(type
== BT_VALUE_TYPE_UNSIGNED_INTEGER
||
626 type
== BT_VALUE_TYPE_SIGNED_INTEGER
);
628 if (type
== BT_VALUE_TYPE_UNSIGNED_INTEGER
) {
629 BT_LOGD("Creating unsigned integer value object: val=%" PRIu64
,
632 BT_LOGD("Creating signed integer value object: val=%" PRId64
,
636 integer_obj
= g_new0(struct bt_value_integer
, 1);
638 BT_LIB_LOGE_APPEND_CAUSE(
639 "Failed to allocate one integer value object.");
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" : "",
650 return (void *) integer_obj
;
653 struct bt_value
*bt_value_integer_unsigned_create_init(uint64_t val
)
655 BT_ASSERT_PRE_NO_ERROR();
657 return bt_value_integer_create_init(BT_VALUE_TYPE_UNSIGNED_INTEGER
,
661 struct bt_value
*bt_value_integer_unsigned_create(void)
663 BT_ASSERT_PRE_NO_ERROR();
665 return bt_value_integer_unsigned_create_init(0);
668 struct bt_value
*bt_value_integer_signed_create_init(int64_t val
)
670 BT_ASSERT_PRE_NO_ERROR();
672 return bt_value_integer_create_init(BT_VALUE_TYPE_SIGNED_INTEGER
,
676 struct bt_value
*bt_value_integer_signed_create(void)
678 BT_ASSERT_PRE_NO_ERROR();
680 return bt_value_integer_signed_create_init(0);
683 struct bt_value
*bt_value_real_create_init(double val
)
685 struct bt_value_real
*real_obj
;
687 BT_ASSERT_PRE_NO_ERROR();
689 BT_LOGD("Creating real number value object: val=%f", val
);
690 real_obj
= g_new0(struct bt_value_real
, 1);
692 BT_LIB_LOGE_APPEND_CAUSE(
693 "Failed to allocate one real number value object.");
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",
703 return (void *) real_obj
;
706 struct bt_value
*bt_value_real_create(void)
708 BT_ASSERT_PRE_NO_ERROR();
710 return bt_value_real_create_init(0.);
713 struct bt_value
*bt_value_string_create_init(const char *val
)
715 struct bt_value_string
*string_obj
= NULL
;
717 BT_ASSERT_PRE_NO_ERROR();
718 BT_ASSERT_PRE_NON_NULL(val
, "Value");
720 BT_LOGD("Creating string value object: val-len=%zu", strlen(val
));
721 string_obj
= g_new0(struct bt_value_string
, 1);
723 BT_LIB_LOGE_APPEND_CAUSE(
724 "Failed to allocate one string object.");
728 string_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_STRING
);
729 string_obj
->gstr
= g_string_new(val
);
730 if (!string_obj
->gstr
) {
731 BT_LIB_LOGE_APPEND_CAUSE(
732 "Failed to allocate a GString.");
738 BT_LOGD("Created string value object: addr=%p",
742 return (void *) string_obj
;
745 struct bt_value
*bt_value_string_create(void)
747 BT_ASSERT_PRE_NO_ERROR();
749 return bt_value_string_create_init("");
752 struct bt_value
*bt_value_array_create(void)
754 struct bt_value_array
*array_obj
;
756 BT_ASSERT_PRE_NO_ERROR();
758 BT_LOGD_STR("Creating empty array value object.");
759 array_obj
= g_new0(struct bt_value_array
, 1);
761 BT_LIB_LOGE_APPEND_CAUSE(
762 "Failed to allocate one array object.");
766 array_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_ARRAY
);
767 array_obj
->garray
= bt_g_ptr_array_new_full(0,
768 (GDestroyNotify
) bt_object_put_ref
);
769 if (!array_obj
->garray
) {
770 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
776 BT_LOGD("Created array value object: addr=%p",
780 return (void *) array_obj
;
783 struct bt_value
*bt_value_map_create(void)
785 struct bt_value_map
*map_obj
;
787 BT_ASSERT_PRE_NO_ERROR();
789 BT_LOGD_STR("Creating empty map value object.");
790 map_obj
= g_new0(struct bt_value_map
, 1);
792 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one map object.");
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
,
798 NULL
, (GDestroyNotify
) bt_object_put_ref
);
800 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GHashTable.");
806 BT_LOGD("Created map value object: addr=%p",
810 return (void *) map_obj
;
813 bt_bool
bt_value_bool_get(const struct bt_value
*bool_obj
)
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
);
817 return BT_VALUE_TO_BOOL(bool_obj
)->value
;
820 void bt_value_bool_set(struct bt_value
*bool_obj
, bt_bool val
)
822 BT_ASSERT_PRE_NON_NULL(bool_obj
, "Value object");
823 BT_ASSERT_PRE_VALUE_IS_TYPE(bool_obj
, BT_VALUE_TYPE_BOOL
);
824 BT_ASSERT_PRE_DEV_VALUE_HOT(bool_obj
, "Value object");
825 BT_VALUE_TO_BOOL(bool_obj
)->value
= val
;
826 BT_LOGT("Set boolean value's raw value: value-addr=%p, value=%d",
830 uint64_t bt_value_integer_unsigned_get(const struct bt_value
*integer_obj
)
832 BT_ASSERT_PRE_DEV_NON_NULL(integer_obj
, "Value object");
833 BT_ASSERT_PRE_DEV_VALUE_IS_TYPE(integer_obj
,
834 BT_VALUE_TYPE_UNSIGNED_INTEGER
);
835 return BT_VALUE_TO_INTEGER(integer_obj
)->value
.u
;
838 int64_t bt_value_integer_signed_get(const struct bt_value
*integer_obj
)
840 BT_ASSERT_PRE_DEV_NON_NULL(integer_obj
, "Value object");
841 BT_ASSERT_PRE_DEV_VALUE_IS_TYPE(integer_obj
,
842 BT_VALUE_TYPE_SIGNED_INTEGER
);
843 return BT_VALUE_TO_INTEGER(integer_obj
)->value
.i
;
847 void bt_value_integer_set(struct bt_value
*integer_obj
,
848 enum bt_value_type expected_type
, uint64_t uval
)
850 BT_ASSERT_PRE_NON_NULL(integer_obj
, "Value object");
851 BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj
, expected_type
);
852 BT_ASSERT_PRE_DEV_VALUE_HOT(integer_obj
, "Value object");
853 BT_VALUE_TO_INTEGER(integer_obj
)->value
.u
= uval
;
856 void bt_value_integer_unsigned_set(struct bt_value
*integer_obj
,
859 bt_value_integer_set(integer_obj
, BT_VALUE_TYPE_UNSIGNED_INTEGER
, val
);
860 BT_LOGT("Set unsigned integer value's raw value: "
861 "value-addr=%p, value=%" PRIu64
, integer_obj
, val
);
864 void bt_value_integer_signed_set(struct bt_value
*integer_obj
,
867 bt_value_integer_set(integer_obj
, BT_VALUE_TYPE_SIGNED_INTEGER
,
869 BT_LOGT("Set signed integer value's raw value: "
870 "value-addr=%p, value=%" PRId64
, integer_obj
, val
);
873 double bt_value_real_get(const struct bt_value
*real_obj
)
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
);
877 return BT_VALUE_TO_REAL(real_obj
)->value
;
880 void bt_value_real_set(struct bt_value
*real_obj
, double val
)
882 BT_ASSERT_PRE_NON_NULL(real_obj
, "Value object");
883 BT_ASSERT_PRE_VALUE_IS_TYPE(real_obj
, BT_VALUE_TYPE_REAL
);
884 BT_ASSERT_PRE_DEV_VALUE_HOT(real_obj
, "Value object");
885 BT_VALUE_TO_REAL(real_obj
)->value
= val
;
886 BT_LOGT("Set real number value's raw value: value-addr=%p, value=%f",
890 const char *bt_value_string_get(const struct bt_value
*string_obj
)
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
);
894 return BT_VALUE_TO_STRING(string_obj
)->gstr
->str
;
897 enum bt_value_string_set_status
bt_value_string_set(
898 struct bt_value
*string_obj
, const char *val
)
900 BT_ASSERT_PRE_NO_ERROR();
901 BT_ASSERT_PRE_NON_NULL(string_obj
, "Value object");
902 BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj
, BT_VALUE_TYPE_STRING
);
903 BT_ASSERT_PRE_DEV_VALUE_HOT(string_obj
, "Value object");
904 g_string_assign(BT_VALUE_TO_STRING(string_obj
)->gstr
, val
);
905 BT_LOGT("Set string value's raw value: value-addr=%p, raw-value-addr=%p",
907 return BT_FUNC_STATUS_OK
;
910 uint64_t bt_value_array_get_length(const struct bt_value
*array_obj
)
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
);
914 return (uint64_t) BT_VALUE_TO_ARRAY(array_obj
)->garray
->len
;
917 struct bt_value
*bt_value_array_borrow_element_by_index(
918 struct bt_value
*array_obj
, uint64_t index
)
920 struct bt_value_array
*typed_array_obj
=
921 BT_VALUE_TO_ARRAY(array_obj
);
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
);
926 return g_ptr_array_index(typed_array_obj
->garray
, index
);
929 const struct bt_value
*bt_value_array_borrow_element_by_index_const(
930 const struct bt_value
*array_obj
,
933 return bt_value_array_borrow_element_by_index(
934 (void *) array_obj
, index
);
937 enum bt_value_array_append_element_status
bt_value_array_append_element(
938 struct bt_value
*array_obj
,
939 struct bt_value
*element_obj
)
941 struct bt_value_array
*typed_array_obj
=
942 BT_VALUE_TO_ARRAY(array_obj
);
944 BT_ASSERT_PRE_NO_ERROR();
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
);
948 BT_ASSERT_PRE_DEV_VALUE_HOT(array_obj
, "Array value object");
949 g_ptr_array_add(typed_array_obj
->garray
, element_obj
);
950 bt_object_get_ref(element_obj
);
951 BT_LOGT("Appended element to array value: array-value-addr=%p, "
952 "element-value-addr=%p, new-size=%u",
953 array_obj
, element_obj
, typed_array_obj
->garray
->len
);
954 return BT_FUNC_STATUS_OK
;
957 enum bt_value_array_append_element_status
958 bt_value_array_append_bool_element(struct bt_value
*array_obj
, bt_bool val
)
960 enum bt_value_array_append_element_status ret
;
961 struct bt_value
*bool_obj
= NULL
;
963 BT_ASSERT_PRE_NO_ERROR();
965 bool_obj
= bt_value_bool_create_init(val
);
966 ret
= bt_value_array_append_element(array_obj
,
968 bt_object_put_ref(bool_obj
);
972 enum bt_value_array_append_element_status
973 bt_value_array_append_unsigned_integer_element(struct bt_value
*array_obj
,
976 enum bt_value_array_append_element_status ret
;
977 struct bt_value
*integer_obj
= NULL
;
979 BT_ASSERT_PRE_NO_ERROR();
981 integer_obj
= bt_value_integer_unsigned_create_init(val
);
982 ret
= bt_value_array_append_element(array_obj
,
983 (void *) integer_obj
);
984 bt_object_put_ref(integer_obj
);
988 enum bt_value_array_append_element_status
989 bt_value_array_append_signed_integer_element(struct bt_value
*array_obj
,
992 enum bt_value_array_append_element_status ret
;
993 struct bt_value
*integer_obj
= NULL
;
995 BT_ASSERT_PRE_NO_ERROR();
997 integer_obj
= bt_value_integer_signed_create_init(val
);
998 ret
= bt_value_array_append_element(array_obj
,
999 (void *) integer_obj
);
1000 bt_object_put_ref(integer_obj
);
1004 enum bt_value_array_append_element_status
1005 bt_value_array_append_real_element(struct bt_value
*array_obj
, double val
)
1007 enum bt_value_array_append_element_status ret
;
1008 struct bt_value
*real_obj
= NULL
;
1010 BT_ASSERT_PRE_NO_ERROR();
1012 real_obj
= bt_value_real_create_init(val
);
1013 ret
= bt_value_array_append_element(array_obj
,
1015 bt_object_put_ref(real_obj
);
1019 enum bt_value_array_append_element_status
1020 bt_value_array_append_string_element(struct bt_value
*array_obj
,
1023 enum bt_value_array_append_element_status ret
;
1024 struct bt_value
*string_obj
= NULL
;
1026 BT_ASSERT_PRE_NO_ERROR();
1028 string_obj
= bt_value_string_create_init(val
);
1029 ret
= bt_value_array_append_element(array_obj
,
1030 (void *) string_obj
);
1031 bt_object_put_ref(string_obj
);
1035 enum bt_value_array_append_element_status
1036 bt_value_array_append_empty_array_element(struct bt_value
*array_obj
,
1037 struct bt_value
**element_obj
)
1039 enum bt_value_array_append_element_status ret
;
1040 struct bt_value
*empty_array_obj
= NULL
;
1042 BT_ASSERT_PRE_NO_ERROR();
1044 empty_array_obj
= bt_value_array_create();
1045 ret
= bt_value_array_append_element(array_obj
,
1046 (void *) empty_array_obj
);
1049 *element_obj
= empty_array_obj
;
1052 bt_object_put_ref(empty_array_obj
);
1056 enum bt_value_array_append_element_status
1057 bt_value_array_append_empty_map_element(struct bt_value
*array_obj
,
1058 struct bt_value
**element_obj
)
1060 enum bt_value_array_append_element_status ret
;
1061 struct bt_value
*map_obj
= NULL
;
1063 BT_ASSERT_PRE_NO_ERROR();
1065 map_obj
= bt_value_map_create();
1066 ret
= bt_value_array_append_element(array_obj
,
1070 *element_obj
= map_obj
;
1073 bt_object_put_ref(map_obj
);
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
,
1079 struct bt_value
*element_obj
)
1081 struct bt_value_array
*typed_array_obj
=
1082 BT_VALUE_TO_ARRAY(array_obj
);
1084 BT_ASSERT_PRE_NO_ERROR();
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
);
1088 BT_ASSERT_PRE_DEV_VALUE_HOT(array_obj
, "Array value object");
1089 BT_ASSERT_PRE_VALID_INDEX(index
, typed_array_obj
->garray
->len
);
1090 bt_object_put_ref(g_ptr_array_index(typed_array_obj
->garray
, index
));
1091 g_ptr_array_index(typed_array_obj
->garray
, index
) = element_obj
;
1092 bt_object_get_ref(element_obj
);
1093 BT_LOGT("Set array value's element: array-value-addr=%p, "
1094 "index=%" PRIu64
", element-value-addr=%p",
1095 array_obj
, index
, element_obj
);
1096 return BT_FUNC_STATUS_OK
;
1099 uint64_t bt_value_map_get_size(const struct bt_value
*map_obj
)
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
);
1103 return (uint64_t) g_hash_table_size(BT_VALUE_TO_MAP(map_obj
)->ght
);
1106 struct bt_value
*bt_value_map_borrow_entry_value(struct bt_value
*map_obj
,
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
);
1112 return g_hash_table_lookup(BT_VALUE_TO_MAP(map_obj
)->ght
,
1113 GUINT_TO_POINTER(g_quark_from_string(key
)));
1116 const struct bt_value
*bt_value_map_borrow_entry_value_const(
1117 const struct bt_value
*map_obj
, const char *key
)
1119 return bt_value_map_borrow_entry_value((void *) map_obj
, key
);
1122 bt_bool
bt_value_map_has_entry(const struct bt_value
*map_obj
, const char *key
)
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
);
1127 return bt_g_hash_table_contains(BT_VALUE_TO_MAP(map_obj
)->ght
,
1128 GUINT_TO_POINTER(g_quark_from_string(key
)));
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
)
1135 BT_ASSERT_PRE_NO_ERROR();
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
);
1140 BT_ASSERT_PRE_DEV_VALUE_HOT(map_obj
, "Map value object");
1141 g_hash_table_insert(BT_VALUE_TO_MAP(map_obj
)->ght
,
1142 GUINT_TO_POINTER(g_quark_from_string(key
)), element_obj
);
1143 bt_object_get_ref(element_obj
);
1144 BT_LOGT("Inserted value into map value: map-value-addr=%p, "
1145 "key=\"%s\", element-value-addr=%p",
1146 map_obj
, key
, element_obj
);
1147 return BT_FUNC_STATUS_OK
;
1150 enum bt_value_map_insert_entry_status
bt_value_map_insert_bool_entry(
1151 struct bt_value
*map_obj
, const char *key
, bt_bool val
)
1153 enum bt_value_map_insert_entry_status ret
;
1154 struct bt_value
*bool_obj
= NULL
;
1156 BT_ASSERT_PRE_NO_ERROR();
1158 bool_obj
= bt_value_bool_create_init(val
);
1159 ret
= bt_value_map_insert_entry(map_obj
, key
,
1161 bt_object_put_ref(bool_obj
);
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
)
1169 enum bt_value_map_insert_entry_status ret
;
1170 struct bt_value
*integer_obj
= NULL
;
1172 BT_ASSERT_PRE_NO_ERROR();
1174 integer_obj
= bt_value_integer_unsigned_create_init(val
);
1175 ret
= bt_value_map_insert_entry(map_obj
, key
,
1176 (void *) integer_obj
);
1177 bt_object_put_ref(integer_obj
);
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
)
1185 enum bt_value_map_insert_entry_status ret
;
1186 struct bt_value
*integer_obj
= NULL
;
1188 BT_ASSERT_PRE_NO_ERROR();
1190 integer_obj
= bt_value_integer_signed_create_init(val
);
1191 ret
= bt_value_map_insert_entry(map_obj
, key
,
1192 (void *) integer_obj
);
1193 bt_object_put_ref(integer_obj
);
1197 enum bt_value_map_insert_entry_status
bt_value_map_insert_real_entry(
1198 struct bt_value
*map_obj
, const char *key
, double val
)
1200 enum bt_value_map_insert_entry_status ret
;
1201 struct bt_value
*real_obj
= NULL
;
1203 BT_ASSERT_PRE_NO_ERROR();
1205 real_obj
= bt_value_real_create_init(val
);
1206 ret
= bt_value_map_insert_entry(map_obj
, key
,
1208 bt_object_put_ref(real_obj
);
1212 enum bt_value_map_insert_entry_status
bt_value_map_insert_string_entry(
1213 struct bt_value
*map_obj
, const char *key
,
1216 enum bt_value_map_insert_entry_status ret
;
1217 struct bt_value
*string_obj
= NULL
;
1219 BT_ASSERT_PRE_NO_ERROR();
1221 string_obj
= bt_value_string_create_init(val
);
1222 ret
= bt_value_map_insert_entry(map_obj
, key
,
1223 (void *) string_obj
);
1224 bt_object_put_ref(string_obj
);
1228 enum bt_value_map_insert_entry_status
1229 bt_value_map_insert_empty_array_entry(
1230 struct bt_value
*map_obj
, const char *key
,
1231 bt_value
**entry_obj
)
1233 enum bt_value_map_insert_entry_status ret
;
1234 struct bt_value
*array_obj
= NULL
;
1236 BT_ASSERT_PRE_NO_ERROR();
1238 array_obj
= bt_value_array_create();
1239 ret
= bt_value_map_insert_entry(map_obj
, key
,
1240 (void *) array_obj
);
1243 *entry_obj
= array_obj
;
1246 bt_object_put_ref(array_obj
);
1250 enum bt_value_map_insert_entry_status
1251 bt_value_map_insert_empty_map_entry(struct bt_value
*map_obj
, const char *key
,
1252 bt_value
**entry_obj
)
1254 enum bt_value_map_insert_entry_status ret
;
1255 struct bt_value
*empty_map_obj
= NULL
;
1257 BT_ASSERT_PRE_NO_ERROR();
1259 empty_map_obj
= bt_value_map_create();
1260 ret
= bt_value_map_insert_entry(map_obj
, key
,
1261 (void *) empty_map_obj
);
1264 *entry_obj
= empty_map_obj
;
1267 bt_object_put_ref(empty_map_obj
);
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
,
1275 int status
= BT_FUNC_STATUS_OK
;
1276 gpointer key
, element_obj
;
1277 GHashTableIter iter
;
1278 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1280 BT_ASSERT_PRE_NO_ERROR();
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
);
1285 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
1287 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
1288 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
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
) {
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
);
1302 if (status
== BT_FUNC_STATUS_ERROR
) {
1304 * User function error becomes a
1305 * user error from this
1306 * function's caller's
1309 status
= BT_FUNC_STATUS_USER_ERROR
;
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
);
1326 enum bt_value_map_foreach_entry_const_status
bt_value_map_foreach_entry_const(
1327 const struct bt_value
*map_obj
,
1328 bt_value_map_foreach_entry_const_func func
, void *data
)
1330 BT_ASSERT_PRE_NO_ERROR();
1332 return (int) bt_value_map_foreach_entry((void *) map_obj
,
1333 (bt_value_map_foreach_entry_func
) func
, data
);
1336 struct extend_map_element_data
{
1337 struct bt_value
*base_obj
;
1341 bt_value_map_foreach_entry_const_func_status
extend_map_element(
1342 const char *key
, const struct bt_value
*extension_obj_elem
,
1346 struct extend_map_element_data
*extend_data
= data
;
1347 struct bt_value
*extension_obj_elem_copy
= NULL
;
1349 /* Copy object which is to replace the current one */
1350 status
= bt_value_copy(extension_obj_elem
, &extension_obj_elem_copy
);
1352 BT_LIB_LOGE_APPEND_CAUSE("Cannot copy map element: %!+v",
1353 extension_obj_elem
);
1357 BT_ASSERT(extension_obj_elem_copy
);
1359 /* Replace in base map value. */
1360 status
= bt_value_map_insert_entry(extend_data
->base_obj
, key
,
1361 extension_obj_elem_copy
);
1363 BT_LIB_LOGE_APPEND_CAUSE(
1364 "Cannot replace value in base map value: key=\"%s\", "
1365 "%![base-map-value-]+v, %![element-value-]+v",
1366 key
, extend_data
->base_obj
, extension_obj_elem_copy
);
1373 BT_ASSERT(status
< 0);
1376 BT_OBJECT_PUT_REF_AND_RESET(extension_obj_elem_copy
);
1377 BT_ASSERT(status
== BT_FUNC_STATUS_OK
||
1378 status
== BT_FUNC_STATUS_MEMORY_ERROR
);
1382 enum bt_value_map_extend_status
bt_value_map_extend(
1383 struct bt_value
*base_map_obj
,
1384 const struct bt_value
*extension_obj
)
1386 int status
= BT_FUNC_STATUS_OK
;
1387 struct extend_map_element_data extend_data
= {
1391 BT_ASSERT_PRE_NO_ERROR();
1392 BT_ASSERT_PRE_NON_NULL(base_map_obj
, "Base value object");
1393 BT_ASSERT_PRE_DEV_VALUE_HOT(base_map_obj
, "Base value object");
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
);
1397 BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p",
1398 base_map_obj
, extension_obj
);
1401 * For each key in the extension map object, replace this key
1402 * in the base map object.
1404 extend_data
.base_obj
= base_map_obj
;
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
);
1409 BT_LIB_LOGE_APPEND_CAUSE(
1410 "Cannot iterate on the extension object's elements: "
1411 "%![extension-value-]+v", extension_obj
);
1417 enum bt_value_copy_status
bt_value_copy(const struct bt_value
*object
,
1418 struct bt_value
**copy_obj
)
1420 enum bt_value_copy_status status
= BT_FUNC_STATUS_OK
;
1422 BT_ASSERT_PRE_NO_ERROR();
1423 BT_ASSERT_PRE_NON_NULL(object
, "Value object");
1424 BT_ASSERT_PRE_NON_NULL(copy_obj
, "Value object copy (output)");
1425 BT_LOGD("Copying value object: addr=%p", object
);
1426 *copy_obj
= copy_funcs
[object
->type
](object
);
1428 BT_LOGD("Copied value object: copy-value-addr=%p",
1431 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1433 BT_LIB_LOGE_APPEND_CAUSE("Failed to copy value object.");
1439 bt_bool
bt_value_is_equal(const struct bt_value
*object_a
,
1440 const struct bt_value
*object_b
)
1442 bt_bool ret
= BT_FALSE
;
1444 BT_ASSERT_PRE_DEV_NON_NULL(object_a
, "Value object A");
1445 BT_ASSERT_PRE_DEV_NON_NULL(object_b
, "Value object B");
1447 if (object_a
->type
!= object_b
->type
) {
1448 BT_LOGT("Values are different: type mismatch: "
1449 "value-a-addr=%p, value-b-addr=%p, "
1450 "value-a-type=%s, value-b-type=%s",
1452 bt_common_value_type_string(object_a
->type
),
1453 bt_common_value_type_string(object_b
->type
));
1457 ret
= is_equal_funcs
[object_a
->type
](object_a
, object_b
);
1463 void bt_value_get_ref(const struct bt_value
*value
)
1465 bt_object_get_ref(value
);
1468 void bt_value_put_ref(const struct bt_value
*value
)
1470 bt_object_put_ref(value
);