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 element_obj_copy
= bt_value_copy(element_obj
);
202 if (!element_obj_copy
) {
203 BT_LOGE("Cannot copy array value's element: "
204 "array-addr=%p, index=%d",
210 ret
= bt_value_array_append(copy_obj
, element_obj_copy
);
211 BT_PUT(element_obj_copy
);
213 BT_LOGE("Cannot append to array value: addr=%p",
220 BT_LOGD("Copied array value: original-addr=%p, copy-addr=%p",
221 array_obj
, copy_obj
);
228 struct bt_value
*bt_value_map_copy(const struct bt_value
*map_obj
)
232 gpointer key
, element_obj
;
233 struct bt_value
*copy_obj
;
234 struct bt_value
*element_obj_copy
;
235 struct bt_value_map
*typed_map_obj
;
237 BT_LOGD("Copying map value: addr=%p", map_obj
);
238 typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
239 copy_obj
= bt_value_map_create();
245 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
247 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
248 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
250 element_obj_copy
= bt_value_copy(element_obj
);
251 if (!element_obj_copy
) {
252 BT_LOGE("Cannot copy map value's element: "
253 "map-addr=%p, key=\"%s\"",
259 ret
= bt_value_map_insert(copy_obj
, key_str
, element_obj_copy
);
260 BT_PUT(element_obj_copy
);
262 BT_LOGE("Cannot insert into map value: addr=%p, key=\"%s\"",
269 BT_LOGD("Copied map value: addr=%p", map_obj
);
276 struct bt_value
*(* const copy_funcs
[])(const struct bt_value
*) = {
277 [BT_VALUE_TYPE_NULL
] = bt_value_null_copy
,
278 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_copy
,
279 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_copy
,
280 [BT_VALUE_TYPE_FLOAT
] = bt_value_float_copy
,
281 [BT_VALUE_TYPE_STRING
] = bt_value_string_copy
,
282 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_copy
,
283 [BT_VALUE_TYPE_MAP
] = bt_value_map_copy
,
287 bt_bool
bt_value_null_compare(const struct bt_value
*object_a
,
288 const struct bt_value
*object_b
)
291 * Always BT_TRUE since bt_value_compare() already checks if both
292 * object_a and object_b have the same type, and in the case of
293 * null value objects, they're always the same if it is so.
299 bt_bool
bt_value_bool_compare(const struct bt_value
*object_a
,
300 const struct bt_value
*object_b
)
302 return BT_VALUE_TO_BOOL(object_a
)->value
==
303 BT_VALUE_TO_BOOL(object_b
)->value
;
307 bt_bool
bt_value_integer_compare(const struct bt_value
*object_a
,
308 const struct bt_value
*object_b
)
310 return BT_VALUE_TO_INTEGER(object_a
)->value
==
311 BT_VALUE_TO_INTEGER(object_b
)->value
;
315 bt_bool
bt_value_float_compare(const struct bt_value
*object_a
,
316 const struct bt_value
*object_b
)
318 return BT_VALUE_TO_FLOAT(object_a
)->value
==
319 BT_VALUE_TO_FLOAT(object_b
)->value
;
323 bt_bool
bt_value_string_compare(const struct bt_value
*object_a
,
324 const struct bt_value
*object_b
)
326 return !strcmp(BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
327 BT_VALUE_TO_STRING(object_b
)->gstr
->str
);
331 bt_bool
bt_value_array_compare(const struct bt_value
*object_a
,
332 const struct bt_value
*object_b
)
335 bt_bool ret
= BT_TRUE
;
336 const struct bt_value_array
*array_obj_a
=
337 BT_VALUE_TO_ARRAY(object_a
);
339 if (bt_value_array_size(object_a
) != bt_value_array_size(object_b
)) {
340 BT_LOGV("Array values are different: size mismatch "
341 "value-a-addr=%p, value-b-addr=%p, "
342 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
344 bt_value_array_size(object_a
),
345 bt_value_array_size(object_b
));
350 for (i
= 0; i
< array_obj_a
->garray
->len
; ++i
) {
351 struct bt_value
*element_obj_a
;
352 struct bt_value
*element_obj_b
;
354 element_obj_a
= bt_value_array_get(object_a
, i
);
355 element_obj_b
= bt_value_array_get(object_b
, i
);
357 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
358 BT_LOGV("Array values's elements are different: "
359 "value-a-addr=%p, value-b-addr=%p, index=%d",
360 element_obj_a
, element_obj_b
, index
);
361 BT_PUT(element_obj_a
);
362 BT_PUT(element_obj_b
);
367 BT_PUT(element_obj_a
);
368 BT_PUT(element_obj_b
);
376 bt_bool
bt_value_map_compare(const struct bt_value
*object_a
,
377 const struct bt_value
*object_b
)
379 bt_bool ret
= BT_TRUE
;
381 gpointer key
, element_obj_a
;
382 const struct bt_value_map
*map_obj_a
= BT_VALUE_TO_MAP(object_a
);
384 if (bt_value_map_size(object_a
) != bt_value_map_size(object_b
)) {
385 BT_LOGV("Map values are different: size mismatch "
386 "value-a-addr=%p, value-b-addr=%p, "
387 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
389 bt_value_map_size(object_a
),
390 bt_value_map_size(object_b
));
395 g_hash_table_iter_init(&iter
, map_obj_a
->ght
);
397 while (g_hash_table_iter_next(&iter
, &key
, &element_obj_a
)) {
398 struct bt_value
*element_obj_b
;
399 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
401 element_obj_b
= bt_value_map_get(object_b
, key_str
);
403 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
404 BT_LOGV("Map values's elements are different: "
405 "value-a-addr=%p, value-b-addr=%p, key=\"%s\"",
406 element_obj_a
, element_obj_b
, key_str
);
407 BT_PUT(element_obj_b
);
412 BT_PUT(element_obj_b
);
420 bt_bool (* const compare_funcs
[])(const struct bt_value
*,
421 const struct bt_value
*) = {
422 [BT_VALUE_TYPE_NULL
] = bt_value_null_compare
,
423 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_compare
,
424 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_compare
,
425 [BT_VALUE_TYPE_FLOAT
] = bt_value_float_compare
,
426 [BT_VALUE_TYPE_STRING
] = bt_value_string_compare
,
427 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_compare
,
428 [BT_VALUE_TYPE_MAP
] = bt_value_map_compare
,
431 void bt_value_null_freeze(struct bt_value
*object
)
435 void bt_value_generic_freeze(struct bt_value
*object
)
437 object
->is_frozen
= BT_TRUE
;
440 void bt_value_array_freeze(struct bt_value
*object
)
443 struct bt_value_array
*typed_array_obj
=
444 BT_VALUE_TO_ARRAY(object
);
446 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
447 struct bt_value
*element_obj
=
448 g_ptr_array_index(typed_array_obj
->garray
, i
);
450 bt_value_freeze(element_obj
);
453 bt_value_generic_freeze(object
);
456 void bt_value_map_freeze(struct bt_value
*object
)
459 gpointer key
, element_obj
;
460 const struct bt_value_map
*map_obj
= BT_VALUE_TO_MAP(object
);
462 g_hash_table_iter_init(&iter
, map_obj
->ght
);
464 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
465 bt_value_freeze(element_obj
);
468 bt_value_generic_freeze(object
);
472 void (* const freeze_funcs
[])(struct bt_value
*) = {
473 [BT_VALUE_TYPE_NULL
] = bt_value_null_freeze
,
474 [BT_VALUE_TYPE_BOOL
] = bt_value_generic_freeze
,
475 [BT_VALUE_TYPE_INTEGER
] = bt_value_generic_freeze
,
476 [BT_VALUE_TYPE_FLOAT
] = bt_value_generic_freeze
,
477 [BT_VALUE_TYPE_STRING
] = bt_value_generic_freeze
,
478 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_freeze
,
479 [BT_VALUE_TYPE_MAP
] = bt_value_map_freeze
,
483 void bt_value_destroy(struct bt_object
*obj
)
485 struct bt_value
*value
;
487 value
= container_of(obj
, struct bt_value
, base
);
488 assert(value
->type
!= BT_VALUE_TYPE_UNKNOWN
);
489 BT_LOGD("Destroying value: addr=%p", value
);
491 if (bt_value_is_null(value
)) {
492 BT_LOGD_STR("Not destroying the null value singleton.");
496 if (destroy_funcs
[value
->type
]) {
497 destroy_funcs
[value
->type
](value
);
503 enum bt_value_status
bt_value_freeze(struct bt_value
*object
)
505 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
508 BT_LOGW_STR("Invalid parameter: value object is NULL.");
509 ret
= BT_VALUE_STATUS_INVAL
;
513 if (object
->is_frozen
) {
517 BT_LOGD("Freezing value: addr=%p", object
);
518 freeze_funcs
[object
->type
](object
);
524 bt_bool
bt_value_is_frozen(const struct bt_value
*object
)
526 return object
&& object
->is_frozen
;
529 enum bt_value_type
bt_value_get_type(const struct bt_value
*object
)
532 BT_LOGW_STR("Invalid parameter: value object is NULL.");
533 return BT_VALUE_TYPE_UNKNOWN
;
540 struct bt_value
bt_value_create_base(enum bt_value_type type
)
542 struct bt_value base
;
545 base
.is_frozen
= BT_FALSE
;
546 bt_object_init(&base
, bt_value_destroy
);
550 struct bt_value
*bt_value_bool_create_init(bt_bool val
)
552 struct bt_value_bool
*bool_obj
;
554 BT_LOGD("Creating boolean value object: val=%d", val
);
555 bool_obj
= g_new0(struct bt_value_bool
, 1);
558 BT_LOGE_STR("Failed to allocate one boolean value object.");
562 bool_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_BOOL
);
563 bool_obj
->value
= val
;
564 BT_LOGD("Created boolean value object: addr=%p", bool_obj
);
567 return BT_VALUE_FROM_CONCRETE(bool_obj
);
570 struct bt_value
*bt_value_bool_create(void)
572 return bt_value_bool_create_init(BT_FALSE
);
575 struct bt_value
*bt_value_integer_create_init(int64_t val
)
577 struct bt_value_integer
*integer_obj
;
579 BT_LOGD("Creating integer value object: val=%" PRId64
, val
);
580 integer_obj
= g_new0(struct bt_value_integer
, 1);
583 BT_LOGE_STR("Failed to allocate one integer value object.");
587 integer_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_INTEGER
);
588 integer_obj
->value
= val
;
589 BT_LOGD("Created integer value object: addr=%p",
593 return BT_VALUE_FROM_CONCRETE(integer_obj
);
596 struct bt_value
*bt_value_integer_create(void)
598 return bt_value_integer_create_init(0);
601 struct bt_value
*bt_value_float_create_init(double val
)
603 struct bt_value_float
*float_obj
;
605 BT_LOGD("Creating floating point number value object: val=%f", val
);
606 float_obj
= g_new0(struct bt_value_float
, 1);
609 BT_LOGE_STR("Failed to allocate one floating point number value object.");
613 float_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_FLOAT
);
614 float_obj
->value
= val
;
615 BT_LOGD("Created floating point number value object: addr=%p",
619 return BT_VALUE_FROM_CONCRETE(float_obj
);
622 struct bt_value
*bt_value_float_create(void)
624 return bt_value_float_create_init(0.);
627 struct bt_value
*bt_value_string_create_init(const char *val
)
629 struct bt_value_string
*string_obj
= NULL
;
632 BT_LOGW_STR("Invalid parameter: value is NULL.");
636 BT_LOGD("Creating string value object: val-len=%u", strlen(val
));
637 string_obj
= g_new0(struct bt_value_string
, 1);
640 BT_LOGE_STR("Failed to allocate one string object.");
644 string_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_STRING
);
645 string_obj
->gstr
= g_string_new(val
);
647 if (!string_obj
->gstr
) {
648 BT_LOGE_STR("Failed to allocate a GString.");
654 BT_LOGD("Created string value object: addr=%p",
658 return BT_VALUE_FROM_CONCRETE(string_obj
);
661 struct bt_value
*bt_value_string_create(void)
663 return bt_value_string_create_init("");
666 struct bt_value
*bt_value_array_create(void)
668 struct bt_value_array
*array_obj
;
670 BT_LOGD_STR("Creating empty array value object.");
671 array_obj
= g_new0(struct bt_value_array
, 1);
674 BT_LOGE_STR("Failed to allocate one array object.");
678 array_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_ARRAY
);
679 array_obj
->garray
= bt_g_ptr_array_new_full(0,
680 (GDestroyNotify
) bt_put
);
682 if (!array_obj
->garray
) {
683 BT_LOGE_STR("Failed to allocate a GPtrArray.");
689 BT_LOGD("Created array value object: addr=%p",
693 return BT_VALUE_FROM_CONCRETE(array_obj
);
696 struct bt_value
*bt_value_map_create(void)
698 struct bt_value_map
*map_obj
;
700 BT_LOGD_STR("Creating empty map value object.");
701 map_obj
= g_new0(struct bt_value_map
, 1);
704 BT_LOGE_STR("Failed to allocate one map object.");
708 map_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_MAP
);
709 map_obj
->ght
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
710 NULL
, (GDestroyNotify
) bt_put
);
713 BT_LOGE_STR("Failed to allocate a GHashTable.");
719 BT_LOGD("Created map value object: addr=%p",
723 return BT_VALUE_FROM_CONCRETE(map_obj
);
726 enum bt_value_status
bt_value_bool_get(const struct bt_value
*bool_obj
,
729 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
730 struct bt_value_bool
*typed_bool_obj
= BT_VALUE_TO_BOOL(bool_obj
);
732 if (!bool_obj
|| !val
) {
733 BT_LOGW("Invalid parameter: value object or value is NULL: "
734 "value-addr=%p, raw-value-addr=%p",
736 ret
= BT_VALUE_STATUS_INVAL
;
740 if (!bt_value_is_bool(bool_obj
)) {
741 BT_LOGW("Invalid parameter: value is not a boolean value: addr=%p, "
742 "type=%s", bool_obj
, bool_obj
->type
,
743 bt_value_type_string(bool_obj
->type
));
744 ret
= BT_VALUE_STATUS_INVAL
;
748 *val
= typed_bool_obj
->value
;
754 enum bt_value_status
bt_value_bool_set(struct bt_value
*bool_obj
, bt_bool val
)
756 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
757 struct bt_value_bool
*typed_bool_obj
= BT_VALUE_TO_BOOL(bool_obj
);
760 BT_LOGW_STR("Invalid parameter: value object is NULL.");
761 ret
= BT_VALUE_STATUS_INVAL
;
765 if (!bt_value_is_bool(bool_obj
)) {
766 BT_LOGW("Invalid parameter: value is not a boolean value: addr=%p, "
768 bt_value_type_string(bool_obj
->type
));
769 ret
= BT_VALUE_STATUS_INVAL
;
773 if (bool_obj
->is_frozen
) {
774 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
776 ret
= BT_VALUE_STATUS_FROZEN
;
780 typed_bool_obj
->value
= val
;
781 BT_LOGV("Set boolean value's raw value: value-addr=%p, value=%d",
788 enum bt_value_status
bt_value_integer_get(const struct bt_value
*integer_obj
,
791 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
792 struct bt_value_integer
*typed_integer_obj
=
793 BT_VALUE_TO_INTEGER(integer_obj
);
795 if (!integer_obj
|| !val
) {
796 BT_LOGW("Invalid parameter: value object or value is NULL: "
797 "value-addr=%p, raw-value-addr=%p",
799 ret
= BT_VALUE_STATUS_INVAL
;
803 if (!bt_value_is_integer(integer_obj
)) {
804 BT_LOGW("Invalid parameter: value is not an integer value: addr=%p, "
805 "type=%s", integer_obj
,
806 bt_value_type_string(integer_obj
->type
));
807 ret
= BT_VALUE_STATUS_INVAL
;
811 *val
= typed_integer_obj
->value
;
817 enum bt_value_status
bt_value_integer_set(struct bt_value
*integer_obj
,
820 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
821 struct bt_value_integer
*typed_integer_obj
=
822 BT_VALUE_TO_INTEGER(integer_obj
);
825 BT_LOGW_STR("Invalid parameter: value object is NULL.");
826 ret
= BT_VALUE_STATUS_INVAL
;
830 if (!bt_value_is_integer(integer_obj
)) {
831 BT_LOGW("Invalid parameter: value is not an integer value: addr=%p, "
832 "type=%s", integer_obj
,
833 bt_value_type_string(integer_obj
->type
));
834 ret
= BT_VALUE_STATUS_INVAL
;
838 if (integer_obj
->is_frozen
) {
839 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
841 ret
= BT_VALUE_STATUS_FROZEN
;
845 typed_integer_obj
->value
= val
;
846 BT_LOGV("Set integer value's raw value: value-addr=%p, value=%" PRId64
,
853 enum bt_value_status
bt_value_float_get(const struct bt_value
*float_obj
,
856 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
857 struct bt_value_float
*typed_float_obj
=
858 BT_VALUE_TO_FLOAT(float_obj
);
860 if (!float_obj
|| !val
) {
861 BT_LOGW("Invalid parameter: value object or value is NULL: "
862 "value-addr=%p, raw-value-addr=%p",
864 ret
= BT_VALUE_STATUS_INVAL
;
868 if (!bt_value_is_float(float_obj
)) {
869 BT_LOGW("Invalid parameter: value is not a floating point number value: addr=%p, "
870 "type=%s", float_obj
,
871 bt_value_type_string(float_obj
->type
));
872 ret
= BT_VALUE_STATUS_INVAL
;
876 *val
= typed_float_obj
->value
;
882 enum bt_value_status
bt_value_float_set(struct bt_value
*float_obj
,
885 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
886 struct bt_value_float
*typed_float_obj
=
887 BT_VALUE_TO_FLOAT(float_obj
);
890 BT_LOGW_STR("Invalid parameter: value object is NULL.");
891 ret
= BT_VALUE_STATUS_INVAL
;
895 if (!bt_value_is_float(float_obj
)) {
896 BT_LOGW("Invalid parameter: value is not a floating point number value: addr=%p, "
897 "type=%s", float_obj
,
898 bt_value_type_string(float_obj
->type
));
899 ret
= BT_VALUE_STATUS_INVAL
;
903 if (float_obj
->is_frozen
) {
904 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
906 ret
= BT_VALUE_STATUS_FROZEN
;
910 typed_float_obj
->value
= val
;
911 BT_LOGV("Set floating point number value's raw value: value-addr=%p, value=%f",
918 enum bt_value_status
bt_value_string_get(const struct bt_value
*string_obj
,
921 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
922 struct bt_value_string
*typed_string_obj
=
923 BT_VALUE_TO_STRING(string_obj
);
925 if (!string_obj
|| !val
) {
926 BT_LOGW("Invalid parameter: value object or value is NULL: "
927 "value-addr=%p, raw-value-addr=%p",
929 ret
= BT_VALUE_STATUS_INVAL
;
933 if (!bt_value_is_string(string_obj
)) {
934 BT_LOGW("Invalid parameter: value is not a string value: addr=%p, "
935 "type=%s", string_obj
,
936 bt_value_type_string(string_obj
->type
));
937 ret
= BT_VALUE_STATUS_INVAL
;
941 *val
= typed_string_obj
->gstr
->str
;
947 enum bt_value_status
bt_value_string_set(struct bt_value
*string_obj
,
950 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
951 struct bt_value_string
*typed_string_obj
=
952 BT_VALUE_TO_STRING(string_obj
);
954 if (!string_obj
|| !val
) {
955 BT_LOGW("Invalid parameter: value object or value is NULL: "
956 "value-addr=%p, raw-value-addr=%p",
958 ret
= BT_VALUE_STATUS_INVAL
;
962 if (!bt_value_is_string(string_obj
)) {
963 BT_LOGW("Invalid parameter: value is not a string value: addr=%p, "
964 "type=%s", string_obj
,
965 bt_value_type_string(string_obj
->type
));
966 ret
= BT_VALUE_STATUS_INVAL
;
970 if (string_obj
->is_frozen
) {
971 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
973 ret
= BT_VALUE_STATUS_FROZEN
;
977 g_string_assign(typed_string_obj
->gstr
, val
);
978 BT_LOGV("Set string value's raw value: value-addr=%p, raw-value-addr=%p",
985 int64_t bt_value_array_size(const struct bt_value
*array_obj
)
988 struct bt_value_array
*typed_array_obj
=
989 BT_VALUE_TO_ARRAY(array_obj
);
992 BT_LOGW_STR("Invalid parameter: value object is NULL.");
993 ret
= (int64_t) BT_VALUE_STATUS_INVAL
;
997 if (!bt_value_is_array(array_obj
)) {
998 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
999 "type=%s", array_obj
,
1000 bt_value_type_string(array_obj
->type
));
1001 ret
= BT_VALUE_STATUS_INVAL
;
1005 ret
= (int64_t) typed_array_obj
->garray
->len
;
1011 bt_bool
bt_value_array_is_empty(const struct bt_value
*array_obj
)
1013 return bt_value_array_size(array_obj
) == 0;
1016 struct bt_value
*bt_value_array_get(const struct bt_value
*array_obj
,
1019 struct bt_value
*ret
;
1020 struct bt_value_array
*typed_array_obj
=
1021 BT_VALUE_TO_ARRAY(array_obj
);
1024 BT_LOGW("Invalid parameter: value object is NULL: index=%" PRIu64
,
1030 if (!bt_value_is_array(array_obj
)) {
1031 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1032 "type=%s", array_obj
,
1033 bt_value_type_string(array_obj
->type
));
1038 if (index
>= typed_array_obj
->garray
->len
) {
1039 BT_LOGW("Invalid parameter: index is out of bounds: "
1040 "addr=%p, index=%" PRIu64
", size=%u",
1041 array_obj
, index
, typed_array_obj
->garray
->len
);
1046 ret
= g_ptr_array_index(typed_array_obj
->garray
, index
);
1053 enum bt_value_status
bt_value_array_append(struct bt_value
*array_obj
,
1054 struct bt_value
*element_obj
)
1056 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1057 struct bt_value_array
*typed_array_obj
=
1058 BT_VALUE_TO_ARRAY(array_obj
);
1060 if (!array_obj
|| !element_obj
) {
1061 BT_LOGW("Invalid parameter: array value or element value is NULL: "
1062 "array-value-addr=%p, element-value-addr=%p",
1063 array_obj
, element_obj
);
1064 ret
= BT_VALUE_STATUS_INVAL
;
1068 if (!bt_value_is_array(array_obj
)) {
1069 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1070 "type=%s", array_obj
,
1071 bt_value_type_string(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=%s", array_obj
,
1182 bt_value_type_string(array_obj
->type
));
1183 ret
= BT_VALUE_STATUS_INVAL
;
1187 if (index
>= typed_array_obj
->garray
->len
) {
1188 BT_LOGW("Invalid parameter: index is out of bounds: "
1189 "addr=%p, index=%" PRIu64
", size=%u",
1190 array_obj
, index
, typed_array_obj
->garray
->len
);
1191 ret
= BT_VALUE_STATUS_INVAL
;
1195 if (array_obj
->is_frozen
) {
1196 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1198 ret
= BT_VALUE_STATUS_FROZEN
;
1202 bt_put(g_ptr_array_index(typed_array_obj
->garray
, index
));
1203 g_ptr_array_index(typed_array_obj
->garray
, index
) = element_obj
;
1204 bt_get(element_obj
);
1205 BT_LOGV("Set array value's element: array-value-addr=%p, "
1206 "index=%" PRIu64
", element-value-addr=%p",
1207 array_obj
, index
, element_obj
);
1213 int64_t bt_value_map_size(const struct bt_value
*map_obj
)
1216 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1219 BT_LOGW_STR("Invalid parameter: value object is NULL.");
1220 ret
= (int64_t) BT_VALUE_STATUS_INVAL
;
1224 if (!bt_value_is_map(map_obj
)) {
1225 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1227 bt_value_type_string(map_obj
->type
));
1228 ret
= (int64_t) BT_VALUE_STATUS_INVAL
;
1232 ret
= (int64_t) g_hash_table_size(typed_map_obj
->ght
);
1238 bt_bool
bt_value_map_is_empty(const struct bt_value
*map_obj
)
1240 return bt_value_map_size(map_obj
) == 0;
1243 struct bt_value
*bt_value_map_get(const struct bt_value
*map_obj
,
1247 struct bt_value
*ret
;
1248 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1250 if (!map_obj
|| !key
) {
1251 BT_LOGW("Invalid parameter: value object or key is NULL: "
1252 "value-addr=%p, key-addr=%p", map_obj
, key
);
1257 if (!bt_value_is_map(map_obj
)) {
1258 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1260 bt_value_type_string(map_obj
->type
));
1265 quark
= g_quark_from_string(key
);
1266 ret
= g_hash_table_lookup(typed_map_obj
->ght
, GUINT_TO_POINTER(quark
));
1275 bt_bool
bt_value_map_has_key(const struct bt_value
*map_obj
, const char *key
)
1279 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1281 if (!map_obj
|| !key
) {
1282 BT_LOGW("Invalid parameter: value object or key is NULL: "
1283 "value-addr=%p, key-addr=%p", map_obj
, key
);
1288 if (!bt_value_is_map(map_obj
)) {
1289 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1291 bt_value_type_string(map_obj
->type
));
1296 quark
= g_quark_from_string(key
);
1297 ret
= bt_g_hash_table_contains(typed_map_obj
->ght
,
1298 GUINT_TO_POINTER(quark
));
1304 enum bt_value_status
bt_value_map_insert(struct bt_value
*map_obj
,
1305 const char *key
, struct bt_value
*element_obj
)
1308 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1309 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1311 if (!map_obj
|| !key
|| !element_obj
) {
1312 BT_LOGW("Invalid parameter: map value, key, or element value is NULL: "
1313 "map-value-addr=%p, key-addr=%p, element-value-addr=%p",
1314 map_obj
, key
, element_obj
);
1315 ret
= BT_VALUE_STATUS_INVAL
;
1319 if (!bt_value_is_map(map_obj
)) {
1320 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1322 bt_value_type_string(map_obj
->type
));
1323 ret
= BT_VALUE_STATUS_INVAL
;
1327 if (map_obj
->is_frozen
) {
1328 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1330 ret
= BT_VALUE_STATUS_FROZEN
;
1334 quark
= g_quark_from_string(key
);
1335 g_hash_table_insert(typed_map_obj
->ght
,
1336 GUINT_TO_POINTER(quark
), element_obj
);
1337 bt_get(element_obj
);
1338 BT_LOGV("Inserted value into map value: map-value-addr=%p, "
1339 "key=\"%s\", element-value-addr=%p",
1340 map_obj
, key
, element_obj
);
1346 enum bt_value_status
bt_value_map_insert_bool(struct bt_value
*map_obj
,
1347 const char *key
, bt_bool val
)
1349 enum bt_value_status ret
;
1350 struct bt_value
*bool_obj
= NULL
;
1352 bool_obj
= bt_value_bool_create_init(val
);
1353 ret
= bt_value_map_insert(map_obj
, key
, bool_obj
);
1358 enum bt_value_status
bt_value_map_insert_integer(struct bt_value
*map_obj
,
1359 const char *key
, int64_t val
)
1361 enum bt_value_status ret
;
1362 struct bt_value
*integer_obj
= NULL
;
1364 integer_obj
= bt_value_integer_create_init(val
);
1365 ret
= bt_value_map_insert(map_obj
, key
, integer_obj
);
1366 bt_put(integer_obj
);
1370 enum bt_value_status
bt_value_map_insert_float(struct bt_value
*map_obj
,
1371 const char *key
, double val
)
1373 enum bt_value_status ret
;
1374 struct bt_value
*float_obj
= NULL
;
1376 float_obj
= bt_value_float_create_init(val
);
1377 ret
= bt_value_map_insert(map_obj
, key
, float_obj
);
1382 enum bt_value_status
bt_value_map_insert_string(struct bt_value
*map_obj
,
1383 const char *key
, const char *val
)
1385 enum bt_value_status ret
;
1386 struct bt_value
*string_obj
= NULL
;
1388 string_obj
= bt_value_string_create_init(val
);
1389 ret
= bt_value_map_insert(map_obj
, key
, string_obj
);
1394 enum bt_value_status
bt_value_map_insert_empty_array(struct bt_value
*map_obj
,
1397 enum bt_value_status ret
;
1398 struct bt_value
*array_obj
= NULL
;
1400 array_obj
= bt_value_array_create();
1401 ret
= bt_value_map_insert(map_obj
, key
, array_obj
);
1406 enum bt_value_status
bt_value_map_insert_empty_map(struct bt_value
*map_obj
,
1409 enum bt_value_status ret
;
1410 struct bt_value
*empty_map_obj
= NULL
;
1412 empty_map_obj
= bt_value_map_create();
1413 ret
= bt_value_map_insert(map_obj
, key
, empty_map_obj
);
1414 bt_put(empty_map_obj
);
1418 enum bt_value_status
bt_value_map_foreach(const struct bt_value
*map_obj
,
1419 bt_value_map_foreach_cb cb
, void *data
)
1421 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1422 gpointer key
, element_obj
;
1423 GHashTableIter iter
;
1424 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1426 if (!map_obj
|| !cb
) {
1427 BT_LOGW("Invalid parameter: map value or callback is NULL: "
1428 "value-addr=%p, cb-addr=%p", map_obj
, cb
);
1429 ret
= BT_VALUE_STATUS_INVAL
;
1433 if (!bt_value_is_map(map_obj
)) {
1434 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1436 bt_value_type_string(map_obj
->type
));
1437 ret
= BT_VALUE_STATUS_INVAL
;
1441 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
1443 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
1444 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
1446 if (!cb(key_str
, element_obj
, data
)) {
1447 BT_LOGV("User cancelled the loop: key=\"%s\", "
1448 "value-addr=%p, data=%p",
1449 key_str
, element_obj
, data
);
1450 ret
= BT_VALUE_STATUS_CANCELLED
;
1459 struct extend_map_element_data
{
1460 struct bt_value
*extended_obj
;
1465 bt_bool
extend_map_element(const char *key
,
1466 struct bt_value
*extension_obj_elem
, void *data
)
1468 bt_bool ret
= BT_TRUE
;
1470 struct extend_map_element_data
*extend_data
= data
;
1472 /* Copy object which is to replace the current one */
1473 struct bt_value
*extension_obj_elem_copy
=
1474 bt_value_copy(extension_obj_elem
);
1476 /* Replace in extended object */
1477 if (bt_value_map_insert(extend_data
->extended_obj
, key
,
1478 extension_obj_elem_copy
)) {
1479 BT_LOGE("Cannot replace value in extended value: key=\"%s\", "
1480 "extended-value-addr=%p, element-value-addr=%p",
1481 key
, extend_data
->extended_obj
,
1482 extension_obj_elem_copy
);
1490 extend_data
->got_error
= BT_TRUE
;
1493 BT_PUT(extension_obj_elem_copy
);
1497 struct bt_value
*bt_value_map_extend(struct bt_value
*base_map_obj
,
1498 struct bt_value
*extension_obj
)
1500 struct bt_value
*extended_obj
= NULL
;
1501 struct extend_map_element_data extend_data
= { 0 };
1503 if (!base_map_obj
|| !extension_obj
) {
1504 BT_LOGW("Invalid parameter: base value or extension value is NULL: "
1505 "base-value-addr=%p, extension-value-addr=%p",
1506 base_map_obj
, extension_obj
);
1510 if (!bt_value_is_map(base_map_obj
)) {
1511 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1512 "type=%s", base_map_obj
,
1513 bt_value_type_string(base_map_obj
->type
));
1517 if (!bt_value_is_map(extension_obj
)) {
1518 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1519 "type=%s", extension_obj
,
1520 bt_value_type_string(extension_obj
->type
));
1524 BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p",
1525 base_map_obj
, extension_obj
);
1527 /* Create copy of base map object to start with */
1528 extended_obj
= bt_value_copy(base_map_obj
);
1529 if (!extended_obj
) {
1530 BT_LOGE("Cannot copy base value: base-value-addr=%p",
1536 * For each key in the extension map object, replace this key
1537 * in the copied map object.
1539 extend_data
.extended_obj
= extended_obj
;
1541 if (bt_value_map_foreach(extension_obj
, extend_map_element
,
1543 BT_LOGE("Cannot iterate on the extension object's elements: ",
1544 "extension-value-addr=%p", extension_obj
);
1548 if (extend_data
.got_error
) {
1549 BT_LOGE("Failed to successfully iterate on the extension object's elements: ",
1550 "extension-value-addr=%p", extension_obj
);
1554 BT_LOGD("Extended map value: extended-value-addr=%p",
1559 BT_PUT(extended_obj
);
1562 return extended_obj
;
1565 struct bt_value
*bt_value_copy(const struct bt_value
*object
)
1567 struct bt_value
*copy_obj
= NULL
;
1570 BT_LOGW_STR("Invalid parameter: value object is NULL.");
1574 BT_LOGD("Copying value object: addr=%p", object
);
1575 copy_obj
= copy_funcs
[object
->type
](object
);
1577 BT_LOGD("Copied value object: copy-value-addr=%p",
1580 BT_LOGE_STR("Failed to copy value object.");
1587 bt_bool
bt_value_compare(const struct bt_value
*object_a
,
1588 const struct bt_value
*object_b
)
1590 bt_bool ret
= BT_FALSE
;
1592 if (!object_a
|| !object_b
) {
1593 BT_LOGW("Invalid parameter: value A or value B is NULL: "
1594 "value-a-addr=%p, value-b-addr=%p",
1595 object_a
, object_b
);
1599 if (object_a
->type
!= object_b
->type
) {
1600 BT_LOGV("Values are different: type mismatch: "
1601 "value-a-addr=%p, value-b-addr=%p, "
1602 "value-a-type=%s, value-b-type=%s",
1604 bt_value_type_string(object_a
->type
),
1605 bt_value_type_string(object_b
->type
));
1609 ret
= compare_funcs
[object_a
->type
](object_a
, object_b
);