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
32 #include <babeltrace/compiler.h>
33 #include <babeltrace/object-internal.h>
34 #include <babeltrace/ref.h>
35 #include <babeltrace/values.h>
36 #include <babeltrace/compat/glib.h>
38 #define BT_VALUE_FROM_CONCRETE(_concrete) ((struct bt_value *) (_concrete))
39 #define BT_VALUE_TO_BOOL(_base) ((struct bt_value_bool *) (_base))
40 #define BT_VALUE_TO_INTEGER(_base) ((struct bt_value_integer *) (_base))
41 #define BT_VALUE_TO_FLOAT(_base) ((struct bt_value_float *) (_base))
42 #define BT_VALUE_TO_STRING(_base) ((struct bt_value_string *) (_base))
43 #define BT_VALUE_TO_ARRAY(_base) ((struct bt_value_array *) (_base))
44 #define BT_VALUE_TO_MAP(_base) ((struct bt_value_map *) (_base))
47 struct bt_object base
;
48 enum bt_value_type type
;
53 struct bt_value bt_value_null_instance
= {
54 .type
= BT_VALUE_TYPE_NULL
,
58 struct bt_value
*bt_value_null
= &bt_value_null_instance
;
60 struct bt_value_bool
{
65 struct bt_value_integer
{
70 struct bt_value_float
{
75 struct bt_value_string
{
80 struct bt_value_array
{
91 void bt_value_destroy(struct bt_object
*obj
);
94 void bt_value_string_destroy(struct bt_value
*object
)
96 g_string_free(BT_VALUE_TO_STRING(object
)->gstr
, TRUE
);
100 void bt_value_array_destroy(struct bt_value
*object
)
103 * Pointer array's registered value destructor will take care
104 * of putting each contained object.
106 g_ptr_array_free(BT_VALUE_TO_ARRAY(object
)->garray
, TRUE
);
110 void bt_value_map_destroy(struct bt_value
*object
)
113 * Hash table's registered value destructor will take care of
114 * putting each contained object. Keys are GQuarks and cannot
115 * be destroyed anyway.
117 g_hash_table_destroy(BT_VALUE_TO_MAP(object
)->ght
);
121 void (* const destroy_funcs
[])(struct bt_value
*) = {
122 [BT_VALUE_TYPE_NULL
] = NULL
,
123 [BT_VALUE_TYPE_BOOL
] = NULL
,
124 [BT_VALUE_TYPE_INTEGER
] = NULL
,
125 [BT_VALUE_TYPE_FLOAT
] = NULL
,
126 [BT_VALUE_TYPE_STRING
] = bt_value_string_destroy
,
127 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_destroy
,
128 [BT_VALUE_TYPE_MAP
] = bt_value_map_destroy
,
132 struct bt_value
*bt_value_null_copy(const struct bt_value
*null_obj
)
134 return bt_value_null
;
138 struct bt_value
*bt_value_bool_copy(const struct bt_value
*bool_obj
)
140 return bt_value_bool_create_init(BT_VALUE_TO_BOOL(bool_obj
)->value
);
144 struct bt_value
*bt_value_integer_copy(const struct bt_value
*integer_obj
)
146 return bt_value_integer_create_init(
147 BT_VALUE_TO_INTEGER(integer_obj
)->value
);
151 struct bt_value
*bt_value_float_copy(const struct bt_value
*float_obj
)
153 return bt_value_float_create_init(
154 BT_VALUE_TO_FLOAT(float_obj
)->value
);
158 struct bt_value
*bt_value_string_copy(const struct bt_value
*string_obj
)
160 return bt_value_string_create_init(
161 BT_VALUE_TO_STRING(string_obj
)->gstr
->str
);
165 struct bt_value
*bt_value_array_copy(const struct bt_value
*array_obj
)
169 struct bt_value
*copy_obj
;
170 struct bt_value_array
*typed_array_obj
;
172 typed_array_obj
= BT_VALUE_TO_ARRAY(array_obj
);
173 copy_obj
= bt_value_array_create();
179 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
180 struct bt_value
*element_obj_copy
;
181 struct bt_value
*element_obj
= bt_value_array_get(array_obj
, i
);
188 element_obj_copy
= bt_value_copy(element_obj
);
191 if (!element_obj_copy
) {
196 ret
= bt_value_array_append(copy_obj
, element_obj_copy
);
197 BT_PUT(element_obj_copy
);
210 struct bt_value
*bt_value_map_copy(const struct bt_value
*map_obj
)
214 gpointer key
, element_obj
;
215 struct bt_value
*copy_obj
;
216 struct bt_value
*element_obj_copy
;
217 struct bt_value_map
*typed_map_obj
;
219 typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
220 copy_obj
= bt_value_map_create();
226 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
228 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
229 const char *key_str
= g_quark_to_string((unsigned long) key
);
231 element_obj_copy
= bt_value_copy(element_obj
);
233 if (!element_obj_copy
) {
238 ret
= bt_value_map_insert(copy_obj
, key_str
, element_obj_copy
);
239 BT_PUT(element_obj_copy
);
252 struct bt_value
*(* const copy_funcs
[])(const struct bt_value
*) = {
253 [BT_VALUE_TYPE_NULL
] = bt_value_null_copy
,
254 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_copy
,
255 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_copy
,
256 [BT_VALUE_TYPE_FLOAT
] = bt_value_float_copy
,
257 [BT_VALUE_TYPE_STRING
] = bt_value_string_copy
,
258 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_copy
,
259 [BT_VALUE_TYPE_MAP
] = bt_value_map_copy
,
263 bool bt_value_null_compare(const struct bt_value
*object_a
,
264 const struct bt_value
*object_b
)
267 * Always true since bt_value_compare() already checks if both
268 * object_a and object_b have the same type, and in the case of
269 * null value objects, they're always the same if it is so.
275 bool bt_value_bool_compare(const struct bt_value
*object_a
,
276 const struct bt_value
*object_b
)
278 return BT_VALUE_TO_BOOL(object_a
)->value
==
279 BT_VALUE_TO_BOOL(object_b
)->value
;
283 bool bt_value_integer_compare(const struct bt_value
*object_a
,
284 const struct bt_value
*object_b
)
286 return BT_VALUE_TO_INTEGER(object_a
)->value
==
287 BT_VALUE_TO_INTEGER(object_b
)->value
;
291 bool bt_value_float_compare(const struct bt_value
*object_a
,
292 const struct bt_value
*object_b
)
294 return BT_VALUE_TO_FLOAT(object_a
)->value
==
295 BT_VALUE_TO_FLOAT(object_b
)->value
;
299 bool bt_value_string_compare(const struct bt_value
*object_a
,
300 const struct bt_value
*object_b
)
302 return !strcmp(BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
303 BT_VALUE_TO_STRING(object_b
)->gstr
->str
);
307 bool bt_value_array_compare(const struct bt_value
*object_a
,
308 const struct bt_value
*object_b
)
312 const struct bt_value_array
*array_obj_a
=
313 BT_VALUE_TO_ARRAY(object_a
);
315 if (bt_value_array_size(object_a
) != bt_value_array_size(object_b
)) {
320 for (i
= 0; i
< array_obj_a
->garray
->len
; ++i
) {
321 struct bt_value
*element_obj_a
;
322 struct bt_value
*element_obj_b
;
324 element_obj_a
= bt_value_array_get(object_a
, i
);
325 element_obj_b
= bt_value_array_get(object_b
, i
);
327 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
328 BT_PUT(element_obj_a
);
329 BT_PUT(element_obj_b
);
334 BT_PUT(element_obj_a
);
335 BT_PUT(element_obj_b
);
343 bool bt_value_map_compare(const struct bt_value
*object_a
,
344 const struct bt_value
*object_b
)
348 gpointer key
, element_obj_a
;
349 const struct bt_value_map
*map_obj_a
= BT_VALUE_TO_MAP(object_a
);
351 if (bt_value_map_size(object_a
) != bt_value_map_size(object_b
)) {
356 g_hash_table_iter_init(&iter
, map_obj_a
->ght
);
358 while (g_hash_table_iter_next(&iter
, &key
, &element_obj_a
)) {
359 struct bt_value
*element_obj_b
;
360 const char *key_str
= g_quark_to_string((unsigned long) key
);
362 element_obj_b
= bt_value_map_get(object_b
, key_str
);
364 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
365 BT_PUT(element_obj_b
);
370 BT_PUT(element_obj_b
);
378 bool (* const compare_funcs
[])(const struct bt_value
*,
379 const struct bt_value
*) = {
380 [BT_VALUE_TYPE_NULL
] = bt_value_null_compare
,
381 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_compare
,
382 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_compare
,
383 [BT_VALUE_TYPE_FLOAT
] = bt_value_float_compare
,
384 [BT_VALUE_TYPE_STRING
] = bt_value_string_compare
,
385 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_compare
,
386 [BT_VALUE_TYPE_MAP
] = bt_value_map_compare
,
390 void bt_value_null_freeze(struct bt_value
*object
)
395 void bt_value_generic_freeze(struct bt_value
*object
)
397 object
->is_frozen
= true;
401 void bt_value_array_freeze(struct bt_value
*object
)
404 struct bt_value_array
*typed_array_obj
=
405 BT_VALUE_TO_ARRAY(object
);
407 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
408 struct bt_value
*element_obj
=
409 g_ptr_array_index(typed_array_obj
->garray
, i
);
411 bt_value_freeze(element_obj
);
414 bt_value_generic_freeze(object
);
418 void bt_value_map_freeze(struct bt_value
*object
)
421 gpointer key
, element_obj
;
422 const struct bt_value_map
*map_obj
= BT_VALUE_TO_MAP(object
);
424 g_hash_table_iter_init(&iter
, map_obj
->ght
);
426 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
427 bt_value_freeze(element_obj
);
430 bt_value_generic_freeze(object
);
434 void (* const freeze_funcs
[])(struct bt_value
*) = {
435 [BT_VALUE_TYPE_NULL
] = bt_value_null_freeze
,
436 [BT_VALUE_TYPE_BOOL
] = bt_value_generic_freeze
,
437 [BT_VALUE_TYPE_INTEGER
] = bt_value_generic_freeze
,
438 [BT_VALUE_TYPE_FLOAT
] = bt_value_generic_freeze
,
439 [BT_VALUE_TYPE_STRING
] = bt_value_generic_freeze
,
440 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_freeze
,
441 [BT_VALUE_TYPE_MAP
] = bt_value_map_freeze
,
445 void bt_value_destroy(struct bt_object
*obj
)
447 struct bt_value
*value
;
449 value
= container_of(obj
, struct bt_value
, base
);
450 assert(value
->type
!= BT_VALUE_TYPE_UNKNOWN
);
452 if (bt_value_is_null(value
)) {
456 if (destroy_funcs
[value
->type
]) {
457 destroy_funcs
[value
->type
](value
);
464 enum bt_value_status
bt_value_freeze(struct bt_value
*object
)
466 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
469 ret
= BT_VALUE_STATUS_INVAL
;
473 freeze_funcs
[object
->type
](object
);
480 bool bt_value_is_frozen(const struct bt_value
*object
)
482 return object
&& object
->is_frozen
;
486 enum bt_value_type
bt_value_get_type(const struct bt_value
*object
)
489 return BT_VALUE_TYPE_UNKNOWN
;
496 struct bt_value
bt_value_create_base(enum bt_value_type type
)
498 struct bt_value base
;
501 base
.is_frozen
= false;
502 bt_object_init(&base
, bt_value_destroy
);
508 struct bt_value
*bt_value_bool_create_init(bool val
)
510 struct bt_value_bool
*bool_obj
;
512 bool_obj
= g_new0(struct bt_value_bool
, 1);
518 bool_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_BOOL
);
519 bool_obj
->value
= val
;
522 return BT_VALUE_FROM_CONCRETE(bool_obj
);
526 struct bt_value
*bt_value_bool_create(void)
528 return bt_value_bool_create_init(false);
532 struct bt_value
*bt_value_integer_create_init(int64_t val
)
534 struct bt_value_integer
*integer_obj
;
536 integer_obj
= g_new0(struct bt_value_integer
, 1);
542 integer_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_INTEGER
);
543 integer_obj
->value
= val
;
546 return BT_VALUE_FROM_CONCRETE(integer_obj
);
550 struct bt_value
*bt_value_integer_create(void)
552 return bt_value_integer_create_init(0);
556 struct bt_value
*bt_value_float_create_init(double val
)
558 struct bt_value_float
*float_obj
;
560 float_obj
= g_new0(struct bt_value_float
, 1);
566 float_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_FLOAT
);
567 float_obj
->value
= val
;
570 return BT_VALUE_FROM_CONCRETE(float_obj
);
574 struct bt_value
*bt_value_float_create(void)
576 return bt_value_float_create_init(0.);
580 struct bt_value
*bt_value_string_create_init(const char *val
)
582 struct bt_value_string
*string_obj
= NULL
;
588 string_obj
= g_new0(struct bt_value_string
, 1);
594 string_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_STRING
);
595 string_obj
->gstr
= g_string_new(val
);
597 if (!string_obj
->gstr
) {
604 return BT_VALUE_FROM_CONCRETE(string_obj
);
608 struct bt_value
*bt_value_string_create(void)
610 return bt_value_string_create_init("");
614 struct bt_value
*bt_value_array_create(void)
616 struct bt_value_array
*array_obj
;
618 array_obj
= g_new0(struct bt_value_array
, 1);
624 array_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_ARRAY
);
625 array_obj
->garray
= babeltrace_g_ptr_array_new_full(0,
626 (GDestroyNotify
) bt_put
);
628 if (!array_obj
->garray
) {
635 return BT_VALUE_FROM_CONCRETE(array_obj
);
639 struct bt_value
*bt_value_map_create(void)
641 struct bt_value_map
*map_obj
;
643 map_obj
= g_new0(struct bt_value_map
, 1);
649 map_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_MAP
);
650 map_obj
->ght
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
651 NULL
, (GDestroyNotify
) bt_put
);
660 return BT_VALUE_FROM_CONCRETE(map_obj
);
664 enum bt_value_status
bt_value_bool_get(const struct bt_value
*bool_obj
,
667 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
668 struct bt_value_bool
*typed_bool_obj
= BT_VALUE_TO_BOOL(bool_obj
);
670 if (!bool_obj
|| !bt_value_is_bool(bool_obj
) || !val
) {
671 ret
= BT_VALUE_STATUS_INVAL
;
675 *val
= typed_bool_obj
->value
;
682 enum bt_value_status
bt_value_bool_set(struct bt_value
*bool_obj
, bool val
)
684 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
685 struct bt_value_bool
*typed_bool_obj
= BT_VALUE_TO_BOOL(bool_obj
);
687 if (!bool_obj
|| !bt_value_is_bool(bool_obj
)) {
688 ret
= BT_VALUE_STATUS_INVAL
;
692 if (bool_obj
->is_frozen
) {
693 ret
= BT_VALUE_STATUS_FROZEN
;
697 typed_bool_obj
->value
= val
;
704 enum bt_value_status
bt_value_integer_get(const struct bt_value
*integer_obj
,
707 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
708 struct bt_value_integer
*typed_integer_obj
=
709 BT_VALUE_TO_INTEGER(integer_obj
);
711 if (!integer_obj
|| !bt_value_is_integer(integer_obj
) || !val
) {
712 ret
= BT_VALUE_STATUS_INVAL
;
716 *val
= typed_integer_obj
->value
;
723 enum bt_value_status
bt_value_integer_set(struct bt_value
*integer_obj
,
726 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
727 struct bt_value_integer
*typed_integer_obj
=
728 BT_VALUE_TO_INTEGER(integer_obj
);
730 if (!integer_obj
|| !bt_value_is_integer(integer_obj
)) {
731 ret
= BT_VALUE_STATUS_INVAL
;
735 if (integer_obj
->is_frozen
) {
736 ret
= BT_VALUE_STATUS_FROZEN
;
740 typed_integer_obj
->value
= val
;
747 enum bt_value_status
bt_value_float_get(const struct bt_value
*float_obj
,
750 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
751 struct bt_value_float
*typed_float_obj
=
752 BT_VALUE_TO_FLOAT(float_obj
);
754 if (!float_obj
|| !bt_value_is_float(float_obj
) || !val
) {
755 ret
= BT_VALUE_STATUS_INVAL
;
759 *val
= typed_float_obj
->value
;
766 enum bt_value_status
bt_value_float_set(struct bt_value
*float_obj
,
769 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
770 struct bt_value_float
*typed_float_obj
=
771 BT_VALUE_TO_FLOAT(float_obj
);
773 if (!float_obj
|| !bt_value_is_float(float_obj
)) {
774 ret
= BT_VALUE_STATUS_INVAL
;
778 if (float_obj
->is_frozen
) {
779 ret
= BT_VALUE_STATUS_FROZEN
;
783 typed_float_obj
->value
= val
;
790 enum bt_value_status
bt_value_string_get(const struct bt_value
*string_obj
,
793 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
794 struct bt_value_string
*typed_string_obj
=
795 BT_VALUE_TO_STRING(string_obj
);
797 if (!string_obj
|| !bt_value_is_string(string_obj
) || !val
) {
798 ret
= BT_VALUE_STATUS_INVAL
;
802 *val
= typed_string_obj
->gstr
->str
;
809 enum bt_value_status
bt_value_string_set(struct bt_value
*string_obj
,
812 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
813 struct bt_value_string
*typed_string_obj
=
814 BT_VALUE_TO_STRING(string_obj
);
816 if (!string_obj
|| !bt_value_is_string(string_obj
) || !val
) {
817 ret
= BT_VALUE_STATUS_INVAL
;
821 if (string_obj
->is_frozen
) {
822 ret
= BT_VALUE_STATUS_FROZEN
;
826 g_string_assign(typed_string_obj
->gstr
, val
);
833 int bt_value_array_size(const struct bt_value
*array_obj
)
836 struct bt_value_array
*typed_array_obj
=
837 BT_VALUE_TO_ARRAY(array_obj
);
839 if (!array_obj
|| !bt_value_is_array(array_obj
)) {
840 ret
= BT_VALUE_STATUS_INVAL
;
844 ret
= (int) typed_array_obj
->garray
->len
;
851 bool bt_value_array_is_empty(const struct bt_value
*array_obj
)
853 return bt_value_array_size(array_obj
) == 0;
857 struct bt_value
*bt_value_array_get(const struct bt_value
*array_obj
,
860 struct bt_value
*ret
;
861 struct bt_value_array
*typed_array_obj
=
862 BT_VALUE_TO_ARRAY(array_obj
);
864 if (!array_obj
|| !bt_value_is_array(array_obj
) ||
865 index
>= typed_array_obj
->garray
->len
) {
870 ret
= g_ptr_array_index(typed_array_obj
->garray
, index
);
878 enum bt_value_status
bt_value_array_append(struct bt_value
*array_obj
,
879 struct bt_value
*element_obj
)
881 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
882 struct bt_value_array
*typed_array_obj
=
883 BT_VALUE_TO_ARRAY(array_obj
);
885 if (!array_obj
|| !bt_value_is_array(array_obj
) || !element_obj
) {
886 ret
= BT_VALUE_STATUS_INVAL
;
890 if (array_obj
->is_frozen
) {
891 ret
= BT_VALUE_STATUS_FROZEN
;
895 g_ptr_array_add(typed_array_obj
->garray
, element_obj
);
903 enum bt_value_status
bt_value_array_append_bool(struct bt_value
*array_obj
,
906 enum bt_value_status ret
;
907 struct bt_value
*bool_obj
= NULL
;
909 bool_obj
= bt_value_bool_create_init(val
);
910 ret
= bt_value_array_append(array_obj
, bool_obj
);
917 enum bt_value_status
bt_value_array_append_integer(
918 struct bt_value
*array_obj
, int64_t val
)
920 enum bt_value_status ret
;
921 struct bt_value
*integer_obj
= NULL
;
923 integer_obj
= bt_value_integer_create_init(val
);
924 ret
= bt_value_array_append(array_obj
, integer_obj
);
931 enum bt_value_status
bt_value_array_append_float(struct bt_value
*array_obj
,
934 enum bt_value_status ret
;
935 struct bt_value
*float_obj
= NULL
;
937 float_obj
= bt_value_float_create_init(val
);
938 ret
= bt_value_array_append(array_obj
, float_obj
);
945 enum bt_value_status
bt_value_array_append_string(struct bt_value
*array_obj
,
948 enum bt_value_status ret
;
949 struct bt_value
*string_obj
= NULL
;
951 string_obj
= bt_value_string_create_init(val
);
952 ret
= bt_value_array_append(array_obj
, string_obj
);
959 enum bt_value_status
bt_value_array_append_empty_array(
960 struct bt_value
*array_obj
)
962 enum bt_value_status ret
;
963 struct bt_value
*empty_array_obj
= NULL
;
965 empty_array_obj
= bt_value_array_create();
966 ret
= bt_value_array_append(array_obj
, empty_array_obj
);
967 bt_put(empty_array_obj
);
973 enum bt_value_status
bt_value_array_append_empty_map(struct bt_value
*array_obj
)
975 enum bt_value_status ret
;
976 struct bt_value
*map_obj
= NULL
;
978 map_obj
= bt_value_map_create();
979 ret
= bt_value_array_append(array_obj
, map_obj
);
986 enum bt_value_status
bt_value_array_set(struct bt_value
*array_obj
,
987 size_t index
, struct bt_value
*element_obj
)
989 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
990 struct bt_value_array
*typed_array_obj
=
991 BT_VALUE_TO_ARRAY(array_obj
);
993 if (!array_obj
|| !bt_value_is_array(array_obj
) || !element_obj
||
994 index
>= typed_array_obj
->garray
->len
) {
995 ret
= BT_VALUE_STATUS_INVAL
;
999 if (array_obj
->is_frozen
) {
1000 ret
= BT_VALUE_STATUS_FROZEN
;
1004 bt_put(g_ptr_array_index(typed_array_obj
->garray
, index
));
1005 g_ptr_array_index(typed_array_obj
->garray
, index
) = element_obj
;
1006 bt_get(element_obj
);
1013 int bt_value_map_size(const struct bt_value
*map_obj
)
1016 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1018 if (!map_obj
|| !bt_value_is_map(map_obj
)) {
1019 ret
= BT_VALUE_STATUS_INVAL
;
1023 ret
= (int) g_hash_table_size(typed_map_obj
->ght
);
1030 bool bt_value_map_is_empty(const struct bt_value
*map_obj
)
1032 return bt_value_map_size(map_obj
) == 0;
1036 struct bt_value
*bt_value_map_get(const struct bt_value
*map_obj
,
1040 struct bt_value
*ret
;
1041 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1043 if (!map_obj
|| !bt_value_is_map(map_obj
) || !key
) {
1048 quark
= g_quark_from_string(key
);
1049 ret
= g_hash_table_lookup(typed_map_obj
->ght
, GUINT_TO_POINTER(quark
));
1060 bool bt_value_map_has_key(const struct bt_value
*map_obj
, const char *key
)
1064 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1066 if (!map_obj
|| !bt_value_is_map(map_obj
) || !key
) {
1071 quark
= g_quark_from_string(key
);
1072 ret
= babeltrace_g_hash_table_contains(typed_map_obj
->ght
,
1073 GUINT_TO_POINTER(quark
));
1080 enum bt_value_status
bt_value_map_insert(struct bt_value
*map_obj
,
1081 const char *key
, struct bt_value
*element_obj
)
1084 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1085 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1087 if (!map_obj
|| !bt_value_is_map(map_obj
) || !key
|| !element_obj
) {
1088 ret
= BT_VALUE_STATUS_INVAL
;
1092 if (map_obj
->is_frozen
) {
1093 ret
= BT_VALUE_STATUS_FROZEN
;
1097 quark
= g_quark_from_string(key
);
1098 g_hash_table_insert(typed_map_obj
->ght
,
1099 GUINT_TO_POINTER(quark
), element_obj
);
1100 bt_get(element_obj
);
1107 enum bt_value_status
bt_value_map_insert_bool(struct bt_value
*map_obj
,
1108 const char *key
, bool val
)
1110 enum bt_value_status ret
;
1111 struct bt_value
*bool_obj
= NULL
;
1113 bool_obj
= bt_value_bool_create_init(val
);
1114 ret
= bt_value_map_insert(map_obj
, key
, bool_obj
);
1121 enum bt_value_status
bt_value_map_insert_integer(struct bt_value
*map_obj
,
1122 const char *key
, int64_t val
)
1124 enum bt_value_status ret
;
1125 struct bt_value
*integer_obj
= NULL
;
1127 integer_obj
= bt_value_integer_create_init(val
);
1128 ret
= bt_value_map_insert(map_obj
, key
, integer_obj
);
1129 bt_put(integer_obj
);
1135 enum bt_value_status
bt_value_map_insert_float(struct bt_value
*map_obj
,
1136 const char *key
, double val
)
1138 enum bt_value_status ret
;
1139 struct bt_value
*float_obj
= NULL
;
1141 float_obj
= bt_value_float_create_init(val
);
1142 ret
= bt_value_map_insert(map_obj
, key
, float_obj
);
1149 enum bt_value_status
bt_value_map_insert_string(struct bt_value
*map_obj
,
1150 const char *key
, const char *val
)
1152 enum bt_value_status ret
;
1153 struct bt_value
*string_obj
= NULL
;
1155 string_obj
= bt_value_string_create_init(val
);
1156 ret
= bt_value_map_insert(map_obj
, key
, string_obj
);
1163 enum bt_value_status
bt_value_map_insert_empty_array(struct bt_value
*map_obj
,
1166 enum bt_value_status ret
;
1167 struct bt_value
*array_obj
= NULL
;
1169 array_obj
= bt_value_array_create();
1170 ret
= bt_value_map_insert(map_obj
, key
, array_obj
);
1177 enum bt_value_status
bt_value_map_insert_empty_map(struct bt_value
*map_obj
,
1180 enum bt_value_status ret
;
1181 struct bt_value
*empty_map_obj
= NULL
;
1183 empty_map_obj
= bt_value_map_create();
1184 ret
= bt_value_map_insert(map_obj
, key
, empty_map_obj
);
1185 bt_put(empty_map_obj
);
1191 enum bt_value_status
bt_value_map_foreach(const struct bt_value
*map_obj
,
1192 bt_value_map_foreach_cb cb
, void *data
)
1194 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1195 gpointer key
, element_obj
;
1196 GHashTableIter iter
;
1197 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1199 if (!map_obj
|| !bt_value_is_map(map_obj
) || !cb
) {
1200 ret
= BT_VALUE_STATUS_INVAL
;
1204 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
1206 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
1207 const char *key_str
= g_quark_to_string((unsigned long) key
);
1209 if (!cb(key_str
, element_obj
, data
)) {
1210 ret
= BT_VALUE_STATUS_CANCELLED
;
1220 struct bt_value
*bt_value_copy(const struct bt_value
*object
)
1222 struct bt_value
*copy_obj
= NULL
;
1228 copy_obj
= copy_funcs
[object
->type
](object
);
1235 bool bt_value_compare(const struct bt_value
*object_a
,
1236 const struct bt_value
*object_b
)
1240 if (!object_a
|| !object_b
) {
1244 if (object_a
->type
!= object_b
->type
) {
1248 ret
= compare_funcs
[object_a
->type
](object_a
, object_b
);