2 * Copyright (c) 2015-2018 Philippe Proulx <pproulx@efficios.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #define BT_LOG_TAG "LIB/VALUE"
24 #include "lib/logging.h"
30 #include "compat/compiler.h"
31 #include "common/common.h"
32 #include <babeltrace2/value-const.h>
33 #include <babeltrace2/value.h>
34 #include "compat/glib.h"
35 #include <babeltrace2/types.h>
36 #include "lib/assert-pre.h"
37 #include "lib/value.h"
38 #include "common/assert.h"
39 #include "func-status.h"
41 #define BT_VALUE_TO_BOOL(_base) ((struct bt_value_bool *) (_base))
42 #define BT_VALUE_TO_INTEGER(_base) ((struct bt_value_integer *) (_base))
43 #define BT_VALUE_TO_REAL(_base) ((struct bt_value_real *) (_base))
44 #define BT_VALUE_TO_STRING(_base) ((struct bt_value_string *) (_base))
45 #define BT_VALUE_TO_ARRAY(_base) ((struct bt_value_array *) (_base))
46 #define BT_VALUE_TO_MAP(_base) ((struct bt_value_map *) (_base))
48 #define BT_ASSERT_PRE_VALUE_IS_TYPE(_value, _type) \
49 BT_ASSERT_PRE(((struct bt_value *) (_value))->type == (_type), \
50 "Value has the wrong type ID: expected-type=%s, " \
51 "%![value-]+v", bt_common_value_type_string(_type), \
54 #define BT_ASSERT_PRE_VALUE_HOT(_value, _name) \
55 BT_ASSERT_PRE_HOT(((struct bt_value *) (_value)), (_name), \
58 #define BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(_index, _count) \
59 BT_ASSERT_PRE((_index) < (_count), \
60 "Index is out of bound: " \
61 "index=%" PRIu64 ", count=%u", (_index), (_count));
64 void bt_value_null_instance_release_func(struct bt_object
*obj
)
66 BT_LOGW("Releasing the null value singleton: addr=%p", obj
);
70 struct bt_value bt_value_null_instance
= {
74 .release_func
= bt_value_null_instance_release_func
,
75 .spec_release_func
= NULL
,
76 .parent_is_owner_listener_func
= NULL
,
79 .type
= BT_VALUE_TYPE_NULL
,
83 struct bt_value
*const bt_value_null
= &bt_value_null_instance
;
86 void bt_value_destroy(struct bt_object
*obj
);
89 void bt_value_string_destroy(struct bt_value
*object
)
91 g_string_free(BT_VALUE_TO_STRING(object
)->gstr
, TRUE
);
92 BT_VALUE_TO_STRING(object
)->gstr
= NULL
;
96 void bt_value_array_destroy(struct bt_value
*object
)
99 * Pointer array's registered value destructor will take care
100 * of putting each contained object.
102 g_ptr_array_free(BT_VALUE_TO_ARRAY(object
)->garray
, TRUE
);
103 BT_VALUE_TO_ARRAY(object
)->garray
= NULL
;
107 void bt_value_map_destroy(struct bt_value
*object
)
110 * Hash table's registered value destructor will take care of
111 * putting each contained object. Keys are GQuarks and cannot
112 * be destroyed anyway.
114 g_hash_table_destroy(BT_VALUE_TO_MAP(object
)->ght
);
115 BT_VALUE_TO_MAP(object
)->ght
= NULL
;
119 void (* const destroy_funcs
[])(struct bt_value
*) = {
120 [BT_VALUE_TYPE_NULL
] = NULL
,
121 [BT_VALUE_TYPE_BOOL
] = NULL
,
122 [BT_VALUE_TYPE_UNSIGNED_INTEGER
] = NULL
,
123 [BT_VALUE_TYPE_SIGNED_INTEGER
] = NULL
,
124 [BT_VALUE_TYPE_REAL
] = NULL
,
125 [BT_VALUE_TYPE_STRING
] = bt_value_string_destroy
,
126 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_destroy
,
127 [BT_VALUE_TYPE_MAP
] = bt_value_map_destroy
,
131 struct bt_value
*bt_value_null_copy(const struct bt_value
*null_obj
)
133 return (void *) bt_value_null
;
137 struct bt_value
*bt_value_bool_copy(const struct bt_value
*bool_obj
)
139 return bt_value_bool_create_init(
140 BT_VALUE_TO_BOOL(bool_obj
)->value
);
144 struct bt_value
*bt_value_integer_create_init(enum bt_value_type type
,
148 struct bt_value
*bt_value_integer_copy(
149 const struct bt_value
*integer_obj
)
151 return bt_value_integer_create_init(integer_obj
->type
,
152 BT_VALUE_TO_INTEGER(integer_obj
)->value
.u
);
156 struct bt_value
*bt_value_real_copy(const struct bt_value
*real_obj
)
158 return bt_value_real_create_init(
159 BT_VALUE_TO_REAL(real_obj
)->value
);
163 struct bt_value
*bt_value_string_copy(const struct bt_value
*string_obj
)
165 return bt_value_string_create_init(
166 BT_VALUE_TO_STRING(string_obj
)->gstr
->str
);
170 struct bt_value
*bt_value_array_copy(const struct bt_value
*array_obj
)
174 struct bt_value
*copy_obj
;
175 struct bt_value_array
*typed_array_obj
;
177 BT_LOGD("Copying array value: addr=%p", array_obj
);
178 typed_array_obj
= BT_VALUE_TO_ARRAY(array_obj
);
179 copy_obj
= bt_value_array_create();
181 BT_LOGE_STR("Cannot create empty array value.");
185 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
186 struct bt_value
*element_obj_copy
= NULL
;
187 const struct bt_value
*element_obj
=
188 bt_value_array_borrow_element_by_index_const(
191 BT_ASSERT(element_obj
);
192 BT_LOGD("Copying array value's element: element-addr=%p, "
193 "index=%d", element_obj
, i
);
194 ret
= bt_value_copy(element_obj
, &element_obj_copy
);
196 BT_LOGE("Cannot copy array value's element: "
197 "array-addr=%p, index=%d",
199 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
203 BT_ASSERT(element_obj_copy
);
204 ret
= bt_value_array_append_element(copy_obj
,
205 (void *) element_obj_copy
);
206 BT_OBJECT_PUT_REF_AND_RESET(element_obj_copy
);
208 BT_LOGE("Cannot append to array value: addr=%p",
210 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
215 BT_LOGD("Copied array value: original-addr=%p, copy-addr=%p",
216 array_obj
, copy_obj
);
223 struct bt_value
*bt_value_map_copy(const struct bt_value
*map_obj
)
227 gpointer key
, element_obj
;
228 struct bt_value
*copy_obj
;
229 struct bt_value
*element_obj_copy
= NULL
;
230 struct bt_value_map
*typed_map_obj
;
232 BT_LOGD("Copying map value: addr=%p", map_obj
);
233 typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
234 copy_obj
= bt_value_map_create();
239 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
241 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
242 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
245 BT_LOGD("Copying map value's element: element-addr=%p, "
246 "key=\"%s\"", element_obj
, key_str
);
247 ret
= bt_value_copy(element_obj
, &element_obj_copy
);
249 BT_LOGE("Cannot copy map value's element: "
250 "map-addr=%p, key=\"%s\"",
252 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
256 BT_ASSERT(element_obj_copy
);
257 ret
= bt_value_map_insert_entry(copy_obj
, key_str
,
258 (void *) element_obj_copy
);
259 BT_OBJECT_PUT_REF_AND_RESET(element_obj_copy
);
261 BT_LOGE("Cannot insert into map value: addr=%p, key=\"%s\"",
263 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
268 BT_LOGD("Copied map value: addr=%p", map_obj
);
275 struct bt_value
*(* const copy_funcs
[])(const struct bt_value
*) = {
276 [BT_VALUE_TYPE_NULL
] = bt_value_null_copy
,
277 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_copy
,
278 [BT_VALUE_TYPE_UNSIGNED_INTEGER
] = bt_value_integer_copy
,
279 [BT_VALUE_TYPE_SIGNED_INTEGER
] = bt_value_integer_copy
,
280 [BT_VALUE_TYPE_REAL
] = bt_value_real_copy
,
281 [BT_VALUE_TYPE_STRING
] = bt_value_string_copy
,
282 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_copy
,
283 [BT_VALUE_TYPE_MAP
] = bt_value_map_copy
,
287 bt_bool
bt_value_null_compare(const struct bt_value
*object_a
,
288 const struct bt_value
*object_b
)
291 * Always BT_TRUE since bt_value_compare() already checks if both
292 * object_a and object_b have the same type, and in the case of
293 * null value objects, they're always the same if it is so.
299 bt_bool
bt_value_bool_compare(const struct bt_value
*object_a
,
300 const struct bt_value
*object_b
)
302 if (BT_VALUE_TO_BOOL(object_a
)->value
!=
303 BT_VALUE_TO_BOOL(object_b
)->value
) {
304 BT_LOGT("Boolean value objects are different: "
305 "bool-a-val=%d, bool-b-val=%d",
306 BT_VALUE_TO_BOOL(object_a
)->value
,
307 BT_VALUE_TO_BOOL(object_b
)->value
);
315 bt_bool
bt_value_integer_compare(const struct bt_value
*object_a
,
316 const struct bt_value
*object_b
)
318 if (BT_VALUE_TO_INTEGER(object_a
)->value
.u
!=
319 BT_VALUE_TO_INTEGER(object_b
)->value
.u
) {
320 if (object_a
->type
== BT_VALUE_TYPE_UNSIGNED_INTEGER
) {
321 BT_LOGT("Unsigned integer value objects are different: "
322 "int-a-val=%" PRIu64
", int-b-val=%" PRIu64
,
323 BT_VALUE_TO_INTEGER(object_a
)->value
.u
,
324 BT_VALUE_TO_INTEGER(object_b
)->value
.u
);
326 BT_LOGT("Signed integer value objects are different: "
327 "int-a-val=%" PRId64
", int-b-val=%" PRId64
,
328 BT_VALUE_TO_INTEGER(object_a
)->value
.i
,
329 BT_VALUE_TO_INTEGER(object_b
)->value
.i
);
339 bt_bool
bt_value_real_compare(const struct bt_value
*object_a
,
340 const struct bt_value
*object_b
)
342 if (BT_VALUE_TO_REAL(object_a
)->value
!=
343 BT_VALUE_TO_REAL(object_b
)->value
) {
344 BT_LOGT("Real number value objects are different: "
345 "real-a-val=%f, real-b-val=%f",
346 BT_VALUE_TO_REAL(object_a
)->value
,
347 BT_VALUE_TO_REAL(object_b
)->value
);
355 bt_bool
bt_value_string_compare(const struct bt_value
*object_a
,
356 const struct bt_value
*object_b
)
358 if (strcmp(BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
359 BT_VALUE_TO_STRING(object_b
)->gstr
->str
) != 0) {
360 BT_LOGT("String value objects are different: "
361 "string-a-val=\"%s\", string-b-val=\"%s\"",
362 BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
363 BT_VALUE_TO_STRING(object_b
)->gstr
->str
);
371 bt_bool
bt_value_array_compare(const struct bt_value
*object_a
,
372 const struct bt_value
*object_b
)
375 bt_bool ret
= BT_TRUE
;
376 const struct bt_value_array
*array_obj_a
=
377 BT_VALUE_TO_ARRAY(object_a
);
379 if (bt_value_array_get_size(object_a
) !=
380 bt_value_array_get_size(object_b
)) {
381 BT_LOGT("Array values are different: size mismatch "
382 "value-a-addr=%p, value-b-addr=%p, "
383 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
385 bt_value_array_get_size(object_a
),
386 bt_value_array_get_size(object_b
));
391 for (i
= 0; i
< array_obj_a
->garray
->len
; ++i
) {
392 const struct bt_value
*element_obj_a
;
393 const struct bt_value
*element_obj_b
;
395 element_obj_a
= bt_value_array_borrow_element_by_index_const(
397 element_obj_b
= bt_value_array_borrow_element_by_index_const(
400 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
401 BT_LOGT("Array values's elements are different: "
402 "value-a-addr=%p, value-b-addr=%p, index=%d",
403 element_obj_a
, element_obj_b
, i
);
414 bt_bool
bt_value_map_compare(const struct bt_value
*object_a
,
415 const struct bt_value
*object_b
)
417 bt_bool ret
= BT_TRUE
;
419 gpointer key
, element_obj_a
;
420 const struct bt_value_map
*map_obj_a
= BT_VALUE_TO_MAP(object_a
);
422 if (bt_value_map_get_size(object_a
) !=
423 bt_value_map_get_size(object_b
)) {
424 BT_LOGT("Map values are different: size mismatch "
425 "value-a-addr=%p, value-b-addr=%p, "
426 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
428 bt_value_map_get_size(object_a
),
429 bt_value_map_get_size(object_b
));
434 g_hash_table_iter_init(&iter
, map_obj_a
->ght
);
436 while (g_hash_table_iter_next(&iter
, &key
, &element_obj_a
)) {
437 const struct bt_value
*element_obj_b
;
438 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
440 element_obj_b
= bt_value_map_borrow_entry_value_const(object_b
,
443 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
444 BT_LOGT("Map values's elements are different: "
445 "value-a-addr=%p, value-b-addr=%p, key=\"%s\"",
446 element_obj_a
, element_obj_b
, key_str
);
457 bt_bool (* const compare_funcs
[])(const struct bt_value
*,
458 const struct bt_value
*) = {
459 [BT_VALUE_TYPE_NULL
] = bt_value_null_compare
,
460 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_compare
,
461 [BT_VALUE_TYPE_UNSIGNED_INTEGER
] = bt_value_integer_compare
,
462 [BT_VALUE_TYPE_SIGNED_INTEGER
] = bt_value_integer_compare
,
463 [BT_VALUE_TYPE_REAL
] = bt_value_real_compare
,
464 [BT_VALUE_TYPE_STRING
] = bt_value_string_compare
,
465 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_compare
,
466 [BT_VALUE_TYPE_MAP
] = bt_value_map_compare
,
470 void bt_value_null_freeze(struct bt_value
*object
)
475 void bt_value_generic_freeze(struct bt_value
*object
)
477 object
->frozen
= BT_TRUE
;
481 void bt_value_array_freeze(struct bt_value
*object
)
484 struct bt_value_array
*typed_array_obj
=
485 BT_VALUE_TO_ARRAY(object
);
487 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
488 bt_value_freeze(g_ptr_array_index(typed_array_obj
->garray
, i
));
491 bt_value_generic_freeze(object
);
495 void bt_value_map_freeze(struct bt_value
*object
)
498 gpointer key
, element_obj
;
499 const struct bt_value_map
*map_obj
= BT_VALUE_TO_MAP(object
);
501 g_hash_table_iter_init(&iter
, map_obj
->ght
);
503 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
504 bt_value_freeze(element_obj
);
507 bt_value_generic_freeze(object
);
511 void (* const freeze_funcs
[])(struct bt_value
*) = {
512 [BT_VALUE_TYPE_NULL
] = bt_value_null_freeze
,
513 [BT_VALUE_TYPE_BOOL
] = bt_value_generic_freeze
,
514 [BT_VALUE_TYPE_UNSIGNED_INTEGER
] = bt_value_generic_freeze
,
515 [BT_VALUE_TYPE_SIGNED_INTEGER
] = bt_value_generic_freeze
,
516 [BT_VALUE_TYPE_REAL
] = bt_value_generic_freeze
,
517 [BT_VALUE_TYPE_STRING
] = bt_value_generic_freeze
,
518 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_freeze
,
519 [BT_VALUE_TYPE_MAP
] = bt_value_map_freeze
,
523 void bt_value_destroy(struct bt_object
*obj
)
525 struct bt_value
*value
;
527 value
= container_of(obj
, struct bt_value
, base
);
528 BT_LOGD("Destroying value: addr=%p", value
);
530 if (bt_value_is_null(value
)) {
531 BT_LOGD_STR("Not destroying the null value singleton.");
535 if (destroy_funcs
[value
->type
]) {
536 destroy_funcs
[value
->type
](value
);
543 void _bt_value_freeze(const struct bt_value
*c_object
)
545 const struct bt_value
*object
= (void *) c_object
;
549 if (object
->frozen
) {
553 BT_LOGD("Freezing value: addr=%p", object
);
554 freeze_funcs
[object
->type
]((void *) object
);
560 enum bt_value_type
bt_value_get_type(const struct bt_value
*object
)
562 BT_ASSERT_PRE_NON_NULL(object
, "Value object");
567 struct bt_value
bt_value_create_base(enum bt_value_type type
)
569 struct bt_value value
;
572 value
.frozen
= BT_FALSE
;
573 bt_object_init_shared(&value
.base
, bt_value_destroy
);
577 struct bt_value
*bt_value_bool_create_init(bt_bool val
)
579 struct bt_value_bool
*bool_obj
;
581 BT_LOGD("Creating boolean value object: val=%d", val
);
582 bool_obj
= g_new0(struct bt_value_bool
, 1);
584 BT_LOGE_STR("Failed to allocate one boolean value object.");
588 bool_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_BOOL
);
589 bool_obj
->value
= val
;
590 BT_LOGD("Created boolean value object: addr=%p", bool_obj
);
593 return (void *) bool_obj
;
596 struct bt_value
*bt_value_bool_create(void)
598 return bt_value_bool_create_init(BT_FALSE
);
602 struct bt_value
*bt_value_integer_create_init(enum bt_value_type type
,
605 struct bt_value_integer
*integer_obj
;
607 BT_ASSERT(type
== BT_VALUE_TYPE_UNSIGNED_INTEGER
||
608 type
== BT_VALUE_TYPE_SIGNED_INTEGER
);
610 if (type
== BT_VALUE_TYPE_UNSIGNED_INTEGER
) {
611 BT_LOGD("Creating unsigned integer value object: val=%" PRIu64
,
614 BT_LOGD("Creating signed integer value object: val=%" PRId64
,
618 integer_obj
= g_new0(struct bt_value_integer
, 1);
620 BT_LOGE_STR("Failed to allocate one integer value object.");
624 integer_obj
->base
= bt_value_create_base(type
);
625 integer_obj
->value
.u
= uval
;
626 BT_LOGD("Created %ssigned integer value object: addr=%p",
627 type
== BT_VALUE_TYPE_UNSIGNED_INTEGER
? "un" : "",
631 return (void *) integer_obj
;
634 struct bt_value
*bt_value_unsigned_integer_create_init(uint64_t val
)
636 return bt_value_integer_create_init(BT_VALUE_TYPE_UNSIGNED_INTEGER
,
640 struct bt_value
*bt_value_unsigned_integer_create(void)
642 return bt_value_unsigned_integer_create_init(0);
645 struct bt_value
*bt_value_signed_integer_create_init(int64_t val
)
647 return bt_value_integer_create_init(BT_VALUE_TYPE_SIGNED_INTEGER
,
651 struct bt_value
*bt_value_signed_integer_create(void)
653 return bt_value_signed_integer_create_init(0);
656 struct bt_value
*bt_value_real_create_init(double val
)
658 struct bt_value_real
*real_obj
;
660 BT_LOGD("Creating real number value object: val=%f", val
);
661 real_obj
= g_new0(struct bt_value_real
, 1);
663 BT_LOGE_STR("Failed to allocate one real number value object.");
667 real_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_REAL
);
668 real_obj
->value
= val
;
669 BT_LOGD("Created real number value object: addr=%p",
673 return (void *) real_obj
;
676 struct bt_value
*bt_value_real_create(void)
678 return bt_value_real_create_init(0.);
681 struct bt_value
*bt_value_string_create_init(const char *val
)
683 struct bt_value_string
*string_obj
= NULL
;
686 BT_LOGW_STR("Invalid parameter: value is NULL.");
690 BT_LOGD("Creating string value object: val-len=%zu", strlen(val
));
691 string_obj
= g_new0(struct bt_value_string
, 1);
693 BT_LOGE_STR("Failed to allocate one string object.");
697 string_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_STRING
);
698 string_obj
->gstr
= g_string_new(val
);
699 if (!string_obj
->gstr
) {
700 BT_LOGE_STR("Failed to allocate a GString.");
706 BT_LOGD("Created string value object: addr=%p",
710 return (void *) string_obj
;
713 struct bt_value
*bt_value_string_create(void)
715 return bt_value_string_create_init("");
718 struct bt_value
*bt_value_array_create(void)
720 struct bt_value_array
*array_obj
;
722 BT_LOGD_STR("Creating empty array value object.");
723 array_obj
= g_new0(struct bt_value_array
, 1);
725 BT_LOGE_STR("Failed to allocate one array object.");
729 array_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_ARRAY
);
730 array_obj
->garray
= bt_g_ptr_array_new_full(0,
731 (GDestroyNotify
) bt_object_put_ref
);
732 if (!array_obj
->garray
) {
733 BT_LOGE_STR("Failed to allocate a GPtrArray.");
739 BT_LOGD("Created array value object: addr=%p",
743 return (void *) array_obj
;
746 struct bt_value
*bt_value_map_create(void)
748 struct bt_value_map
*map_obj
;
750 BT_LOGD_STR("Creating empty map value object.");
751 map_obj
= g_new0(struct bt_value_map
, 1);
753 BT_LOGE_STR("Failed to allocate one map object.");
757 map_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_MAP
);
758 map_obj
->ght
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
759 NULL
, (GDestroyNotify
) bt_object_put_ref
);
761 BT_LOGE_STR("Failed to allocate a GHashTable.");
767 BT_LOGD("Created map value object: addr=%p",
771 return (void *) map_obj
;
774 bt_bool
bt_value_bool_get(const struct bt_value
*bool_obj
)
776 BT_ASSERT_PRE_NON_NULL(bool_obj
, "Value object");
777 BT_ASSERT_PRE_VALUE_IS_TYPE(bool_obj
, BT_VALUE_TYPE_BOOL
);
778 return BT_VALUE_TO_BOOL(bool_obj
)->value
;
781 void bt_value_bool_set(struct bt_value
*bool_obj
, bt_bool val
)
783 BT_ASSERT_PRE_NON_NULL(bool_obj
, "Value object");
784 BT_ASSERT_PRE_VALUE_IS_TYPE(bool_obj
, BT_VALUE_TYPE_BOOL
);
785 BT_ASSERT_PRE_VALUE_HOT(bool_obj
, "Value object");
786 BT_VALUE_TO_BOOL(bool_obj
)->value
= val
;
787 BT_LOGT("Set boolean value's raw value: value-addr=%p, value=%d",
791 uint64_t bt_value_unsigned_integer_get(const struct bt_value
*integer_obj
)
793 BT_ASSERT_PRE_NON_NULL(integer_obj
, "Value object");
794 BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj
,
795 BT_VALUE_TYPE_UNSIGNED_INTEGER
);
796 return BT_VALUE_TO_INTEGER(integer_obj
)->value
.u
;
799 int64_t bt_value_signed_integer_get(const struct bt_value
*integer_obj
)
801 BT_ASSERT_PRE_NON_NULL(integer_obj
, "Value object");
802 BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj
,
803 BT_VALUE_TYPE_SIGNED_INTEGER
);
804 return BT_VALUE_TO_INTEGER(integer_obj
)->value
.i
;
808 void bt_value_integer_set(struct bt_value
*integer_obj
,
809 enum bt_value_type expected_type
, uint64_t uval
)
811 BT_ASSERT_PRE_NON_NULL(integer_obj
, "Value object");
812 BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj
, expected_type
);
813 BT_ASSERT_PRE_VALUE_HOT(integer_obj
, "Value object");
814 BT_VALUE_TO_INTEGER(integer_obj
)->value
.u
= uval
;
817 void bt_value_unsigned_integer_set(struct bt_value
*integer_obj
,
820 bt_value_integer_set(integer_obj
, BT_VALUE_TYPE_UNSIGNED_INTEGER
, val
);
821 BT_LOGT("Set unsigned integer value's raw value: "
822 "value-addr=%p, value=%" PRIu64
, integer_obj
, val
);
825 void bt_value_signed_integer_set(struct bt_value
*integer_obj
,
828 bt_value_integer_set(integer_obj
, BT_VALUE_TYPE_SIGNED_INTEGER
,
830 BT_LOGT("Set signed integer value's raw value: "
831 "value-addr=%p, value=%" PRId64
, integer_obj
, val
);
834 double bt_value_real_get(const struct bt_value
*real_obj
)
836 BT_ASSERT_PRE_NON_NULL(real_obj
, "Value object");
837 BT_ASSERT_PRE_VALUE_IS_TYPE(real_obj
, BT_VALUE_TYPE_REAL
);
838 return BT_VALUE_TO_REAL(real_obj
)->value
;
841 void bt_value_real_set(struct bt_value
*real_obj
, double val
)
843 BT_ASSERT_PRE_NON_NULL(real_obj
, "Value object");
844 BT_ASSERT_PRE_VALUE_IS_TYPE(real_obj
, BT_VALUE_TYPE_REAL
);
845 BT_ASSERT_PRE_VALUE_HOT(real_obj
, "Value object");
846 BT_VALUE_TO_REAL(real_obj
)->value
= val
;
847 BT_LOGT("Set real number value's raw value: value-addr=%p, value=%f",
851 const char *bt_value_string_get(const struct bt_value
*string_obj
)
853 BT_ASSERT_PRE_NON_NULL(string_obj
, "Value object");
854 BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj
, BT_VALUE_TYPE_STRING
);
855 return BT_VALUE_TO_STRING(string_obj
)->gstr
->str
;
858 enum bt_value_string_set_status
bt_value_string_set(
859 struct bt_value
*string_obj
, const char *val
)
861 BT_ASSERT_PRE_NON_NULL(string_obj
, "Value object");
862 BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj
, BT_VALUE_TYPE_STRING
);
863 BT_ASSERT_PRE_VALUE_HOT(string_obj
, "Value object");
864 g_string_assign(BT_VALUE_TO_STRING(string_obj
)->gstr
, val
);
865 BT_LOGT("Set string value's raw value: value-addr=%p, raw-value-addr=%p",
867 return BT_FUNC_STATUS_OK
;
870 uint64_t bt_value_array_get_size(const struct bt_value
*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 return (uint64_t) BT_VALUE_TO_ARRAY(array_obj
)->garray
->len
;
877 struct bt_value
*bt_value_array_borrow_element_by_index(
878 struct bt_value
*array_obj
, uint64_t index
)
880 struct bt_value_array
*typed_array_obj
=
881 BT_VALUE_TO_ARRAY(array_obj
);
883 BT_ASSERT_PRE_NON_NULL(array_obj
, "Value object");
884 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
885 BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index
,
886 typed_array_obj
->garray
->len
);
887 return g_ptr_array_index(typed_array_obj
->garray
, index
);
890 const struct bt_value
*bt_value_array_borrow_element_by_index_const(
891 const struct bt_value
*array_obj
,
894 return bt_value_array_borrow_element_by_index(
895 (void *) array_obj
, index
);
898 enum bt_value_array_append_element_status
bt_value_array_append_element(
899 struct bt_value
*array_obj
,
900 struct bt_value
*element_obj
)
902 struct bt_value_array
*typed_array_obj
=
903 BT_VALUE_TO_ARRAY(array_obj
);
905 BT_ASSERT_PRE_NON_NULL(array_obj
, "Array value object");
906 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
907 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
908 BT_ASSERT_PRE_VALUE_HOT(array_obj
, "Array value object");
909 g_ptr_array_add(typed_array_obj
->garray
, element_obj
);
910 bt_object_get_ref(element_obj
);
911 BT_LOGT("Appended element to array value: array-value-addr=%p, "
912 "element-value-addr=%p, new-size=%u",
913 array_obj
, element_obj
, typed_array_obj
->garray
->len
);
914 return BT_FUNC_STATUS_OK
;
917 enum bt_value_array_append_element_status
918 bt_value_array_append_bool_element(struct bt_value
*array_obj
, bt_bool val
)
920 enum bt_value_array_append_element_status ret
;
921 struct bt_value
*bool_obj
= NULL
;
923 bool_obj
= bt_value_bool_create_init(val
);
924 ret
= bt_value_array_append_element(array_obj
,
926 bt_object_put_ref(bool_obj
);
930 enum bt_value_array_append_element_status
931 bt_value_array_append_unsigned_integer_element(struct bt_value
*array_obj
,
934 enum bt_value_array_append_element_status ret
;
935 struct bt_value
*integer_obj
= NULL
;
937 integer_obj
= bt_value_unsigned_integer_create_init(val
);
938 ret
= bt_value_array_append_element(array_obj
,
939 (void *) integer_obj
);
940 bt_object_put_ref(integer_obj
);
944 enum bt_value_array_append_element_status
945 bt_value_array_append_signed_integer_element(struct bt_value
*array_obj
,
948 enum bt_value_array_append_element_status ret
;
949 struct bt_value
*integer_obj
= NULL
;
951 integer_obj
= bt_value_signed_integer_create_init(val
);
952 ret
= bt_value_array_append_element(array_obj
,
953 (void *) integer_obj
);
954 bt_object_put_ref(integer_obj
);
958 enum bt_value_array_append_element_status
959 bt_value_array_append_real_element(struct bt_value
*array_obj
, double val
)
961 enum bt_value_array_append_element_status ret
;
962 struct bt_value
*real_obj
= NULL
;
964 real_obj
= bt_value_real_create_init(val
);
965 ret
= bt_value_array_append_element(array_obj
,
967 bt_object_put_ref(real_obj
);
971 enum bt_value_array_append_element_status
972 bt_value_array_append_string_element(struct bt_value
*array_obj
,
975 enum bt_value_array_append_element_status ret
;
976 struct bt_value
*string_obj
= NULL
;
978 string_obj
= bt_value_string_create_init(val
);
979 ret
= bt_value_array_append_element(array_obj
,
980 (void *) string_obj
);
981 bt_object_put_ref(string_obj
);
985 enum bt_value_array_append_element_status
986 bt_value_array_append_empty_array_element(struct bt_value
*array_obj
)
988 enum bt_value_array_append_element_status ret
;
989 struct bt_value
*empty_array_obj
= NULL
;
991 empty_array_obj
= bt_value_array_create();
992 ret
= bt_value_array_append_element(array_obj
,
993 (void *) empty_array_obj
);
994 bt_object_put_ref(empty_array_obj
);
998 enum bt_value_array_append_element_status
999 bt_value_array_append_empty_map_element(struct bt_value
*array_obj
)
1001 enum bt_value_array_append_element_status ret
;
1002 struct bt_value
*map_obj
= NULL
;
1004 map_obj
= bt_value_map_create();
1005 ret
= bt_value_array_append_element(array_obj
,
1007 bt_object_put_ref(map_obj
);
1011 enum bt_value_array_set_element_by_index_status
1012 bt_value_array_set_element_by_index(struct bt_value
*array_obj
, uint64_t index
,
1013 struct bt_value
*element_obj
)
1015 struct bt_value_array
*typed_array_obj
=
1016 BT_VALUE_TO_ARRAY(array_obj
);
1018 BT_ASSERT_PRE_NON_NULL(array_obj
, "Array value object");
1019 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
1020 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
1021 BT_ASSERT_PRE_VALUE_HOT(array_obj
, "Array value object");
1022 BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index
,
1023 typed_array_obj
->garray
->len
);
1024 bt_object_put_ref(g_ptr_array_index(typed_array_obj
->garray
, index
));
1025 g_ptr_array_index(typed_array_obj
->garray
, index
) = element_obj
;
1026 bt_object_get_ref(element_obj
);
1027 BT_LOGT("Set array value's element: array-value-addr=%p, "
1028 "index=%" PRIu64
", element-value-addr=%p",
1029 array_obj
, index
, element_obj
);
1030 return BT_FUNC_STATUS_OK
;
1033 uint64_t bt_value_map_get_size(const struct bt_value
*map_obj
)
1035 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
1036 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1037 return (uint64_t) g_hash_table_size(BT_VALUE_TO_MAP(map_obj
)->ght
);
1040 struct bt_value
*bt_value_map_borrow_entry_value(struct bt_value
*map_obj
,
1043 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
1044 BT_ASSERT_PRE_NON_NULL(key
, "Key");
1045 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1046 return g_hash_table_lookup(BT_VALUE_TO_MAP(map_obj
)->ght
,
1047 GUINT_TO_POINTER(g_quark_from_string(key
)));
1050 const struct bt_value
*bt_value_map_borrow_entry_value_const(
1051 const struct bt_value
*map_obj
, const char *key
)
1053 return bt_value_map_borrow_entry_value((void *) map_obj
, key
);
1056 bt_bool
bt_value_map_has_entry(const struct bt_value
*map_obj
, const char *key
)
1058 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
1059 BT_ASSERT_PRE_NON_NULL(key
, "Key");
1060 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1061 return bt_g_hash_table_contains(BT_VALUE_TO_MAP(map_obj
)->ght
,
1062 GUINT_TO_POINTER(g_quark_from_string(key
)));
1065 enum bt_value_map_insert_entry_status
bt_value_map_insert_entry(
1066 struct bt_value
*map_obj
, const char *key
,
1067 struct bt_value
*element_obj
)
1069 BT_ASSERT_PRE_NON_NULL(map_obj
, "Map value object");
1070 BT_ASSERT_PRE_NON_NULL(key
, "Key");
1071 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
1072 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1073 BT_ASSERT_PRE_VALUE_HOT(map_obj
, "Map value object");
1074 g_hash_table_insert(BT_VALUE_TO_MAP(map_obj
)->ght
,
1075 GUINT_TO_POINTER(g_quark_from_string(key
)), element_obj
);
1076 bt_object_get_ref(element_obj
);
1077 BT_LOGT("Inserted value into map value: map-value-addr=%p, "
1078 "key=\"%s\", element-value-addr=%p",
1079 map_obj
, key
, element_obj
);
1080 return BT_FUNC_STATUS_OK
;
1083 enum bt_value_map_insert_entry_status
bt_value_map_insert_bool_entry(
1084 struct bt_value
*map_obj
, const char *key
, bt_bool val
)
1086 enum bt_value_map_insert_entry_status ret
;
1087 struct bt_value
*bool_obj
= NULL
;
1089 bool_obj
= bt_value_bool_create_init(val
);
1090 ret
= bt_value_map_insert_entry(map_obj
, key
,
1092 bt_object_put_ref(bool_obj
);
1096 enum bt_value_map_insert_entry_status
1097 bt_value_map_insert_unsigned_integer_entry(struct bt_value
*map_obj
,
1098 const char *key
, uint64_t val
)
1100 enum bt_value_map_insert_entry_status ret
;
1101 struct bt_value
*integer_obj
= NULL
;
1103 integer_obj
= bt_value_unsigned_integer_create_init(val
);
1104 ret
= bt_value_map_insert_entry(map_obj
, key
,
1105 (void *) integer_obj
);
1106 bt_object_put_ref(integer_obj
);
1110 enum bt_value_map_insert_entry_status
1111 bt_value_map_insert_signed_integer_entry(struct bt_value
*map_obj
,
1112 const char *key
, int64_t val
)
1114 enum bt_value_map_insert_entry_status ret
;
1115 struct bt_value
*integer_obj
= NULL
;
1117 integer_obj
= bt_value_signed_integer_create_init(val
);
1118 ret
= bt_value_map_insert_entry(map_obj
, key
,
1119 (void *) integer_obj
);
1120 bt_object_put_ref(integer_obj
);
1124 enum bt_value_map_insert_entry_status
bt_value_map_insert_real_entry(
1125 struct bt_value
*map_obj
, const char *key
, double val
)
1127 enum bt_value_map_insert_entry_status ret
;
1128 struct bt_value
*real_obj
= NULL
;
1130 real_obj
= bt_value_real_create_init(val
);
1131 ret
= bt_value_map_insert_entry(map_obj
, key
,
1133 bt_object_put_ref(real_obj
);
1137 enum bt_value_map_insert_entry_status
bt_value_map_insert_string_entry(
1138 struct bt_value
*map_obj
, const char *key
,
1141 enum bt_value_map_insert_entry_status ret
;
1142 struct bt_value
*string_obj
= NULL
;
1144 string_obj
= bt_value_string_create_init(val
);
1145 ret
= bt_value_map_insert_entry(map_obj
, key
,
1146 (void *) string_obj
);
1147 bt_object_put_ref(string_obj
);
1151 enum bt_value_map_insert_entry_status
1152 bt_value_map_insert_empty_array_entry(
1153 struct bt_value
*map_obj
, const char *key
)
1155 enum bt_value_map_insert_entry_status ret
;
1156 struct bt_value
*array_obj
= NULL
;
1158 array_obj
= bt_value_array_create();
1159 ret
= bt_value_map_insert_entry(map_obj
, key
,
1160 (void *) array_obj
);
1161 bt_object_put_ref(array_obj
);
1165 enum bt_value_map_insert_entry_status
1166 bt_value_map_insert_empty_map_entry(struct bt_value
*map_obj
, const char *key
)
1168 enum bt_value_map_insert_entry_status ret
;
1169 struct bt_value
*empty_map_obj
= NULL
;
1171 empty_map_obj
= bt_value_map_create();
1172 ret
= bt_value_map_insert_entry(map_obj
, key
,
1173 (void *) empty_map_obj
);
1174 bt_object_put_ref(empty_map_obj
);
1178 enum bt_value_map_foreach_entry_status
bt_value_map_foreach_entry(
1179 struct bt_value
*map_obj
, bt_value_map_foreach_entry_func func
,
1182 enum bt_value_map_foreach_entry_status ret
= BT_FUNC_STATUS_OK
;
1183 gpointer key
, element_obj
;
1184 GHashTableIter iter
;
1185 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1187 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
1188 BT_ASSERT_PRE_NON_NULL(func
, "Callback");
1189 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1190 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
1192 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
1193 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
1195 if (!func(key_str
, element_obj
, data
)) {
1196 BT_LOGT("User canceled the loop: key=\"%s\", "
1197 "value-addr=%p, data=%p",
1198 key_str
, element_obj
, data
);
1199 ret
= BT_FUNC_STATUS_CANCELED
;
1207 enum bt_value_map_foreach_entry_const_status
bt_value_map_foreach_entry_const(
1208 const struct bt_value
*map_obj
,
1209 bt_value_map_foreach_entry_const_func func
, void *data
)
1211 return (int) bt_value_map_foreach_entry((void *) map_obj
,
1212 (bt_value_map_foreach_entry_func
) func
, data
);
1215 struct extend_map_element_data
{
1216 struct bt_value
*extended_obj
;
1221 bt_bool
extend_map_element(const char *key
,
1222 const struct bt_value
*extension_obj_elem
, void *data
)
1224 bt_bool ret
= BT_TRUE
;
1225 struct extend_map_element_data
*extend_data
= data
;
1226 struct bt_value
*extension_obj_elem_copy
= NULL
;
1228 /* Copy object which is to replace the current one */
1229 extend_data
->status
= bt_value_copy(extension_obj_elem
,
1230 &extension_obj_elem_copy
);
1231 if (extend_data
->status
) {
1232 BT_LOGE("Cannot copy map element: addr=%p",
1233 extension_obj_elem
);
1237 BT_ASSERT(extension_obj_elem_copy
);
1239 /* Replace in extended object */
1240 extend_data
->status
= bt_value_map_insert_entry(
1241 extend_data
->extended_obj
, key
,
1242 (void *) extension_obj_elem_copy
);
1243 if (extend_data
->status
) {
1244 BT_LOGE("Cannot replace value in extended value: key=\"%s\", "
1245 "extended-value-addr=%p, element-value-addr=%p",
1246 key
, extend_data
->extended_obj
,
1247 extension_obj_elem_copy
);
1254 BT_ASSERT(extend_data
->status
!= BT_FUNC_STATUS_OK
);
1258 BT_OBJECT_PUT_REF_AND_RESET(extension_obj_elem_copy
);
1262 enum bt_value_map_extend_status
bt_value_map_extend(
1263 const struct bt_value
*base_map_obj
,
1264 const struct bt_value
*extension_obj
,
1265 struct bt_value
**extended_map_obj
)
1267 struct extend_map_element_data extend_data
= {
1268 .extended_obj
= NULL
,
1269 .status
= BT_FUNC_STATUS_OK
,
1272 BT_ASSERT_PRE_NON_NULL(base_map_obj
, "Base value object");
1273 BT_ASSERT_PRE_NON_NULL(extension_obj
, "Extension value object");
1274 BT_ASSERT_PRE_NON_NULL(extended_map_obj
,
1275 "Extended value object (output)");
1276 BT_ASSERT_PRE_VALUE_IS_TYPE(base_map_obj
, BT_VALUE_TYPE_MAP
);
1277 BT_ASSERT_PRE_VALUE_IS_TYPE(extension_obj
, BT_VALUE_TYPE_MAP
);
1278 BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p",
1279 base_map_obj
, extension_obj
);
1280 *extended_map_obj
= NULL
;
1282 /* Create copy of base map object to start with */
1283 extend_data
.status
= bt_value_copy(base_map_obj
, extended_map_obj
);
1284 if (extend_data
.status
) {
1285 BT_LOGE("Cannot copy base value: base-value-addr=%p",
1290 BT_ASSERT(extended_map_obj
);
1293 * For each key in the extension map object, replace this key
1294 * in the copied map object.
1296 extend_data
.extended_obj
= *extended_map_obj
;
1298 if (bt_value_map_foreach_entry_const(extension_obj
, extend_map_element
,
1300 BT_LOGE("Cannot iterate on the extension object's elements: "
1301 "extension-value-addr=%p", extension_obj
);
1305 if (extend_data
.status
) {
1306 BT_LOGE("Failed to successfully iterate on the extension object's elements: "
1307 "extension-value-addr=%p", extension_obj
);
1311 BT_LOGD("Extended map value: extended-value-addr=%p",
1316 BT_OBJECT_PUT_REF_AND_RESET(*extended_map_obj
);
1317 *extended_map_obj
= NULL
;
1320 return extend_data
.status
;
1323 enum bt_value_copy_status
bt_value_copy(const struct bt_value
*object
,
1324 struct bt_value
**copy_obj
)
1326 enum bt_value_copy_status status
= BT_FUNC_STATUS_OK
;
1328 BT_ASSERT_PRE_NON_NULL(object
, "Value object");
1329 BT_ASSERT_PRE_NON_NULL(copy_obj
, "Value object copy (output)");
1330 BT_LOGD("Copying value object: addr=%p", object
);
1331 *copy_obj
= copy_funcs
[object
->type
](object
);
1333 BT_LOGD("Copied value object: copy-value-addr=%p",
1336 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1338 BT_LOGE_STR("Failed to copy value object.");
1344 bt_bool
bt_value_compare(const struct bt_value
*object_a
,
1345 const struct bt_value
*object_b
)
1347 bt_bool ret
= BT_FALSE
;
1349 BT_ASSERT_PRE_NON_NULL(object_a
, "Value object A");
1350 BT_ASSERT_PRE_NON_NULL(object_b
, "Value object B");
1352 if (object_a
->type
!= object_b
->type
) {
1353 BT_LOGT("Values are different: type mismatch: "
1354 "value-a-addr=%p, value-b-addr=%p, "
1355 "value-a-type=%s, value-b-type=%s",
1357 bt_common_value_type_string(object_a
->type
),
1358 bt_common_value_type_string(object_b
->type
));
1362 ret
= compare_funcs
[object_a
->type
](object_a
, object_b
);
1368 void bt_value_get_ref(const struct bt_value
*value
)
1370 bt_object_get_ref(value
);
1373 void bt_value_put_ref(const struct bt_value
*value
)
1375 bt_object_put_ref(value
);