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 "VALUES"
24 #include <babeltrace/lib-logging-internal.h>
30 #include <babeltrace/compiler-internal.h>
31 #include <babeltrace/common-internal.h>
32 #include <babeltrace/value-const.h>
33 #include <babeltrace/value.h>
34 #include <babeltrace/compat/glib-internal.h>
35 #include <babeltrace/types.h>
36 #include <babeltrace/assert-pre-internal.h>
37 #include <babeltrace/value-internal.h>
38 #include <babeltrace/assert-internal.h>
40 #define BT_VALUE_FROM_CONCRETE(_concrete) ((struct bt_value *) (_concrete))
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_INTEGER
] = NULL
,
123 [BT_VALUE_TYPE_REAL
] = NULL
,
124 [BT_VALUE_TYPE_STRING
] = bt_value_string_destroy
,
125 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_destroy
,
126 [BT_VALUE_TYPE_MAP
] = bt_value_map_destroy
,
130 struct bt_value
*bt_value_null_copy(const struct bt_value
*null_obj
)
132 return (void *) bt_value_null
;
136 struct bt_value
*bt_value_bool_copy(const struct bt_value
*bool_obj
)
138 return bt_value_bool_create_init(
139 BT_VALUE_TO_BOOL(bool_obj
)->value
);
143 struct bt_value
*bt_value_integer_copy(
144 const struct bt_value
*integer_obj
)
146 return bt_value_integer_create_init(
147 BT_VALUE_TO_INTEGER(integer_obj
)->value
);
151 struct bt_value
*bt_value_real_copy(const struct bt_value
*real_obj
)
153 return bt_value_real_create_init(
154 BT_VALUE_TO_REAL(real_obj
)->value
);
158 struct bt_value
*bt_value_string_copy(const struct bt_value
*string_obj
)
160 return bt_value_string_create_init(
161 BT_VALUE_TO_STRING(string_obj
)->gstr
->str
);
165 struct bt_value
*bt_value_array_copy(const struct bt_value
*array_obj
)
169 struct bt_value
*copy_obj
;
170 struct bt_value_array
*typed_array_obj
;
172 BT_LOGD("Copying array value: addr=%p", array_obj
);
173 typed_array_obj
= BT_VALUE_TO_ARRAY(array_obj
);
174 copy_obj
= bt_value_array_create();
176 BT_LOGE_STR("Cannot create empty array value.");
180 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
181 struct bt_value
*element_obj_copy
= NULL
;
182 const struct bt_value
*element_obj
=
183 bt_value_array_borrow_element_by_index_const(
186 BT_ASSERT(element_obj
);
187 BT_LOGD("Copying array value's element: element-addr=%p, "
188 "index=%d", element_obj
, i
);
189 ret
= bt_value_copy(element_obj
, &element_obj_copy
);
191 BT_LOGE("Cannot copy array value's element: "
192 "array-addr=%p, index=%d",
194 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
198 BT_ASSERT(element_obj_copy
);
199 ret
= bt_value_array_append_element(copy_obj
,
200 (void *) element_obj_copy
);
201 BT_OBJECT_PUT_REF_AND_RESET(element_obj_copy
);
203 BT_LOGE("Cannot append to array value: addr=%p",
205 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
210 BT_LOGD("Copied array value: original-addr=%p, copy-addr=%p",
211 array_obj
, copy_obj
);
218 struct bt_value
*bt_value_map_copy(const struct bt_value
*map_obj
)
222 gpointer key
, element_obj
;
223 struct bt_value
*copy_obj
;
224 struct bt_value
*element_obj_copy
= NULL
;
225 struct bt_value_map
*typed_map_obj
;
227 BT_LOGD("Copying map value: addr=%p", map_obj
);
228 typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
229 copy_obj
= bt_value_map_create();
234 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
236 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
237 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
240 BT_LOGD("Copying map value's element: element-addr=%p, "
241 "key=\"%s\"", element_obj
, key_str
);
242 ret
= bt_value_copy(element_obj
, &element_obj_copy
);
244 BT_LOGE("Cannot copy map value's element: "
245 "map-addr=%p, key=\"%s\"",
247 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
251 BT_ASSERT(element_obj_copy
);
252 ret
= bt_value_map_insert_entry(copy_obj
, key_str
,
253 (void *) element_obj_copy
);
254 BT_OBJECT_PUT_REF_AND_RESET(element_obj_copy
);
256 BT_LOGE("Cannot insert into map value: addr=%p, key=\"%s\"",
258 BT_OBJECT_PUT_REF_AND_RESET(copy_obj
);
263 BT_LOGD("Copied map value: addr=%p", map_obj
);
270 struct bt_value
*(* const copy_funcs
[])(const struct bt_value
*) = {
271 [BT_VALUE_TYPE_NULL
] = bt_value_null_copy
,
272 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_copy
,
273 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_copy
,
274 [BT_VALUE_TYPE_REAL
] = bt_value_real_copy
,
275 [BT_VALUE_TYPE_STRING
] = bt_value_string_copy
,
276 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_copy
,
277 [BT_VALUE_TYPE_MAP
] = bt_value_map_copy
,
281 bt_bool
bt_value_null_compare(const struct bt_value
*object_a
,
282 const struct bt_value
*object_b
)
285 * Always BT_TRUE since bt_value_compare() already checks if both
286 * object_a and object_b have the same type, and in the case of
287 * null value objects, they're always the same if it is so.
293 bt_bool
bt_value_bool_compare(const struct bt_value
*object_a
,
294 const struct bt_value
*object_b
)
296 if (BT_VALUE_TO_BOOL(object_a
)->value
!=
297 BT_VALUE_TO_BOOL(object_b
)->value
) {
298 BT_LOGV("Boolean value objects are different: "
299 "bool-a-val=%d, bool-b-val=%d",
300 BT_VALUE_TO_BOOL(object_a
)->value
,
301 BT_VALUE_TO_BOOL(object_b
)->value
);
309 bt_bool
bt_value_integer_compare(const struct bt_value
*object_a
,
310 const struct bt_value
*object_b
)
312 if (BT_VALUE_TO_INTEGER(object_a
)->value
!=
313 BT_VALUE_TO_INTEGER(object_b
)->value
) {
314 BT_LOGV("Integer value objects are different: "
315 "int-a-val=%" PRId64
", int-b-val=%" PRId64
,
316 BT_VALUE_TO_INTEGER(object_a
)->value
,
317 BT_VALUE_TO_INTEGER(object_b
)->value
);
325 bt_bool
bt_value_real_compare(const struct bt_value
*object_a
,
326 const struct bt_value
*object_b
)
328 if (BT_VALUE_TO_REAL(object_a
)->value
!=
329 BT_VALUE_TO_REAL(object_b
)->value
) {
330 BT_LOGV("Real number value objects are different: "
331 "real-a-val=%f, real-b-val=%f",
332 BT_VALUE_TO_REAL(object_a
)->value
,
333 BT_VALUE_TO_REAL(object_b
)->value
);
341 bt_bool
bt_value_string_compare(const struct bt_value
*object_a
,
342 const struct bt_value
*object_b
)
344 if (strcmp(BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
345 BT_VALUE_TO_STRING(object_b
)->gstr
->str
) != 0) {
346 BT_LOGV("String value objects are different: "
347 "string-a-val=\"%s\", string-b-val=\"%s\"",
348 BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
349 BT_VALUE_TO_STRING(object_b
)->gstr
->str
);
357 bt_bool
bt_value_array_compare(const struct bt_value
*object_a
,
358 const struct bt_value
*object_b
)
361 bt_bool ret
= BT_TRUE
;
362 const struct bt_value_array
*array_obj_a
=
363 BT_VALUE_TO_ARRAY(object_a
);
365 if (bt_value_array_get_size(object_a
) !=
366 bt_value_array_get_size(object_b
)) {
367 BT_LOGV("Array values are different: size mismatch "
368 "value-a-addr=%p, value-b-addr=%p, "
369 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
371 bt_value_array_get_size(object_a
),
372 bt_value_array_get_size(object_b
));
377 for (i
= 0; i
< array_obj_a
->garray
->len
; ++i
) {
378 const struct bt_value
*element_obj_a
;
379 const struct bt_value
*element_obj_b
;
381 element_obj_a
= bt_value_array_borrow_element_by_index_const(
383 element_obj_b
= bt_value_array_borrow_element_by_index_const(
386 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
387 BT_LOGV("Array values's elements are different: "
388 "value-a-addr=%p, value-b-addr=%p, index=%d",
389 element_obj_a
, element_obj_b
, i
);
400 bt_bool
bt_value_map_compare(const struct bt_value
*object_a
,
401 const struct bt_value
*object_b
)
403 bt_bool ret
= BT_TRUE
;
405 gpointer key
, element_obj_a
;
406 const struct bt_value_map
*map_obj_a
= BT_VALUE_TO_MAP(object_a
);
408 if (bt_value_map_get_size(object_a
) !=
409 bt_value_map_get_size(object_b
)) {
410 BT_LOGV("Map values are different: size mismatch "
411 "value-a-addr=%p, value-b-addr=%p, "
412 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
414 bt_value_map_get_size(object_a
),
415 bt_value_map_get_size(object_b
));
420 g_hash_table_iter_init(&iter
, map_obj_a
->ght
);
422 while (g_hash_table_iter_next(&iter
, &key
, &element_obj_a
)) {
423 const struct bt_value
*element_obj_b
;
424 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
426 element_obj_b
= bt_value_map_borrow_entry_value_const(object_b
,
429 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
430 BT_LOGV("Map values's elements are different: "
431 "value-a-addr=%p, value-b-addr=%p, key=\"%s\"",
432 element_obj_a
, element_obj_b
, key_str
);
443 bt_bool (* const compare_funcs
[])(const struct bt_value
*,
444 const struct bt_value
*) = {
445 [BT_VALUE_TYPE_NULL
] = bt_value_null_compare
,
446 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_compare
,
447 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_compare
,
448 [BT_VALUE_TYPE_REAL
] = bt_value_real_compare
,
449 [BT_VALUE_TYPE_STRING
] = bt_value_string_compare
,
450 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_compare
,
451 [BT_VALUE_TYPE_MAP
] = bt_value_map_compare
,
455 void bt_value_null_freeze(struct bt_value
*object
)
460 void bt_value_generic_freeze(struct bt_value
*object
)
462 object
->frozen
= BT_TRUE
;
466 void bt_value_array_freeze(struct bt_value
*object
)
469 struct bt_value_array
*typed_array_obj
=
470 BT_VALUE_TO_ARRAY(object
);
472 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
473 bt_value_freeze(g_ptr_array_index(typed_array_obj
->garray
, i
));
476 bt_value_generic_freeze(object
);
480 void bt_value_map_freeze(struct bt_value
*object
)
483 gpointer key
, element_obj
;
484 const struct bt_value_map
*map_obj
= BT_VALUE_TO_MAP(object
);
486 g_hash_table_iter_init(&iter
, map_obj
->ght
);
488 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
489 bt_value_freeze(element_obj
);
492 bt_value_generic_freeze(object
);
496 void (* const freeze_funcs
[])(struct bt_value
*) = {
497 [BT_VALUE_TYPE_NULL
] = bt_value_null_freeze
,
498 [BT_VALUE_TYPE_BOOL
] = bt_value_generic_freeze
,
499 [BT_VALUE_TYPE_INTEGER
] = bt_value_generic_freeze
,
500 [BT_VALUE_TYPE_REAL
] = bt_value_generic_freeze
,
501 [BT_VALUE_TYPE_STRING
] = bt_value_generic_freeze
,
502 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_freeze
,
503 [BT_VALUE_TYPE_MAP
] = bt_value_map_freeze
,
507 void bt_value_destroy(struct bt_object
*obj
)
509 struct bt_value
*value
;
511 value
= container_of(obj
, struct bt_value
, base
);
512 BT_LOGD("Destroying value: addr=%p", value
);
514 if (bt_value_is_null(value
)) {
515 BT_LOGD_STR("Not destroying the null value singleton.");
519 if (destroy_funcs
[value
->type
]) {
520 destroy_funcs
[value
->type
](value
);
527 enum bt_value_status
_bt_value_freeze(const struct bt_value
*c_object
)
529 const struct bt_value
*object
= (void *) c_object
;
530 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
534 if (object
->frozen
) {
538 BT_LOGD("Freezing value: addr=%p", object
);
539 freeze_funcs
[object
->type
]((void *) object
);
545 enum bt_value_type
bt_value_get_type(const struct bt_value
*object
)
547 BT_ASSERT_PRE_NON_NULL(object
, "Value object");
552 struct bt_value
bt_value_create_base(enum bt_value_type type
)
554 struct bt_value value
;
557 value
.frozen
= BT_FALSE
;
558 bt_object_init_shared(&value
.base
, bt_value_destroy
);
562 struct bt_value
*bt_value_bool_create_init(bt_bool val
)
564 struct bt_value_bool
*bool_obj
;
566 BT_LOGD("Creating boolean value object: val=%d", val
);
567 bool_obj
= g_new0(struct bt_value_bool
, 1);
569 BT_LOGE_STR("Failed to allocate one boolean value object.");
573 bool_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_BOOL
);
574 bool_obj
->value
= val
;
575 BT_LOGD("Created boolean value object: addr=%p", bool_obj
);
578 return (void *) BT_VALUE_FROM_CONCRETE(bool_obj
);
581 struct bt_value
*bt_value_bool_create(void)
583 return bt_value_bool_create_init(BT_FALSE
);
586 struct bt_value
*bt_value_integer_create_init(int64_t val
)
588 struct bt_value_integer
*integer_obj
;
590 BT_LOGD("Creating integer value object: val=%" PRId64
, val
);
591 integer_obj
= g_new0(struct bt_value_integer
, 1);
593 BT_LOGE_STR("Failed to allocate one integer value object.");
597 integer_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_INTEGER
);
598 integer_obj
->value
= val
;
599 BT_LOGD("Created integer value object: addr=%p",
603 return (void *) BT_VALUE_FROM_CONCRETE(integer_obj
);
606 struct bt_value
*bt_value_integer_create(void)
608 return bt_value_integer_create_init(0);
611 struct bt_value
*bt_value_real_create_init(double val
)
613 struct bt_value_real
*real_obj
;
615 BT_LOGD("Creating real number value object: val=%f", val
);
616 real_obj
= g_new0(struct bt_value_real
, 1);
618 BT_LOGE_STR("Failed to allocate one real number value object.");
622 real_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_REAL
);
623 real_obj
->value
= val
;
624 BT_LOGD("Created real number value object: addr=%p",
628 return (void *) BT_VALUE_FROM_CONCRETE(real_obj
);
631 struct bt_value
*bt_value_real_create(void)
633 return bt_value_real_create_init(0.);
636 struct bt_value
*bt_value_string_create_init(const char *val
)
638 struct bt_value_string
*string_obj
= NULL
;
641 BT_LOGW_STR("Invalid parameter: value is NULL.");
645 BT_LOGD("Creating string value object: val-len=%zu", strlen(val
));
646 string_obj
= g_new0(struct bt_value_string
, 1);
648 BT_LOGE_STR("Failed to allocate one string object.");
652 string_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_STRING
);
653 string_obj
->gstr
= g_string_new(val
);
654 if (!string_obj
->gstr
) {
655 BT_LOGE_STR("Failed to allocate a GString.");
661 BT_LOGD("Created string value object: addr=%p",
665 return (void *) BT_VALUE_FROM_CONCRETE(string_obj
);
668 struct bt_value
*bt_value_string_create(void)
670 return bt_value_string_create_init("");
673 struct bt_value
*bt_value_array_create(void)
675 struct bt_value_array
*array_obj
;
677 BT_LOGD_STR("Creating empty array value object.");
678 array_obj
= g_new0(struct bt_value_array
, 1);
680 BT_LOGE_STR("Failed to allocate one array object.");
684 array_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_ARRAY
);
685 array_obj
->garray
= bt_g_ptr_array_new_full(0,
686 (GDestroyNotify
) bt_object_put_ref
);
687 if (!array_obj
->garray
) {
688 BT_LOGE_STR("Failed to allocate a GPtrArray.");
694 BT_LOGD("Created array value object: addr=%p",
698 return (void *) BT_VALUE_FROM_CONCRETE(array_obj
);
701 struct bt_value
*bt_value_map_create(void)
703 struct bt_value_map
*map_obj
;
705 BT_LOGD_STR("Creating empty map value object.");
706 map_obj
= g_new0(struct bt_value_map
, 1);
708 BT_LOGE_STR("Failed to allocate one map object.");
712 map_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_MAP
);
713 map_obj
->ght
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
714 NULL
, (GDestroyNotify
) bt_object_put_ref
);
716 BT_LOGE_STR("Failed to allocate a GHashTable.");
722 BT_LOGD("Created map value object: addr=%p",
726 return (void *) BT_VALUE_FROM_CONCRETE(map_obj
);
729 bt_bool
bt_value_bool_get(const struct bt_value
*bool_obj
)
731 BT_ASSERT_PRE_NON_NULL(bool_obj
, "Value object");
732 BT_ASSERT_PRE_VALUE_IS_TYPE(bool_obj
, BT_VALUE_TYPE_BOOL
);
733 return BT_VALUE_TO_BOOL(bool_obj
)->value
;
736 void bt_value_bool_set(struct bt_value
*bool_obj
, bt_bool val
)
738 BT_ASSERT_PRE_NON_NULL(bool_obj
, "Value object");
739 BT_ASSERT_PRE_VALUE_IS_TYPE(bool_obj
, BT_VALUE_TYPE_BOOL
);
740 BT_ASSERT_PRE_VALUE_HOT(bool_obj
, "Value object");
741 BT_VALUE_TO_BOOL(bool_obj
)->value
= val
;
742 BT_LOGV("Set boolean value's raw value: value-addr=%p, value=%d",
746 int64_t bt_value_integer_get(const struct bt_value
*integer_obj
)
748 BT_ASSERT_PRE_NON_NULL(integer_obj
, "Value object");
749 BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj
, BT_VALUE_TYPE_INTEGER
);
750 return BT_VALUE_TO_INTEGER(integer_obj
)->value
;
753 void bt_value_integer_set(struct bt_value
*integer_obj
,
756 BT_ASSERT_PRE_NON_NULL(integer_obj
, "Value object");
757 BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj
, BT_VALUE_TYPE_INTEGER
);
758 BT_ASSERT_PRE_VALUE_HOT(integer_obj
, "Value object");
759 BT_VALUE_TO_INTEGER(integer_obj
)->value
= val
;
760 BT_LOGV("Set integer value's raw value: value-addr=%p, value=%" PRId64
,
764 double bt_value_real_get(const struct bt_value
*real_obj
)
766 BT_ASSERT_PRE_NON_NULL(real_obj
, "Value object");
767 BT_ASSERT_PRE_VALUE_IS_TYPE(real_obj
, BT_VALUE_TYPE_REAL
);
768 return BT_VALUE_TO_REAL(real_obj
)->value
;
771 void bt_value_real_set(struct bt_value
*real_obj
, double val
)
773 BT_ASSERT_PRE_NON_NULL(real_obj
, "Value object");
774 BT_ASSERT_PRE_VALUE_IS_TYPE(real_obj
, BT_VALUE_TYPE_REAL
);
775 BT_ASSERT_PRE_VALUE_HOT(real_obj
, "Value object");
776 BT_VALUE_TO_REAL(real_obj
)->value
= val
;
777 BT_LOGV("Set real number value's raw value: value-addr=%p, value=%f",
781 const char *bt_value_string_get(const struct bt_value
*string_obj
)
783 BT_ASSERT_PRE_NON_NULL(string_obj
, "Value object");
784 BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj
, BT_VALUE_TYPE_STRING
);
785 return BT_VALUE_TO_STRING(string_obj
)->gstr
->str
;
788 enum bt_value_status
bt_value_string_set(
789 struct bt_value
*string_obj
, const char *val
)
791 BT_ASSERT_PRE_NON_NULL(string_obj
, "Value object");
792 BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj
, BT_VALUE_TYPE_STRING
);
793 BT_ASSERT_PRE_VALUE_HOT(string_obj
, "Value object");
794 g_string_assign(BT_VALUE_TO_STRING(string_obj
)->gstr
, val
);
795 BT_LOGV("Set string value's raw value: value-addr=%p, raw-value-addr=%p",
797 return BT_VALUE_STATUS_OK
;
800 uint64_t bt_value_array_get_size(const struct bt_value
*array_obj
)
802 BT_ASSERT_PRE_NON_NULL(array_obj
, "Value object");
803 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
804 return (uint64_t) BT_VALUE_TO_ARRAY(array_obj
)->garray
->len
;
807 struct bt_value
*bt_value_array_borrow_element_by_index(
808 struct bt_value
*array_obj
, uint64_t index
)
810 struct bt_value_array
*typed_array_obj
=
811 BT_VALUE_TO_ARRAY(array_obj
);
813 BT_ASSERT_PRE_NON_NULL(array_obj
, "Value object");
814 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
815 BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index
,
816 typed_array_obj
->garray
->len
);
817 return g_ptr_array_index(typed_array_obj
->garray
, index
);
820 const struct bt_value
*bt_value_array_borrow_element_by_index_const(
821 const struct bt_value
*array_obj
,
824 return bt_value_array_borrow_element_by_index(
825 (void *) array_obj
, index
);
828 enum bt_value_status
bt_value_array_append_element(
829 struct bt_value
*array_obj
,
830 struct bt_value
*element_obj
)
832 struct bt_value_array
*typed_array_obj
=
833 BT_VALUE_TO_ARRAY(array_obj
);
835 BT_ASSERT_PRE_NON_NULL(array_obj
, "Array value object");
836 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
837 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
838 BT_ASSERT_PRE_VALUE_HOT(array_obj
, "Array value object");
839 g_ptr_array_add(typed_array_obj
->garray
, element_obj
);
840 bt_object_get_ref(element_obj
);
841 BT_LOGV("Appended element to array value: array-value-addr=%p, "
842 "element-value-addr=%p, new-size=%u",
843 array_obj
, element_obj
, typed_array_obj
->garray
->len
);
844 return BT_VALUE_STATUS_OK
;
847 enum bt_value_status
bt_value_array_append_bool_element(
848 struct bt_value
*array_obj
, bt_bool val
)
850 enum bt_value_status ret
;
851 struct bt_value
*bool_obj
= NULL
;
853 bool_obj
= bt_value_bool_create_init(val
);
854 ret
= bt_value_array_append_element(array_obj
,
856 bt_object_put_ref(bool_obj
);
860 enum bt_value_status
bt_value_array_append_integer_element(
861 struct bt_value
*array_obj
, int64_t val
)
863 enum bt_value_status ret
;
864 struct bt_value
*integer_obj
= NULL
;
866 integer_obj
= bt_value_integer_create_init(val
);
867 ret
= bt_value_array_append_element(array_obj
,
868 (void *) integer_obj
);
869 bt_object_put_ref(integer_obj
);
873 enum bt_value_status
bt_value_array_append_real_element(
874 struct bt_value
*array_obj
, double val
)
876 enum bt_value_status ret
;
877 struct bt_value
*real_obj
= NULL
;
879 real_obj
= bt_value_real_create_init(val
);
880 ret
= bt_value_array_append_element(array_obj
,
882 bt_object_put_ref(real_obj
);
886 enum bt_value_status
bt_value_array_append_string_element(
887 struct bt_value
*array_obj
, const char *val
)
889 enum bt_value_status ret
;
890 struct bt_value
*string_obj
= NULL
;
892 string_obj
= bt_value_string_create_init(val
);
893 ret
= bt_value_array_append_element(array_obj
,
894 (void *) string_obj
);
895 bt_object_put_ref(string_obj
);
899 enum bt_value_status
bt_value_array_append_empty_array_element(
900 struct bt_value
*array_obj
)
902 enum bt_value_status ret
;
903 struct bt_value
*empty_array_obj
= NULL
;
905 empty_array_obj
= bt_value_array_create();
906 ret
= bt_value_array_append_element(array_obj
,
907 (void *) empty_array_obj
);
908 bt_object_put_ref(empty_array_obj
);
912 enum bt_value_status
bt_value_array_append_empty_map_element(
913 struct bt_value
*array_obj
)
915 enum bt_value_status ret
;
916 struct bt_value
*map_obj
= NULL
;
918 map_obj
= bt_value_map_create();
919 ret
= bt_value_array_append_element(array_obj
,
921 bt_object_put_ref(map_obj
);
925 enum bt_value_status
bt_value_array_set_element_by_index(
926 struct bt_value
*array_obj
, uint64_t index
,
927 struct bt_value
*element_obj
)
929 struct bt_value_array
*typed_array_obj
=
930 BT_VALUE_TO_ARRAY(array_obj
);
932 BT_ASSERT_PRE_NON_NULL(array_obj
, "Array value object");
933 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
934 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
935 BT_ASSERT_PRE_VALUE_HOT(array_obj
, "Array value object");
936 BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index
,
937 typed_array_obj
->garray
->len
);
938 bt_object_put_ref(g_ptr_array_index(typed_array_obj
->garray
, index
));
939 g_ptr_array_index(typed_array_obj
->garray
, index
) = element_obj
;
940 bt_object_get_ref(element_obj
);
941 BT_LOGV("Set array value's element: array-value-addr=%p, "
942 "index=%" PRIu64
", element-value-addr=%p",
943 array_obj
, index
, element_obj
);
944 return BT_VALUE_STATUS_OK
;
947 uint64_t bt_value_map_get_size(const struct bt_value
*map_obj
)
949 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
950 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
951 return (uint64_t) g_hash_table_size(BT_VALUE_TO_MAP(map_obj
)->ght
);
954 struct bt_value
*bt_value_map_borrow_entry_value(struct bt_value
*map_obj
,
957 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
958 BT_ASSERT_PRE_NON_NULL(key
, "Key");
959 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
960 return g_hash_table_lookup(BT_VALUE_TO_MAP(map_obj
)->ght
,
961 GUINT_TO_POINTER(g_quark_from_string(key
)));
964 const struct bt_value
*bt_value_map_borrow_entry_value_const(
965 const struct bt_value
*map_obj
, const char *key
)
967 return bt_value_map_borrow_entry_value((void *) map_obj
, key
);
970 bt_bool
bt_value_map_has_entry(const struct bt_value
*map_obj
, const char *key
)
972 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
973 BT_ASSERT_PRE_NON_NULL(key
, "Key");
974 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
975 return bt_g_hash_table_contains(BT_VALUE_TO_MAP(map_obj
)->ght
,
976 GUINT_TO_POINTER(g_quark_from_string(key
)));
979 enum bt_value_status
bt_value_map_insert_entry(
980 struct bt_value
*map_obj
,
981 const char *key
, struct bt_value
*element_obj
)
983 BT_ASSERT_PRE_NON_NULL(map_obj
, "Map value object");
984 BT_ASSERT_PRE_NON_NULL(key
, "Key");
985 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
986 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
987 BT_ASSERT_PRE_VALUE_HOT(map_obj
, "Map value object");
988 g_hash_table_insert(BT_VALUE_TO_MAP(map_obj
)->ght
,
989 GUINT_TO_POINTER(g_quark_from_string(key
)), element_obj
);
990 bt_object_get_ref(element_obj
);
991 BT_LOGV("Inserted value into map value: map-value-addr=%p, "
992 "key=\"%s\", element-value-addr=%p",
993 map_obj
, key
, element_obj
);
994 return BT_VALUE_STATUS_OK
;
997 enum bt_value_status
bt_value_map_insert_bool_entry(
998 struct bt_value
*map_obj
, const char *key
, bt_bool val
)
1000 enum bt_value_status ret
;
1001 struct bt_value
*bool_obj
= NULL
;
1003 bool_obj
= bt_value_bool_create_init(val
);
1004 ret
= bt_value_map_insert_entry(map_obj
, key
,
1006 bt_object_put_ref(bool_obj
);
1010 enum bt_value_status
bt_value_map_insert_integer_entry(
1011 struct bt_value
*map_obj
, const char *key
, int64_t val
)
1013 enum bt_value_status ret
;
1014 struct bt_value
*integer_obj
= NULL
;
1016 integer_obj
= bt_value_integer_create_init(val
);
1017 ret
= bt_value_map_insert_entry(map_obj
, key
,
1018 (void *) integer_obj
);
1019 bt_object_put_ref(integer_obj
);
1023 enum bt_value_status
bt_value_map_insert_real_entry(
1024 struct bt_value
*map_obj
, const char *key
, double val
)
1026 enum bt_value_status ret
;
1027 struct bt_value
*real_obj
= NULL
;
1029 real_obj
= bt_value_real_create_init(val
);
1030 ret
= bt_value_map_insert_entry(map_obj
, key
,
1032 bt_object_put_ref(real_obj
);
1036 enum bt_value_status
bt_value_map_insert_string_entry(
1037 struct bt_value
*map_obj
, const char *key
,
1040 enum bt_value_status ret
;
1041 struct bt_value
*string_obj
= NULL
;
1043 string_obj
= bt_value_string_create_init(val
);
1044 ret
= bt_value_map_insert_entry(map_obj
, key
,
1045 (void *) string_obj
);
1046 bt_object_put_ref(string_obj
);
1050 enum bt_value_status
bt_value_map_insert_empty_array_entry(
1051 struct bt_value
*map_obj
, const char *key
)
1053 enum bt_value_status ret
;
1054 struct bt_value
*array_obj
= NULL
;
1056 array_obj
= bt_value_array_create();
1057 ret
= bt_value_map_insert_entry(map_obj
, key
,
1058 (void *) array_obj
);
1059 bt_object_put_ref(array_obj
);
1063 enum bt_value_status
bt_value_map_insert_empty_map_entry(
1064 struct bt_value
*map_obj
, const char *key
)
1066 enum bt_value_status ret
;
1067 struct bt_value
*empty_map_obj
= NULL
;
1069 empty_map_obj
= bt_value_map_create();
1070 ret
= bt_value_map_insert_entry(map_obj
, key
,
1071 (void *) empty_map_obj
);
1072 bt_object_put_ref(empty_map_obj
);
1076 enum bt_value_status
bt_value_map_foreach_entry(struct bt_value
*map_obj
,
1077 bt_value_map_foreach_entry_func func
, void *data
)
1079 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1080 gpointer key
, element_obj
;
1081 GHashTableIter iter
;
1082 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1084 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
1085 BT_ASSERT_PRE_NON_NULL(func
, "Callback");
1086 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1087 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
1089 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
1090 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
1092 if (!func(key_str
, element_obj
, data
)) {
1093 BT_LOGV("User canceled the loop: key=\"%s\", "
1094 "value-addr=%p, data=%p",
1095 key_str
, element_obj
, data
);
1096 ret
= BT_VALUE_STATUS_CANCELED
;
1104 enum bt_value_status
bt_value_map_foreach_entry_const(
1105 const struct bt_value
*map_obj
,
1106 bt_value_map_foreach_entry_const_func func
, void *data
)
1108 return bt_value_map_foreach_entry((void *) map_obj
,
1109 (bt_value_map_foreach_entry_func
) func
, data
);
1112 struct extend_map_element_data
{
1113 struct bt_value
*extended_obj
;
1114 enum bt_value_status status
;
1118 bt_bool
extend_map_element(const char *key
,
1119 const struct bt_value
*extension_obj_elem
, void *data
)
1121 bt_bool ret
= BT_TRUE
;
1122 struct extend_map_element_data
*extend_data
= data
;
1123 struct bt_value
*extension_obj_elem_copy
= NULL
;
1125 /* Copy object which is to replace the current one */
1126 extend_data
->status
= bt_value_copy(extension_obj_elem
,
1127 &extension_obj_elem_copy
);
1128 if (extend_data
->status
) {
1129 BT_LOGE("Cannot copy map element: addr=%p",
1130 extension_obj_elem
);
1134 BT_ASSERT(extension_obj_elem_copy
);
1136 /* Replace in extended object */
1137 extend_data
->status
= bt_value_map_insert_entry(
1138 extend_data
->extended_obj
, key
,
1139 (void *) extension_obj_elem_copy
);
1140 if (extend_data
->status
) {
1141 BT_LOGE("Cannot replace value in extended value: key=\"%s\", "
1142 "extended-value-addr=%p, element-value-addr=%p",
1143 key
, extend_data
->extended_obj
,
1144 extension_obj_elem_copy
);
1151 BT_ASSERT(extend_data
->status
!= BT_VALUE_STATUS_OK
);
1155 BT_OBJECT_PUT_REF_AND_RESET(extension_obj_elem_copy
);
1159 enum bt_value_status
bt_value_map_extend(
1160 const struct bt_value
*base_map_obj
,
1161 const struct bt_value
*extension_obj
,
1162 struct bt_value
**extended_map_obj
)
1164 struct extend_map_element_data extend_data
= {
1165 .extended_obj
= NULL
,
1166 .status
= BT_VALUE_STATUS_OK
,
1169 BT_ASSERT_PRE_NON_NULL(base_map_obj
, "Base value object");
1170 BT_ASSERT_PRE_NON_NULL(extension_obj
, "Extension value object");
1171 BT_ASSERT_PRE_NON_NULL(extended_map_obj
,
1172 "Extended value object (output)");
1173 BT_ASSERT_PRE_VALUE_IS_TYPE(base_map_obj
, BT_VALUE_TYPE_MAP
);
1174 BT_ASSERT_PRE_VALUE_IS_TYPE(extension_obj
, BT_VALUE_TYPE_MAP
);
1175 BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p",
1176 base_map_obj
, extension_obj
);
1177 *extended_map_obj
= NULL
;
1179 /* Create copy of base map object to start with */
1180 extend_data
.status
= bt_value_copy(base_map_obj
, extended_map_obj
);
1181 if (extend_data
.status
) {
1182 BT_LOGE("Cannot copy base value: base-value-addr=%p",
1187 BT_ASSERT(extended_map_obj
);
1190 * For each key in the extension map object, replace this key
1191 * in the copied map object.
1193 extend_data
.extended_obj
= *extended_map_obj
;
1195 if (bt_value_map_foreach_entry_const(extension_obj
, extend_map_element
,
1197 BT_LOGE("Cannot iterate on the extension object's elements: "
1198 "extension-value-addr=%p", extension_obj
);
1202 if (extend_data
.status
) {
1203 BT_LOGE("Failed to successfully iterate on the extension object's elements: "
1204 "extension-value-addr=%p", extension_obj
);
1208 BT_LOGD("Extended map value: extended-value-addr=%p",
1213 BT_OBJECT_PUT_REF_AND_RESET(*extended_map_obj
);
1214 *extended_map_obj
= NULL
;
1217 return extend_data
.status
;
1220 enum bt_value_status
bt_value_copy(const struct bt_value
*object
,
1221 struct bt_value
**copy_obj
)
1223 enum bt_value_status status
= BT_VALUE_STATUS_OK
;
1225 BT_ASSERT_PRE_NON_NULL(object
, "Value object");
1226 BT_ASSERT_PRE_NON_NULL(copy_obj
, "Value object copy (output)");
1227 BT_LOGD("Copying value object: addr=%p", object
);
1228 *copy_obj
= copy_funcs
[object
->type
](object
);
1230 BT_LOGD("Copied value object: copy-value-addr=%p",
1233 status
= BT_VALUE_STATUS_NOMEM
;
1235 BT_LOGE_STR("Failed to copy value object.");
1241 bt_bool
bt_value_compare(const struct bt_value
*object_a
,
1242 const struct bt_value
*object_b
)
1244 bt_bool ret
= BT_FALSE
;
1246 BT_ASSERT_PRE_NON_NULL(object_a
, "Value object A");
1247 BT_ASSERT_PRE_NON_NULL(object_b
, "Value object B");
1249 if (object_a
->type
!= object_b
->type
) {
1250 BT_LOGV("Values are different: type mismatch: "
1251 "value-a-addr=%p, value-b-addr=%p, "
1252 "value-a-type=%s, value-b-type=%s",
1254 bt_common_value_type_string(object_a
->type
),
1255 bt_common_value_type_string(object_b
->type
));
1259 ret
= compare_funcs
[object_a
->type
](object_a
, object_b
);
1265 void bt_value_get_ref(const struct bt_value
*value
)
1267 bt_object_get_ref(value
);
1270 void bt_value_put_ref(const struct bt_value
*value
)
1272 bt_object_put_ref(value
);