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
= (void *) &bt_value_null_instance
;
99 struct bt_value_bool
{
100 struct bt_value base
;
104 struct bt_value_integer
{
105 struct bt_value base
;
109 struct bt_value_real
{
110 struct bt_value base
;
114 struct bt_value_string
{
115 struct bt_value base
;
119 struct bt_value_array
{
120 struct bt_value base
;
124 struct bt_value_map
{
125 struct bt_value base
;
130 void bt_value_destroy(struct bt_object
*obj
);
133 void bt_value_string_destroy(struct bt_value
*object
)
135 g_string_free(BT_VALUE_TO_STRING(object
)->gstr
, TRUE
);
139 void bt_value_array_destroy(struct bt_value
*object
)
142 * Pointer array's registered value destructor will take care
143 * of putting each contained object.
145 g_ptr_array_free(BT_VALUE_TO_ARRAY(object
)->garray
, TRUE
);
149 void bt_value_map_destroy(struct bt_value
*object
)
152 * Hash table's registered value destructor will take care of
153 * putting each contained object. Keys are GQuarks and cannot
154 * be destroyed anyway.
156 g_hash_table_destroy(BT_VALUE_TO_MAP(object
)->ght
);
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(BT_VALUE_TO_BOOL(bool_obj
)->value
);
183 struct bt_private_value
*bt_value_integer_copy(
184 const struct bt_value
*integer_obj
)
186 return bt_private_value_integer_create_init(
187 BT_VALUE_TO_INTEGER(integer_obj
)->value
);
191 struct bt_private_value
*bt_value_real_copy(const struct bt_value
*real_obj
)
193 return bt_private_value_real_create_init(
194 BT_VALUE_TO_REAL(real_obj
)->value
);
198 struct bt_private_value
*bt_value_string_copy(const struct bt_value
*string_obj
)
200 return bt_private_value_string_create_init(
201 BT_VALUE_TO_STRING(string_obj
)->gstr
->str
);
205 struct bt_private_value
*bt_value_array_copy(const struct bt_value
*array_obj
)
209 struct bt_private_value
*copy_obj
;
210 struct bt_value_array
*typed_array_obj
;
212 BT_LOGD("Copying array value: addr=%p", array_obj
);
213 typed_array_obj
= BT_VALUE_TO_ARRAY(array_obj
);
214 copy_obj
= bt_private_value_array_create();
216 BT_LOGE_STR("Cannot create empty array value.");
220 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
221 struct bt_private_value
*element_obj_copy
;
222 struct bt_value
*element_obj
= bt_value_array_borrow_element_by_index(
225 BT_ASSERT(element_obj
);
226 BT_LOGD("Copying array value's element: element-addr=%p, "
227 "index=%d", element_obj
, i
);
228 element_obj_copy
= bt_value_copy(element_obj
);
229 if (!element_obj_copy
) {
230 BT_LOGE("Cannot copy array value's element: "
231 "array-addr=%p, index=%d",
233 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
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
;
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 element_obj_copy
= bt_value_copy(element_obj
);
281 if (!element_obj_copy
) {
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 ret
= bt_private_value_map_insert_entry(copy_obj
, key_str
,
290 (void *) element_obj_copy
);
291 BT_OBJECT_PUT_REF_AND_RESET(element_obj_copy
);
293 BT_LOGE("Cannot insert into map value: addr=%p, key=\"%s\"",
295 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
300 BT_LOGD("Copied map value: addr=%p", map_obj
);
307 struct bt_private_value
*(* const copy_funcs
[])(const struct bt_value
*) = {
308 [BT_VALUE_TYPE_NULL
] = bt_value_null_copy
,
309 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_copy
,
310 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_copy
,
311 [BT_VALUE_TYPE_REAL
] = bt_value_real_copy
,
312 [BT_VALUE_TYPE_STRING
] = bt_value_string_copy
,
313 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_copy
,
314 [BT_VALUE_TYPE_MAP
] = bt_value_map_copy
,
318 bt_bool
bt_value_null_compare(const struct bt_value
*object_a
,
319 const struct bt_value
*object_b
)
322 * Always BT_TRUE since bt_value_compare() already checks if both
323 * object_a and object_b have the same type, and in the case of
324 * null value objects, they're always the same if it is so.
330 bt_bool
bt_value_bool_compare(const struct bt_value
*object_a
,
331 const struct bt_value
*object_b
)
333 if (BT_VALUE_TO_BOOL(object_a
)->value
!=
334 BT_VALUE_TO_BOOL(object_b
)->value
) {
335 BT_LOGV("Boolean value objects are different: "
336 "bool-a-val=%d, bool-b-val=%d",
337 BT_VALUE_TO_BOOL(object_a
)->value
,
338 BT_VALUE_TO_BOOL(object_b
)->value
);
346 bt_bool
bt_value_integer_compare(const struct bt_value
*object_a
,
347 const struct bt_value
*object_b
)
349 if (BT_VALUE_TO_INTEGER(object_a
)->value
!=
350 BT_VALUE_TO_INTEGER(object_b
)->value
) {
351 BT_LOGV("Integer value objects are different: "
352 "int-a-val=%" PRId64
", int-b-val=%" PRId64
,
353 BT_VALUE_TO_INTEGER(object_a
)->value
,
354 BT_VALUE_TO_INTEGER(object_b
)->value
);
362 bt_bool
bt_value_real_compare(const struct bt_value
*object_a
,
363 const struct bt_value
*object_b
)
365 if (BT_VALUE_TO_REAL(object_a
)->value
!=
366 BT_VALUE_TO_REAL(object_b
)->value
) {
367 BT_LOGV("Real number value objects are different: "
368 "real-a-val=%f, real-b-val=%f",
369 BT_VALUE_TO_REAL(object_a
)->value
,
370 BT_VALUE_TO_REAL(object_b
)->value
);
378 bt_bool
bt_value_string_compare(const struct bt_value
*object_a
,
379 const struct bt_value
*object_b
)
381 if (strcmp(BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
382 BT_VALUE_TO_STRING(object_b
)->gstr
->str
) != 0) {
383 BT_LOGV("String value objects are different: "
384 "string-a-val=\"%s\", string-b-val=\"%s\"",
385 BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
386 BT_VALUE_TO_STRING(object_b
)->gstr
->str
);
394 bt_bool
bt_value_array_compare(const struct bt_value
*object_a
,
395 const struct bt_value
*object_b
)
398 bt_bool ret
= BT_TRUE
;
399 const struct bt_value_array
*array_obj_a
=
400 BT_VALUE_TO_ARRAY(object_a
);
402 if (bt_value_array_get_size(object_a
) !=
403 bt_value_array_get_size(object_b
)) {
404 BT_LOGV("Array values are different: size mismatch "
405 "value-a-addr=%p, value-b-addr=%p, "
406 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
408 bt_value_array_get_size(object_a
),
409 bt_value_array_get_size(object_b
));
414 for (i
= 0; i
< array_obj_a
->garray
->len
; ++i
) {
415 struct bt_value
*element_obj_a
;
416 struct bt_value
*element_obj_b
;
418 element_obj_a
= bt_value_array_borrow_element_by_index(
420 element_obj_b
= bt_value_array_borrow_element_by_index(
423 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
424 BT_LOGV("Array values's elements are different: "
425 "value-a-addr=%p, value-b-addr=%p, index=%d",
426 element_obj_a
, element_obj_b
, i
);
437 bt_bool
bt_value_map_compare(const struct bt_value
*object_a
,
438 const struct bt_value
*object_b
)
440 bt_bool ret
= BT_TRUE
;
442 gpointer key
, element_obj_a
;
443 const struct bt_value_map
*map_obj_a
= BT_VALUE_TO_MAP(object_a
);
445 if (bt_value_map_get_size(object_a
) != bt_value_map_get_size(object_b
)) {
446 BT_LOGV("Map values are different: size mismatch "
447 "value-a-addr=%p, value-b-addr=%p, "
448 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
450 bt_value_map_get_size(object_a
),
451 bt_value_map_get_size(object_b
));
456 g_hash_table_iter_init(&iter
, map_obj_a
->ght
);
458 while (g_hash_table_iter_next(&iter
, &key
, &element_obj_a
)) {
459 struct bt_value
*element_obj_b
;
460 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
462 element_obj_b
= bt_value_map_borrow_entry_value(object_b
,
465 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
466 BT_LOGV("Map values's elements are different: "
467 "value-a-addr=%p, value-b-addr=%p, key=\"%s\"",
468 element_obj_a
, element_obj_b
, key_str
);
479 bt_bool (* const compare_funcs
[])(const struct bt_value
*,
480 const struct bt_value
*) = {
481 [BT_VALUE_TYPE_NULL
] = bt_value_null_compare
,
482 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_compare
,
483 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_compare
,
484 [BT_VALUE_TYPE_REAL
] = bt_value_real_compare
,
485 [BT_VALUE_TYPE_STRING
] = bt_value_string_compare
,
486 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_compare
,
487 [BT_VALUE_TYPE_MAP
] = bt_value_map_compare
,
491 void bt_value_null_freeze(struct bt_value
*object
)
496 void bt_value_generic_freeze(struct bt_value
*object
)
498 object
->frozen
= BT_TRUE
;
502 void bt_value_array_freeze(struct bt_value
*object
)
505 struct bt_value_array
*typed_array_obj
=
506 BT_VALUE_TO_ARRAY(object
);
508 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
509 bt_value_freeze(g_ptr_array_index(typed_array_obj
->garray
, i
));
512 bt_value_generic_freeze(object
);
516 void bt_value_map_freeze(struct bt_value
*object
)
519 gpointer key
, element_obj
;
520 const struct bt_value_map
*map_obj
= BT_VALUE_TO_MAP(object
);
522 g_hash_table_iter_init(&iter
, map_obj
->ght
);
524 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
525 bt_value_freeze(element_obj
);
528 bt_value_generic_freeze(object
);
532 void (* const freeze_funcs
[])(struct bt_value
*) = {
533 [BT_VALUE_TYPE_NULL
] = bt_value_null_freeze
,
534 [BT_VALUE_TYPE_BOOL
] = bt_value_generic_freeze
,
535 [BT_VALUE_TYPE_INTEGER
] = bt_value_generic_freeze
,
536 [BT_VALUE_TYPE_REAL
] = bt_value_generic_freeze
,
537 [BT_VALUE_TYPE_STRING
] = bt_value_generic_freeze
,
538 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_freeze
,
539 [BT_VALUE_TYPE_MAP
] = bt_value_map_freeze
,
543 void bt_value_destroy(struct bt_object
*obj
)
545 struct bt_value
*value
;
547 value
= container_of(obj
, struct bt_value
, base
);
548 BT_LOGD("Destroying value: addr=%p", value
);
550 if (bt_value_is_null(value
)) {
551 BT_LOGD_STR("Not destroying the null value singleton.");
555 if (destroy_funcs
[value
->type
]) {
556 destroy_funcs
[value
->type
](value
);
563 enum bt_value_status
_bt_value_freeze(struct bt_value
*object
)
565 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
569 if (object
->frozen
) {
573 BT_LOGD("Freezing value: addr=%p", object
);
574 freeze_funcs
[object
->type
](object
);
580 enum bt_value_type
bt_value_get_type(const struct bt_value
*object
)
582 BT_ASSERT_PRE_NON_NULL(object
, "Value object");
587 struct bt_value
bt_value_create_base(enum bt_value_type type
)
589 struct bt_value value
;
592 value
.frozen
= BT_FALSE
;
593 bt_object_init_shared(&value
.base
, bt_value_destroy
);
597 struct bt_private_value
*bt_private_value_bool_create_init(bt_bool val
)
599 struct bt_value_bool
*bool_obj
;
601 BT_LOGD("Creating boolean value object: val=%d", val
);
602 bool_obj
= g_new0(struct bt_value_bool
, 1);
604 BT_LOGE_STR("Failed to allocate one boolean value object.");
608 bool_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_BOOL
);
609 bool_obj
->value
= val
;
610 BT_LOGD("Created boolean value object: addr=%p", bool_obj
);
613 return (void *) BT_VALUE_FROM_CONCRETE(bool_obj
);
616 struct bt_private_value
*bt_private_value_bool_create(void)
618 return bt_private_value_bool_create_init(BT_FALSE
);
621 struct bt_private_value
*bt_private_value_integer_create_init(int64_t val
)
623 struct bt_value_integer
*integer_obj
;
625 BT_LOGD("Creating integer value object: val=%" PRId64
, val
);
626 integer_obj
= g_new0(struct bt_value_integer
, 1);
628 BT_LOGE_STR("Failed to allocate one integer value object.");
632 integer_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_INTEGER
);
633 integer_obj
->value
= val
;
634 BT_LOGD("Created integer value object: addr=%p",
638 return (void *) BT_VALUE_FROM_CONCRETE(integer_obj
);
641 struct bt_private_value
*bt_private_value_integer_create(void)
643 return bt_private_value_integer_create_init(0);
646 struct bt_private_value
*bt_private_value_real_create_init(double val
)
648 struct bt_value_real
*real_obj
;
650 BT_LOGD("Creating real number value object: val=%f", val
);
651 real_obj
= g_new0(struct bt_value_real
, 1);
653 BT_LOGE_STR("Failed to allocate one real number value object.");
657 real_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_REAL
);
658 real_obj
->value
= val
;
659 BT_LOGD("Created real number value object: addr=%p",
663 return (void *) BT_VALUE_FROM_CONCRETE(real_obj
);
666 struct bt_private_value
*bt_private_value_real_create(void)
668 return bt_private_value_real_create_init(0.);
671 struct bt_private_value
*bt_private_value_string_create_init(const char *val
)
673 struct bt_value_string
*string_obj
= NULL
;
676 BT_LOGW_STR("Invalid parameter: value is NULL.");
680 BT_LOGD("Creating string value object: val-len=%zu", strlen(val
));
681 string_obj
= g_new0(struct bt_value_string
, 1);
683 BT_LOGE_STR("Failed to allocate one string object.");
687 string_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_STRING
);
688 string_obj
->gstr
= g_string_new(val
);
689 if (!string_obj
->gstr
) {
690 BT_LOGE_STR("Failed to allocate a GString.");
696 BT_LOGD("Created string value object: addr=%p",
700 return (void *) BT_VALUE_FROM_CONCRETE(string_obj
);
703 struct bt_private_value
*bt_private_value_string_create(void)
705 return bt_private_value_string_create_init("");
708 struct bt_private_value
*bt_private_value_array_create(void)
710 struct bt_value_array
*array_obj
;
712 BT_LOGD_STR("Creating empty array value object.");
713 array_obj
= g_new0(struct bt_value_array
, 1);
715 BT_LOGE_STR("Failed to allocate one array object.");
719 array_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_ARRAY
);
720 array_obj
->garray
= bt_g_ptr_array_new_full(0,
721 (GDestroyNotify
) bt_object_put_ref
);
722 if (!array_obj
->garray
) {
723 BT_LOGE_STR("Failed to allocate a GPtrArray.");
729 BT_LOGD("Created array value object: addr=%p",
733 return (void *) BT_VALUE_FROM_CONCRETE(array_obj
);
736 struct bt_private_value
*bt_private_value_map_create(void)
738 struct bt_value_map
*map_obj
;
740 BT_LOGD_STR("Creating empty map value object.");
741 map_obj
= g_new0(struct bt_value_map
, 1);
743 BT_LOGE_STR("Failed to allocate one map object.");
747 map_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_MAP
);
748 map_obj
->ght
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
749 NULL
, (GDestroyNotify
) bt_object_put_ref
);
751 BT_LOGE_STR("Failed to allocate a GHashTable.");
757 BT_LOGD("Created map value object: addr=%p",
761 return (void *) BT_VALUE_FROM_CONCRETE(map_obj
);
764 enum bt_value_status
bt_value_bool_get(const struct bt_value
*bool_obj
,
767 BT_ASSERT_PRE_NON_NULL(bool_obj
, "Value object");
768 BT_ASSERT_PRE_NON_NULL(val
, "Raw value");
769 BT_ASSERT_PRE_VALUE_IS_TYPE(bool_obj
, BT_VALUE_TYPE_BOOL
);
770 *val
= BT_VALUE_TO_BOOL(bool_obj
)->value
;
771 return BT_VALUE_STATUS_OK
;
774 enum bt_value_status
bt_private_value_bool_set(struct bt_private_value
*bool_obj
,
777 BT_ASSERT_PRE_NON_NULL(bool_obj
, "Value object");
778 BT_ASSERT_PRE_VALUE_IS_TYPE(bool_obj
, BT_VALUE_TYPE_BOOL
);
779 BT_ASSERT_PRE_VALUE_HOT(bool_obj
, "Value object");
780 BT_VALUE_TO_BOOL(bool_obj
)->value
= val
;
781 BT_LOGV("Set boolean value's raw value: value-addr=%p, value=%d",
783 return BT_VALUE_STATUS_OK
;
786 enum bt_value_status
bt_value_integer_get(const struct bt_value
*integer_obj
,
789 BT_ASSERT_PRE_NON_NULL(integer_obj
, "Value object");
790 BT_ASSERT_PRE_NON_NULL(val
, "Raw value");
791 BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj
, BT_VALUE_TYPE_INTEGER
);
792 *val
= BT_VALUE_TO_INTEGER(integer_obj
)->value
;
793 return BT_VALUE_STATUS_OK
;
796 enum bt_value_status
bt_private_integer_bool_set(struct bt_private_value
*integer_obj
,
799 BT_ASSERT_PRE_NON_NULL(integer_obj
, "Value object");
800 BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj
, BT_VALUE_TYPE_INTEGER
);
801 BT_ASSERT_PRE_VALUE_HOT(integer_obj
, "Value object");
802 BT_VALUE_TO_INTEGER(integer_obj
)->value
= val
;
803 BT_LOGV("Set integer value's raw value: value-addr=%p, value=%" PRId64
,
805 return BT_VALUE_STATUS_OK
;
808 enum bt_value_status
bt_value_real_get(const struct bt_value
*real_obj
,
811 BT_ASSERT_PRE_NON_NULL(real_obj
, "Value object");
812 BT_ASSERT_PRE_NON_NULL(val
, "Raw value");
813 BT_ASSERT_PRE_VALUE_IS_TYPE(real_obj
, BT_VALUE_TYPE_REAL
);
814 *val
= BT_VALUE_TO_REAL(real_obj
)->value
;
815 return BT_VALUE_STATUS_OK
;
818 enum bt_value_status
bt_private_value_real_set(struct bt_private_value
*real_obj
,
821 BT_ASSERT_PRE_NON_NULL(real_obj
, "Value object");
822 BT_ASSERT_PRE_VALUE_IS_TYPE(real_obj
, BT_VALUE_TYPE_REAL
);
823 BT_ASSERT_PRE_VALUE_HOT(real_obj
, "Value object");
824 BT_VALUE_TO_REAL(real_obj
)->value
= val
;
825 BT_LOGV("Set real number value's raw value: value-addr=%p, value=%f",
827 return BT_VALUE_STATUS_OK
;
830 enum bt_value_status
bt_value_string_get(const struct bt_value
*string_obj
,
833 BT_ASSERT_PRE_NON_NULL(string_obj
, "Value object");
834 BT_ASSERT_PRE_NON_NULL(val
, "Raw value");
835 BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj
, BT_VALUE_TYPE_STRING
);
836 *val
= BT_VALUE_TO_STRING(string_obj
)->gstr
->str
;
837 return BT_VALUE_STATUS_OK
;
840 enum bt_value_status
bt_private_value_string_set(struct bt_private_value
*string_obj
,
843 BT_ASSERT_PRE_NON_NULL(string_obj
, "Value object");
844 BT_ASSERT_PRE_NON_NULL(val
, "Raw value");
845 BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj
, BT_VALUE_TYPE_STRING
);
846 BT_ASSERT_PRE_VALUE_HOT(string_obj
, "Value object");
847 g_string_assign(BT_VALUE_TO_STRING(string_obj
)->gstr
, val
);
848 BT_LOGV("Set string value's raw value: value-addr=%p, raw-value-addr=%p",
850 return BT_VALUE_STATUS_OK
;
853 int64_t bt_value_array_get_size(const struct bt_value
*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 return (int64_t) BT_VALUE_TO_ARRAY(array_obj
)->garray
->len
;
860 bt_bool
bt_value_array_is_empty(const struct bt_value
*array_obj
)
862 return bt_value_array_get_size(array_obj
) == 0;
865 struct bt_value
*bt_value_array_borrow_element_by_index(
866 const struct bt_value
*array_obj
,
869 struct bt_value_array
*typed_array_obj
=
870 BT_VALUE_TO_ARRAY(array_obj
);
872 BT_ASSERT_PRE_NON_NULL(array_obj
, "Value object");
873 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
874 BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index
,
875 typed_array_obj
->garray
->len
);
876 return g_ptr_array_index(typed_array_obj
->garray
, index
);
879 struct bt_private_value
*bt_private_value_array_borrow_element_by_index(
880 const struct bt_private_value
*array_obj
,
883 return (void *) bt_value_array_borrow_element_by_index(
884 (void *) array_obj
, index
);
887 enum bt_value_status
bt_private_value_array_append_element(
888 struct bt_private_value
*array_obj
,
889 struct bt_value
*element_obj
)
891 struct bt_value_array
*typed_array_obj
=
892 BT_VALUE_TO_ARRAY(array_obj
);
894 BT_ASSERT_PRE_NON_NULL(array_obj
, "Array value object");
895 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
896 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
897 BT_ASSERT_PRE_VALUE_HOT(array_obj
, "Array value object");
898 g_ptr_array_add(typed_array_obj
->garray
, element_obj
);
899 bt_object_get_ref(element_obj
);
900 BT_LOGV("Appended element to array value: array-value-addr=%p, "
901 "element-value-addr=%p, new-size=%u",
902 array_obj
, element_obj
, typed_array_obj
->garray
->len
);
903 return BT_VALUE_STATUS_OK
;
906 enum bt_value_status
bt_private_value_array_append_bool_element(
907 struct bt_private_value
*array_obj
, bt_bool val
)
909 enum bt_value_status ret
;
910 struct bt_private_value
*bool_obj
= NULL
;
912 bool_obj
= bt_private_value_bool_create_init(val
);
913 ret
= bt_private_value_array_append_element(array_obj
,
915 bt_object_put_ref(bool_obj
);
919 enum bt_value_status
bt_private_value_array_append_integer_element(
920 struct bt_private_value
*array_obj
, int64_t val
)
922 enum bt_value_status ret
;
923 struct bt_private_value
*integer_obj
= NULL
;
925 integer_obj
= bt_private_value_integer_create_init(val
);
926 ret
= bt_private_value_array_append_element(array_obj
,
927 (void *) integer_obj
);
928 bt_object_put_ref(integer_obj
);
932 enum bt_value_status
bt_private_value_array_append_real_element(
933 struct bt_private_value
*array_obj
, double val
)
935 enum bt_value_status ret
;
936 struct bt_private_value
*real_obj
= NULL
;
938 real_obj
= bt_private_value_real_create_init(val
);
939 ret
= bt_private_value_array_append_element(array_obj
,
941 bt_object_put_ref(real_obj
);
945 enum bt_value_status
bt_private_value_array_append_string_element(
946 struct bt_private_value
*array_obj
, const char *val
)
948 enum bt_value_status ret
;
949 struct bt_private_value
*string_obj
= NULL
;
951 string_obj
= bt_private_value_string_create_init(val
);
952 ret
= bt_private_value_array_append_element(array_obj
,
953 (void *) string_obj
);
954 bt_object_put_ref(string_obj
);
958 enum bt_value_status
bt_private_value_array_append_empty_array_element(
959 struct bt_private_value
*array_obj
)
961 enum bt_value_status ret
;
962 struct bt_private_value
*empty_array_obj
= NULL
;
964 empty_array_obj
= bt_private_value_array_create();
965 ret
= bt_private_value_array_append_element(array_obj
,
966 (void *) empty_array_obj
);
967 bt_object_put_ref(empty_array_obj
);
971 enum bt_value_status
bt_private_value_array_append_empty_map_element(
972 struct bt_private_value
*array_obj
)
974 enum bt_value_status ret
;
975 struct bt_private_value
*map_obj
= NULL
;
977 map_obj
= bt_private_value_map_create();
978 ret
= bt_private_value_array_append_element(array_obj
,
980 bt_object_put_ref(map_obj
);
984 enum bt_value_status
bt_private_value_array_set_element_by_index(
985 struct bt_private_value
*array_obj
, uint64_t index
,
986 struct bt_value
*element_obj
)
988 struct bt_value_array
*typed_array_obj
=
989 BT_VALUE_TO_ARRAY(array_obj
);
991 BT_ASSERT_PRE_NON_NULL(array_obj
, "Array value object");
992 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
993 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
994 BT_ASSERT_PRE_VALUE_HOT(array_obj
, "Array value object");
995 BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index
,
996 typed_array_obj
->garray
->len
);
997 bt_object_put_ref(g_ptr_array_index(typed_array_obj
->garray
, index
));
998 g_ptr_array_index(typed_array_obj
->garray
, index
) = element_obj
;
999 bt_object_get_ref(element_obj
);
1000 BT_LOGV("Set array value's element: array-value-addr=%p, "
1001 "index=%" PRIu64
", element-value-addr=%p",
1002 array_obj
, index
, element_obj
);
1003 return BT_VALUE_STATUS_OK
;
1006 int64_t bt_value_map_get_size(const struct bt_value
*map_obj
)
1008 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
1009 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1010 return (int64_t) g_hash_table_size(BT_VALUE_TO_MAP(map_obj
)->ght
);
1013 bt_bool
bt_value_map_is_empty(const struct bt_value
*map_obj
)
1015 return bt_value_map_get_size(map_obj
) == 0;
1018 struct bt_value
*bt_value_map_borrow_entry_value(const struct bt_value
*map_obj
,
1021 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
1022 BT_ASSERT_PRE_NON_NULL(key
, "Key");
1023 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1024 return g_hash_table_lookup(BT_VALUE_TO_MAP(map_obj
)->ght
,
1025 GUINT_TO_POINTER(g_quark_from_string(key
)));
1028 struct bt_private_value
*bt_private_value_map_borrow_entry_value(
1029 const struct bt_private_value
*map_obj
, const char *key
)
1031 return (void *) bt_value_map_borrow_entry_value((void *) map_obj
, key
);
1034 bt_bool
bt_value_map_has_entry(const struct bt_value
*map_obj
, const char *key
)
1036 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
1037 BT_ASSERT_PRE_NON_NULL(key
, "Key");
1038 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1039 return bt_g_hash_table_contains(BT_VALUE_TO_MAP(map_obj
)->ght
,
1040 GUINT_TO_POINTER(g_quark_from_string(key
)));
1043 enum bt_value_status
bt_private_value_map_insert_entry(
1044 struct bt_private_value
*map_obj
,
1045 const char *key
, struct bt_value
*element_obj
)
1047 BT_ASSERT_PRE_NON_NULL(map_obj
, "Map value object");
1048 BT_ASSERT_PRE_NON_NULL(key
, "Key");
1049 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
1050 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1051 BT_ASSERT_PRE_VALUE_HOT(map_obj
, "Map value object");
1052 g_hash_table_insert(BT_VALUE_TO_MAP(map_obj
)->ght
,
1053 GUINT_TO_POINTER(g_quark_from_string(key
)), element_obj
);
1054 bt_object_get_ref(element_obj
);
1055 BT_LOGV("Inserted value into map value: map-value-addr=%p, "
1056 "key=\"%s\", element-value-addr=%p",
1057 map_obj
, key
, element_obj
);
1058 return BT_VALUE_STATUS_OK
;
1061 enum bt_value_status
bt_private_value_map_insert_bool_entry(
1062 struct bt_private_value
*map_obj
, const char *key
, bt_bool val
)
1064 enum bt_value_status ret
;
1065 struct bt_private_value
*bool_obj
= NULL
;
1067 bool_obj
= bt_private_value_bool_create_init(val
);
1068 ret
= bt_private_value_map_insert_entry(map_obj
, key
,
1070 bt_object_put_ref(bool_obj
);
1074 enum bt_value_status
bt_private_value_map_insert_integer_entry(
1075 struct bt_private_value
*map_obj
, const char *key
, int64_t val
)
1077 enum bt_value_status ret
;
1078 struct bt_private_value
*integer_obj
= NULL
;
1080 integer_obj
= bt_private_value_integer_create_init(val
);
1081 ret
= bt_private_value_map_insert_entry(map_obj
, key
,
1082 (void *) integer_obj
);
1083 bt_object_put_ref(integer_obj
);
1087 enum bt_value_status
bt_private_value_map_insert_real_entry(
1088 struct bt_private_value
*map_obj
, const char *key
, double val
)
1090 enum bt_value_status ret
;
1091 struct bt_private_value
*real_obj
= NULL
;
1093 real_obj
= bt_private_value_real_create_init(val
);
1094 ret
= bt_private_value_map_insert_entry(map_obj
, key
,
1096 bt_object_put_ref(real_obj
);
1100 enum bt_value_status
bt_private_value_map_insert_string_entry(
1101 struct bt_private_value
*map_obj
, const char *key
, const char *val
)
1103 enum bt_value_status ret
;
1104 struct bt_private_value
*string_obj
= NULL
;
1106 string_obj
= bt_private_value_string_create_init(val
);
1107 ret
= bt_private_value_map_insert_entry(map_obj
, key
,
1108 (void *) string_obj
);
1109 bt_object_put_ref(string_obj
);
1113 enum bt_value_status
bt_private_value_map_insert_empty_array_entry(
1114 struct bt_private_value
*map_obj
, const char *key
)
1116 enum bt_value_status ret
;
1117 struct bt_private_value
*array_obj
= NULL
;
1119 array_obj
= bt_private_value_array_create();
1120 ret
= bt_private_value_map_insert_entry(map_obj
, key
,
1121 (void *) array_obj
);
1122 bt_object_put_ref(array_obj
);
1126 enum bt_value_status
bt_private_value_map_insert_empty_map_entry(
1127 struct bt_private_value
*map_obj
, const char *key
)
1129 enum bt_value_status ret
;
1130 struct bt_private_value
*empty_map_obj
= NULL
;
1132 empty_map_obj
= bt_private_value_map_create();
1133 ret
= bt_private_value_map_insert_entry(map_obj
, key
,
1134 (void *) empty_map_obj
);
1135 bt_object_put_ref(empty_map_obj
);
1139 enum bt_value_status
bt_value_map_foreach_entry(const struct bt_value
*map_obj
,
1140 bt_value_map_foreach_entry_cb cb
, void *data
)
1142 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1143 gpointer key
, element_obj
;
1144 GHashTableIter iter
;
1145 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1147 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
1148 BT_ASSERT_PRE_NON_NULL(cb
, "Callback");
1149 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1150 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
1152 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
1153 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
1155 if (!cb(key_str
, element_obj
, data
)) {
1156 BT_LOGV("User canceled the loop: key=\"%s\", "
1157 "value-addr=%p, data=%p",
1158 key_str
, element_obj
, data
);
1159 ret
= BT_VALUE_STATUS_CANCELED
;
1167 enum bt_value_status
bt_private_value_map_foreach_entry(
1168 const struct bt_private_value
*map_obj
,
1169 bt_private_value_map_foreach_entry_cb cb
, void *data
)
1171 return bt_value_map_foreach_entry((void *) map_obj
,
1172 (bt_value_map_foreach_entry_cb
) cb
, data
);
1175 struct extend_map_element_data
{
1176 struct bt_private_value
*extended_obj
;
1181 bt_bool
extend_map_element(const char *key
,
1182 struct bt_value
*extension_obj_elem
, void *data
)
1184 bt_bool ret
= BT_TRUE
;
1186 struct extend_map_element_data
*extend_data
= data
;
1188 /* Copy object which is to replace the current one */
1189 struct bt_private_value
*extension_obj_elem_copy
=
1190 bt_value_copy(extension_obj_elem
);
1192 /* Replace in extended object */
1193 if (bt_private_value_map_insert_entry(extend_data
->extended_obj
, key
,
1194 (void *) extension_obj_elem_copy
)) {
1195 BT_LOGE("Cannot replace value in extended value: key=\"%s\", "
1196 "extended-value-addr=%p, element-value-addr=%p",
1197 key
, extend_data
->extended_obj
,
1198 extension_obj_elem_copy
);
1206 extend_data
->got_error
= BT_TRUE
;
1209 BT_OBJECT_PUT_REF_AND_RESET(extension_obj_elem_copy
);
1213 struct bt_private_value
*bt_value_map_extend(struct bt_value
*base_map_obj
,
1214 struct bt_value
*extension_obj
)
1216 struct bt_private_value
*extended_obj
= NULL
;
1217 struct extend_map_element_data extend_data
= { 0 };
1219 BT_ASSERT_PRE_NON_NULL(base_map_obj
, "Base value object");
1220 BT_ASSERT_PRE_NON_NULL(extension_obj
, "Extension value object");
1221 BT_ASSERT_PRE_VALUE_IS_TYPE(base_map_obj
, BT_VALUE_TYPE_MAP
);
1222 BT_ASSERT_PRE_VALUE_IS_TYPE(extension_obj
, BT_VALUE_TYPE_MAP
);
1223 BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p",
1224 base_map_obj
, extension_obj
);
1226 /* Create copy of base map object to start with */
1227 extended_obj
= bt_value_copy(base_map_obj
);
1228 if (!extended_obj
) {
1229 BT_LOGE("Cannot copy base value: base-value-addr=%p",
1235 * For each key in the extension map object, replace this key
1236 * in the copied map object.
1238 extend_data
.extended_obj
= extended_obj
;
1240 if (bt_value_map_foreach_entry(extension_obj
, extend_map_element
,
1242 BT_LOGE("Cannot iterate on the extension object's elements: "
1243 "extension-value-addr=%p", extension_obj
);
1247 if (extend_data
.got_error
) {
1248 BT_LOGE("Failed to successfully iterate on the extension object's elements: "
1249 "extension-value-addr=%p", extension_obj
);
1253 BT_LOGD("Extended map value: extended-value-addr=%p",
1258 BT_OBJECT_PUT_REF_AND_RESET(extended_obj
);
1261 return (void *) extended_obj
;
1264 struct bt_private_value
*bt_value_copy(const struct bt_value
*object
)
1266 struct bt_private_value
*copy_obj
= NULL
;
1268 BT_ASSERT_PRE_NON_NULL(object
, "Value object");
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 BT_LOGE_STR("Failed to copy value object.");
1278 return (void *) copy_obj
;
1281 bt_bool
bt_value_compare(const struct bt_value
*object_a
,
1282 const struct bt_value
*object_b
)
1284 bt_bool ret
= BT_FALSE
;
1286 BT_ASSERT_PRE_NON_NULL(object_a
, "Value object A");
1287 BT_ASSERT_PRE_NON_NULL(object_b
, "Value object B");
1289 if (object_a
->type
!= object_b
->type
) {
1290 BT_LOGV("Values are different: type mismatch: "
1291 "value-a-addr=%p, value-b-addr=%p, "
1292 "value-a-type=%s, value-b-type=%s",
1294 bt_common_value_type_string(object_a
->type
),
1295 bt_common_value_type_string(object_b
->type
));
1299 ret
= compare_funcs
[object_a
->type
](object_a
, object_b
);
1305 struct bt_value
*bt_value_borrow_from_private(
1306 struct bt_private_value
*priv_value
)
1308 return (void *) priv_value
;