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
33 #include <babeltrace/compiler-internal.h>
34 #include <babeltrace/object-internal.h>
35 #include <babeltrace/ref.h>
36 #include <babeltrace/values.h>
37 #include <babeltrace/compat/glib-internal.h>
38 #include <babeltrace/types.h>
40 #define BT_LOG_TAG "VALUES"
41 #include <babeltrace/lib-logging-internal.h>
43 #define BT_VALUE_FROM_CONCRETE(_concrete) ((struct bt_value *) (_concrete))
44 #define BT_VALUE_TO_BOOL(_base) ((struct bt_value_bool *) (_base))
45 #define BT_VALUE_TO_INTEGER(_base) ((struct bt_value_integer *) (_base))
46 #define BT_VALUE_TO_FLOAT(_base) ((struct bt_value_float *) (_base))
47 #define BT_VALUE_TO_STRING(_base) ((struct bt_value_string *) (_base))
48 #define BT_VALUE_TO_ARRAY(_base) ((struct bt_value_array *) (_base))
49 #define BT_VALUE_TO_MAP(_base) ((struct bt_value_map *) (_base))
52 struct bt_object base
;
53 enum bt_value_type type
;
58 struct bt_value bt_value_null_instance
= {
67 .type
= BT_VALUE_TYPE_NULL
,
71 struct bt_value
*bt_value_null
= &bt_value_null_instance
;
73 struct bt_value_bool
{
78 struct bt_value_integer
{
83 struct bt_value_float
{
88 struct bt_value_string
{
93 struct bt_value_array
{
104 void bt_value_destroy(struct bt_object
*obj
);
107 void bt_value_string_destroy(struct bt_value
*object
)
109 g_string_free(BT_VALUE_TO_STRING(object
)->gstr
, TRUE
);
113 void bt_value_array_destroy(struct bt_value
*object
)
116 * Pointer array's registered value destructor will take care
117 * of putting each contained object.
119 g_ptr_array_free(BT_VALUE_TO_ARRAY(object
)->garray
, TRUE
);
123 void bt_value_map_destroy(struct bt_value
*object
)
126 * Hash table's registered value destructor will take care of
127 * putting each contained object. Keys are GQuarks and cannot
128 * be destroyed anyway.
130 g_hash_table_destroy(BT_VALUE_TO_MAP(object
)->ght
);
134 void (* const destroy_funcs
[])(struct bt_value
*) = {
135 [BT_VALUE_TYPE_NULL
] = NULL
,
136 [BT_VALUE_TYPE_BOOL
] = NULL
,
137 [BT_VALUE_TYPE_INTEGER
] = NULL
,
138 [BT_VALUE_TYPE_FLOAT
] = NULL
,
139 [BT_VALUE_TYPE_STRING
] = bt_value_string_destroy
,
140 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_destroy
,
141 [BT_VALUE_TYPE_MAP
] = bt_value_map_destroy
,
145 struct bt_value
*bt_value_null_copy(const struct bt_value
*null_obj
)
147 return bt_value_null
;
151 struct bt_value
*bt_value_bool_copy(const struct bt_value
*bool_obj
)
153 return bt_value_bool_create_init(BT_VALUE_TO_BOOL(bool_obj
)->value
);
157 struct bt_value
*bt_value_integer_copy(const struct bt_value
*integer_obj
)
159 return bt_value_integer_create_init(
160 BT_VALUE_TO_INTEGER(integer_obj
)->value
);
164 struct bt_value
*bt_value_float_copy(const struct bt_value
*float_obj
)
166 return bt_value_float_create_init(
167 BT_VALUE_TO_FLOAT(float_obj
)->value
);
171 struct bt_value
*bt_value_string_copy(const struct bt_value
*string_obj
)
173 return bt_value_string_create_init(
174 BT_VALUE_TO_STRING(string_obj
)->gstr
->str
);
178 struct bt_value
*bt_value_array_copy(const struct bt_value
*array_obj
)
182 struct bt_value
*copy_obj
;
183 struct bt_value_array
*typed_array_obj
;
185 BT_LOGD("Copying array value: addr=%p", array_obj
);
186 typed_array_obj
= BT_VALUE_TO_ARRAY(array_obj
);
187 copy_obj
= bt_value_array_create();
190 BT_LOGE_STR("Cannot create empty array value.");
194 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
195 struct bt_value
*element_obj_copy
;
196 struct bt_value
*element_obj
= bt_value_array_get(array_obj
, i
);
199 BT_LOGE("Cannot get array value's element: "
206 element_obj_copy
= bt_value_copy(element_obj
);
209 if (!element_obj_copy
) {
210 BT_LOGE("Cannot copy array value's element: "
211 "array-addr=%p, index=%d",
217 ret
= bt_value_array_append(copy_obj
, element_obj_copy
);
218 BT_PUT(element_obj_copy
);
221 BT_LOGE("Cannot append to array value: addr=%p",
228 BT_LOGD("Copied array value: addr=%p", array_obj
);
235 struct bt_value
*bt_value_map_copy(const struct bt_value
*map_obj
)
239 gpointer key
, element_obj
;
240 struct bt_value
*copy_obj
;
241 struct bt_value
*element_obj_copy
;
242 struct bt_value_map
*typed_map_obj
;
244 BT_LOGD("Copying map value: addr=%p", map_obj
);
245 typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
246 copy_obj
= bt_value_map_create();
252 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
254 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
255 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
257 element_obj_copy
= bt_value_copy(element_obj
);
259 if (!element_obj_copy
) {
260 BT_LOGE("Cannot copy map value's element: "
261 "map-addr=%p, key=\"%s\"",
267 ret
= bt_value_map_insert(copy_obj
, key_str
, element_obj_copy
);
268 BT_PUT(element_obj_copy
);
271 BT_LOGE("Cannot insert into map value: addr=%p, key=\"%s\"",
278 BT_LOGD("Copied map value: addr=%p", map_obj
);
285 struct bt_value
*(* const copy_funcs
[])(const struct bt_value
*) = {
286 [BT_VALUE_TYPE_NULL
] = bt_value_null_copy
,
287 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_copy
,
288 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_copy
,
289 [BT_VALUE_TYPE_FLOAT
] = bt_value_float_copy
,
290 [BT_VALUE_TYPE_STRING
] = bt_value_string_copy
,
291 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_copy
,
292 [BT_VALUE_TYPE_MAP
] = bt_value_map_copy
,
296 bt_bool
bt_value_null_compare(const struct bt_value
*object_a
,
297 const struct bt_value
*object_b
)
300 * Always BT_TRUE since bt_value_compare() already checks if both
301 * object_a and object_b have the same type, and in the case of
302 * null value objects, they're always the same if it is so.
308 bt_bool
bt_value_bool_compare(const struct bt_value
*object_a
,
309 const struct bt_value
*object_b
)
311 return BT_VALUE_TO_BOOL(object_a
)->value
==
312 BT_VALUE_TO_BOOL(object_b
)->value
;
316 bt_bool
bt_value_integer_compare(const struct bt_value
*object_a
,
317 const struct bt_value
*object_b
)
319 return BT_VALUE_TO_INTEGER(object_a
)->value
==
320 BT_VALUE_TO_INTEGER(object_b
)->value
;
324 bt_bool
bt_value_float_compare(const struct bt_value
*object_a
,
325 const struct bt_value
*object_b
)
327 return BT_VALUE_TO_FLOAT(object_a
)->value
==
328 BT_VALUE_TO_FLOAT(object_b
)->value
;
332 bt_bool
bt_value_string_compare(const struct bt_value
*object_a
,
333 const struct bt_value
*object_b
)
335 return !strcmp(BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
336 BT_VALUE_TO_STRING(object_b
)->gstr
->str
);
340 bt_bool
bt_value_array_compare(const struct bt_value
*object_a
,
341 const struct bt_value
*object_b
)
344 bt_bool ret
= BT_TRUE
;
345 const struct bt_value_array
*array_obj_a
=
346 BT_VALUE_TO_ARRAY(object_a
);
348 if (bt_value_array_size(object_a
) != bt_value_array_size(object_b
)) {
349 BT_LOGV("Array values are different: size mismatch "
350 "value-a-addr=%p, value-b-addr=%p, "
351 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
353 bt_value_array_size(object_a
),
354 bt_value_array_size(object_b
));
359 for (i
= 0; i
< array_obj_a
->garray
->len
; ++i
) {
360 struct bt_value
*element_obj_a
;
361 struct bt_value
*element_obj_b
;
363 element_obj_a
= bt_value_array_get(object_a
, i
);
364 element_obj_b
= bt_value_array_get(object_b
, i
);
366 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
367 BT_LOGV("Array values's elements are different: "
368 "value-a-addr=%p, value-b-addr=%p, index=%d",
369 element_obj_a
, element_obj_b
, index
);
370 BT_PUT(element_obj_a
);
371 BT_PUT(element_obj_b
);
376 BT_PUT(element_obj_a
);
377 BT_PUT(element_obj_b
);
385 bt_bool
bt_value_map_compare(const struct bt_value
*object_a
,
386 const struct bt_value
*object_b
)
388 bt_bool ret
= BT_TRUE
;
390 gpointer key
, element_obj_a
;
391 const struct bt_value_map
*map_obj_a
= BT_VALUE_TO_MAP(object_a
);
393 if (bt_value_map_size(object_a
) != bt_value_map_size(object_b
)) {
394 BT_LOGV("Map values are different: size mismatch "
395 "value-a-addr=%p, value-b-addr=%p, "
396 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
398 bt_value_map_size(object_a
),
399 bt_value_map_size(object_b
));
404 g_hash_table_iter_init(&iter
, map_obj_a
->ght
);
406 while (g_hash_table_iter_next(&iter
, &key
, &element_obj_a
)) {
407 struct bt_value
*element_obj_b
;
408 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
410 element_obj_b
= bt_value_map_get(object_b
, key_str
);
412 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
413 BT_LOGV("Map values's elements are different: "
414 "value-a-addr=%p, value-b-addr=%p, key=\"%s\"",
415 element_obj_a
, element_obj_b
, key_str
);
416 BT_PUT(element_obj_b
);
421 BT_PUT(element_obj_b
);
429 bt_bool (* const compare_funcs
[])(const struct bt_value
*,
430 const struct bt_value
*) = {
431 [BT_VALUE_TYPE_NULL
] = bt_value_null_compare
,
432 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_compare
,
433 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_compare
,
434 [BT_VALUE_TYPE_FLOAT
] = bt_value_float_compare
,
435 [BT_VALUE_TYPE_STRING
] = bt_value_string_compare
,
436 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_compare
,
437 [BT_VALUE_TYPE_MAP
] = bt_value_map_compare
,
440 void bt_value_null_freeze(struct bt_value
*object
)
444 void bt_value_generic_freeze(struct bt_value
*object
)
446 object
->is_frozen
= BT_TRUE
;
449 void bt_value_array_freeze(struct bt_value
*object
)
452 struct bt_value_array
*typed_array_obj
=
453 BT_VALUE_TO_ARRAY(object
);
455 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
456 struct bt_value
*element_obj
=
457 g_ptr_array_index(typed_array_obj
->garray
, i
);
459 bt_value_freeze(element_obj
);
462 bt_value_generic_freeze(object
);
465 void bt_value_map_freeze(struct bt_value
*object
)
468 gpointer key
, element_obj
;
469 const struct bt_value_map
*map_obj
= BT_VALUE_TO_MAP(object
);
471 g_hash_table_iter_init(&iter
, map_obj
->ght
);
473 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
474 bt_value_freeze(element_obj
);
477 bt_value_generic_freeze(object
);
481 void (* const freeze_funcs
[])(struct bt_value
*) = {
482 [BT_VALUE_TYPE_NULL
] = bt_value_null_freeze
,
483 [BT_VALUE_TYPE_BOOL
] = bt_value_generic_freeze
,
484 [BT_VALUE_TYPE_INTEGER
] = bt_value_generic_freeze
,
485 [BT_VALUE_TYPE_FLOAT
] = bt_value_generic_freeze
,
486 [BT_VALUE_TYPE_STRING
] = bt_value_generic_freeze
,
487 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_freeze
,
488 [BT_VALUE_TYPE_MAP
] = bt_value_map_freeze
,
492 void bt_value_destroy(struct bt_object
*obj
)
494 struct bt_value
*value
;
496 value
= container_of(obj
, struct bt_value
, base
);
497 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=%d", bool_obj
, bool_obj
->type
);
753 ret
= BT_VALUE_STATUS_INVAL
;
757 *val
= typed_bool_obj
->value
;
763 enum bt_value_status
bt_value_bool_set(struct bt_value
*bool_obj
, bt_bool val
)
765 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
766 struct bt_value_bool
*typed_bool_obj
= BT_VALUE_TO_BOOL(bool_obj
);
769 BT_LOGW_STR("Invalid parameter: value object is NULL.");
770 ret
= BT_VALUE_STATUS_INVAL
;
774 if (!bt_value_is_bool(bool_obj
)) {
775 BT_LOGW("Invalid parameter: value is not a boolean value: addr=%p, "
776 "type=%d", bool_obj
, bool_obj
->type
);
777 ret
= BT_VALUE_STATUS_INVAL
;
781 if (bool_obj
->is_frozen
) {
782 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
784 ret
= BT_VALUE_STATUS_FROZEN
;
788 typed_bool_obj
->value
= val
;
789 BT_LOGV("Set boolean value's raw value: value-addr=%p, value=%d",
796 enum bt_value_status
bt_value_integer_get(const struct bt_value
*integer_obj
,
799 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
800 struct bt_value_integer
*typed_integer_obj
=
801 BT_VALUE_TO_INTEGER(integer_obj
);
803 if (!integer_obj
|| !val
) {
804 BT_LOGW("Invalid parameter: value object or value is NULL: "
805 "value-addr=%p, raw-value-addr=%p",
807 ret
= BT_VALUE_STATUS_INVAL
;
811 if (!bt_value_is_integer(integer_obj
)) {
812 BT_LOGW("Invalid parameter: value is not an integer value: addr=%p, "
813 "type=%d", integer_obj
, integer_obj
->type
);
814 ret
= BT_VALUE_STATUS_INVAL
;
818 *val
= typed_integer_obj
->value
;
824 enum bt_value_status
bt_value_integer_set(struct bt_value
*integer_obj
,
827 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
828 struct bt_value_integer
*typed_integer_obj
=
829 BT_VALUE_TO_INTEGER(integer_obj
);
832 BT_LOGW_STR("Invalid parameter: value object is NULL.");
833 ret
= BT_VALUE_STATUS_INVAL
;
837 if (!bt_value_is_integer(integer_obj
)) {
838 BT_LOGW("Invalid parameter: value is not an integer value: addr=%p, "
839 "type=%d", integer_obj
, integer_obj
->type
);
840 ret
= BT_VALUE_STATUS_INVAL
;
844 if (integer_obj
->is_frozen
) {
845 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
847 ret
= BT_VALUE_STATUS_FROZEN
;
851 typed_integer_obj
->value
= val
;
852 BT_LOGV("Set integer value's raw value: value-addr=%p, value=%" PRId64
,
859 enum bt_value_status
bt_value_float_get(const struct bt_value
*float_obj
,
862 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
863 struct bt_value_float
*typed_float_obj
=
864 BT_VALUE_TO_FLOAT(float_obj
);
866 if (!float_obj
|| !val
) {
867 BT_LOGW("Invalid parameter: value object or value is NULL: "
868 "value-addr=%p, raw-value-addr=%p",
870 ret
= BT_VALUE_STATUS_INVAL
;
874 if (!bt_value_is_float(float_obj
)) {
875 BT_LOGW("Invalid parameter: value is not a floating point number value: addr=%p, "
876 "type=%d", float_obj
, float_obj
->type
);
877 ret
= BT_VALUE_STATUS_INVAL
;
881 *val
= typed_float_obj
->value
;
887 enum bt_value_status
bt_value_float_set(struct bt_value
*float_obj
,
890 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
891 struct bt_value_float
*typed_float_obj
=
892 BT_VALUE_TO_FLOAT(float_obj
);
895 BT_LOGW_STR("Invalid parameter: value object is NULL.");
896 ret
= BT_VALUE_STATUS_INVAL
;
900 if (!bt_value_is_float(float_obj
)) {
901 BT_LOGW("Invalid parameter: value is not a floating point number value: addr=%p, "
902 "type=%d", float_obj
, float_obj
->type
);
903 ret
= BT_VALUE_STATUS_INVAL
;
907 if (float_obj
->is_frozen
) {
908 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
910 ret
= BT_VALUE_STATUS_FROZEN
;
914 typed_float_obj
->value
= val
;
915 BT_LOGV("Set floating point number value's raw value: value-addr=%p, value=%f",
922 enum bt_value_status
bt_value_string_get(const struct bt_value
*string_obj
,
925 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
926 struct bt_value_string
*typed_string_obj
=
927 BT_VALUE_TO_STRING(string_obj
);
929 if (!string_obj
|| !val
) {
930 BT_LOGW("Invalid parameter: value object or value is NULL: "
931 "value-addr=%p, raw-value-addr=%p",
933 ret
= BT_VALUE_STATUS_INVAL
;
937 if (!bt_value_is_string(string_obj
)) {
938 BT_LOGW("Invalid parameter: value is not a string value: addr=%p, "
939 "type=%d", string_obj
, string_obj
->type
);
940 ret
= BT_VALUE_STATUS_INVAL
;
944 *val
= typed_string_obj
->gstr
->str
;
950 enum bt_value_status
bt_value_string_set(struct bt_value
*string_obj
,
953 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
954 struct bt_value_string
*typed_string_obj
=
955 BT_VALUE_TO_STRING(string_obj
);
957 if (!string_obj
|| !val
) {
958 BT_LOGW("Invalid parameter: value object or value is NULL: "
959 "value-addr=%p, raw-value-addr=%p",
961 ret
= BT_VALUE_STATUS_INVAL
;
965 if (!bt_value_is_string(string_obj
)) {
966 BT_LOGW("Invalid parameter: value is not a string value: addr=%p, "
967 "type=%d", string_obj
, string_obj
->type
);
968 ret
= BT_VALUE_STATUS_INVAL
;
972 if (string_obj
->is_frozen
) {
973 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
975 ret
= BT_VALUE_STATUS_FROZEN
;
979 g_string_assign(typed_string_obj
->gstr
, val
);
980 BT_LOGV("Set string value's raw value: value-addr=%p, raw-value-addr=%p",
987 int64_t bt_value_array_size(const struct bt_value
*array_obj
)
990 struct bt_value_array
*typed_array_obj
=
991 BT_VALUE_TO_ARRAY(array_obj
);
994 BT_LOGW_STR("Invalid parameter: value object is NULL.");
995 ret
= (int64_t) BT_VALUE_STATUS_INVAL
;
999 if (!bt_value_is_array(array_obj
)) {
1000 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1001 "type=%d", array_obj
, array_obj
->type
);
1002 ret
= BT_VALUE_STATUS_INVAL
;
1006 ret
= (int64_t) typed_array_obj
->garray
->len
;
1012 bt_bool
bt_value_array_is_empty(const struct bt_value
*array_obj
)
1014 return bt_value_array_size(array_obj
) == 0;
1017 struct bt_value
*bt_value_array_get(const struct bt_value
*array_obj
,
1020 struct bt_value
*ret
;
1021 struct bt_value_array
*typed_array_obj
=
1022 BT_VALUE_TO_ARRAY(array_obj
);
1025 BT_LOGW("Invalid parameter: value object is NULL: index=%" PRIu64
,
1031 if (!bt_value_is_array(array_obj
)) {
1032 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1033 "type=%d", array_obj
, 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
,
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=%d", array_obj
, array_obj
->type
);
1071 ret
= BT_VALUE_STATUS_INVAL
;
1075 if (array_obj
->is_frozen
) {
1076 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1078 ret
= BT_VALUE_STATUS_FROZEN
;
1082 g_ptr_array_add(typed_array_obj
->garray
, element_obj
);
1083 bt_get(element_obj
);
1084 BT_LOGV("Appended element to array value: array-value-addr=%p, "
1085 "element-value-addr=%p, new-size=%u",
1086 array_obj
, element_obj
, typed_array_obj
->garray
->len
);
1092 enum bt_value_status
bt_value_array_append_bool(struct bt_value
*array_obj
,
1095 enum bt_value_status ret
;
1096 struct bt_value
*bool_obj
= NULL
;
1098 bool_obj
= bt_value_bool_create_init(val
);
1099 ret
= bt_value_array_append(array_obj
, bool_obj
);
1104 enum bt_value_status
bt_value_array_append_integer(
1105 struct bt_value
*array_obj
, int64_t val
)
1107 enum bt_value_status ret
;
1108 struct bt_value
*integer_obj
= NULL
;
1110 integer_obj
= bt_value_integer_create_init(val
);
1111 ret
= bt_value_array_append(array_obj
, integer_obj
);
1112 bt_put(integer_obj
);
1116 enum bt_value_status
bt_value_array_append_float(struct bt_value
*array_obj
,
1119 enum bt_value_status ret
;
1120 struct bt_value
*float_obj
= NULL
;
1122 float_obj
= bt_value_float_create_init(val
);
1123 ret
= bt_value_array_append(array_obj
, float_obj
);
1128 enum bt_value_status
bt_value_array_append_string(struct bt_value
*array_obj
,
1131 enum bt_value_status ret
;
1132 struct bt_value
*string_obj
= NULL
;
1134 string_obj
= bt_value_string_create_init(val
);
1135 ret
= bt_value_array_append(array_obj
, string_obj
);
1140 enum bt_value_status
bt_value_array_append_empty_array(
1141 struct bt_value
*array_obj
)
1143 enum bt_value_status ret
;
1144 struct bt_value
*empty_array_obj
= NULL
;
1146 empty_array_obj
= bt_value_array_create();
1147 ret
= bt_value_array_append(array_obj
, empty_array_obj
);
1148 bt_put(empty_array_obj
);
1152 enum bt_value_status
bt_value_array_append_empty_map(struct bt_value
*array_obj
)
1154 enum bt_value_status ret
;
1155 struct bt_value
*map_obj
= NULL
;
1157 map_obj
= bt_value_map_create();
1158 ret
= bt_value_array_append(array_obj
, map_obj
);
1163 enum bt_value_status
bt_value_array_set(struct bt_value
*array_obj
,
1164 uint64_t index
, struct bt_value
*element_obj
)
1166 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1167 struct bt_value_array
*typed_array_obj
=
1168 BT_VALUE_TO_ARRAY(array_obj
);
1170 if (!array_obj
|| !element_obj
) {
1171 BT_LOGW("Invalid parameter: array value or element value is NULL: "
1172 "index=%" PRIu64
", array-value-addr=%p, element-value-addr=%p",
1173 index
, array_obj
, element_obj
);
1174 ret
= BT_VALUE_STATUS_INVAL
;
1178 if (!bt_value_is_array(array_obj
)) {
1179 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1180 "type=%d", array_obj
, array_obj
->type
);
1181 ret
= BT_VALUE_STATUS_INVAL
;
1185 if (index
>= typed_array_obj
->garray
->len
) {
1186 BT_LOGW("Invalid parameter: index is out of bounds: "
1187 "addr=%p, index=%" PRIu64
,
1189 ret
= BT_VALUE_STATUS_INVAL
;
1193 if (array_obj
->is_frozen
) {
1194 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1196 ret
= BT_VALUE_STATUS_FROZEN
;
1200 bt_put(g_ptr_array_index(typed_array_obj
->garray
, index
));
1201 g_ptr_array_index(typed_array_obj
->garray
, index
) = element_obj
;
1202 bt_get(element_obj
);
1203 BT_LOGV("Set array value's element: array-value-addr=%p, "
1204 "index=%" PRIu64
", element-value-addr=%p",
1205 array_obj
, index
, element_obj
);
1211 int64_t bt_value_map_size(const struct bt_value
*map_obj
)
1214 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1217 BT_LOGW_STR("Invalid parameter: value object is NULL.");
1218 ret
= (int64_t) BT_VALUE_STATUS_INVAL
;
1222 if (!bt_value_is_map(map_obj
)) {
1223 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1224 "type=%d", map_obj
, map_obj
->type
);
1225 ret
= (int64_t) BT_VALUE_STATUS_INVAL
;
1229 ret
= (int64_t) g_hash_table_size(typed_map_obj
->ght
);
1235 bt_bool
bt_value_map_is_empty(const struct bt_value
*map_obj
)
1237 return bt_value_map_size(map_obj
) == 0;
1240 struct bt_value
*bt_value_map_get(const struct bt_value
*map_obj
,
1244 struct bt_value
*ret
;
1245 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1247 if (!map_obj
|| !key
) {
1248 BT_LOGW("Invalid parameter: value object or key is NULL: "
1249 "value-addr=%p, key-addr=%p", map_obj
, key
);
1254 if (!bt_value_is_map(map_obj
)) {
1255 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1256 "type=%d", map_obj
, map_obj
->type
);
1261 quark
= g_quark_from_string(key
);
1262 ret
= g_hash_table_lookup(typed_map_obj
->ght
, GUINT_TO_POINTER(quark
));
1272 bt_bool
bt_value_map_has_key(const struct bt_value
*map_obj
, const char *key
)
1276 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1278 if (!map_obj
|| !key
) {
1279 BT_LOGW("Invalid parameter: value object or key is NULL: "
1280 "value-addr=%p, key-addr=%p", map_obj
, key
);
1285 if (!bt_value_is_map(map_obj
)) {
1286 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1287 "type=%d", map_obj
, map_obj
->type
);
1292 quark
= g_quark_from_string(key
);
1293 ret
= bt_g_hash_table_contains(typed_map_obj
->ght
,
1294 GUINT_TO_POINTER(quark
));
1300 enum bt_value_status
bt_value_map_insert(struct bt_value
*map_obj
,
1301 const char *key
, struct bt_value
*element_obj
)
1304 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1305 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1307 if (!map_obj
|| !key
|| !element_obj
) {
1308 BT_LOGW("Invalid parameter: map value, key, or element value is NULL: "
1309 "map-value-addr=%p, key-addr=%p, element-value-addr=%p",
1310 map_obj
, key
, element_obj
);
1311 ret
= BT_VALUE_STATUS_INVAL
;
1315 if (!bt_value_is_map(map_obj
)) {
1316 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1317 "type=%d", map_obj
, map_obj
->type
);
1318 ret
= BT_VALUE_STATUS_INVAL
;
1322 if (map_obj
->is_frozen
) {
1323 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1325 ret
= BT_VALUE_STATUS_FROZEN
;
1329 quark
= g_quark_from_string(key
);
1330 g_hash_table_insert(typed_map_obj
->ght
,
1331 GUINT_TO_POINTER(quark
), element_obj
);
1332 bt_get(element_obj
);
1333 BT_LOGV("Inserted value into map value: map-value-addr=%p, "
1334 "key=\"%s\", element-value-addr=%p",
1335 map_obj
, key
, element_obj
);
1341 enum bt_value_status
bt_value_map_insert_bool(struct bt_value
*map_obj
,
1342 const char *key
, bt_bool val
)
1344 enum bt_value_status ret
;
1345 struct bt_value
*bool_obj
= NULL
;
1347 bool_obj
= bt_value_bool_create_init(val
);
1348 ret
= bt_value_map_insert(map_obj
, key
, bool_obj
);
1353 enum bt_value_status
bt_value_map_insert_integer(struct bt_value
*map_obj
,
1354 const char *key
, int64_t val
)
1356 enum bt_value_status ret
;
1357 struct bt_value
*integer_obj
= NULL
;
1359 integer_obj
= bt_value_integer_create_init(val
);
1360 ret
= bt_value_map_insert(map_obj
, key
, integer_obj
);
1361 bt_put(integer_obj
);
1365 enum bt_value_status
bt_value_map_insert_float(struct bt_value
*map_obj
,
1366 const char *key
, double val
)
1368 enum bt_value_status ret
;
1369 struct bt_value
*float_obj
= NULL
;
1371 float_obj
= bt_value_float_create_init(val
);
1372 ret
= bt_value_map_insert(map_obj
, key
, float_obj
);
1377 enum bt_value_status
bt_value_map_insert_string(struct bt_value
*map_obj
,
1378 const char *key
, const char *val
)
1380 enum bt_value_status ret
;
1381 struct bt_value
*string_obj
= NULL
;
1383 string_obj
= bt_value_string_create_init(val
);
1384 ret
= bt_value_map_insert(map_obj
, key
, string_obj
);
1389 enum bt_value_status
bt_value_map_insert_empty_array(struct bt_value
*map_obj
,
1392 enum bt_value_status ret
;
1393 struct bt_value
*array_obj
= NULL
;
1395 array_obj
= bt_value_array_create();
1396 ret
= bt_value_map_insert(map_obj
, key
, array_obj
);
1401 enum bt_value_status
bt_value_map_insert_empty_map(struct bt_value
*map_obj
,
1404 enum bt_value_status ret
;
1405 struct bt_value
*empty_map_obj
= NULL
;
1407 empty_map_obj
= bt_value_map_create();
1408 ret
= bt_value_map_insert(map_obj
, key
, empty_map_obj
);
1409 bt_put(empty_map_obj
);
1413 enum bt_value_status
bt_value_map_foreach(const struct bt_value
*map_obj
,
1414 bt_value_map_foreach_cb cb
, void *data
)
1416 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1417 gpointer key
, element_obj
;
1418 GHashTableIter iter
;
1419 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1421 if (!map_obj
|| !cb
) {
1422 BT_LOGW("Invalid parameter: map value or callback is NULL: "
1423 "value-addr=%p, cb-addr=%p", map_obj
, cb
);
1424 ret
= BT_VALUE_STATUS_INVAL
;
1428 if (!bt_value_is_map(map_obj
)) {
1429 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1430 "type=%d", map_obj
, map_obj
->type
);
1431 ret
= BT_VALUE_STATUS_INVAL
;
1435 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
1437 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
1438 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
1440 if (!cb(key_str
, element_obj
, data
)) {
1441 BT_LOGD("User cancelled the loop: key=\"%s\", "
1442 "value-addr=%p, data=%p",
1443 key_str
, element_obj
, data
);
1444 ret
= BT_VALUE_STATUS_CANCELLED
;
1453 struct extend_map_element_data
{
1454 struct bt_value
*extended_obj
;
1459 bt_bool
extend_map_element(const char *key
,
1460 struct bt_value
*extension_obj_elem
, void *data
)
1462 bt_bool ret
= BT_TRUE
;
1464 struct extend_map_element_data
*extend_data
= data
;
1466 /* Copy object which is to replace the current one */
1467 struct bt_value
*extension_obj_elem_copy
=
1468 bt_value_copy(extension_obj_elem
);
1470 /* Replace in extended object */
1471 if (bt_value_map_insert(extend_data
->extended_obj
, key
,
1472 extension_obj_elem_copy
)) {
1473 BT_LOGE("Cannot replace value in extended value: key=\"%s\", "
1474 "extended-value-addr=%p, element-value-addr=%p",
1475 key
, extend_data
->extended_obj
,
1476 extension_obj_elem_copy
);
1484 extend_data
->got_error
= BT_TRUE
;
1487 BT_PUT(extension_obj_elem_copy
);
1491 struct bt_value
*bt_value_map_extend(struct bt_value
*base_map_obj
,
1492 struct bt_value
*extension_obj
)
1494 struct bt_value
*extended_obj
= NULL
;
1495 struct extend_map_element_data extend_data
= { 0 };
1497 if (!base_map_obj
|| !extension_obj
) {
1498 BT_LOGW("Invalid parameter: base value or extension value is NULL: "
1499 "base-value-addr=%p, extension-value-addr=%p",
1500 base_map_obj
, extension_obj
);
1504 if (!bt_value_is_map(base_map_obj
)) {
1505 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1506 "type=%d", base_map_obj
, base_map_obj
->type
);
1510 if (!bt_value_is_map(extension_obj
)) {
1511 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1512 "type=%d", extension_obj
, extension_obj
->type
);
1516 BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p",
1517 base_map_obj
, extension_obj
);
1519 /* Create copy of base map object to start with */
1520 extended_obj
= bt_value_copy(base_map_obj
);
1521 if (!extended_obj
) {
1522 BT_LOGE("Cannot copy base value: base-value-addr=%p",
1528 * For each key in the extension map object, replace this key
1529 * in the copied map object.
1531 extend_data
.extended_obj
= extended_obj
;
1533 if (bt_value_map_foreach(extension_obj
, extend_map_element
,
1535 BT_LOGE("Cannot iterate on the extension object's elements: ",
1536 "extension-value-addr=%p", extension_obj
);
1540 if (extend_data
.got_error
) {
1541 BT_LOGE("Failed to successfully iterate on the extension object's elements: ",
1542 "extension-value-addr=%p", extension_obj
);
1546 BT_LOGD("Extended map value: extended-value-addr=%p",
1551 BT_PUT(extended_obj
);
1554 return extended_obj
;
1557 struct bt_value
*bt_value_copy(const struct bt_value
*object
)
1559 struct bt_value
*copy_obj
= NULL
;
1562 BT_LOGW_STR("Invalid parameter: value object is NULL.");
1566 BT_LOGD("Copying value object: addr=%p", object
);
1567 copy_obj
= copy_funcs
[object
->type
](object
);
1569 BT_LOGD("Copied value object: copy-value-addr=%p",
1572 BT_LOGE_STR("Failed to copy value object.");
1579 bt_bool
bt_value_compare(const struct bt_value
*object_a
,
1580 const struct bt_value
*object_b
)
1582 bt_bool ret
= BT_FALSE
;
1584 if (!object_a
|| !object_b
) {
1585 BT_LOGW("Invalid parameter: value A or value B is NULL: "
1586 "value-a-addr=%p, value-b-addr=%p",
1587 object_a
, object_b
);
1591 if (object_a
->type
!= object_b
->type
) {
1592 BT_LOGV("Values are different: type mismatch: "
1593 "value-a-addr=%p, value-b-addr=%p, "
1594 "value-a-type=%d, value-b-type=%d",
1596 object_a
->type
, object_b
->type
);
1600 ret
= compare_funcs
[object_a
->type
](object_a
, object_b
);