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/ref-internal.h>
33 #include <babeltrace/compiler.h>
34 #include <babeltrace/values.h>
37 #define BT_VALUE_FROM_CONCRETE(_concrete) ((struct bt_value *) (_concrete))
38 #define BT_VALUE_TO_BOOL(_base) ((struct bt_value_bool *) (_base))
39 #define BT_VALUE_TO_INTEGER(_base) ((struct bt_value_integer *) (_base))
40 #define BT_VALUE_TO_FLOAT(_base) ((struct bt_value_float *) (_base))
41 #define BT_VALUE_TO_STRING(_base) ((struct bt_value_string *) (_base))
42 #define BT_VALUE_TO_ARRAY(_base) ((struct bt_value_array *) (_base))
43 #define BT_VALUE_TO_MAP(_base) ((struct bt_value_map *) (_base))
46 enum bt_value_type type
;
47 struct bt_ref ref_count
;
52 struct bt_value bt_value_null_instance
= {
53 .type
= BT_VALUE_TYPE_NULL
,
57 struct bt_value
*bt_value_null
= &bt_value_null_instance
;
59 struct bt_value_bool
{
64 struct bt_value_integer
{
69 struct bt_value_float
{
74 struct bt_value_string
{
79 struct bt_value_array
{
90 void bt_value_destroy(struct bt_ref
*ref_count
);
93 void bt_value_string_destroy(struct bt_value
*object
)
95 g_string_free(BT_VALUE_TO_STRING(object
)->gstr
, TRUE
);
99 void bt_value_array_destroy(struct bt_value
*object
)
102 * Pointer array's registered value destructor will take care
103 * of putting each contained object.
105 g_ptr_array_free(BT_VALUE_TO_ARRAY(object
)->garray
, TRUE
);
109 void bt_value_map_destroy(struct bt_value
*object
)
112 * Hash table's registered value destructor will take care of
113 * putting each contained object. Keys are GQuarks and cannot
114 * be destroyed anyway.
116 g_hash_table_destroy(BT_VALUE_TO_MAP(object
)->ght
);
120 void (* const destroy_funcs
[])(struct bt_value
*) = {
121 [BT_VALUE_TYPE_NULL
] = NULL
,
122 [BT_VALUE_TYPE_BOOL
] = NULL
,
123 [BT_VALUE_TYPE_INTEGER
] = NULL
,
124 [BT_VALUE_TYPE_FLOAT
] = NULL
,
125 [BT_VALUE_TYPE_STRING
] = bt_value_string_destroy
,
126 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_destroy
,
127 [BT_VALUE_TYPE_MAP
] = bt_value_map_destroy
,
131 struct bt_value
*bt_value_null_copy(const struct bt_value
*null_obj
)
133 return bt_value_null
;
137 struct bt_value
*bt_value_bool_copy(const struct bt_value
*bool_obj
)
139 return bt_value_bool_create_init(BT_VALUE_TO_BOOL(bool_obj
)->value
);
143 struct bt_value
*bt_value_integer_copy(const struct bt_value
*integer_obj
)
145 return bt_value_integer_create_init(
146 BT_VALUE_TO_INTEGER(integer_obj
)->value
);
150 struct bt_value
*bt_value_float_copy(const struct bt_value
*float_obj
)
152 return bt_value_float_create_init(
153 BT_VALUE_TO_FLOAT(float_obj
)->value
);
157 struct bt_value
*bt_value_string_copy(const struct bt_value
*string_obj
)
159 return bt_value_string_create_init(
160 BT_VALUE_TO_STRING(string_obj
)->gstr
->str
);
164 struct bt_value
*bt_value_array_copy(const struct bt_value
*array_obj
)
168 struct bt_value
*copy_obj
;
169 struct bt_value_array
*typed_array_obj
;
171 typed_array_obj
= BT_VALUE_TO_ARRAY(array_obj
);
172 copy_obj
= bt_value_array_create();
178 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
179 struct bt_value
*element_obj_copy
;
180 struct bt_value
*element_obj
= bt_value_array_get(array_obj
, i
);
183 BT_VALUE_PUT(copy_obj
);
187 element_obj_copy
= bt_value_copy(element_obj
);
188 BT_VALUE_PUT(element_obj
);
190 if (!element_obj_copy
) {
191 BT_VALUE_PUT(copy_obj
);
195 ret
= bt_value_array_append(copy_obj
, element_obj_copy
);
196 BT_VALUE_PUT(element_obj_copy
);
199 BT_VALUE_PUT(copy_obj
);
209 struct bt_value
*bt_value_map_copy(const struct bt_value
*map_obj
)
213 gpointer key
, element_obj
;
214 struct bt_value
*copy_obj
;
215 struct bt_value
*element_obj_copy
;
216 struct bt_value_map
*typed_map_obj
;
218 typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
219 copy_obj
= bt_value_map_create();
225 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
227 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
228 const char *key_str
= g_quark_to_string((unsigned long) key
);
230 element_obj_copy
= bt_value_copy(element_obj
);
232 if (!element_obj_copy
) {
233 BT_VALUE_PUT(copy_obj
);
237 ret
= bt_value_map_insert(copy_obj
, key_str
, element_obj_copy
);
238 BT_VALUE_PUT(element_obj_copy
);
241 BT_VALUE_PUT(copy_obj
);
251 struct bt_value
*(* const copy_funcs
[])(const struct bt_value
*) = {
252 [BT_VALUE_TYPE_NULL
] = bt_value_null_copy
,
253 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_copy
,
254 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_copy
,
255 [BT_VALUE_TYPE_FLOAT
] = bt_value_float_copy
,
256 [BT_VALUE_TYPE_STRING
] = bt_value_string_copy
,
257 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_copy
,
258 [BT_VALUE_TYPE_MAP
] = bt_value_map_copy
,
262 bool bt_value_null_compare(const struct bt_value
*object_a
,
263 const struct bt_value
*object_b
)
266 * Always true since bt_value_compare() already checks if both
267 * object_a and object_b have the same type, and in the case of
268 * null value objects, they're always the same if it is so.
274 bool bt_value_bool_compare(const struct bt_value
*object_a
,
275 const struct bt_value
*object_b
)
277 return BT_VALUE_TO_BOOL(object_a
)->value
==
278 BT_VALUE_TO_BOOL(object_b
)->value
;
282 bool bt_value_integer_compare(const struct bt_value
*object_a
,
283 const struct bt_value
*object_b
)
285 return BT_VALUE_TO_INTEGER(object_a
)->value
==
286 BT_VALUE_TO_INTEGER(object_b
)->value
;
290 bool bt_value_float_compare(const struct bt_value
*object_a
,
291 const struct bt_value
*object_b
)
293 return BT_VALUE_TO_FLOAT(object_a
)->value
==
294 BT_VALUE_TO_FLOAT(object_b
)->value
;
298 bool bt_value_string_compare(const struct bt_value
*object_a
,
299 const struct bt_value
*object_b
)
301 return !strcmp(BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
302 BT_VALUE_TO_STRING(object_b
)->gstr
->str
);
306 bool bt_value_array_compare(const struct bt_value
*object_a
,
307 const struct bt_value
*object_b
)
311 const struct bt_value_array
*array_obj_a
=
312 BT_VALUE_TO_ARRAY(object_a
);
314 if (bt_value_array_size(object_a
) != bt_value_array_size(object_b
)) {
319 for (i
= 0; i
< array_obj_a
->garray
->len
; ++i
) {
320 struct bt_value
*element_obj_a
;
321 struct bt_value
*element_obj_b
;
323 element_obj_a
= bt_value_array_get(object_a
, i
);
324 element_obj_b
= bt_value_array_get(object_b
, i
);
326 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
327 BT_VALUE_PUT(element_obj_a
);
328 BT_VALUE_PUT(element_obj_b
);
333 BT_VALUE_PUT(element_obj_a
);
334 BT_VALUE_PUT(element_obj_b
);
342 bool bt_value_map_compare(const struct bt_value
*object_a
,
343 const struct bt_value
*object_b
)
347 gpointer key
, element_obj_a
;
348 const struct bt_value_map
*map_obj_a
= BT_VALUE_TO_MAP(object_a
);
350 if (bt_value_map_size(object_a
) != bt_value_map_size(object_b
)) {
355 g_hash_table_iter_init(&iter
, map_obj_a
->ght
);
357 while (g_hash_table_iter_next(&iter
, &key
, &element_obj_a
)) {
358 struct bt_value
*element_obj_b
;
359 const char *key_str
= g_quark_to_string((unsigned long) key
);
361 element_obj_b
= bt_value_map_get(object_b
, key_str
);
363 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
364 BT_VALUE_PUT(element_obj_b
);
369 BT_VALUE_PUT(element_obj_b
);
377 bool (* const compare_funcs
[])(const struct bt_value
*,
378 const struct bt_value
*) = {
379 [BT_VALUE_TYPE_NULL
] = bt_value_null_compare
,
380 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_compare
,
381 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_compare
,
382 [BT_VALUE_TYPE_FLOAT
] = bt_value_float_compare
,
383 [BT_VALUE_TYPE_STRING
] = bt_value_string_compare
,
384 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_compare
,
385 [BT_VALUE_TYPE_MAP
] = bt_value_map_compare
,
388 void bt_value_null_freeze(struct bt_value
*object
)
392 void bt_value_generic_freeze(struct bt_value
*object
)
394 object
->is_frozen
= true;
397 void bt_value_array_freeze(struct bt_value
*object
)
400 struct bt_value_array
*typed_array_obj
=
401 BT_VALUE_TO_ARRAY(object
);
403 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
404 struct bt_value
*element_obj
=
405 g_ptr_array_index(typed_array_obj
->garray
, i
);
407 bt_value_freeze(element_obj
);
410 bt_value_generic_freeze(object
);
413 void bt_value_map_freeze(struct bt_value
*object
)
416 gpointer key
, element_obj
;
417 const struct bt_value_map
*map_obj
= BT_VALUE_TO_MAP(object
);
419 g_hash_table_iter_init(&iter
, map_obj
->ght
);
421 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
422 bt_value_freeze(element_obj
);
425 bt_value_generic_freeze(object
);
429 void (* const freeze_funcs
[])(struct bt_value
*) = {
430 [BT_VALUE_TYPE_NULL
] = bt_value_null_freeze
,
431 [BT_VALUE_TYPE_BOOL
] = bt_value_generic_freeze
,
432 [BT_VALUE_TYPE_INTEGER
] = bt_value_generic_freeze
,
433 [BT_VALUE_TYPE_FLOAT
] = bt_value_generic_freeze
,
434 [BT_VALUE_TYPE_STRING
] = bt_value_generic_freeze
,
435 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_freeze
,
436 [BT_VALUE_TYPE_MAP
] = bt_value_map_freeze
,
440 void bt_value_destroy(struct bt_ref
*ref_count
)
442 struct bt_value
*object
;
444 object
= container_of(ref_count
, struct bt_value
, ref_count
);
445 assert(object
->type
!= BT_VALUE_TYPE_UNKNOWN
);
447 if (bt_value_is_null(object
)) {
451 if (destroy_funcs
[object
->type
]) {
452 destroy_funcs
[object
->type
](object
);
458 void bt_value_get(struct bt_value
*object
)
460 if (object
&& !bt_value_is_null(object
)) {
461 bt_ref_get(&object
->ref_count
);
467 void bt_value_put(struct bt_value
*object
)
469 if (object
&& !bt_value_is_null(object
)) {
470 bt_ref_put(&object
->ref_count
);
474 enum bt_value_status
bt_value_freeze(struct bt_value
*object
)
476 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
479 ret
= BT_VALUE_STATUS_INVAL
;
483 freeze_funcs
[object
->type
](object
);
489 bool bt_value_is_frozen(const struct bt_value
*object
)
491 return object
&& object
->is_frozen
;
494 enum bt_value_type
bt_value_get_type(const struct bt_value
*object
)
497 return BT_VALUE_TYPE_UNKNOWN
;
504 struct bt_value
bt_value_create_base(enum bt_value_type type
)
506 struct bt_value base
;
509 base
.is_frozen
= false;
510 bt_ref_init(&base
.ref_count
, bt_value_destroy
);
515 struct bt_value
*bt_value_bool_create_init(bool val
)
517 struct bt_value_bool
*bool_obj
;
519 bool_obj
= g_new0(struct bt_value_bool
, 1);
525 bool_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_BOOL
);
526 bool_obj
->value
= val
;
529 return BT_VALUE_FROM_CONCRETE(bool_obj
);
532 struct bt_value
*bt_value_bool_create(void)
534 return bt_value_bool_create_init(false);
537 struct bt_value
*bt_value_integer_create_init(int64_t val
)
539 struct bt_value_integer
*integer_obj
;
541 integer_obj
= g_new0(struct bt_value_integer
, 1);
547 integer_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_INTEGER
);
548 integer_obj
->value
= val
;
551 return BT_VALUE_FROM_CONCRETE(integer_obj
);
554 struct bt_value
*bt_value_integer_create(void)
556 return bt_value_integer_create_init(0);
559 struct bt_value
*bt_value_float_create_init(double val
)
561 struct bt_value_float
*float_obj
;
563 float_obj
= g_new0(struct bt_value_float
, 1);
569 float_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_FLOAT
);
570 float_obj
->value
= val
;
573 return BT_VALUE_FROM_CONCRETE(float_obj
);
576 struct bt_value
*bt_value_float_create(void)
578 return bt_value_float_create_init(0.);
581 struct bt_value
*bt_value_string_create_init(const char *val
)
583 struct bt_value_string
*string_obj
= NULL
;
589 string_obj
= g_new0(struct bt_value_string
, 1);
595 string_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_STRING
);
596 string_obj
->gstr
= g_string_new(val
);
598 if (!string_obj
->gstr
) {
605 return BT_VALUE_FROM_CONCRETE(string_obj
);
608 struct bt_value
*bt_value_string_create(void)
610 return bt_value_string_create_init("");
613 struct bt_value
*bt_value_array_create(void)
615 struct bt_value_array
*array_obj
;
617 array_obj
= g_new0(struct bt_value_array
, 1);
623 array_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_ARRAY
);
624 array_obj
->garray
= g_ptr_array_new_full(0,
625 (GDestroyNotify
) bt_value_put
);
627 if (!array_obj
->garray
) {
634 return BT_VALUE_FROM_CONCRETE(array_obj
);
637 struct bt_value
*bt_value_map_create(void)
639 struct bt_value_map
*map_obj
;
641 map_obj
= g_new0(struct bt_value_map
, 1);
647 map_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_MAP
);
648 map_obj
->ght
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
649 NULL
, (GDestroyNotify
) bt_value_put
);
658 return BT_VALUE_FROM_CONCRETE(map_obj
);
661 enum bt_value_status
bt_value_bool_get(const struct bt_value
*bool_obj
,
664 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
665 struct bt_value_bool
*typed_bool_obj
= BT_VALUE_TO_BOOL(bool_obj
);
667 if (!bool_obj
|| !bt_value_is_bool(bool_obj
) || !val
) {
668 ret
= BT_VALUE_STATUS_INVAL
;
672 *val
= typed_bool_obj
->value
;
678 enum bt_value_status
bt_value_bool_set(struct bt_value
*bool_obj
, bool val
)
680 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
681 struct bt_value_bool
*typed_bool_obj
= BT_VALUE_TO_BOOL(bool_obj
);
683 if (!bool_obj
|| !bt_value_is_bool(bool_obj
)) {
684 ret
= BT_VALUE_STATUS_INVAL
;
688 if (bool_obj
->is_frozen
) {
689 ret
= BT_VALUE_STATUS_FROZEN
;
693 typed_bool_obj
->value
= val
;
699 enum bt_value_status
bt_value_integer_get(const struct bt_value
*integer_obj
,
702 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
703 struct bt_value_integer
*typed_integer_obj
=
704 BT_VALUE_TO_INTEGER(integer_obj
);
706 if (!integer_obj
|| !bt_value_is_integer(integer_obj
) || !val
) {
707 ret
= BT_VALUE_STATUS_INVAL
;
711 *val
= typed_integer_obj
->value
;
717 enum bt_value_status
bt_value_integer_set(struct bt_value
*integer_obj
,
720 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
721 struct bt_value_integer
*typed_integer_obj
=
722 BT_VALUE_TO_INTEGER(integer_obj
);
724 if (!integer_obj
|| !bt_value_is_integer(integer_obj
)) {
725 ret
= BT_VALUE_STATUS_INVAL
;
729 if (integer_obj
->is_frozen
) {
730 ret
= BT_VALUE_STATUS_FROZEN
;
734 typed_integer_obj
->value
= val
;
740 enum bt_value_status
bt_value_float_get(const struct bt_value
*float_obj
,
743 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
744 struct bt_value_float
*typed_float_obj
=
745 BT_VALUE_TO_FLOAT(float_obj
);
747 if (!float_obj
|| !bt_value_is_float(float_obj
) || !val
) {
748 ret
= BT_VALUE_STATUS_INVAL
;
752 *val
= typed_float_obj
->value
;
758 enum bt_value_status
bt_value_float_set(struct bt_value
*float_obj
,
761 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
762 struct bt_value_float
*typed_float_obj
=
763 BT_VALUE_TO_FLOAT(float_obj
);
765 if (!float_obj
|| !bt_value_is_float(float_obj
)) {
766 ret
= BT_VALUE_STATUS_INVAL
;
770 if (float_obj
->is_frozen
) {
771 ret
= BT_VALUE_STATUS_FROZEN
;
775 typed_float_obj
->value
= val
;
781 enum bt_value_status
bt_value_string_get(const struct bt_value
*string_obj
,
784 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
785 struct bt_value_string
*typed_string_obj
=
786 BT_VALUE_TO_STRING(string_obj
);
788 if (!string_obj
|| !bt_value_is_string(string_obj
) || !val
) {
789 ret
= BT_VALUE_STATUS_INVAL
;
793 *val
= typed_string_obj
->gstr
->str
;
799 enum bt_value_status
bt_value_string_set(struct bt_value
*string_obj
,
802 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
803 struct bt_value_string
*typed_string_obj
=
804 BT_VALUE_TO_STRING(string_obj
);
806 if (!string_obj
|| !bt_value_is_string(string_obj
) || !val
) {
807 ret
= BT_VALUE_STATUS_INVAL
;
811 if (string_obj
->is_frozen
) {
812 ret
= BT_VALUE_STATUS_FROZEN
;
816 g_string_assign(typed_string_obj
->gstr
, val
);
822 int bt_value_array_size(const struct bt_value
*array_obj
)
825 struct bt_value_array
*typed_array_obj
=
826 BT_VALUE_TO_ARRAY(array_obj
);
828 if (!array_obj
|| !bt_value_is_array(array_obj
)) {
829 ret
= BT_VALUE_STATUS_INVAL
;
833 ret
= (int) typed_array_obj
->garray
->len
;
839 bool bt_value_array_is_empty(const struct bt_value
*array_obj
)
841 return bt_value_array_size(array_obj
) == 0;
844 struct bt_value
*bt_value_array_get(const struct bt_value
*array_obj
,
847 struct bt_value
*ret
;
848 struct bt_value_array
*typed_array_obj
=
849 BT_VALUE_TO_ARRAY(array_obj
);
851 if (!array_obj
|| !bt_value_is_array(array_obj
) ||
852 index
>= typed_array_obj
->garray
->len
) {
857 ret
= g_ptr_array_index(typed_array_obj
->garray
, index
);
864 enum bt_value_status
bt_value_array_append(struct bt_value
*array_obj
,
865 struct bt_value
*element_obj
)
867 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
868 struct bt_value_array
*typed_array_obj
=
869 BT_VALUE_TO_ARRAY(array_obj
);
871 if (!array_obj
|| !bt_value_is_array(array_obj
) || !element_obj
) {
872 ret
= BT_VALUE_STATUS_INVAL
;
876 if (array_obj
->is_frozen
) {
877 ret
= BT_VALUE_STATUS_FROZEN
;
881 g_ptr_array_add(typed_array_obj
->garray
, element_obj
);
882 bt_value_get(element_obj
);
888 enum bt_value_status
bt_value_array_append_bool(struct bt_value
*array_obj
,
891 enum bt_value_status ret
;
892 struct bt_value
*bool_obj
= NULL
;
894 bool_obj
= bt_value_bool_create_init(val
);
895 ret
= bt_value_array_append(array_obj
, bool_obj
);
896 bt_value_put(bool_obj
);
901 enum bt_value_status
bt_value_array_append_integer(
902 struct bt_value
*array_obj
, int64_t val
)
904 enum bt_value_status ret
;
905 struct bt_value
*integer_obj
= NULL
;
907 integer_obj
= bt_value_integer_create_init(val
);
908 ret
= bt_value_array_append(array_obj
, integer_obj
);
909 bt_value_put(integer_obj
);
914 enum bt_value_status
bt_value_array_append_float(struct bt_value
*array_obj
,
917 enum bt_value_status ret
;
918 struct bt_value
*float_obj
= NULL
;
920 float_obj
= bt_value_float_create_init(val
);
921 ret
= bt_value_array_append(array_obj
, float_obj
);
922 bt_value_put(float_obj
);
927 enum bt_value_status
bt_value_array_append_string(struct bt_value
*array_obj
,
930 enum bt_value_status ret
;
931 struct bt_value
*string_obj
= NULL
;
933 string_obj
= bt_value_string_create_init(val
);
934 ret
= bt_value_array_append(array_obj
, string_obj
);
935 bt_value_put(string_obj
);
940 enum bt_value_status
bt_value_array_append_array(struct bt_value
*array_obj
)
942 enum bt_value_status ret
;
943 struct bt_value
*empty_array_obj
= NULL
;
945 empty_array_obj
= bt_value_array_create();
946 ret
= bt_value_array_append(array_obj
, empty_array_obj
);
947 bt_value_put(empty_array_obj
);
952 enum bt_value_status
bt_value_array_append_map(struct bt_value
*array_obj
)
954 enum bt_value_status ret
;
955 struct bt_value
*map_obj
= NULL
;
957 map_obj
= bt_value_map_create();
958 ret
= bt_value_array_append(array_obj
, map_obj
);
959 bt_value_put(map_obj
);
964 enum bt_value_status
bt_value_array_set(struct bt_value
*array_obj
,
965 size_t index
, struct bt_value
*element_obj
)
967 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
968 struct bt_value_array
*typed_array_obj
=
969 BT_VALUE_TO_ARRAY(array_obj
);
971 if (!array_obj
|| !bt_value_is_array(array_obj
) || !element_obj
||
972 index
>= typed_array_obj
->garray
->len
) {
973 ret
= BT_VALUE_STATUS_INVAL
;
977 if (array_obj
->is_frozen
) {
978 ret
= BT_VALUE_STATUS_FROZEN
;
982 bt_value_put(g_ptr_array_index(typed_array_obj
->garray
, index
));
983 g_ptr_array_index(typed_array_obj
->garray
, index
) = element_obj
;
984 bt_value_get(element_obj
);
990 int bt_value_map_size(const struct bt_value
*map_obj
)
993 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
995 if (!map_obj
|| !bt_value_is_map(map_obj
)) {
996 ret
= BT_VALUE_STATUS_INVAL
;
1000 ret
= (int) g_hash_table_size(typed_map_obj
->ght
);
1006 bool bt_value_map_is_empty(const struct bt_value
*map_obj
)
1008 return bt_value_map_size(map_obj
) == 0;
1011 struct bt_value
*bt_value_map_get(const struct bt_value
*map_obj
,
1015 struct bt_value
*ret
;
1016 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1018 if (!map_obj
|| !bt_value_is_map(map_obj
) || !key
) {
1023 quark
= g_quark_from_string(key
);
1024 ret
= g_hash_table_lookup(typed_map_obj
->ght
, GUINT_TO_POINTER(quark
));
1034 bool bt_value_map_has_key(const struct bt_value
*map_obj
, const char *key
)
1038 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1040 if (!map_obj
|| !bt_value_is_map(map_obj
) || !key
) {
1045 quark
= g_quark_from_string(key
);
1046 ret
= g_hash_table_contains(typed_map_obj
->ght
,
1047 GUINT_TO_POINTER(quark
));
1053 enum bt_value_status
bt_value_map_insert(struct bt_value
*map_obj
,
1054 const char *key
, struct bt_value
*element_obj
)
1057 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1058 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1060 if (!map_obj
|| !bt_value_is_map(map_obj
) || !key
|| !element_obj
) {
1061 ret
= BT_VALUE_STATUS_INVAL
;
1065 if (map_obj
->is_frozen
) {
1066 ret
= BT_VALUE_STATUS_FROZEN
;
1070 quark
= g_quark_from_string(key
);
1071 g_hash_table_insert(typed_map_obj
->ght
,
1072 GUINT_TO_POINTER(quark
), element_obj
);
1073 bt_value_get(element_obj
);
1079 enum bt_value_status
bt_value_map_insert_bool(struct bt_value
*map_obj
,
1080 const char *key
, bool val
)
1082 enum bt_value_status ret
;
1083 struct bt_value
*bool_obj
= NULL
;
1085 bool_obj
= bt_value_bool_create_init(val
);
1086 ret
= bt_value_map_insert(map_obj
, key
, bool_obj
);
1087 bt_value_put(bool_obj
);
1092 enum bt_value_status
bt_value_map_insert_integer(struct bt_value
*map_obj
,
1093 const char *key
, int64_t val
)
1095 enum bt_value_status ret
;
1096 struct bt_value
*integer_obj
= NULL
;
1098 integer_obj
= bt_value_integer_create_init(val
);
1099 ret
= bt_value_map_insert(map_obj
, key
, integer_obj
);
1100 bt_value_put(integer_obj
);
1105 enum bt_value_status
bt_value_map_insert_float(struct bt_value
*map_obj
,
1106 const char *key
, double val
)
1108 enum bt_value_status ret
;
1109 struct bt_value
*float_obj
= NULL
;
1111 float_obj
= bt_value_float_create_init(val
);
1112 ret
= bt_value_map_insert(map_obj
, key
, float_obj
);
1113 bt_value_put(float_obj
);
1118 enum bt_value_status
bt_value_map_insert_string(struct bt_value
*map_obj
,
1119 const char *key
, const char *val
)
1121 enum bt_value_status ret
;
1122 struct bt_value
*string_obj
= NULL
;
1124 string_obj
= bt_value_string_create_init(val
);
1125 ret
= bt_value_map_insert(map_obj
, key
, string_obj
);
1126 bt_value_put(string_obj
);
1131 enum bt_value_status
bt_value_map_insert_array(struct bt_value
*map_obj
,
1134 enum bt_value_status ret
;
1135 struct bt_value
*array_obj
= NULL
;
1137 array_obj
= bt_value_array_create();
1138 ret
= bt_value_map_insert(map_obj
, key
, array_obj
);
1139 bt_value_put(array_obj
);
1144 enum bt_value_status
bt_value_map_insert_map(struct bt_value
*map_obj
,
1147 enum bt_value_status ret
;
1148 struct bt_value
*empty_map_obj
= NULL
;
1150 empty_map_obj
= bt_value_map_create();
1151 ret
= bt_value_map_insert(map_obj
, key
, empty_map_obj
);
1152 bt_value_put(empty_map_obj
);
1157 enum bt_value_status
bt_value_map_foreach(const struct bt_value
*map_obj
,
1158 bt_value_map_foreach_cb cb
, void *data
)
1160 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1161 gpointer key
, element_obj
;
1162 GHashTableIter iter
;
1163 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1165 if (!map_obj
|| !bt_value_is_map(map_obj
) || !cb
) {
1166 ret
= BT_VALUE_STATUS_INVAL
;
1170 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
1172 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
1173 const char *key_str
= g_quark_to_string((unsigned long) key
);
1175 if (!cb(key_str
, element_obj
, data
)) {
1176 ret
= BT_VALUE_STATUS_CANCELLED
;
1185 struct bt_value
*bt_value_copy(const struct bt_value
*object
)
1187 struct bt_value
*copy_obj
= NULL
;
1193 copy_obj
= copy_funcs
[object
->type
](object
);
1199 bool bt_value_compare(const struct bt_value
*object_a
,
1200 const struct bt_value
*object_b
)
1204 if (!object_a
|| !object_b
) {
1208 if (object_a
->type
!= object_b
->type
) {
1212 ret
= compare_funcs
[object_a
->type
](object_a
, object_b
);