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-const.h>
35 #include <babeltrace/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
;
94 struct bt_value_bool
{
99 struct bt_value_integer
{
100 struct bt_value base
;
104 struct bt_value_real
{
105 struct bt_value base
;
109 struct bt_value_string
{
110 struct bt_value base
;
114 struct bt_value_array
{
115 struct bt_value base
;
119 struct bt_value_map
{
120 struct bt_value base
;
125 void bt_value_destroy(struct bt_object
*obj
);
128 void bt_value_string_destroy(struct bt_value
*object
)
130 g_string_free(BT_VALUE_TO_STRING(object
)->gstr
, TRUE
);
131 BT_VALUE_TO_STRING(object
)->gstr
= NULL
;
135 void bt_value_array_destroy(struct bt_value
*object
)
138 * Pointer array's registered value destructor will take care
139 * of putting each contained object.
141 g_ptr_array_free(BT_VALUE_TO_ARRAY(object
)->garray
, TRUE
);
142 BT_VALUE_TO_ARRAY(object
)->garray
= NULL
;
146 void bt_value_map_destroy(struct bt_value
*object
)
149 * Hash table's registered value destructor will take care of
150 * putting each contained object. Keys are GQuarks and cannot
151 * be destroyed anyway.
153 g_hash_table_destroy(BT_VALUE_TO_MAP(object
)->ght
);
154 BT_VALUE_TO_MAP(object
)->ght
= NULL
;
158 void (* const destroy_funcs
[])(struct bt_value
*) = {
159 [BT_VALUE_TYPE_NULL
] = NULL
,
160 [BT_VALUE_TYPE_BOOL
] = NULL
,
161 [BT_VALUE_TYPE_INTEGER
] = NULL
,
162 [BT_VALUE_TYPE_REAL
] = NULL
,
163 [BT_VALUE_TYPE_STRING
] = bt_value_string_destroy
,
164 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_destroy
,
165 [BT_VALUE_TYPE_MAP
] = bt_value_map_destroy
,
169 struct bt_value
*bt_value_null_copy(const struct bt_value
*null_obj
)
171 return (void *) bt_value_null
;
175 struct bt_value
*bt_value_bool_copy(const struct bt_value
*bool_obj
)
177 return bt_value_bool_create_init(
178 BT_VALUE_TO_BOOL(bool_obj
)->value
);
182 struct bt_value
*bt_value_integer_copy(
183 const struct bt_value
*integer_obj
)
185 return bt_value_integer_create_init(
186 BT_VALUE_TO_INTEGER(integer_obj
)->value
);
190 struct bt_value
*bt_value_real_copy(const struct bt_value
*real_obj
)
192 return bt_value_real_create_init(
193 BT_VALUE_TO_REAL(real_obj
)->value
);
197 struct bt_value
*bt_value_string_copy(const struct bt_value
*string_obj
)
199 return bt_value_string_create_init(
200 BT_VALUE_TO_STRING(string_obj
)->gstr
->str
);
204 struct bt_value
*bt_value_array_copy(const struct bt_value
*array_obj
)
208 struct bt_value
*copy_obj
;
209 struct bt_value_array
*typed_array_obj
;
211 BT_LOGD("Copying array value: addr=%p", array_obj
);
212 typed_array_obj
= BT_VALUE_TO_ARRAY(array_obj
);
213 copy_obj
= bt_value_array_create();
215 BT_LOGE_STR("Cannot create empty array value.");
219 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
220 struct bt_value
*element_obj_copy
= NULL
;
221 const struct bt_value
*element_obj
=
222 bt_value_array_borrow_element_by_index_const(
225 BT_ASSERT(element_obj
);
226 BT_LOGD("Copying array value's element: element-addr=%p, "
227 "index=%d", element_obj
, i
);
228 ret
= bt_value_copy(&element_obj_copy
, element_obj
);
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 BT_ASSERT(element_obj_copy
);
238 ret
= bt_value_array_append_element(copy_obj
,
239 (void *) element_obj_copy
);
240 BT_OBJECT_PUT_REF_AND_RESET(element_obj_copy
);
242 BT_LOGE("Cannot append to array value: addr=%p",
244 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
249 BT_LOGD("Copied array value: original-addr=%p, copy-addr=%p",
250 array_obj
, copy_obj
);
257 struct bt_value
*bt_value_map_copy(const struct bt_value
*map_obj
)
261 gpointer key
, element_obj
;
262 struct bt_value
*copy_obj
;
263 struct bt_value
*element_obj_copy
= NULL
;
264 struct bt_value_map
*typed_map_obj
;
266 BT_LOGD("Copying map value: addr=%p", map_obj
);
267 typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
268 copy_obj
= bt_value_map_create();
273 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
275 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
276 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
279 BT_LOGD("Copying map value's element: element-addr=%p, "
280 "key=\"%s\"", element_obj
, key_str
);
281 ret
= bt_value_copy(&element_obj_copy
, element_obj
);
283 BT_LOGE("Cannot copy map value's element: "
284 "map-addr=%p, key=\"%s\"",
286 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
290 BT_ASSERT(element_obj_copy
);
291 ret
= bt_value_map_insert_entry(copy_obj
, key_str
,
292 (void *) element_obj_copy
);
293 BT_OBJECT_PUT_REF_AND_RESET(element_obj_copy
);
295 BT_LOGE("Cannot insert into map value: addr=%p, key=\"%s\"",
297 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
302 BT_LOGD("Copied map value: addr=%p", map_obj
);
309 struct bt_value
*(* const copy_funcs
[])(const struct bt_value
*) = {
310 [BT_VALUE_TYPE_NULL
] = bt_value_null_copy
,
311 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_copy
,
312 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_copy
,
313 [BT_VALUE_TYPE_REAL
] = bt_value_real_copy
,
314 [BT_VALUE_TYPE_STRING
] = bt_value_string_copy
,
315 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_copy
,
316 [BT_VALUE_TYPE_MAP
] = bt_value_map_copy
,
320 bt_bool
bt_value_null_compare(const struct bt_value
*object_a
,
321 const struct bt_value
*object_b
)
324 * Always BT_TRUE since bt_value_compare() already checks if both
325 * object_a and object_b have the same type, and in the case of
326 * null value objects, they're always the same if it is so.
332 bt_bool
bt_value_bool_compare(const struct bt_value
*object_a
,
333 const struct bt_value
*object_b
)
335 if (BT_VALUE_TO_BOOL(object_a
)->value
!=
336 BT_VALUE_TO_BOOL(object_b
)->value
) {
337 BT_LOGV("Boolean value objects are different: "
338 "bool-a-val=%d, bool-b-val=%d",
339 BT_VALUE_TO_BOOL(object_a
)->value
,
340 BT_VALUE_TO_BOOL(object_b
)->value
);
348 bt_bool
bt_value_integer_compare(const struct bt_value
*object_a
,
349 const struct bt_value
*object_b
)
351 if (BT_VALUE_TO_INTEGER(object_a
)->value
!=
352 BT_VALUE_TO_INTEGER(object_b
)->value
) {
353 BT_LOGV("Integer value objects are different: "
354 "int-a-val=%" PRId64
", int-b-val=%" PRId64
,
355 BT_VALUE_TO_INTEGER(object_a
)->value
,
356 BT_VALUE_TO_INTEGER(object_b
)->value
);
364 bt_bool
bt_value_real_compare(const struct bt_value
*object_a
,
365 const struct bt_value
*object_b
)
367 if (BT_VALUE_TO_REAL(object_a
)->value
!=
368 BT_VALUE_TO_REAL(object_b
)->value
) {
369 BT_LOGV("Real number value objects are different: "
370 "real-a-val=%f, real-b-val=%f",
371 BT_VALUE_TO_REAL(object_a
)->value
,
372 BT_VALUE_TO_REAL(object_b
)->value
);
380 bt_bool
bt_value_string_compare(const struct bt_value
*object_a
,
381 const struct bt_value
*object_b
)
383 if (strcmp(BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
384 BT_VALUE_TO_STRING(object_b
)->gstr
->str
) != 0) {
385 BT_LOGV("String value objects are different: "
386 "string-a-val=\"%s\", string-b-val=\"%s\"",
387 BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
388 BT_VALUE_TO_STRING(object_b
)->gstr
->str
);
396 bt_bool
bt_value_array_compare(const struct bt_value
*object_a
,
397 const struct bt_value
*object_b
)
400 bt_bool ret
= BT_TRUE
;
401 const struct bt_value_array
*array_obj_a
=
402 BT_VALUE_TO_ARRAY(object_a
);
404 if (bt_value_array_get_size(object_a
) !=
405 bt_value_array_get_size(object_b
)) {
406 BT_LOGV("Array values are different: size mismatch "
407 "value-a-addr=%p, value-b-addr=%p, "
408 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
410 bt_value_array_get_size(object_a
),
411 bt_value_array_get_size(object_b
));
416 for (i
= 0; i
< array_obj_a
->garray
->len
; ++i
) {
417 const struct bt_value
*element_obj_a
;
418 const struct bt_value
*element_obj_b
;
420 element_obj_a
= bt_value_array_borrow_element_by_index_const(
422 element_obj_b
= bt_value_array_borrow_element_by_index_const(
425 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
426 BT_LOGV("Array values's elements are different: "
427 "value-a-addr=%p, value-b-addr=%p, index=%d",
428 element_obj_a
, element_obj_b
, i
);
439 bt_bool
bt_value_map_compare(const struct bt_value
*object_a
,
440 const struct bt_value
*object_b
)
442 bt_bool ret
= BT_TRUE
;
444 gpointer key
, element_obj_a
;
445 const struct bt_value_map
*map_obj_a
= BT_VALUE_TO_MAP(object_a
);
447 if (bt_value_map_get_size(object_a
) !=
448 bt_value_map_get_size(object_b
)) {
449 BT_LOGV("Map values are different: size mismatch "
450 "value-a-addr=%p, value-b-addr=%p, "
451 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
453 bt_value_map_get_size(object_a
),
454 bt_value_map_get_size(object_b
));
459 g_hash_table_iter_init(&iter
, map_obj_a
->ght
);
461 while (g_hash_table_iter_next(&iter
, &key
, &element_obj_a
)) {
462 const struct bt_value
*element_obj_b
;
463 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
465 element_obj_b
= bt_value_map_borrow_entry_value_const(object_b
,
468 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
469 BT_LOGV("Map values's elements are different: "
470 "value-a-addr=%p, value-b-addr=%p, key=\"%s\"",
471 element_obj_a
, element_obj_b
, key_str
);
482 bt_bool (* const compare_funcs
[])(const struct bt_value
*,
483 const struct bt_value
*) = {
484 [BT_VALUE_TYPE_NULL
] = bt_value_null_compare
,
485 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_compare
,
486 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_compare
,
487 [BT_VALUE_TYPE_REAL
] = bt_value_real_compare
,
488 [BT_VALUE_TYPE_STRING
] = bt_value_string_compare
,
489 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_compare
,
490 [BT_VALUE_TYPE_MAP
] = bt_value_map_compare
,
494 void bt_value_null_freeze(struct bt_value
*object
)
499 void bt_value_generic_freeze(struct bt_value
*object
)
501 object
->frozen
= BT_TRUE
;
505 void bt_value_array_freeze(struct bt_value
*object
)
508 struct bt_value_array
*typed_array_obj
=
509 BT_VALUE_TO_ARRAY(object
);
511 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
512 bt_value_freeze(g_ptr_array_index(typed_array_obj
->garray
, i
));
515 bt_value_generic_freeze(object
);
519 void bt_value_map_freeze(struct bt_value
*object
)
522 gpointer key
, element_obj
;
523 const struct bt_value_map
*map_obj
= BT_VALUE_TO_MAP(object
);
525 g_hash_table_iter_init(&iter
, map_obj
->ght
);
527 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
528 bt_value_freeze(element_obj
);
531 bt_value_generic_freeze(object
);
535 void (* const freeze_funcs
[])(struct bt_value
*) = {
536 [BT_VALUE_TYPE_NULL
] = bt_value_null_freeze
,
537 [BT_VALUE_TYPE_BOOL
] = bt_value_generic_freeze
,
538 [BT_VALUE_TYPE_INTEGER
] = bt_value_generic_freeze
,
539 [BT_VALUE_TYPE_REAL
] = bt_value_generic_freeze
,
540 [BT_VALUE_TYPE_STRING
] = bt_value_generic_freeze
,
541 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_freeze
,
542 [BT_VALUE_TYPE_MAP
] = bt_value_map_freeze
,
546 void bt_value_destroy(struct bt_object
*obj
)
548 struct bt_value
*value
;
550 value
= container_of(obj
, struct bt_value
, base
);
551 BT_LOGD("Destroying value: addr=%p", value
);
553 if (bt_value_is_null(value
)) {
554 BT_LOGD_STR("Not destroying the null value singleton.");
558 if (destroy_funcs
[value
->type
]) {
559 destroy_funcs
[value
->type
](value
);
566 enum bt_value_status
_bt_value_freeze(const struct bt_value
*c_object
)
568 const struct bt_value
*object
= (void *) c_object
;
569 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
573 if (object
->frozen
) {
577 BT_LOGD("Freezing value: addr=%p", object
);
578 freeze_funcs
[object
->type
]((void *) object
);
584 enum bt_value_type
bt_value_get_type(const struct bt_value
*object
)
586 BT_ASSERT_PRE_NON_NULL(object
, "Value object");
591 struct bt_value
bt_value_create_base(enum bt_value_type type
)
593 struct bt_value value
;
596 value
.frozen
= BT_FALSE
;
597 bt_object_init_shared(&value
.base
, bt_value_destroy
);
601 struct bt_value
*bt_value_bool_create_init(bt_bool val
)
603 struct bt_value_bool
*bool_obj
;
605 BT_LOGD("Creating boolean value object: val=%d", val
);
606 bool_obj
= g_new0(struct bt_value_bool
, 1);
608 BT_LOGE_STR("Failed to allocate one boolean value object.");
612 bool_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_BOOL
);
613 bool_obj
->value
= val
;
614 BT_LOGD("Created boolean value object: addr=%p", bool_obj
);
617 return (void *) BT_VALUE_FROM_CONCRETE(bool_obj
);
620 struct bt_value
*bt_value_bool_create(void)
622 return bt_value_bool_create_init(BT_FALSE
);
625 struct bt_value
*bt_value_integer_create_init(int64_t val
)
627 struct bt_value_integer
*integer_obj
;
629 BT_LOGD("Creating integer value object: val=%" PRId64
, val
);
630 integer_obj
= g_new0(struct bt_value_integer
, 1);
632 BT_LOGE_STR("Failed to allocate one integer value object.");
636 integer_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_INTEGER
);
637 integer_obj
->value
= val
;
638 BT_LOGD("Created integer value object: addr=%p",
642 return (void *) BT_VALUE_FROM_CONCRETE(integer_obj
);
645 struct bt_value
*bt_value_integer_create(void)
647 return bt_value_integer_create_init(0);
650 struct bt_value
*bt_value_real_create_init(double val
)
652 struct bt_value_real
*real_obj
;
654 BT_LOGD("Creating real number value object: val=%f", val
);
655 real_obj
= g_new0(struct bt_value_real
, 1);
657 BT_LOGE_STR("Failed to allocate one real number value object.");
661 real_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_REAL
);
662 real_obj
->value
= val
;
663 BT_LOGD("Created real number value object: addr=%p",
667 return (void *) BT_VALUE_FROM_CONCRETE(real_obj
);
670 struct bt_value
*bt_value_real_create(void)
672 return bt_value_real_create_init(0.);
675 struct bt_value
*bt_value_string_create_init(const char *val
)
677 struct bt_value_string
*string_obj
= NULL
;
680 BT_LOGW_STR("Invalid parameter: value is NULL.");
684 BT_LOGD("Creating string value object: val-len=%zu", strlen(val
));
685 string_obj
= g_new0(struct bt_value_string
, 1);
687 BT_LOGE_STR("Failed to allocate one string object.");
691 string_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_STRING
);
692 string_obj
->gstr
= g_string_new(val
);
693 if (!string_obj
->gstr
) {
694 BT_LOGE_STR("Failed to allocate a GString.");
700 BT_LOGD("Created string value object: addr=%p",
704 return (void *) BT_VALUE_FROM_CONCRETE(string_obj
);
707 struct bt_value
*bt_value_string_create(void)
709 return bt_value_string_create_init("");
712 struct bt_value
*bt_value_array_create(void)
714 struct bt_value_array
*array_obj
;
716 BT_LOGD_STR("Creating empty array value object.");
717 array_obj
= g_new0(struct bt_value_array
, 1);
719 BT_LOGE_STR("Failed to allocate one array object.");
723 array_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_ARRAY
);
724 array_obj
->garray
= bt_g_ptr_array_new_full(0,
725 (GDestroyNotify
) bt_object_put_ref
);
726 if (!array_obj
->garray
) {
727 BT_LOGE_STR("Failed to allocate a GPtrArray.");
733 BT_LOGD("Created array value object: addr=%p",
737 return (void *) BT_VALUE_FROM_CONCRETE(array_obj
);
740 struct bt_value
*bt_value_map_create(void)
742 struct bt_value_map
*map_obj
;
744 BT_LOGD_STR("Creating empty map value object.");
745 map_obj
= g_new0(struct bt_value_map
, 1);
747 BT_LOGE_STR("Failed to allocate one map object.");
751 map_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_MAP
);
752 map_obj
->ght
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
753 NULL
, (GDestroyNotify
) bt_object_put_ref
);
755 BT_LOGE_STR("Failed to allocate a GHashTable.");
761 BT_LOGD("Created map value object: addr=%p",
765 return (void *) BT_VALUE_FROM_CONCRETE(map_obj
);
768 bt_bool
bt_value_bool_get(const struct bt_value
*bool_obj
)
770 BT_ASSERT_PRE_NON_NULL(bool_obj
, "Value object");
771 BT_ASSERT_PRE_VALUE_IS_TYPE(bool_obj
, BT_VALUE_TYPE_BOOL
);
772 return BT_VALUE_TO_BOOL(bool_obj
)->value
;
775 void bt_value_bool_set(struct bt_value
*bool_obj
, bt_bool val
)
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",
785 int64_t bt_value_integer_get(const struct bt_value
*integer_obj
)
787 BT_ASSERT_PRE_NON_NULL(integer_obj
, "Value object");
788 BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj
, BT_VALUE_TYPE_INTEGER
);
789 return BT_VALUE_TO_INTEGER(integer_obj
)->value
;
792 void bt_value_integer_set(struct bt_value
*integer_obj
,
795 BT_ASSERT_PRE_NON_NULL(integer_obj
, "Value object");
796 BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj
, BT_VALUE_TYPE_INTEGER
);
797 BT_ASSERT_PRE_VALUE_HOT(integer_obj
, "Value object");
798 BT_VALUE_TO_INTEGER(integer_obj
)->value
= val
;
799 BT_LOGV("Set integer value's raw value: value-addr=%p, value=%" PRId64
,
803 double bt_value_real_get(const struct bt_value
*real_obj
)
805 BT_ASSERT_PRE_NON_NULL(real_obj
, "Value object");
806 BT_ASSERT_PRE_VALUE_IS_TYPE(real_obj
, BT_VALUE_TYPE_REAL
);
807 return BT_VALUE_TO_REAL(real_obj
)->value
;
810 void bt_value_real_set(struct bt_value
*real_obj
, double val
)
812 BT_ASSERT_PRE_NON_NULL(real_obj
, "Value object");
813 BT_ASSERT_PRE_VALUE_IS_TYPE(real_obj
, BT_VALUE_TYPE_REAL
);
814 BT_ASSERT_PRE_VALUE_HOT(real_obj
, "Value object");
815 BT_VALUE_TO_REAL(real_obj
)->value
= val
;
816 BT_LOGV("Set real number value's raw value: value-addr=%p, value=%f",
820 const char *bt_value_string_get(const struct bt_value
*string_obj
)
822 BT_ASSERT_PRE_NON_NULL(string_obj
, "Value object");
823 BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj
, BT_VALUE_TYPE_STRING
);
824 return BT_VALUE_TO_STRING(string_obj
)->gstr
->str
;
827 enum bt_value_status
bt_value_string_set(
828 struct bt_value
*string_obj
, const char *val
)
830 BT_ASSERT_PRE_NON_NULL(string_obj
, "Value object");
831 BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj
, BT_VALUE_TYPE_STRING
);
832 BT_ASSERT_PRE_VALUE_HOT(string_obj
, "Value object");
833 g_string_assign(BT_VALUE_TO_STRING(string_obj
)->gstr
, val
);
834 BT_LOGV("Set string value's raw value: value-addr=%p, raw-value-addr=%p",
836 return BT_VALUE_STATUS_OK
;
839 uint64_t bt_value_array_get_size(const struct bt_value
*array_obj
)
841 BT_ASSERT_PRE_NON_NULL(array_obj
, "Value object");
842 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
843 return (uint64_t) BT_VALUE_TO_ARRAY(array_obj
)->garray
->len
;
846 struct bt_value
*bt_value_array_borrow_element_by_index(
847 struct bt_value
*array_obj
, uint64_t index
)
849 struct bt_value_array
*typed_array_obj
=
850 BT_VALUE_TO_ARRAY(array_obj
);
852 BT_ASSERT_PRE_NON_NULL(array_obj
, "Value object");
853 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
854 BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index
,
855 typed_array_obj
->garray
->len
);
856 return g_ptr_array_index(typed_array_obj
->garray
, index
);
859 const struct bt_value
*bt_value_array_borrow_element_by_index_const(
860 const struct bt_value
*array_obj
,
863 return bt_value_array_borrow_element_by_index(
864 (void *) array_obj
, index
);
867 enum bt_value_status
bt_value_array_append_element(
868 struct bt_value
*array_obj
,
869 struct bt_value
*element_obj
)
871 struct bt_value_array
*typed_array_obj
=
872 BT_VALUE_TO_ARRAY(array_obj
);
874 BT_ASSERT_PRE_NON_NULL(array_obj
, "Array value object");
875 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
876 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
877 BT_ASSERT_PRE_VALUE_HOT(array_obj
, "Array value object");
878 g_ptr_array_add(typed_array_obj
->garray
, element_obj
);
879 bt_object_get_ref(element_obj
);
880 BT_LOGV("Appended element to array value: array-value-addr=%p, "
881 "element-value-addr=%p, new-size=%u",
882 array_obj
, element_obj
, typed_array_obj
->garray
->len
);
883 return BT_VALUE_STATUS_OK
;
886 enum bt_value_status
bt_value_array_append_bool_element(
887 struct bt_value
*array_obj
, bt_bool val
)
889 enum bt_value_status ret
;
890 struct bt_value
*bool_obj
= NULL
;
892 bool_obj
= bt_value_bool_create_init(val
);
893 ret
= bt_value_array_append_element(array_obj
,
895 bt_object_put_ref(bool_obj
);
899 enum bt_value_status
bt_value_array_append_integer_element(
900 struct bt_value
*array_obj
, int64_t val
)
902 enum bt_value_status ret
;
903 struct bt_value
*integer_obj
= NULL
;
905 integer_obj
= bt_value_integer_create_init(val
);
906 ret
= bt_value_array_append_element(array_obj
,
907 (void *) integer_obj
);
908 bt_object_put_ref(integer_obj
);
912 enum bt_value_status
bt_value_array_append_real_element(
913 struct bt_value
*array_obj
, double val
)
915 enum bt_value_status ret
;
916 struct bt_value
*real_obj
= NULL
;
918 real_obj
= bt_value_real_create_init(val
);
919 ret
= bt_value_array_append_element(array_obj
,
921 bt_object_put_ref(real_obj
);
925 enum bt_value_status
bt_value_array_append_string_element(
926 struct bt_value
*array_obj
, const char *val
)
928 enum bt_value_status ret
;
929 struct bt_value
*string_obj
= NULL
;
931 string_obj
= bt_value_string_create_init(val
);
932 ret
= bt_value_array_append_element(array_obj
,
933 (void *) string_obj
);
934 bt_object_put_ref(string_obj
);
938 enum bt_value_status
bt_value_array_append_empty_array_element(
939 struct bt_value
*array_obj
)
941 enum bt_value_status ret
;
942 struct bt_value
*empty_array_obj
= NULL
;
944 empty_array_obj
= bt_value_array_create();
945 ret
= bt_value_array_append_element(array_obj
,
946 (void *) empty_array_obj
);
947 bt_object_put_ref(empty_array_obj
);
951 enum bt_value_status
bt_value_array_append_empty_map_element(
952 struct bt_value
*array_obj
)
954 enum bt_value_status ret
;
955 struct bt_value
*map_obj
= NULL
;
957 map_obj
= bt_value_map_create();
958 ret
= bt_value_array_append_element(array_obj
,
960 bt_object_put_ref(map_obj
);
964 enum bt_value_status
bt_value_array_set_element_by_index(
965 struct bt_value
*array_obj
, uint64_t index
,
966 struct bt_value
*element_obj
)
968 struct bt_value_array
*typed_array_obj
=
969 BT_VALUE_TO_ARRAY(array_obj
);
971 BT_ASSERT_PRE_NON_NULL(array_obj
, "Array value object");
972 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
973 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
974 BT_ASSERT_PRE_VALUE_HOT(array_obj
, "Array value object");
975 BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index
,
976 typed_array_obj
->garray
->len
);
977 bt_object_put_ref(g_ptr_array_index(typed_array_obj
->garray
, index
));
978 g_ptr_array_index(typed_array_obj
->garray
, index
) = element_obj
;
979 bt_object_get_ref(element_obj
);
980 BT_LOGV("Set array value's element: array-value-addr=%p, "
981 "index=%" PRIu64
", element-value-addr=%p",
982 array_obj
, index
, element_obj
);
983 return BT_VALUE_STATUS_OK
;
986 uint64_t bt_value_map_get_size(const struct bt_value
*map_obj
)
988 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
989 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
990 return (uint64_t) g_hash_table_size(BT_VALUE_TO_MAP(map_obj
)->ght
);
993 struct bt_value
*bt_value_map_borrow_entry_value(struct bt_value
*map_obj
,
996 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
997 BT_ASSERT_PRE_NON_NULL(key
, "Key");
998 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
999 return g_hash_table_lookup(BT_VALUE_TO_MAP(map_obj
)->ght
,
1000 GUINT_TO_POINTER(g_quark_from_string(key
)));
1003 const struct bt_value
*bt_value_map_borrow_entry_value_const(
1004 const struct bt_value
*map_obj
, const char *key
)
1006 return bt_value_map_borrow_entry_value((void *) map_obj
, key
);
1009 bt_bool
bt_value_map_has_entry(const struct bt_value
*map_obj
, const char *key
)
1011 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
1012 BT_ASSERT_PRE_NON_NULL(key
, "Key");
1013 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1014 return bt_g_hash_table_contains(BT_VALUE_TO_MAP(map_obj
)->ght
,
1015 GUINT_TO_POINTER(g_quark_from_string(key
)));
1018 enum bt_value_status
bt_value_map_insert_entry(
1019 struct bt_value
*map_obj
,
1020 const char *key
, struct bt_value
*element_obj
)
1022 BT_ASSERT_PRE_NON_NULL(map_obj
, "Map value object");
1023 BT_ASSERT_PRE_NON_NULL(key
, "Key");
1024 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
1025 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1026 BT_ASSERT_PRE_VALUE_HOT(map_obj
, "Map value object");
1027 g_hash_table_insert(BT_VALUE_TO_MAP(map_obj
)->ght
,
1028 GUINT_TO_POINTER(g_quark_from_string(key
)), element_obj
);
1029 bt_object_get_ref(element_obj
);
1030 BT_LOGV("Inserted value into map value: map-value-addr=%p, "
1031 "key=\"%s\", element-value-addr=%p",
1032 map_obj
, key
, element_obj
);
1033 return BT_VALUE_STATUS_OK
;
1036 enum bt_value_status
bt_value_map_insert_bool_entry(
1037 struct bt_value
*map_obj
, const char *key
, bt_bool val
)
1039 enum bt_value_status ret
;
1040 struct bt_value
*bool_obj
= NULL
;
1042 bool_obj
= bt_value_bool_create_init(val
);
1043 ret
= bt_value_map_insert_entry(map_obj
, key
,
1045 bt_object_put_ref(bool_obj
);
1049 enum bt_value_status
bt_value_map_insert_integer_entry(
1050 struct bt_value
*map_obj
, const char *key
, int64_t val
)
1052 enum bt_value_status ret
;
1053 struct bt_value
*integer_obj
= NULL
;
1055 integer_obj
= bt_value_integer_create_init(val
);
1056 ret
= bt_value_map_insert_entry(map_obj
, key
,
1057 (void *) integer_obj
);
1058 bt_object_put_ref(integer_obj
);
1062 enum bt_value_status
bt_value_map_insert_real_entry(
1063 struct bt_value
*map_obj
, const char *key
, double val
)
1065 enum bt_value_status ret
;
1066 struct bt_value
*real_obj
= NULL
;
1068 real_obj
= bt_value_real_create_init(val
);
1069 ret
= bt_value_map_insert_entry(map_obj
, key
,
1071 bt_object_put_ref(real_obj
);
1075 enum bt_value_status
bt_value_map_insert_string_entry(
1076 struct bt_value
*map_obj
, const char *key
,
1079 enum bt_value_status ret
;
1080 struct bt_value
*string_obj
= NULL
;
1082 string_obj
= bt_value_string_create_init(val
);
1083 ret
= bt_value_map_insert_entry(map_obj
, key
,
1084 (void *) string_obj
);
1085 bt_object_put_ref(string_obj
);
1089 enum bt_value_status
bt_value_map_insert_empty_array_entry(
1090 struct bt_value
*map_obj
, const char *key
)
1092 enum bt_value_status ret
;
1093 struct bt_value
*array_obj
= NULL
;
1095 array_obj
= bt_value_array_create();
1096 ret
= bt_value_map_insert_entry(map_obj
, key
,
1097 (void *) array_obj
);
1098 bt_object_put_ref(array_obj
);
1102 enum bt_value_status
bt_value_map_insert_empty_map_entry(
1103 struct bt_value
*map_obj
, const char *key
)
1105 enum bt_value_status ret
;
1106 struct bt_value
*empty_map_obj
= NULL
;
1108 empty_map_obj
= bt_value_map_create();
1109 ret
= bt_value_map_insert_entry(map_obj
, key
,
1110 (void *) empty_map_obj
);
1111 bt_object_put_ref(empty_map_obj
);
1115 enum bt_value_status
bt_value_map_foreach_entry(struct bt_value
*map_obj
,
1116 bt_value_map_foreach_entry_cb cb
, void *data
)
1118 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1119 gpointer key
, element_obj
;
1120 GHashTableIter iter
;
1121 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1123 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
1124 BT_ASSERT_PRE_NON_NULL(cb
, "Callback");
1125 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1126 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
1128 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
1129 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
1131 if (!cb(key_str
, element_obj
, data
)) {
1132 BT_LOGV("User canceled the loop: key=\"%s\", "
1133 "value-addr=%p, data=%p",
1134 key_str
, element_obj
, data
);
1135 ret
= BT_VALUE_STATUS_CANCELED
;
1143 enum bt_value_status
bt_value_map_foreach_entry_const(
1144 const struct bt_value
*map_obj
,
1145 bt_value_map_foreach_entry_const_cb cb
, void *data
)
1147 return bt_value_map_foreach_entry((void *) map_obj
,
1148 (bt_value_map_foreach_entry_cb
) cb
, data
);
1151 struct extend_map_element_data
{
1152 struct bt_value
*extended_obj
;
1153 enum bt_value_status status
;
1157 bt_bool
extend_map_element(const char *key
,
1158 const struct bt_value
*extension_obj_elem
, void *data
)
1160 bt_bool ret
= BT_TRUE
;
1161 struct extend_map_element_data
*extend_data
= data
;
1162 struct bt_value
*extension_obj_elem_copy
= NULL
;
1164 /* Copy object which is to replace the current one */
1165 extend_data
->status
= bt_value_copy(&extension_obj_elem_copy
,
1166 extension_obj_elem
);
1167 if (extend_data
->status
) {
1168 BT_LOGE("Cannot copy map element: addr=%p",
1169 extension_obj_elem
);
1173 BT_ASSERT(extension_obj_elem_copy
);
1175 /* Replace in extended object */
1176 extend_data
->status
= bt_value_map_insert_entry(
1177 extend_data
->extended_obj
, key
,
1178 (void *) extension_obj_elem_copy
);
1179 if (extend_data
->status
) {
1180 BT_LOGE("Cannot replace value in extended value: key=\"%s\", "
1181 "extended-value-addr=%p, element-value-addr=%p",
1182 key
, extend_data
->extended_obj
,
1183 extension_obj_elem_copy
);
1190 BT_ASSERT(extend_data
->status
!= BT_VALUE_STATUS_OK
);
1194 BT_OBJECT_PUT_REF_AND_RESET(extension_obj_elem_copy
);
1198 enum bt_value_status
bt_value_map_extend(
1199 struct bt_value
**extended_map_obj
,
1200 const struct bt_value
*base_map_obj
,
1201 const struct bt_value
*extension_obj
)
1203 struct extend_map_element_data extend_data
= {
1204 .extended_obj
= NULL
,
1205 .status
= BT_VALUE_STATUS_OK
,
1208 BT_ASSERT_PRE_NON_NULL(base_map_obj
, "Base value object");
1209 BT_ASSERT_PRE_NON_NULL(extension_obj
, "Extension value object");
1210 BT_ASSERT_PRE_NON_NULL(extended_map_obj
,
1211 "Extended value object (output)");
1212 BT_ASSERT_PRE_VALUE_IS_TYPE(base_map_obj
, BT_VALUE_TYPE_MAP
);
1213 BT_ASSERT_PRE_VALUE_IS_TYPE(extension_obj
, BT_VALUE_TYPE_MAP
);
1214 BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p",
1215 base_map_obj
, extension_obj
);
1216 *extended_map_obj
= NULL
;
1218 /* Create copy of base map object to start with */
1219 extend_data
.status
= bt_value_copy(extended_map_obj
, base_map_obj
);
1220 if (extend_data
.status
) {
1221 BT_LOGE("Cannot copy base value: base-value-addr=%p",
1226 BT_ASSERT(extended_map_obj
);
1229 * For each key in the extension map object, replace this key
1230 * in the copied map object.
1232 extend_data
.extended_obj
= *extended_map_obj
;
1234 if (bt_value_map_foreach_entry_const(extension_obj
, extend_map_element
,
1236 BT_LOGE("Cannot iterate on the extension object's elements: "
1237 "extension-value-addr=%p", extension_obj
);
1241 if (extend_data
.status
) {
1242 BT_LOGE("Failed to successfully iterate on the extension object's elements: "
1243 "extension-value-addr=%p", extension_obj
);
1247 BT_LOGD("Extended map value: extended-value-addr=%p",
1252 BT_OBJECT_PUT_REF_AND_RESET(*extended_map_obj
);
1253 *extended_map_obj
= NULL
;
1256 return extend_data
.status
;
1259 enum bt_value_status
bt_value_copy(struct bt_value
**copy_obj
,
1260 const struct bt_value
*object
)
1262 enum bt_value_status status
= BT_VALUE_STATUS_OK
;
1264 BT_ASSERT_PRE_NON_NULL(object
, "Value object");
1265 BT_ASSERT_PRE_NON_NULL(copy_obj
, "Value object copy (output)");
1266 BT_LOGD("Copying value object: addr=%p", object
);
1267 *copy_obj
= copy_funcs
[object
->type
](object
);
1269 BT_LOGD("Copied value object: copy-value-addr=%p",
1272 status
= BT_VALUE_STATUS_NOMEM
;
1274 BT_LOGE_STR("Failed to copy value object.");
1280 bt_bool
bt_value_compare(const struct bt_value
*object_a
,
1281 const struct bt_value
*object_b
)
1283 bt_bool ret
= BT_FALSE
;
1285 BT_ASSERT_PRE_NON_NULL(object_a
, "Value object A");
1286 BT_ASSERT_PRE_NON_NULL(object_b
, "Value object B");
1288 if (object_a
->type
!= object_b
->type
) {
1289 BT_LOGV("Values are different: type mismatch: "
1290 "value-a-addr=%p, value-b-addr=%p, "
1291 "value-a-type=%s, value-b-type=%s",
1293 bt_common_value_type_string(object_a
->type
),
1294 bt_common_value_type_string(object_b
->type
));
1298 ret
= compare_funcs
[object_a
->type
](object_a
, object_b
);