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_LOGD("Copying array value's element: element-addr=%p, "
201 "index=%d", element_obj
, i
);
202 element_obj_copy
= bt_value_copy(element_obj
);
204 if (!element_obj_copy
) {
205 BT_LOGE("Cannot copy array value's element: "
206 "array-addr=%p, index=%d",
212 ret
= bt_value_array_append(copy_obj
, element_obj_copy
);
213 BT_PUT(element_obj_copy
);
215 BT_LOGE("Cannot append to array value: addr=%p",
222 BT_LOGD("Copied array value: original-addr=%p, copy-addr=%p",
223 array_obj
, copy_obj
);
230 struct bt_value
*bt_value_map_copy(const struct bt_value
*map_obj
)
234 gpointer key
, element_obj
;
235 struct bt_value
*copy_obj
;
236 struct bt_value
*element_obj_copy
;
237 struct bt_value_map
*typed_map_obj
;
239 BT_LOGD("Copying map value: addr=%p", map_obj
);
240 typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
241 copy_obj
= bt_value_map_create();
247 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
249 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
250 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
253 BT_LOGD("Copying map value's element: element-addr=%p, "
254 "key=\"%s\"", element_obj
, key_str
);
255 element_obj_copy
= bt_value_copy(element_obj
);
256 if (!element_obj_copy
) {
257 BT_LOGE("Cannot copy map value's element: "
258 "map-addr=%p, key=\"%s\"",
264 ret
= bt_value_map_insert(copy_obj
, key_str
, element_obj_copy
);
265 BT_PUT(element_obj_copy
);
267 BT_LOGE("Cannot insert into map value: addr=%p, key=\"%s\"",
274 BT_LOGD("Copied map value: addr=%p", map_obj
);
281 struct bt_value
*(* const copy_funcs
[])(const struct bt_value
*) = {
282 [BT_VALUE_TYPE_NULL
] = bt_value_null_copy
,
283 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_copy
,
284 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_copy
,
285 [BT_VALUE_TYPE_FLOAT
] = bt_value_float_copy
,
286 [BT_VALUE_TYPE_STRING
] = bt_value_string_copy
,
287 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_copy
,
288 [BT_VALUE_TYPE_MAP
] = bt_value_map_copy
,
292 bt_bool
bt_value_null_compare(const struct bt_value
*object_a
,
293 const struct bt_value
*object_b
)
296 * Always BT_TRUE since bt_value_compare() already checks if both
297 * object_a and object_b have the same type, and in the case of
298 * null value objects, they're always the same if it is so.
304 bt_bool
bt_value_bool_compare(const struct bt_value
*object_a
,
305 const struct bt_value
*object_b
)
307 if (BT_VALUE_TO_BOOL(object_a
)->value
!=
308 BT_VALUE_TO_BOOL(object_b
)->value
) {
309 BT_LOGV("Boolean value objects are different: "
310 "bool-a-val=%d, bool-b-val=%d",
311 BT_VALUE_TO_BOOL(object_a
)->value
,
312 BT_VALUE_TO_BOOL(object_b
)->value
);
320 bt_bool
bt_value_integer_compare(const struct bt_value
*object_a
,
321 const struct bt_value
*object_b
)
323 if (BT_VALUE_TO_INTEGER(object_a
)->value
!=
324 BT_VALUE_TO_INTEGER(object_b
)->value
) {
325 BT_LOGV("Integer value objects are different: "
326 "int-a-val=%" PRId64
", int-b-val=%" PRId64
,
327 BT_VALUE_TO_INTEGER(object_a
)->value
,
328 BT_VALUE_TO_INTEGER(object_b
)->value
);
336 bt_bool
bt_value_float_compare(const struct bt_value
*object_a
,
337 const struct bt_value
*object_b
)
339 if (BT_VALUE_TO_FLOAT(object_a
)->value
!=
340 BT_VALUE_TO_FLOAT(object_b
)->value
) {
341 BT_LOGV("Floating point number value objects are different: "
342 "float-a-val=%f, float-b-val=%f",
343 BT_VALUE_TO_FLOAT(object_a
)->value
,
344 BT_VALUE_TO_FLOAT(object_b
)->value
);
352 bt_bool
bt_value_string_compare(const struct bt_value
*object_a
,
353 const struct bt_value
*object_b
)
355 if (strcmp(BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
356 BT_VALUE_TO_STRING(object_b
)->gstr
->str
) != 0) {
357 BT_LOGV("String value objects are different: "
358 "string-a-val=\"%s\", string-b-val=\"%s\"",
359 BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
360 BT_VALUE_TO_STRING(object_b
)->gstr
->str
);
368 bt_bool
bt_value_array_compare(const struct bt_value
*object_a
,
369 const struct bt_value
*object_b
)
372 bt_bool ret
= BT_TRUE
;
373 const struct bt_value_array
*array_obj_a
=
374 BT_VALUE_TO_ARRAY(object_a
);
376 if (bt_value_array_size(object_a
) != bt_value_array_size(object_b
)) {
377 BT_LOGV("Array values are different: size mismatch "
378 "value-a-addr=%p, value-b-addr=%p, "
379 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
381 bt_value_array_size(object_a
),
382 bt_value_array_size(object_b
));
387 for (i
= 0; i
< array_obj_a
->garray
->len
; ++i
) {
388 struct bt_value
*element_obj_a
;
389 struct bt_value
*element_obj_b
;
391 element_obj_a
= bt_value_array_get(object_a
, i
);
392 element_obj_b
= bt_value_array_get(object_b
, i
);
394 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
395 BT_LOGV("Array values's elements are different: "
396 "value-a-addr=%p, value-b-addr=%p, index=%d",
397 element_obj_a
, element_obj_b
, i
);
398 BT_PUT(element_obj_a
);
399 BT_PUT(element_obj_b
);
404 BT_PUT(element_obj_a
);
405 BT_PUT(element_obj_b
);
413 bt_bool
bt_value_map_compare(const struct bt_value
*object_a
,
414 const struct bt_value
*object_b
)
416 bt_bool ret
= BT_TRUE
;
418 gpointer key
, element_obj_a
;
419 const struct bt_value_map
*map_obj_a
= BT_VALUE_TO_MAP(object_a
);
421 if (bt_value_map_size(object_a
) != bt_value_map_size(object_b
)) {
422 BT_LOGV("Map values are different: size mismatch "
423 "value-a-addr=%p, value-b-addr=%p, "
424 "value-a-size=%" PRId64
", value-b-size=%" PRId64
,
426 bt_value_map_size(object_a
),
427 bt_value_map_size(object_b
));
432 g_hash_table_iter_init(&iter
, map_obj_a
->ght
);
434 while (g_hash_table_iter_next(&iter
, &key
, &element_obj_a
)) {
435 struct bt_value
*element_obj_b
;
436 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
438 element_obj_b
= bt_value_map_get(object_b
, key_str
);
440 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
441 BT_LOGV("Map values's elements are different: "
442 "value-a-addr=%p, value-b-addr=%p, key=\"%s\"",
443 element_obj_a
, element_obj_b
, key_str
);
444 BT_PUT(element_obj_b
);
449 BT_PUT(element_obj_b
);
457 bt_bool (* const compare_funcs
[])(const struct bt_value
*,
458 const struct bt_value
*) = {
459 [BT_VALUE_TYPE_NULL
] = bt_value_null_compare
,
460 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_compare
,
461 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_compare
,
462 [BT_VALUE_TYPE_FLOAT
] = bt_value_float_compare
,
463 [BT_VALUE_TYPE_STRING
] = bt_value_string_compare
,
464 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_compare
,
465 [BT_VALUE_TYPE_MAP
] = bt_value_map_compare
,
468 void bt_value_null_freeze(struct bt_value
*object
)
472 void bt_value_generic_freeze(struct bt_value
*object
)
474 object
->is_frozen
= BT_TRUE
;
477 void bt_value_array_freeze(struct bt_value
*object
)
480 struct bt_value_array
*typed_array_obj
=
481 BT_VALUE_TO_ARRAY(object
);
483 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
484 struct bt_value
*element_obj
=
485 g_ptr_array_index(typed_array_obj
->garray
, i
);
487 bt_value_freeze(element_obj
);
490 bt_value_generic_freeze(object
);
493 void bt_value_map_freeze(struct bt_value
*object
)
496 gpointer key
, element_obj
;
497 const struct bt_value_map
*map_obj
= BT_VALUE_TO_MAP(object
);
499 g_hash_table_iter_init(&iter
, map_obj
->ght
);
501 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
502 bt_value_freeze(element_obj
);
505 bt_value_generic_freeze(object
);
509 void (* const freeze_funcs
[])(struct bt_value
*) = {
510 [BT_VALUE_TYPE_NULL
] = bt_value_null_freeze
,
511 [BT_VALUE_TYPE_BOOL
] = bt_value_generic_freeze
,
512 [BT_VALUE_TYPE_INTEGER
] = bt_value_generic_freeze
,
513 [BT_VALUE_TYPE_FLOAT
] = bt_value_generic_freeze
,
514 [BT_VALUE_TYPE_STRING
] = bt_value_generic_freeze
,
515 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_freeze
,
516 [BT_VALUE_TYPE_MAP
] = bt_value_map_freeze
,
520 void bt_value_destroy(struct bt_object
*obj
)
522 struct bt_value
*value
;
524 value
= container_of(obj
, struct bt_value
, base
);
525 assert(value
->type
!= BT_VALUE_TYPE_UNKNOWN
);
526 BT_LOGD("Destroying value: addr=%p", value
);
528 if (bt_value_is_null(value
)) {
529 BT_LOGD_STR("Not destroying the null value singleton.");
533 if (destroy_funcs
[value
->type
]) {
534 destroy_funcs
[value
->type
](value
);
540 enum bt_value_status
bt_value_freeze(struct bt_value
*object
)
542 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
545 BT_LOGW_STR("Invalid parameter: value object is NULL.");
546 ret
= BT_VALUE_STATUS_INVAL
;
550 if (object
->is_frozen
) {
554 BT_LOGD("Freezing value: addr=%p", object
);
555 freeze_funcs
[object
->type
](object
);
561 bt_bool
bt_value_is_frozen(const struct bt_value
*object
)
563 return object
&& object
->is_frozen
;
566 enum bt_value_type
bt_value_get_type(const struct bt_value
*object
)
570 * Not an error: user can test NULL value object with
573 BT_LOGV_STR("Value object is NULL.");
574 return BT_VALUE_TYPE_UNKNOWN
;
581 struct bt_value
bt_value_create_base(enum bt_value_type type
)
583 struct bt_value base
;
586 base
.is_frozen
= BT_FALSE
;
587 bt_object_init(&base
, bt_value_destroy
);
591 struct bt_value
*bt_value_bool_create_init(bt_bool val
)
593 struct bt_value_bool
*bool_obj
;
595 BT_LOGD("Creating boolean value object: val=%d", val
);
596 bool_obj
= g_new0(struct bt_value_bool
, 1);
599 BT_LOGE_STR("Failed to allocate one boolean value object.");
603 bool_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_BOOL
);
604 bool_obj
->value
= val
;
605 BT_LOGD("Created boolean value object: addr=%p", bool_obj
);
608 return BT_VALUE_FROM_CONCRETE(bool_obj
);
611 struct bt_value
*bt_value_bool_create(void)
613 return bt_value_bool_create_init(BT_FALSE
);
616 struct bt_value
*bt_value_integer_create_init(int64_t val
)
618 struct bt_value_integer
*integer_obj
;
620 BT_LOGD("Creating integer value object: val=%" PRId64
, val
);
621 integer_obj
= g_new0(struct bt_value_integer
, 1);
624 BT_LOGE_STR("Failed to allocate one integer value object.");
628 integer_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_INTEGER
);
629 integer_obj
->value
= val
;
630 BT_LOGD("Created integer value object: addr=%p",
634 return BT_VALUE_FROM_CONCRETE(integer_obj
);
637 struct bt_value
*bt_value_integer_create(void)
639 return bt_value_integer_create_init(0);
642 struct bt_value
*bt_value_float_create_init(double val
)
644 struct bt_value_float
*float_obj
;
646 BT_LOGD("Creating floating point number value object: val=%f", val
);
647 float_obj
= g_new0(struct bt_value_float
, 1);
650 BT_LOGE_STR("Failed to allocate one floating point number value object.");
654 float_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_FLOAT
);
655 float_obj
->value
= val
;
656 BT_LOGD("Created floating point number value object: addr=%p",
660 return BT_VALUE_FROM_CONCRETE(float_obj
);
663 struct bt_value
*bt_value_float_create(void)
665 return bt_value_float_create_init(0.);
668 struct bt_value
*bt_value_string_create_init(const char *val
)
670 struct bt_value_string
*string_obj
= NULL
;
673 BT_LOGW_STR("Invalid parameter: value is NULL.");
677 BT_LOGD("Creating string value object: val-len=%zu", strlen(val
));
678 string_obj
= g_new0(struct bt_value_string
, 1);
681 BT_LOGE_STR("Failed to allocate one string object.");
685 string_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_STRING
);
686 string_obj
->gstr
= g_string_new(val
);
688 if (!string_obj
->gstr
) {
689 BT_LOGE_STR("Failed to allocate a GString.");
695 BT_LOGD("Created string value object: addr=%p",
699 return BT_VALUE_FROM_CONCRETE(string_obj
);
702 struct bt_value
*bt_value_string_create(void)
704 return bt_value_string_create_init("");
707 struct bt_value
*bt_value_array_create(void)
709 struct bt_value_array
*array_obj
;
711 BT_LOGD_STR("Creating empty array value object.");
712 array_obj
= g_new0(struct bt_value_array
, 1);
715 BT_LOGE_STR("Failed to allocate one array object.");
719 array_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_ARRAY
);
720 array_obj
->garray
= bt_g_ptr_array_new_full(0,
721 (GDestroyNotify
) bt_put
);
723 if (!array_obj
->garray
) {
724 BT_LOGE_STR("Failed to allocate a GPtrArray.");
730 BT_LOGD("Created array value object: addr=%p",
734 return BT_VALUE_FROM_CONCRETE(array_obj
);
737 struct bt_value
*bt_value_map_create(void)
739 struct bt_value_map
*map_obj
;
741 BT_LOGD_STR("Creating empty map value object.");
742 map_obj
= g_new0(struct bt_value_map
, 1);
745 BT_LOGE_STR("Failed to allocate one map object.");
749 map_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_MAP
);
750 map_obj
->ght
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
751 NULL
, (GDestroyNotify
) bt_put
);
754 BT_LOGE_STR("Failed to allocate a GHashTable.");
760 BT_LOGD("Created map value object: addr=%p",
764 return BT_VALUE_FROM_CONCRETE(map_obj
);
767 enum bt_value_status
bt_value_bool_get(const struct bt_value
*bool_obj
,
770 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
771 struct bt_value_bool
*typed_bool_obj
= BT_VALUE_TO_BOOL(bool_obj
);
773 if (!bool_obj
|| !val
) {
774 BT_LOGW("Invalid parameter: value object or value is NULL: "
775 "value-addr=%p, raw-value-addr=%p",
777 ret
= BT_VALUE_STATUS_INVAL
;
781 if (!bt_value_is_bool(bool_obj
)) {
782 BT_LOGW("Invalid parameter: value is not a boolean value: addr=%p, "
784 bt_value_type_string(bool_obj
->type
));
785 ret
= BT_VALUE_STATUS_INVAL
;
789 *val
= typed_bool_obj
->value
;
795 enum bt_value_status
bt_value_bool_set(struct bt_value
*bool_obj
, bt_bool val
)
797 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
798 struct bt_value_bool
*typed_bool_obj
= BT_VALUE_TO_BOOL(bool_obj
);
801 BT_LOGW_STR("Invalid parameter: value object is NULL.");
802 ret
= BT_VALUE_STATUS_INVAL
;
806 if (!bt_value_is_bool(bool_obj
)) {
807 BT_LOGW("Invalid parameter: value is not a boolean value: addr=%p, "
809 bt_value_type_string(bool_obj
->type
));
810 ret
= BT_VALUE_STATUS_INVAL
;
814 if (bool_obj
->is_frozen
) {
815 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
817 ret
= BT_VALUE_STATUS_FROZEN
;
821 typed_bool_obj
->value
= val
;
822 BT_LOGV("Set boolean value's raw value: value-addr=%p, value=%d",
829 enum bt_value_status
bt_value_integer_get(const struct bt_value
*integer_obj
,
832 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
833 struct bt_value_integer
*typed_integer_obj
=
834 BT_VALUE_TO_INTEGER(integer_obj
);
836 if (!integer_obj
|| !val
) {
837 BT_LOGW("Invalid parameter: value object or value is NULL: "
838 "value-addr=%p, raw-value-addr=%p",
840 ret
= BT_VALUE_STATUS_INVAL
;
844 if (!bt_value_is_integer(integer_obj
)) {
845 BT_LOGW("Invalid parameter: value is not an integer value: addr=%p, "
846 "type=%s", integer_obj
,
847 bt_value_type_string(integer_obj
->type
));
848 ret
= BT_VALUE_STATUS_INVAL
;
852 *val
= typed_integer_obj
->value
;
858 enum bt_value_status
bt_value_integer_set(struct bt_value
*integer_obj
,
861 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
862 struct bt_value_integer
*typed_integer_obj
=
863 BT_VALUE_TO_INTEGER(integer_obj
);
866 BT_LOGW_STR("Invalid parameter: value object is NULL.");
867 ret
= BT_VALUE_STATUS_INVAL
;
871 if (!bt_value_is_integer(integer_obj
)) {
872 BT_LOGW("Invalid parameter: value is not an integer value: addr=%p, "
873 "type=%s", integer_obj
,
874 bt_value_type_string(integer_obj
->type
));
875 ret
= BT_VALUE_STATUS_INVAL
;
879 if (integer_obj
->is_frozen
) {
880 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
882 ret
= BT_VALUE_STATUS_FROZEN
;
886 typed_integer_obj
->value
= val
;
887 BT_LOGV("Set integer value's raw value: value-addr=%p, value=%" PRId64
,
894 enum bt_value_status
bt_value_float_get(const struct bt_value
*float_obj
,
897 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
898 struct bt_value_float
*typed_float_obj
=
899 BT_VALUE_TO_FLOAT(float_obj
);
901 if (!float_obj
|| !val
) {
902 BT_LOGW("Invalid parameter: value object or value is NULL: "
903 "value-addr=%p, raw-value-addr=%p",
905 ret
= BT_VALUE_STATUS_INVAL
;
909 if (!bt_value_is_float(float_obj
)) {
910 BT_LOGW("Invalid parameter: value is not a floating point number value: addr=%p, "
911 "type=%s", float_obj
,
912 bt_value_type_string(float_obj
->type
));
913 ret
= BT_VALUE_STATUS_INVAL
;
917 *val
= typed_float_obj
->value
;
923 enum bt_value_status
bt_value_float_set(struct bt_value
*float_obj
,
926 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
927 struct bt_value_float
*typed_float_obj
=
928 BT_VALUE_TO_FLOAT(float_obj
);
931 BT_LOGW_STR("Invalid parameter: value object is NULL.");
932 ret
= BT_VALUE_STATUS_INVAL
;
936 if (!bt_value_is_float(float_obj
)) {
937 BT_LOGW("Invalid parameter: value is not a floating point number value: addr=%p, "
938 "type=%s", float_obj
,
939 bt_value_type_string(float_obj
->type
));
940 ret
= BT_VALUE_STATUS_INVAL
;
944 if (float_obj
->is_frozen
) {
945 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
947 ret
= BT_VALUE_STATUS_FROZEN
;
951 typed_float_obj
->value
= val
;
952 BT_LOGV("Set floating point number value's raw value: value-addr=%p, value=%f",
959 enum bt_value_status
bt_value_string_get(const struct bt_value
*string_obj
,
962 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
963 struct bt_value_string
*typed_string_obj
=
964 BT_VALUE_TO_STRING(string_obj
);
966 if (!string_obj
|| !val
) {
967 BT_LOGW("Invalid parameter: value object or value is NULL: "
968 "value-addr=%p, raw-value-addr=%p",
970 ret
= BT_VALUE_STATUS_INVAL
;
974 if (!bt_value_is_string(string_obj
)) {
975 BT_LOGW("Invalid parameter: value is not a string value: addr=%p, "
976 "type=%s", string_obj
,
977 bt_value_type_string(string_obj
->type
));
978 ret
= BT_VALUE_STATUS_INVAL
;
982 *val
= typed_string_obj
->gstr
->str
;
988 enum bt_value_status
bt_value_string_set(struct bt_value
*string_obj
,
991 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
992 struct bt_value_string
*typed_string_obj
=
993 BT_VALUE_TO_STRING(string_obj
);
995 if (!string_obj
|| !val
) {
996 BT_LOGW("Invalid parameter: value object or value is NULL: "
997 "value-addr=%p, raw-value-addr=%p",
999 ret
= BT_VALUE_STATUS_INVAL
;
1003 if (!bt_value_is_string(string_obj
)) {
1004 BT_LOGW("Invalid parameter: value is not a string value: addr=%p, "
1005 "type=%s", string_obj
,
1006 bt_value_type_string(string_obj
->type
));
1007 ret
= BT_VALUE_STATUS_INVAL
;
1011 if (string_obj
->is_frozen
) {
1012 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1014 ret
= BT_VALUE_STATUS_FROZEN
;
1018 g_string_assign(typed_string_obj
->gstr
, val
);
1019 BT_LOGV("Set string value's raw value: value-addr=%p, raw-value-addr=%p",
1026 int64_t bt_value_array_size(const struct bt_value
*array_obj
)
1029 struct bt_value_array
*typed_array_obj
=
1030 BT_VALUE_TO_ARRAY(array_obj
);
1033 BT_LOGW_STR("Invalid parameter: value object is NULL.");
1034 ret
= (int64_t) BT_VALUE_STATUS_INVAL
;
1038 if (!bt_value_is_array(array_obj
)) {
1039 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1040 "type=%s", array_obj
,
1041 bt_value_type_string(array_obj
->type
));
1042 ret
= BT_VALUE_STATUS_INVAL
;
1046 ret
= (int64_t) typed_array_obj
->garray
->len
;
1052 bt_bool
bt_value_array_is_empty(const struct bt_value
*array_obj
)
1054 return bt_value_array_size(array_obj
) == 0;
1057 struct bt_value
*bt_value_array_get(const struct bt_value
*array_obj
,
1060 struct bt_value
*ret
;
1061 struct bt_value_array
*typed_array_obj
=
1062 BT_VALUE_TO_ARRAY(array_obj
);
1065 BT_LOGW("Invalid parameter: value object is NULL: index=%" PRIu64
,
1071 if (!bt_value_is_array(array_obj
)) {
1072 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1073 "type=%s", array_obj
,
1074 bt_value_type_string(array_obj
->type
));
1079 if (index
>= typed_array_obj
->garray
->len
) {
1080 BT_LOGW("Invalid parameter: index is out of bounds: "
1081 "addr=%p, index=%" PRIu64
", size=%u",
1082 array_obj
, index
, typed_array_obj
->garray
->len
);
1087 ret
= g_ptr_array_index(typed_array_obj
->garray
, index
);
1094 enum bt_value_status
bt_value_array_append(struct bt_value
*array_obj
,
1095 struct bt_value
*element_obj
)
1097 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1098 struct bt_value_array
*typed_array_obj
=
1099 BT_VALUE_TO_ARRAY(array_obj
);
1101 if (!array_obj
|| !element_obj
) {
1102 BT_LOGW("Invalid parameter: array value or element value is NULL: "
1103 "array-value-addr=%p, element-value-addr=%p",
1104 array_obj
, element_obj
);
1105 ret
= BT_VALUE_STATUS_INVAL
;
1109 if (!bt_value_is_array(array_obj
)) {
1110 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1111 "type=%s", array_obj
,
1112 bt_value_type_string(array_obj
->type
));
1113 ret
= BT_VALUE_STATUS_INVAL
;
1117 if (array_obj
->is_frozen
) {
1118 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1120 ret
= BT_VALUE_STATUS_FROZEN
;
1124 g_ptr_array_add(typed_array_obj
->garray
, element_obj
);
1125 bt_get(element_obj
);
1126 BT_LOGV("Appended element to array value: array-value-addr=%p, "
1127 "element-value-addr=%p, new-size=%u",
1128 array_obj
, element_obj
, typed_array_obj
->garray
->len
);
1134 enum bt_value_status
bt_value_array_append_bool(struct bt_value
*array_obj
,
1137 enum bt_value_status ret
;
1138 struct bt_value
*bool_obj
= NULL
;
1140 bool_obj
= bt_value_bool_create_init(val
);
1141 ret
= bt_value_array_append(array_obj
, bool_obj
);
1146 enum bt_value_status
bt_value_array_append_integer(
1147 struct bt_value
*array_obj
, int64_t val
)
1149 enum bt_value_status ret
;
1150 struct bt_value
*integer_obj
= NULL
;
1152 integer_obj
= bt_value_integer_create_init(val
);
1153 ret
= bt_value_array_append(array_obj
, integer_obj
);
1154 bt_put(integer_obj
);
1158 enum bt_value_status
bt_value_array_append_float(struct bt_value
*array_obj
,
1161 enum bt_value_status ret
;
1162 struct bt_value
*float_obj
= NULL
;
1164 float_obj
= bt_value_float_create_init(val
);
1165 ret
= bt_value_array_append(array_obj
, float_obj
);
1170 enum bt_value_status
bt_value_array_append_string(struct bt_value
*array_obj
,
1173 enum bt_value_status ret
;
1174 struct bt_value
*string_obj
= NULL
;
1176 string_obj
= bt_value_string_create_init(val
);
1177 ret
= bt_value_array_append(array_obj
, string_obj
);
1182 enum bt_value_status
bt_value_array_append_empty_array(
1183 struct bt_value
*array_obj
)
1185 enum bt_value_status ret
;
1186 struct bt_value
*empty_array_obj
= NULL
;
1188 empty_array_obj
= bt_value_array_create();
1189 ret
= bt_value_array_append(array_obj
, empty_array_obj
);
1190 bt_put(empty_array_obj
);
1194 enum bt_value_status
bt_value_array_append_empty_map(struct bt_value
*array_obj
)
1196 enum bt_value_status ret
;
1197 struct bt_value
*map_obj
= NULL
;
1199 map_obj
= bt_value_map_create();
1200 ret
= bt_value_array_append(array_obj
, map_obj
);
1205 enum bt_value_status
bt_value_array_set(struct bt_value
*array_obj
,
1206 uint64_t index
, struct bt_value
*element_obj
)
1208 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1209 struct bt_value_array
*typed_array_obj
=
1210 BT_VALUE_TO_ARRAY(array_obj
);
1212 if (!array_obj
|| !element_obj
) {
1213 BT_LOGW("Invalid parameter: array value or element value is NULL: "
1214 "index=%" PRIu64
", array-value-addr=%p, element-value-addr=%p",
1215 index
, array_obj
, element_obj
);
1216 ret
= BT_VALUE_STATUS_INVAL
;
1220 if (!bt_value_is_array(array_obj
)) {
1221 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1222 "type=%s", array_obj
,
1223 bt_value_type_string(array_obj
->type
));
1224 ret
= BT_VALUE_STATUS_INVAL
;
1228 if (index
>= typed_array_obj
->garray
->len
) {
1229 BT_LOGW("Invalid parameter: index is out of bounds: "
1230 "addr=%p, index=%" PRIu64
", size=%u",
1231 array_obj
, index
, typed_array_obj
->garray
->len
);
1232 ret
= BT_VALUE_STATUS_INVAL
;
1236 if (array_obj
->is_frozen
) {
1237 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1239 ret
= BT_VALUE_STATUS_FROZEN
;
1243 bt_put(g_ptr_array_index(typed_array_obj
->garray
, index
));
1244 g_ptr_array_index(typed_array_obj
->garray
, index
) = element_obj
;
1245 bt_get(element_obj
);
1246 BT_LOGV("Set array value's element: array-value-addr=%p, "
1247 "index=%" PRIu64
", element-value-addr=%p",
1248 array_obj
, index
, element_obj
);
1254 int64_t bt_value_map_size(const struct bt_value
*map_obj
)
1257 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1260 BT_LOGW_STR("Invalid parameter: value object is NULL.");
1261 ret
= (int64_t) BT_VALUE_STATUS_INVAL
;
1265 if (!bt_value_is_map(map_obj
)) {
1266 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1268 bt_value_type_string(map_obj
->type
));
1269 ret
= (int64_t) BT_VALUE_STATUS_INVAL
;
1273 ret
= (int64_t) g_hash_table_size(typed_map_obj
->ght
);
1279 bt_bool
bt_value_map_is_empty(const struct bt_value
*map_obj
)
1281 return bt_value_map_size(map_obj
) == 0;
1284 struct bt_value
*bt_value_map_get(const struct bt_value
*map_obj
,
1288 struct bt_value
*ret
;
1289 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1291 if (!map_obj
|| !key
) {
1292 BT_LOGW("Invalid parameter: value object or key is NULL: "
1293 "value-addr=%p, key-addr=%p", map_obj
, key
);
1298 if (!bt_value_is_map(map_obj
)) {
1299 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1301 bt_value_type_string(map_obj
->type
));
1306 quark
= g_quark_from_string(key
);
1307 ret
= g_hash_table_lookup(typed_map_obj
->ght
, GUINT_TO_POINTER(quark
));
1316 bt_bool
bt_value_map_has_key(const struct bt_value
*map_obj
, const char *key
)
1320 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1322 if (!map_obj
|| !key
) {
1323 BT_LOGW("Invalid parameter: value object or key is NULL: "
1324 "value-addr=%p, key-addr=%p", map_obj
, key
);
1329 if (!bt_value_is_map(map_obj
)) {
1330 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1332 bt_value_type_string(map_obj
->type
));
1337 quark
= g_quark_from_string(key
);
1338 ret
= bt_g_hash_table_contains(typed_map_obj
->ght
,
1339 GUINT_TO_POINTER(quark
));
1345 enum bt_value_status
bt_value_map_insert(struct bt_value
*map_obj
,
1346 const char *key
, struct bt_value
*element_obj
)
1349 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1350 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1352 if (!map_obj
|| !key
|| !element_obj
) {
1353 BT_LOGW("Invalid parameter: map value, key, or element value is NULL: "
1354 "map-value-addr=%p, key-addr=%p, element-value-addr=%p",
1355 map_obj
, key
, element_obj
);
1356 ret
= BT_VALUE_STATUS_INVAL
;
1360 if (!bt_value_is_map(map_obj
)) {
1361 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1363 bt_value_type_string(map_obj
->type
));
1364 ret
= BT_VALUE_STATUS_INVAL
;
1368 if (map_obj
->is_frozen
) {
1369 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1371 ret
= BT_VALUE_STATUS_FROZEN
;
1375 quark
= g_quark_from_string(key
);
1376 g_hash_table_insert(typed_map_obj
->ght
,
1377 GUINT_TO_POINTER(quark
), element_obj
);
1378 bt_get(element_obj
);
1379 BT_LOGV("Inserted value into map value: map-value-addr=%p, "
1380 "key=\"%s\", element-value-addr=%p",
1381 map_obj
, key
, element_obj
);
1387 enum bt_value_status
bt_value_map_insert_bool(struct bt_value
*map_obj
,
1388 const char *key
, bt_bool val
)
1390 enum bt_value_status ret
;
1391 struct bt_value
*bool_obj
= NULL
;
1393 bool_obj
= bt_value_bool_create_init(val
);
1394 ret
= bt_value_map_insert(map_obj
, key
, bool_obj
);
1399 enum bt_value_status
bt_value_map_insert_integer(struct bt_value
*map_obj
,
1400 const char *key
, int64_t val
)
1402 enum bt_value_status ret
;
1403 struct bt_value
*integer_obj
= NULL
;
1405 integer_obj
= bt_value_integer_create_init(val
);
1406 ret
= bt_value_map_insert(map_obj
, key
, integer_obj
);
1407 bt_put(integer_obj
);
1411 enum bt_value_status
bt_value_map_insert_float(struct bt_value
*map_obj
,
1412 const char *key
, double val
)
1414 enum bt_value_status ret
;
1415 struct bt_value
*float_obj
= NULL
;
1417 float_obj
= bt_value_float_create_init(val
);
1418 ret
= bt_value_map_insert(map_obj
, key
, float_obj
);
1423 enum bt_value_status
bt_value_map_insert_string(struct bt_value
*map_obj
,
1424 const char *key
, const char *val
)
1426 enum bt_value_status ret
;
1427 struct bt_value
*string_obj
= NULL
;
1429 string_obj
= bt_value_string_create_init(val
);
1430 ret
= bt_value_map_insert(map_obj
, key
, string_obj
);
1435 enum bt_value_status
bt_value_map_insert_empty_array(struct bt_value
*map_obj
,
1438 enum bt_value_status ret
;
1439 struct bt_value
*array_obj
= NULL
;
1441 array_obj
= bt_value_array_create();
1442 ret
= bt_value_map_insert(map_obj
, key
, array_obj
);
1447 enum bt_value_status
bt_value_map_insert_empty_map(struct bt_value
*map_obj
,
1450 enum bt_value_status ret
;
1451 struct bt_value
*empty_map_obj
= NULL
;
1453 empty_map_obj
= bt_value_map_create();
1454 ret
= bt_value_map_insert(map_obj
, key
, empty_map_obj
);
1455 bt_put(empty_map_obj
);
1459 enum bt_value_status
bt_value_map_foreach(const struct bt_value
*map_obj
,
1460 bt_value_map_foreach_cb cb
, void *data
)
1462 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1463 gpointer key
, element_obj
;
1464 GHashTableIter iter
;
1465 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1467 if (!map_obj
|| !cb
) {
1468 BT_LOGW("Invalid parameter: map value or callback is NULL: "
1469 "value-addr=%p, cb-addr=%p", map_obj
, cb
);
1470 ret
= BT_VALUE_STATUS_INVAL
;
1474 if (!bt_value_is_map(map_obj
)) {
1475 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1477 bt_value_type_string(map_obj
->type
));
1478 ret
= BT_VALUE_STATUS_INVAL
;
1482 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
1484 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
1485 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
1487 if (!cb(key_str
, element_obj
, data
)) {
1488 BT_LOGV("User cancelled the loop: key=\"%s\", "
1489 "value-addr=%p, data=%p",
1490 key_str
, element_obj
, data
);
1491 ret
= BT_VALUE_STATUS_CANCELLED
;
1500 struct extend_map_element_data
{
1501 struct bt_value
*extended_obj
;
1506 bt_bool
extend_map_element(const char *key
,
1507 struct bt_value
*extension_obj_elem
, void *data
)
1509 bt_bool ret
= BT_TRUE
;
1511 struct extend_map_element_data
*extend_data
= data
;
1513 /* Copy object which is to replace the current one */
1514 struct bt_value
*extension_obj_elem_copy
=
1515 bt_value_copy(extension_obj_elem
);
1517 /* Replace in extended object */
1518 if (bt_value_map_insert(extend_data
->extended_obj
, key
,
1519 extension_obj_elem_copy
)) {
1520 BT_LOGE("Cannot replace value in extended value: key=\"%s\", "
1521 "extended-value-addr=%p, element-value-addr=%p",
1522 key
, extend_data
->extended_obj
,
1523 extension_obj_elem_copy
);
1531 extend_data
->got_error
= BT_TRUE
;
1534 BT_PUT(extension_obj_elem_copy
);
1538 struct bt_value
*bt_value_map_extend(struct bt_value
*base_map_obj
,
1539 struct bt_value
*extension_obj
)
1541 struct bt_value
*extended_obj
= NULL
;
1542 struct extend_map_element_data extend_data
= { 0 };
1544 if (!base_map_obj
|| !extension_obj
) {
1545 BT_LOGW("Invalid parameter: base value or extension value is NULL: "
1546 "base-value-addr=%p, extension-value-addr=%p",
1547 base_map_obj
, extension_obj
);
1551 if (!bt_value_is_map(base_map_obj
)) {
1552 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1553 "type=%s", base_map_obj
,
1554 bt_value_type_string(base_map_obj
->type
));
1558 if (!bt_value_is_map(extension_obj
)) {
1559 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1560 "type=%s", extension_obj
,
1561 bt_value_type_string(extension_obj
->type
));
1565 BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p",
1566 base_map_obj
, extension_obj
);
1568 /* Create copy of base map object to start with */
1569 extended_obj
= bt_value_copy(base_map_obj
);
1570 if (!extended_obj
) {
1571 BT_LOGE("Cannot copy base value: base-value-addr=%p",
1577 * For each key in the extension map object, replace this key
1578 * in the copied map object.
1580 extend_data
.extended_obj
= extended_obj
;
1582 if (bt_value_map_foreach(extension_obj
, extend_map_element
,
1584 BT_LOGE("Cannot iterate on the extension object's elements: "
1585 "extension-value-addr=%p", extension_obj
);
1589 if (extend_data
.got_error
) {
1590 BT_LOGE("Failed to successfully iterate on the extension object's elements: "
1591 "extension-value-addr=%p", extension_obj
);
1595 BT_LOGD("Extended map value: extended-value-addr=%p",
1600 BT_PUT(extended_obj
);
1603 return extended_obj
;
1606 struct bt_value
*bt_value_copy(const struct bt_value
*object
)
1608 struct bt_value
*copy_obj
= NULL
;
1611 BT_LOGW_STR("Invalid parameter: value object is NULL.");
1615 BT_LOGD("Copying value object: addr=%p", object
);
1616 copy_obj
= copy_funcs
[object
->type
](object
);
1618 BT_LOGD("Copied value object: copy-value-addr=%p",
1621 BT_LOGE_STR("Failed to copy value object.");
1628 bt_bool
bt_value_compare(const struct bt_value
*object_a
,
1629 const struct bt_value
*object_b
)
1631 bt_bool ret
= BT_FALSE
;
1633 if (!object_a
|| !object_b
) {
1634 BT_LOGW("Invalid parameter: value A or value B is NULL: "
1635 "value-a-addr=%p, value-b-addr=%p",
1636 object_a
, object_b
);
1640 if (object_a
->type
!= object_b
->type
) {
1641 BT_LOGV("Values are different: type mismatch: "
1642 "value-a-addr=%p, value-b-addr=%p, "
1643 "value-a-type=%s, value-b-type=%s",
1645 bt_value_type_string(object_a
->type
),
1646 bt_value_type_string(object_b
->type
));
1650 ret
= compare_funcs
[object_a
->type
](object_a
, object_b
);