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
= {
85 .type
= BT_VALUE_TYPE_NULL
,
89 struct bt_value
*bt_value_null
= &bt_value_null_instance
;
91 struct bt_value_bool
{
96 struct bt_value_integer
{
101 struct bt_value_float
{
102 struct bt_value base
;
106 struct bt_value_string
{
107 struct bt_value base
;
111 struct bt_value_array
{
112 struct bt_value base
;
116 struct bt_value_map
{
117 struct bt_value base
;
122 void bt_value_destroy(struct bt_object
*obj
);
125 void bt_value_string_destroy(struct bt_value
*object
)
127 g_string_free(BT_VALUE_TO_STRING(object
)->gstr
, TRUE
);
131 void bt_value_array_destroy(struct bt_value
*object
)
134 * Pointer array's registered value destructor will take care
135 * of putting each contained object.
137 g_ptr_array_free(BT_VALUE_TO_ARRAY(object
)->garray
, TRUE
);
141 void bt_value_map_destroy(struct bt_value
*object
)
144 * Hash table's registered value destructor will take care of
145 * putting each contained object. Keys are GQuarks and cannot
146 * be destroyed anyway.
148 g_hash_table_destroy(BT_VALUE_TO_MAP(object
)->ght
);
152 void (* const destroy_funcs
[])(struct bt_value
*) = {
153 [BT_VALUE_TYPE_NULL
] = NULL
,
154 [BT_VALUE_TYPE_BOOL
] = NULL
,
155 [BT_VALUE_TYPE_INTEGER
] = NULL
,
156 [BT_VALUE_TYPE_FLOAT
] = NULL
,
157 [BT_VALUE_TYPE_STRING
] = bt_value_string_destroy
,
158 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_destroy
,
159 [BT_VALUE_TYPE_MAP
] = bt_value_map_destroy
,
163 struct bt_value
*bt_value_null_copy(const struct bt_value
*null_obj
)
165 return bt_value_null
;
169 struct bt_value
*bt_value_bool_copy(const struct bt_value
*bool_obj
)
171 return bt_value_bool_create_init(BT_VALUE_TO_BOOL(bool_obj
)->value
);
175 struct bt_value
*bt_value_integer_copy(const struct bt_value
*integer_obj
)
177 return bt_value_integer_create_init(
178 BT_VALUE_TO_INTEGER(integer_obj
)->value
);
182 struct bt_value
*bt_value_float_copy(const struct bt_value
*float_obj
)
184 return bt_value_float_create_init(
185 BT_VALUE_TO_FLOAT(float_obj
)->value
);
189 struct bt_value
*bt_value_string_copy(const struct bt_value
*string_obj
)
191 return bt_value_string_create_init(
192 BT_VALUE_TO_STRING(string_obj
)->gstr
->str
);
196 struct bt_value
*bt_value_array_copy(const struct bt_value
*array_obj
)
200 struct bt_value
*copy_obj
;
201 struct bt_value_array
*typed_array_obj
;
203 BT_LOGD("Copying array value: addr=%p", array_obj
);
204 typed_array_obj
= BT_VALUE_TO_ARRAY(array_obj
);
205 copy_obj
= bt_value_array_create();
207 BT_LOGE_STR("Cannot create empty array value.");
211 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
212 struct bt_value
*element_obj_copy
;
213 struct bt_value
*element_obj
= bt_value_array_borrow(
216 BT_ASSERT(element_obj
);
217 BT_LOGD("Copying array value's element: element-addr=%p, "
218 "index=%d", element_obj
, i
);
219 element_obj_copy
= bt_value_copy(element_obj
);
220 if (!element_obj_copy
) {
221 BT_LOGE("Cannot copy array value's element: "
222 "array-addr=%p, index=%d",
228 ret
= bt_value_array_append(copy_obj
, element_obj_copy
);
229 BT_PUT(element_obj_copy
);
231 BT_LOGE("Cannot append to array value: addr=%p",
238 BT_LOGD("Copied array value: original-addr=%p, copy-addr=%p",
239 array_obj
, copy_obj
);
246 struct bt_value
*bt_value_map_copy(const struct bt_value
*map_obj
)
250 gpointer key
, element_obj
;
251 struct bt_value
*copy_obj
;
252 struct bt_value
*element_obj_copy
;
253 struct bt_value_map
*typed_map_obj
;
255 BT_LOGD("Copying map value: addr=%p", map_obj
);
256 typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
257 copy_obj
= bt_value_map_create();
262 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
264 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
265 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
268 BT_LOGD("Copying map value's element: element-addr=%p, "
269 "key=\"%s\"", element_obj
, key_str
);
270 element_obj_copy
= bt_value_copy(element_obj
);
271 if (!element_obj_copy
) {
272 BT_LOGE("Cannot copy map value's element: "
273 "map-addr=%p, key=\"%s\"",
279 ret
= bt_value_map_insert(copy_obj
, key_str
, element_obj_copy
);
280 BT_PUT(element_obj_copy
);
282 BT_LOGE("Cannot insert into map value: addr=%p, key=\"%s\"",
289 BT_LOGD("Copied map value: addr=%p", map_obj
);
296 struct bt_value
*(* const copy_funcs
[])(const struct bt_value
*) = {
297 [BT_VALUE_TYPE_NULL
] = bt_value_null_copy
,
298 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_copy
,
299 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_copy
,
300 [BT_VALUE_TYPE_FLOAT
] = bt_value_float_copy
,
301 [BT_VALUE_TYPE_STRING
] = bt_value_string_copy
,
302 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_copy
,
303 [BT_VALUE_TYPE_MAP
] = bt_value_map_copy
,
307 bt_bool
bt_value_null_compare(const struct bt_value
*object_a
,
308 const struct bt_value
*object_b
)
311 * Always BT_TRUE since bt_value_compare() already checks if both
312 * object_a and object_b have the same type, and in the case of
313 * null value objects, they're always the same if it is so.
319 bt_bool
bt_value_bool_compare(const struct bt_value
*object_a
,
320 const struct bt_value
*object_b
)
322 if (BT_VALUE_TO_BOOL(object_a
)->value
!=
323 BT_VALUE_TO_BOOL(object_b
)->value
) {
324 BT_LOGV("Boolean value objects are different: "
325 "bool-a-val=%d, bool-b-val=%d",
326 BT_VALUE_TO_BOOL(object_a
)->value
,
327 BT_VALUE_TO_BOOL(object_b
)->value
);
335 bt_bool
bt_value_integer_compare(const struct bt_value
*object_a
,
336 const struct bt_value
*object_b
)
338 if (BT_VALUE_TO_INTEGER(object_a
)->value
!=
339 BT_VALUE_TO_INTEGER(object_b
)->value
) {
340 BT_LOGV("Integer value objects are different: "
341 "int-a-val=%" PRId64
", int-b-val=%" PRId64
,
342 BT_VALUE_TO_INTEGER(object_a
)->value
,
343 BT_VALUE_TO_INTEGER(object_b
)->value
);
351 bt_bool
bt_value_float_compare(const struct bt_value
*object_a
,
352 const struct bt_value
*object_b
)
354 if (BT_VALUE_TO_FLOAT(object_a
)->value
!=
355 BT_VALUE_TO_FLOAT(object_b
)->value
) {
356 BT_LOGV("Floating point number value objects are different: "
357 "float-a-val=%f, float-b-val=%f",
358 BT_VALUE_TO_FLOAT(object_a
)->value
,
359 BT_VALUE_TO_FLOAT(object_b
)->value
);
367 bt_bool
bt_value_string_compare(const struct bt_value
*object_a
,
368 const struct bt_value
*object_b
)
370 if (strcmp(BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
371 BT_VALUE_TO_STRING(object_b
)->gstr
->str
) != 0) {
372 BT_LOGV("String value objects are different: "
373 "string-a-val=\"%s\", string-b-val=\"%s\"",
374 BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
375 BT_VALUE_TO_STRING(object_b
)->gstr
->str
);
383 bt_bool
bt_value_array_compare(const struct bt_value
*object_a
,
384 const struct bt_value
*object_b
)
387 bt_bool ret
= BT_TRUE
;
388 const struct bt_value_array
*array_obj_a
=
389 BT_VALUE_TO_ARRAY(object_a
);
391 if (bt_value_array_size(object_a
) != bt_value_array_size(object_b
)) {
392 BT_LOGV("Array values are different: size mismatch "
393 "value-a-addr=%p, value-b-addr=%p, "
394 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
396 bt_value_array_size(object_a
),
397 bt_value_array_size(object_b
));
402 for (i
= 0; i
< array_obj_a
->garray
->len
; ++i
) {
403 struct bt_value
*element_obj_a
;
404 struct bt_value
*element_obj_b
;
406 element_obj_a
= bt_value_array_borrow(object_a
, i
);
407 element_obj_b
= bt_value_array_borrow(object_b
, i
);
409 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
410 BT_LOGV("Array values's elements are different: "
411 "value-a-addr=%p, value-b-addr=%p, index=%d",
412 element_obj_a
, element_obj_b
, i
);
423 bt_bool
bt_value_map_compare(const struct bt_value
*object_a
,
424 const struct bt_value
*object_b
)
426 bt_bool ret
= BT_TRUE
;
428 gpointer key
, element_obj_a
;
429 const struct bt_value_map
*map_obj_a
= BT_VALUE_TO_MAP(object_a
);
431 if (bt_value_map_size(object_a
) != bt_value_map_size(object_b
)) {
432 BT_LOGV("Map values are different: size mismatch "
433 "value-a-addr=%p, value-b-addr=%p, "
434 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
436 bt_value_map_size(object_a
),
437 bt_value_map_size(object_b
));
442 g_hash_table_iter_init(&iter
, map_obj_a
->ght
);
444 while (g_hash_table_iter_next(&iter
, &key
, &element_obj_a
)) {
445 struct bt_value
*element_obj_b
;
446 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
448 element_obj_b
= bt_value_map_borrow(object_b
, key_str
);
450 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
451 BT_LOGV("Map values's elements are different: "
452 "value-a-addr=%p, value-b-addr=%p, key=\"%s\"",
453 element_obj_a
, element_obj_b
, key_str
);
464 bt_bool (* const compare_funcs
[])(const struct bt_value
*,
465 const struct bt_value
*) = {
466 [BT_VALUE_TYPE_NULL
] = bt_value_null_compare
,
467 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_compare
,
468 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_compare
,
469 [BT_VALUE_TYPE_FLOAT
] = bt_value_float_compare
,
470 [BT_VALUE_TYPE_STRING
] = bt_value_string_compare
,
471 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_compare
,
472 [BT_VALUE_TYPE_MAP
] = bt_value_map_compare
,
476 void bt_value_null_freeze(struct bt_value
*object
)
481 void bt_value_generic_freeze(struct bt_value
*object
)
483 object
->frozen
= BT_TRUE
;
487 void bt_value_array_freeze(struct bt_value
*object
)
490 struct bt_value_array
*typed_array_obj
=
491 BT_VALUE_TO_ARRAY(object
);
493 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
494 bt_value_freeze(g_ptr_array_index(typed_array_obj
->garray
, i
));
497 bt_value_generic_freeze(object
);
501 void bt_value_map_freeze(struct bt_value
*object
)
504 gpointer key
, element_obj
;
505 const struct bt_value_map
*map_obj
= BT_VALUE_TO_MAP(object
);
507 g_hash_table_iter_init(&iter
, map_obj
->ght
);
509 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
510 bt_value_freeze(element_obj
);
513 bt_value_generic_freeze(object
);
517 void (* const freeze_funcs
[])(struct bt_value
*) = {
518 [BT_VALUE_TYPE_NULL
] = bt_value_null_freeze
,
519 [BT_VALUE_TYPE_BOOL
] = bt_value_generic_freeze
,
520 [BT_VALUE_TYPE_INTEGER
] = bt_value_generic_freeze
,
521 [BT_VALUE_TYPE_FLOAT
] = bt_value_generic_freeze
,
522 [BT_VALUE_TYPE_STRING
] = bt_value_generic_freeze
,
523 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_freeze
,
524 [BT_VALUE_TYPE_MAP
] = bt_value_map_freeze
,
528 void bt_value_destroy(struct bt_object
*obj
)
530 struct bt_value
*value
;
532 value
= container_of(obj
, struct bt_value
, base
);
533 BT_ASSERT(value
->type
!= BT_VALUE_TYPE_UNKNOWN
);
534 BT_LOGD("Destroying value: addr=%p", value
);
536 if (bt_value_is_null(value
)) {
537 BT_LOGD_STR("Not destroying the null value singleton.");
541 if (destroy_funcs
[value
->type
]) {
542 destroy_funcs
[value
->type
](value
);
549 enum bt_value_status
_bt_value_freeze(struct bt_value
*object
)
551 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
555 if (object
->frozen
) {
559 BT_LOGD("Freezing value: addr=%p", object
);
560 freeze_funcs
[object
->type
](object
);
566 enum bt_value_type
bt_value_get_type(const struct bt_value
*object
)
568 BT_ASSERT_PRE_NON_NULL(object
, "Value object");
573 struct bt_value
bt_value_create_base(enum bt_value_type type
)
575 struct bt_value base
;
578 base
.frozen
= BT_FALSE
;
579 bt_object_init(&base
, bt_value_destroy
);
583 struct bt_value
*bt_value_bool_create_init(bt_bool val
)
585 struct bt_value_bool
*bool_obj
;
587 BT_LOGD("Creating boolean value object: val=%d", val
);
588 bool_obj
= g_new0(struct bt_value_bool
, 1);
590 BT_LOGE_STR("Failed to allocate one boolean value object.");
594 bool_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_BOOL
);
595 bool_obj
->value
= val
;
596 BT_LOGD("Created boolean value object: addr=%p", bool_obj
);
599 return BT_VALUE_FROM_CONCRETE(bool_obj
);
602 struct bt_value
*bt_value_bool_create(void)
604 return bt_value_bool_create_init(BT_FALSE
);
607 struct bt_value
*bt_value_integer_create_init(int64_t val
)
609 struct bt_value_integer
*integer_obj
;
611 BT_LOGD("Creating integer value object: val=%" PRId64
, val
);
612 integer_obj
= g_new0(struct bt_value_integer
, 1);
614 BT_LOGE_STR("Failed to allocate one integer value object.");
618 integer_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_INTEGER
);
619 integer_obj
->value
= val
;
620 BT_LOGD("Created integer value object: addr=%p",
624 return BT_VALUE_FROM_CONCRETE(integer_obj
);
627 struct bt_value
*bt_value_integer_create(void)
629 return bt_value_integer_create_init(0);
632 struct bt_value
*bt_value_float_create_init(double val
)
634 struct bt_value_float
*float_obj
;
636 BT_LOGD("Creating floating point number value object: val=%f", val
);
637 float_obj
= g_new0(struct bt_value_float
, 1);
639 BT_LOGE_STR("Failed to allocate one floating point number value object.");
643 float_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_FLOAT
);
644 float_obj
->value
= val
;
645 BT_LOGD("Created floating point number value object: addr=%p",
649 return BT_VALUE_FROM_CONCRETE(float_obj
);
652 struct bt_value
*bt_value_float_create(void)
654 return bt_value_float_create_init(0.);
657 struct bt_value
*bt_value_string_create_init(const char *val
)
659 struct bt_value_string
*string_obj
= NULL
;
662 BT_LOGW_STR("Invalid parameter: value is NULL.");
666 BT_LOGD("Creating string value object: val-len=%zu", strlen(val
));
667 string_obj
= g_new0(struct bt_value_string
, 1);
669 BT_LOGE_STR("Failed to allocate one string object.");
673 string_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_STRING
);
674 string_obj
->gstr
= g_string_new(val
);
675 if (!string_obj
->gstr
) {
676 BT_LOGE_STR("Failed to allocate a GString.");
682 BT_LOGD("Created string value object: addr=%p",
686 return BT_VALUE_FROM_CONCRETE(string_obj
);
689 struct bt_value
*bt_value_string_create(void)
691 return bt_value_string_create_init("");
694 struct bt_value
*bt_value_array_create(void)
696 struct bt_value_array
*array_obj
;
698 BT_LOGD_STR("Creating empty array value object.");
699 array_obj
= g_new0(struct bt_value_array
, 1);
701 BT_LOGE_STR("Failed to allocate one array object.");
705 array_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_ARRAY
);
706 array_obj
->garray
= bt_g_ptr_array_new_full(0,
707 (GDestroyNotify
) bt_put
);
708 if (!array_obj
->garray
) {
709 BT_LOGE_STR("Failed to allocate a GPtrArray.");
715 BT_LOGD("Created array value object: addr=%p",
719 return BT_VALUE_FROM_CONCRETE(array_obj
);
722 struct bt_value
*bt_value_map_create(void)
724 struct bt_value_map
*map_obj
;
726 BT_LOGD_STR("Creating empty map value object.");
727 map_obj
= g_new0(struct bt_value_map
, 1);
729 BT_LOGE_STR("Failed to allocate one map object.");
733 map_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_MAP
);
734 map_obj
->ght
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
735 NULL
, (GDestroyNotify
) bt_put
);
737 BT_LOGE_STR("Failed to allocate a GHashTable.");
743 BT_LOGD("Created map value object: addr=%p",
747 return BT_VALUE_FROM_CONCRETE(map_obj
);
750 enum bt_value_status
bt_value_bool_get(const struct bt_value
*bool_obj
,
753 BT_ASSERT_PRE_NON_NULL(bool_obj
, "Value object");
754 BT_ASSERT_PRE_NON_NULL(val
, "Raw value");
755 BT_ASSERT_PRE_VALUE_IS_TYPE(bool_obj
, BT_VALUE_TYPE_BOOL
);
756 *val
= BT_VALUE_TO_BOOL(bool_obj
)->value
;
757 return BT_VALUE_STATUS_OK
;
760 enum bt_value_status
bt_value_bool_set(struct bt_value
*bool_obj
, bt_bool val
)
762 BT_ASSERT_PRE_NON_NULL(bool_obj
, "Value object");
763 BT_ASSERT_PRE_VALUE_IS_TYPE(bool_obj
, BT_VALUE_TYPE_BOOL
);
764 BT_ASSERT_PRE_VALUE_HOT(bool_obj
, "Value object");
765 BT_VALUE_TO_BOOL(bool_obj
)->value
= val
;
766 BT_LOGV("Set boolean value's raw value: value-addr=%p, value=%d",
768 return BT_VALUE_STATUS_OK
;
771 enum bt_value_status
bt_value_integer_get(const struct bt_value
*integer_obj
,
774 BT_ASSERT_PRE_NON_NULL(integer_obj
, "Value object");
775 BT_ASSERT_PRE_NON_NULL(val
, "Raw value");
776 BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj
, BT_VALUE_TYPE_INTEGER
);
777 *val
= BT_VALUE_TO_INTEGER(integer_obj
)->value
;
778 return BT_VALUE_STATUS_OK
;
781 enum bt_value_status
bt_value_integer_set(struct bt_value
*integer_obj
,
784 BT_ASSERT_PRE_NON_NULL(integer_obj
, "Value object");
785 BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj
, BT_VALUE_TYPE_INTEGER
);
786 BT_ASSERT_PRE_VALUE_HOT(integer_obj
, "Value object");
787 BT_VALUE_TO_INTEGER(integer_obj
)->value
= val
;
788 BT_LOGV("Set integer value's raw value: value-addr=%p, value=%" PRId64
,
790 return BT_VALUE_STATUS_OK
;
793 enum bt_value_status
bt_value_float_get(const struct bt_value
*float_obj
,
796 BT_ASSERT_PRE_NON_NULL(float_obj
, "Value object");
797 BT_ASSERT_PRE_NON_NULL(val
, "Raw value");
798 BT_ASSERT_PRE_VALUE_IS_TYPE(float_obj
, BT_VALUE_TYPE_FLOAT
);
799 *val
= BT_VALUE_TO_FLOAT(float_obj
)->value
;
800 return BT_VALUE_STATUS_OK
;
803 enum bt_value_status
bt_value_float_set(struct bt_value
*float_obj
,
806 BT_ASSERT_PRE_NON_NULL(float_obj
, "Value object");
807 BT_ASSERT_PRE_VALUE_IS_TYPE(float_obj
, BT_VALUE_TYPE_FLOAT
);
808 BT_ASSERT_PRE_VALUE_HOT(float_obj
, "Value object");
809 BT_VALUE_TO_FLOAT(float_obj
)->value
= val
;
810 BT_LOGV("Set floating point number value's raw value: value-addr=%p, value=%f",
812 return BT_VALUE_STATUS_OK
;
815 enum bt_value_status
bt_value_string_get(const struct bt_value
*string_obj
,
818 BT_ASSERT_PRE_NON_NULL(string_obj
, "Value object");
819 BT_ASSERT_PRE_NON_NULL(val
, "Raw value");
820 BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj
, BT_VALUE_TYPE_STRING
);
821 *val
= BT_VALUE_TO_STRING(string_obj
)->gstr
->str
;
822 return BT_VALUE_STATUS_OK
;
825 enum bt_value_status
bt_value_string_set(struct bt_value
*string_obj
,
828 BT_ASSERT_PRE_NON_NULL(string_obj
, "Value object");
829 BT_ASSERT_PRE_NON_NULL(val
, "Raw value");
830 BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj
, BT_VALUE_TYPE_STRING
);
831 BT_ASSERT_PRE_VALUE_HOT(string_obj
, "Value object");
832 g_string_assign(BT_VALUE_TO_STRING(string_obj
)->gstr
, val
);
833 BT_LOGV("Set string value's raw value: value-addr=%p, raw-value-addr=%p",
835 return BT_VALUE_STATUS_OK
;
838 int64_t bt_value_array_size(const struct bt_value
*array_obj
)
840 BT_ASSERT_PRE_NON_NULL(array_obj
, "Value object");
841 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
842 return (int64_t) BT_VALUE_TO_ARRAY(array_obj
)->garray
->len
;
845 bt_bool
bt_value_array_is_empty(const struct bt_value
*array_obj
)
847 return bt_value_array_size(array_obj
) == 0;
850 struct bt_value
*bt_value_array_borrow(const struct bt_value
*array_obj
,
853 struct bt_value_array
*typed_array_obj
=
854 BT_VALUE_TO_ARRAY(array_obj
);
856 BT_ASSERT_PRE_NON_NULL(array_obj
, "Value object");
857 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
858 BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index
,
859 typed_array_obj
->garray
->len
);
860 return g_ptr_array_index(typed_array_obj
->garray
, index
);
863 enum bt_value_status
bt_value_array_append(struct bt_value
*array_obj
,
864 struct bt_value
*element_obj
)
866 struct bt_value_array
*typed_array_obj
=
867 BT_VALUE_TO_ARRAY(array_obj
);
869 BT_ASSERT_PRE_NON_NULL(array_obj
, "Array value object");
870 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
871 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
872 BT_ASSERT_PRE_VALUE_HOT(array_obj
, "Array value object");
873 g_ptr_array_add(typed_array_obj
->garray
, element_obj
);
875 BT_LOGV("Appended element to array value: array-value-addr=%p, "
876 "element-value-addr=%p, new-size=%u",
877 array_obj
, element_obj
, typed_array_obj
->garray
->len
);
878 return BT_VALUE_STATUS_OK
;
881 enum bt_value_status
bt_value_array_append_bool(struct bt_value
*array_obj
,
884 enum bt_value_status ret
;
885 struct bt_value
*bool_obj
= NULL
;
887 bool_obj
= bt_value_bool_create_init(val
);
888 ret
= bt_value_array_append(array_obj
, bool_obj
);
893 enum bt_value_status
bt_value_array_append_integer(
894 struct bt_value
*array_obj
, int64_t val
)
896 enum bt_value_status ret
;
897 struct bt_value
*integer_obj
= NULL
;
899 integer_obj
= bt_value_integer_create_init(val
);
900 ret
= bt_value_array_append(array_obj
, integer_obj
);
905 enum bt_value_status
bt_value_array_append_float(struct bt_value
*array_obj
,
908 enum bt_value_status ret
;
909 struct bt_value
*float_obj
= NULL
;
911 float_obj
= bt_value_float_create_init(val
);
912 ret
= bt_value_array_append(array_obj
, float_obj
);
917 enum bt_value_status
bt_value_array_append_string(struct bt_value
*array_obj
,
920 enum bt_value_status ret
;
921 struct bt_value
*string_obj
= NULL
;
923 string_obj
= bt_value_string_create_init(val
);
924 ret
= bt_value_array_append(array_obj
, string_obj
);
929 enum bt_value_status
bt_value_array_append_empty_array(
930 struct bt_value
*array_obj
)
932 enum bt_value_status ret
;
933 struct bt_value
*empty_array_obj
= NULL
;
935 empty_array_obj
= bt_value_array_create();
936 ret
= bt_value_array_append(array_obj
, empty_array_obj
);
937 bt_put(empty_array_obj
);
941 enum bt_value_status
bt_value_array_append_empty_map(struct bt_value
*array_obj
)
943 enum bt_value_status ret
;
944 struct bt_value
*map_obj
= NULL
;
946 map_obj
= bt_value_map_create();
947 ret
= bt_value_array_append(array_obj
, map_obj
);
952 enum bt_value_status
bt_value_array_set(struct bt_value
*array_obj
,
953 uint64_t index
, struct bt_value
*element_obj
)
955 struct bt_value_array
*typed_array_obj
=
956 BT_VALUE_TO_ARRAY(array_obj
);
958 BT_ASSERT_PRE_NON_NULL(array_obj
, "Array value object");
959 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
960 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj
, BT_VALUE_TYPE_ARRAY
);
961 BT_ASSERT_PRE_VALUE_HOT(array_obj
, "Array value object");
962 BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index
,
963 typed_array_obj
->garray
->len
);
964 bt_put(g_ptr_array_index(typed_array_obj
->garray
, index
));
965 g_ptr_array_index(typed_array_obj
->garray
, index
) = element_obj
;
967 BT_LOGV("Set array value's element: array-value-addr=%p, "
968 "index=%" PRIu64
", element-value-addr=%p",
969 array_obj
, index
, element_obj
);
970 return BT_VALUE_STATUS_OK
;
973 int64_t bt_value_map_size(const struct bt_value
*map_obj
)
975 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
976 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
977 return (int64_t) g_hash_table_size(BT_VALUE_TO_MAP(map_obj
)->ght
);
980 bt_bool
bt_value_map_is_empty(const struct bt_value
*map_obj
)
982 return bt_value_map_size(map_obj
) == 0;
985 struct bt_value
*bt_value_map_borrow(const struct bt_value
*map_obj
,
988 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
989 BT_ASSERT_PRE_NON_NULL(key
, "Key");
990 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
991 return g_hash_table_lookup(BT_VALUE_TO_MAP(map_obj
)->ght
,
992 GUINT_TO_POINTER(g_quark_from_string(key
)));
995 bt_bool
bt_value_map_has_key(const struct bt_value
*map_obj
, const char *key
)
997 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
998 BT_ASSERT_PRE_NON_NULL(key
, "Key");
999 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1000 return bt_g_hash_table_contains(BT_VALUE_TO_MAP(map_obj
)->ght
,
1001 GUINT_TO_POINTER(g_quark_from_string(key
)));
1004 enum bt_value_status
bt_value_map_insert(struct bt_value
*map_obj
,
1005 const char *key
, struct bt_value
*element_obj
)
1007 BT_ASSERT_PRE_NON_NULL(map_obj
, "Map value object");
1008 BT_ASSERT_PRE_NON_NULL(key
, "Key");
1009 BT_ASSERT_PRE_NON_NULL(element_obj
, "Element value object");
1010 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1011 BT_ASSERT_PRE_VALUE_HOT(map_obj
, "Map value object");
1012 g_hash_table_insert(BT_VALUE_TO_MAP(map_obj
)->ght
,
1013 GUINT_TO_POINTER(g_quark_from_string(key
)), element_obj
);
1014 bt_get(element_obj
);
1015 BT_LOGV("Inserted value into map value: map-value-addr=%p, "
1016 "key=\"%s\", element-value-addr=%p",
1017 map_obj
, key
, element_obj
);
1018 return BT_VALUE_STATUS_OK
;
1021 enum bt_value_status
bt_value_map_insert_bool(struct bt_value
*map_obj
,
1022 const char *key
, bt_bool val
)
1024 enum bt_value_status ret
;
1025 struct bt_value
*bool_obj
= NULL
;
1027 bool_obj
= bt_value_bool_create_init(val
);
1028 ret
= bt_value_map_insert(map_obj
, key
, bool_obj
);
1033 enum bt_value_status
bt_value_map_insert_integer(struct bt_value
*map_obj
,
1034 const char *key
, int64_t val
)
1036 enum bt_value_status ret
;
1037 struct bt_value
*integer_obj
= NULL
;
1039 integer_obj
= bt_value_integer_create_init(val
);
1040 ret
= bt_value_map_insert(map_obj
, key
, integer_obj
);
1041 bt_put(integer_obj
);
1045 enum bt_value_status
bt_value_map_insert_float(struct bt_value
*map_obj
,
1046 const char *key
, double val
)
1048 enum bt_value_status ret
;
1049 struct bt_value
*float_obj
= NULL
;
1051 float_obj
= bt_value_float_create_init(val
);
1052 ret
= bt_value_map_insert(map_obj
, key
, float_obj
);
1057 enum bt_value_status
bt_value_map_insert_string(struct bt_value
*map_obj
,
1058 const char *key
, const char *val
)
1060 enum bt_value_status ret
;
1061 struct bt_value
*string_obj
= NULL
;
1063 string_obj
= bt_value_string_create_init(val
);
1064 ret
= bt_value_map_insert(map_obj
, key
, string_obj
);
1069 enum bt_value_status
bt_value_map_insert_empty_array(struct bt_value
*map_obj
,
1072 enum bt_value_status ret
;
1073 struct bt_value
*array_obj
= NULL
;
1075 array_obj
= bt_value_array_create();
1076 ret
= bt_value_map_insert(map_obj
, key
, array_obj
);
1081 enum bt_value_status
bt_value_map_insert_empty_map(struct bt_value
*map_obj
,
1084 enum bt_value_status ret
;
1085 struct bt_value
*empty_map_obj
= NULL
;
1087 empty_map_obj
= bt_value_map_create();
1088 ret
= bt_value_map_insert(map_obj
, key
, empty_map_obj
);
1089 bt_put(empty_map_obj
);
1093 enum bt_value_status
bt_value_map_foreach(const struct bt_value
*map_obj
,
1094 bt_value_map_foreach_cb cb
, void *data
)
1096 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1097 gpointer key
, element_obj
;
1098 GHashTableIter iter
;
1099 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1101 BT_ASSERT_PRE_NON_NULL(map_obj
, "Value object");
1102 BT_ASSERT_PRE_NON_NULL(cb
, "Callback");
1103 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj
, BT_VALUE_TYPE_MAP
);
1104 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
1106 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
1107 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
1109 if (!cb(key_str
, element_obj
, data
)) {
1110 BT_LOGV("User canceled the loop: key=\"%s\", "
1111 "value-addr=%p, data=%p",
1112 key_str
, element_obj
, data
);
1113 ret
= BT_VALUE_STATUS_CANCELED
;
1121 struct extend_map_element_data
{
1122 struct bt_value
*extended_obj
;
1127 bt_bool
extend_map_element(const char *key
,
1128 struct bt_value
*extension_obj_elem
, void *data
)
1130 bt_bool ret
= BT_TRUE
;
1132 struct extend_map_element_data
*extend_data
= data
;
1134 /* Copy object which is to replace the current one */
1135 struct bt_value
*extension_obj_elem_copy
=
1136 bt_value_copy(extension_obj_elem
);
1138 /* Replace in extended object */
1139 if (bt_value_map_insert(extend_data
->extended_obj
, key
,
1140 extension_obj_elem_copy
)) {
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
);
1152 extend_data
->got_error
= BT_TRUE
;
1155 BT_PUT(extension_obj_elem_copy
);
1159 struct bt_value
*bt_value_map_extend(struct bt_value
*base_map_obj
,
1160 struct bt_value
*extension_obj
)
1162 struct bt_value
*extended_obj
= NULL
;
1163 struct extend_map_element_data extend_data
= { 0 };
1165 BT_ASSERT_PRE_NON_NULL(base_map_obj
, "Base value object");
1166 BT_ASSERT_PRE_NON_NULL(extension_obj
, "Extension value object");
1167 BT_ASSERT_PRE_VALUE_IS_TYPE(base_map_obj
, BT_VALUE_TYPE_MAP
);
1168 BT_ASSERT_PRE_VALUE_IS_TYPE(extension_obj
, BT_VALUE_TYPE_MAP
);
1169 BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p",
1170 base_map_obj
, extension_obj
);
1172 /* Create copy of base map object to start with */
1173 extended_obj
= bt_value_copy(base_map_obj
);
1174 if (!extended_obj
) {
1175 BT_LOGE("Cannot copy base value: base-value-addr=%p",
1181 * For each key in the extension map object, replace this key
1182 * in the copied map object.
1184 extend_data
.extended_obj
= extended_obj
;
1186 if (bt_value_map_foreach(extension_obj
, extend_map_element
,
1188 BT_LOGE("Cannot iterate on the extension object's elements: "
1189 "extension-value-addr=%p", extension_obj
);
1193 if (extend_data
.got_error
) {
1194 BT_LOGE("Failed to successfully iterate on the extension object's elements: "
1195 "extension-value-addr=%p", extension_obj
);
1199 BT_LOGD("Extended map value: extended-value-addr=%p",
1204 BT_PUT(extended_obj
);
1207 return extended_obj
;
1210 struct bt_value
*bt_value_copy(const struct bt_value
*object
)
1212 struct bt_value
*copy_obj
= NULL
;
1214 BT_ASSERT_PRE_NON_NULL(object
, "Value object");
1215 BT_LOGD("Copying value object: addr=%p", object
);
1216 copy_obj
= copy_funcs
[object
->type
](object
);
1218 BT_LOGD("Copied value object: copy-value-addr=%p",
1221 BT_LOGE_STR("Failed to copy value object.");
1227 bt_bool
bt_value_compare(const struct bt_value
*object_a
,
1228 const struct bt_value
*object_b
)
1230 bt_bool ret
= BT_FALSE
;
1232 BT_ASSERT_PRE_NON_NULL(object_a
, "Value object A");
1233 BT_ASSERT_PRE_NON_NULL(object_b
, "Value object B");
1235 if (object_a
->type
!= object_b
->type
) {
1236 BT_LOGV("Values are different: type mismatch: "
1237 "value-a-addr=%p, value-b-addr=%p, "
1238 "value-a-type=%s, value-b-type=%s",
1240 bt_value_type_string(object_a
->type
),
1241 bt_value_type_string(object_b
->type
));
1245 ret
= compare_funcs
[object_a
->type
](object_a
, object_b
);