2 * Values.c: value objects
6 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
7 * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 #define BT_LOG_TAG "VALUES"
29 #include <babeltrace/lib-logging-internal.h>
35 #include <babeltrace/compiler-internal.h>
36 #include <babeltrace/ref.h>
37 #include <babeltrace/values.h>
38 #include <babeltrace/compat/glib-internal.h>
39 #include <babeltrace/types.h>
40 #include <babeltrace/object-internal.h>
41 #include <babeltrace/values-internal.h>
42 #include <babeltrace/assert-internal.h>
43 #include <babeltrace/assert-pre-internal.h>
45 #define BT_VALUE_FROM_CONCRETE(_concrete) ((struct bt_value *) (_concrete))
46 #define BT_VALUE_TO_BOOL(_base) ((struct bt_value_bool *) (_base))
47 #define BT_VALUE_TO_INTEGER(_base) ((struct bt_value_integer *) (_base))
48 #define BT_VALUE_TO_FLOAT(_base) ((struct bt_value_float *) (_base))
49 #define BT_VALUE_TO_STRING(_base) ((struct bt_value_string *) (_base))
50 #define BT_VALUE_TO_ARRAY(_base) ((struct bt_value_array *) (_base))
51 #define BT_VALUE_TO_MAP(_base) ((struct bt_value_map *) (_base))
53 #define BT_ASSERT_PRE_VALUE_IS_TYPE(_value, _type) \
54 BT_ASSERT_PRE((_value)->type == (_type), \
55 "Value has the wrong type ID: expected-type=%s, " \
56 "%![value-]+v", bt_value_type_string(_type), \
59 #define BT_ASSERT_PRE_VALUE_HOT(_value, _name) \
60 BT_ASSERT_PRE_HOT((_value), (_name), ": +%!+v", (_value))
62 #define BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(_index, _count) \
63 BT_ASSERT_PRE((_index) < (_count), \
64 "Index is out of bound: " \
65 "index=%" PRIu64 ", count=%u", (_index), (_count));
69 struct bt_object base
;
70 enum bt_value_type type
;
75 struct bt_value bt_value_null_instance
= {
84 .type
= BT_VALUE_TYPE_NULL
,
88 struct bt_value
*bt_value_null
= &bt_value_null_instance
;
90 struct bt_value_bool
{
95 struct bt_value_integer
{
100 struct bt_value_float
{
101 struct bt_value base
;
105 struct bt_value_string
{
106 struct bt_value base
;
110 struct bt_value_array
{
111 struct bt_value base
;
115 struct bt_value_map
{
116 struct bt_value base
;
121 void bt_value_destroy(struct bt_object
*obj
);
124 void bt_value_string_destroy(struct bt_value
*object
)
126 g_string_free(BT_VALUE_TO_STRING(object
)->gstr
, TRUE
);
130 void bt_value_array_destroy(struct bt_value
*object
)
133 * Pointer array's registered value destructor will take care
134 * of putting each contained object.
136 g_ptr_array_free(BT_VALUE_TO_ARRAY(object
)->garray
, TRUE
);
140 void bt_value_map_destroy(struct bt_value
*object
)
143 * Hash table's registered value destructor will take care of
144 * putting each contained object. Keys are GQuarks and cannot
145 * be destroyed anyway.
147 g_hash_table_destroy(BT_VALUE_TO_MAP(object
)->ght
);
151 void (* const destroy_funcs
[])(struct bt_value
*) = {
152 [BT_VALUE_TYPE_NULL
] = NULL
,
153 [BT_VALUE_TYPE_BOOL
] = NULL
,
154 [BT_VALUE_TYPE_INTEGER
] = NULL
,
155 [BT_VALUE_TYPE_FLOAT
] = NULL
,
156 [BT_VALUE_TYPE_STRING
] = bt_value_string_destroy
,
157 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_destroy
,
158 [BT_VALUE_TYPE_MAP
] = bt_value_map_destroy
,
162 struct bt_value
*bt_value_null_copy(const struct bt_value
*null_obj
)
164 return bt_value_null
;
168 struct bt_value
*bt_value_bool_copy(const struct bt_value
*bool_obj
)
170 return bt_value_bool_create_init(BT_VALUE_TO_BOOL(bool_obj
)->value
);
174 struct bt_value
*bt_value_integer_copy(const struct bt_value
*integer_obj
)
176 return bt_value_integer_create_init(
177 BT_VALUE_TO_INTEGER(integer_obj
)->value
);
181 struct bt_value
*bt_value_float_copy(const struct bt_value
*float_obj
)
183 return bt_value_float_create_init(
184 BT_VALUE_TO_FLOAT(float_obj
)->value
);
188 struct bt_value
*bt_value_string_copy(const struct bt_value
*string_obj
)
190 return bt_value_string_create_init(
191 BT_VALUE_TO_STRING(string_obj
)->gstr
->str
);
195 struct bt_value
*bt_value_array_copy(const struct bt_value
*array_obj
)
199 struct bt_value
*copy_obj
;
200 struct bt_value_array
*typed_array_obj
;
202 BT_LOGD("Copying array value: addr=%p", array_obj
);
203 typed_array_obj
= BT_VALUE_TO_ARRAY(array_obj
);
204 copy_obj
= bt_value_array_create();
206 BT_LOGE_STR("Cannot create empty array value.");
210 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
211 struct bt_value
*element_obj_copy
;
212 struct bt_value
*element_obj
= bt_value_array_get(array_obj
, i
);
214 BT_ASSERT(element_obj
);
215 BT_LOGD("Copying array value's element: element-addr=%p, "
216 "index=%d", element_obj
, i
);
217 element_obj_copy
= bt_value_copy(element_obj
);
219 if (!element_obj_copy
) {
220 BT_LOGE("Cannot copy array value's element: "
221 "array-addr=%p, index=%d",
227 ret
= bt_value_array_append(copy_obj
, element_obj_copy
);
228 BT_PUT(element_obj_copy
);
230 BT_LOGE("Cannot append to array value: addr=%p",
237 BT_LOGD("Copied array value: original-addr=%p, copy-addr=%p",
238 array_obj
, copy_obj
);
245 struct bt_value
*bt_value_map_copy(const struct bt_value
*map_obj
)
249 gpointer key
, element_obj
;
250 struct bt_value
*copy_obj
;
251 struct bt_value
*element_obj_copy
;
252 struct bt_value_map
*typed_map_obj
;
254 BT_LOGD("Copying map value: addr=%p", map_obj
);
255 typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
256 copy_obj
= bt_value_map_create();
261 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
263 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
264 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
267 BT_LOGD("Copying map value's element: element-addr=%p, "
268 "key=\"%s\"", element_obj
, key_str
);
269 element_obj_copy
= bt_value_copy(element_obj
);
270 if (!element_obj_copy
) {
271 BT_LOGE("Cannot copy map value's element: "
272 "map-addr=%p, key=\"%s\"",
278 ret
= bt_value_map_insert(copy_obj
, key_str
, element_obj_copy
);
279 BT_PUT(element_obj_copy
);
281 BT_LOGE("Cannot insert into map value: addr=%p, key=\"%s\"",
288 BT_LOGD("Copied map value: addr=%p", map_obj
);
295 struct bt_value
*(* const copy_funcs
[])(const struct bt_value
*) = {
296 [BT_VALUE_TYPE_NULL
] = bt_value_null_copy
,
297 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_copy
,
298 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_copy
,
299 [BT_VALUE_TYPE_FLOAT
] = bt_value_float_copy
,
300 [BT_VALUE_TYPE_STRING
] = bt_value_string_copy
,
301 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_copy
,
302 [BT_VALUE_TYPE_MAP
] = bt_value_map_copy
,
306 bt_bool
bt_value_null_compare(const struct bt_value
*object_a
,
307 const struct bt_value
*object_b
)
310 * Always BT_TRUE since bt_value_compare() already checks if both
311 * object_a and object_b have the same type, and in the case of
312 * null value objects, they're always the same if it is so.
318 bt_bool
bt_value_bool_compare(const struct bt_value
*object_a
,
319 const struct bt_value
*object_b
)
321 if (BT_VALUE_TO_BOOL(object_a
)->value
!=
322 BT_VALUE_TO_BOOL(object_b
)->value
) {
323 BT_LOGV("Boolean value objects are different: "
324 "bool-a-val=%d, bool-b-val=%d",
325 BT_VALUE_TO_BOOL(object_a
)->value
,
326 BT_VALUE_TO_BOOL(object_b
)->value
);
334 bt_bool
bt_value_integer_compare(const struct bt_value
*object_a
,
335 const struct bt_value
*object_b
)
337 if (BT_VALUE_TO_INTEGER(object_a
)->value
!=
338 BT_VALUE_TO_INTEGER(object_b
)->value
) {
339 BT_LOGV("Integer value objects are different: "
340 "int-a-val=%" PRId64
", int-b-val=%" PRId64
,
341 BT_VALUE_TO_INTEGER(object_a
)->value
,
342 BT_VALUE_TO_INTEGER(object_b
)->value
);
350 bt_bool
bt_value_float_compare(const struct bt_value
*object_a
,
351 const struct bt_value
*object_b
)
353 if (BT_VALUE_TO_FLOAT(object_a
)->value
!=
354 BT_VALUE_TO_FLOAT(object_b
)->value
) {
355 BT_LOGV("Floating point number value objects are different: "
356 "float-a-val=%f, float-b-val=%f",
357 BT_VALUE_TO_FLOAT(object_a
)->value
,
358 BT_VALUE_TO_FLOAT(object_b
)->value
);
366 bt_bool
bt_value_string_compare(const struct bt_value
*object_a
,
367 const struct bt_value
*object_b
)
369 if (strcmp(BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
370 BT_VALUE_TO_STRING(object_b
)->gstr
->str
) != 0) {
371 BT_LOGV("String value objects are different: "
372 "string-a-val=\"%s\", string-b-val=\"%s\"",
373 BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
374 BT_VALUE_TO_STRING(object_b
)->gstr
->str
);
382 bt_bool
bt_value_array_compare(const struct bt_value
*object_a
,
383 const struct bt_value
*object_b
)
386 bt_bool ret
= BT_TRUE
;
387 const struct bt_value_array
*array_obj_a
=
388 BT_VALUE_TO_ARRAY(object_a
);
390 if (bt_value_array_size(object_a
) != bt_value_array_size(object_b
)) {
391 BT_LOGV("Array values are different: size mismatch "
392 "value-a-addr=%p, value-b-addr=%p, "
393 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
395 bt_value_array_size(object_a
),
396 bt_value_array_size(object_b
));
401 for (i
= 0; i
< array_obj_a
->garray
->len
; ++i
) {
402 struct bt_value
*element_obj_a
;
403 struct bt_value
*element_obj_b
;
405 element_obj_a
= bt_value_array_get(object_a
, i
);
406 element_obj_b
= bt_value_array_get(object_b
, i
);
408 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
409 BT_LOGV("Array values's elements are different: "
410 "value-a-addr=%p, value-b-addr=%p, index=%d",
411 element_obj_a
, element_obj_b
, i
);
412 BT_PUT(element_obj_a
);
413 BT_PUT(element_obj_b
);
418 BT_PUT(element_obj_a
);
419 BT_PUT(element_obj_b
);
427 bt_bool
bt_value_map_compare(const struct bt_value
*object_a
,
428 const struct bt_value
*object_b
)
430 bt_bool ret
= BT_TRUE
;
432 gpointer key
, element_obj_a
;
433 const struct bt_value_map
*map_obj_a
= BT_VALUE_TO_MAP(object_a
);
435 if (bt_value_map_size(object_a
) != bt_value_map_size(object_b
)) {
436 BT_LOGV("Map values are different: size mismatch "
437 "value-a-addr=%p, value-b-addr=%p, "
438 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
440 bt_value_map_size(object_a
),
441 bt_value_map_size(object_b
));
446 g_hash_table_iter_init(&iter
, map_obj_a
->ght
);
448 while (g_hash_table_iter_next(&iter
, &key
, &element_obj_a
)) {
449 struct bt_value
*element_obj_b
;
450 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
452 element_obj_b
= bt_value_map_get(object_b
, key_str
);
454 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
455 BT_LOGV("Map values's elements are different: "
456 "value-a-addr=%p, value-b-addr=%p, key=\"%s\"",
457 element_obj_a
, element_obj_b
, key_str
);
458 BT_PUT(element_obj_b
);
463 BT_PUT(element_obj_b
);
471 bt_bool (* const compare_funcs
[])(const struct bt_value
*,
472 const struct bt_value
*) = {
473 [BT_VALUE_TYPE_NULL
] = bt_value_null_compare
,
474 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_compare
,
475 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_compare
,
476 [BT_VALUE_TYPE_FLOAT
] = bt_value_float_compare
,
477 [BT_VALUE_TYPE_STRING
] = bt_value_string_compare
,
478 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_compare
,
479 [BT_VALUE_TYPE_MAP
] = bt_value_map_compare
,
483 void bt_value_null_freeze(struct bt_value
*object
)
488 void bt_value_generic_freeze(struct bt_value
*object
)
490 object
->frozen
= BT_TRUE
;
494 void bt_value_array_freeze(struct bt_value
*object
)
497 struct bt_value_array
*typed_array_obj
=
498 BT_VALUE_TO_ARRAY(object
);
500 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
501 bt_value_freeze(g_ptr_array_index(typed_array_obj
->garray
, i
));
504 bt_value_generic_freeze(object
);
508 void bt_value_map_freeze(struct bt_value
*object
)
511 gpointer key
, element_obj
;
512 const struct bt_value_map
*map_obj
= BT_VALUE_TO_MAP(object
);
514 g_hash_table_iter_init(&iter
, map_obj
->ght
);
516 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
517 bt_value_freeze(element_obj
);
520 bt_value_generic_freeze(object
);
524 void (* const freeze_funcs
[])(struct bt_value
*) = {
525 [BT_VALUE_TYPE_NULL
] = bt_value_null_freeze
,
526 [BT_VALUE_TYPE_BOOL
] = bt_value_generic_freeze
,
527 [BT_VALUE_TYPE_INTEGER
] = bt_value_generic_freeze
,
528 [BT_VALUE_TYPE_FLOAT
] = bt_value_generic_freeze
,
529 [BT_VALUE_TYPE_STRING
] = bt_value_generic_freeze
,
530 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_freeze
,
531 [BT_VALUE_TYPE_MAP
] = bt_value_map_freeze
,
535 void bt_value_destroy(struct bt_object
*obj
)
537 struct bt_value
*value
;
539 value
= container_of(obj
, struct bt_value
, base
);
540 BT_ASSERT(value
->type
!= BT_VALUE_TYPE_UNKNOWN
);
541 BT_LOGD("Destroying value: addr=%p", value
);
543 if (bt_value_is_null(value
)) {
544 BT_LOGD_STR("Not destroying the null value singleton.");
548 if (destroy_funcs
[value
->type
]) {
549 destroy_funcs
[value
->type
](value
);
556 enum bt_value_status
_bt_value_freeze(struct bt_value
*object
)
558 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
562 if (object
->frozen
) {
566 BT_LOGD("Freezing value: addr=%p", object
);
567 freeze_funcs
[object
->type
](object
);
573 enum bt_value_type
bt_value_get_type(const struct bt_value
*object
)
575 BT_ASSERT_PRE_NON_NULL(object
, "Value object");
580 struct bt_value
bt_value_create_base(enum bt_value_type type
)
582 struct bt_value base
;
585 base
.frozen
= BT_FALSE
;
586 bt_object_init(&base
, bt_value_destroy
);
590 struct bt_value
*bt_value_bool_create_init(bt_bool val
)
592 struct bt_value_bool
*bool_obj
;
594 BT_LOGD("Creating boolean value object: val=%d", val
);
595 bool_obj
= g_new0(struct bt_value_bool
, 1);
597 BT_LOGE_STR("Failed to allocate one boolean value object.");
601 bool_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_BOOL
);
602 bool_obj
->value
= val
;
603 BT_LOGD("Created boolean value object: addr=%p", bool_obj
);
606 return BT_VALUE_FROM_CONCRETE(bool_obj
);
609 struct bt_value
*bt_value_bool_create(void)
611 return bt_value_bool_create_init(BT_FALSE
);
614 struct bt_value
*bt_value_integer_create_init(int64_t val
)
616 struct bt_value_integer
*integer_obj
;
618 BT_LOGD("Creating integer value object: val=%" PRId64
, val
);
619 integer_obj
= g_new0(struct bt_value_integer
, 1);
621 BT_LOGE_STR("Failed to allocate one integer value object.");
625 integer_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_INTEGER
);
626 integer_obj
->value
= val
;
627 BT_LOGD("Created integer value object: addr=%p",
631 return BT_VALUE_FROM_CONCRETE(integer_obj
);
634 struct bt_value
*bt_value_integer_create(void)
636 return bt_value_integer_create_init(0);
639 struct bt_value
*bt_value_float_create_init(double val
)
641 struct bt_value_float
*float_obj
;
643 BT_LOGD("Creating floating point number value object: val=%f", val
);
644 float_obj
= g_new0(struct bt_value_float
, 1);
646 BT_LOGE_STR("Failed to allocate one floating point number value object.");
650 float_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_FLOAT
);
651 float_obj
->value
= val
;
652 BT_LOGD("Created floating point number value object: addr=%p",
656 return BT_VALUE_FROM_CONCRETE(float_obj
);
659 struct bt_value
*bt_value_float_create(void)
661 return bt_value_float_create_init(0.);
664 struct bt_value
*bt_value_string_create_init(const char *val
)
666 struct bt_value_string
*string_obj
= NULL
;
669 BT_LOGW_STR("Invalid parameter: value is NULL.");
673 BT_LOGD("Creating string value object: val-len=%zu", strlen(val
));
674 string_obj
= g_new0(struct bt_value_string
, 1);
676 BT_LOGE_STR("Failed to allocate one string object.");
680 string_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_STRING
);
681 string_obj
->gstr
= g_string_new(val
);
682 if (!string_obj
->gstr
) {
683 BT_LOGE_STR("Failed to allocate a GString.");
689 BT_LOGD("Created string value object: addr=%p",
693 return BT_VALUE_FROM_CONCRETE(string_obj
);
696 struct bt_value
*bt_value_string_create(void)
698 return bt_value_string_create_init("");
701 struct bt_value
*bt_value_array_create(void)
703 struct bt_value_array
*array_obj
;
705 BT_LOGD_STR("Creating empty array value object.");
706 array_obj
= g_new0(struct bt_value_array
, 1);
708 BT_LOGE_STR("Failed to allocate one array object.");
712 array_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_ARRAY
);
713 array_obj
->garray
= bt_g_ptr_array_new_full(0,
714 (GDestroyNotify
) bt_put
);
715 if (!array_obj
->garray
) {
716 BT_LOGE_STR("Failed to allocate a GPtrArray.");
722 BT_LOGD("Created array value object: addr=%p",
726 return BT_VALUE_FROM_CONCRETE(array_obj
);
729 struct bt_value
*bt_value_map_create(void)
731 struct bt_value_map
*map_obj
;
733 BT_LOGD_STR("Creating empty map value object.");
734 map_obj
= g_new0(struct bt_value_map
, 1);
736 BT_LOGE_STR("Failed to allocate one map object.");
740 map_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_MAP
);
741 map_obj
->ght
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
742 NULL
, (GDestroyNotify
) bt_put
);
744 BT_LOGE_STR("Failed to allocate a GHashTable.");
750 BT_LOGD("Created map value object: addr=%p",
754 return BT_VALUE_FROM_CONCRETE(map_obj
);
757 enum bt_value_status
bt_value_bool_get(const struct bt_value
*bool_obj
,
760 BT_ASSERT_PRE_NON_NULL(bool_obj
, "Value object");
761 BT_ASSERT_PRE_NON_NULL(val
, "Raw value");
762 BT_ASSERT_PRE_VALUE_IS_TYPE(bool_obj
, BT_VALUE_TYPE_BOOL
);
763 *val
= BT_VALUE_TO_BOOL(bool_obj
)->value
;
764 return BT_VALUE_STATUS_OK
;
767 enum bt_value_status
bt_value_bool_set(struct bt_value
*bool_obj
, bt_bool val
)
769 BT_ASSERT_PRE_NON_NULL(bool_obj
, "Value object");
770 BT_ASSERT_PRE_VALUE_IS_TYPE(bool_obj
, BT_VALUE_TYPE_BOOL
);
771 BT_ASSERT_PRE_VALUE_HOT(bool_obj
, "Value object");
772 BT_VALUE_TO_BOOL(bool_obj
)->value
= val
;
773 BT_LOGV("Set boolean value's raw value: value-addr=%p, value=%d",
775 return BT_VALUE_STATUS_OK
;
778 enum bt_value_status
bt_value_integer_get(const struct bt_value
*integer_obj
,
781 BT_ASSERT_PRE_NON_NULL(integer_obj
, "Value object");
782 BT_ASSERT_PRE_NON_NULL(val
, "Raw value");
783 BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj
, BT_VALUE_TYPE_INTEGER
);
784 *val
= BT_VALUE_TO_INTEGER(integer_obj
)->value
;
785 return BT_VALUE_STATUS_OK
;
788 enum bt_value_status
bt_value_integer_set(struct bt_value
*integer_obj
,
791 BT_ASSERT_PRE_NON_NULL(integer_obj
, "Value object");
792 BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj
, BT_VALUE_TYPE_INTEGER
);
793 BT_ASSERT_PRE_VALUE_HOT(integer_obj
, "Value object");
794 BT_VALUE_TO_INTEGER(integer_obj
)->value
= val
;
795 BT_LOGV("Set integer value's raw value: value-addr=%p, value=%" PRId64
,
797 return BT_VALUE_STATUS_OK
;
800 enum bt_value_status
bt_value_float_get(const struct bt_value
*float_obj
,
803 BT_ASSERT_PRE_NON_NULL(float_obj
, "Value object");
804 BT_ASSERT_PRE_NON_NULL(val
, "Raw value");
805 BT_ASSERT_PRE_VALUE_IS_TYPE(float_obj
, BT_VALUE_TYPE_FLOAT
);
806 *val
= BT_VALUE_TO_FLOAT(float_obj
)->value
;
807 return BT_VALUE_STATUS_OK
;
810 enum bt_value_status
bt_value_float_set(struct bt_value
*float_obj
,
813 BT_ASSERT_PRE_NON_NULL(float_obj
, "Value object");
814 BT_ASSERT_PRE_VALUE_IS_TYPE(float_obj
, BT_VALUE_TYPE_FLOAT
);
815 BT_ASSERT_PRE_VALUE_HOT(float_obj
, "Value object");
816 BT_VALUE_TO_FLOAT(float_obj
)->value
= val
;
817 BT_LOGV("Set floating point number value's raw value: value-addr=%p, value=%f",
819 return BT_VALUE_STATUS_OK
;
822 enum bt_value_status
bt_value_string_get(const struct bt_value
*string_obj
,
825 BT_ASSERT_PRE_NON_NULL(string_obj
, "Value object");
826 BT_ASSERT_PRE_NON_NULL(val
, "Raw value");
827 BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj
, BT_VALUE_TYPE_STRING
);
828 *val
= BT_VALUE_TO_STRING(string_obj
)->gstr
->str
;
829 return BT_VALUE_STATUS_OK
;
832 enum bt_value_status
bt_value_string_set(struct bt_value
*string_obj
,
835 BT_ASSERT_PRE_NON_NULL(string_obj
, "Value object");
836 BT_ASSERT_PRE_NON_NULL(val
, "Raw value");
837 BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj
, BT_VALUE_TYPE_STRING
);
838 BT_ASSERT_PRE_VALUE_HOT(string_obj
, "Value object");
839 g_string_assign(BT_VALUE_TO_STRING(string_obj
)->gstr
, val
);
840 BT_LOGV("Set string value's raw value: value-addr=%p, raw-value-addr=%p",
842 return BT_VALUE_STATUS_OK
;
845 int64_t bt_value_array_size(const struct bt_value
*array_obj
)
847 BT_ASSERT_PRE_NON_NULL(array_obj
, "Value object");
848 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
849 return (int64_t) BT_VALUE_TO_ARRAY(array_obj
)->garray
->len
;
852 bt_bool
bt_value_array_is_empty(const struct bt_value
*array_obj
)
854 return bt_value_array_size(array_obj
) == 0;
857 struct bt_value
*bt_value_array_get(const struct bt_value
*array_obj
,
860 struct bt_value_array
*typed_array_obj
=
861 BT_VALUE_TO_ARRAY(array_obj
);
863 BT_ASSERT_PRE_NON_NULL(array_obj
, "Value object");
864 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
865 BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index
,
866 typed_array_obj
->garray
->len
);
867 return bt_get(g_ptr_array_index(typed_array_obj
->garray
, index
));
870 enum bt_value_status
bt_value_array_append(struct bt_value
*array_obj
,
871 struct bt_value
*element_obj
)
873 struct bt_value_array
*typed_array_obj
=
874 BT_VALUE_TO_ARRAY(array_obj
);
876 BT_ASSERT_PRE_NON_NULL(array_obj
, "Array value object");
877 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
878 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
879 BT_ASSERT_PRE_VALUE_HOT(array_obj
, "Array value object");
880 g_ptr_array_add(typed_array_obj
->garray
, element_obj
);
882 BT_LOGV("Appended element to array value: array-value-addr=%p, "
883 "element-value-addr=%p, new-size=%u",
884 array_obj
, element_obj
, typed_array_obj
->garray
->len
);
885 return BT_VALUE_STATUS_OK
;
888 enum bt_value_status
bt_value_array_append_bool(struct bt_value
*array_obj
,
891 enum bt_value_status ret
;
892 struct bt_value
*bool_obj
= NULL
;
894 bool_obj
= bt_value_bool_create_init(val
);
895 ret
= bt_value_array_append(array_obj
, bool_obj
);
900 enum bt_value_status
bt_value_array_append_integer(
901 struct bt_value
*array_obj
, int64_t val
)
903 enum bt_value_status ret
;
904 struct bt_value
*integer_obj
= NULL
;
906 integer_obj
= bt_value_integer_create_init(val
);
907 ret
= bt_value_array_append(array_obj
, integer_obj
);
912 enum bt_value_status
bt_value_array_append_float(struct bt_value
*array_obj
,
915 enum bt_value_status ret
;
916 struct bt_value
*float_obj
= NULL
;
918 float_obj
= bt_value_float_create_init(val
);
919 ret
= bt_value_array_append(array_obj
, float_obj
);
924 enum bt_value_status
bt_value_array_append_string(struct bt_value
*array_obj
,
927 enum bt_value_status ret
;
928 struct bt_value
*string_obj
= NULL
;
930 string_obj
= bt_value_string_create_init(val
);
931 ret
= bt_value_array_append(array_obj
, string_obj
);
936 enum bt_value_status
bt_value_array_append_empty_array(
937 struct bt_value
*array_obj
)
939 enum bt_value_status ret
;
940 struct bt_value
*empty_array_obj
= NULL
;
942 empty_array_obj
= bt_value_array_create();
943 ret
= bt_value_array_append(array_obj
, empty_array_obj
);
944 bt_put(empty_array_obj
);
948 enum bt_value_status
bt_value_array_append_empty_map(struct bt_value
*array_obj
)
950 enum bt_value_status ret
;
951 struct bt_value
*map_obj
= NULL
;
953 map_obj
= bt_value_map_create();
954 ret
= bt_value_array_append(array_obj
, map_obj
);
959 enum bt_value_status
bt_value_array_set(struct bt_value
*array_obj
,
960 uint64_t index
, struct bt_value
*element_obj
)
962 struct bt_value_array
*typed_array_obj
=
963 BT_VALUE_TO_ARRAY(array_obj
);
965 BT_ASSERT_PRE_NON_NULL(array_obj
, "Array value object");
966 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
967 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
968 BT_ASSERT_PRE_VALUE_HOT(array_obj
, "Array value object");
969 BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index
,
970 typed_array_obj
->garray
->len
);
971 bt_put(g_ptr_array_index(typed_array_obj
->garray
, index
));
972 g_ptr_array_index(typed_array_obj
->garray
, index
) = element_obj
;
974 BT_LOGV("Set array value's element: array-value-addr=%p, "
975 "index=%" PRIu64
", element-value-addr=%p",
976 array_obj
, index
, element_obj
);
977 return BT_VALUE_STATUS_OK
;
980 int64_t bt_value_map_size(const struct bt_value
*map_obj
)
982 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
983 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
984 return (int64_t) g_hash_table_size(BT_VALUE_TO_MAP(map_obj
)->ght
);
987 bt_bool
bt_value_map_is_empty(const struct bt_value
*map_obj
)
989 return bt_value_map_size(map_obj
) == 0;
992 struct bt_value
*bt_value_map_get(const struct bt_value
*map_obj
,
995 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
996 BT_ASSERT_PRE_NON_NULL(key
, "Key");
997 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
998 return bt_get(g_hash_table_lookup(BT_VALUE_TO_MAP(map_obj
)->ght
,
999 GUINT_TO_POINTER(g_quark_from_string(key
))));
1002 bt_bool
bt_value_map_has_key(const struct bt_value
*map_obj
, const char *key
)
1004 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
1005 BT_ASSERT_PRE_NON_NULL(key
, "Key");
1006 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1007 return bt_g_hash_table_contains(BT_VALUE_TO_MAP(map_obj
)->ght
,
1008 GUINT_TO_POINTER(g_quark_from_string(key
)));
1011 enum bt_value_status
bt_value_map_insert(struct bt_value
*map_obj
,
1012 const char *key
, struct bt_value
*element_obj
)
1014 BT_ASSERT_PRE_NON_NULL(map_obj
, "Map value object");
1015 BT_ASSERT_PRE_NON_NULL(key
, "Key");
1016 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
1017 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1018 BT_ASSERT_PRE_VALUE_HOT(map_obj
, "Map value object");
1019 g_hash_table_insert(BT_VALUE_TO_MAP(map_obj
)->ght
,
1020 GUINT_TO_POINTER(g_quark_from_string(key
)), element_obj
);
1021 bt_get(element_obj
);
1022 BT_LOGV("Inserted value into map value: map-value-addr=%p, "
1023 "key=\"%s\", element-value-addr=%p",
1024 map_obj
, key
, element_obj
);
1025 return BT_VALUE_STATUS_OK
;
1028 enum bt_value_status
bt_value_map_insert_bool(struct bt_value
*map_obj
,
1029 const char *key
, bt_bool val
)
1031 enum bt_value_status ret
;
1032 struct bt_value
*bool_obj
= NULL
;
1034 bool_obj
= bt_value_bool_create_init(val
);
1035 ret
= bt_value_map_insert(map_obj
, key
, bool_obj
);
1040 enum bt_value_status
bt_value_map_insert_integer(struct bt_value
*map_obj
,
1041 const char *key
, int64_t val
)
1043 enum bt_value_status ret
;
1044 struct bt_value
*integer_obj
= NULL
;
1046 integer_obj
= bt_value_integer_create_init(val
);
1047 ret
= bt_value_map_insert(map_obj
, key
, integer_obj
);
1048 bt_put(integer_obj
);
1052 enum bt_value_status
bt_value_map_insert_float(struct bt_value
*map_obj
,
1053 const char *key
, double val
)
1055 enum bt_value_status ret
;
1056 struct bt_value
*float_obj
= NULL
;
1058 float_obj
= bt_value_float_create_init(val
);
1059 ret
= bt_value_map_insert(map_obj
, key
, float_obj
);
1064 enum bt_value_status
bt_value_map_insert_string(struct bt_value
*map_obj
,
1065 const char *key
, const char *val
)
1067 enum bt_value_status ret
;
1068 struct bt_value
*string_obj
= NULL
;
1070 string_obj
= bt_value_string_create_init(val
);
1071 ret
= bt_value_map_insert(map_obj
, key
, string_obj
);
1076 enum bt_value_status
bt_value_map_insert_empty_array(struct bt_value
*map_obj
,
1079 enum bt_value_status ret
;
1080 struct bt_value
*array_obj
= NULL
;
1082 array_obj
= bt_value_array_create();
1083 ret
= bt_value_map_insert(map_obj
, key
, array_obj
);
1088 enum bt_value_status
bt_value_map_insert_empty_map(struct bt_value
*map_obj
,
1091 enum bt_value_status ret
;
1092 struct bt_value
*empty_map_obj
= NULL
;
1094 empty_map_obj
= bt_value_map_create();
1095 ret
= bt_value_map_insert(map_obj
, key
, empty_map_obj
);
1096 bt_put(empty_map_obj
);
1100 enum bt_value_status
bt_value_map_foreach(const struct bt_value
*map_obj
,
1101 bt_value_map_foreach_cb cb
, void *data
)
1103 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1104 gpointer key
, element_obj
;
1105 GHashTableIter iter
;
1106 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1108 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
1109 BT_ASSERT_PRE_NON_NULL(cb
, "Callback");
1110 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1111 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
1113 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
1114 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
1116 if (!cb(key_str
, element_obj
, data
)) {
1117 BT_LOGV("User canceled the loop: key=\"%s\", "
1118 "value-addr=%p, data=%p",
1119 key_str
, element_obj
, data
);
1120 ret
= BT_VALUE_STATUS_CANCELED
;
1128 struct extend_map_element_data
{
1129 struct bt_value
*extended_obj
;
1134 bt_bool
extend_map_element(const char *key
,
1135 struct bt_value
*extension_obj_elem
, void *data
)
1137 bt_bool ret
= BT_TRUE
;
1139 struct extend_map_element_data
*extend_data
= data
;
1141 /* Copy object which is to replace the current one */
1142 struct bt_value
*extension_obj_elem_copy
=
1143 bt_value_copy(extension_obj_elem
);
1145 /* Replace in extended object */
1146 if (bt_value_map_insert(extend_data
->extended_obj
, key
,
1147 extension_obj_elem_copy
)) {
1148 BT_LOGE("Cannot replace value in extended value: key=\"%s\", "
1149 "extended-value-addr=%p, element-value-addr=%p",
1150 key
, extend_data
->extended_obj
,
1151 extension_obj_elem_copy
);
1159 extend_data
->got_error
= BT_TRUE
;
1162 BT_PUT(extension_obj_elem_copy
);
1166 struct bt_value
*bt_value_map_extend(struct bt_value
*base_map_obj
,
1167 struct bt_value
*extension_obj
)
1169 struct bt_value
*extended_obj
= NULL
;
1170 struct extend_map_element_data extend_data
= { 0 };
1172 BT_ASSERT_PRE_NON_NULL(base_map_obj
, "Base value object");
1173 BT_ASSERT_PRE_NON_NULL(extension_obj
, "Extension value object");
1174 BT_ASSERT_PRE_VALUE_IS_TYPE(base_map_obj
, BT_VALUE_TYPE_MAP
);
1175 BT_ASSERT_PRE_VALUE_IS_TYPE(extension_obj
, BT_VALUE_TYPE_MAP
);
1176 BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p",
1177 base_map_obj
, extension_obj
);
1179 /* Create copy of base map object to start with */
1180 extended_obj
= bt_value_copy(base_map_obj
);
1181 if (!extended_obj
) {
1182 BT_LOGE("Cannot copy base value: base-value-addr=%p",
1188 * For each key in the extension map object, replace this key
1189 * in the copied map object.
1191 extend_data
.extended_obj
= extended_obj
;
1193 if (bt_value_map_foreach(extension_obj
, extend_map_element
,
1195 BT_LOGE("Cannot iterate on the extension object's elements: "
1196 "extension-value-addr=%p", extension_obj
);
1200 if (extend_data
.got_error
) {
1201 BT_LOGE("Failed to successfully iterate on the extension object's elements: "
1202 "extension-value-addr=%p", extension_obj
);
1206 BT_LOGD("Extended map value: extended-value-addr=%p",
1211 BT_PUT(extended_obj
);
1214 return extended_obj
;
1217 struct bt_value
*bt_value_copy(const struct bt_value
*object
)
1219 struct bt_value
*copy_obj
= NULL
;
1221 BT_ASSERT_PRE_NON_NULL(object
, "Value object");
1222 BT_LOGD("Copying value object: addr=%p", object
);
1223 copy_obj
= copy_funcs
[object
->type
](object
);
1225 BT_LOGD("Copied value object: copy-value-addr=%p",
1228 BT_LOGE_STR("Failed to copy value object.");
1234 bt_bool
bt_value_compare(const struct bt_value
*object_a
,
1235 const struct bt_value
*object_b
)
1237 bt_bool ret
= BT_FALSE
;
1239 BT_ASSERT_PRE_NON_NULL(object_a
, "Value object A");
1240 BT_ASSERT_PRE_NON_NULL(object_b
, "Value object B");
1242 if (object_a
->type
!= object_b
->type
) {
1243 BT_LOGV("Values are different: type mismatch: "
1244 "value-a-addr=%p, value-b-addr=%p, "
1245 "value-a-type=%s, value-b-type=%s",
1247 bt_value_type_string(object_a
->type
),
1248 bt_value_type_string(object_b
->type
));
1252 ret
= compare_funcs
[object_a
->type
](object_a
, object_b
);