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"
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>
41 #include <babeltrace/lib-logging-internal.h>
42 #include <babeltrace/object-internal.h>
44 #define BT_VALUE_FROM_CONCRETE(_concrete) ((struct bt_value *) (_concrete))
45 #define BT_VALUE_TO_BOOL(_base) ((struct bt_value_bool *) (_base))
46 #define BT_VALUE_TO_INTEGER(_base) ((struct bt_value_integer *) (_base))
47 #define BT_VALUE_TO_FLOAT(_base) ((struct bt_value_float *) (_base))
48 #define BT_VALUE_TO_STRING(_base) ((struct bt_value_string *) (_base))
49 #define BT_VALUE_TO_ARRAY(_base) ((struct bt_value_array *) (_base))
50 #define BT_VALUE_TO_MAP(_base) ((struct bt_value_map *) (_base))
53 struct bt_object base
;
54 enum bt_value_type type
;
59 struct bt_value bt_value_null_instance
= {
68 .type
= BT_VALUE_TYPE_NULL
,
72 struct bt_value
*bt_value_null
= &bt_value_null_instance
;
74 struct bt_value_bool
{
79 struct bt_value_integer
{
84 struct bt_value_float
{
89 struct bt_value_string
{
94 struct bt_value_array
{
100 struct bt_value base
;
105 void bt_value_destroy(struct bt_object
*obj
);
108 void bt_value_string_destroy(struct bt_value
*object
)
110 g_string_free(BT_VALUE_TO_STRING(object
)->gstr
, TRUE
);
114 void bt_value_array_destroy(struct bt_value
*object
)
117 * Pointer array's registered value destructor will take care
118 * of putting each contained object.
120 g_ptr_array_free(BT_VALUE_TO_ARRAY(object
)->garray
, TRUE
);
124 void bt_value_map_destroy(struct bt_value
*object
)
127 * Hash table's registered value destructor will take care of
128 * putting each contained object. Keys are GQuarks and cannot
129 * be destroyed anyway.
131 g_hash_table_destroy(BT_VALUE_TO_MAP(object
)->ght
);
135 void (* const destroy_funcs
[])(struct bt_value
*) = {
136 [BT_VALUE_TYPE_NULL
] = NULL
,
137 [BT_VALUE_TYPE_BOOL
] = NULL
,
138 [BT_VALUE_TYPE_INTEGER
] = NULL
,
139 [BT_VALUE_TYPE_FLOAT
] = NULL
,
140 [BT_VALUE_TYPE_STRING
] = bt_value_string_destroy
,
141 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_destroy
,
142 [BT_VALUE_TYPE_MAP
] = bt_value_map_destroy
,
146 struct bt_value
*bt_value_null_copy(const struct bt_value
*null_obj
)
148 return bt_value_null
;
152 struct bt_value
*bt_value_bool_copy(const struct bt_value
*bool_obj
)
154 return bt_value_bool_create_init(BT_VALUE_TO_BOOL(bool_obj
)->value
);
158 struct bt_value
*bt_value_integer_copy(const struct bt_value
*integer_obj
)
160 return bt_value_integer_create_init(
161 BT_VALUE_TO_INTEGER(integer_obj
)->value
);
165 struct bt_value
*bt_value_float_copy(const struct bt_value
*float_obj
)
167 return bt_value_float_create_init(
168 BT_VALUE_TO_FLOAT(float_obj
)->value
);
172 struct bt_value
*bt_value_string_copy(const struct bt_value
*string_obj
)
174 return bt_value_string_create_init(
175 BT_VALUE_TO_STRING(string_obj
)->gstr
->str
);
179 struct bt_value
*bt_value_array_copy(const struct bt_value
*array_obj
)
183 struct bt_value
*copy_obj
;
184 struct bt_value_array
*typed_array_obj
;
186 BT_LOGD("Copying array value: addr=%p", array_obj
);
187 typed_array_obj
= BT_VALUE_TO_ARRAY(array_obj
);
188 copy_obj
= bt_value_array_create();
191 BT_LOGE_STR("Cannot create empty array value.");
195 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
196 struct bt_value
*element_obj_copy
;
197 struct bt_value
*element_obj
= bt_value_array_get(array_obj
, i
);
200 BT_LOGE("Cannot get array value's element: "
207 element_obj_copy
= bt_value_copy(element_obj
);
210 if (!element_obj_copy
) {
211 BT_LOGE("Cannot copy array value's element: "
212 "array-addr=%p, index=%d",
218 ret
= bt_value_array_append(copy_obj
, element_obj_copy
);
219 BT_PUT(element_obj_copy
);
222 BT_LOGE("Cannot append to array value: addr=%p",
229 BT_LOGD("Copied array value: addr=%p", array_obj
);
236 struct bt_value
*bt_value_map_copy(const struct bt_value
*map_obj
)
240 gpointer key
, element_obj
;
241 struct bt_value
*copy_obj
;
242 struct bt_value
*element_obj_copy
;
243 struct bt_value_map
*typed_map_obj
;
245 BT_LOGD("Copying map value: addr=%p", map_obj
);
246 typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
247 copy_obj
= bt_value_map_create();
253 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
255 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
256 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
258 element_obj_copy
= bt_value_copy(element_obj
);
260 if (!element_obj_copy
) {
261 BT_LOGE("Cannot copy map value's element: "
262 "map-addr=%p, key=\"%s\"",
268 ret
= bt_value_map_insert(copy_obj
, key_str
, element_obj_copy
);
269 BT_PUT(element_obj_copy
);
272 BT_LOGE("Cannot insert into map value: addr=%p, key=\"%s\"",
279 BT_LOGD("Copied map value: addr=%p", map_obj
);
286 struct bt_value
*(* const copy_funcs
[])(const struct bt_value
*) = {
287 [BT_VALUE_TYPE_NULL
] = bt_value_null_copy
,
288 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_copy
,
289 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_copy
,
290 [BT_VALUE_TYPE_FLOAT
] = bt_value_float_copy
,
291 [BT_VALUE_TYPE_STRING
] = bt_value_string_copy
,
292 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_copy
,
293 [BT_VALUE_TYPE_MAP
] = bt_value_map_copy
,
297 bt_bool
bt_value_null_compare(const struct bt_value
*object_a
,
298 const struct bt_value
*object_b
)
301 * Always BT_TRUE since bt_value_compare() already checks if both
302 * object_a and object_b have the same type, and in the case of
303 * null value objects, they're always the same if it is so.
309 bt_bool
bt_value_bool_compare(const struct bt_value
*object_a
,
310 const struct bt_value
*object_b
)
312 return BT_VALUE_TO_BOOL(object_a
)->value
==
313 BT_VALUE_TO_BOOL(object_b
)->value
;
317 bt_bool
bt_value_integer_compare(const struct bt_value
*object_a
,
318 const struct bt_value
*object_b
)
320 return BT_VALUE_TO_INTEGER(object_a
)->value
==
321 BT_VALUE_TO_INTEGER(object_b
)->value
;
325 bt_bool
bt_value_float_compare(const struct bt_value
*object_a
,
326 const struct bt_value
*object_b
)
328 return BT_VALUE_TO_FLOAT(object_a
)->value
==
329 BT_VALUE_TO_FLOAT(object_b
)->value
;
333 bt_bool
bt_value_string_compare(const struct bt_value
*object_a
,
334 const struct bt_value
*object_b
)
336 return !strcmp(BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
337 BT_VALUE_TO_STRING(object_b
)->gstr
->str
);
341 bt_bool
bt_value_array_compare(const struct bt_value
*object_a
,
342 const struct bt_value
*object_b
)
345 bt_bool ret
= BT_TRUE
;
346 const struct bt_value_array
*array_obj_a
=
347 BT_VALUE_TO_ARRAY(object_a
);
349 if (bt_value_array_size(object_a
) != bt_value_array_size(object_b
)) {
350 BT_LOGV("Array values are different: size mismatch "
351 "value-a-addr=%p, value-b-addr=%p, "
352 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
354 bt_value_array_size(object_a
),
355 bt_value_array_size(object_b
));
360 for (i
= 0; i
< array_obj_a
->garray
->len
; ++i
) {
361 struct bt_value
*element_obj_a
;
362 struct bt_value
*element_obj_b
;
364 element_obj_a
= bt_value_array_get(object_a
, i
);
365 element_obj_b
= bt_value_array_get(object_b
, i
);
367 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
368 BT_LOGV("Array values's elements are different: "
369 "value-a-addr=%p, value-b-addr=%p, index=%d",
370 element_obj_a
, element_obj_b
, index
);
371 BT_PUT(element_obj_a
);
372 BT_PUT(element_obj_b
);
377 BT_PUT(element_obj_a
);
378 BT_PUT(element_obj_b
);
386 bt_bool
bt_value_map_compare(const struct bt_value
*object_a
,
387 const struct bt_value
*object_b
)
389 bt_bool ret
= BT_TRUE
;
391 gpointer key
, element_obj_a
;
392 const struct bt_value_map
*map_obj_a
= BT_VALUE_TO_MAP(object_a
);
394 if (bt_value_map_size(object_a
) != bt_value_map_size(object_b
)) {
395 BT_LOGV("Map values are different: size mismatch "
396 "value-a-addr=%p, value-b-addr=%p, "
397 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
399 bt_value_map_size(object_a
),
400 bt_value_map_size(object_b
));
405 g_hash_table_iter_init(&iter
, map_obj_a
->ght
);
407 while (g_hash_table_iter_next(&iter
, &key
, &element_obj_a
)) {
408 struct bt_value
*element_obj_b
;
409 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
411 element_obj_b
= bt_value_map_get(object_b
, key_str
);
413 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
414 BT_LOGV("Map values's elements are different: "
415 "value-a-addr=%p, value-b-addr=%p, key=\"%s\"",
416 element_obj_a
, element_obj_b
, key_str
);
417 BT_PUT(element_obj_b
);
422 BT_PUT(element_obj_b
);
430 bt_bool (* const compare_funcs
[])(const struct bt_value
*,
431 const struct bt_value
*) = {
432 [BT_VALUE_TYPE_NULL
] = bt_value_null_compare
,
433 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_compare
,
434 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_compare
,
435 [BT_VALUE_TYPE_FLOAT
] = bt_value_float_compare
,
436 [BT_VALUE_TYPE_STRING
] = bt_value_string_compare
,
437 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_compare
,
438 [BT_VALUE_TYPE_MAP
] = bt_value_map_compare
,
441 void bt_value_null_freeze(struct bt_value
*object
)
445 void bt_value_generic_freeze(struct bt_value
*object
)
447 object
->is_frozen
= BT_TRUE
;
450 void bt_value_array_freeze(struct bt_value
*object
)
453 struct bt_value_array
*typed_array_obj
=
454 BT_VALUE_TO_ARRAY(object
);
456 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
457 struct bt_value
*element_obj
=
458 g_ptr_array_index(typed_array_obj
->garray
, i
);
460 bt_value_freeze(element_obj
);
463 bt_value_generic_freeze(object
);
466 void bt_value_map_freeze(struct bt_value
*object
)
469 gpointer key
, element_obj
;
470 const struct bt_value_map
*map_obj
= BT_VALUE_TO_MAP(object
);
472 g_hash_table_iter_init(&iter
, map_obj
->ght
);
474 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
475 bt_value_freeze(element_obj
);
478 bt_value_generic_freeze(object
);
482 void (* const freeze_funcs
[])(struct bt_value
*) = {
483 [BT_VALUE_TYPE_NULL
] = bt_value_null_freeze
,
484 [BT_VALUE_TYPE_BOOL
] = bt_value_generic_freeze
,
485 [BT_VALUE_TYPE_INTEGER
] = bt_value_generic_freeze
,
486 [BT_VALUE_TYPE_FLOAT
] = bt_value_generic_freeze
,
487 [BT_VALUE_TYPE_STRING
] = bt_value_generic_freeze
,
488 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_freeze
,
489 [BT_VALUE_TYPE_MAP
] = bt_value_map_freeze
,
493 void bt_value_destroy(struct bt_object
*obj
)
495 struct bt_value
*value
;
497 value
= container_of(obj
, struct bt_value
, base
);
498 assert(value
->type
!= BT_VALUE_TYPE_UNKNOWN
);
500 BT_LOGD("Destroying value: addr=%p", value
);
502 if (bt_value_is_null(value
)) {
503 BT_LOGD_STR("Not destroying the null value singleton.");
507 if (destroy_funcs
[value
->type
]) {
508 destroy_funcs
[value
->type
](value
);
514 enum bt_value_status
bt_value_freeze(struct bt_value
*object
)
516 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
519 BT_LOGW_STR("Invalid parameter: value object is NULL.");
520 ret
= BT_VALUE_STATUS_INVAL
;
524 if (object
->is_frozen
) {
528 BT_LOGD("Freezing value: addr=%p", object
);
529 freeze_funcs
[object
->type
](object
);
535 bt_bool
bt_value_is_frozen(const struct bt_value
*object
)
537 return object
&& object
->is_frozen
;
540 enum bt_value_type
bt_value_get_type(const struct bt_value
*object
)
543 BT_LOGW_STR("Invalid parameter: value object is NULL.");
544 return BT_VALUE_TYPE_UNKNOWN
;
551 struct bt_value
bt_value_create_base(enum bt_value_type type
)
553 struct bt_value base
;
556 base
.is_frozen
= BT_FALSE
;
557 bt_object_init(&base
, bt_value_destroy
);
561 struct bt_value
*bt_value_bool_create_init(bt_bool val
)
563 struct bt_value_bool
*bool_obj
;
565 BT_LOGD("Creating boolean value object: val=%d", val
);
566 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 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);
594 BT_LOGE_STR("Failed to allocate one integer value object.");
598 integer_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_INTEGER
);
599 integer_obj
->value
= val
;
600 BT_LOGD("Created integer value object: addr=%p",
604 return BT_VALUE_FROM_CONCRETE(integer_obj
);
607 struct bt_value
*bt_value_integer_create(void)
609 return bt_value_integer_create_init(0);
612 struct bt_value
*bt_value_float_create_init(double val
)
614 struct bt_value_float
*float_obj
;
616 BT_LOGD("Creating floating point number value object: val=%f", val
);
617 float_obj
= g_new0(struct bt_value_float
, 1);
620 BT_LOGE_STR("Failed to allocate one floating point number value object.");
624 float_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_FLOAT
);
625 float_obj
->value
= val
;
626 BT_LOGD("Created floating point number value object: addr=%p",
630 return BT_VALUE_FROM_CONCRETE(float_obj
);
633 struct bt_value
*bt_value_float_create(void)
635 return bt_value_float_create_init(0.);
638 struct bt_value
*bt_value_string_create_init(const char *val
)
640 struct bt_value_string
*string_obj
= NULL
;
643 BT_LOGW_STR("Invalid parameter: value is NULL.");
647 BT_LOGD("Creating string value object: val-len=%u", strlen(val
));
648 string_obj
= g_new0(struct bt_value_string
, 1);
651 BT_LOGE_STR("Failed to allocate one string object.");
655 string_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_STRING
);
656 string_obj
->gstr
= g_string_new(val
);
658 if (!string_obj
->gstr
) {
659 BT_LOGE_STR("Failed to allocate a GString.");
665 BT_LOGD("Created string value object: addr=%p",
669 return BT_VALUE_FROM_CONCRETE(string_obj
);
672 struct bt_value
*bt_value_string_create(void)
674 return bt_value_string_create_init("");
677 struct bt_value
*bt_value_array_create(void)
679 struct bt_value_array
*array_obj
;
681 BT_LOGD_STR("Creating empty array value object.");
682 array_obj
= g_new0(struct bt_value_array
, 1);
685 BT_LOGE_STR("Failed to allocate one array object.");
689 array_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_ARRAY
);
690 array_obj
->garray
= bt_g_ptr_array_new_full(0,
691 (GDestroyNotify
) bt_put
);
693 if (!array_obj
->garray
) {
694 BT_LOGE_STR("Failed to allocate a GPtrArray.");
700 BT_LOGD("Created array value object: addr=%p",
704 return BT_VALUE_FROM_CONCRETE(array_obj
);
707 struct bt_value
*bt_value_map_create(void)
709 struct bt_value_map
*map_obj
;
711 BT_LOGD_STR("Creating empty map value object.");
712 map_obj
= g_new0(struct bt_value_map
, 1);
715 BT_LOGE_STR("Failed to allocate one map object.");
719 map_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_MAP
);
720 map_obj
->ght
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
721 NULL
, (GDestroyNotify
) bt_put
);
724 BT_LOGE_STR("Failed to allocate a GHashTable.");
730 BT_LOGD("Created map value object: addr=%p",
734 return BT_VALUE_FROM_CONCRETE(map_obj
);
737 enum bt_value_status
bt_value_bool_get(const struct bt_value
*bool_obj
,
740 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
741 struct bt_value_bool
*typed_bool_obj
= BT_VALUE_TO_BOOL(bool_obj
);
743 if (!bool_obj
|| !val
) {
744 BT_LOGW("Invalid parameter: value object or value is NULL: "
745 "value-addr=%p, raw-value-addr=%p",
747 ret
= BT_VALUE_STATUS_INVAL
;
751 if (!bt_value_is_bool(bool_obj
)) {
752 BT_LOGW("Invalid parameter: value is not a boolean value: addr=%p, "
753 "type=%d", bool_obj
, bool_obj
->type
);
754 ret
= BT_VALUE_STATUS_INVAL
;
758 *val
= typed_bool_obj
->value
;
764 enum bt_value_status
bt_value_bool_set(struct bt_value
*bool_obj
, bt_bool val
)
766 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
767 struct bt_value_bool
*typed_bool_obj
= BT_VALUE_TO_BOOL(bool_obj
);
770 BT_LOGW_STR("Invalid parameter: value object is NULL.");
771 ret
= BT_VALUE_STATUS_INVAL
;
775 if (!bt_value_is_bool(bool_obj
)) {
776 BT_LOGW("Invalid parameter: value is not a boolean value: addr=%p, "
777 "type=%d", bool_obj
, bool_obj
->type
);
778 ret
= BT_VALUE_STATUS_INVAL
;
782 if (bool_obj
->is_frozen
) {
783 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
785 ret
= BT_VALUE_STATUS_FROZEN
;
789 typed_bool_obj
->value
= val
;
790 BT_LOGV("Set boolean value's raw value: value-addr=%p, value=%d",
797 enum bt_value_status
bt_value_integer_get(const struct bt_value
*integer_obj
,
800 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
801 struct bt_value_integer
*typed_integer_obj
=
802 BT_VALUE_TO_INTEGER(integer_obj
);
804 if (!integer_obj
|| !val
) {
805 BT_LOGW("Invalid parameter: value object or value is NULL: "
806 "value-addr=%p, raw-value-addr=%p",
808 ret
= BT_VALUE_STATUS_INVAL
;
812 if (!bt_value_is_integer(integer_obj
)) {
813 BT_LOGW("Invalid parameter: value is not an integer value: addr=%p, "
814 "type=%d", integer_obj
, integer_obj
->type
);
815 ret
= BT_VALUE_STATUS_INVAL
;
819 *val
= typed_integer_obj
->value
;
825 enum bt_value_status
bt_value_integer_set(struct bt_value
*integer_obj
,
828 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
829 struct bt_value_integer
*typed_integer_obj
=
830 BT_VALUE_TO_INTEGER(integer_obj
);
833 BT_LOGW_STR("Invalid parameter: value object is NULL.");
834 ret
= BT_VALUE_STATUS_INVAL
;
838 if (!bt_value_is_integer(integer_obj
)) {
839 BT_LOGW("Invalid parameter: value is not an integer value: addr=%p, "
840 "type=%d", integer_obj
, integer_obj
->type
);
841 ret
= BT_VALUE_STATUS_INVAL
;
845 if (integer_obj
->is_frozen
) {
846 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
848 ret
= BT_VALUE_STATUS_FROZEN
;
852 typed_integer_obj
->value
= val
;
853 BT_LOGV("Set integer value's raw value: value-addr=%p, value=%" PRId64
,
860 enum bt_value_status
bt_value_float_get(const struct bt_value
*float_obj
,
863 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
864 struct bt_value_float
*typed_float_obj
=
865 BT_VALUE_TO_FLOAT(float_obj
);
867 if (!float_obj
|| !val
) {
868 BT_LOGW("Invalid parameter: value object or value is NULL: "
869 "value-addr=%p, raw-value-addr=%p",
871 ret
= BT_VALUE_STATUS_INVAL
;
875 if (!bt_value_is_float(float_obj
)) {
876 BT_LOGW("Invalid parameter: value is not a floating point number value: addr=%p, "
877 "type=%d", float_obj
, float_obj
->type
);
878 ret
= BT_VALUE_STATUS_INVAL
;
882 *val
= typed_float_obj
->value
;
888 enum bt_value_status
bt_value_float_set(struct bt_value
*float_obj
,
891 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
892 struct bt_value_float
*typed_float_obj
=
893 BT_VALUE_TO_FLOAT(float_obj
);
896 BT_LOGW_STR("Invalid parameter: value object is NULL.");
897 ret
= BT_VALUE_STATUS_INVAL
;
901 if (!bt_value_is_float(float_obj
)) {
902 BT_LOGW("Invalid parameter: value is not a floating point number value: addr=%p, "
903 "type=%d", float_obj
, float_obj
->type
);
904 ret
= BT_VALUE_STATUS_INVAL
;
908 if (float_obj
->is_frozen
) {
909 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
911 ret
= BT_VALUE_STATUS_FROZEN
;
915 typed_float_obj
->value
= val
;
916 BT_LOGV("Set floating point number value's raw value: value-addr=%p, value=%f",
923 enum bt_value_status
bt_value_string_get(const struct bt_value
*string_obj
,
926 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
927 struct bt_value_string
*typed_string_obj
=
928 BT_VALUE_TO_STRING(string_obj
);
930 if (!string_obj
|| !val
) {
931 BT_LOGW("Invalid parameter: value object or value is NULL: "
932 "value-addr=%p, raw-value-addr=%p",
934 ret
= BT_VALUE_STATUS_INVAL
;
938 if (!bt_value_is_string(string_obj
)) {
939 BT_LOGW("Invalid parameter: value is not a string value: addr=%p, "
940 "type=%d", string_obj
, string_obj
->type
);
941 ret
= BT_VALUE_STATUS_INVAL
;
945 *val
= typed_string_obj
->gstr
->str
;
951 enum bt_value_status
bt_value_string_set(struct bt_value
*string_obj
,
954 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
955 struct bt_value_string
*typed_string_obj
=
956 BT_VALUE_TO_STRING(string_obj
);
958 if (!string_obj
|| !val
) {
959 BT_LOGW("Invalid parameter: value object or value is NULL: "
960 "value-addr=%p, raw-value-addr=%p",
962 ret
= BT_VALUE_STATUS_INVAL
;
966 if (!bt_value_is_string(string_obj
)) {
967 BT_LOGW("Invalid parameter: value is not a string value: addr=%p, "
968 "type=%d", string_obj
, string_obj
->type
);
969 ret
= BT_VALUE_STATUS_INVAL
;
973 if (string_obj
->is_frozen
) {
974 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
976 ret
= BT_VALUE_STATUS_FROZEN
;
980 g_string_assign(typed_string_obj
->gstr
, val
);
981 BT_LOGV("Set string value's raw value: value-addr=%p, raw-value-addr=%p",
988 int64_t bt_value_array_size(const struct bt_value
*array_obj
)
991 struct bt_value_array
*typed_array_obj
=
992 BT_VALUE_TO_ARRAY(array_obj
);
995 BT_LOGW_STR("Invalid parameter: value object is NULL.");
996 ret
= (int64_t) BT_VALUE_STATUS_INVAL
;
1000 if (!bt_value_is_array(array_obj
)) {
1001 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1002 "type=%d", array_obj
, array_obj
->type
);
1003 ret
= BT_VALUE_STATUS_INVAL
;
1007 ret
= (int64_t) typed_array_obj
->garray
->len
;
1013 bt_bool
bt_value_array_is_empty(const struct bt_value
*array_obj
)
1015 return bt_value_array_size(array_obj
) == 0;
1018 struct bt_value
*bt_value_array_get(const struct bt_value
*array_obj
,
1021 struct bt_value
*ret
;
1022 struct bt_value_array
*typed_array_obj
=
1023 BT_VALUE_TO_ARRAY(array_obj
);
1026 BT_LOGW("Invalid parameter: value object is NULL: index=%" PRIu64
,
1032 if (!bt_value_is_array(array_obj
)) {
1033 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1034 "type=%d", array_obj
, array_obj
->type
);
1039 if (index
>= typed_array_obj
->garray
->len
) {
1040 BT_LOGW("Invalid parameter: index is out of bounds: "
1041 "addr=%p, index=%" PRIu64
,
1047 ret
= g_ptr_array_index(typed_array_obj
->garray
, index
);
1054 enum bt_value_status
bt_value_array_append(struct bt_value
*array_obj
,
1055 struct bt_value
*element_obj
)
1057 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1058 struct bt_value_array
*typed_array_obj
=
1059 BT_VALUE_TO_ARRAY(array_obj
);
1061 if (!array_obj
|| !element_obj
) {
1062 BT_LOGW("Invalid parameter: array value or element value is NULL: "
1063 "array-value-addr=%p, element-value-addr=%p",
1064 array_obj
, element_obj
);
1065 ret
= BT_VALUE_STATUS_INVAL
;
1069 if (!bt_value_is_array(array_obj
)) {
1070 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1071 "type=%d", array_obj
, array_obj
->type
);
1072 ret
= BT_VALUE_STATUS_INVAL
;
1076 if (array_obj
->is_frozen
) {
1077 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1079 ret
= BT_VALUE_STATUS_FROZEN
;
1083 g_ptr_array_add(typed_array_obj
->garray
, element_obj
);
1084 bt_get(element_obj
);
1085 BT_LOGV("Appended element to array value: array-value-addr=%p, "
1086 "element-value-addr=%p, new-size=%u",
1087 array_obj
, element_obj
, typed_array_obj
->garray
->len
);
1093 enum bt_value_status
bt_value_array_append_bool(struct bt_value
*array_obj
,
1096 enum bt_value_status ret
;
1097 struct bt_value
*bool_obj
= NULL
;
1099 bool_obj
= bt_value_bool_create_init(val
);
1100 ret
= bt_value_array_append(array_obj
, bool_obj
);
1105 enum bt_value_status
bt_value_array_append_integer(
1106 struct bt_value
*array_obj
, int64_t val
)
1108 enum bt_value_status ret
;
1109 struct bt_value
*integer_obj
= NULL
;
1111 integer_obj
= bt_value_integer_create_init(val
);
1112 ret
= bt_value_array_append(array_obj
, integer_obj
);
1113 bt_put(integer_obj
);
1117 enum bt_value_status
bt_value_array_append_float(struct bt_value
*array_obj
,
1120 enum bt_value_status ret
;
1121 struct bt_value
*float_obj
= NULL
;
1123 float_obj
= bt_value_float_create_init(val
);
1124 ret
= bt_value_array_append(array_obj
, float_obj
);
1129 enum bt_value_status
bt_value_array_append_string(struct bt_value
*array_obj
,
1132 enum bt_value_status ret
;
1133 struct bt_value
*string_obj
= NULL
;
1135 string_obj
= bt_value_string_create_init(val
);
1136 ret
= bt_value_array_append(array_obj
, string_obj
);
1141 enum bt_value_status
bt_value_array_append_empty_array(
1142 struct bt_value
*array_obj
)
1144 enum bt_value_status ret
;
1145 struct bt_value
*empty_array_obj
= NULL
;
1147 empty_array_obj
= bt_value_array_create();
1148 ret
= bt_value_array_append(array_obj
, empty_array_obj
);
1149 bt_put(empty_array_obj
);
1153 enum bt_value_status
bt_value_array_append_empty_map(struct bt_value
*array_obj
)
1155 enum bt_value_status ret
;
1156 struct bt_value
*map_obj
= NULL
;
1158 map_obj
= bt_value_map_create();
1159 ret
= bt_value_array_append(array_obj
, map_obj
);
1164 enum bt_value_status
bt_value_array_set(struct bt_value
*array_obj
,
1165 uint64_t index
, struct bt_value
*element_obj
)
1167 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1168 struct bt_value_array
*typed_array_obj
=
1169 BT_VALUE_TO_ARRAY(array_obj
);
1171 if (!array_obj
|| !element_obj
) {
1172 BT_LOGW("Invalid parameter: array value or element value is NULL: "
1173 "index=%" PRIu64
", array-value-addr=%p, element-value-addr=%p",
1174 index
, array_obj
, element_obj
);
1175 ret
= BT_VALUE_STATUS_INVAL
;
1179 if (!bt_value_is_array(array_obj
)) {
1180 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1181 "type=%d", array_obj
, array_obj
->type
);
1182 ret
= BT_VALUE_STATUS_INVAL
;
1186 if (index
>= typed_array_obj
->garray
->len
) {
1187 BT_LOGW("Invalid parameter: index is out of bounds: "
1188 "addr=%p, index=%" PRIu64
,
1190 ret
= BT_VALUE_STATUS_INVAL
;
1194 if (array_obj
->is_frozen
) {
1195 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1197 ret
= BT_VALUE_STATUS_FROZEN
;
1201 bt_put(g_ptr_array_index(typed_array_obj
->garray
, index
));
1202 g_ptr_array_index(typed_array_obj
->garray
, index
) = element_obj
;
1203 bt_get(element_obj
);
1204 BT_LOGV("Set array value's element: array-value-addr=%p, "
1205 "index=%" PRIu64
", element-value-addr=%p",
1206 array_obj
, index
, element_obj
);
1212 int64_t bt_value_map_size(const struct bt_value
*map_obj
)
1215 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1218 BT_LOGW_STR("Invalid parameter: value object is NULL.");
1219 ret
= (int64_t) BT_VALUE_STATUS_INVAL
;
1223 if (!bt_value_is_map(map_obj
)) {
1224 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1225 "type=%d", map_obj
, map_obj
->type
);
1226 ret
= (int64_t) BT_VALUE_STATUS_INVAL
;
1230 ret
= (int64_t) g_hash_table_size(typed_map_obj
->ght
);
1236 bt_bool
bt_value_map_is_empty(const struct bt_value
*map_obj
)
1238 return bt_value_map_size(map_obj
) == 0;
1241 struct bt_value
*bt_value_map_get(const struct bt_value
*map_obj
,
1245 struct bt_value
*ret
;
1246 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1248 if (!map_obj
|| !key
) {
1249 BT_LOGW("Invalid parameter: value object or key is NULL: "
1250 "value-addr=%p, key-addr=%p", map_obj
, key
);
1255 if (!bt_value_is_map(map_obj
)) {
1256 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1257 "type=%d", map_obj
, map_obj
->type
);
1262 quark
= g_quark_from_string(key
);
1263 ret
= g_hash_table_lookup(typed_map_obj
->ght
, GUINT_TO_POINTER(quark
));
1273 bt_bool
bt_value_map_has_key(const struct bt_value
*map_obj
, const char *key
)
1277 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1279 if (!map_obj
|| !key
) {
1280 BT_LOGW("Invalid parameter: value object or key is NULL: "
1281 "value-addr=%p, key-addr=%p", map_obj
, key
);
1286 if (!bt_value_is_map(map_obj
)) {
1287 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1288 "type=%d", map_obj
, map_obj
->type
);
1293 quark
= g_quark_from_string(key
);
1294 ret
= bt_g_hash_table_contains(typed_map_obj
->ght
,
1295 GUINT_TO_POINTER(quark
));
1301 enum bt_value_status
bt_value_map_insert(struct bt_value
*map_obj
,
1302 const char *key
, struct bt_value
*element_obj
)
1305 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1306 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1308 if (!map_obj
|| !key
|| !element_obj
) {
1309 BT_LOGW("Invalid parameter: map value, key, or element value is NULL: "
1310 "map-value-addr=%p, key-addr=%p, element-value-addr=%p",
1311 map_obj
, key
, element_obj
);
1312 ret
= BT_VALUE_STATUS_INVAL
;
1316 if (!bt_value_is_map(map_obj
)) {
1317 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1318 "type=%d", map_obj
, map_obj
->type
);
1319 ret
= BT_VALUE_STATUS_INVAL
;
1323 if (map_obj
->is_frozen
) {
1324 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1326 ret
= BT_VALUE_STATUS_FROZEN
;
1330 quark
= g_quark_from_string(key
);
1331 g_hash_table_insert(typed_map_obj
->ght
,
1332 GUINT_TO_POINTER(quark
), element_obj
);
1333 bt_get(element_obj
);
1334 BT_LOGV("Inserted value into map value: map-value-addr=%p, "
1335 "key=\"%s\", element-value-addr=%p",
1336 map_obj
, key
, element_obj
);
1342 enum bt_value_status
bt_value_map_insert_bool(struct bt_value
*map_obj
,
1343 const char *key
, bt_bool val
)
1345 enum bt_value_status ret
;
1346 struct bt_value
*bool_obj
= NULL
;
1348 bool_obj
= bt_value_bool_create_init(val
);
1349 ret
= bt_value_map_insert(map_obj
, key
, bool_obj
);
1354 enum bt_value_status
bt_value_map_insert_integer(struct bt_value
*map_obj
,
1355 const char *key
, int64_t val
)
1357 enum bt_value_status ret
;
1358 struct bt_value
*integer_obj
= NULL
;
1360 integer_obj
= bt_value_integer_create_init(val
);
1361 ret
= bt_value_map_insert(map_obj
, key
, integer_obj
);
1362 bt_put(integer_obj
);
1366 enum bt_value_status
bt_value_map_insert_float(struct bt_value
*map_obj
,
1367 const char *key
, double val
)
1369 enum bt_value_status ret
;
1370 struct bt_value
*float_obj
= NULL
;
1372 float_obj
= bt_value_float_create_init(val
);
1373 ret
= bt_value_map_insert(map_obj
, key
, float_obj
);
1378 enum bt_value_status
bt_value_map_insert_string(struct bt_value
*map_obj
,
1379 const char *key
, const char *val
)
1381 enum bt_value_status ret
;
1382 struct bt_value
*string_obj
= NULL
;
1384 string_obj
= bt_value_string_create_init(val
);
1385 ret
= bt_value_map_insert(map_obj
, key
, string_obj
);
1390 enum bt_value_status
bt_value_map_insert_empty_array(struct bt_value
*map_obj
,
1393 enum bt_value_status ret
;
1394 struct bt_value
*array_obj
= NULL
;
1396 array_obj
= bt_value_array_create();
1397 ret
= bt_value_map_insert(map_obj
, key
, array_obj
);
1402 enum bt_value_status
bt_value_map_insert_empty_map(struct bt_value
*map_obj
,
1405 enum bt_value_status ret
;
1406 struct bt_value
*empty_map_obj
= NULL
;
1408 empty_map_obj
= bt_value_map_create();
1409 ret
= bt_value_map_insert(map_obj
, key
, empty_map_obj
);
1410 bt_put(empty_map_obj
);
1414 enum bt_value_status
bt_value_map_foreach(const struct bt_value
*map_obj
,
1415 bt_value_map_foreach_cb cb
, void *data
)
1417 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1418 gpointer key
, element_obj
;
1419 GHashTableIter iter
;
1420 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1422 if (!map_obj
|| !cb
) {
1423 BT_LOGW("Invalid parameter: map value or callback is NULL: "
1424 "value-addr=%p, cb-addr=%p", map_obj
, cb
);
1425 ret
= BT_VALUE_STATUS_INVAL
;
1429 if (!bt_value_is_map(map_obj
)) {
1430 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1431 "type=%d", map_obj
, map_obj
->type
);
1432 ret
= BT_VALUE_STATUS_INVAL
;
1436 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
1438 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
1439 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
1441 if (!cb(key_str
, element_obj
, data
)) {
1442 BT_LOGD("User cancelled the loop: key=\"%s\", "
1443 "value-addr=%p, data=%p",
1444 key_str
, element_obj
, data
);
1445 ret
= BT_VALUE_STATUS_CANCELLED
;
1454 struct extend_map_element_data
{
1455 struct bt_value
*extended_obj
;
1460 bt_bool
extend_map_element(const char *key
,
1461 struct bt_value
*extension_obj_elem
, void *data
)
1463 bt_bool ret
= BT_TRUE
;
1465 struct extend_map_element_data
*extend_data
= data
;
1467 /* Copy object which is to replace the current one */
1468 struct bt_value
*extension_obj_elem_copy
=
1469 bt_value_copy(extension_obj_elem
);
1471 /* Replace in extended object */
1472 if (bt_value_map_insert(extend_data
->extended_obj
, key
,
1473 extension_obj_elem_copy
)) {
1474 BT_LOGE("Cannot replace value in extended value: key=\"%s\", "
1475 "extended-value-addr=%p, element-value-addr=%p",
1476 key
, extend_data
->extended_obj
,
1477 extension_obj_elem_copy
);
1485 extend_data
->got_error
= BT_TRUE
;
1488 BT_PUT(extension_obj_elem_copy
);
1492 struct bt_value
*bt_value_map_extend(struct bt_value
*base_map_obj
,
1493 struct bt_value
*extension_obj
)
1495 struct bt_value
*extended_obj
= NULL
;
1496 struct extend_map_element_data extend_data
= { 0 };
1498 if (!base_map_obj
|| !extension_obj
) {
1499 BT_LOGW("Invalid parameter: base value or extension value is NULL: "
1500 "base-value-addr=%p, extension-value-addr=%p",
1501 base_map_obj
, extension_obj
);
1505 if (!bt_value_is_map(base_map_obj
)) {
1506 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1507 "type=%d", base_map_obj
, base_map_obj
->type
);
1511 if (!bt_value_is_map(extension_obj
)) {
1512 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1513 "type=%d", extension_obj
, extension_obj
->type
);
1517 BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p",
1518 base_map_obj
, extension_obj
);
1520 /* Create copy of base map object to start with */
1521 extended_obj
= bt_value_copy(base_map_obj
);
1522 if (!extended_obj
) {
1523 BT_LOGE("Cannot copy base value: base-value-addr=%p",
1529 * For each key in the extension map object, replace this key
1530 * in the copied map object.
1532 extend_data
.extended_obj
= extended_obj
;
1534 if (bt_value_map_foreach(extension_obj
, extend_map_element
,
1536 BT_LOGE("Cannot iterate on the extension object's elements: ",
1537 "extension-value-addr=%p", extension_obj
);
1541 if (extend_data
.got_error
) {
1542 BT_LOGE("Failed to successfully iterate on the extension object's elements: ",
1543 "extension-value-addr=%p", extension_obj
);
1547 BT_LOGD("Extended map value: extended-value-addr=%p",
1552 BT_PUT(extended_obj
);
1555 return extended_obj
;
1558 struct bt_value
*bt_value_copy(const struct bt_value
*object
)
1560 struct bt_value
*copy_obj
= NULL
;
1563 BT_LOGW_STR("Invalid parameter: value object is NULL.");
1567 BT_LOGD("Copying value object: addr=%p", object
);
1568 copy_obj
= copy_funcs
[object
->type
](object
);
1570 BT_LOGD("Copied value object: copy-value-addr=%p",
1573 BT_LOGE_STR("Failed to copy value object.");
1580 bt_bool
bt_value_compare(const struct bt_value
*object_a
,
1581 const struct bt_value
*object_b
)
1583 bt_bool ret
= BT_FALSE
;
1585 if (!object_a
|| !object_b
) {
1586 BT_LOGW("Invalid parameter: value A or value B is NULL: "
1587 "value-a-addr=%p, value-b-addr=%p",
1588 object_a
, object_b
);
1592 if (object_a
->type
!= object_b
->type
) {
1593 BT_LOGV("Values are different: type mismatch: "
1594 "value-a-addr=%p, value-b-addr=%p, "
1595 "value-a-type=%d, value-b-type=%d",
1597 object_a
->type
, object_b
->type
);
1601 ret
= compare_funcs
[object_a
->type
](object_a
, object_b
);