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>
36 #include <babeltrace/compiler-internal.h>
37 #include <babeltrace/ref.h>
38 #include <babeltrace/values.h>
39 #include <babeltrace/compat/glib-internal.h>
40 #include <babeltrace/types.h>
41 #include <babeltrace/object-internal.h>
42 #include <babeltrace/values-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
);
499 BT_LOGD("Destroying value: addr=%p", value
);
501 if (bt_value_is_null(value
)) {
502 BT_LOGD_STR("Not destroying the null value singleton.");
506 if (destroy_funcs
[value
->type
]) {
507 destroy_funcs
[value
->type
](value
);
513 enum bt_value_status
bt_value_freeze(struct bt_value
*object
)
515 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
518 BT_LOGW_STR("Invalid parameter: value object is NULL.");
519 ret
= BT_VALUE_STATUS_INVAL
;
523 if (object
->is_frozen
) {
527 BT_LOGD("Freezing value: addr=%p", object
);
528 freeze_funcs
[object
->type
](object
);
534 bt_bool
bt_value_is_frozen(const struct bt_value
*object
)
536 return object
&& object
->is_frozen
;
539 enum bt_value_type
bt_value_get_type(const struct bt_value
*object
)
542 BT_LOGW_STR("Invalid parameter: value object is NULL.");
543 return BT_VALUE_TYPE_UNKNOWN
;
550 struct bt_value
bt_value_create_base(enum bt_value_type type
)
552 struct bt_value base
;
555 base
.is_frozen
= BT_FALSE
;
556 bt_object_init(&base
, bt_value_destroy
);
560 struct bt_value
*bt_value_bool_create_init(bt_bool val
)
562 struct bt_value_bool
*bool_obj
;
564 BT_LOGD("Creating boolean value object: val=%d", val
);
565 bool_obj
= g_new0(struct bt_value_bool
, 1);
568 BT_LOGE_STR("Failed to allocate one boolean value object.");
572 bool_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_BOOL
);
573 bool_obj
->value
= val
;
574 BT_LOGD("Created boolean value object: addr=%p", bool_obj
);
577 return BT_VALUE_FROM_CONCRETE(bool_obj
);
580 struct bt_value
*bt_value_bool_create(void)
582 return bt_value_bool_create_init(BT_FALSE
);
585 struct bt_value
*bt_value_integer_create_init(int64_t val
)
587 struct bt_value_integer
*integer_obj
;
589 BT_LOGD("Creating integer value object: val=%" PRId64
, val
);
590 integer_obj
= g_new0(struct bt_value_integer
, 1);
593 BT_LOGE_STR("Failed to allocate one integer value object.");
597 integer_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_INTEGER
);
598 integer_obj
->value
= val
;
599 BT_LOGD("Created integer value object: addr=%p",
603 return BT_VALUE_FROM_CONCRETE(integer_obj
);
606 struct bt_value
*bt_value_integer_create(void)
608 return bt_value_integer_create_init(0);
611 struct bt_value
*bt_value_float_create_init(double val
)
613 struct bt_value_float
*float_obj
;
615 BT_LOGD("Creating floating point number value object: val=%f", val
);
616 float_obj
= g_new0(struct bt_value_float
, 1);
619 BT_LOGE_STR("Failed to allocate one floating point number value object.");
623 float_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_FLOAT
);
624 float_obj
->value
= val
;
625 BT_LOGD("Created floating point number value object: addr=%p",
629 return BT_VALUE_FROM_CONCRETE(float_obj
);
632 struct bt_value
*bt_value_float_create(void)
634 return bt_value_float_create_init(0.);
637 struct bt_value
*bt_value_string_create_init(const char *val
)
639 struct bt_value_string
*string_obj
= NULL
;
642 BT_LOGW_STR("Invalid parameter: value is NULL.");
646 BT_LOGD("Creating string value object: val-len=%u", strlen(val
));
647 string_obj
= g_new0(struct bt_value_string
, 1);
650 BT_LOGE_STR("Failed to allocate one string object.");
654 string_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_STRING
);
655 string_obj
->gstr
= g_string_new(val
);
657 if (!string_obj
->gstr
) {
658 BT_LOGE_STR("Failed to allocate a GString.");
664 BT_LOGD("Created string value object: addr=%p",
668 return BT_VALUE_FROM_CONCRETE(string_obj
);
671 struct bt_value
*bt_value_string_create(void)
673 return bt_value_string_create_init("");
676 struct bt_value
*bt_value_array_create(void)
678 struct bt_value_array
*array_obj
;
680 BT_LOGD_STR("Creating empty array value object.");
681 array_obj
= g_new0(struct bt_value_array
, 1);
684 BT_LOGE_STR("Failed to allocate one array object.");
688 array_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_ARRAY
);
689 array_obj
->garray
= bt_g_ptr_array_new_full(0,
690 (GDestroyNotify
) bt_put
);
692 if (!array_obj
->garray
) {
693 BT_LOGE_STR("Failed to allocate a GPtrArray.");
699 BT_LOGD("Created array value object: addr=%p",
703 return BT_VALUE_FROM_CONCRETE(array_obj
);
706 struct bt_value
*bt_value_map_create(void)
708 struct bt_value_map
*map_obj
;
710 BT_LOGD_STR("Creating empty map value object.");
711 map_obj
= g_new0(struct bt_value_map
, 1);
714 BT_LOGE_STR("Failed to allocate one map object.");
718 map_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_MAP
);
719 map_obj
->ght
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
720 NULL
, (GDestroyNotify
) bt_put
);
723 BT_LOGE_STR("Failed to allocate a GHashTable.");
729 BT_LOGD("Created map value object: addr=%p",
733 return BT_VALUE_FROM_CONCRETE(map_obj
);
736 enum bt_value_status
bt_value_bool_get(const struct bt_value
*bool_obj
,
739 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
740 struct bt_value_bool
*typed_bool_obj
= BT_VALUE_TO_BOOL(bool_obj
);
742 if (!bool_obj
|| !val
) {
743 BT_LOGW("Invalid parameter: value object or value is NULL: "
744 "value-addr=%p, raw-value-addr=%p",
746 ret
= BT_VALUE_STATUS_INVAL
;
750 if (!bt_value_is_bool(bool_obj
)) {
751 BT_LOGW("Invalid parameter: value is not a boolean value: addr=%p, "
752 "type=%s", bool_obj
, bool_obj
->type
,
753 bt_value_type_string(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, "
778 bt_value_type_string(bool_obj
->type
));
779 ret
= BT_VALUE_STATUS_INVAL
;
783 if (bool_obj
->is_frozen
) {
784 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
786 ret
= BT_VALUE_STATUS_FROZEN
;
790 typed_bool_obj
->value
= val
;
791 BT_LOGV("Set boolean value's raw value: value-addr=%p, value=%d",
798 enum bt_value_status
bt_value_integer_get(const struct bt_value
*integer_obj
,
801 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
802 struct bt_value_integer
*typed_integer_obj
=
803 BT_VALUE_TO_INTEGER(integer_obj
);
805 if (!integer_obj
|| !val
) {
806 BT_LOGW("Invalid parameter: value object or value is NULL: "
807 "value-addr=%p, raw-value-addr=%p",
809 ret
= BT_VALUE_STATUS_INVAL
;
813 if (!bt_value_is_integer(integer_obj
)) {
814 BT_LOGW("Invalid parameter: value is not an integer value: addr=%p, "
815 "type=%s", integer_obj
,
816 bt_value_type_string(integer_obj
->type
));
817 ret
= BT_VALUE_STATUS_INVAL
;
821 *val
= typed_integer_obj
->value
;
827 enum bt_value_status
bt_value_integer_set(struct bt_value
*integer_obj
,
830 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
831 struct bt_value_integer
*typed_integer_obj
=
832 BT_VALUE_TO_INTEGER(integer_obj
);
835 BT_LOGW_STR("Invalid parameter: value object is NULL.");
836 ret
= BT_VALUE_STATUS_INVAL
;
840 if (!bt_value_is_integer(integer_obj
)) {
841 BT_LOGW("Invalid parameter: value is not an integer value: addr=%p, "
842 "type=%s", integer_obj
,
843 bt_value_type_string(integer_obj
->type
));
844 ret
= BT_VALUE_STATUS_INVAL
;
848 if (integer_obj
->is_frozen
) {
849 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
851 ret
= BT_VALUE_STATUS_FROZEN
;
855 typed_integer_obj
->value
= val
;
856 BT_LOGV("Set integer value's raw value: value-addr=%p, value=%" PRId64
,
863 enum bt_value_status
bt_value_float_get(const struct bt_value
*float_obj
,
866 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
867 struct bt_value_float
*typed_float_obj
=
868 BT_VALUE_TO_FLOAT(float_obj
);
870 if (!float_obj
|| !val
) {
871 BT_LOGW("Invalid parameter: value object or value is NULL: "
872 "value-addr=%p, raw-value-addr=%p",
874 ret
= BT_VALUE_STATUS_INVAL
;
878 if (!bt_value_is_float(float_obj
)) {
879 BT_LOGW("Invalid parameter: value is not a floating point number value: addr=%p, "
880 "type=%s", float_obj
,
881 bt_value_type_string(float_obj
->type
));
882 ret
= BT_VALUE_STATUS_INVAL
;
886 *val
= typed_float_obj
->value
;
892 enum bt_value_status
bt_value_float_set(struct bt_value
*float_obj
,
895 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
896 struct bt_value_float
*typed_float_obj
=
897 BT_VALUE_TO_FLOAT(float_obj
);
900 BT_LOGW_STR("Invalid parameter: value object is NULL.");
901 ret
= BT_VALUE_STATUS_INVAL
;
905 if (!bt_value_is_float(float_obj
)) {
906 BT_LOGW("Invalid parameter: value is not a floating point number value: addr=%p, "
907 "type=%s", float_obj
,
908 bt_value_type_string(float_obj
->type
));
909 ret
= BT_VALUE_STATUS_INVAL
;
913 if (float_obj
->is_frozen
) {
914 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
916 ret
= BT_VALUE_STATUS_FROZEN
;
920 typed_float_obj
->value
= val
;
921 BT_LOGV("Set floating point number value's raw value: value-addr=%p, value=%f",
928 enum bt_value_status
bt_value_string_get(const struct bt_value
*string_obj
,
931 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
932 struct bt_value_string
*typed_string_obj
=
933 BT_VALUE_TO_STRING(string_obj
);
935 if (!string_obj
|| !val
) {
936 BT_LOGW("Invalid parameter: value object or value is NULL: "
937 "value-addr=%p, raw-value-addr=%p",
939 ret
= BT_VALUE_STATUS_INVAL
;
943 if (!bt_value_is_string(string_obj
)) {
944 BT_LOGW("Invalid parameter: value is not a string value: addr=%p, "
945 "type=%s", string_obj
,
946 bt_value_type_string(string_obj
->type
));
947 ret
= BT_VALUE_STATUS_INVAL
;
951 *val
= typed_string_obj
->gstr
->str
;
957 enum bt_value_status
bt_value_string_set(struct bt_value
*string_obj
,
960 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
961 struct bt_value_string
*typed_string_obj
=
962 BT_VALUE_TO_STRING(string_obj
);
964 if (!string_obj
|| !val
) {
965 BT_LOGW("Invalid parameter: value object or value is NULL: "
966 "value-addr=%p, raw-value-addr=%p",
968 ret
= BT_VALUE_STATUS_INVAL
;
972 if (!bt_value_is_string(string_obj
)) {
973 BT_LOGW("Invalid parameter: value is not a string value: addr=%p, "
974 "type=%s", string_obj
,
975 bt_value_type_string(string_obj
->type
));
976 ret
= BT_VALUE_STATUS_INVAL
;
980 if (string_obj
->is_frozen
) {
981 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
983 ret
= BT_VALUE_STATUS_FROZEN
;
987 g_string_assign(typed_string_obj
->gstr
, val
);
988 BT_LOGV("Set string value's raw value: value-addr=%p, raw-value-addr=%p",
995 int64_t bt_value_array_size(const struct bt_value
*array_obj
)
998 struct bt_value_array
*typed_array_obj
=
999 BT_VALUE_TO_ARRAY(array_obj
);
1002 BT_LOGW_STR("Invalid parameter: value object is NULL.");
1003 ret
= (int64_t) BT_VALUE_STATUS_INVAL
;
1007 if (!bt_value_is_array(array_obj
)) {
1008 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1009 "type=%s", array_obj
,
1010 bt_value_type_string(array_obj
->type
));
1011 ret
= BT_VALUE_STATUS_INVAL
;
1015 ret
= (int64_t) typed_array_obj
->garray
->len
;
1021 bt_bool
bt_value_array_is_empty(const struct bt_value
*array_obj
)
1023 return bt_value_array_size(array_obj
) == 0;
1026 struct bt_value
*bt_value_array_get(const struct bt_value
*array_obj
,
1029 struct bt_value
*ret
;
1030 struct bt_value_array
*typed_array_obj
=
1031 BT_VALUE_TO_ARRAY(array_obj
);
1034 BT_LOGW("Invalid parameter: value object is NULL: index=%" PRIu64
,
1040 if (!bt_value_is_array(array_obj
)) {
1041 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1042 "type=%s", array_obj
,
1043 bt_value_type_string(array_obj
->type
));
1048 if (index
>= typed_array_obj
->garray
->len
) {
1049 BT_LOGW("Invalid parameter: index is out of bounds: "
1050 "addr=%p, index=%" PRIu64
", size=%u",
1051 array_obj
, index
, typed_array_obj
->garray
->len
);
1056 ret
= g_ptr_array_index(typed_array_obj
->garray
, index
);
1063 enum bt_value_status
bt_value_array_append(struct bt_value
*array_obj
,
1064 struct bt_value
*element_obj
)
1066 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1067 struct bt_value_array
*typed_array_obj
=
1068 BT_VALUE_TO_ARRAY(array_obj
);
1070 if (!array_obj
|| !element_obj
) {
1071 BT_LOGW("Invalid parameter: array value or element value is NULL: "
1072 "array-value-addr=%p, element-value-addr=%p",
1073 array_obj
, element_obj
);
1074 ret
= BT_VALUE_STATUS_INVAL
;
1078 if (!bt_value_is_array(array_obj
)) {
1079 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1080 "type=%s", array_obj
,
1081 bt_value_type_string(array_obj
->type
));
1082 ret
= BT_VALUE_STATUS_INVAL
;
1086 if (array_obj
->is_frozen
) {
1087 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1089 ret
= BT_VALUE_STATUS_FROZEN
;
1093 g_ptr_array_add(typed_array_obj
->garray
, element_obj
);
1094 bt_get(element_obj
);
1095 BT_LOGV("Appended element to array value: array-value-addr=%p, "
1096 "element-value-addr=%p, new-size=%u",
1097 array_obj
, element_obj
, typed_array_obj
->garray
->len
);
1103 enum bt_value_status
bt_value_array_append_bool(struct bt_value
*array_obj
,
1106 enum bt_value_status ret
;
1107 struct bt_value
*bool_obj
= NULL
;
1109 bool_obj
= bt_value_bool_create_init(val
);
1110 ret
= bt_value_array_append(array_obj
, bool_obj
);
1115 enum bt_value_status
bt_value_array_append_integer(
1116 struct bt_value
*array_obj
, int64_t val
)
1118 enum bt_value_status ret
;
1119 struct bt_value
*integer_obj
= NULL
;
1121 integer_obj
= bt_value_integer_create_init(val
);
1122 ret
= bt_value_array_append(array_obj
, integer_obj
);
1123 bt_put(integer_obj
);
1127 enum bt_value_status
bt_value_array_append_float(struct bt_value
*array_obj
,
1130 enum bt_value_status ret
;
1131 struct bt_value
*float_obj
= NULL
;
1133 float_obj
= bt_value_float_create_init(val
);
1134 ret
= bt_value_array_append(array_obj
, float_obj
);
1139 enum bt_value_status
bt_value_array_append_string(struct bt_value
*array_obj
,
1142 enum bt_value_status ret
;
1143 struct bt_value
*string_obj
= NULL
;
1145 string_obj
= bt_value_string_create_init(val
);
1146 ret
= bt_value_array_append(array_obj
, string_obj
);
1151 enum bt_value_status
bt_value_array_append_empty_array(
1152 struct bt_value
*array_obj
)
1154 enum bt_value_status ret
;
1155 struct bt_value
*empty_array_obj
= NULL
;
1157 empty_array_obj
= bt_value_array_create();
1158 ret
= bt_value_array_append(array_obj
, empty_array_obj
);
1159 bt_put(empty_array_obj
);
1163 enum bt_value_status
bt_value_array_append_empty_map(struct bt_value
*array_obj
)
1165 enum bt_value_status ret
;
1166 struct bt_value
*map_obj
= NULL
;
1168 map_obj
= bt_value_map_create();
1169 ret
= bt_value_array_append(array_obj
, map_obj
);
1174 enum bt_value_status
bt_value_array_set(struct bt_value
*array_obj
,
1175 uint64_t index
, struct bt_value
*element_obj
)
1177 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1178 struct bt_value_array
*typed_array_obj
=
1179 BT_VALUE_TO_ARRAY(array_obj
);
1181 if (!array_obj
|| !element_obj
) {
1182 BT_LOGW("Invalid parameter: array value or element value is NULL: "
1183 "index=%" PRIu64
", array-value-addr=%p, element-value-addr=%p",
1184 index
, array_obj
, element_obj
);
1185 ret
= BT_VALUE_STATUS_INVAL
;
1189 if (!bt_value_is_array(array_obj
)) {
1190 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1191 "type=%s", array_obj
,
1192 bt_value_type_string(array_obj
->type
));
1193 ret
= BT_VALUE_STATUS_INVAL
;
1197 if (index
>= typed_array_obj
->garray
->len
) {
1198 BT_LOGW("Invalid parameter: index is out of bounds: "
1199 "addr=%p, index=%" PRIu64
", size=%u",
1200 array_obj
, index
, typed_array_obj
->garray
->len
);
1201 ret
= BT_VALUE_STATUS_INVAL
;
1205 if (array_obj
->is_frozen
) {
1206 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1208 ret
= BT_VALUE_STATUS_FROZEN
;
1212 bt_put(g_ptr_array_index(typed_array_obj
->garray
, index
));
1213 g_ptr_array_index(typed_array_obj
->garray
, index
) = element_obj
;
1214 bt_get(element_obj
);
1215 BT_LOGV("Set array value's element: array-value-addr=%p, "
1216 "index=%" PRIu64
", element-value-addr=%p",
1217 array_obj
, index
, element_obj
);
1223 int64_t bt_value_map_size(const struct bt_value
*map_obj
)
1226 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1229 BT_LOGW_STR("Invalid parameter: value object is NULL.");
1230 ret
= (int64_t) BT_VALUE_STATUS_INVAL
;
1234 if (!bt_value_is_map(map_obj
)) {
1235 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1237 bt_value_type_string(map_obj
->type
));
1238 ret
= (int64_t) BT_VALUE_STATUS_INVAL
;
1242 ret
= (int64_t) g_hash_table_size(typed_map_obj
->ght
);
1248 bt_bool
bt_value_map_is_empty(const struct bt_value
*map_obj
)
1250 return bt_value_map_size(map_obj
) == 0;
1253 struct bt_value
*bt_value_map_get(const struct bt_value
*map_obj
,
1257 struct bt_value
*ret
;
1258 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1260 if (!map_obj
|| !key
) {
1261 BT_LOGW("Invalid parameter: value object or key is NULL: "
1262 "value-addr=%p, key-addr=%p", map_obj
, key
);
1267 if (!bt_value_is_map(map_obj
)) {
1268 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1270 bt_value_type_string(map_obj
->type
));
1275 quark
= g_quark_from_string(key
);
1276 ret
= g_hash_table_lookup(typed_map_obj
->ght
, GUINT_TO_POINTER(quark
));
1285 bt_bool
bt_value_map_has_key(const struct bt_value
*map_obj
, const char *key
)
1289 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1291 if (!map_obj
|| !key
) {
1292 BT_LOGW("Invalid parameter: value object or key is NULL: "
1293 "value-addr=%p, key-addr=%p", map_obj
, key
);
1298 if (!bt_value_is_map(map_obj
)) {
1299 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1301 bt_value_type_string(map_obj
->type
));
1306 quark
= g_quark_from_string(key
);
1307 ret
= bt_g_hash_table_contains(typed_map_obj
->ght
,
1308 GUINT_TO_POINTER(quark
));
1314 enum bt_value_status
bt_value_map_insert(struct bt_value
*map_obj
,
1315 const char *key
, struct bt_value
*element_obj
)
1318 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1319 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1321 if (!map_obj
|| !key
|| !element_obj
) {
1322 BT_LOGW("Invalid parameter: map value, key, or element value is NULL: "
1323 "map-value-addr=%p, key-addr=%p, element-value-addr=%p",
1324 map_obj
, key
, element_obj
);
1325 ret
= BT_VALUE_STATUS_INVAL
;
1329 if (!bt_value_is_map(map_obj
)) {
1330 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1332 bt_value_type_string(map_obj
->type
));
1333 ret
= BT_VALUE_STATUS_INVAL
;
1337 if (map_obj
->is_frozen
) {
1338 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1340 ret
= BT_VALUE_STATUS_FROZEN
;
1344 quark
= g_quark_from_string(key
);
1345 g_hash_table_insert(typed_map_obj
->ght
,
1346 GUINT_TO_POINTER(quark
), element_obj
);
1347 bt_get(element_obj
);
1348 BT_LOGV("Inserted value into map value: map-value-addr=%p, "
1349 "key=\"%s\", element-value-addr=%p",
1350 map_obj
, key
, element_obj
);
1356 enum bt_value_status
bt_value_map_insert_bool(struct bt_value
*map_obj
,
1357 const char *key
, bt_bool val
)
1359 enum bt_value_status ret
;
1360 struct bt_value
*bool_obj
= NULL
;
1362 bool_obj
= bt_value_bool_create_init(val
);
1363 ret
= bt_value_map_insert(map_obj
, key
, bool_obj
);
1368 enum bt_value_status
bt_value_map_insert_integer(struct bt_value
*map_obj
,
1369 const char *key
, int64_t val
)
1371 enum bt_value_status ret
;
1372 struct bt_value
*integer_obj
= NULL
;
1374 integer_obj
= bt_value_integer_create_init(val
);
1375 ret
= bt_value_map_insert(map_obj
, key
, integer_obj
);
1376 bt_put(integer_obj
);
1380 enum bt_value_status
bt_value_map_insert_float(struct bt_value
*map_obj
,
1381 const char *key
, double val
)
1383 enum bt_value_status ret
;
1384 struct bt_value
*float_obj
= NULL
;
1386 float_obj
= bt_value_float_create_init(val
);
1387 ret
= bt_value_map_insert(map_obj
, key
, float_obj
);
1392 enum bt_value_status
bt_value_map_insert_string(struct bt_value
*map_obj
,
1393 const char *key
, const char *val
)
1395 enum bt_value_status ret
;
1396 struct bt_value
*string_obj
= NULL
;
1398 string_obj
= bt_value_string_create_init(val
);
1399 ret
= bt_value_map_insert(map_obj
, key
, string_obj
);
1404 enum bt_value_status
bt_value_map_insert_empty_array(struct bt_value
*map_obj
,
1407 enum bt_value_status ret
;
1408 struct bt_value
*array_obj
= NULL
;
1410 array_obj
= bt_value_array_create();
1411 ret
= bt_value_map_insert(map_obj
, key
, array_obj
);
1416 enum bt_value_status
bt_value_map_insert_empty_map(struct bt_value
*map_obj
,
1419 enum bt_value_status ret
;
1420 struct bt_value
*empty_map_obj
= NULL
;
1422 empty_map_obj
= bt_value_map_create();
1423 ret
= bt_value_map_insert(map_obj
, key
, empty_map_obj
);
1424 bt_put(empty_map_obj
);
1428 enum bt_value_status
bt_value_map_foreach(const struct bt_value
*map_obj
,
1429 bt_value_map_foreach_cb cb
, void *data
)
1431 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1432 gpointer key
, element_obj
;
1433 GHashTableIter iter
;
1434 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1436 if (!map_obj
|| !cb
) {
1437 BT_LOGW("Invalid parameter: map value or callback is NULL: "
1438 "value-addr=%p, cb-addr=%p", map_obj
, cb
);
1439 ret
= BT_VALUE_STATUS_INVAL
;
1443 if (!bt_value_is_map(map_obj
)) {
1444 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1446 bt_value_type_string(map_obj
->type
));
1447 ret
= BT_VALUE_STATUS_INVAL
;
1451 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
1453 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
1454 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
1456 if (!cb(key_str
, element_obj
, data
)) {
1457 BT_LOGV("User cancelled the loop: key=\"%s\", "
1458 "value-addr=%p, data=%p",
1459 key_str
, element_obj
, data
);
1460 ret
= BT_VALUE_STATUS_CANCELLED
;
1469 struct extend_map_element_data
{
1470 struct bt_value
*extended_obj
;
1475 bt_bool
extend_map_element(const char *key
,
1476 struct bt_value
*extension_obj_elem
, void *data
)
1478 bt_bool ret
= BT_TRUE
;
1480 struct extend_map_element_data
*extend_data
= data
;
1482 /* Copy object which is to replace the current one */
1483 struct bt_value
*extension_obj_elem_copy
=
1484 bt_value_copy(extension_obj_elem
);
1486 /* Replace in extended object */
1487 if (bt_value_map_insert(extend_data
->extended_obj
, key
,
1488 extension_obj_elem_copy
)) {
1489 BT_LOGE("Cannot replace value in extended value: key=\"%s\", "
1490 "extended-value-addr=%p, element-value-addr=%p",
1491 key
, extend_data
->extended_obj
,
1492 extension_obj_elem_copy
);
1500 extend_data
->got_error
= BT_TRUE
;
1503 BT_PUT(extension_obj_elem_copy
);
1507 struct bt_value
*bt_value_map_extend(struct bt_value
*base_map_obj
,
1508 struct bt_value
*extension_obj
)
1510 struct bt_value
*extended_obj
= NULL
;
1511 struct extend_map_element_data extend_data
= { 0 };
1513 if (!base_map_obj
|| !extension_obj
) {
1514 BT_LOGW("Invalid parameter: base value or extension value is NULL: "
1515 "base-value-addr=%p, extension-value-addr=%p",
1516 base_map_obj
, extension_obj
);
1520 if (!bt_value_is_map(base_map_obj
)) {
1521 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1522 "type=%s", base_map_obj
,
1523 bt_value_type_string(base_map_obj
->type
));
1527 if (!bt_value_is_map(extension_obj
)) {
1528 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1529 "type=%s", extension_obj
,
1530 bt_value_type_string(extension_obj
->type
));
1534 BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p",
1535 base_map_obj
, extension_obj
);
1537 /* Create copy of base map object to start with */
1538 extended_obj
= bt_value_copy(base_map_obj
);
1539 if (!extended_obj
) {
1540 BT_LOGE("Cannot copy base value: base-value-addr=%p",
1546 * For each key in the extension map object, replace this key
1547 * in the copied map object.
1549 extend_data
.extended_obj
= extended_obj
;
1551 if (bt_value_map_foreach(extension_obj
, extend_map_element
,
1553 BT_LOGE("Cannot iterate on the extension object's elements: ",
1554 "extension-value-addr=%p", extension_obj
);
1558 if (extend_data
.got_error
) {
1559 BT_LOGE("Failed to successfully iterate on the extension object's elements: ",
1560 "extension-value-addr=%p", extension_obj
);
1564 BT_LOGD("Extended map value: extended-value-addr=%p",
1569 BT_PUT(extended_obj
);
1572 return extended_obj
;
1575 struct bt_value
*bt_value_copy(const struct bt_value
*object
)
1577 struct bt_value
*copy_obj
= NULL
;
1580 BT_LOGW_STR("Invalid parameter: value object is NULL.");
1584 BT_LOGD("Copying value object: addr=%p", object
);
1585 copy_obj
= copy_funcs
[object
->type
](object
);
1587 BT_LOGD("Copied value object: copy-value-addr=%p",
1590 BT_LOGE_STR("Failed to copy value object.");
1597 bt_bool
bt_value_compare(const struct bt_value
*object_a
,
1598 const struct bt_value
*object_b
)
1600 bt_bool ret
= BT_FALSE
;
1602 if (!object_a
|| !object_b
) {
1603 BT_LOGW("Invalid parameter: value A or value B is NULL: "
1604 "value-a-addr=%p, value-b-addr=%p",
1605 object_a
, object_b
);
1609 if (object_a
->type
!= object_b
->type
) {
1610 BT_LOGV("Values are different: type mismatch: "
1611 "value-a-addr=%p, value-b-addr=%p, "
1612 "value-a-type=%s, value-b-type=%s",
1614 bt_value_type_string(object_a
->type
),
1615 bt_value_type_string(object_b
->type
));
1619 ret
= compare_funcs
[object_a
->type
](object_a
, object_b
);