2 * Values.c: value objects
6 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
7 * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 #define BT_LOG_TAG "VALUES"
29 #include <babeltrace/lib-logging-internal.h>
35 #include <babeltrace/compiler-internal.h>
36 #include <babeltrace/common-internal.h>
37 #include <babeltrace/object.h>
38 #include <babeltrace/values.h>
39 #include <babeltrace/private-values.h>
40 #include <babeltrace/compat/glib-internal.h>
41 #include <babeltrace/types.h>
42 #include <babeltrace/object-internal.h>
43 #include <babeltrace/values-internal.h>
44 #include <babeltrace/assert-internal.h>
45 #include <babeltrace/assert-pre-internal.h>
47 #define BT_VALUE_FROM_CONCRETE(_concrete) ((struct bt_value *) (_concrete))
48 #define BT_VALUE_TO_BOOL(_base) ((struct bt_value_bool *) (_base))
49 #define BT_VALUE_TO_INTEGER(_base) ((struct bt_value_integer *) (_base))
50 #define BT_VALUE_TO_REAL(_base) ((struct bt_value_real *) (_base))
51 #define BT_VALUE_TO_STRING(_base) ((struct bt_value_string *) (_base))
52 #define BT_VALUE_TO_ARRAY(_base) ((struct bt_value_array *) (_base))
53 #define BT_VALUE_TO_MAP(_base) ((struct bt_value_map *) (_base))
55 #define BT_ASSERT_PRE_VALUE_IS_TYPE(_value, _type) \
56 BT_ASSERT_PRE(((struct bt_value *) (_value))->type == (_type), \
57 "Value has the wrong type ID: expected-type=%s, " \
58 "%![value-]+v", bt_common_value_type_string(_type), \
61 #define BT_ASSERT_PRE_VALUE_HOT(_value, _name) \
62 BT_ASSERT_PRE_HOT(((struct bt_value *) (_value)), (_name), \
65 #define BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(_index, _count) \
66 BT_ASSERT_PRE((_index) < (_count), \
67 "Index is out of bound: " \
68 "index=%" PRIu64 ", count=%u", (_index), (_count));
71 struct bt_object base
;
72 enum bt_value_type type
;
77 void bt_value_null_instance_release_func(struct bt_object
*obj
)
79 BT_LOGW("Releasing the null value singleton: addr=%p", obj
);
83 struct bt_value bt_value_null_instance
= {
87 .release_func
= bt_value_null_instance_release_func
,
88 .spec_release_func
= NULL
,
89 .parent_is_owner_listener_func
= NULL
,
92 .type
= BT_VALUE_TYPE_NULL
,
96 struct bt_value
*bt_value_null
= &bt_value_null_instance
;
97 struct bt_private_value
*bt_private_value_null
=
98 (void *) &bt_value_null_instance
;
100 struct bt_value_bool
{
101 struct bt_value base
;
105 struct bt_value_integer
{
106 struct bt_value base
;
110 struct bt_value_real
{
111 struct bt_value base
;
115 struct bt_value_string
{
116 struct bt_value base
;
120 struct bt_value_array
{
121 struct bt_value base
;
125 struct bt_value_map
{
126 struct bt_value base
;
131 void bt_value_destroy(struct bt_object
*obj
);
134 void bt_value_string_destroy(struct bt_value
*object
)
136 g_string_free(BT_VALUE_TO_STRING(object
)->gstr
, TRUE
);
140 void bt_value_array_destroy(struct bt_value
*object
)
143 * Pointer array's registered value destructor will take care
144 * of putting each contained object.
146 g_ptr_array_free(BT_VALUE_TO_ARRAY(object
)->garray
, TRUE
);
150 void bt_value_map_destroy(struct bt_value
*object
)
153 * Hash table's registered value destructor will take care of
154 * putting each contained object. Keys are GQuarks and cannot
155 * be destroyed anyway.
157 g_hash_table_destroy(BT_VALUE_TO_MAP(object
)->ght
);
161 void (* const destroy_funcs
[])(struct bt_value
*) = {
162 [BT_VALUE_TYPE_NULL
] = NULL
,
163 [BT_VALUE_TYPE_BOOL
] = NULL
,
164 [BT_VALUE_TYPE_INTEGER
] = NULL
,
165 [BT_VALUE_TYPE_REAL
] = NULL
,
166 [BT_VALUE_TYPE_STRING
] = bt_value_string_destroy
,
167 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_destroy
,
168 [BT_VALUE_TYPE_MAP
] = bt_value_map_destroy
,
172 struct bt_private_value
*bt_value_null_copy(const struct bt_value
*null_obj
)
174 return (void *) bt_value_null
;
178 struct bt_private_value
*bt_value_bool_copy(const struct bt_value
*bool_obj
)
180 return bt_private_value_bool_create_init(
181 BT_VALUE_TO_BOOL(bool_obj
)->value
);
185 struct bt_private_value
*bt_value_integer_copy(
186 const struct bt_value
*integer_obj
)
188 return bt_private_value_integer_create_init(
189 BT_VALUE_TO_INTEGER(integer_obj
)->value
);
193 struct bt_private_value
*bt_value_real_copy(const struct bt_value
*real_obj
)
195 return bt_private_value_real_create_init(
196 BT_VALUE_TO_REAL(real_obj
)->value
);
200 struct bt_private_value
*bt_value_string_copy(const struct bt_value
*string_obj
)
202 return bt_private_value_string_create_init(
203 BT_VALUE_TO_STRING(string_obj
)->gstr
->str
);
207 struct bt_private_value
*bt_value_array_copy(const struct bt_value
*array_obj
)
211 struct bt_private_value
*copy_obj
;
212 struct bt_value_array
*typed_array_obj
;
214 BT_LOGD("Copying array value: addr=%p", array_obj
);
215 typed_array_obj
= BT_VALUE_TO_ARRAY(array_obj
);
216 copy_obj
= bt_private_value_array_create();
218 BT_LOGE_STR("Cannot create empty array value.");
222 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
223 struct bt_private_value
*element_obj_copy
= NULL
;
224 struct bt_value
*element_obj
=
225 bt_value_array_borrow_element_by_index(
228 BT_ASSERT(element_obj
);
229 BT_LOGD("Copying array value's element: element-addr=%p, "
230 "index=%d", element_obj
, i
);
231 ret
= bt_value_copy(&element_obj_copy
, element_obj
);
233 BT_LOGE("Cannot copy array value's element: "
234 "array-addr=%p, index=%d",
236 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
240 BT_ASSERT(element_obj_copy
);
241 ret
= bt_private_value_array_append_element(copy_obj
,
242 (void *) element_obj_copy
);
243 BT_OBJECT_PUT_REF_AND_RESET(element_obj_copy
);
245 BT_LOGE("Cannot append to array value: addr=%p",
247 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
252 BT_LOGD("Copied array value: original-addr=%p, copy-addr=%p",
253 array_obj
, copy_obj
);
260 struct bt_private_value
*bt_value_map_copy(const struct bt_value
*map_obj
)
264 gpointer key
, element_obj
;
265 struct bt_private_value
*copy_obj
;
266 struct bt_private_value
*element_obj_copy
= NULL
;
267 struct bt_value_map
*typed_map_obj
;
269 BT_LOGD("Copying map value: addr=%p", map_obj
);
270 typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
271 copy_obj
= bt_private_value_map_create();
276 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
278 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
279 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
282 BT_LOGD("Copying map value's element: element-addr=%p, "
283 "key=\"%s\"", element_obj
, key_str
);
284 ret
= bt_value_copy(&element_obj_copy
, element_obj
);
286 BT_LOGE("Cannot copy map value's element: "
287 "map-addr=%p, key=\"%s\"",
289 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
293 BT_ASSERT(element_obj_copy
);
294 ret
= bt_private_value_map_insert_entry(copy_obj
, key_str
,
295 (void *) element_obj_copy
);
296 BT_OBJECT_PUT_REF_AND_RESET(element_obj_copy
);
298 BT_LOGE("Cannot insert into map value: addr=%p, key=\"%s\"",
300 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
305 BT_LOGD("Copied map value: addr=%p", map_obj
);
312 struct bt_private_value
*(* const copy_funcs
[])(const struct bt_value
*) = {
313 [BT_VALUE_TYPE_NULL
] = bt_value_null_copy
,
314 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_copy
,
315 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_copy
,
316 [BT_VALUE_TYPE_REAL
] = bt_value_real_copy
,
317 [BT_VALUE_TYPE_STRING
] = bt_value_string_copy
,
318 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_copy
,
319 [BT_VALUE_TYPE_MAP
] = bt_value_map_copy
,
323 bt_bool
bt_value_null_compare(const struct bt_value
*object_a
,
324 const struct bt_value
*object_b
)
327 * Always BT_TRUE since bt_value_compare() already checks if both
328 * object_a and object_b have the same type, and in the case of
329 * null value objects, they're always the same if it is so.
335 bt_bool
bt_value_bool_compare(const struct bt_value
*object_a
,
336 const struct bt_value
*object_b
)
338 if (BT_VALUE_TO_BOOL(object_a
)->value
!=
339 BT_VALUE_TO_BOOL(object_b
)->value
) {
340 BT_LOGV("Boolean value objects are different: "
341 "bool-a-val=%d, bool-b-val=%d",
342 BT_VALUE_TO_BOOL(object_a
)->value
,
343 BT_VALUE_TO_BOOL(object_b
)->value
);
351 bt_bool
bt_value_integer_compare(const struct bt_value
*object_a
,
352 const struct bt_value
*object_b
)
354 if (BT_VALUE_TO_INTEGER(object_a
)->value
!=
355 BT_VALUE_TO_INTEGER(object_b
)->value
) {
356 BT_LOGV("Integer value objects are different: "
357 "int-a-val=%" PRId64
", int-b-val=%" PRId64
,
358 BT_VALUE_TO_INTEGER(object_a
)->value
,
359 BT_VALUE_TO_INTEGER(object_b
)->value
);
367 bt_bool
bt_value_real_compare(const struct bt_value
*object_a
,
368 const struct bt_value
*object_b
)
370 if (BT_VALUE_TO_REAL(object_a
)->value
!=
371 BT_VALUE_TO_REAL(object_b
)->value
) {
372 BT_LOGV("Real number value objects are different: "
373 "real-a-val=%f, real-b-val=%f",
374 BT_VALUE_TO_REAL(object_a
)->value
,
375 BT_VALUE_TO_REAL(object_b
)->value
);
383 bt_bool
bt_value_string_compare(const struct bt_value
*object_a
,
384 const struct bt_value
*object_b
)
386 if (strcmp(BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
387 BT_VALUE_TO_STRING(object_b
)->gstr
->str
) != 0) {
388 BT_LOGV("String value objects are different: "
389 "string-a-val=\"%s\", string-b-val=\"%s\"",
390 BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
391 BT_VALUE_TO_STRING(object_b
)->gstr
->str
);
399 bt_bool
bt_value_array_compare(const struct bt_value
*object_a
,
400 const struct bt_value
*object_b
)
403 bt_bool ret
= BT_TRUE
;
404 const struct bt_value_array
*array_obj_a
=
405 BT_VALUE_TO_ARRAY(object_a
);
407 if (bt_value_array_get_size(object_a
) !=
408 bt_value_array_get_size(object_b
)) {
409 BT_LOGV("Array values are different: size mismatch "
410 "value-a-addr=%p, value-b-addr=%p, "
411 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
413 bt_value_array_get_size(object_a
),
414 bt_value_array_get_size(object_b
));
419 for (i
= 0; i
< array_obj_a
->garray
->len
; ++i
) {
420 struct bt_value
*element_obj_a
;
421 struct bt_value
*element_obj_b
;
423 element_obj_a
= bt_value_array_borrow_element_by_index(
425 element_obj_b
= bt_value_array_borrow_element_by_index(
428 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
429 BT_LOGV("Array values's elements are different: "
430 "value-a-addr=%p, value-b-addr=%p, index=%d",
431 element_obj_a
, element_obj_b
, i
);
442 bt_bool
bt_value_map_compare(const struct bt_value
*object_a
,
443 const struct bt_value
*object_b
)
445 bt_bool ret
= BT_TRUE
;
447 gpointer key
, element_obj_a
;
448 const struct bt_value_map
*map_obj_a
= BT_VALUE_TO_MAP(object_a
);
450 if (bt_value_map_get_size(object_a
) !=
451 bt_value_map_get_size(object_b
)) {
452 BT_LOGV("Map values are different: size mismatch "
453 "value-a-addr=%p, value-b-addr=%p, "
454 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
456 bt_value_map_get_size(object_a
),
457 bt_value_map_get_size(object_b
));
462 g_hash_table_iter_init(&iter
, map_obj_a
->ght
);
464 while (g_hash_table_iter_next(&iter
, &key
, &element_obj_a
)) {
465 struct bt_value
*element_obj_b
;
466 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
468 element_obj_b
= bt_value_map_borrow_entry_value(object_b
,
471 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
472 BT_LOGV("Map values's elements are different: "
473 "value-a-addr=%p, value-b-addr=%p, key=\"%s\"",
474 element_obj_a
, element_obj_b
, key_str
);
485 bt_bool (* const compare_funcs
[])(const struct bt_value
*,
486 const struct bt_value
*) = {
487 [BT_VALUE_TYPE_NULL
] = bt_value_null_compare
,
488 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_compare
,
489 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_compare
,
490 [BT_VALUE_TYPE_REAL
] = bt_value_real_compare
,
491 [BT_VALUE_TYPE_STRING
] = bt_value_string_compare
,
492 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_compare
,
493 [BT_VALUE_TYPE_MAP
] = bt_value_map_compare
,
497 void bt_value_null_freeze(struct bt_value
*object
)
502 void bt_value_generic_freeze(struct bt_value
*object
)
504 object
->frozen
= BT_TRUE
;
508 void bt_value_array_freeze(struct bt_value
*object
)
511 struct bt_value_array
*typed_array_obj
=
512 BT_VALUE_TO_ARRAY(object
);
514 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
515 bt_value_freeze(g_ptr_array_index(typed_array_obj
->garray
, i
));
518 bt_value_generic_freeze(object
);
522 void bt_value_map_freeze(struct bt_value
*object
)
525 gpointer key
, element_obj
;
526 const struct bt_value_map
*map_obj
= BT_VALUE_TO_MAP(object
);
528 g_hash_table_iter_init(&iter
, map_obj
->ght
);
530 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
531 bt_value_freeze(element_obj
);
534 bt_value_generic_freeze(object
);
538 void (* const freeze_funcs
[])(struct bt_value
*) = {
539 [BT_VALUE_TYPE_NULL
] = bt_value_null_freeze
,
540 [BT_VALUE_TYPE_BOOL
] = bt_value_generic_freeze
,
541 [BT_VALUE_TYPE_INTEGER
] = bt_value_generic_freeze
,
542 [BT_VALUE_TYPE_REAL
] = bt_value_generic_freeze
,
543 [BT_VALUE_TYPE_STRING
] = bt_value_generic_freeze
,
544 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_freeze
,
545 [BT_VALUE_TYPE_MAP
] = bt_value_map_freeze
,
549 void bt_value_destroy(struct bt_object
*obj
)
551 struct bt_value
*value
;
553 value
= container_of(obj
, struct bt_value
, base
);
554 BT_LOGD("Destroying value: addr=%p", value
);
556 if (bt_value_is_null(value
)) {
557 BT_LOGD_STR("Not destroying the null value singleton.");
561 if (destroy_funcs
[value
->type
]) {
562 destroy_funcs
[value
->type
](value
);
569 enum bt_value_status
_bt_value_freeze(struct bt_value
*object
)
571 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
575 if (object
->frozen
) {
579 BT_LOGD("Freezing value: addr=%p", object
);
580 freeze_funcs
[object
->type
](object
);
586 enum bt_value_type
bt_value_get_type(const struct bt_value
*object
)
588 BT_ASSERT_PRE_NON_NULL(object
, "Value object");
593 struct bt_value
bt_value_create_base(enum bt_value_type type
)
595 struct bt_value value
;
598 value
.frozen
= BT_FALSE
;
599 bt_object_init_shared(&value
.base
, bt_value_destroy
);
603 struct bt_private_value
*bt_private_value_bool_create_init(bt_bool val
)
605 struct bt_value_bool
*bool_obj
;
607 BT_LOGD("Creating boolean value object: val=%d", val
);
608 bool_obj
= g_new0(struct bt_value_bool
, 1);
610 BT_LOGE_STR("Failed to allocate one boolean value object.");
614 bool_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_BOOL
);
615 bool_obj
->value
= val
;
616 BT_LOGD("Created boolean value object: addr=%p", bool_obj
);
619 return (void *) BT_VALUE_FROM_CONCRETE(bool_obj
);
622 struct bt_private_value
*bt_private_value_bool_create(void)
624 return bt_private_value_bool_create_init(BT_FALSE
);
627 struct bt_private_value
*bt_private_value_integer_create_init(int64_t val
)
629 struct bt_value_integer
*integer_obj
;
631 BT_LOGD("Creating integer value object: val=%" PRId64
, val
);
632 integer_obj
= g_new0(struct bt_value_integer
, 1);
634 BT_LOGE_STR("Failed to allocate one integer value object.");
638 integer_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_INTEGER
);
639 integer_obj
->value
= val
;
640 BT_LOGD("Created integer value object: addr=%p",
644 return (void *) BT_VALUE_FROM_CONCRETE(integer_obj
);
647 struct bt_private_value
*bt_private_value_integer_create(void)
649 return bt_private_value_integer_create_init(0);
652 struct bt_private_value
*bt_private_value_real_create_init(double val
)
654 struct bt_value_real
*real_obj
;
656 BT_LOGD("Creating real number value object: val=%f", val
);
657 real_obj
= g_new0(struct bt_value_real
, 1);
659 BT_LOGE_STR("Failed to allocate one real number value object.");
663 real_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_REAL
);
664 real_obj
->value
= val
;
665 BT_LOGD("Created real number value object: addr=%p",
669 return (void *) BT_VALUE_FROM_CONCRETE(real_obj
);
672 struct bt_private_value
*bt_private_value_real_create(void)
674 return bt_private_value_real_create_init(0.);
677 struct bt_private_value
*bt_private_value_string_create_init(const char *val
)
679 struct bt_value_string
*string_obj
= NULL
;
682 BT_LOGW_STR("Invalid parameter: value is NULL.");
686 BT_LOGD("Creating string value object: val-len=%zu", strlen(val
));
687 string_obj
= g_new0(struct bt_value_string
, 1);
689 BT_LOGE_STR("Failed to allocate one string object.");
693 string_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_STRING
);
694 string_obj
->gstr
= g_string_new(val
);
695 if (!string_obj
->gstr
) {
696 BT_LOGE_STR("Failed to allocate a GString.");
702 BT_LOGD("Created string value object: addr=%p",
706 return (void *) BT_VALUE_FROM_CONCRETE(string_obj
);
709 struct bt_private_value
*bt_private_value_string_create(void)
711 return bt_private_value_string_create_init("");
714 struct bt_private_value
*bt_private_value_array_create(void)
716 struct bt_value_array
*array_obj
;
718 BT_LOGD_STR("Creating empty array value object.");
719 array_obj
= g_new0(struct bt_value_array
, 1);
721 BT_LOGE_STR("Failed to allocate one array object.");
725 array_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_ARRAY
);
726 array_obj
->garray
= bt_g_ptr_array_new_full(0,
727 (GDestroyNotify
) bt_object_put_ref
);
728 if (!array_obj
->garray
) {
729 BT_LOGE_STR("Failed to allocate a GPtrArray.");
735 BT_LOGD("Created array value object: addr=%p",
739 return (void *) BT_VALUE_FROM_CONCRETE(array_obj
);
742 struct bt_private_value
*bt_private_value_map_create(void)
744 struct bt_value_map
*map_obj
;
746 BT_LOGD_STR("Creating empty map value object.");
747 map_obj
= g_new0(struct bt_value_map
, 1);
749 BT_LOGE_STR("Failed to allocate one map object.");
753 map_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_MAP
);
754 map_obj
->ght
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
755 NULL
, (GDestroyNotify
) bt_object_put_ref
);
757 BT_LOGE_STR("Failed to allocate a GHashTable.");
763 BT_LOGD("Created map value object: addr=%p",
767 return (void *) BT_VALUE_FROM_CONCRETE(map_obj
);
770 bt_bool
bt_value_bool_get(const struct bt_value
*bool_obj
)
772 BT_ASSERT_PRE_NON_NULL(bool_obj
, "Value object");
773 BT_ASSERT_PRE_VALUE_IS_TYPE(bool_obj
, BT_VALUE_TYPE_BOOL
);
774 return BT_VALUE_TO_BOOL(bool_obj
)->value
;
777 void bt_private_value_bool_set(struct bt_private_value
*bool_obj
, bt_bool val
)
779 BT_ASSERT_PRE_NON_NULL(bool_obj
, "Value object");
780 BT_ASSERT_PRE_VALUE_IS_TYPE(bool_obj
, BT_VALUE_TYPE_BOOL
);
781 BT_ASSERT_PRE_VALUE_HOT(bool_obj
, "Value object");
782 BT_VALUE_TO_BOOL(bool_obj
)->value
= val
;
783 BT_LOGV("Set boolean value's raw value: value-addr=%p, value=%d",
787 int64_t bt_value_integer_get(const struct bt_value
*integer_obj
)
789 BT_ASSERT_PRE_NON_NULL(integer_obj
, "Value object");
790 BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj
, BT_VALUE_TYPE_INTEGER
);
791 return BT_VALUE_TO_INTEGER(integer_obj
)->value
;
794 void bt_private_value_integer_set(struct bt_private_value
*integer_obj
,
797 BT_ASSERT_PRE_NON_NULL(integer_obj
, "Value object");
798 BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj
, BT_VALUE_TYPE_INTEGER
);
799 BT_ASSERT_PRE_VALUE_HOT(integer_obj
, "Value object");
800 BT_VALUE_TO_INTEGER(integer_obj
)->value
= val
;
801 BT_LOGV("Set integer value's raw value: value-addr=%p, value=%" PRId64
,
805 double bt_value_real_get(const struct bt_value
*real_obj
)
807 BT_ASSERT_PRE_NON_NULL(real_obj
, "Value object");
808 BT_ASSERT_PRE_VALUE_IS_TYPE(real_obj
, BT_VALUE_TYPE_REAL
);
809 return BT_VALUE_TO_REAL(real_obj
)->value
;
812 void bt_private_value_real_set(struct bt_private_value
*real_obj
, double val
)
814 BT_ASSERT_PRE_NON_NULL(real_obj
, "Value object");
815 BT_ASSERT_PRE_VALUE_IS_TYPE(real_obj
, BT_VALUE_TYPE_REAL
);
816 BT_ASSERT_PRE_VALUE_HOT(real_obj
, "Value object");
817 BT_VALUE_TO_REAL(real_obj
)->value
= val
;
818 BT_LOGV("Set real number value's raw value: value-addr=%p, value=%f",
822 const char *bt_value_string_get(const struct bt_value
*string_obj
)
824 BT_ASSERT_PRE_NON_NULL(string_obj
, "Value object");
825 BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj
, BT_VALUE_TYPE_STRING
);
826 return BT_VALUE_TO_STRING(string_obj
)->gstr
->str
;
829 enum bt_value_status
bt_private_value_string_set(
830 struct bt_private_value
*string_obj
, const char *val
)
832 BT_ASSERT_PRE_NON_NULL(string_obj
, "Value object");
833 BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj
, BT_VALUE_TYPE_STRING
);
834 BT_ASSERT_PRE_VALUE_HOT(string_obj
, "Value object");
835 g_string_assign(BT_VALUE_TO_STRING(string_obj
)->gstr
, val
);
836 BT_LOGV("Set string value's raw value: value-addr=%p, raw-value-addr=%p",
838 return BT_VALUE_STATUS_OK
;
841 uint64_t bt_value_array_get_size(const struct bt_value
*array_obj
)
843 BT_ASSERT_PRE_NON_NULL(array_obj
, "Value object");
844 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
845 return (uint64_t) BT_VALUE_TO_ARRAY(array_obj
)->garray
->len
;
848 struct bt_value
*bt_value_array_borrow_element_by_index(
849 const struct bt_value
*array_obj
,
852 struct bt_value_array
*typed_array_obj
=
853 BT_VALUE_TO_ARRAY(array_obj
);
855 BT_ASSERT_PRE_NON_NULL(array_obj
, "Value object");
856 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
857 BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index
,
858 typed_array_obj
->garray
->len
);
859 return g_ptr_array_index(typed_array_obj
->garray
, index
);
862 struct bt_private_value
*bt_private_value_array_borrow_element_by_index(
863 const struct bt_private_value
*array_obj
,
866 return (void *) bt_value_array_borrow_element_by_index(
867 (void *) array_obj
, index
);
870 enum bt_value_status
bt_private_value_array_append_element(
871 struct bt_private_value
*array_obj
,
872 struct bt_value
*element_obj
)
874 struct bt_value_array
*typed_array_obj
=
875 BT_VALUE_TO_ARRAY(array_obj
);
877 BT_ASSERT_PRE_NON_NULL(array_obj
, "Array value object");
878 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
879 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
880 BT_ASSERT_PRE_VALUE_HOT(array_obj
, "Array value object");
881 g_ptr_array_add(typed_array_obj
->garray
, element_obj
);
882 bt_object_get_ref(element_obj
);
883 BT_LOGV("Appended element to array value: array-value-addr=%p, "
884 "element-value-addr=%p, new-size=%u",
885 array_obj
, element_obj
, typed_array_obj
->garray
->len
);
886 return BT_VALUE_STATUS_OK
;
889 enum bt_value_status
bt_private_value_array_append_bool_element(
890 struct bt_private_value
*array_obj
, bt_bool val
)
892 enum bt_value_status ret
;
893 struct bt_private_value
*bool_obj
= NULL
;
895 bool_obj
= bt_private_value_bool_create_init(val
);
896 ret
= bt_private_value_array_append_element(array_obj
,
898 bt_object_put_ref(bool_obj
);
902 enum bt_value_status
bt_private_value_array_append_integer_element(
903 struct bt_private_value
*array_obj
, int64_t val
)
905 enum bt_value_status ret
;
906 struct bt_private_value
*integer_obj
= NULL
;
908 integer_obj
= bt_private_value_integer_create_init(val
);
909 ret
= bt_private_value_array_append_element(array_obj
,
910 (void *) integer_obj
);
911 bt_object_put_ref(integer_obj
);
915 enum bt_value_status
bt_private_value_array_append_real_element(
916 struct bt_private_value
*array_obj
, double val
)
918 enum bt_value_status ret
;
919 struct bt_private_value
*real_obj
= NULL
;
921 real_obj
= bt_private_value_real_create_init(val
);
922 ret
= bt_private_value_array_append_element(array_obj
,
924 bt_object_put_ref(real_obj
);
928 enum bt_value_status
bt_private_value_array_append_string_element(
929 struct bt_private_value
*array_obj
, const char *val
)
931 enum bt_value_status ret
;
932 struct bt_private_value
*string_obj
= NULL
;
934 string_obj
= bt_private_value_string_create_init(val
);
935 ret
= bt_private_value_array_append_element(array_obj
,
936 (void *) string_obj
);
937 bt_object_put_ref(string_obj
);
941 enum bt_value_status
bt_private_value_array_append_empty_array_element(
942 struct bt_private_value
*array_obj
)
944 enum bt_value_status ret
;
945 struct bt_private_value
*empty_array_obj
= NULL
;
947 empty_array_obj
= bt_private_value_array_create();
948 ret
= bt_private_value_array_append_element(array_obj
,
949 (void *) empty_array_obj
);
950 bt_object_put_ref(empty_array_obj
);
954 enum bt_value_status
bt_private_value_array_append_empty_map_element(
955 struct bt_private_value
*array_obj
)
957 enum bt_value_status ret
;
958 struct bt_private_value
*map_obj
= NULL
;
960 map_obj
= bt_private_value_map_create();
961 ret
= bt_private_value_array_append_element(array_obj
,
963 bt_object_put_ref(map_obj
);
967 enum bt_value_status
bt_private_value_array_set_element_by_index(
968 struct bt_private_value
*array_obj
, uint64_t index
,
969 struct bt_value
*element_obj
)
971 struct bt_value_array
*typed_array_obj
=
972 BT_VALUE_TO_ARRAY(array_obj
);
974 BT_ASSERT_PRE_NON_NULL(array_obj
, "Array value object");
975 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
976 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
977 BT_ASSERT_PRE_VALUE_HOT(array_obj
, "Array value object");
978 BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index
,
979 typed_array_obj
->garray
->len
);
980 bt_object_put_ref(g_ptr_array_index(typed_array_obj
->garray
, index
));
981 g_ptr_array_index(typed_array_obj
->garray
, index
) = element_obj
;
982 bt_object_get_ref(element_obj
);
983 BT_LOGV("Set array value's element: array-value-addr=%p, "
984 "index=%" PRIu64
", element-value-addr=%p",
985 array_obj
, index
, element_obj
);
986 return BT_VALUE_STATUS_OK
;
989 uint64_t bt_value_map_get_size(const struct bt_value
*map_obj
)
991 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
992 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
993 return (uint64_t) g_hash_table_size(BT_VALUE_TO_MAP(map_obj
)->ght
);
996 struct bt_value
*bt_value_map_borrow_entry_value(const struct bt_value
*map_obj
,
999 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
1000 BT_ASSERT_PRE_NON_NULL(key
, "Key");
1001 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1002 return g_hash_table_lookup(BT_VALUE_TO_MAP(map_obj
)->ght
,
1003 GUINT_TO_POINTER(g_quark_from_string(key
)));
1006 struct bt_private_value
*bt_private_value_map_borrow_entry_value(
1007 const struct bt_private_value
*map_obj
, const char *key
)
1009 return (void *) bt_value_map_borrow_entry_value((void *) map_obj
, key
);
1012 bt_bool
bt_value_map_has_entry(const struct bt_value
*map_obj
, const char *key
)
1014 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
1015 BT_ASSERT_PRE_NON_NULL(key
, "Key");
1016 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1017 return bt_g_hash_table_contains(BT_VALUE_TO_MAP(map_obj
)->ght
,
1018 GUINT_TO_POINTER(g_quark_from_string(key
)));
1021 enum bt_value_status
bt_private_value_map_insert_entry(
1022 struct bt_private_value
*map_obj
,
1023 const char *key
, struct bt_value
*element_obj
)
1025 BT_ASSERT_PRE_NON_NULL(map_obj
, "Map value object");
1026 BT_ASSERT_PRE_NON_NULL(key
, "Key");
1027 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
1028 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1029 BT_ASSERT_PRE_VALUE_HOT(map_obj
, "Map value object");
1030 g_hash_table_insert(BT_VALUE_TO_MAP(map_obj
)->ght
,
1031 GUINT_TO_POINTER(g_quark_from_string(key
)), element_obj
);
1032 bt_object_get_ref(element_obj
);
1033 BT_LOGV("Inserted value into map value: map-value-addr=%p, "
1034 "key=\"%s\", element-value-addr=%p",
1035 map_obj
, key
, element_obj
);
1036 return BT_VALUE_STATUS_OK
;
1039 enum bt_value_status
bt_private_value_map_insert_bool_entry(
1040 struct bt_private_value
*map_obj
, const char *key
, bt_bool val
)
1042 enum bt_value_status ret
;
1043 struct bt_private_value
*bool_obj
= NULL
;
1045 bool_obj
= bt_private_value_bool_create_init(val
);
1046 ret
= bt_private_value_map_insert_entry(map_obj
, key
,
1048 bt_object_put_ref(bool_obj
);
1052 enum bt_value_status
bt_private_value_map_insert_integer_entry(
1053 struct bt_private_value
*map_obj
, const char *key
, int64_t val
)
1055 enum bt_value_status ret
;
1056 struct bt_private_value
*integer_obj
= NULL
;
1058 integer_obj
= bt_private_value_integer_create_init(val
);
1059 ret
= bt_private_value_map_insert_entry(map_obj
, key
,
1060 (void *) integer_obj
);
1061 bt_object_put_ref(integer_obj
);
1065 enum bt_value_status
bt_private_value_map_insert_real_entry(
1066 struct bt_private_value
*map_obj
, const char *key
, double val
)
1068 enum bt_value_status ret
;
1069 struct bt_private_value
*real_obj
= NULL
;
1071 real_obj
= bt_private_value_real_create_init(val
);
1072 ret
= bt_private_value_map_insert_entry(map_obj
, key
,
1074 bt_object_put_ref(real_obj
);
1078 enum bt_value_status
bt_private_value_map_insert_string_entry(
1079 struct bt_private_value
*map_obj
, const char *key
,
1082 enum bt_value_status ret
;
1083 struct bt_private_value
*string_obj
= NULL
;
1085 string_obj
= bt_private_value_string_create_init(val
);
1086 ret
= bt_private_value_map_insert_entry(map_obj
, key
,
1087 (void *) string_obj
);
1088 bt_object_put_ref(string_obj
);
1092 enum bt_value_status
bt_private_value_map_insert_empty_array_entry(
1093 struct bt_private_value
*map_obj
, const char *key
)
1095 enum bt_value_status ret
;
1096 struct bt_private_value
*array_obj
= NULL
;
1098 array_obj
= bt_private_value_array_create();
1099 ret
= bt_private_value_map_insert_entry(map_obj
, key
,
1100 (void *) array_obj
);
1101 bt_object_put_ref(array_obj
);
1105 enum bt_value_status
bt_private_value_map_insert_empty_map_entry(
1106 struct bt_private_value
*map_obj
, const char *key
)
1108 enum bt_value_status ret
;
1109 struct bt_private_value
*empty_map_obj
= NULL
;
1111 empty_map_obj
= bt_private_value_map_create();
1112 ret
= bt_private_value_map_insert_entry(map_obj
, key
,
1113 (void *) empty_map_obj
);
1114 bt_object_put_ref(empty_map_obj
);
1118 enum bt_value_status
bt_value_map_foreach_entry(const struct bt_value
*map_obj
,
1119 bt_value_map_foreach_entry_cb cb
, void *data
)
1121 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1122 gpointer key
, element_obj
;
1123 GHashTableIter iter
;
1124 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1126 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
1127 BT_ASSERT_PRE_NON_NULL(cb
, "Callback");
1128 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1129 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
1131 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
1132 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
1134 if (!cb(key_str
, element_obj
, data
)) {
1135 BT_LOGV("User canceled the loop: key=\"%s\", "
1136 "value-addr=%p, data=%p",
1137 key_str
, element_obj
, data
);
1138 ret
= BT_VALUE_STATUS_CANCELED
;
1146 enum bt_value_status
bt_private_value_map_foreach_entry(
1147 const struct bt_private_value
*map_obj
,
1148 bt_private_value_map_foreach_entry_cb cb
, void *data
)
1150 return bt_value_map_foreach_entry((void *) map_obj
,
1151 (bt_value_map_foreach_entry_cb
) cb
, data
);
1154 struct extend_map_element_data
{
1155 struct bt_private_value
*extended_obj
;
1156 enum bt_value_status status
;
1160 bt_bool
extend_map_element(const char *key
,
1161 struct bt_value
*extension_obj_elem
, void *data
)
1163 bt_bool ret
= BT_TRUE
;
1164 struct extend_map_element_data
*extend_data
= data
;
1165 struct bt_private_value
*extension_obj_elem_copy
= NULL
;
1167 /* Copy object which is to replace the current one */
1168 extend_data
->status
= bt_value_copy(&extension_obj_elem_copy
,
1169 extension_obj_elem
);
1170 if (extend_data
->status
) {
1171 BT_LOGE("Cannot copy map element: addr=%p",
1172 extension_obj_elem
);
1176 BT_ASSERT(extension_obj_elem_copy
);
1178 /* Replace in extended object */
1179 extend_data
->status
= bt_private_value_map_insert_entry(
1180 extend_data
->extended_obj
, key
,
1181 (void *) extension_obj_elem_copy
);
1182 if (extend_data
->status
) {
1183 BT_LOGE("Cannot replace value in extended value: key=\"%s\", "
1184 "extended-value-addr=%p, element-value-addr=%p",
1185 key
, extend_data
->extended_obj
,
1186 extension_obj_elem_copy
);
1193 BT_ASSERT(extend_data
->status
!= BT_VALUE_STATUS_OK
);
1197 BT_OBJECT_PUT_REF_AND_RESET(extension_obj_elem_copy
);
1201 enum bt_value_status
bt_value_map_extend(
1202 struct bt_private_value
**extended_map_obj
,
1203 struct bt_value
*base_map_obj
,
1204 struct bt_value
*extension_obj
)
1206 struct extend_map_element_data extend_data
= {
1207 .extended_obj
= NULL
,
1208 .status
= BT_VALUE_STATUS_OK
,
1211 BT_ASSERT_PRE_NON_NULL(base_map_obj
, "Base value object");
1212 BT_ASSERT_PRE_NON_NULL(extension_obj
, "Extension value object");
1213 BT_ASSERT_PRE_NON_NULL(extended_map_obj
,
1214 "Extended value object (output)");
1215 BT_ASSERT_PRE_VALUE_IS_TYPE(base_map_obj
, BT_VALUE_TYPE_MAP
);
1216 BT_ASSERT_PRE_VALUE_IS_TYPE(extension_obj
, BT_VALUE_TYPE_MAP
);
1217 BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p",
1218 base_map_obj
, extension_obj
);
1219 *extended_map_obj
= NULL
;
1221 /* Create copy of base map object to start with */
1222 extend_data
.status
= bt_value_copy(extended_map_obj
, base_map_obj
);
1223 if (extend_data
.status
) {
1224 BT_LOGE("Cannot copy base value: base-value-addr=%p",
1229 BT_ASSERT(extended_map_obj
);
1232 * For each key in the extension map object, replace this key
1233 * in the copied map object.
1235 extend_data
.extended_obj
= *extended_map_obj
;
1237 if (bt_value_map_foreach_entry(extension_obj
, extend_map_element
,
1239 BT_LOGE("Cannot iterate on the extension object's elements: "
1240 "extension-value-addr=%p", extension_obj
);
1244 if (extend_data
.status
) {
1245 BT_LOGE("Failed to successfully iterate on the extension object's elements: "
1246 "extension-value-addr=%p", extension_obj
);
1250 BT_LOGD("Extended map value: extended-value-addr=%p",
1255 BT_OBJECT_PUT_REF_AND_RESET(*extended_map_obj
);
1256 *extended_map_obj
= NULL
;
1259 return extend_data
.status
;
1262 enum bt_value_status
bt_value_copy(struct bt_private_value
**copy_obj
,
1263 const struct bt_value
*object
)
1265 enum bt_value_status status
= BT_VALUE_STATUS_OK
;
1267 BT_ASSERT_PRE_NON_NULL(object
, "Value object");
1268 BT_ASSERT_PRE_NON_NULL(copy_obj
, "Value object copy (output)");
1269 BT_LOGD("Copying value object: addr=%p", object
);
1270 *copy_obj
= copy_funcs
[object
->type
](object
);
1272 BT_LOGD("Copied value object: copy-value-addr=%p",
1275 status
= BT_VALUE_STATUS_NOMEM
;
1277 BT_LOGE_STR("Failed to copy value object.");
1283 bt_bool
bt_value_compare(const struct bt_value
*object_a
,
1284 const struct bt_value
*object_b
)
1286 bt_bool ret
= BT_FALSE
;
1288 BT_ASSERT_PRE_NON_NULL(object_a
, "Value object A");
1289 BT_ASSERT_PRE_NON_NULL(object_b
, "Value object B");
1291 if (object_a
->type
!= object_b
->type
) {
1292 BT_LOGV("Values are different: type mismatch: "
1293 "value-a-addr=%p, value-b-addr=%p, "
1294 "value-a-type=%s, value-b-type=%s",
1296 bt_common_value_type_string(object_a
->type
),
1297 bt_common_value_type_string(object_b
->type
));
1301 ret
= compare_funcs
[object_a
->type
](object_a
, object_b
);
1307 struct bt_value
*bt_value_borrow_from_private(
1308 struct bt_private_value
*priv_value
)
1310 return (void *) priv_value
;