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 "VALUES"
24 #include <babeltrace/lib-logging-internal.h>
30 #include <babeltrace/compiler-internal.h>
31 #include <babeltrace/common-internal.h>
32 #include <babeltrace/value-const.h>
33 #include <babeltrace/value.h>
34 #include <babeltrace/compat/glib-internal.h>
35 #include <babeltrace/types.h>
36 #include <babeltrace/assert-pre-internal.h>
37 #include <babeltrace/object-internal.h>
38 #include <babeltrace/value-internal.h>
39 #include <babeltrace/assert-internal.h>
41 #define BT_VALUE_FROM_CONCRETE(_concrete) ((struct bt_value *) (_concrete))
42 #define BT_VALUE_TO_BOOL(_base) ((struct bt_value_bool *) (_base))
43 #define BT_VALUE_TO_INTEGER(_base) ((struct bt_value_integer *) (_base))
44 #define BT_VALUE_TO_REAL(_base) ((struct bt_value_real *) (_base))
45 #define BT_VALUE_TO_STRING(_base) ((struct bt_value_string *) (_base))
46 #define BT_VALUE_TO_ARRAY(_base) ((struct bt_value_array *) (_base))
47 #define BT_VALUE_TO_MAP(_base) ((struct bt_value_map *) (_base))
49 #define BT_ASSERT_PRE_VALUE_IS_TYPE(_value, _type) \
50 BT_ASSERT_PRE(((struct bt_value *) (_value))->type == (_type), \
51 "Value has the wrong type ID: expected-type=%s, " \
52 "%![value-]+v", bt_common_value_type_string(_type), \
55 #define BT_ASSERT_PRE_VALUE_HOT(_value, _name) \
56 BT_ASSERT_PRE_HOT(((struct bt_value *) (_value)), (_name), \
59 #define BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(_index, _count) \
60 BT_ASSERT_PRE((_index) < (_count), \
61 "Index is out of bound: " \
62 "index=%" PRIu64 ", count=%u", (_index), (_count));
65 struct bt_object base
;
66 enum bt_value_type type
;
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
*bt_value_null
= &bt_value_null_instance
;
92 struct bt_value_bool
{
97 struct bt_value_integer
{
102 struct bt_value_real
{
103 struct bt_value base
;
107 struct bt_value_string
{
108 struct bt_value base
;
112 struct bt_value_array
{
113 struct bt_value base
;
117 struct bt_value_map
{
118 struct bt_value base
;
123 void bt_value_destroy(struct bt_object
*obj
);
126 void bt_value_string_destroy(struct bt_value
*object
)
128 g_string_free(BT_VALUE_TO_STRING(object
)->gstr
, TRUE
);
129 BT_VALUE_TO_STRING(object
)->gstr
= NULL
;
133 void bt_value_array_destroy(struct bt_value
*object
)
136 * Pointer array's registered value destructor will take care
137 * of putting each contained object.
139 g_ptr_array_free(BT_VALUE_TO_ARRAY(object
)->garray
, TRUE
);
140 BT_VALUE_TO_ARRAY(object
)->garray
= NULL
;
144 void bt_value_map_destroy(struct bt_value
*object
)
147 * Hash table's registered value destructor will take care of
148 * putting each contained object. Keys are GQuarks and cannot
149 * be destroyed anyway.
151 g_hash_table_destroy(BT_VALUE_TO_MAP(object
)->ght
);
152 BT_VALUE_TO_MAP(object
)->ght
= NULL
;
156 void (* const destroy_funcs
[])(struct bt_value
*) = {
157 [BT_VALUE_TYPE_NULL
] = NULL
,
158 [BT_VALUE_TYPE_BOOL
] = NULL
,
159 [BT_VALUE_TYPE_INTEGER
] = NULL
,
160 [BT_VALUE_TYPE_REAL
] = NULL
,
161 [BT_VALUE_TYPE_STRING
] = bt_value_string_destroy
,
162 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_destroy
,
163 [BT_VALUE_TYPE_MAP
] = bt_value_map_destroy
,
167 struct bt_value
*bt_value_null_copy(const struct bt_value
*null_obj
)
169 return (void *) bt_value_null
;
173 struct bt_value
*bt_value_bool_copy(const struct bt_value
*bool_obj
)
175 return bt_value_bool_create_init(
176 BT_VALUE_TO_BOOL(bool_obj
)->value
);
180 struct bt_value
*bt_value_integer_copy(
181 const struct bt_value
*integer_obj
)
183 return bt_value_integer_create_init(
184 BT_VALUE_TO_INTEGER(integer_obj
)->value
);
188 struct bt_value
*bt_value_real_copy(const struct bt_value
*real_obj
)
190 return bt_value_real_create_init(
191 BT_VALUE_TO_REAL(real_obj
)->value
);
195 struct bt_value
*bt_value_string_copy(const struct bt_value
*string_obj
)
197 return bt_value_string_create_init(
198 BT_VALUE_TO_STRING(string_obj
)->gstr
->str
);
202 struct bt_value
*bt_value_array_copy(const struct bt_value
*array_obj
)
206 struct bt_value
*copy_obj
;
207 struct bt_value_array
*typed_array_obj
;
209 BT_LOGD("Copying array value: addr=%p", array_obj
);
210 typed_array_obj
= BT_VALUE_TO_ARRAY(array_obj
);
211 copy_obj
= bt_value_array_create();
213 BT_LOGE_STR("Cannot create empty array value.");
217 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
218 struct bt_value
*element_obj_copy
= NULL
;
219 const struct bt_value
*element_obj
=
220 bt_value_array_borrow_element_by_index_const(
223 BT_ASSERT(element_obj
);
224 BT_LOGD("Copying array value's element: element-addr=%p, "
225 "index=%d", element_obj
, i
);
226 ret
= bt_value_copy(element_obj
, &element_obj_copy
);
228 BT_LOGE("Cannot copy array value's element: "
229 "array-addr=%p, index=%d",
231 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
235 BT_ASSERT(element_obj_copy
);
236 ret
= bt_value_array_append_element(copy_obj
,
237 (void *) element_obj_copy
);
238 BT_OBJECT_PUT_REF_AND_RESET(element_obj_copy
);
240 BT_LOGE("Cannot append to array value: addr=%p",
242 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
247 BT_LOGD("Copied array value: original-addr=%p, copy-addr=%p",
248 array_obj
, copy_obj
);
255 struct bt_value
*bt_value_map_copy(const struct bt_value
*map_obj
)
259 gpointer key
, element_obj
;
260 struct bt_value
*copy_obj
;
261 struct bt_value
*element_obj_copy
= NULL
;
262 struct bt_value_map
*typed_map_obj
;
264 BT_LOGD("Copying map value: addr=%p", map_obj
);
265 typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
266 copy_obj
= bt_value_map_create();
271 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
273 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
274 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
277 BT_LOGD("Copying map value's element: element-addr=%p, "
278 "key=\"%s\"", element_obj
, key_str
);
279 ret
= bt_value_copy(element_obj
, &element_obj_copy
);
281 BT_LOGE("Cannot copy map value's element: "
282 "map-addr=%p, key=\"%s\"",
284 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
288 BT_ASSERT(element_obj_copy
);
289 ret
= bt_value_map_insert_entry(copy_obj
, key_str
,
290 (void *) element_obj_copy
);
291 BT_OBJECT_PUT_REF_AND_RESET(element_obj_copy
);
293 BT_LOGE("Cannot insert into map value: addr=%p, key=\"%s\"",
295 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
300 BT_LOGD("Copied map value: addr=%p", map_obj
);
307 struct bt_value
*(* const copy_funcs
[])(const struct bt_value
*) = {
308 [BT_VALUE_TYPE_NULL
] = bt_value_null_copy
,
309 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_copy
,
310 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_copy
,
311 [BT_VALUE_TYPE_REAL
] = bt_value_real_copy
,
312 [BT_VALUE_TYPE_STRING
] = bt_value_string_copy
,
313 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_copy
,
314 [BT_VALUE_TYPE_MAP
] = bt_value_map_copy
,
318 bt_bool
bt_value_null_compare(const struct bt_value
*object_a
,
319 const struct bt_value
*object_b
)
322 * Always BT_TRUE since bt_value_compare() already checks if both
323 * object_a and object_b have the same type, and in the case of
324 * null value objects, they're always the same if it is so.
330 bt_bool
bt_value_bool_compare(const struct bt_value
*object_a
,
331 const struct bt_value
*object_b
)
333 if (BT_VALUE_TO_BOOL(object_a
)->value
!=
334 BT_VALUE_TO_BOOL(object_b
)->value
) {
335 BT_LOGV("Boolean value objects are different: "
336 "bool-a-val=%d, bool-b-val=%d",
337 BT_VALUE_TO_BOOL(object_a
)->value
,
338 BT_VALUE_TO_BOOL(object_b
)->value
);
346 bt_bool
bt_value_integer_compare(const struct bt_value
*object_a
,
347 const struct bt_value
*object_b
)
349 if (BT_VALUE_TO_INTEGER(object_a
)->value
!=
350 BT_VALUE_TO_INTEGER(object_b
)->value
) {
351 BT_LOGV("Integer value objects are different: "
352 "int-a-val=%" PRId64
", int-b-val=%" PRId64
,
353 BT_VALUE_TO_INTEGER(object_a
)->value
,
354 BT_VALUE_TO_INTEGER(object_b
)->value
);
362 bt_bool
bt_value_real_compare(const struct bt_value
*object_a
,
363 const struct bt_value
*object_b
)
365 if (BT_VALUE_TO_REAL(object_a
)->value
!=
366 BT_VALUE_TO_REAL(object_b
)->value
) {
367 BT_LOGV("Real number value objects are different: "
368 "real-a-val=%f, real-b-val=%f",
369 BT_VALUE_TO_REAL(object_a
)->value
,
370 BT_VALUE_TO_REAL(object_b
)->value
);
378 bt_bool
bt_value_string_compare(const struct bt_value
*object_a
,
379 const struct bt_value
*object_b
)
381 if (strcmp(BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
382 BT_VALUE_TO_STRING(object_b
)->gstr
->str
) != 0) {
383 BT_LOGV("String value objects are different: "
384 "string-a-val=\"%s\", string-b-val=\"%s\"",
385 BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
386 BT_VALUE_TO_STRING(object_b
)->gstr
->str
);
394 bt_bool
bt_value_array_compare(const struct bt_value
*object_a
,
395 const struct bt_value
*object_b
)
398 bt_bool ret
= BT_TRUE
;
399 const struct bt_value_array
*array_obj_a
=
400 BT_VALUE_TO_ARRAY(object_a
);
402 if (bt_value_array_get_size(object_a
) !=
403 bt_value_array_get_size(object_b
)) {
404 BT_LOGV("Array values are different: size mismatch "
405 "value-a-addr=%p, value-b-addr=%p, "
406 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
408 bt_value_array_get_size(object_a
),
409 bt_value_array_get_size(object_b
));
414 for (i
= 0; i
< array_obj_a
->garray
->len
; ++i
) {
415 const struct bt_value
*element_obj_a
;
416 const struct bt_value
*element_obj_b
;
418 element_obj_a
= bt_value_array_borrow_element_by_index_const(
420 element_obj_b
= bt_value_array_borrow_element_by_index_const(
423 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
424 BT_LOGV("Array values's elements are different: "
425 "value-a-addr=%p, value-b-addr=%p, index=%d",
426 element_obj_a
, element_obj_b
, i
);
437 bt_bool
bt_value_map_compare(const struct bt_value
*object_a
,
438 const struct bt_value
*object_b
)
440 bt_bool ret
= BT_TRUE
;
442 gpointer key
, element_obj_a
;
443 const struct bt_value_map
*map_obj_a
= BT_VALUE_TO_MAP(object_a
);
445 if (bt_value_map_get_size(object_a
) !=
446 bt_value_map_get_size(object_b
)) {
447 BT_LOGV("Map values are different: size mismatch "
448 "value-a-addr=%p, value-b-addr=%p, "
449 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
451 bt_value_map_get_size(object_a
),
452 bt_value_map_get_size(object_b
));
457 g_hash_table_iter_init(&iter
, map_obj_a
->ght
);
459 while (g_hash_table_iter_next(&iter
, &key
, &element_obj_a
)) {
460 const struct bt_value
*element_obj_b
;
461 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
463 element_obj_b
= bt_value_map_borrow_entry_value_const(object_b
,
466 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
467 BT_LOGV("Map values's elements are different: "
468 "value-a-addr=%p, value-b-addr=%p, key=\"%s\"",
469 element_obj_a
, element_obj_b
, key_str
);
480 bt_bool (* const compare_funcs
[])(const struct bt_value
*,
481 const struct bt_value
*) = {
482 [BT_VALUE_TYPE_NULL
] = bt_value_null_compare
,
483 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_compare
,
484 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_compare
,
485 [BT_VALUE_TYPE_REAL
] = bt_value_real_compare
,
486 [BT_VALUE_TYPE_STRING
] = bt_value_string_compare
,
487 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_compare
,
488 [BT_VALUE_TYPE_MAP
] = bt_value_map_compare
,
492 void bt_value_null_freeze(struct bt_value
*object
)
497 void bt_value_generic_freeze(struct bt_value
*object
)
499 object
->frozen
= BT_TRUE
;
503 void bt_value_array_freeze(struct bt_value
*object
)
506 struct bt_value_array
*typed_array_obj
=
507 BT_VALUE_TO_ARRAY(object
);
509 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
510 bt_value_freeze(g_ptr_array_index(typed_array_obj
->garray
, i
));
513 bt_value_generic_freeze(object
);
517 void bt_value_map_freeze(struct bt_value
*object
)
520 gpointer key
, element_obj
;
521 const struct bt_value_map
*map_obj
= BT_VALUE_TO_MAP(object
);
523 g_hash_table_iter_init(&iter
, map_obj
->ght
);
525 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
526 bt_value_freeze(element_obj
);
529 bt_value_generic_freeze(object
);
533 void (* const freeze_funcs
[])(struct bt_value
*) = {
534 [BT_VALUE_TYPE_NULL
] = bt_value_null_freeze
,
535 [BT_VALUE_TYPE_BOOL
] = bt_value_generic_freeze
,
536 [BT_VALUE_TYPE_INTEGER
] = bt_value_generic_freeze
,
537 [BT_VALUE_TYPE_REAL
] = bt_value_generic_freeze
,
538 [BT_VALUE_TYPE_STRING
] = bt_value_generic_freeze
,
539 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_freeze
,
540 [BT_VALUE_TYPE_MAP
] = bt_value_map_freeze
,
544 void bt_value_destroy(struct bt_object
*obj
)
546 struct bt_value
*value
;
548 value
= container_of(obj
, struct bt_value
, base
);
549 BT_LOGD("Destroying value: addr=%p", value
);
551 if (bt_value_is_null(value
)) {
552 BT_LOGD_STR("Not destroying the null value singleton.");
556 if (destroy_funcs
[value
->type
]) {
557 destroy_funcs
[value
->type
](value
);
564 enum bt_value_status
_bt_value_freeze(const struct bt_value
*c_object
)
566 const struct bt_value
*object
= (void *) c_object
;
567 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
571 if (object
->frozen
) {
575 BT_LOGD("Freezing value: addr=%p", object
);
576 freeze_funcs
[object
->type
]((void *) object
);
582 enum bt_value_type
bt_value_get_type(const struct bt_value
*object
)
584 BT_ASSERT_PRE_NON_NULL(object
, "Value object");
589 struct bt_value
bt_value_create_base(enum bt_value_type type
)
591 struct bt_value value
;
594 value
.frozen
= BT_FALSE
;
595 bt_object_init_shared(&value
.base
, bt_value_destroy
);
599 struct bt_value
*bt_value_bool_create_init(bt_bool val
)
601 struct bt_value_bool
*bool_obj
;
603 BT_LOGD("Creating boolean value object: val=%d", val
);
604 bool_obj
= g_new0(struct bt_value_bool
, 1);
606 BT_LOGE_STR("Failed to allocate one boolean value object.");
610 bool_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_BOOL
);
611 bool_obj
->value
= val
;
612 BT_LOGD("Created boolean value object: addr=%p", bool_obj
);
615 return (void *) BT_VALUE_FROM_CONCRETE(bool_obj
);
618 struct bt_value
*bt_value_bool_create(void)
620 return bt_value_bool_create_init(BT_FALSE
);
623 struct bt_value
*bt_value_integer_create_init(int64_t val
)
625 struct bt_value_integer
*integer_obj
;
627 BT_LOGD("Creating integer value object: val=%" PRId64
, val
);
628 integer_obj
= g_new0(struct bt_value_integer
, 1);
630 BT_LOGE_STR("Failed to allocate one integer value object.");
634 integer_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_INTEGER
);
635 integer_obj
->value
= val
;
636 BT_LOGD("Created integer value object: addr=%p",
640 return (void *) BT_VALUE_FROM_CONCRETE(integer_obj
);
643 struct bt_value
*bt_value_integer_create(void)
645 return bt_value_integer_create_init(0);
648 struct bt_value
*bt_value_real_create_init(double val
)
650 struct bt_value_real
*real_obj
;
652 BT_LOGD("Creating real number value object: val=%f", val
);
653 real_obj
= g_new0(struct bt_value_real
, 1);
655 BT_LOGE_STR("Failed to allocate one real number value object.");
659 real_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_REAL
);
660 real_obj
->value
= val
;
661 BT_LOGD("Created real number value object: addr=%p",
665 return (void *) BT_VALUE_FROM_CONCRETE(real_obj
);
668 struct bt_value
*bt_value_real_create(void)
670 return bt_value_real_create_init(0.);
673 struct bt_value
*bt_value_string_create_init(const char *val
)
675 struct bt_value_string
*string_obj
= NULL
;
678 BT_LOGW_STR("Invalid parameter: value is NULL.");
682 BT_LOGD("Creating string value object: val-len=%zu", strlen(val
));
683 string_obj
= g_new0(struct bt_value_string
, 1);
685 BT_LOGE_STR("Failed to allocate one string object.");
689 string_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_STRING
);
690 string_obj
->gstr
= g_string_new(val
);
691 if (!string_obj
->gstr
) {
692 BT_LOGE_STR("Failed to allocate a GString.");
698 BT_LOGD("Created string value object: addr=%p",
702 return (void *) BT_VALUE_FROM_CONCRETE(string_obj
);
705 struct bt_value
*bt_value_string_create(void)
707 return bt_value_string_create_init("");
710 struct bt_value
*bt_value_array_create(void)
712 struct bt_value_array
*array_obj
;
714 BT_LOGD_STR("Creating empty array value object.");
715 array_obj
= g_new0(struct bt_value_array
, 1);
717 BT_LOGE_STR("Failed to allocate one array object.");
721 array_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_ARRAY
);
722 array_obj
->garray
= bt_g_ptr_array_new_full(0,
723 (GDestroyNotify
) bt_object_put_ref
);
724 if (!array_obj
->garray
) {
725 BT_LOGE_STR("Failed to allocate a GPtrArray.");
731 BT_LOGD("Created array value object: addr=%p",
735 return (void *) BT_VALUE_FROM_CONCRETE(array_obj
);
738 struct bt_value
*bt_value_map_create(void)
740 struct bt_value_map
*map_obj
;
742 BT_LOGD_STR("Creating empty map value object.");
743 map_obj
= g_new0(struct bt_value_map
, 1);
745 BT_LOGE_STR("Failed to allocate one map object.");
749 map_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_MAP
);
750 map_obj
->ght
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
751 NULL
, (GDestroyNotify
) bt_object_put_ref
);
753 BT_LOGE_STR("Failed to allocate a GHashTable.");
759 BT_LOGD("Created map value object: addr=%p",
763 return (void *) BT_VALUE_FROM_CONCRETE(map_obj
);
766 bt_bool
bt_value_bool_get(const struct bt_value
*bool_obj
)
768 BT_ASSERT_PRE_NON_NULL(bool_obj
, "Value object");
769 BT_ASSERT_PRE_VALUE_IS_TYPE(bool_obj
, BT_VALUE_TYPE_BOOL
);
770 return BT_VALUE_TO_BOOL(bool_obj
)->value
;
773 void bt_value_bool_set(struct bt_value
*bool_obj
, bt_bool val
)
775 BT_ASSERT_PRE_NON_NULL(bool_obj
, "Value object");
776 BT_ASSERT_PRE_VALUE_IS_TYPE(bool_obj
, BT_VALUE_TYPE_BOOL
);
777 BT_ASSERT_PRE_VALUE_HOT(bool_obj
, "Value object");
778 BT_VALUE_TO_BOOL(bool_obj
)->value
= val
;
779 BT_LOGV("Set boolean value's raw value: value-addr=%p, value=%d",
783 int64_t bt_value_integer_get(const struct bt_value
*integer_obj
)
785 BT_ASSERT_PRE_NON_NULL(integer_obj
, "Value object");
786 BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj
, BT_VALUE_TYPE_INTEGER
);
787 return BT_VALUE_TO_INTEGER(integer_obj
)->value
;
790 void bt_value_integer_set(struct bt_value
*integer_obj
,
793 BT_ASSERT_PRE_NON_NULL(integer_obj
, "Value object");
794 BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj
, BT_VALUE_TYPE_INTEGER
);
795 BT_ASSERT_PRE_VALUE_HOT(integer_obj
, "Value object");
796 BT_VALUE_TO_INTEGER(integer_obj
)->value
= val
;
797 BT_LOGV("Set integer value's raw value: value-addr=%p, value=%" PRId64
,
801 double bt_value_real_get(const struct bt_value
*real_obj
)
803 BT_ASSERT_PRE_NON_NULL(real_obj
, "Value object");
804 BT_ASSERT_PRE_VALUE_IS_TYPE(real_obj
, BT_VALUE_TYPE_REAL
);
805 return BT_VALUE_TO_REAL(real_obj
)->value
;
808 void bt_value_real_set(struct bt_value
*real_obj
, double val
)
810 BT_ASSERT_PRE_NON_NULL(real_obj
, "Value object");
811 BT_ASSERT_PRE_VALUE_IS_TYPE(real_obj
, BT_VALUE_TYPE_REAL
);
812 BT_ASSERT_PRE_VALUE_HOT(real_obj
, "Value object");
813 BT_VALUE_TO_REAL(real_obj
)->value
= val
;
814 BT_LOGV("Set real number value's raw value: value-addr=%p, value=%f",
818 const char *bt_value_string_get(const struct bt_value
*string_obj
)
820 BT_ASSERT_PRE_NON_NULL(string_obj
, "Value object");
821 BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj
, BT_VALUE_TYPE_STRING
);
822 return BT_VALUE_TO_STRING(string_obj
)->gstr
->str
;
825 enum bt_value_status
bt_value_string_set(
826 struct bt_value
*string_obj
, const char *val
)
828 BT_ASSERT_PRE_NON_NULL(string_obj
, "Value object");
829 BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj
, BT_VALUE_TYPE_STRING
);
830 BT_ASSERT_PRE_VALUE_HOT(string_obj
, "Value object");
831 g_string_assign(BT_VALUE_TO_STRING(string_obj
)->gstr
, val
);
832 BT_LOGV("Set string value's raw value: value-addr=%p, raw-value-addr=%p",
834 return BT_VALUE_STATUS_OK
;
837 uint64_t bt_value_array_get_size(const struct bt_value
*array_obj
)
839 BT_ASSERT_PRE_NON_NULL(array_obj
, "Value object");
840 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
841 return (uint64_t) BT_VALUE_TO_ARRAY(array_obj
)->garray
->len
;
844 struct bt_value
*bt_value_array_borrow_element_by_index(
845 struct bt_value
*array_obj
, uint64_t index
)
847 struct bt_value_array
*typed_array_obj
=
848 BT_VALUE_TO_ARRAY(array_obj
);
850 BT_ASSERT_PRE_NON_NULL(array_obj
, "Value object");
851 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
852 BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index
,
853 typed_array_obj
->garray
->len
);
854 return g_ptr_array_index(typed_array_obj
->garray
, index
);
857 const struct bt_value
*bt_value_array_borrow_element_by_index_const(
858 const struct bt_value
*array_obj
,
861 return bt_value_array_borrow_element_by_index(
862 (void *) array_obj
, index
);
865 enum bt_value_status
bt_value_array_append_element(
866 struct bt_value
*array_obj
,
867 struct bt_value
*element_obj
)
869 struct bt_value_array
*typed_array_obj
=
870 BT_VALUE_TO_ARRAY(array_obj
);
872 BT_ASSERT_PRE_NON_NULL(array_obj
, "Array value object");
873 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
874 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
875 BT_ASSERT_PRE_VALUE_HOT(array_obj
, "Array value object");
876 g_ptr_array_add(typed_array_obj
->garray
, element_obj
);
877 bt_object_get_ref(element_obj
);
878 BT_LOGV("Appended element to array value: array-value-addr=%p, "
879 "element-value-addr=%p, new-size=%u",
880 array_obj
, element_obj
, typed_array_obj
->garray
->len
);
881 return BT_VALUE_STATUS_OK
;
884 enum bt_value_status
bt_value_array_append_bool_element(
885 struct bt_value
*array_obj
, bt_bool val
)
887 enum bt_value_status ret
;
888 struct bt_value
*bool_obj
= NULL
;
890 bool_obj
= bt_value_bool_create_init(val
);
891 ret
= bt_value_array_append_element(array_obj
,
893 bt_object_put_ref(bool_obj
);
897 enum bt_value_status
bt_value_array_append_integer_element(
898 struct bt_value
*array_obj
, int64_t val
)
900 enum bt_value_status ret
;
901 struct bt_value
*integer_obj
= NULL
;
903 integer_obj
= bt_value_integer_create_init(val
);
904 ret
= bt_value_array_append_element(array_obj
,
905 (void *) integer_obj
);
906 bt_object_put_ref(integer_obj
);
910 enum bt_value_status
bt_value_array_append_real_element(
911 struct bt_value
*array_obj
, double val
)
913 enum bt_value_status ret
;
914 struct bt_value
*real_obj
= NULL
;
916 real_obj
= bt_value_real_create_init(val
);
917 ret
= bt_value_array_append_element(array_obj
,
919 bt_object_put_ref(real_obj
);
923 enum bt_value_status
bt_value_array_append_string_element(
924 struct bt_value
*array_obj
, const char *val
)
926 enum bt_value_status ret
;
927 struct bt_value
*string_obj
= NULL
;
929 string_obj
= bt_value_string_create_init(val
);
930 ret
= bt_value_array_append_element(array_obj
,
931 (void *) string_obj
);
932 bt_object_put_ref(string_obj
);
936 enum bt_value_status
bt_value_array_append_empty_array_element(
937 struct bt_value
*array_obj
)
939 enum bt_value_status ret
;
940 struct bt_value
*empty_array_obj
= NULL
;
942 empty_array_obj
= bt_value_array_create();
943 ret
= bt_value_array_append_element(array_obj
,
944 (void *) empty_array_obj
);
945 bt_object_put_ref(empty_array_obj
);
949 enum bt_value_status
bt_value_array_append_empty_map_element(
950 struct bt_value
*array_obj
)
952 enum bt_value_status ret
;
953 struct bt_value
*map_obj
= NULL
;
955 map_obj
= bt_value_map_create();
956 ret
= bt_value_array_append_element(array_obj
,
958 bt_object_put_ref(map_obj
);
962 enum bt_value_status
bt_value_array_set_element_by_index(
963 struct bt_value
*array_obj
, uint64_t index
,
964 struct bt_value
*element_obj
)
966 struct bt_value_array
*typed_array_obj
=
967 BT_VALUE_TO_ARRAY(array_obj
);
969 BT_ASSERT_PRE_NON_NULL(array_obj
, "Array value object");
970 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
971 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
972 BT_ASSERT_PRE_VALUE_HOT(array_obj
, "Array value object");
973 BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index
,
974 typed_array_obj
->garray
->len
);
975 bt_object_put_ref(g_ptr_array_index(typed_array_obj
->garray
, index
));
976 g_ptr_array_index(typed_array_obj
->garray
, index
) = element_obj
;
977 bt_object_get_ref(element_obj
);
978 BT_LOGV("Set array value's element: array-value-addr=%p, "
979 "index=%" PRIu64
", element-value-addr=%p",
980 array_obj
, index
, element_obj
);
981 return BT_VALUE_STATUS_OK
;
984 uint64_t bt_value_map_get_size(const struct bt_value
*map_obj
)
986 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
987 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
988 return (uint64_t) g_hash_table_size(BT_VALUE_TO_MAP(map_obj
)->ght
);
991 struct bt_value
*bt_value_map_borrow_entry_value(struct bt_value
*map_obj
,
994 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
995 BT_ASSERT_PRE_NON_NULL(key
, "Key");
996 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
997 return g_hash_table_lookup(BT_VALUE_TO_MAP(map_obj
)->ght
,
998 GUINT_TO_POINTER(g_quark_from_string(key
)));
1001 const struct bt_value
*bt_value_map_borrow_entry_value_const(
1002 const struct bt_value
*map_obj
, const char *key
)
1004 return bt_value_map_borrow_entry_value((void *) map_obj
, key
);
1007 bt_bool
bt_value_map_has_entry(const struct bt_value
*map_obj
, const char *key
)
1009 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
1010 BT_ASSERT_PRE_NON_NULL(key
, "Key");
1011 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1012 return bt_g_hash_table_contains(BT_VALUE_TO_MAP(map_obj
)->ght
,
1013 GUINT_TO_POINTER(g_quark_from_string(key
)));
1016 enum bt_value_status
bt_value_map_insert_entry(
1017 struct bt_value
*map_obj
,
1018 const char *key
, struct bt_value
*element_obj
)
1020 BT_ASSERT_PRE_NON_NULL(map_obj
, "Map value object");
1021 BT_ASSERT_PRE_NON_NULL(key
, "Key");
1022 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
1023 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1024 BT_ASSERT_PRE_VALUE_HOT(map_obj
, "Map value object");
1025 g_hash_table_insert(BT_VALUE_TO_MAP(map_obj
)->ght
,
1026 GUINT_TO_POINTER(g_quark_from_string(key
)), element_obj
);
1027 bt_object_get_ref(element_obj
);
1028 BT_LOGV("Inserted value into map value: map-value-addr=%p, "
1029 "key=\"%s\", element-value-addr=%p",
1030 map_obj
, key
, element_obj
);
1031 return BT_VALUE_STATUS_OK
;
1034 enum bt_value_status
bt_value_map_insert_bool_entry(
1035 struct bt_value
*map_obj
, const char *key
, bt_bool val
)
1037 enum bt_value_status ret
;
1038 struct bt_value
*bool_obj
= NULL
;
1040 bool_obj
= bt_value_bool_create_init(val
);
1041 ret
= bt_value_map_insert_entry(map_obj
, key
,
1043 bt_object_put_ref(bool_obj
);
1047 enum bt_value_status
bt_value_map_insert_integer_entry(
1048 struct bt_value
*map_obj
, const char *key
, int64_t val
)
1050 enum bt_value_status ret
;
1051 struct bt_value
*integer_obj
= NULL
;
1053 integer_obj
= bt_value_integer_create_init(val
);
1054 ret
= bt_value_map_insert_entry(map_obj
, key
,
1055 (void *) integer_obj
);
1056 bt_object_put_ref(integer_obj
);
1060 enum bt_value_status
bt_value_map_insert_real_entry(
1061 struct bt_value
*map_obj
, const char *key
, double val
)
1063 enum bt_value_status ret
;
1064 struct bt_value
*real_obj
= NULL
;
1066 real_obj
= bt_value_real_create_init(val
);
1067 ret
= bt_value_map_insert_entry(map_obj
, key
,
1069 bt_object_put_ref(real_obj
);
1073 enum bt_value_status
bt_value_map_insert_string_entry(
1074 struct bt_value
*map_obj
, const char *key
,
1077 enum bt_value_status ret
;
1078 struct bt_value
*string_obj
= NULL
;
1080 string_obj
= bt_value_string_create_init(val
);
1081 ret
= bt_value_map_insert_entry(map_obj
, key
,
1082 (void *) string_obj
);
1083 bt_object_put_ref(string_obj
);
1087 enum bt_value_status
bt_value_map_insert_empty_array_entry(
1088 struct bt_value
*map_obj
, const char *key
)
1090 enum bt_value_status ret
;
1091 struct bt_value
*array_obj
= NULL
;
1093 array_obj
= bt_value_array_create();
1094 ret
= bt_value_map_insert_entry(map_obj
, key
,
1095 (void *) array_obj
);
1096 bt_object_put_ref(array_obj
);
1100 enum bt_value_status
bt_value_map_insert_empty_map_entry(
1101 struct bt_value
*map_obj
, const char *key
)
1103 enum bt_value_status ret
;
1104 struct bt_value
*empty_map_obj
= NULL
;
1106 empty_map_obj
= bt_value_map_create();
1107 ret
= bt_value_map_insert_entry(map_obj
, key
,
1108 (void *) empty_map_obj
);
1109 bt_object_put_ref(empty_map_obj
);
1113 enum bt_value_status
bt_value_map_foreach_entry(struct bt_value
*map_obj
,
1114 bt_value_map_foreach_entry_func func
, void *data
)
1116 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1117 gpointer key
, element_obj
;
1118 GHashTableIter iter
;
1119 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1121 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
1122 BT_ASSERT_PRE_NON_NULL(func
, "Callback");
1123 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1124 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
1126 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
1127 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
1129 if (!func(key_str
, element_obj
, data
)) {
1130 BT_LOGV("User canceled the loop: key=\"%s\", "
1131 "value-addr=%p, data=%p",
1132 key_str
, element_obj
, data
);
1133 ret
= BT_VALUE_STATUS_CANCELED
;
1141 enum bt_value_status
bt_value_map_foreach_entry_const(
1142 const struct bt_value
*map_obj
,
1143 bt_value_map_foreach_entry_const_func func
, void *data
)
1145 return bt_value_map_foreach_entry((void *) map_obj
,
1146 (bt_value_map_foreach_entry_func
) func
, data
);
1149 struct extend_map_element_data
{
1150 struct bt_value
*extended_obj
;
1151 enum bt_value_status status
;
1155 bt_bool
extend_map_element(const char *key
,
1156 const struct bt_value
*extension_obj_elem
, void *data
)
1158 bt_bool ret
= BT_TRUE
;
1159 struct extend_map_element_data
*extend_data
= data
;
1160 struct bt_value
*extension_obj_elem_copy
= NULL
;
1162 /* Copy object which is to replace the current one */
1163 extend_data
->status
= bt_value_copy(extension_obj_elem
,
1164 &extension_obj_elem_copy
);
1165 if (extend_data
->status
) {
1166 BT_LOGE("Cannot copy map element: addr=%p",
1167 extension_obj_elem
);
1171 BT_ASSERT(extension_obj_elem_copy
);
1173 /* Replace in extended object */
1174 extend_data
->status
= bt_value_map_insert_entry(
1175 extend_data
->extended_obj
, key
,
1176 (void *) extension_obj_elem_copy
);
1177 if (extend_data
->status
) {
1178 BT_LOGE("Cannot replace value in extended value: key=\"%s\", "
1179 "extended-value-addr=%p, element-value-addr=%p",
1180 key
, extend_data
->extended_obj
,
1181 extension_obj_elem_copy
);
1188 BT_ASSERT(extend_data
->status
!= BT_VALUE_STATUS_OK
);
1192 BT_OBJECT_PUT_REF_AND_RESET(extension_obj_elem_copy
);
1196 enum bt_value_status
bt_value_map_extend(
1197 const struct bt_value
*base_map_obj
,
1198 const struct bt_value
*extension_obj
,
1199 struct bt_value
**extended_map_obj
)
1201 struct extend_map_element_data extend_data
= {
1202 .extended_obj
= NULL
,
1203 .status
= BT_VALUE_STATUS_OK
,
1206 BT_ASSERT_PRE_NON_NULL(base_map_obj
, "Base value object");
1207 BT_ASSERT_PRE_NON_NULL(extension_obj
, "Extension value object");
1208 BT_ASSERT_PRE_NON_NULL(extended_map_obj
,
1209 "Extended value object (output)");
1210 BT_ASSERT_PRE_VALUE_IS_TYPE(base_map_obj
, BT_VALUE_TYPE_MAP
);
1211 BT_ASSERT_PRE_VALUE_IS_TYPE(extension_obj
, BT_VALUE_TYPE_MAP
);
1212 BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p",
1213 base_map_obj
, extension_obj
);
1214 *extended_map_obj
= NULL
;
1216 /* Create copy of base map object to start with */
1217 extend_data
.status
= bt_value_copy(base_map_obj
, extended_map_obj
);
1218 if (extend_data
.status
) {
1219 BT_LOGE("Cannot copy base value: base-value-addr=%p",
1224 BT_ASSERT(extended_map_obj
);
1227 * For each key in the extension map object, replace this key
1228 * in the copied map object.
1230 extend_data
.extended_obj
= *extended_map_obj
;
1232 if (bt_value_map_foreach_entry_const(extension_obj
, extend_map_element
,
1234 BT_LOGE("Cannot iterate on the extension object's elements: "
1235 "extension-value-addr=%p", extension_obj
);
1239 if (extend_data
.status
) {
1240 BT_LOGE("Failed to successfully iterate on the extension object's elements: "
1241 "extension-value-addr=%p", extension_obj
);
1245 BT_LOGD("Extended map value: extended-value-addr=%p",
1250 BT_OBJECT_PUT_REF_AND_RESET(*extended_map_obj
);
1251 *extended_map_obj
= NULL
;
1254 return extend_data
.status
;
1257 enum bt_value_status
bt_value_copy(const struct bt_value
*object
,
1258 struct bt_value
**copy_obj
)
1260 enum bt_value_status status
= BT_VALUE_STATUS_OK
;
1262 BT_ASSERT_PRE_NON_NULL(object
, "Value object");
1263 BT_ASSERT_PRE_NON_NULL(copy_obj
, "Value object copy (output)");
1264 BT_LOGD("Copying value object: addr=%p", object
);
1265 *copy_obj
= copy_funcs
[object
->type
](object
);
1267 BT_LOGD("Copied value object: copy-value-addr=%p",
1270 status
= BT_VALUE_STATUS_NOMEM
;
1272 BT_LOGE_STR("Failed to copy value object.");
1278 bt_bool
bt_value_compare(const struct bt_value
*object_a
,
1279 const struct bt_value
*object_b
)
1281 bt_bool ret
= BT_FALSE
;
1283 BT_ASSERT_PRE_NON_NULL(object_a
, "Value object A");
1284 BT_ASSERT_PRE_NON_NULL(object_b
, "Value object B");
1286 if (object_a
->type
!= object_b
->type
) {
1287 BT_LOGV("Values are different: type mismatch: "
1288 "value-a-addr=%p, value-b-addr=%p, "
1289 "value-a-type=%s, value-b-type=%s",
1291 bt_common_value_type_string(object_a
->type
),
1292 bt_common_value_type_string(object_b
->type
));
1296 ret
= compare_funcs
[object_a
->type
](object_a
, object_b
);
1302 void bt_value_get_ref(const struct bt_value
*value
)
1304 bt_object_get_ref(value
);
1307 void bt_value_put_ref(const struct bt_value
*value
)
1309 bt_object_put_ref(value
);