2 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
3 * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #define BT_LOG_TAG "VALUES"
25 #include <babeltrace/lib-logging-internal.h>
31 #include <babeltrace/compiler-internal.h>
32 #include <babeltrace/common-internal.h>
33 #include <babeltrace/object.h>
34 #include <babeltrace/values.h>
35 #include <babeltrace/private-values.h>
36 #include <babeltrace/compat/glib-internal.h>
37 #include <babeltrace/types.h>
38 #include <babeltrace/object-internal.h>
39 #include <babeltrace/values-internal.h>
40 #include <babeltrace/assert-internal.h>
41 #include <babeltrace/assert-pre-internal.h>
43 #define BT_VALUE_FROM_CONCRETE(_concrete) ((struct bt_value *) (_concrete))
44 #define BT_VALUE_TO_BOOL(_base) ((struct bt_value_bool *) (_base))
45 #define BT_VALUE_TO_INTEGER(_base) ((struct bt_value_integer *) (_base))
46 #define BT_VALUE_TO_REAL(_base) ((struct bt_value_real *) (_base))
47 #define BT_VALUE_TO_STRING(_base) ((struct bt_value_string *) (_base))
48 #define BT_VALUE_TO_ARRAY(_base) ((struct bt_value_array *) (_base))
49 #define BT_VALUE_TO_MAP(_base) ((struct bt_value_map *) (_base))
51 #define BT_ASSERT_PRE_VALUE_IS_TYPE(_value, _type) \
52 BT_ASSERT_PRE(((struct bt_value *) (_value))->type == (_type), \
53 "Value has the wrong type ID: expected-type=%s, " \
54 "%![value-]+v", bt_common_value_type_string(_type), \
57 #define BT_ASSERT_PRE_VALUE_HOT(_value, _name) \
58 BT_ASSERT_PRE_HOT(((struct bt_value *) (_value)), (_name), \
61 #define BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(_index, _count) \
62 BT_ASSERT_PRE((_index) < (_count), \
63 "Index is out of bound: " \
64 "index=%" PRIu64 ", count=%u", (_index), (_count));
67 struct bt_object base
;
68 enum bt_value_type type
;
73 void bt_value_null_instance_release_func(struct bt_object
*obj
)
75 BT_LOGW("Releasing the null value singleton: addr=%p", obj
);
79 struct bt_value bt_value_null_instance
= {
83 .release_func
= bt_value_null_instance_release_func
,
84 .spec_release_func
= NULL
,
85 .parent_is_owner_listener_func
= NULL
,
88 .type
= BT_VALUE_TYPE_NULL
,
92 struct bt_value
*bt_value_null
= &bt_value_null_instance
;
93 struct bt_private_value
*bt_private_value_null
=
94 (void *) &bt_value_null_instance
;
96 struct bt_value_bool
{
101 struct bt_value_integer
{
102 struct bt_value base
;
106 struct bt_value_real
{
107 struct bt_value base
;
111 struct bt_value_string
{
112 struct bt_value base
;
116 struct bt_value_array
{
117 struct bt_value base
;
121 struct bt_value_map
{
122 struct bt_value base
;
127 void bt_value_destroy(struct bt_object
*obj
);
130 void bt_value_string_destroy(struct bt_value
*object
)
132 g_string_free(BT_VALUE_TO_STRING(object
)->gstr
, TRUE
);
136 void bt_value_array_destroy(struct bt_value
*object
)
139 * Pointer array's registered value destructor will take care
140 * of putting each contained object.
142 g_ptr_array_free(BT_VALUE_TO_ARRAY(object
)->garray
, TRUE
);
146 void bt_value_map_destroy(struct bt_value
*object
)
149 * Hash table's registered value destructor will take care of
150 * putting each contained object. Keys are GQuarks and cannot
151 * be destroyed anyway.
153 g_hash_table_destroy(BT_VALUE_TO_MAP(object
)->ght
);
157 void (* const destroy_funcs
[])(struct bt_value
*) = {
158 [BT_VALUE_TYPE_NULL
] = NULL
,
159 [BT_VALUE_TYPE_BOOL
] = NULL
,
160 [BT_VALUE_TYPE_INTEGER
] = NULL
,
161 [BT_VALUE_TYPE_REAL
] = NULL
,
162 [BT_VALUE_TYPE_STRING
] = bt_value_string_destroy
,
163 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_destroy
,
164 [BT_VALUE_TYPE_MAP
] = bt_value_map_destroy
,
168 struct bt_private_value
*bt_value_null_copy(const struct bt_value
*null_obj
)
170 return (void *) bt_value_null
;
174 struct bt_private_value
*bt_value_bool_copy(const struct bt_value
*bool_obj
)
176 return bt_private_value_bool_create_init(
177 BT_VALUE_TO_BOOL(bool_obj
)->value
);
181 struct bt_private_value
*bt_value_integer_copy(
182 const struct bt_value
*integer_obj
)
184 return bt_private_value_integer_create_init(
185 BT_VALUE_TO_INTEGER(integer_obj
)->value
);
189 struct bt_private_value
*bt_value_real_copy(const struct bt_value
*real_obj
)
191 return bt_private_value_real_create_init(
192 BT_VALUE_TO_REAL(real_obj
)->value
);
196 struct bt_private_value
*bt_value_string_copy(const struct bt_value
*string_obj
)
198 return bt_private_value_string_create_init(
199 BT_VALUE_TO_STRING(string_obj
)->gstr
->str
);
203 struct bt_private_value
*bt_value_array_copy(const struct bt_value
*array_obj
)
207 struct bt_private_value
*copy_obj
;
208 struct bt_value_array
*typed_array_obj
;
210 BT_LOGD("Copying array value: addr=%p", array_obj
);
211 typed_array_obj
= BT_VALUE_TO_ARRAY(array_obj
);
212 copy_obj
= bt_private_value_array_create();
214 BT_LOGE_STR("Cannot create empty array value.");
218 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
219 struct bt_private_value
*element_obj_copy
= NULL
;
220 struct bt_value
*element_obj
=
221 bt_value_array_borrow_element_by_index(
224 BT_ASSERT(element_obj
);
225 BT_LOGD("Copying array value's element: element-addr=%p, "
226 "index=%d", element_obj
, i
);
227 ret
= bt_value_copy(&element_obj_copy
, element_obj
);
229 BT_LOGE("Cannot copy array value's element: "
230 "array-addr=%p, index=%d",
232 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
236 BT_ASSERT(element_obj_copy
);
237 ret
= bt_private_value_array_append_element(copy_obj
,
238 (void *) element_obj_copy
);
239 BT_OBJECT_PUT_REF_AND_RESET(element_obj_copy
);
241 BT_LOGE("Cannot append to array value: addr=%p",
243 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
248 BT_LOGD("Copied array value: original-addr=%p, copy-addr=%p",
249 array_obj
, copy_obj
);
256 struct bt_private_value
*bt_value_map_copy(const struct bt_value
*map_obj
)
260 gpointer key
, element_obj
;
261 struct bt_private_value
*copy_obj
;
262 struct bt_private_value
*element_obj_copy
= NULL
;
263 struct bt_value_map
*typed_map_obj
;
265 BT_LOGD("Copying map value: addr=%p", map_obj
);
266 typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
267 copy_obj
= bt_private_value_map_create();
272 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
274 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
275 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
278 BT_LOGD("Copying map value's element: element-addr=%p, "
279 "key=\"%s\"", element_obj
, key_str
);
280 ret
= bt_value_copy(&element_obj_copy
, element_obj
);
282 BT_LOGE("Cannot copy map value's element: "
283 "map-addr=%p, key=\"%s\"",
285 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
289 BT_ASSERT(element_obj_copy
);
290 ret
= bt_private_value_map_insert_entry(copy_obj
, key_str
,
291 (void *) element_obj_copy
);
292 BT_OBJECT_PUT_REF_AND_RESET(element_obj_copy
);
294 BT_LOGE("Cannot insert into map value: addr=%p, key=\"%s\"",
296 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
301 BT_LOGD("Copied map value: addr=%p", map_obj
);
308 struct bt_private_value
*(* const copy_funcs
[])(const struct bt_value
*) = {
309 [BT_VALUE_TYPE_NULL
] = bt_value_null_copy
,
310 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_copy
,
311 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_copy
,
312 [BT_VALUE_TYPE_REAL
] = bt_value_real_copy
,
313 [BT_VALUE_TYPE_STRING
] = bt_value_string_copy
,
314 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_copy
,
315 [BT_VALUE_TYPE_MAP
] = bt_value_map_copy
,
319 bt_bool
bt_value_null_compare(const struct bt_value
*object_a
,
320 const struct bt_value
*object_b
)
323 * Always BT_TRUE since bt_value_compare() already checks if both
324 * object_a and object_b have the same type, and in the case of
325 * null value objects, they're always the same if it is so.
331 bt_bool
bt_value_bool_compare(const struct bt_value
*object_a
,
332 const struct bt_value
*object_b
)
334 if (BT_VALUE_TO_BOOL(object_a
)->value
!=
335 BT_VALUE_TO_BOOL(object_b
)->value
) {
336 BT_LOGV("Boolean value objects are different: "
337 "bool-a-val=%d, bool-b-val=%d",
338 BT_VALUE_TO_BOOL(object_a
)->value
,
339 BT_VALUE_TO_BOOL(object_b
)->value
);
347 bt_bool
bt_value_integer_compare(const struct bt_value
*object_a
,
348 const struct bt_value
*object_b
)
350 if (BT_VALUE_TO_INTEGER(object_a
)->value
!=
351 BT_VALUE_TO_INTEGER(object_b
)->value
) {
352 BT_LOGV("Integer value objects are different: "
353 "int-a-val=%" PRId64
", int-b-val=%" PRId64
,
354 BT_VALUE_TO_INTEGER(object_a
)->value
,
355 BT_VALUE_TO_INTEGER(object_b
)->value
);
363 bt_bool
bt_value_real_compare(const struct bt_value
*object_a
,
364 const struct bt_value
*object_b
)
366 if (BT_VALUE_TO_REAL(object_a
)->value
!=
367 BT_VALUE_TO_REAL(object_b
)->value
) {
368 BT_LOGV("Real number value objects are different: "
369 "real-a-val=%f, real-b-val=%f",
370 BT_VALUE_TO_REAL(object_a
)->value
,
371 BT_VALUE_TO_REAL(object_b
)->value
);
379 bt_bool
bt_value_string_compare(const struct bt_value
*object_a
,
380 const struct bt_value
*object_b
)
382 if (strcmp(BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
383 BT_VALUE_TO_STRING(object_b
)->gstr
->str
) != 0) {
384 BT_LOGV("String value objects are different: "
385 "string-a-val=\"%s\", string-b-val=\"%s\"",
386 BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
387 BT_VALUE_TO_STRING(object_b
)->gstr
->str
);
395 bt_bool
bt_value_array_compare(const struct bt_value
*object_a
,
396 const struct bt_value
*object_b
)
399 bt_bool ret
= BT_TRUE
;
400 const struct bt_value_array
*array_obj_a
=
401 BT_VALUE_TO_ARRAY(object_a
);
403 if (bt_value_array_get_size(object_a
) !=
404 bt_value_array_get_size(object_b
)) {
405 BT_LOGV("Array values are different: size mismatch "
406 "value-a-addr=%p, value-b-addr=%p, "
407 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
409 bt_value_array_get_size(object_a
),
410 bt_value_array_get_size(object_b
));
415 for (i
= 0; i
< array_obj_a
->garray
->len
; ++i
) {
416 struct bt_value
*element_obj_a
;
417 struct bt_value
*element_obj_b
;
419 element_obj_a
= bt_value_array_borrow_element_by_index(
421 element_obj_b
= bt_value_array_borrow_element_by_index(
424 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
425 BT_LOGV("Array values's elements are different: "
426 "value-a-addr=%p, value-b-addr=%p, index=%d",
427 element_obj_a
, element_obj_b
, i
);
438 bt_bool
bt_value_map_compare(const struct bt_value
*object_a
,
439 const struct bt_value
*object_b
)
441 bt_bool ret
= BT_TRUE
;
443 gpointer key
, element_obj_a
;
444 const struct bt_value_map
*map_obj_a
= BT_VALUE_TO_MAP(object_a
);
446 if (bt_value_map_get_size(object_a
) !=
447 bt_value_map_get_size(object_b
)) {
448 BT_LOGV("Map values are different: size mismatch "
449 "value-a-addr=%p, value-b-addr=%p, "
450 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
452 bt_value_map_get_size(object_a
),
453 bt_value_map_get_size(object_b
));
458 g_hash_table_iter_init(&iter
, map_obj_a
->ght
);
460 while (g_hash_table_iter_next(&iter
, &key
, &element_obj_a
)) {
461 struct bt_value
*element_obj_b
;
462 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
464 element_obj_b
= bt_value_map_borrow_entry_value(object_b
,
467 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
468 BT_LOGV("Map values's elements are different: "
469 "value-a-addr=%p, value-b-addr=%p, key=\"%s\"",
470 element_obj_a
, element_obj_b
, key_str
);
481 bt_bool (* const compare_funcs
[])(const struct bt_value
*,
482 const struct bt_value
*) = {
483 [BT_VALUE_TYPE_NULL
] = bt_value_null_compare
,
484 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_compare
,
485 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_compare
,
486 [BT_VALUE_TYPE_REAL
] = bt_value_real_compare
,
487 [BT_VALUE_TYPE_STRING
] = bt_value_string_compare
,
488 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_compare
,
489 [BT_VALUE_TYPE_MAP
] = bt_value_map_compare
,
493 void bt_value_null_freeze(struct bt_value
*object
)
498 void bt_value_generic_freeze(struct bt_value
*object
)
500 object
->frozen
= BT_TRUE
;
504 void bt_value_array_freeze(struct bt_value
*object
)
507 struct bt_value_array
*typed_array_obj
=
508 BT_VALUE_TO_ARRAY(object
);
510 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
511 bt_value_freeze(g_ptr_array_index(typed_array_obj
->garray
, i
));
514 bt_value_generic_freeze(object
);
518 void bt_value_map_freeze(struct bt_value
*object
)
521 gpointer key
, element_obj
;
522 const struct bt_value_map
*map_obj
= BT_VALUE_TO_MAP(object
);
524 g_hash_table_iter_init(&iter
, map_obj
->ght
);
526 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
527 bt_value_freeze(element_obj
);
530 bt_value_generic_freeze(object
);
534 void (* const freeze_funcs
[])(struct bt_value
*) = {
535 [BT_VALUE_TYPE_NULL
] = bt_value_null_freeze
,
536 [BT_VALUE_TYPE_BOOL
] = bt_value_generic_freeze
,
537 [BT_VALUE_TYPE_INTEGER
] = bt_value_generic_freeze
,
538 [BT_VALUE_TYPE_REAL
] = bt_value_generic_freeze
,
539 [BT_VALUE_TYPE_STRING
] = bt_value_generic_freeze
,
540 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_freeze
,
541 [BT_VALUE_TYPE_MAP
] = bt_value_map_freeze
,
545 void bt_value_destroy(struct bt_object
*obj
)
547 struct bt_value
*value
;
549 value
= container_of(obj
, struct bt_value
, base
);
550 BT_LOGD("Destroying value: addr=%p", value
);
552 if (bt_value_is_null(value
)) {
553 BT_LOGD_STR("Not destroying the null value singleton.");
557 if (destroy_funcs
[value
->type
]) {
558 destroy_funcs
[value
->type
](value
);
565 enum bt_value_status
_bt_value_freeze(struct bt_value
*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
](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_private_value
*bt_private_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_private_value
*bt_private_value_bool_create(void)
620 return bt_private_value_bool_create_init(BT_FALSE
);
623 struct bt_private_value
*bt_private_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_private_value
*bt_private_value_integer_create(void)
645 return bt_private_value_integer_create_init(0);
648 struct bt_private_value
*bt_private_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_private_value
*bt_private_value_real_create(void)
670 return bt_private_value_real_create_init(0.);
673 struct bt_private_value
*bt_private_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_private_value
*bt_private_value_string_create(void)
707 return bt_private_value_string_create_init("");
710 struct bt_private_value
*bt_private_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_private_value
*bt_private_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_private_value_bool_set(struct bt_private_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_private_value_integer_set(struct bt_private_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_private_value_real_set(struct bt_private_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_private_value_string_set(
826 struct bt_private_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 const struct bt_value
*array_obj
,
848 struct bt_value_array
*typed_array_obj
=
849 BT_VALUE_TO_ARRAY(array_obj
);
851 BT_ASSERT_PRE_NON_NULL(array_obj
, "Value object");
852 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
853 BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index
,
854 typed_array_obj
->garray
->len
);
855 return g_ptr_array_index(typed_array_obj
->garray
, index
);
858 struct bt_private_value
*bt_private_value_array_borrow_element_by_index(
859 const struct bt_private_value
*array_obj
,
862 return (void *) bt_value_array_borrow_element_by_index(
863 (void *) array_obj
, index
);
866 enum bt_value_status
bt_private_value_array_append_element(
867 struct bt_private_value
*array_obj
,
868 struct bt_value
*element_obj
)
870 struct bt_value_array
*typed_array_obj
=
871 BT_VALUE_TO_ARRAY(array_obj
);
873 BT_ASSERT_PRE_NON_NULL(array_obj
, "Array value object");
874 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
875 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
876 BT_ASSERT_PRE_VALUE_HOT(array_obj
, "Array value object");
877 g_ptr_array_add(typed_array_obj
->garray
, element_obj
);
878 bt_object_get_ref(element_obj
);
879 BT_LOGV("Appended element to array value: array-value-addr=%p, "
880 "element-value-addr=%p, new-size=%u",
881 array_obj
, element_obj
, typed_array_obj
->garray
->len
);
882 return BT_VALUE_STATUS_OK
;
885 enum bt_value_status
bt_private_value_array_append_bool_element(
886 struct bt_private_value
*array_obj
, bt_bool val
)
888 enum bt_value_status ret
;
889 struct bt_private_value
*bool_obj
= NULL
;
891 bool_obj
= bt_private_value_bool_create_init(val
);
892 ret
= bt_private_value_array_append_element(array_obj
,
894 bt_object_put_ref(bool_obj
);
898 enum bt_value_status
bt_private_value_array_append_integer_element(
899 struct bt_private_value
*array_obj
, int64_t val
)
901 enum bt_value_status ret
;
902 struct bt_private_value
*integer_obj
= NULL
;
904 integer_obj
= bt_private_value_integer_create_init(val
);
905 ret
= bt_private_value_array_append_element(array_obj
,
906 (void *) integer_obj
);
907 bt_object_put_ref(integer_obj
);
911 enum bt_value_status
bt_private_value_array_append_real_element(
912 struct bt_private_value
*array_obj
, double val
)
914 enum bt_value_status ret
;
915 struct bt_private_value
*real_obj
= NULL
;
917 real_obj
= bt_private_value_real_create_init(val
);
918 ret
= bt_private_value_array_append_element(array_obj
,
920 bt_object_put_ref(real_obj
);
924 enum bt_value_status
bt_private_value_array_append_string_element(
925 struct bt_private_value
*array_obj
, const char *val
)
927 enum bt_value_status ret
;
928 struct bt_private_value
*string_obj
= NULL
;
930 string_obj
= bt_private_value_string_create_init(val
);
931 ret
= bt_private_value_array_append_element(array_obj
,
932 (void *) string_obj
);
933 bt_object_put_ref(string_obj
);
937 enum bt_value_status
bt_private_value_array_append_empty_array_element(
938 struct bt_private_value
*array_obj
)
940 enum bt_value_status ret
;
941 struct bt_private_value
*empty_array_obj
= NULL
;
943 empty_array_obj
= bt_private_value_array_create();
944 ret
= bt_private_value_array_append_element(array_obj
,
945 (void *) empty_array_obj
);
946 bt_object_put_ref(empty_array_obj
);
950 enum bt_value_status
bt_private_value_array_append_empty_map_element(
951 struct bt_private_value
*array_obj
)
953 enum bt_value_status ret
;
954 struct bt_private_value
*map_obj
= NULL
;
956 map_obj
= bt_private_value_map_create();
957 ret
= bt_private_value_array_append_element(array_obj
,
959 bt_object_put_ref(map_obj
);
963 enum bt_value_status
bt_private_value_array_set_element_by_index(
964 struct bt_private_value
*array_obj
, uint64_t index
,
965 struct bt_value
*element_obj
)
967 struct bt_value_array
*typed_array_obj
=
968 BT_VALUE_TO_ARRAY(array_obj
);
970 BT_ASSERT_PRE_NON_NULL(array_obj
, "Array value object");
971 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
972 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
973 BT_ASSERT_PRE_VALUE_HOT(array_obj
, "Array value object");
974 BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index
,
975 typed_array_obj
->garray
->len
);
976 bt_object_put_ref(g_ptr_array_index(typed_array_obj
->garray
, index
));
977 g_ptr_array_index(typed_array_obj
->garray
, index
) = element_obj
;
978 bt_object_get_ref(element_obj
);
979 BT_LOGV("Set array value's element: array-value-addr=%p, "
980 "index=%" PRIu64
", element-value-addr=%p",
981 array_obj
, index
, element_obj
);
982 return BT_VALUE_STATUS_OK
;
985 uint64_t bt_value_map_get_size(const struct bt_value
*map_obj
)
987 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
988 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
989 return (uint64_t) g_hash_table_size(BT_VALUE_TO_MAP(map_obj
)->ght
);
992 struct bt_value
*bt_value_map_borrow_entry_value(const struct bt_value
*map_obj
,
995 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
996 BT_ASSERT_PRE_NON_NULL(key
, "Key");
997 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
998 return g_hash_table_lookup(BT_VALUE_TO_MAP(map_obj
)->ght
,
999 GUINT_TO_POINTER(g_quark_from_string(key
)));
1002 struct bt_private_value
*bt_private_value_map_borrow_entry_value(
1003 const struct bt_private_value
*map_obj
, const char *key
)
1005 return (void *) bt_value_map_borrow_entry_value((void *) map_obj
, key
);
1008 bt_bool
bt_value_map_has_entry(const struct bt_value
*map_obj
, const char *key
)
1010 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
1011 BT_ASSERT_PRE_NON_NULL(key
, "Key");
1012 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1013 return bt_g_hash_table_contains(BT_VALUE_TO_MAP(map_obj
)->ght
,
1014 GUINT_TO_POINTER(g_quark_from_string(key
)));
1017 enum bt_value_status
bt_private_value_map_insert_entry(
1018 struct bt_private_value
*map_obj
,
1019 const char *key
, struct bt_value
*element_obj
)
1021 BT_ASSERT_PRE_NON_NULL(map_obj
, "Map value object");
1022 BT_ASSERT_PRE_NON_NULL(key
, "Key");
1023 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
1024 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1025 BT_ASSERT_PRE_VALUE_HOT(map_obj
, "Map value object");
1026 g_hash_table_insert(BT_VALUE_TO_MAP(map_obj
)->ght
,
1027 GUINT_TO_POINTER(g_quark_from_string(key
)), element_obj
);
1028 bt_object_get_ref(element_obj
);
1029 BT_LOGV("Inserted value into map value: map-value-addr=%p, "
1030 "key=\"%s\", element-value-addr=%p",
1031 map_obj
, key
, element_obj
);
1032 return BT_VALUE_STATUS_OK
;
1035 enum bt_value_status
bt_private_value_map_insert_bool_entry(
1036 struct bt_private_value
*map_obj
, const char *key
, bt_bool val
)
1038 enum bt_value_status ret
;
1039 struct bt_private_value
*bool_obj
= NULL
;
1041 bool_obj
= bt_private_value_bool_create_init(val
);
1042 ret
= bt_private_value_map_insert_entry(map_obj
, key
,
1044 bt_object_put_ref(bool_obj
);
1048 enum bt_value_status
bt_private_value_map_insert_integer_entry(
1049 struct bt_private_value
*map_obj
, const char *key
, int64_t val
)
1051 enum bt_value_status ret
;
1052 struct bt_private_value
*integer_obj
= NULL
;
1054 integer_obj
= bt_private_value_integer_create_init(val
);
1055 ret
= bt_private_value_map_insert_entry(map_obj
, key
,
1056 (void *) integer_obj
);
1057 bt_object_put_ref(integer_obj
);
1061 enum bt_value_status
bt_private_value_map_insert_real_entry(
1062 struct bt_private_value
*map_obj
, const char *key
, double val
)
1064 enum bt_value_status ret
;
1065 struct bt_private_value
*real_obj
= NULL
;
1067 real_obj
= bt_private_value_real_create_init(val
);
1068 ret
= bt_private_value_map_insert_entry(map_obj
, key
,
1070 bt_object_put_ref(real_obj
);
1074 enum bt_value_status
bt_private_value_map_insert_string_entry(
1075 struct bt_private_value
*map_obj
, const char *key
,
1078 enum bt_value_status ret
;
1079 struct bt_private_value
*string_obj
= NULL
;
1081 string_obj
= bt_private_value_string_create_init(val
);
1082 ret
= bt_private_value_map_insert_entry(map_obj
, key
,
1083 (void *) string_obj
);
1084 bt_object_put_ref(string_obj
);
1088 enum bt_value_status
bt_private_value_map_insert_empty_array_entry(
1089 struct bt_private_value
*map_obj
, const char *key
)
1091 enum bt_value_status ret
;
1092 struct bt_private_value
*array_obj
= NULL
;
1094 array_obj
= bt_private_value_array_create();
1095 ret
= bt_private_value_map_insert_entry(map_obj
, key
,
1096 (void *) array_obj
);
1097 bt_object_put_ref(array_obj
);
1101 enum bt_value_status
bt_private_value_map_insert_empty_map_entry(
1102 struct bt_private_value
*map_obj
, const char *key
)
1104 enum bt_value_status ret
;
1105 struct bt_private_value
*empty_map_obj
= NULL
;
1107 empty_map_obj
= bt_private_value_map_create();
1108 ret
= bt_private_value_map_insert_entry(map_obj
, key
,
1109 (void *) empty_map_obj
);
1110 bt_object_put_ref(empty_map_obj
);
1114 enum bt_value_status
bt_value_map_foreach_entry(const struct bt_value
*map_obj
,
1115 bt_value_map_foreach_entry_cb cb
, void *data
)
1117 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1118 gpointer key
, element_obj
;
1119 GHashTableIter iter
;
1120 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1122 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
1123 BT_ASSERT_PRE_NON_NULL(cb
, "Callback");
1124 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1125 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
1127 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
1128 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
1130 if (!cb(key_str
, element_obj
, data
)) {
1131 BT_LOGV("User canceled the loop: key=\"%s\", "
1132 "value-addr=%p, data=%p",
1133 key_str
, element_obj
, data
);
1134 ret
= BT_VALUE_STATUS_CANCELED
;
1142 enum bt_value_status
bt_private_value_map_foreach_entry(
1143 const struct bt_private_value
*map_obj
,
1144 bt_private_value_map_foreach_entry_cb cb
, void *data
)
1146 return bt_value_map_foreach_entry((void *) map_obj
,
1147 (bt_value_map_foreach_entry_cb
) cb
, data
);
1150 struct extend_map_element_data
{
1151 struct bt_private_value
*extended_obj
;
1152 enum bt_value_status status
;
1156 bt_bool
extend_map_element(const char *key
,
1157 struct bt_value
*extension_obj_elem
, void *data
)
1159 bt_bool ret
= BT_TRUE
;
1160 struct extend_map_element_data
*extend_data
= data
;
1161 struct bt_private_value
*extension_obj_elem_copy
= NULL
;
1163 /* Copy object which is to replace the current one */
1164 extend_data
->status
= bt_value_copy(&extension_obj_elem_copy
,
1165 extension_obj_elem
);
1166 if (extend_data
->status
) {
1167 BT_LOGE("Cannot copy map element: addr=%p",
1168 extension_obj_elem
);
1172 BT_ASSERT(extension_obj_elem_copy
);
1174 /* Replace in extended object */
1175 extend_data
->status
= bt_private_value_map_insert_entry(
1176 extend_data
->extended_obj
, key
,
1177 (void *) extension_obj_elem_copy
);
1178 if (extend_data
->status
) {
1179 BT_LOGE("Cannot replace value in extended value: key=\"%s\", "
1180 "extended-value-addr=%p, element-value-addr=%p",
1181 key
, extend_data
->extended_obj
,
1182 extension_obj_elem_copy
);
1189 BT_ASSERT(extend_data
->status
!= BT_VALUE_STATUS_OK
);
1193 BT_OBJECT_PUT_REF_AND_RESET(extension_obj_elem_copy
);
1197 enum bt_value_status
bt_value_map_extend(
1198 struct bt_private_value
**extended_map_obj
,
1199 const struct bt_value
*base_map_obj
,
1200 const struct bt_value
*extension_obj
)
1202 struct extend_map_element_data extend_data
= {
1203 .extended_obj
= NULL
,
1204 .status
= BT_VALUE_STATUS_OK
,
1207 BT_ASSERT_PRE_NON_NULL(base_map_obj
, "Base value object");
1208 BT_ASSERT_PRE_NON_NULL(extension_obj
, "Extension value object");
1209 BT_ASSERT_PRE_NON_NULL(extended_map_obj
,
1210 "Extended value object (output)");
1211 BT_ASSERT_PRE_VALUE_IS_TYPE(base_map_obj
, BT_VALUE_TYPE_MAP
);
1212 BT_ASSERT_PRE_VALUE_IS_TYPE(extension_obj
, BT_VALUE_TYPE_MAP
);
1213 BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p",
1214 base_map_obj
, extension_obj
);
1215 *extended_map_obj
= NULL
;
1217 /* Create copy of base map object to start with */
1218 extend_data
.status
= bt_value_copy(extended_map_obj
, base_map_obj
);
1219 if (extend_data
.status
) {
1220 BT_LOGE("Cannot copy base value: base-value-addr=%p",
1225 BT_ASSERT(extended_map_obj
);
1228 * For each key in the extension map object, replace this key
1229 * in the copied map object.
1231 extend_data
.extended_obj
= *extended_map_obj
;
1233 if (bt_value_map_foreach_entry(extension_obj
, extend_map_element
,
1235 BT_LOGE("Cannot iterate on the extension object's elements: "
1236 "extension-value-addr=%p", extension_obj
);
1240 if (extend_data
.status
) {
1241 BT_LOGE("Failed to successfully iterate on the extension object's elements: "
1242 "extension-value-addr=%p", extension_obj
);
1246 BT_LOGD("Extended map value: extended-value-addr=%p",
1251 BT_OBJECT_PUT_REF_AND_RESET(*extended_map_obj
);
1252 *extended_map_obj
= NULL
;
1255 return extend_data
.status
;
1258 enum bt_value_status
bt_value_copy(struct bt_private_value
**copy_obj
,
1259 const struct bt_value
*object
)
1261 enum bt_value_status status
= BT_VALUE_STATUS_OK
;
1263 BT_ASSERT_PRE_NON_NULL(object
, "Value object");
1264 BT_ASSERT_PRE_NON_NULL(copy_obj
, "Value object copy (output)");
1265 BT_LOGD("Copying value object: addr=%p", object
);
1266 *copy_obj
= copy_funcs
[object
->type
](object
);
1268 BT_LOGD("Copied value object: copy-value-addr=%p",
1271 status
= BT_VALUE_STATUS_NOMEM
;
1273 BT_LOGE_STR("Failed to copy value object.");
1279 bt_bool
bt_value_compare(const struct bt_value
*object_a
,
1280 const struct bt_value
*object_b
)
1282 bt_bool ret
= BT_FALSE
;
1284 BT_ASSERT_PRE_NON_NULL(object_a
, "Value object A");
1285 BT_ASSERT_PRE_NON_NULL(object_b
, "Value object B");
1287 if (object_a
->type
!= object_b
->type
) {
1288 BT_LOGV("Values are different: type mismatch: "
1289 "value-a-addr=%p, value-b-addr=%p, "
1290 "value-a-type=%s, value-b-type=%s",
1292 bt_common_value_type_string(object_a
->type
),
1293 bt_common_value_type_string(object_b
->type
));
1297 ret
= compare_funcs
[object_a
->type
](object_a
, object_b
);