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
)
569 BT_LOGW_STR("Invalid parameter: value object is NULL.");
570 return BT_VALUE_TYPE_UNKNOWN
;
577 struct bt_value
bt_value_create_base(enum bt_value_type type
)
579 struct bt_value base
;
582 base
.is_frozen
= BT_FALSE
;
583 bt_object_init(&base
, bt_value_destroy
);
587 struct bt_value
*bt_value_bool_create_init(bt_bool val
)
589 struct bt_value_bool
*bool_obj
;
591 BT_LOGD("Creating boolean value object: val=%d", val
);
592 bool_obj
= g_new0(struct bt_value_bool
, 1);
595 BT_LOGE_STR("Failed to allocate one boolean value object.");
599 bool_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_BOOL
);
600 bool_obj
->value
= val
;
601 BT_LOGD("Created boolean value object: addr=%p", bool_obj
);
604 return BT_VALUE_FROM_CONCRETE(bool_obj
);
607 struct bt_value
*bt_value_bool_create(void)
609 return bt_value_bool_create_init(BT_FALSE
);
612 struct bt_value
*bt_value_integer_create_init(int64_t val
)
614 struct bt_value_integer
*integer_obj
;
616 BT_LOGD("Creating integer value object: val=%" PRId64
, val
);
617 integer_obj
= g_new0(struct bt_value_integer
, 1);
620 BT_LOGE_STR("Failed to allocate one integer value object.");
624 integer_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_INTEGER
);
625 integer_obj
->value
= val
;
626 BT_LOGD("Created integer value object: addr=%p",
630 return BT_VALUE_FROM_CONCRETE(integer_obj
);
633 struct bt_value
*bt_value_integer_create(void)
635 return bt_value_integer_create_init(0);
638 struct bt_value
*bt_value_float_create_init(double val
)
640 struct bt_value_float
*float_obj
;
642 BT_LOGD("Creating floating point number value object: val=%f", val
);
643 float_obj
= g_new0(struct bt_value_float
, 1);
646 BT_LOGE_STR("Failed to allocate one floating point number value object.");
650 float_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_FLOAT
);
651 float_obj
->value
= val
;
652 BT_LOGD("Created floating point number value object: addr=%p",
656 return BT_VALUE_FROM_CONCRETE(float_obj
);
659 struct bt_value
*bt_value_float_create(void)
661 return bt_value_float_create_init(0.);
664 struct bt_value
*bt_value_string_create_init(const char *val
)
666 struct bt_value_string
*string_obj
= NULL
;
669 BT_LOGW_STR("Invalid parameter: value is NULL.");
673 BT_LOGD("Creating string value object: val-len=%lu", strlen(val
));
674 string_obj
= g_new0(struct bt_value_string
, 1);
677 BT_LOGE_STR("Failed to allocate one string object.");
681 string_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_STRING
);
682 string_obj
->gstr
= g_string_new(val
);
684 if (!string_obj
->gstr
) {
685 BT_LOGE_STR("Failed to allocate a GString.");
691 BT_LOGD("Created string value object: addr=%p",
695 return BT_VALUE_FROM_CONCRETE(string_obj
);
698 struct bt_value
*bt_value_string_create(void)
700 return bt_value_string_create_init("");
703 struct bt_value
*bt_value_array_create(void)
705 struct bt_value_array
*array_obj
;
707 BT_LOGD_STR("Creating empty array value object.");
708 array_obj
= g_new0(struct bt_value_array
, 1);
711 BT_LOGE_STR("Failed to allocate one array object.");
715 array_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_ARRAY
);
716 array_obj
->garray
= bt_g_ptr_array_new_full(0,
717 (GDestroyNotify
) bt_put
);
719 if (!array_obj
->garray
) {
720 BT_LOGE_STR("Failed to allocate a GPtrArray.");
726 BT_LOGD("Created array value object: addr=%p",
730 return BT_VALUE_FROM_CONCRETE(array_obj
);
733 struct bt_value
*bt_value_map_create(void)
735 struct bt_value_map
*map_obj
;
737 BT_LOGD_STR("Creating empty map value object.");
738 map_obj
= g_new0(struct bt_value_map
, 1);
741 BT_LOGE_STR("Failed to allocate one map object.");
745 map_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_MAP
);
746 map_obj
->ght
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
747 NULL
, (GDestroyNotify
) bt_put
);
750 BT_LOGE_STR("Failed to allocate a GHashTable.");
756 BT_LOGD("Created map value object: addr=%p",
760 return BT_VALUE_FROM_CONCRETE(map_obj
);
763 enum bt_value_status
bt_value_bool_get(const struct bt_value
*bool_obj
,
766 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
767 struct bt_value_bool
*typed_bool_obj
= BT_VALUE_TO_BOOL(bool_obj
);
769 if (!bool_obj
|| !val
) {
770 BT_LOGW("Invalid parameter: value object or value is NULL: "
771 "value-addr=%p, raw-value-addr=%p",
773 ret
= BT_VALUE_STATUS_INVAL
;
777 if (!bt_value_is_bool(bool_obj
)) {
778 BT_LOGW("Invalid parameter: value is not a boolean value: addr=%p, "
780 bt_value_type_string(bool_obj
->type
));
781 ret
= BT_VALUE_STATUS_INVAL
;
785 *val
= typed_bool_obj
->value
;
791 enum bt_value_status
bt_value_bool_set(struct bt_value
*bool_obj
, bt_bool val
)
793 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
794 struct bt_value_bool
*typed_bool_obj
= BT_VALUE_TO_BOOL(bool_obj
);
797 BT_LOGW_STR("Invalid parameter: value object is NULL.");
798 ret
= BT_VALUE_STATUS_INVAL
;
802 if (!bt_value_is_bool(bool_obj
)) {
803 BT_LOGW("Invalid parameter: value is not a boolean value: addr=%p, "
805 bt_value_type_string(bool_obj
->type
));
806 ret
= BT_VALUE_STATUS_INVAL
;
810 if (bool_obj
->is_frozen
) {
811 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
813 ret
= BT_VALUE_STATUS_FROZEN
;
817 typed_bool_obj
->value
= val
;
818 BT_LOGV("Set boolean value's raw value: value-addr=%p, value=%d",
825 enum bt_value_status
bt_value_integer_get(const struct bt_value
*integer_obj
,
828 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
829 struct bt_value_integer
*typed_integer_obj
=
830 BT_VALUE_TO_INTEGER(integer_obj
);
832 if (!integer_obj
|| !val
) {
833 BT_LOGW("Invalid parameter: value object or value is NULL: "
834 "value-addr=%p, raw-value-addr=%p",
836 ret
= BT_VALUE_STATUS_INVAL
;
840 if (!bt_value_is_integer(integer_obj
)) {
841 BT_LOGW("Invalid parameter: value is not an integer value: addr=%p, "
842 "type=%s", integer_obj
,
843 bt_value_type_string(integer_obj
->type
));
844 ret
= BT_VALUE_STATUS_INVAL
;
848 *val
= typed_integer_obj
->value
;
854 enum bt_value_status
bt_value_integer_set(struct bt_value
*integer_obj
,
857 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
858 struct bt_value_integer
*typed_integer_obj
=
859 BT_VALUE_TO_INTEGER(integer_obj
);
862 BT_LOGW_STR("Invalid parameter: value object is NULL.");
863 ret
= BT_VALUE_STATUS_INVAL
;
867 if (!bt_value_is_integer(integer_obj
)) {
868 BT_LOGW("Invalid parameter: value is not an integer value: addr=%p, "
869 "type=%s", integer_obj
,
870 bt_value_type_string(integer_obj
->type
));
871 ret
= BT_VALUE_STATUS_INVAL
;
875 if (integer_obj
->is_frozen
) {
876 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
878 ret
= BT_VALUE_STATUS_FROZEN
;
882 typed_integer_obj
->value
= val
;
883 BT_LOGV("Set integer value's raw value: value-addr=%p, value=%" PRId64
,
890 enum bt_value_status
bt_value_float_get(const struct bt_value
*float_obj
,
893 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
894 struct bt_value_float
*typed_float_obj
=
895 BT_VALUE_TO_FLOAT(float_obj
);
897 if (!float_obj
|| !val
) {
898 BT_LOGW("Invalid parameter: value object or value is NULL: "
899 "value-addr=%p, raw-value-addr=%p",
901 ret
= BT_VALUE_STATUS_INVAL
;
905 if (!bt_value_is_float(float_obj
)) {
906 BT_LOGW("Invalid parameter: value is not a floating point number value: addr=%p, "
907 "type=%s", float_obj
,
908 bt_value_type_string(float_obj
->type
));
909 ret
= BT_VALUE_STATUS_INVAL
;
913 *val
= typed_float_obj
->value
;
919 enum bt_value_status
bt_value_float_set(struct bt_value
*float_obj
,
922 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
923 struct bt_value_float
*typed_float_obj
=
924 BT_VALUE_TO_FLOAT(float_obj
);
927 BT_LOGW_STR("Invalid parameter: value object is NULL.");
928 ret
= BT_VALUE_STATUS_INVAL
;
932 if (!bt_value_is_float(float_obj
)) {
933 BT_LOGW("Invalid parameter: value is not a floating point number value: addr=%p, "
934 "type=%s", float_obj
,
935 bt_value_type_string(float_obj
->type
));
936 ret
= BT_VALUE_STATUS_INVAL
;
940 if (float_obj
->is_frozen
) {
941 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
943 ret
= BT_VALUE_STATUS_FROZEN
;
947 typed_float_obj
->value
= val
;
948 BT_LOGV("Set floating point number value's raw value: value-addr=%p, value=%f",
955 enum bt_value_status
bt_value_string_get(const struct bt_value
*string_obj
,
958 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
959 struct bt_value_string
*typed_string_obj
=
960 BT_VALUE_TO_STRING(string_obj
);
962 if (!string_obj
|| !val
) {
963 BT_LOGW("Invalid parameter: value object or value is NULL: "
964 "value-addr=%p, raw-value-addr=%p",
966 ret
= BT_VALUE_STATUS_INVAL
;
970 if (!bt_value_is_string(string_obj
)) {
971 BT_LOGW("Invalid parameter: value is not a string value: addr=%p, "
972 "type=%s", string_obj
,
973 bt_value_type_string(string_obj
->type
));
974 ret
= BT_VALUE_STATUS_INVAL
;
978 *val
= typed_string_obj
->gstr
->str
;
984 enum bt_value_status
bt_value_string_set(struct bt_value
*string_obj
,
987 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
988 struct bt_value_string
*typed_string_obj
=
989 BT_VALUE_TO_STRING(string_obj
);
991 if (!string_obj
|| !val
) {
992 BT_LOGW("Invalid parameter: value object or value is NULL: "
993 "value-addr=%p, raw-value-addr=%p",
995 ret
= BT_VALUE_STATUS_INVAL
;
999 if (!bt_value_is_string(string_obj
)) {
1000 BT_LOGW("Invalid parameter: value is not a string value: addr=%p, "
1001 "type=%s", string_obj
,
1002 bt_value_type_string(string_obj
->type
));
1003 ret
= BT_VALUE_STATUS_INVAL
;
1007 if (string_obj
->is_frozen
) {
1008 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1010 ret
= BT_VALUE_STATUS_FROZEN
;
1014 g_string_assign(typed_string_obj
->gstr
, val
);
1015 BT_LOGV("Set string value's raw value: value-addr=%p, raw-value-addr=%p",
1022 int64_t bt_value_array_size(const struct bt_value
*array_obj
)
1025 struct bt_value_array
*typed_array_obj
=
1026 BT_VALUE_TO_ARRAY(array_obj
);
1029 BT_LOGW_STR("Invalid parameter: value object is NULL.");
1030 ret
= (int64_t) BT_VALUE_STATUS_INVAL
;
1034 if (!bt_value_is_array(array_obj
)) {
1035 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1036 "type=%s", array_obj
,
1037 bt_value_type_string(array_obj
->type
));
1038 ret
= BT_VALUE_STATUS_INVAL
;
1042 ret
= (int64_t) typed_array_obj
->garray
->len
;
1048 bt_bool
bt_value_array_is_empty(const struct bt_value
*array_obj
)
1050 return bt_value_array_size(array_obj
) == 0;
1053 struct bt_value
*bt_value_array_get(const struct bt_value
*array_obj
,
1056 struct bt_value
*ret
;
1057 struct bt_value_array
*typed_array_obj
=
1058 BT_VALUE_TO_ARRAY(array_obj
);
1061 BT_LOGW("Invalid parameter: value object is NULL: index=%" PRIu64
,
1067 if (!bt_value_is_array(array_obj
)) {
1068 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1069 "type=%s", array_obj
,
1070 bt_value_type_string(array_obj
->type
));
1075 if (index
>= typed_array_obj
->garray
->len
) {
1076 BT_LOGW("Invalid parameter: index is out of bounds: "
1077 "addr=%p, index=%" PRIu64
", size=%u",
1078 array_obj
, index
, typed_array_obj
->garray
->len
);
1083 ret
= g_ptr_array_index(typed_array_obj
->garray
, index
);
1090 enum bt_value_status
bt_value_array_append(struct bt_value
*array_obj
,
1091 struct bt_value
*element_obj
)
1093 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1094 struct bt_value_array
*typed_array_obj
=
1095 BT_VALUE_TO_ARRAY(array_obj
);
1097 if (!array_obj
|| !element_obj
) {
1098 BT_LOGW("Invalid parameter: array value or element value is NULL: "
1099 "array-value-addr=%p, element-value-addr=%p",
1100 array_obj
, element_obj
);
1101 ret
= BT_VALUE_STATUS_INVAL
;
1105 if (!bt_value_is_array(array_obj
)) {
1106 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1107 "type=%s", array_obj
,
1108 bt_value_type_string(array_obj
->type
));
1109 ret
= BT_VALUE_STATUS_INVAL
;
1113 if (array_obj
->is_frozen
) {
1114 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1116 ret
= BT_VALUE_STATUS_FROZEN
;
1120 g_ptr_array_add(typed_array_obj
->garray
, element_obj
);
1121 bt_get(element_obj
);
1122 BT_LOGV("Appended element to array value: array-value-addr=%p, "
1123 "element-value-addr=%p, new-size=%u",
1124 array_obj
, element_obj
, typed_array_obj
->garray
->len
);
1130 enum bt_value_status
bt_value_array_append_bool(struct bt_value
*array_obj
,
1133 enum bt_value_status ret
;
1134 struct bt_value
*bool_obj
= NULL
;
1136 bool_obj
= bt_value_bool_create_init(val
);
1137 ret
= bt_value_array_append(array_obj
, bool_obj
);
1142 enum bt_value_status
bt_value_array_append_integer(
1143 struct bt_value
*array_obj
, int64_t val
)
1145 enum bt_value_status ret
;
1146 struct bt_value
*integer_obj
= NULL
;
1148 integer_obj
= bt_value_integer_create_init(val
);
1149 ret
= bt_value_array_append(array_obj
, integer_obj
);
1150 bt_put(integer_obj
);
1154 enum bt_value_status
bt_value_array_append_float(struct bt_value
*array_obj
,
1157 enum bt_value_status ret
;
1158 struct bt_value
*float_obj
= NULL
;
1160 float_obj
= bt_value_float_create_init(val
);
1161 ret
= bt_value_array_append(array_obj
, float_obj
);
1166 enum bt_value_status
bt_value_array_append_string(struct bt_value
*array_obj
,
1169 enum bt_value_status ret
;
1170 struct bt_value
*string_obj
= NULL
;
1172 string_obj
= bt_value_string_create_init(val
);
1173 ret
= bt_value_array_append(array_obj
, string_obj
);
1178 enum bt_value_status
bt_value_array_append_empty_array(
1179 struct bt_value
*array_obj
)
1181 enum bt_value_status ret
;
1182 struct bt_value
*empty_array_obj
= NULL
;
1184 empty_array_obj
= bt_value_array_create();
1185 ret
= bt_value_array_append(array_obj
, empty_array_obj
);
1186 bt_put(empty_array_obj
);
1190 enum bt_value_status
bt_value_array_append_empty_map(struct bt_value
*array_obj
)
1192 enum bt_value_status ret
;
1193 struct bt_value
*map_obj
= NULL
;
1195 map_obj
= bt_value_map_create();
1196 ret
= bt_value_array_append(array_obj
, map_obj
);
1201 enum bt_value_status
bt_value_array_set(struct bt_value
*array_obj
,
1202 uint64_t index
, struct bt_value
*element_obj
)
1204 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1205 struct bt_value_array
*typed_array_obj
=
1206 BT_VALUE_TO_ARRAY(array_obj
);
1208 if (!array_obj
|| !element_obj
) {
1209 BT_LOGW("Invalid parameter: array value or element value is NULL: "
1210 "index=%" PRIu64
", array-value-addr=%p, element-value-addr=%p",
1211 index
, array_obj
, element_obj
);
1212 ret
= BT_VALUE_STATUS_INVAL
;
1216 if (!bt_value_is_array(array_obj
)) {
1217 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1218 "type=%s", array_obj
,
1219 bt_value_type_string(array_obj
->type
));
1220 ret
= BT_VALUE_STATUS_INVAL
;
1224 if (index
>= typed_array_obj
->garray
->len
) {
1225 BT_LOGW("Invalid parameter: index is out of bounds: "
1226 "addr=%p, index=%" PRIu64
", size=%u",
1227 array_obj
, index
, typed_array_obj
->garray
->len
);
1228 ret
= BT_VALUE_STATUS_INVAL
;
1232 if (array_obj
->is_frozen
) {
1233 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1235 ret
= BT_VALUE_STATUS_FROZEN
;
1239 bt_put(g_ptr_array_index(typed_array_obj
->garray
, index
));
1240 g_ptr_array_index(typed_array_obj
->garray
, index
) = element_obj
;
1241 bt_get(element_obj
);
1242 BT_LOGV("Set array value's element: array-value-addr=%p, "
1243 "index=%" PRIu64
", element-value-addr=%p",
1244 array_obj
, index
, element_obj
);
1250 int64_t bt_value_map_size(const struct bt_value
*map_obj
)
1253 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1256 BT_LOGW_STR("Invalid parameter: value object is NULL.");
1257 ret
= (int64_t) BT_VALUE_STATUS_INVAL
;
1261 if (!bt_value_is_map(map_obj
)) {
1262 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1264 bt_value_type_string(map_obj
->type
));
1265 ret
= (int64_t) BT_VALUE_STATUS_INVAL
;
1269 ret
= (int64_t) g_hash_table_size(typed_map_obj
->ght
);
1275 bt_bool
bt_value_map_is_empty(const struct bt_value
*map_obj
)
1277 return bt_value_map_size(map_obj
) == 0;
1280 struct bt_value
*bt_value_map_get(const struct bt_value
*map_obj
,
1284 struct bt_value
*ret
;
1285 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1287 if (!map_obj
|| !key
) {
1288 BT_LOGW("Invalid parameter: value object or key is NULL: "
1289 "value-addr=%p, key-addr=%p", map_obj
, key
);
1294 if (!bt_value_is_map(map_obj
)) {
1295 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1297 bt_value_type_string(map_obj
->type
));
1302 quark
= g_quark_from_string(key
);
1303 ret
= g_hash_table_lookup(typed_map_obj
->ght
, GUINT_TO_POINTER(quark
));
1312 bt_bool
bt_value_map_has_key(const struct bt_value
*map_obj
, const char *key
)
1316 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1318 if (!map_obj
|| !key
) {
1319 BT_LOGW("Invalid parameter: value object or key is NULL: "
1320 "value-addr=%p, key-addr=%p", map_obj
, key
);
1325 if (!bt_value_is_map(map_obj
)) {
1326 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1328 bt_value_type_string(map_obj
->type
));
1333 quark
= g_quark_from_string(key
);
1334 ret
= bt_g_hash_table_contains(typed_map_obj
->ght
,
1335 GUINT_TO_POINTER(quark
));
1341 enum bt_value_status
bt_value_map_insert(struct bt_value
*map_obj
,
1342 const char *key
, struct bt_value
*element_obj
)
1345 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1346 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1348 if (!map_obj
|| !key
|| !element_obj
) {
1349 BT_LOGW("Invalid parameter: map value, key, or element value is NULL: "
1350 "map-value-addr=%p, key-addr=%p, element-value-addr=%p",
1351 map_obj
, key
, element_obj
);
1352 ret
= BT_VALUE_STATUS_INVAL
;
1356 if (!bt_value_is_map(map_obj
)) {
1357 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1359 bt_value_type_string(map_obj
->type
));
1360 ret
= BT_VALUE_STATUS_INVAL
;
1364 if (map_obj
->is_frozen
) {
1365 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1367 ret
= BT_VALUE_STATUS_FROZEN
;
1371 quark
= g_quark_from_string(key
);
1372 g_hash_table_insert(typed_map_obj
->ght
,
1373 GUINT_TO_POINTER(quark
), element_obj
);
1374 bt_get(element_obj
);
1375 BT_LOGV("Inserted value into map value: map-value-addr=%p, "
1376 "key=\"%s\", element-value-addr=%p",
1377 map_obj
, key
, element_obj
);
1383 enum bt_value_status
bt_value_map_insert_bool(struct bt_value
*map_obj
,
1384 const char *key
, bt_bool val
)
1386 enum bt_value_status ret
;
1387 struct bt_value
*bool_obj
= NULL
;
1389 bool_obj
= bt_value_bool_create_init(val
);
1390 ret
= bt_value_map_insert(map_obj
, key
, bool_obj
);
1395 enum bt_value_status
bt_value_map_insert_integer(struct bt_value
*map_obj
,
1396 const char *key
, int64_t val
)
1398 enum bt_value_status ret
;
1399 struct bt_value
*integer_obj
= NULL
;
1401 integer_obj
= bt_value_integer_create_init(val
);
1402 ret
= bt_value_map_insert(map_obj
, key
, integer_obj
);
1403 bt_put(integer_obj
);
1407 enum bt_value_status
bt_value_map_insert_float(struct bt_value
*map_obj
,
1408 const char *key
, double val
)
1410 enum bt_value_status ret
;
1411 struct bt_value
*float_obj
= NULL
;
1413 float_obj
= bt_value_float_create_init(val
);
1414 ret
= bt_value_map_insert(map_obj
, key
, float_obj
);
1419 enum bt_value_status
bt_value_map_insert_string(struct bt_value
*map_obj
,
1420 const char *key
, const char *val
)
1422 enum bt_value_status ret
;
1423 struct bt_value
*string_obj
= NULL
;
1425 string_obj
= bt_value_string_create_init(val
);
1426 ret
= bt_value_map_insert(map_obj
, key
, string_obj
);
1431 enum bt_value_status
bt_value_map_insert_empty_array(struct bt_value
*map_obj
,
1434 enum bt_value_status ret
;
1435 struct bt_value
*array_obj
= NULL
;
1437 array_obj
= bt_value_array_create();
1438 ret
= bt_value_map_insert(map_obj
, key
, array_obj
);
1443 enum bt_value_status
bt_value_map_insert_empty_map(struct bt_value
*map_obj
,
1446 enum bt_value_status ret
;
1447 struct bt_value
*empty_map_obj
= NULL
;
1449 empty_map_obj
= bt_value_map_create();
1450 ret
= bt_value_map_insert(map_obj
, key
, empty_map_obj
);
1451 bt_put(empty_map_obj
);
1455 enum bt_value_status
bt_value_map_foreach(const struct bt_value
*map_obj
,
1456 bt_value_map_foreach_cb cb
, void *data
)
1458 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1459 gpointer key
, element_obj
;
1460 GHashTableIter iter
;
1461 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1463 if (!map_obj
|| !cb
) {
1464 BT_LOGW("Invalid parameter: map value or callback is NULL: "
1465 "value-addr=%p, cb-addr=%p", map_obj
, cb
);
1466 ret
= BT_VALUE_STATUS_INVAL
;
1470 if (!bt_value_is_map(map_obj
)) {
1471 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1473 bt_value_type_string(map_obj
->type
));
1474 ret
= BT_VALUE_STATUS_INVAL
;
1478 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
1480 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
1481 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
1483 if (!cb(key_str
, element_obj
, data
)) {
1484 BT_LOGV("User cancelled the loop: key=\"%s\", "
1485 "value-addr=%p, data=%p",
1486 key_str
, element_obj
, data
);
1487 ret
= BT_VALUE_STATUS_CANCELLED
;
1496 struct extend_map_element_data
{
1497 struct bt_value
*extended_obj
;
1502 bt_bool
extend_map_element(const char *key
,
1503 struct bt_value
*extension_obj_elem
, void *data
)
1505 bt_bool ret
= BT_TRUE
;
1507 struct extend_map_element_data
*extend_data
= data
;
1509 /* Copy object which is to replace the current one */
1510 struct bt_value
*extension_obj_elem_copy
=
1511 bt_value_copy(extension_obj_elem
);
1513 /* Replace in extended object */
1514 if (bt_value_map_insert(extend_data
->extended_obj
, key
,
1515 extension_obj_elem_copy
)) {
1516 BT_LOGE("Cannot replace value in extended value: key=\"%s\", "
1517 "extended-value-addr=%p, element-value-addr=%p",
1518 key
, extend_data
->extended_obj
,
1519 extension_obj_elem_copy
);
1527 extend_data
->got_error
= BT_TRUE
;
1530 BT_PUT(extension_obj_elem_copy
);
1534 struct bt_value
*bt_value_map_extend(struct bt_value
*base_map_obj
,
1535 struct bt_value
*extension_obj
)
1537 struct bt_value
*extended_obj
= NULL
;
1538 struct extend_map_element_data extend_data
= { 0 };
1540 if (!base_map_obj
|| !extension_obj
) {
1541 BT_LOGW("Invalid parameter: base value or extension value is NULL: "
1542 "base-value-addr=%p, extension-value-addr=%p",
1543 base_map_obj
, extension_obj
);
1547 if (!bt_value_is_map(base_map_obj
)) {
1548 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1549 "type=%s", base_map_obj
,
1550 bt_value_type_string(base_map_obj
->type
));
1554 if (!bt_value_is_map(extension_obj
)) {
1555 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1556 "type=%s", extension_obj
,
1557 bt_value_type_string(extension_obj
->type
));
1561 BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p",
1562 base_map_obj
, extension_obj
);
1564 /* Create copy of base map object to start with */
1565 extended_obj
= bt_value_copy(base_map_obj
);
1566 if (!extended_obj
) {
1567 BT_LOGE("Cannot copy base value: base-value-addr=%p",
1573 * For each key in the extension map object, replace this key
1574 * in the copied map object.
1576 extend_data
.extended_obj
= extended_obj
;
1578 if (bt_value_map_foreach(extension_obj
, extend_map_element
,
1580 BT_LOGE("Cannot iterate on the extension object's elements: "
1581 "extension-value-addr=%p", extension_obj
);
1585 if (extend_data
.got_error
) {
1586 BT_LOGE("Failed to successfully iterate on the extension object's elements: "
1587 "extension-value-addr=%p", extension_obj
);
1591 BT_LOGD("Extended map value: extended-value-addr=%p",
1596 BT_PUT(extended_obj
);
1599 return extended_obj
;
1602 struct bt_value
*bt_value_copy(const struct bt_value
*object
)
1604 struct bt_value
*copy_obj
= NULL
;
1607 BT_LOGW_STR("Invalid parameter: value object is NULL.");
1611 BT_LOGD("Copying value object: addr=%p", object
);
1612 copy_obj
= copy_funcs
[object
->type
](object
);
1614 BT_LOGD("Copied value object: copy-value-addr=%p",
1617 BT_LOGE_STR("Failed to copy value object.");
1624 bt_bool
bt_value_compare(const struct bt_value
*object_a
,
1625 const struct bt_value
*object_b
)
1627 bt_bool ret
= BT_FALSE
;
1629 if (!object_a
|| !object_b
) {
1630 BT_LOGW("Invalid parameter: value A or value B is NULL: "
1631 "value-a-addr=%p, value-b-addr=%p",
1632 object_a
, object_b
);
1636 if (object_a
->type
!= object_b
->type
) {
1637 BT_LOGV("Values are different: type mismatch: "
1638 "value-a-addr=%p, value-b-addr=%p, "
1639 "value-a-type=%s, value-b-type=%s",
1641 bt_value_type_string(object_a
->type
),
1642 bt_value_type_string(object_b
->type
));
1646 ret
= compare_funcs
[object_a
->type
](object_a
, object_b
);