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
);
133 BT_VALUE_TO_STRING(object
)->gstr
= NULL
;
137 void bt_value_array_destroy(struct bt_value
*object
)
140 * Pointer array's registered value destructor will take care
141 * of putting each contained object.
143 g_ptr_array_free(BT_VALUE_TO_ARRAY(object
)->garray
, TRUE
);
144 BT_VALUE_TO_ARRAY(object
)->garray
= NULL
;
148 void bt_value_map_destroy(struct bt_value
*object
)
151 * Hash table's registered value destructor will take care of
152 * putting each contained object. Keys are GQuarks and cannot
153 * be destroyed anyway.
155 g_hash_table_destroy(BT_VALUE_TO_MAP(object
)->ght
);
156 BT_VALUE_TO_MAP(object
)->ght
= NULL
;
160 void (* const destroy_funcs
[])(struct bt_value
*) = {
161 [BT_VALUE_TYPE_NULL
] = NULL
,
162 [BT_VALUE_TYPE_BOOL
] = NULL
,
163 [BT_VALUE_TYPE_INTEGER
] = NULL
,
164 [BT_VALUE_TYPE_REAL
] = NULL
,
165 [BT_VALUE_TYPE_STRING
] = bt_value_string_destroy
,
166 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_destroy
,
167 [BT_VALUE_TYPE_MAP
] = bt_value_map_destroy
,
171 struct bt_private_value
*bt_value_null_copy(const struct bt_value
*null_obj
)
173 return (void *) bt_value_null
;
177 struct bt_private_value
*bt_value_bool_copy(const struct bt_value
*bool_obj
)
179 return bt_private_value_bool_create_init(
180 BT_VALUE_TO_BOOL(bool_obj
)->value
);
184 struct bt_private_value
*bt_value_integer_copy(
185 const struct bt_value
*integer_obj
)
187 return bt_private_value_integer_create_init(
188 BT_VALUE_TO_INTEGER(integer_obj
)->value
);
192 struct bt_private_value
*bt_value_real_copy(const struct bt_value
*real_obj
)
194 return bt_private_value_real_create_init(
195 BT_VALUE_TO_REAL(real_obj
)->value
);
199 struct bt_private_value
*bt_value_string_copy(const struct bt_value
*string_obj
)
201 return bt_private_value_string_create_init(
202 BT_VALUE_TO_STRING(string_obj
)->gstr
->str
);
206 struct bt_private_value
*bt_value_array_copy(const struct bt_value
*array_obj
)
210 struct bt_private_value
*copy_obj
;
211 struct bt_value_array
*typed_array_obj
;
213 BT_LOGD("Copying array value: addr=%p", array_obj
);
214 typed_array_obj
= BT_VALUE_TO_ARRAY(array_obj
);
215 copy_obj
= bt_private_value_array_create();
217 BT_LOGE_STR("Cannot create empty array value.");
221 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
222 struct bt_private_value
*element_obj_copy
= NULL
;
223 struct bt_value
*element_obj
=
224 bt_value_array_borrow_element_by_index(
227 BT_ASSERT(element_obj
);
228 BT_LOGD("Copying array value's element: element-addr=%p, "
229 "index=%d", element_obj
, i
);
230 ret
= bt_value_copy(&element_obj_copy
, element_obj
);
232 BT_LOGE("Cannot copy array value's element: "
233 "array-addr=%p, index=%d",
235 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
239 BT_ASSERT(element_obj_copy
);
240 ret
= bt_private_value_array_append_element(copy_obj
,
241 (void *) element_obj_copy
);
242 BT_OBJECT_PUT_REF_AND_RESET(element_obj_copy
);
244 BT_LOGE("Cannot append to array value: addr=%p",
246 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
251 BT_LOGD("Copied array value: original-addr=%p, copy-addr=%p",
252 array_obj
, copy_obj
);
259 struct bt_private_value
*bt_value_map_copy(const struct bt_value
*map_obj
)
263 gpointer key
, element_obj
;
264 struct bt_private_value
*copy_obj
;
265 struct bt_private_value
*element_obj_copy
= NULL
;
266 struct bt_value_map
*typed_map_obj
;
268 BT_LOGD("Copying map value: addr=%p", map_obj
);
269 typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
270 copy_obj
= bt_private_value_map_create();
275 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
277 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
278 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
281 BT_LOGD("Copying map value's element: element-addr=%p, "
282 "key=\"%s\"", element_obj
, key_str
);
283 ret
= bt_value_copy(&element_obj_copy
, element_obj
);
285 BT_LOGE("Cannot copy map value's element: "
286 "map-addr=%p, key=\"%s\"",
288 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
292 BT_ASSERT(element_obj_copy
);
293 ret
= bt_private_value_map_insert_entry(copy_obj
, key_str
,
294 (void *) element_obj_copy
);
295 BT_OBJECT_PUT_REF_AND_RESET(element_obj_copy
);
297 BT_LOGE("Cannot insert into map value: addr=%p, key=\"%s\"",
299 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
304 BT_LOGD("Copied map value: addr=%p", map_obj
);
311 struct bt_private_value
*(* const copy_funcs
[])(const struct bt_value
*) = {
312 [BT_VALUE_TYPE_NULL
] = bt_value_null_copy
,
313 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_copy
,
314 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_copy
,
315 [BT_VALUE_TYPE_REAL
] = bt_value_real_copy
,
316 [BT_VALUE_TYPE_STRING
] = bt_value_string_copy
,
317 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_copy
,
318 [BT_VALUE_TYPE_MAP
] = bt_value_map_copy
,
322 bt_bool
bt_value_null_compare(const struct bt_value
*object_a
,
323 const struct bt_value
*object_b
)
326 * Always BT_TRUE since bt_value_compare() already checks if both
327 * object_a and object_b have the same type, and in the case of
328 * null value objects, they're always the same if it is so.
334 bt_bool
bt_value_bool_compare(const struct bt_value
*object_a
,
335 const struct bt_value
*object_b
)
337 if (BT_VALUE_TO_BOOL(object_a
)->value
!=
338 BT_VALUE_TO_BOOL(object_b
)->value
) {
339 BT_LOGV("Boolean value objects are different: "
340 "bool-a-val=%d, bool-b-val=%d",
341 BT_VALUE_TO_BOOL(object_a
)->value
,
342 BT_VALUE_TO_BOOL(object_b
)->value
);
350 bt_bool
bt_value_integer_compare(const struct bt_value
*object_a
,
351 const struct bt_value
*object_b
)
353 if (BT_VALUE_TO_INTEGER(object_a
)->value
!=
354 BT_VALUE_TO_INTEGER(object_b
)->value
) {
355 BT_LOGV("Integer value objects are different: "
356 "int-a-val=%" PRId64
", int-b-val=%" PRId64
,
357 BT_VALUE_TO_INTEGER(object_a
)->value
,
358 BT_VALUE_TO_INTEGER(object_b
)->value
);
366 bt_bool
bt_value_real_compare(const struct bt_value
*object_a
,
367 const struct bt_value
*object_b
)
369 if (BT_VALUE_TO_REAL(object_a
)->value
!=
370 BT_VALUE_TO_REAL(object_b
)->value
) {
371 BT_LOGV("Real number value objects are different: "
372 "real-a-val=%f, real-b-val=%f",
373 BT_VALUE_TO_REAL(object_a
)->value
,
374 BT_VALUE_TO_REAL(object_b
)->value
);
382 bt_bool
bt_value_string_compare(const struct bt_value
*object_a
,
383 const struct bt_value
*object_b
)
385 if (strcmp(BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
386 BT_VALUE_TO_STRING(object_b
)->gstr
->str
) != 0) {
387 BT_LOGV("String value objects are different: "
388 "string-a-val=\"%s\", string-b-val=\"%s\"",
389 BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
390 BT_VALUE_TO_STRING(object_b
)->gstr
->str
);
398 bt_bool
bt_value_array_compare(const struct bt_value
*object_a
,
399 const struct bt_value
*object_b
)
402 bt_bool ret
= BT_TRUE
;
403 const struct bt_value_array
*array_obj_a
=
404 BT_VALUE_TO_ARRAY(object_a
);
406 if (bt_value_array_get_size(object_a
) !=
407 bt_value_array_get_size(object_b
)) {
408 BT_LOGV("Array values are different: size mismatch "
409 "value-a-addr=%p, value-b-addr=%p, "
410 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
412 bt_value_array_get_size(object_a
),
413 bt_value_array_get_size(object_b
));
418 for (i
= 0; i
< array_obj_a
->garray
->len
; ++i
) {
419 struct bt_value
*element_obj_a
;
420 struct bt_value
*element_obj_b
;
422 element_obj_a
= bt_value_array_borrow_element_by_index(
424 element_obj_b
= bt_value_array_borrow_element_by_index(
427 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
428 BT_LOGV("Array values's elements are different: "
429 "value-a-addr=%p, value-b-addr=%p, index=%d",
430 element_obj_a
, element_obj_b
, i
);
441 bt_bool
bt_value_map_compare(const struct bt_value
*object_a
,
442 const struct bt_value
*object_b
)
444 bt_bool ret
= BT_TRUE
;
446 gpointer key
, element_obj_a
;
447 const struct bt_value_map
*map_obj_a
= BT_VALUE_TO_MAP(object_a
);
449 if (bt_value_map_get_size(object_a
) !=
450 bt_value_map_get_size(object_b
)) {
451 BT_LOGV("Map values are different: size mismatch "
452 "value-a-addr=%p, value-b-addr=%p, "
453 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
455 bt_value_map_get_size(object_a
),
456 bt_value_map_get_size(object_b
));
461 g_hash_table_iter_init(&iter
, map_obj_a
->ght
);
463 while (g_hash_table_iter_next(&iter
, &key
, &element_obj_a
)) {
464 struct bt_value
*element_obj_b
;
465 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
467 element_obj_b
= bt_value_map_borrow_entry_value(object_b
,
470 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
471 BT_LOGV("Map values's elements are different: "
472 "value-a-addr=%p, value-b-addr=%p, key=\"%s\"",
473 element_obj_a
, element_obj_b
, key_str
);
484 bt_bool (* const compare_funcs
[])(const struct bt_value
*,
485 const struct bt_value
*) = {
486 [BT_VALUE_TYPE_NULL
] = bt_value_null_compare
,
487 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_compare
,
488 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_compare
,
489 [BT_VALUE_TYPE_REAL
] = bt_value_real_compare
,
490 [BT_VALUE_TYPE_STRING
] = bt_value_string_compare
,
491 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_compare
,
492 [BT_VALUE_TYPE_MAP
] = bt_value_map_compare
,
496 void bt_value_null_freeze(struct bt_value
*object
)
501 void bt_value_generic_freeze(struct bt_value
*object
)
503 object
->frozen
= BT_TRUE
;
507 void bt_value_array_freeze(struct bt_value
*object
)
510 struct bt_value_array
*typed_array_obj
=
511 BT_VALUE_TO_ARRAY(object
);
513 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
514 bt_value_freeze(g_ptr_array_index(typed_array_obj
->garray
, i
));
517 bt_value_generic_freeze(object
);
521 void bt_value_map_freeze(struct bt_value
*object
)
524 gpointer key
, element_obj
;
525 const struct bt_value_map
*map_obj
= BT_VALUE_TO_MAP(object
);
527 g_hash_table_iter_init(&iter
, map_obj
->ght
);
529 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
530 bt_value_freeze(element_obj
);
533 bt_value_generic_freeze(object
);
537 void (* const freeze_funcs
[])(struct bt_value
*) = {
538 [BT_VALUE_TYPE_NULL
] = bt_value_null_freeze
,
539 [BT_VALUE_TYPE_BOOL
] = bt_value_generic_freeze
,
540 [BT_VALUE_TYPE_INTEGER
] = bt_value_generic_freeze
,
541 [BT_VALUE_TYPE_REAL
] = bt_value_generic_freeze
,
542 [BT_VALUE_TYPE_STRING
] = bt_value_generic_freeze
,
543 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_freeze
,
544 [BT_VALUE_TYPE_MAP
] = bt_value_map_freeze
,
548 void bt_value_destroy(struct bt_object
*obj
)
550 struct bt_value
*value
;
552 value
= container_of(obj
, struct bt_value
, base
);
553 BT_LOGD("Destroying value: addr=%p", value
);
555 if (bt_value_is_null(value
)) {
556 BT_LOGD_STR("Not destroying the null value singleton.");
560 if (destroy_funcs
[value
->type
]) {
561 destroy_funcs
[value
->type
](value
);
568 enum bt_value_status
_bt_value_freeze(struct bt_value
*object
)
570 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
574 if (object
->frozen
) {
578 BT_LOGD("Freezing value: addr=%p", object
);
579 freeze_funcs
[object
->type
](object
);
585 enum bt_value_type
bt_value_get_type(const struct bt_value
*object
)
587 BT_ASSERT_PRE_NON_NULL(object
, "Value object");
592 struct bt_value
bt_value_create_base(enum bt_value_type type
)
594 struct bt_value value
;
597 value
.frozen
= BT_FALSE
;
598 bt_object_init_shared(&value
.base
, bt_value_destroy
);
602 struct bt_private_value
*bt_private_value_bool_create_init(bt_bool val
)
604 struct bt_value_bool
*bool_obj
;
606 BT_LOGD("Creating boolean value object: val=%d", val
);
607 bool_obj
= g_new0(struct bt_value_bool
, 1);
609 BT_LOGE_STR("Failed to allocate one boolean value object.");
613 bool_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_BOOL
);
614 bool_obj
->value
= val
;
615 BT_LOGD("Created boolean value object: addr=%p", bool_obj
);
618 return (void *) BT_VALUE_FROM_CONCRETE(bool_obj
);
621 struct bt_private_value
*bt_private_value_bool_create(void)
623 return bt_private_value_bool_create_init(BT_FALSE
);
626 struct bt_private_value
*bt_private_value_integer_create_init(int64_t val
)
628 struct bt_value_integer
*integer_obj
;
630 BT_LOGD("Creating integer value object: val=%" PRId64
, val
);
631 integer_obj
= g_new0(struct bt_value_integer
, 1);
633 BT_LOGE_STR("Failed to allocate one integer value object.");
637 integer_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_INTEGER
);
638 integer_obj
->value
= val
;
639 BT_LOGD("Created integer value object: addr=%p",
643 return (void *) BT_VALUE_FROM_CONCRETE(integer_obj
);
646 struct bt_private_value
*bt_private_value_integer_create(void)
648 return bt_private_value_integer_create_init(0);
651 struct bt_private_value
*bt_private_value_real_create_init(double val
)
653 struct bt_value_real
*real_obj
;
655 BT_LOGD("Creating real number value object: val=%f", val
);
656 real_obj
= g_new0(struct bt_value_real
, 1);
658 BT_LOGE_STR("Failed to allocate one real number value object.");
662 real_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_REAL
);
663 real_obj
->value
= val
;
664 BT_LOGD("Created real number value object: addr=%p",
668 return (void *) BT_VALUE_FROM_CONCRETE(real_obj
);
671 struct bt_private_value
*bt_private_value_real_create(void)
673 return bt_private_value_real_create_init(0.);
676 struct bt_private_value
*bt_private_value_string_create_init(const char *val
)
678 struct bt_value_string
*string_obj
= NULL
;
681 BT_LOGW_STR("Invalid parameter: value is NULL.");
685 BT_LOGD("Creating string value object: val-len=%zu", strlen(val
));
686 string_obj
= g_new0(struct bt_value_string
, 1);
688 BT_LOGE_STR("Failed to allocate one string object.");
692 string_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_STRING
);
693 string_obj
->gstr
= g_string_new(val
);
694 if (!string_obj
->gstr
) {
695 BT_LOGE_STR("Failed to allocate a GString.");
701 BT_LOGD("Created string value object: addr=%p",
705 return (void *) BT_VALUE_FROM_CONCRETE(string_obj
);
708 struct bt_private_value
*bt_private_value_string_create(void)
710 return bt_private_value_string_create_init("");
713 struct bt_private_value
*bt_private_value_array_create(void)
715 struct bt_value_array
*array_obj
;
717 BT_LOGD_STR("Creating empty array value object.");
718 array_obj
= g_new0(struct bt_value_array
, 1);
720 BT_LOGE_STR("Failed to allocate one array object.");
724 array_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_ARRAY
);
725 array_obj
->garray
= bt_g_ptr_array_new_full(0,
726 (GDestroyNotify
) bt_object_put_ref
);
727 if (!array_obj
->garray
) {
728 BT_LOGE_STR("Failed to allocate a GPtrArray.");
734 BT_LOGD("Created array value object: addr=%p",
738 return (void *) BT_VALUE_FROM_CONCRETE(array_obj
);
741 struct bt_private_value
*bt_private_value_map_create(void)
743 struct bt_value_map
*map_obj
;
745 BT_LOGD_STR("Creating empty map value object.");
746 map_obj
= g_new0(struct bt_value_map
, 1);
748 BT_LOGE_STR("Failed to allocate one map object.");
752 map_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_MAP
);
753 map_obj
->ght
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
754 NULL
, (GDestroyNotify
) bt_object_put_ref
);
756 BT_LOGE_STR("Failed to allocate a GHashTable.");
762 BT_LOGD("Created map value object: addr=%p",
766 return (void *) BT_VALUE_FROM_CONCRETE(map_obj
);
769 bt_bool
bt_value_bool_get(const struct bt_value
*bool_obj
)
771 BT_ASSERT_PRE_NON_NULL(bool_obj
, "Value object");
772 BT_ASSERT_PRE_VALUE_IS_TYPE(bool_obj
, BT_VALUE_TYPE_BOOL
);
773 return BT_VALUE_TO_BOOL(bool_obj
)->value
;
776 void bt_private_value_bool_set(struct bt_private_value
*bool_obj
, bt_bool val
)
778 BT_ASSERT_PRE_NON_NULL(bool_obj
, "Value object");
779 BT_ASSERT_PRE_VALUE_IS_TYPE(bool_obj
, BT_VALUE_TYPE_BOOL
);
780 BT_ASSERT_PRE_VALUE_HOT(bool_obj
, "Value object");
781 BT_VALUE_TO_BOOL(bool_obj
)->value
= val
;
782 BT_LOGV("Set boolean value's raw value: value-addr=%p, value=%d",
786 int64_t bt_value_integer_get(const struct bt_value
*integer_obj
)
788 BT_ASSERT_PRE_NON_NULL(integer_obj
, "Value object");
789 BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj
, BT_VALUE_TYPE_INTEGER
);
790 return BT_VALUE_TO_INTEGER(integer_obj
)->value
;
793 void bt_private_value_integer_set(struct bt_private_value
*integer_obj
,
796 BT_ASSERT_PRE_NON_NULL(integer_obj
, "Value object");
797 BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj
, BT_VALUE_TYPE_INTEGER
);
798 BT_ASSERT_PRE_VALUE_HOT(integer_obj
, "Value object");
799 BT_VALUE_TO_INTEGER(integer_obj
)->value
= val
;
800 BT_LOGV("Set integer value's raw value: value-addr=%p, value=%" PRId64
,
804 double bt_value_real_get(const struct bt_value
*real_obj
)
806 BT_ASSERT_PRE_NON_NULL(real_obj
, "Value object");
807 BT_ASSERT_PRE_VALUE_IS_TYPE(real_obj
, BT_VALUE_TYPE_REAL
);
808 return BT_VALUE_TO_REAL(real_obj
)->value
;
811 void bt_private_value_real_set(struct bt_private_value
*real_obj
, double val
)
813 BT_ASSERT_PRE_NON_NULL(real_obj
, "Value object");
814 BT_ASSERT_PRE_VALUE_IS_TYPE(real_obj
, BT_VALUE_TYPE_REAL
);
815 BT_ASSERT_PRE_VALUE_HOT(real_obj
, "Value object");
816 BT_VALUE_TO_REAL(real_obj
)->value
= val
;
817 BT_LOGV("Set real number value's raw value: value-addr=%p, value=%f",
821 const char *bt_value_string_get(const struct bt_value
*string_obj
)
823 BT_ASSERT_PRE_NON_NULL(string_obj
, "Value object");
824 BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj
, BT_VALUE_TYPE_STRING
);
825 return BT_VALUE_TO_STRING(string_obj
)->gstr
->str
;
828 enum bt_value_status
bt_private_value_string_set(
829 struct bt_private_value
*string_obj
, const char *val
)
831 BT_ASSERT_PRE_NON_NULL(string_obj
, "Value object");
832 BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj
, BT_VALUE_TYPE_STRING
);
833 BT_ASSERT_PRE_VALUE_HOT(string_obj
, "Value object");
834 g_string_assign(BT_VALUE_TO_STRING(string_obj
)->gstr
, val
);
835 BT_LOGV("Set string value's raw value: value-addr=%p, raw-value-addr=%p",
837 return BT_VALUE_STATUS_OK
;
840 uint64_t bt_value_array_get_size(const struct bt_value
*array_obj
)
842 BT_ASSERT_PRE_NON_NULL(array_obj
, "Value object");
843 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
844 return (uint64_t) BT_VALUE_TO_ARRAY(array_obj
)->garray
->len
;
847 struct bt_value
*bt_value_array_borrow_element_by_index(
848 const struct bt_value
*array_obj
,
851 struct bt_value_array
*typed_array_obj
=
852 BT_VALUE_TO_ARRAY(array_obj
);
854 BT_ASSERT_PRE_NON_NULL(array_obj
, "Value object");
855 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
856 BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index
,
857 typed_array_obj
->garray
->len
);
858 return g_ptr_array_index(typed_array_obj
->garray
, index
);
861 struct bt_private_value
*bt_private_value_array_borrow_element_by_index(
862 const struct bt_private_value
*array_obj
,
865 return (void *) bt_value_array_borrow_element_by_index(
866 (void *) array_obj
, index
);
869 enum bt_value_status
bt_private_value_array_append_element(
870 struct bt_private_value
*array_obj
,
871 struct bt_value
*element_obj
)
873 struct bt_value_array
*typed_array_obj
=
874 BT_VALUE_TO_ARRAY(array_obj
);
876 BT_ASSERT_PRE_NON_NULL(array_obj
, "Array value object");
877 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
878 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
879 BT_ASSERT_PRE_VALUE_HOT(array_obj
, "Array value object");
880 g_ptr_array_add(typed_array_obj
->garray
, element_obj
);
881 bt_object_get_ref(element_obj
);
882 BT_LOGV("Appended element to array value: array-value-addr=%p, "
883 "element-value-addr=%p, new-size=%u",
884 array_obj
, element_obj
, typed_array_obj
->garray
->len
);
885 return BT_VALUE_STATUS_OK
;
888 enum bt_value_status
bt_private_value_array_append_bool_element(
889 struct bt_private_value
*array_obj
, bt_bool val
)
891 enum bt_value_status ret
;
892 struct bt_private_value
*bool_obj
= NULL
;
894 bool_obj
= bt_private_value_bool_create_init(val
);
895 ret
= bt_private_value_array_append_element(array_obj
,
897 bt_object_put_ref(bool_obj
);
901 enum bt_value_status
bt_private_value_array_append_integer_element(
902 struct bt_private_value
*array_obj
, int64_t val
)
904 enum bt_value_status ret
;
905 struct bt_private_value
*integer_obj
= NULL
;
907 integer_obj
= bt_private_value_integer_create_init(val
);
908 ret
= bt_private_value_array_append_element(array_obj
,
909 (void *) integer_obj
);
910 bt_object_put_ref(integer_obj
);
914 enum bt_value_status
bt_private_value_array_append_real_element(
915 struct bt_private_value
*array_obj
, double val
)
917 enum bt_value_status ret
;
918 struct bt_private_value
*real_obj
= NULL
;
920 real_obj
= bt_private_value_real_create_init(val
);
921 ret
= bt_private_value_array_append_element(array_obj
,
923 bt_object_put_ref(real_obj
);
927 enum bt_value_status
bt_private_value_array_append_string_element(
928 struct bt_private_value
*array_obj
, const char *val
)
930 enum bt_value_status ret
;
931 struct bt_private_value
*string_obj
= NULL
;
933 string_obj
= bt_private_value_string_create_init(val
);
934 ret
= bt_private_value_array_append_element(array_obj
,
935 (void *) string_obj
);
936 bt_object_put_ref(string_obj
);
940 enum bt_value_status
bt_private_value_array_append_empty_array_element(
941 struct bt_private_value
*array_obj
)
943 enum bt_value_status ret
;
944 struct bt_private_value
*empty_array_obj
= NULL
;
946 empty_array_obj
= bt_private_value_array_create();
947 ret
= bt_private_value_array_append_element(array_obj
,
948 (void *) empty_array_obj
);
949 bt_object_put_ref(empty_array_obj
);
953 enum bt_value_status
bt_private_value_array_append_empty_map_element(
954 struct bt_private_value
*array_obj
)
956 enum bt_value_status ret
;
957 struct bt_private_value
*map_obj
= NULL
;
959 map_obj
= bt_private_value_map_create();
960 ret
= bt_private_value_array_append_element(array_obj
,
962 bt_object_put_ref(map_obj
);
966 enum bt_value_status
bt_private_value_array_set_element_by_index(
967 struct bt_private_value
*array_obj
, uint64_t index
,
968 struct bt_value
*element_obj
)
970 struct bt_value_array
*typed_array_obj
=
971 BT_VALUE_TO_ARRAY(array_obj
);
973 BT_ASSERT_PRE_NON_NULL(array_obj
, "Array value object");
974 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
975 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
976 BT_ASSERT_PRE_VALUE_HOT(array_obj
, "Array value object");
977 BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index
,
978 typed_array_obj
->garray
->len
);
979 bt_object_put_ref(g_ptr_array_index(typed_array_obj
->garray
, index
));
980 g_ptr_array_index(typed_array_obj
->garray
, index
) = element_obj
;
981 bt_object_get_ref(element_obj
);
982 BT_LOGV("Set array value's element: array-value-addr=%p, "
983 "index=%" PRIu64
", element-value-addr=%p",
984 array_obj
, index
, element_obj
);
985 return BT_VALUE_STATUS_OK
;
988 uint64_t bt_value_map_get_size(const struct bt_value
*map_obj
)
990 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
991 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
992 return (uint64_t) g_hash_table_size(BT_VALUE_TO_MAP(map_obj
)->ght
);
995 struct bt_value
*bt_value_map_borrow_entry_value(const struct bt_value
*map_obj
,
998 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
999 BT_ASSERT_PRE_NON_NULL(key
, "Key");
1000 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1001 return g_hash_table_lookup(BT_VALUE_TO_MAP(map_obj
)->ght
,
1002 GUINT_TO_POINTER(g_quark_from_string(key
)));
1005 struct bt_private_value
*bt_private_value_map_borrow_entry_value(
1006 const struct bt_private_value
*map_obj
, const char *key
)
1008 return (void *) bt_value_map_borrow_entry_value((void *) map_obj
, key
);
1011 bt_bool
bt_value_map_has_entry(const struct bt_value
*map_obj
, const char *key
)
1013 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
1014 BT_ASSERT_PRE_NON_NULL(key
, "Key");
1015 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1016 return bt_g_hash_table_contains(BT_VALUE_TO_MAP(map_obj
)->ght
,
1017 GUINT_TO_POINTER(g_quark_from_string(key
)));
1020 enum bt_value_status
bt_private_value_map_insert_entry(
1021 struct bt_private_value
*map_obj
,
1022 const char *key
, struct bt_value
*element_obj
)
1024 BT_ASSERT_PRE_NON_NULL(map_obj
, "Map value object");
1025 BT_ASSERT_PRE_NON_NULL(key
, "Key");
1026 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
1027 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1028 BT_ASSERT_PRE_VALUE_HOT(map_obj
, "Map value object");
1029 g_hash_table_insert(BT_VALUE_TO_MAP(map_obj
)->ght
,
1030 GUINT_TO_POINTER(g_quark_from_string(key
)), element_obj
);
1031 bt_object_get_ref(element_obj
);
1032 BT_LOGV("Inserted value into map value: map-value-addr=%p, "
1033 "key=\"%s\", element-value-addr=%p",
1034 map_obj
, key
, element_obj
);
1035 return BT_VALUE_STATUS_OK
;
1038 enum bt_value_status
bt_private_value_map_insert_bool_entry(
1039 struct bt_private_value
*map_obj
, const char *key
, bt_bool val
)
1041 enum bt_value_status ret
;
1042 struct bt_private_value
*bool_obj
= NULL
;
1044 bool_obj
= bt_private_value_bool_create_init(val
);
1045 ret
= bt_private_value_map_insert_entry(map_obj
, key
,
1047 bt_object_put_ref(bool_obj
);
1051 enum bt_value_status
bt_private_value_map_insert_integer_entry(
1052 struct bt_private_value
*map_obj
, const char *key
, int64_t val
)
1054 enum bt_value_status ret
;
1055 struct bt_private_value
*integer_obj
= NULL
;
1057 integer_obj
= bt_private_value_integer_create_init(val
);
1058 ret
= bt_private_value_map_insert_entry(map_obj
, key
,
1059 (void *) integer_obj
);
1060 bt_object_put_ref(integer_obj
);
1064 enum bt_value_status
bt_private_value_map_insert_real_entry(
1065 struct bt_private_value
*map_obj
, const char *key
, double val
)
1067 enum bt_value_status ret
;
1068 struct bt_private_value
*real_obj
= NULL
;
1070 real_obj
= bt_private_value_real_create_init(val
);
1071 ret
= bt_private_value_map_insert_entry(map_obj
, key
,
1073 bt_object_put_ref(real_obj
);
1077 enum bt_value_status
bt_private_value_map_insert_string_entry(
1078 struct bt_private_value
*map_obj
, const char *key
,
1081 enum bt_value_status ret
;
1082 struct bt_private_value
*string_obj
= NULL
;
1084 string_obj
= bt_private_value_string_create_init(val
);
1085 ret
= bt_private_value_map_insert_entry(map_obj
, key
,
1086 (void *) string_obj
);
1087 bt_object_put_ref(string_obj
);
1091 enum bt_value_status
bt_private_value_map_insert_empty_array_entry(
1092 struct bt_private_value
*map_obj
, const char *key
)
1094 enum bt_value_status ret
;
1095 struct bt_private_value
*array_obj
= NULL
;
1097 array_obj
= bt_private_value_array_create();
1098 ret
= bt_private_value_map_insert_entry(map_obj
, key
,
1099 (void *) array_obj
);
1100 bt_object_put_ref(array_obj
);
1104 enum bt_value_status
bt_private_value_map_insert_empty_map_entry(
1105 struct bt_private_value
*map_obj
, const char *key
)
1107 enum bt_value_status ret
;
1108 struct bt_private_value
*empty_map_obj
= NULL
;
1110 empty_map_obj
= bt_private_value_map_create();
1111 ret
= bt_private_value_map_insert_entry(map_obj
, key
,
1112 (void *) empty_map_obj
);
1113 bt_object_put_ref(empty_map_obj
);
1117 enum bt_value_status
bt_value_map_foreach_entry(const struct bt_value
*map_obj
,
1118 bt_value_map_foreach_entry_cb cb
, void *data
)
1120 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1121 gpointer key
, element_obj
;
1122 GHashTableIter iter
;
1123 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1125 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
1126 BT_ASSERT_PRE_NON_NULL(cb
, "Callback");
1127 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1128 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
1130 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
1131 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
1133 if (!cb(key_str
, element_obj
, data
)) {
1134 BT_LOGV("User canceled the loop: key=\"%s\", "
1135 "value-addr=%p, data=%p",
1136 key_str
, element_obj
, data
);
1137 ret
= BT_VALUE_STATUS_CANCELED
;
1145 enum bt_value_status
bt_private_value_map_foreach_entry(
1146 const struct bt_private_value
*map_obj
,
1147 bt_private_value_map_foreach_entry_cb cb
, void *data
)
1149 return bt_value_map_foreach_entry((void *) map_obj
,
1150 (bt_value_map_foreach_entry_cb
) cb
, data
);
1153 struct extend_map_element_data
{
1154 struct bt_private_value
*extended_obj
;
1155 enum bt_value_status status
;
1159 bt_bool
extend_map_element(const char *key
,
1160 struct bt_value
*extension_obj_elem
, void *data
)
1162 bt_bool ret
= BT_TRUE
;
1163 struct extend_map_element_data
*extend_data
= data
;
1164 struct bt_private_value
*extension_obj_elem_copy
= NULL
;
1166 /* Copy object which is to replace the current one */
1167 extend_data
->status
= bt_value_copy(&extension_obj_elem_copy
,
1168 extension_obj_elem
);
1169 if (extend_data
->status
) {
1170 BT_LOGE("Cannot copy map element: addr=%p",
1171 extension_obj_elem
);
1175 BT_ASSERT(extension_obj_elem_copy
);
1177 /* Replace in extended object */
1178 extend_data
->status
= bt_private_value_map_insert_entry(
1179 extend_data
->extended_obj
, key
,
1180 (void *) extension_obj_elem_copy
);
1181 if (extend_data
->status
) {
1182 BT_LOGE("Cannot replace value in extended value: key=\"%s\", "
1183 "extended-value-addr=%p, element-value-addr=%p",
1184 key
, extend_data
->extended_obj
,
1185 extension_obj_elem_copy
);
1192 BT_ASSERT(extend_data
->status
!= BT_VALUE_STATUS_OK
);
1196 BT_OBJECT_PUT_REF_AND_RESET(extension_obj_elem_copy
);
1200 enum bt_value_status
bt_value_map_extend(
1201 struct bt_private_value
**extended_map_obj
,
1202 const struct bt_value
*base_map_obj
,
1203 const struct bt_value
*extension_obj
)
1205 struct extend_map_element_data extend_data
= {
1206 .extended_obj
= NULL
,
1207 .status
= BT_VALUE_STATUS_OK
,
1210 BT_ASSERT_PRE_NON_NULL(base_map_obj
, "Base value object");
1211 BT_ASSERT_PRE_NON_NULL(extension_obj
, "Extension value object");
1212 BT_ASSERT_PRE_NON_NULL(extended_map_obj
,
1213 "Extended value object (output)");
1214 BT_ASSERT_PRE_VALUE_IS_TYPE(base_map_obj
, BT_VALUE_TYPE_MAP
);
1215 BT_ASSERT_PRE_VALUE_IS_TYPE(extension_obj
, BT_VALUE_TYPE_MAP
);
1216 BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p",
1217 base_map_obj
, extension_obj
);
1218 *extended_map_obj
= NULL
;
1220 /* Create copy of base map object to start with */
1221 extend_data
.status
= bt_value_copy(extended_map_obj
, base_map_obj
);
1222 if (extend_data
.status
) {
1223 BT_LOGE("Cannot copy base value: base-value-addr=%p",
1228 BT_ASSERT(extended_map_obj
);
1231 * For each key in the extension map object, replace this key
1232 * in the copied map object.
1234 extend_data
.extended_obj
= *extended_map_obj
;
1236 if (bt_value_map_foreach_entry(extension_obj
, extend_map_element
,
1238 BT_LOGE("Cannot iterate on the extension object's elements: "
1239 "extension-value-addr=%p", extension_obj
);
1243 if (extend_data
.status
) {
1244 BT_LOGE("Failed to successfully iterate on the extension object's elements: "
1245 "extension-value-addr=%p", extension_obj
);
1249 BT_LOGD("Extended map value: extended-value-addr=%p",
1254 BT_OBJECT_PUT_REF_AND_RESET(*extended_map_obj
);
1255 *extended_map_obj
= NULL
;
1258 return extend_data
.status
;
1261 enum bt_value_status
bt_value_copy(struct bt_private_value
**copy_obj
,
1262 const struct bt_value
*object
)
1264 enum bt_value_status status
= BT_VALUE_STATUS_OK
;
1266 BT_ASSERT_PRE_NON_NULL(object
, "Value object");
1267 BT_ASSERT_PRE_NON_NULL(copy_obj
, "Value object copy (output)");
1268 BT_LOGD("Copying value object: addr=%p", object
);
1269 *copy_obj
= copy_funcs
[object
->type
](object
);
1271 BT_LOGD("Copied value object: copy-value-addr=%p",
1274 status
= BT_VALUE_STATUS_NOMEM
;
1276 BT_LOGE_STR("Failed to copy value object.");
1282 bt_bool
bt_value_compare(const struct bt_value
*object_a
,
1283 const struct bt_value
*object_b
)
1285 bt_bool ret
= BT_FALSE
;
1287 BT_ASSERT_PRE_NON_NULL(object_a
, "Value object A");
1288 BT_ASSERT_PRE_NON_NULL(object_b
, "Value object B");
1290 if (object_a
->type
!= object_b
->type
) {
1291 BT_LOGV("Values are different: type mismatch: "
1292 "value-a-addr=%p, value-b-addr=%p, "
1293 "value-a-type=%s, value-b-type=%s",
1295 bt_common_value_type_string(object_a
->type
),
1296 bt_common_value_type_string(object_b
->type
));
1300 ret
= compare_funcs
[object_a
->type
](object_a
, object_b
);