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: original-addr=%p, copy-addr=%p",
230 array_obj
, copy_obj
);
237 struct bt_value
*bt_value_map_copy(const struct bt_value
*map_obj
)
241 gpointer key
, element_obj
;
242 struct bt_value
*copy_obj
;
243 struct bt_value
*element_obj_copy
;
244 struct bt_value_map
*typed_map_obj
;
246 BT_LOGD("Copying map value: addr=%p", map_obj
);
247 typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
248 copy_obj
= bt_value_map_create();
254 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
256 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
257 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
259 element_obj_copy
= bt_value_copy(element_obj
);
261 if (!element_obj_copy
) {
262 BT_LOGE("Cannot copy map value's element: "
263 "map-addr=%p, key=\"%s\"",
269 ret
= bt_value_map_insert(copy_obj
, key_str
, element_obj_copy
);
270 BT_PUT(element_obj_copy
);
273 BT_LOGE("Cannot insert into map value: addr=%p, key=\"%s\"",
280 BT_LOGD("Copied map value: addr=%p", map_obj
);
287 struct bt_value
*(* const copy_funcs
[])(const struct bt_value
*) = {
288 [BT_VALUE_TYPE_NULL
] = bt_value_null_copy
,
289 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_copy
,
290 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_copy
,
291 [BT_VALUE_TYPE_FLOAT
] = bt_value_float_copy
,
292 [BT_VALUE_TYPE_STRING
] = bt_value_string_copy
,
293 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_copy
,
294 [BT_VALUE_TYPE_MAP
] = bt_value_map_copy
,
298 bt_bool
bt_value_null_compare(const struct bt_value
*object_a
,
299 const struct bt_value
*object_b
)
302 * Always BT_TRUE since bt_value_compare() already checks if both
303 * object_a and object_b have the same type, and in the case of
304 * null value objects, they're always the same if it is so.
310 bt_bool
bt_value_bool_compare(const struct bt_value
*object_a
,
311 const struct bt_value
*object_b
)
313 return BT_VALUE_TO_BOOL(object_a
)->value
==
314 BT_VALUE_TO_BOOL(object_b
)->value
;
318 bt_bool
bt_value_integer_compare(const struct bt_value
*object_a
,
319 const struct bt_value
*object_b
)
321 return BT_VALUE_TO_INTEGER(object_a
)->value
==
322 BT_VALUE_TO_INTEGER(object_b
)->value
;
326 bt_bool
bt_value_float_compare(const struct bt_value
*object_a
,
327 const struct bt_value
*object_b
)
329 return BT_VALUE_TO_FLOAT(object_a
)->value
==
330 BT_VALUE_TO_FLOAT(object_b
)->value
;
334 bt_bool
bt_value_string_compare(const struct bt_value
*object_a
,
335 const struct bt_value
*object_b
)
337 return !strcmp(BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
338 BT_VALUE_TO_STRING(object_b
)->gstr
->str
);
342 bt_bool
bt_value_array_compare(const struct bt_value
*object_a
,
343 const struct bt_value
*object_b
)
346 bt_bool ret
= BT_TRUE
;
347 const struct bt_value_array
*array_obj_a
=
348 BT_VALUE_TO_ARRAY(object_a
);
350 if (bt_value_array_size(object_a
) != bt_value_array_size(object_b
)) {
351 BT_LOGV("Array values are different: size mismatch "
352 "value-a-addr=%p, value-b-addr=%p, "
353 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
355 bt_value_array_size(object_a
),
356 bt_value_array_size(object_b
));
361 for (i
= 0; i
< array_obj_a
->garray
->len
; ++i
) {
362 struct bt_value
*element_obj_a
;
363 struct bt_value
*element_obj_b
;
365 element_obj_a
= bt_value_array_get(object_a
, i
);
366 element_obj_b
= bt_value_array_get(object_b
, i
);
368 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
369 BT_LOGV("Array values's elements are different: "
370 "value-a-addr=%p, value-b-addr=%p, index=%d",
371 element_obj_a
, element_obj_b
, index
);
372 BT_PUT(element_obj_a
);
373 BT_PUT(element_obj_b
);
378 BT_PUT(element_obj_a
);
379 BT_PUT(element_obj_b
);
387 bt_bool
bt_value_map_compare(const struct bt_value
*object_a
,
388 const struct bt_value
*object_b
)
390 bt_bool ret
= BT_TRUE
;
392 gpointer key
, element_obj_a
;
393 const struct bt_value_map
*map_obj_a
= BT_VALUE_TO_MAP(object_a
);
395 if (bt_value_map_size(object_a
) != bt_value_map_size(object_b
)) {
396 BT_LOGV("Map values are different: size mismatch "
397 "value-a-addr=%p, value-b-addr=%p, "
398 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
400 bt_value_map_size(object_a
),
401 bt_value_map_size(object_b
));
406 g_hash_table_iter_init(&iter
, map_obj_a
->ght
);
408 while (g_hash_table_iter_next(&iter
, &key
, &element_obj_a
)) {
409 struct bt_value
*element_obj_b
;
410 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
412 element_obj_b
= bt_value_map_get(object_b
, key_str
);
414 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
415 BT_LOGV("Map values's elements are different: "
416 "value-a-addr=%p, value-b-addr=%p, key=\"%s\"",
417 element_obj_a
, element_obj_b
, key_str
);
418 BT_PUT(element_obj_b
);
423 BT_PUT(element_obj_b
);
431 bt_bool (* const compare_funcs
[])(const struct bt_value
*,
432 const struct bt_value
*) = {
433 [BT_VALUE_TYPE_NULL
] = bt_value_null_compare
,
434 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_compare
,
435 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_compare
,
436 [BT_VALUE_TYPE_FLOAT
] = bt_value_float_compare
,
437 [BT_VALUE_TYPE_STRING
] = bt_value_string_compare
,
438 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_compare
,
439 [BT_VALUE_TYPE_MAP
] = bt_value_map_compare
,
442 void bt_value_null_freeze(struct bt_value
*object
)
446 void bt_value_generic_freeze(struct bt_value
*object
)
448 object
->is_frozen
= BT_TRUE
;
451 void bt_value_array_freeze(struct bt_value
*object
)
454 struct bt_value_array
*typed_array_obj
=
455 BT_VALUE_TO_ARRAY(object
);
457 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
458 struct bt_value
*element_obj
=
459 g_ptr_array_index(typed_array_obj
->garray
, i
);
461 bt_value_freeze(element_obj
);
464 bt_value_generic_freeze(object
);
467 void bt_value_map_freeze(struct bt_value
*object
)
470 gpointer key
, element_obj
;
471 const struct bt_value_map
*map_obj
= BT_VALUE_TO_MAP(object
);
473 g_hash_table_iter_init(&iter
, map_obj
->ght
);
475 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
476 bt_value_freeze(element_obj
);
479 bt_value_generic_freeze(object
);
483 void (* const freeze_funcs
[])(struct bt_value
*) = {
484 [BT_VALUE_TYPE_NULL
] = bt_value_null_freeze
,
485 [BT_VALUE_TYPE_BOOL
] = bt_value_generic_freeze
,
486 [BT_VALUE_TYPE_INTEGER
] = bt_value_generic_freeze
,
487 [BT_VALUE_TYPE_FLOAT
] = bt_value_generic_freeze
,
488 [BT_VALUE_TYPE_STRING
] = bt_value_generic_freeze
,
489 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_freeze
,
490 [BT_VALUE_TYPE_MAP
] = bt_value_map_freeze
,
494 void bt_value_destroy(struct bt_object
*obj
)
496 struct bt_value
*value
;
498 value
= container_of(obj
, struct bt_value
, base
);
499 assert(value
->type
!= BT_VALUE_TYPE_UNKNOWN
);
500 BT_LOGD("Destroying value: addr=%p", value
);
502 if (bt_value_is_null(value
)) {
503 BT_LOGD_STR("Not destroying the null value singleton.");
507 if (destroy_funcs
[value
->type
]) {
508 destroy_funcs
[value
->type
](value
);
514 enum bt_value_status
bt_value_freeze(struct bt_value
*object
)
516 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
519 BT_LOGW_STR("Invalid parameter: value object is NULL.");
520 ret
= BT_VALUE_STATUS_INVAL
;
524 if (object
->is_frozen
) {
528 BT_LOGD("Freezing value: addr=%p", object
);
529 freeze_funcs
[object
->type
](object
);
535 bt_bool
bt_value_is_frozen(const struct bt_value
*object
)
537 return object
&& object
->is_frozen
;
540 enum bt_value_type
bt_value_get_type(const struct bt_value
*object
)
543 BT_LOGW_STR("Invalid parameter: value object is NULL.");
544 return BT_VALUE_TYPE_UNKNOWN
;
551 struct bt_value
bt_value_create_base(enum bt_value_type type
)
553 struct bt_value base
;
556 base
.is_frozen
= BT_FALSE
;
557 bt_object_init(&base
, bt_value_destroy
);
561 struct bt_value
*bt_value_bool_create_init(bt_bool val
)
563 struct bt_value_bool
*bool_obj
;
565 BT_LOGD("Creating boolean value object: val=%d", val
);
566 bool_obj
= g_new0(struct bt_value_bool
, 1);
569 BT_LOGE_STR("Failed to allocate one boolean value object.");
573 bool_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_BOOL
);
574 bool_obj
->value
= val
;
575 BT_LOGD("Created boolean value object: addr=%p", bool_obj
);
578 return BT_VALUE_FROM_CONCRETE(bool_obj
);
581 struct bt_value
*bt_value_bool_create(void)
583 return bt_value_bool_create_init(BT_FALSE
);
586 struct bt_value
*bt_value_integer_create_init(int64_t val
)
588 struct bt_value_integer
*integer_obj
;
590 BT_LOGD("Creating integer value object: val=%" PRId64
, val
);
591 integer_obj
= g_new0(struct bt_value_integer
, 1);
594 BT_LOGE_STR("Failed to allocate one integer value object.");
598 integer_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_INTEGER
);
599 integer_obj
->value
= val
;
600 BT_LOGD("Created integer value object: addr=%p",
604 return BT_VALUE_FROM_CONCRETE(integer_obj
);
607 struct bt_value
*bt_value_integer_create(void)
609 return bt_value_integer_create_init(0);
612 struct bt_value
*bt_value_float_create_init(double val
)
614 struct bt_value_float
*float_obj
;
616 BT_LOGD("Creating floating point number value object: val=%f", val
);
617 float_obj
= g_new0(struct bt_value_float
, 1);
620 BT_LOGE_STR("Failed to allocate one floating point number value object.");
624 float_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_FLOAT
);
625 float_obj
->value
= val
;
626 BT_LOGD("Created floating point number value object: addr=%p",
630 return BT_VALUE_FROM_CONCRETE(float_obj
);
633 struct bt_value
*bt_value_float_create(void)
635 return bt_value_float_create_init(0.);
638 struct bt_value
*bt_value_string_create_init(const char *val
)
640 struct bt_value_string
*string_obj
= NULL
;
643 BT_LOGW_STR("Invalid parameter: value is NULL.");
647 BT_LOGD("Creating string value object: val-len=%u", strlen(val
));
648 string_obj
= g_new0(struct bt_value_string
, 1);
651 BT_LOGE_STR("Failed to allocate one string object.");
655 string_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_STRING
);
656 string_obj
->gstr
= g_string_new(val
);
658 if (!string_obj
->gstr
) {
659 BT_LOGE_STR("Failed to allocate a GString.");
665 BT_LOGD("Created string value object: addr=%p",
669 return BT_VALUE_FROM_CONCRETE(string_obj
);
672 struct bt_value
*bt_value_string_create(void)
674 return bt_value_string_create_init("");
677 struct bt_value
*bt_value_array_create(void)
679 struct bt_value_array
*array_obj
;
681 BT_LOGD_STR("Creating empty array value object.");
682 array_obj
= g_new0(struct bt_value_array
, 1);
685 BT_LOGE_STR("Failed to allocate one array object.");
689 array_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_ARRAY
);
690 array_obj
->garray
= bt_g_ptr_array_new_full(0,
691 (GDestroyNotify
) bt_put
);
693 if (!array_obj
->garray
) {
694 BT_LOGE_STR("Failed to allocate a GPtrArray.");
700 BT_LOGD("Created array value object: addr=%p",
704 return BT_VALUE_FROM_CONCRETE(array_obj
);
707 struct bt_value
*bt_value_map_create(void)
709 struct bt_value_map
*map_obj
;
711 BT_LOGD_STR("Creating empty map value object.");
712 map_obj
= g_new0(struct bt_value_map
, 1);
715 BT_LOGE_STR("Failed to allocate one map object.");
719 map_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_MAP
);
720 map_obj
->ght
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
721 NULL
, (GDestroyNotify
) bt_put
);
724 BT_LOGE_STR("Failed to allocate a GHashTable.");
730 BT_LOGD("Created map value object: addr=%p",
734 return BT_VALUE_FROM_CONCRETE(map_obj
);
737 enum bt_value_status
bt_value_bool_get(const struct bt_value
*bool_obj
,
740 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
741 struct bt_value_bool
*typed_bool_obj
= BT_VALUE_TO_BOOL(bool_obj
);
743 if (!bool_obj
|| !val
) {
744 BT_LOGW("Invalid parameter: value object or value is NULL: "
745 "value-addr=%p, raw-value-addr=%p",
747 ret
= BT_VALUE_STATUS_INVAL
;
751 if (!bt_value_is_bool(bool_obj
)) {
752 BT_LOGW("Invalid parameter: value is not a boolean value: addr=%p, "
753 "type=%s", bool_obj
, bool_obj
->type
,
754 bt_value_type_string(bool_obj
->type
));
755 ret
= BT_VALUE_STATUS_INVAL
;
759 *val
= typed_bool_obj
->value
;
765 enum bt_value_status
bt_value_bool_set(struct bt_value
*bool_obj
, bt_bool val
)
767 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
768 struct bt_value_bool
*typed_bool_obj
= BT_VALUE_TO_BOOL(bool_obj
);
771 BT_LOGW_STR("Invalid parameter: value object is NULL.");
772 ret
= BT_VALUE_STATUS_INVAL
;
776 if (!bt_value_is_bool(bool_obj
)) {
777 BT_LOGW("Invalid parameter: value is not a boolean value: addr=%p, "
779 bt_value_type_string(bool_obj
->type
));
780 ret
= BT_VALUE_STATUS_INVAL
;
784 if (bool_obj
->is_frozen
) {
785 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
787 ret
= BT_VALUE_STATUS_FROZEN
;
791 typed_bool_obj
->value
= val
;
792 BT_LOGV("Set boolean value's raw value: value-addr=%p, value=%d",
799 enum bt_value_status
bt_value_integer_get(const struct bt_value
*integer_obj
,
802 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
803 struct bt_value_integer
*typed_integer_obj
=
804 BT_VALUE_TO_INTEGER(integer_obj
);
806 if (!integer_obj
|| !val
) {
807 BT_LOGW("Invalid parameter: value object or value is NULL: "
808 "value-addr=%p, raw-value-addr=%p",
810 ret
= BT_VALUE_STATUS_INVAL
;
814 if (!bt_value_is_integer(integer_obj
)) {
815 BT_LOGW("Invalid parameter: value is not an integer value: addr=%p, "
816 "type=%s", integer_obj
,
817 bt_value_type_string(integer_obj
->type
));
818 ret
= BT_VALUE_STATUS_INVAL
;
822 *val
= typed_integer_obj
->value
;
828 enum bt_value_status
bt_value_integer_set(struct bt_value
*integer_obj
,
831 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
832 struct bt_value_integer
*typed_integer_obj
=
833 BT_VALUE_TO_INTEGER(integer_obj
);
836 BT_LOGW_STR("Invalid parameter: value object is NULL.");
837 ret
= BT_VALUE_STATUS_INVAL
;
841 if (!bt_value_is_integer(integer_obj
)) {
842 BT_LOGW("Invalid parameter: value is not an integer value: addr=%p, "
843 "type=%s", integer_obj
,
844 bt_value_type_string(integer_obj
->type
));
845 ret
= BT_VALUE_STATUS_INVAL
;
849 if (integer_obj
->is_frozen
) {
850 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
852 ret
= BT_VALUE_STATUS_FROZEN
;
856 typed_integer_obj
->value
= val
;
857 BT_LOGV("Set integer value's raw value: value-addr=%p, value=%" PRId64
,
864 enum bt_value_status
bt_value_float_get(const struct bt_value
*float_obj
,
867 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
868 struct bt_value_float
*typed_float_obj
=
869 BT_VALUE_TO_FLOAT(float_obj
);
871 if (!float_obj
|| !val
) {
872 BT_LOGW("Invalid parameter: value object or value is NULL: "
873 "value-addr=%p, raw-value-addr=%p",
875 ret
= BT_VALUE_STATUS_INVAL
;
879 if (!bt_value_is_float(float_obj
)) {
880 BT_LOGW("Invalid parameter: value is not a floating point number value: addr=%p, "
881 "type=%s", float_obj
,
882 bt_value_type_string(float_obj
->type
));
883 ret
= BT_VALUE_STATUS_INVAL
;
887 *val
= typed_float_obj
->value
;
893 enum bt_value_status
bt_value_float_set(struct bt_value
*float_obj
,
896 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
897 struct bt_value_float
*typed_float_obj
=
898 BT_VALUE_TO_FLOAT(float_obj
);
901 BT_LOGW_STR("Invalid parameter: value object is NULL.");
902 ret
= BT_VALUE_STATUS_INVAL
;
906 if (!bt_value_is_float(float_obj
)) {
907 BT_LOGW("Invalid parameter: value is not a floating point number value: addr=%p, "
908 "type=%s", float_obj
,
909 bt_value_type_string(float_obj
->type
));
910 ret
= BT_VALUE_STATUS_INVAL
;
914 if (float_obj
->is_frozen
) {
915 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
917 ret
= BT_VALUE_STATUS_FROZEN
;
921 typed_float_obj
->value
= val
;
922 BT_LOGV("Set floating point number value's raw value: value-addr=%p, value=%f",
929 enum bt_value_status
bt_value_string_get(const struct bt_value
*string_obj
,
932 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
933 struct bt_value_string
*typed_string_obj
=
934 BT_VALUE_TO_STRING(string_obj
);
936 if (!string_obj
|| !val
) {
937 BT_LOGW("Invalid parameter: value object or value is NULL: "
938 "value-addr=%p, raw-value-addr=%p",
940 ret
= BT_VALUE_STATUS_INVAL
;
944 if (!bt_value_is_string(string_obj
)) {
945 BT_LOGW("Invalid parameter: value is not a string value: addr=%p, "
946 "type=%s", string_obj
,
947 bt_value_type_string(string_obj
->type
));
948 ret
= BT_VALUE_STATUS_INVAL
;
952 *val
= typed_string_obj
->gstr
->str
;
958 enum bt_value_status
bt_value_string_set(struct bt_value
*string_obj
,
961 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
962 struct bt_value_string
*typed_string_obj
=
963 BT_VALUE_TO_STRING(string_obj
);
965 if (!string_obj
|| !val
) {
966 BT_LOGW("Invalid parameter: value object or value is NULL: "
967 "value-addr=%p, raw-value-addr=%p",
969 ret
= BT_VALUE_STATUS_INVAL
;
973 if (!bt_value_is_string(string_obj
)) {
974 BT_LOGW("Invalid parameter: value is not a string value: addr=%p, "
975 "type=%s", string_obj
,
976 bt_value_type_string(string_obj
->type
));
977 ret
= BT_VALUE_STATUS_INVAL
;
981 if (string_obj
->is_frozen
) {
982 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
984 ret
= BT_VALUE_STATUS_FROZEN
;
988 g_string_assign(typed_string_obj
->gstr
, val
);
989 BT_LOGV("Set string value's raw value: value-addr=%p, raw-value-addr=%p",
996 int64_t bt_value_array_size(const struct bt_value
*array_obj
)
999 struct bt_value_array
*typed_array_obj
=
1000 BT_VALUE_TO_ARRAY(array_obj
);
1003 BT_LOGW_STR("Invalid parameter: value object is NULL.");
1004 ret
= (int64_t) BT_VALUE_STATUS_INVAL
;
1008 if (!bt_value_is_array(array_obj
)) {
1009 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1010 "type=%s", array_obj
,
1011 bt_value_type_string(array_obj
->type
));
1012 ret
= BT_VALUE_STATUS_INVAL
;
1016 ret
= (int64_t) typed_array_obj
->garray
->len
;
1022 bt_bool
bt_value_array_is_empty(const struct bt_value
*array_obj
)
1024 return bt_value_array_size(array_obj
) == 0;
1027 struct bt_value
*bt_value_array_get(const struct bt_value
*array_obj
,
1030 struct bt_value
*ret
;
1031 struct bt_value_array
*typed_array_obj
=
1032 BT_VALUE_TO_ARRAY(array_obj
);
1035 BT_LOGW("Invalid parameter: value object is NULL: index=%" PRIu64
,
1041 if (!bt_value_is_array(array_obj
)) {
1042 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1043 "type=%s", array_obj
,
1044 bt_value_type_string(array_obj
->type
));
1049 if (index
>= typed_array_obj
->garray
->len
) {
1050 BT_LOGW("Invalid parameter: index is out of bounds: "
1051 "addr=%p, index=%" PRIu64
", size=%u",
1052 array_obj
, index
, typed_array_obj
->garray
->len
);
1057 ret
= g_ptr_array_index(typed_array_obj
->garray
, index
);
1064 enum bt_value_status
bt_value_array_append(struct bt_value
*array_obj
,
1065 struct bt_value
*element_obj
)
1067 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1068 struct bt_value_array
*typed_array_obj
=
1069 BT_VALUE_TO_ARRAY(array_obj
);
1071 if (!array_obj
|| !element_obj
) {
1072 BT_LOGW("Invalid parameter: array value or element value is NULL: "
1073 "array-value-addr=%p, element-value-addr=%p",
1074 array_obj
, element_obj
);
1075 ret
= BT_VALUE_STATUS_INVAL
;
1079 if (!bt_value_is_array(array_obj
)) {
1080 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1081 "type=%s", array_obj
,
1082 bt_value_type_string(array_obj
->type
));
1083 ret
= BT_VALUE_STATUS_INVAL
;
1087 if (array_obj
->is_frozen
) {
1088 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1090 ret
= BT_VALUE_STATUS_FROZEN
;
1094 g_ptr_array_add(typed_array_obj
->garray
, element_obj
);
1095 bt_get(element_obj
);
1096 BT_LOGV("Appended element to array value: array-value-addr=%p, "
1097 "element-value-addr=%p, new-size=%u",
1098 array_obj
, element_obj
, typed_array_obj
->garray
->len
);
1104 enum bt_value_status
bt_value_array_append_bool(struct bt_value
*array_obj
,
1107 enum bt_value_status ret
;
1108 struct bt_value
*bool_obj
= NULL
;
1110 bool_obj
= bt_value_bool_create_init(val
);
1111 ret
= bt_value_array_append(array_obj
, bool_obj
);
1116 enum bt_value_status
bt_value_array_append_integer(
1117 struct bt_value
*array_obj
, int64_t val
)
1119 enum bt_value_status ret
;
1120 struct bt_value
*integer_obj
= NULL
;
1122 integer_obj
= bt_value_integer_create_init(val
);
1123 ret
= bt_value_array_append(array_obj
, integer_obj
);
1124 bt_put(integer_obj
);
1128 enum bt_value_status
bt_value_array_append_float(struct bt_value
*array_obj
,
1131 enum bt_value_status ret
;
1132 struct bt_value
*float_obj
= NULL
;
1134 float_obj
= bt_value_float_create_init(val
);
1135 ret
= bt_value_array_append(array_obj
, float_obj
);
1140 enum bt_value_status
bt_value_array_append_string(struct bt_value
*array_obj
,
1143 enum bt_value_status ret
;
1144 struct bt_value
*string_obj
= NULL
;
1146 string_obj
= bt_value_string_create_init(val
);
1147 ret
= bt_value_array_append(array_obj
, string_obj
);
1152 enum bt_value_status
bt_value_array_append_empty_array(
1153 struct bt_value
*array_obj
)
1155 enum bt_value_status ret
;
1156 struct bt_value
*empty_array_obj
= NULL
;
1158 empty_array_obj
= bt_value_array_create();
1159 ret
= bt_value_array_append(array_obj
, empty_array_obj
);
1160 bt_put(empty_array_obj
);
1164 enum bt_value_status
bt_value_array_append_empty_map(struct bt_value
*array_obj
)
1166 enum bt_value_status ret
;
1167 struct bt_value
*map_obj
= NULL
;
1169 map_obj
= bt_value_map_create();
1170 ret
= bt_value_array_append(array_obj
, map_obj
);
1175 enum bt_value_status
bt_value_array_set(struct bt_value
*array_obj
,
1176 uint64_t index
, struct bt_value
*element_obj
)
1178 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1179 struct bt_value_array
*typed_array_obj
=
1180 BT_VALUE_TO_ARRAY(array_obj
);
1182 if (!array_obj
|| !element_obj
) {
1183 BT_LOGW("Invalid parameter: array value or element value is NULL: "
1184 "index=%" PRIu64
", array-value-addr=%p, element-value-addr=%p",
1185 index
, array_obj
, element_obj
);
1186 ret
= BT_VALUE_STATUS_INVAL
;
1190 if (!bt_value_is_array(array_obj
)) {
1191 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1192 "type=%s", array_obj
,
1193 bt_value_type_string(array_obj
->type
));
1194 ret
= BT_VALUE_STATUS_INVAL
;
1198 if (index
>= typed_array_obj
->garray
->len
) {
1199 BT_LOGW("Invalid parameter: index is out of bounds: "
1200 "addr=%p, index=%" PRIu64
", size=%u",
1201 array_obj
, index
, typed_array_obj
->garray
->len
);
1202 ret
= BT_VALUE_STATUS_INVAL
;
1206 if (array_obj
->is_frozen
) {
1207 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1209 ret
= BT_VALUE_STATUS_FROZEN
;
1213 bt_put(g_ptr_array_index(typed_array_obj
->garray
, index
));
1214 g_ptr_array_index(typed_array_obj
->garray
, index
) = element_obj
;
1215 bt_get(element_obj
);
1216 BT_LOGV("Set array value's element: array-value-addr=%p, "
1217 "index=%" PRIu64
", element-value-addr=%p",
1218 array_obj
, index
, element_obj
);
1224 int64_t bt_value_map_size(const struct bt_value
*map_obj
)
1227 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1230 BT_LOGW_STR("Invalid parameter: value object is NULL.");
1231 ret
= (int64_t) BT_VALUE_STATUS_INVAL
;
1235 if (!bt_value_is_map(map_obj
)) {
1236 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1238 bt_value_type_string(map_obj
->type
));
1239 ret
= (int64_t) BT_VALUE_STATUS_INVAL
;
1243 ret
= (int64_t) g_hash_table_size(typed_map_obj
->ght
);
1249 bt_bool
bt_value_map_is_empty(const struct bt_value
*map_obj
)
1251 return bt_value_map_size(map_obj
) == 0;
1254 struct bt_value
*bt_value_map_get(const struct bt_value
*map_obj
,
1258 struct bt_value
*ret
;
1259 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1261 if (!map_obj
|| !key
) {
1262 BT_LOGW("Invalid parameter: value object or key is NULL: "
1263 "value-addr=%p, key-addr=%p", map_obj
, key
);
1268 if (!bt_value_is_map(map_obj
)) {
1269 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1271 bt_value_type_string(map_obj
->type
));
1276 quark
= g_quark_from_string(key
);
1277 ret
= g_hash_table_lookup(typed_map_obj
->ght
, GUINT_TO_POINTER(quark
));
1286 bt_bool
bt_value_map_has_key(const struct bt_value
*map_obj
, const char *key
)
1290 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1292 if (!map_obj
|| !key
) {
1293 BT_LOGW("Invalid parameter: value object or key is NULL: "
1294 "value-addr=%p, key-addr=%p", map_obj
, key
);
1299 if (!bt_value_is_map(map_obj
)) {
1300 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1302 bt_value_type_string(map_obj
->type
));
1307 quark
= g_quark_from_string(key
);
1308 ret
= bt_g_hash_table_contains(typed_map_obj
->ght
,
1309 GUINT_TO_POINTER(quark
));
1315 enum bt_value_status
bt_value_map_insert(struct bt_value
*map_obj
,
1316 const char *key
, struct bt_value
*element_obj
)
1319 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1320 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1322 if (!map_obj
|| !key
|| !element_obj
) {
1323 BT_LOGW("Invalid parameter: map value, key, or element value is NULL: "
1324 "map-value-addr=%p, key-addr=%p, element-value-addr=%p",
1325 map_obj
, key
, element_obj
);
1326 ret
= BT_VALUE_STATUS_INVAL
;
1330 if (!bt_value_is_map(map_obj
)) {
1331 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1333 bt_value_type_string(map_obj
->type
));
1334 ret
= BT_VALUE_STATUS_INVAL
;
1338 if (map_obj
->is_frozen
) {
1339 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1341 ret
= BT_VALUE_STATUS_FROZEN
;
1345 quark
= g_quark_from_string(key
);
1346 g_hash_table_insert(typed_map_obj
->ght
,
1347 GUINT_TO_POINTER(quark
), element_obj
);
1348 bt_get(element_obj
);
1349 BT_LOGV("Inserted value into map value: map-value-addr=%p, "
1350 "key=\"%s\", element-value-addr=%p",
1351 map_obj
, key
, element_obj
);
1357 enum bt_value_status
bt_value_map_insert_bool(struct bt_value
*map_obj
,
1358 const char *key
, bt_bool val
)
1360 enum bt_value_status ret
;
1361 struct bt_value
*bool_obj
= NULL
;
1363 bool_obj
= bt_value_bool_create_init(val
);
1364 ret
= bt_value_map_insert(map_obj
, key
, bool_obj
);
1369 enum bt_value_status
bt_value_map_insert_integer(struct bt_value
*map_obj
,
1370 const char *key
, int64_t val
)
1372 enum bt_value_status ret
;
1373 struct bt_value
*integer_obj
= NULL
;
1375 integer_obj
= bt_value_integer_create_init(val
);
1376 ret
= bt_value_map_insert(map_obj
, key
, integer_obj
);
1377 bt_put(integer_obj
);
1381 enum bt_value_status
bt_value_map_insert_float(struct bt_value
*map_obj
,
1382 const char *key
, double val
)
1384 enum bt_value_status ret
;
1385 struct bt_value
*float_obj
= NULL
;
1387 float_obj
= bt_value_float_create_init(val
);
1388 ret
= bt_value_map_insert(map_obj
, key
, float_obj
);
1393 enum bt_value_status
bt_value_map_insert_string(struct bt_value
*map_obj
,
1394 const char *key
, const char *val
)
1396 enum bt_value_status ret
;
1397 struct bt_value
*string_obj
= NULL
;
1399 string_obj
= bt_value_string_create_init(val
);
1400 ret
= bt_value_map_insert(map_obj
, key
, string_obj
);
1405 enum bt_value_status
bt_value_map_insert_empty_array(struct bt_value
*map_obj
,
1408 enum bt_value_status ret
;
1409 struct bt_value
*array_obj
= NULL
;
1411 array_obj
= bt_value_array_create();
1412 ret
= bt_value_map_insert(map_obj
, key
, array_obj
);
1417 enum bt_value_status
bt_value_map_insert_empty_map(struct bt_value
*map_obj
,
1420 enum bt_value_status ret
;
1421 struct bt_value
*empty_map_obj
= NULL
;
1423 empty_map_obj
= bt_value_map_create();
1424 ret
= bt_value_map_insert(map_obj
, key
, empty_map_obj
);
1425 bt_put(empty_map_obj
);
1429 enum bt_value_status
bt_value_map_foreach(const struct bt_value
*map_obj
,
1430 bt_value_map_foreach_cb cb
, void *data
)
1432 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1433 gpointer key
, element_obj
;
1434 GHashTableIter iter
;
1435 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1437 if (!map_obj
|| !cb
) {
1438 BT_LOGW("Invalid parameter: map value or callback is NULL: "
1439 "value-addr=%p, cb-addr=%p", map_obj
, cb
);
1440 ret
= BT_VALUE_STATUS_INVAL
;
1444 if (!bt_value_is_map(map_obj
)) {
1445 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1447 bt_value_type_string(map_obj
->type
));
1448 ret
= BT_VALUE_STATUS_INVAL
;
1452 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
1454 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
1455 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
1457 if (!cb(key_str
, element_obj
, data
)) {
1458 BT_LOGV("User cancelled the loop: key=\"%s\", "
1459 "value-addr=%p, data=%p",
1460 key_str
, element_obj
, data
);
1461 ret
= BT_VALUE_STATUS_CANCELLED
;
1470 struct extend_map_element_data
{
1471 struct bt_value
*extended_obj
;
1476 bt_bool
extend_map_element(const char *key
,
1477 struct bt_value
*extension_obj_elem
, void *data
)
1479 bt_bool ret
= BT_TRUE
;
1481 struct extend_map_element_data
*extend_data
= data
;
1483 /* Copy object which is to replace the current one */
1484 struct bt_value
*extension_obj_elem_copy
=
1485 bt_value_copy(extension_obj_elem
);
1487 /* Replace in extended object */
1488 if (bt_value_map_insert(extend_data
->extended_obj
, key
,
1489 extension_obj_elem_copy
)) {
1490 BT_LOGE("Cannot replace value in extended value: key=\"%s\", "
1491 "extended-value-addr=%p, element-value-addr=%p",
1492 key
, extend_data
->extended_obj
,
1493 extension_obj_elem_copy
);
1501 extend_data
->got_error
= BT_TRUE
;
1504 BT_PUT(extension_obj_elem_copy
);
1508 struct bt_value
*bt_value_map_extend(struct bt_value
*base_map_obj
,
1509 struct bt_value
*extension_obj
)
1511 struct bt_value
*extended_obj
= NULL
;
1512 struct extend_map_element_data extend_data
= { 0 };
1514 if (!base_map_obj
|| !extension_obj
) {
1515 BT_LOGW("Invalid parameter: base value or extension value is NULL: "
1516 "base-value-addr=%p, extension-value-addr=%p",
1517 base_map_obj
, extension_obj
);
1521 if (!bt_value_is_map(base_map_obj
)) {
1522 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1523 "type=%s", base_map_obj
,
1524 bt_value_type_string(base_map_obj
->type
));
1528 if (!bt_value_is_map(extension_obj
)) {
1529 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1530 "type=%s", extension_obj
,
1531 bt_value_type_string(extension_obj
->type
));
1535 BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p",
1536 base_map_obj
, extension_obj
);
1538 /* Create copy of base map object to start with */
1539 extended_obj
= bt_value_copy(base_map_obj
);
1540 if (!extended_obj
) {
1541 BT_LOGE("Cannot copy base value: base-value-addr=%p",
1547 * For each key in the extension map object, replace this key
1548 * in the copied map object.
1550 extend_data
.extended_obj
= extended_obj
;
1552 if (bt_value_map_foreach(extension_obj
, extend_map_element
,
1554 BT_LOGE("Cannot iterate on the extension object's elements: ",
1555 "extension-value-addr=%p", extension_obj
);
1559 if (extend_data
.got_error
) {
1560 BT_LOGE("Failed to successfully iterate on the extension object's elements: ",
1561 "extension-value-addr=%p", extension_obj
);
1565 BT_LOGD("Extended map value: extended-value-addr=%p",
1570 BT_PUT(extended_obj
);
1573 return extended_obj
;
1576 struct bt_value
*bt_value_copy(const struct bt_value
*object
)
1578 struct bt_value
*copy_obj
= NULL
;
1581 BT_LOGW_STR("Invalid parameter: value object is NULL.");
1585 BT_LOGD("Copying value object: addr=%p", object
);
1586 copy_obj
= copy_funcs
[object
->type
](object
);
1588 BT_LOGD("Copied value object: copy-value-addr=%p",
1591 BT_LOGE_STR("Failed to copy value object.");
1598 bt_bool
bt_value_compare(const struct bt_value
*object_a
,
1599 const struct bt_value
*object_b
)
1601 bt_bool ret
= BT_FALSE
;
1603 if (!object_a
|| !object_b
) {
1604 BT_LOGW("Invalid parameter: value A or value B is NULL: "
1605 "value-a-addr=%p, value-b-addr=%p",
1606 object_a
, object_b
);
1610 if (object_a
->type
!= object_b
->type
) {
1611 BT_LOGV("Values are different: type mismatch: "
1612 "value-a-addr=%p, value-b-addr=%p, "
1613 "value-a-type=%s, value-b-type=%s",
1615 bt_value_type_string(object_a
->type
),
1616 bt_value_type_string(object_b
->type
));
1620 ret
= compare_funcs
[object_a
->type
](object_a
, object_b
);