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
= {
62 .type
= BT_VALUE_TYPE_NULL
,
66 struct bt_value
*bt_value_null
= &bt_value_null_instance
;
68 struct bt_value_bool
{
73 struct bt_value_integer
{
78 struct bt_value_float
{
83 struct bt_value_string
{
88 struct bt_value_array
{
99 void bt_value_destroy(struct bt_object
*obj
);
102 void bt_value_string_destroy(struct bt_value
*object
)
104 g_string_free(BT_VALUE_TO_STRING(object
)->gstr
, TRUE
);
108 void bt_value_array_destroy(struct bt_value
*object
)
111 * Pointer array's registered value destructor will take care
112 * of putting each contained object.
114 g_ptr_array_free(BT_VALUE_TO_ARRAY(object
)->garray
, TRUE
);
118 void bt_value_map_destroy(struct bt_value
*object
)
121 * Hash table's registered value destructor will take care of
122 * putting each contained object. Keys are GQuarks and cannot
123 * be destroyed anyway.
125 g_hash_table_destroy(BT_VALUE_TO_MAP(object
)->ght
);
129 void (* const destroy_funcs
[])(struct bt_value
*) = {
130 [BT_VALUE_TYPE_NULL
] = NULL
,
131 [BT_VALUE_TYPE_BOOL
] = NULL
,
132 [BT_VALUE_TYPE_INTEGER
] = NULL
,
133 [BT_VALUE_TYPE_FLOAT
] = NULL
,
134 [BT_VALUE_TYPE_STRING
] = bt_value_string_destroy
,
135 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_destroy
,
136 [BT_VALUE_TYPE_MAP
] = bt_value_map_destroy
,
140 struct bt_value
*bt_value_null_copy(const struct bt_value
*null_obj
)
142 return bt_value_null
;
146 struct bt_value
*bt_value_bool_copy(const struct bt_value
*bool_obj
)
148 return bt_value_bool_create_init(BT_VALUE_TO_BOOL(bool_obj
)->value
);
152 struct bt_value
*bt_value_integer_copy(const struct bt_value
*integer_obj
)
154 return bt_value_integer_create_init(
155 BT_VALUE_TO_INTEGER(integer_obj
)->value
);
159 struct bt_value
*bt_value_float_copy(const struct bt_value
*float_obj
)
161 return bt_value_float_create_init(
162 BT_VALUE_TO_FLOAT(float_obj
)->value
);
166 struct bt_value
*bt_value_string_copy(const struct bt_value
*string_obj
)
168 return bt_value_string_create_init(
169 BT_VALUE_TO_STRING(string_obj
)->gstr
->str
);
173 struct bt_value
*bt_value_array_copy(const struct bt_value
*array_obj
)
177 struct bt_value
*copy_obj
;
178 struct bt_value_array
*typed_array_obj
;
180 typed_array_obj
= BT_VALUE_TO_ARRAY(array_obj
);
181 copy_obj
= bt_value_array_create();
187 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
188 struct bt_value
*element_obj_copy
;
189 struct bt_value
*element_obj
= bt_value_array_get(array_obj
, i
);
196 element_obj_copy
= bt_value_copy(element_obj
);
199 if (!element_obj_copy
) {
204 ret
= bt_value_array_append(copy_obj
, element_obj_copy
);
205 BT_PUT(element_obj_copy
);
218 struct bt_value
*bt_value_map_copy(const struct bt_value
*map_obj
)
222 gpointer key
, element_obj
;
223 struct bt_value
*copy_obj
;
224 struct bt_value
*element_obj_copy
;
225 struct bt_value_map
*typed_map_obj
;
227 typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
228 copy_obj
= bt_value_map_create();
234 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
236 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
237 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
239 element_obj_copy
= bt_value_copy(element_obj
);
241 if (!element_obj_copy
) {
246 ret
= bt_value_map_insert(copy_obj
, key_str
, element_obj_copy
);
247 BT_PUT(element_obj_copy
);
260 struct bt_value
*(* const copy_funcs
[])(const struct bt_value
*) = {
261 [BT_VALUE_TYPE_NULL
] = bt_value_null_copy
,
262 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_copy
,
263 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_copy
,
264 [BT_VALUE_TYPE_FLOAT
] = bt_value_float_copy
,
265 [BT_VALUE_TYPE_STRING
] = bt_value_string_copy
,
266 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_copy
,
267 [BT_VALUE_TYPE_MAP
] = bt_value_map_copy
,
271 bool bt_value_null_compare(const struct bt_value
*object_a
,
272 const struct bt_value
*object_b
)
275 * Always true since bt_value_compare() already checks if both
276 * object_a and object_b have the same type, and in the case of
277 * null value objects, they're always the same if it is so.
283 bool bt_value_bool_compare(const struct bt_value
*object_a
,
284 const struct bt_value
*object_b
)
286 return BT_VALUE_TO_BOOL(object_a
)->value
==
287 BT_VALUE_TO_BOOL(object_b
)->value
;
291 bool bt_value_integer_compare(const struct bt_value
*object_a
,
292 const struct bt_value
*object_b
)
294 return BT_VALUE_TO_INTEGER(object_a
)->value
==
295 BT_VALUE_TO_INTEGER(object_b
)->value
;
299 bool bt_value_float_compare(const struct bt_value
*object_a
,
300 const struct bt_value
*object_b
)
302 return BT_VALUE_TO_FLOAT(object_a
)->value
==
303 BT_VALUE_TO_FLOAT(object_b
)->value
;
307 bool bt_value_string_compare(const struct bt_value
*object_a
,
308 const struct bt_value
*object_b
)
310 return !strcmp(BT_VALUE_TO_STRING(object_a
)->gstr
->str
,
311 BT_VALUE_TO_STRING(object_b
)->gstr
->str
);
315 bool bt_value_array_compare(const struct bt_value
*object_a
,
316 const struct bt_value
*object_b
)
320 const struct bt_value_array
*array_obj_a
=
321 BT_VALUE_TO_ARRAY(object_a
);
323 if (bt_value_array_size(object_a
) != bt_value_array_size(object_b
)) {
328 for (i
= 0; i
< array_obj_a
->garray
->len
; ++i
) {
329 struct bt_value
*element_obj_a
;
330 struct bt_value
*element_obj_b
;
332 element_obj_a
= bt_value_array_get(object_a
, i
);
333 element_obj_b
= bt_value_array_get(object_b
, i
);
335 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
336 BT_PUT(element_obj_a
);
337 BT_PUT(element_obj_b
);
342 BT_PUT(element_obj_a
);
343 BT_PUT(element_obj_b
);
351 bool bt_value_map_compare(const struct bt_value
*object_a
,
352 const struct bt_value
*object_b
)
356 gpointer key
, element_obj_a
;
357 const struct bt_value_map
*map_obj_a
= BT_VALUE_TO_MAP(object_a
);
359 if (bt_value_map_size(object_a
) != bt_value_map_size(object_b
)) {
364 g_hash_table_iter_init(&iter
, map_obj_a
->ght
);
366 while (g_hash_table_iter_next(&iter
, &key
, &element_obj_a
)) {
367 struct bt_value
*element_obj_b
;
368 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
370 element_obj_b
= bt_value_map_get(object_b
, key_str
);
372 if (!bt_value_compare(element_obj_a
, element_obj_b
)) {
373 BT_PUT(element_obj_b
);
378 BT_PUT(element_obj_b
);
386 bool (* const compare_funcs
[])(const struct bt_value
*,
387 const struct bt_value
*) = {
388 [BT_VALUE_TYPE_NULL
] = bt_value_null_compare
,
389 [BT_VALUE_TYPE_BOOL
] = bt_value_bool_compare
,
390 [BT_VALUE_TYPE_INTEGER
] = bt_value_integer_compare
,
391 [BT_VALUE_TYPE_FLOAT
] = bt_value_float_compare
,
392 [BT_VALUE_TYPE_STRING
] = bt_value_string_compare
,
393 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_compare
,
394 [BT_VALUE_TYPE_MAP
] = bt_value_map_compare
,
397 void bt_value_null_freeze(struct bt_value
*object
)
401 void bt_value_generic_freeze(struct bt_value
*object
)
403 object
->is_frozen
= true;
406 void bt_value_array_freeze(struct bt_value
*object
)
409 struct bt_value_array
*typed_array_obj
=
410 BT_VALUE_TO_ARRAY(object
);
412 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
413 struct bt_value
*element_obj
=
414 g_ptr_array_index(typed_array_obj
->garray
, i
);
416 bt_value_freeze(element_obj
);
419 bt_value_generic_freeze(object
);
422 void bt_value_map_freeze(struct bt_value
*object
)
425 gpointer key
, element_obj
;
426 const struct bt_value_map
*map_obj
= BT_VALUE_TO_MAP(object
);
428 g_hash_table_iter_init(&iter
, map_obj
->ght
);
430 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
431 bt_value_freeze(element_obj
);
434 bt_value_generic_freeze(object
);
438 void (* const freeze_funcs
[])(struct bt_value
*) = {
439 [BT_VALUE_TYPE_NULL
] = bt_value_null_freeze
,
440 [BT_VALUE_TYPE_BOOL
] = bt_value_generic_freeze
,
441 [BT_VALUE_TYPE_INTEGER
] = bt_value_generic_freeze
,
442 [BT_VALUE_TYPE_FLOAT
] = bt_value_generic_freeze
,
443 [BT_VALUE_TYPE_STRING
] = bt_value_generic_freeze
,
444 [BT_VALUE_TYPE_ARRAY
] = bt_value_array_freeze
,
445 [BT_VALUE_TYPE_MAP
] = bt_value_map_freeze
,
449 void bt_value_destroy(struct bt_object
*obj
)
451 struct bt_value
*value
;
453 value
= container_of(obj
, struct bt_value
, base
);
454 assert(value
->type
!= BT_VALUE_TYPE_UNKNOWN
);
456 if (bt_value_is_null(value
)) {
460 if (destroy_funcs
[value
->type
]) {
461 destroy_funcs
[value
->type
](value
);
467 enum bt_value_status
bt_value_freeze(struct bt_value
*object
)
469 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
472 ret
= BT_VALUE_STATUS_INVAL
;
476 freeze_funcs
[object
->type
](object
);
482 bool bt_value_is_frozen(const struct bt_value
*object
)
484 return object
&& object
->is_frozen
;
487 enum bt_value_type
bt_value_get_type(const struct bt_value
*object
)
490 return BT_VALUE_TYPE_UNKNOWN
;
497 struct bt_value
bt_value_create_base(enum bt_value_type type
)
499 struct bt_value base
;
502 base
.is_frozen
= false;
503 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
);
525 struct bt_value
*bt_value_bool_create(void)
527 return bt_value_bool_create_init(false);
530 struct bt_value
*bt_value_integer_create_init(int64_t val
)
532 struct bt_value_integer
*integer_obj
;
534 integer_obj
= g_new0(struct bt_value_integer
, 1);
540 integer_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_INTEGER
);
541 integer_obj
->value
= val
;
544 return BT_VALUE_FROM_CONCRETE(integer_obj
);
547 struct bt_value
*bt_value_integer_create(void)
549 return bt_value_integer_create_init(0);
552 struct bt_value
*bt_value_float_create_init(double val
)
554 struct bt_value_float
*float_obj
;
556 float_obj
= g_new0(struct bt_value_float
, 1);
562 float_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_FLOAT
);
563 float_obj
->value
= val
;
566 return BT_VALUE_FROM_CONCRETE(float_obj
);
569 struct bt_value
*bt_value_float_create(void)
571 return bt_value_float_create_init(0.);
574 struct bt_value
*bt_value_string_create_init(const char *val
)
576 struct bt_value_string
*string_obj
= NULL
;
582 string_obj
= g_new0(struct bt_value_string
, 1);
588 string_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_STRING
);
589 string_obj
->gstr
= g_string_new(val
);
591 if (!string_obj
->gstr
) {
598 return BT_VALUE_FROM_CONCRETE(string_obj
);
601 struct bt_value
*bt_value_string_create(void)
603 return bt_value_string_create_init("");
606 struct bt_value
*bt_value_array_create(void)
608 struct bt_value_array
*array_obj
;
610 array_obj
= g_new0(struct bt_value_array
, 1);
616 array_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_ARRAY
);
617 array_obj
->garray
= bt_g_ptr_array_new_full(0,
618 (GDestroyNotify
) bt_put
);
620 if (!array_obj
->garray
) {
627 return BT_VALUE_FROM_CONCRETE(array_obj
);
630 struct bt_value
*bt_value_map_create(void)
632 struct bt_value_map
*map_obj
;
634 map_obj
= g_new0(struct bt_value_map
, 1);
640 map_obj
->base
= bt_value_create_base(BT_VALUE_TYPE_MAP
);
641 map_obj
->ght
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
642 NULL
, (GDestroyNotify
) bt_put
);
651 return BT_VALUE_FROM_CONCRETE(map_obj
);
654 enum bt_value_status
bt_value_bool_get(const struct bt_value
*bool_obj
,
657 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
658 struct bt_value_bool
*typed_bool_obj
= BT_VALUE_TO_BOOL(bool_obj
);
660 if (!bool_obj
|| !bt_value_is_bool(bool_obj
) || !val
) {
661 ret
= BT_VALUE_STATUS_INVAL
;
665 *val
= typed_bool_obj
->value
;
671 enum bt_value_status
bt_value_bool_set(struct bt_value
*bool_obj
, bool val
)
673 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
674 struct bt_value_bool
*typed_bool_obj
= BT_VALUE_TO_BOOL(bool_obj
);
676 if (!bool_obj
|| !bt_value_is_bool(bool_obj
)) {
677 ret
= BT_VALUE_STATUS_INVAL
;
681 if (bool_obj
->is_frozen
) {
682 ret
= BT_VALUE_STATUS_FROZEN
;
686 typed_bool_obj
->value
= val
;
692 enum bt_value_status
bt_value_integer_get(const struct bt_value
*integer_obj
,
695 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
696 struct bt_value_integer
*typed_integer_obj
=
697 BT_VALUE_TO_INTEGER(integer_obj
);
699 if (!integer_obj
|| !bt_value_is_integer(integer_obj
) || !val
) {
700 ret
= BT_VALUE_STATUS_INVAL
;
704 *val
= typed_integer_obj
->value
;
710 enum bt_value_status
bt_value_integer_set(struct bt_value
*integer_obj
,
713 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
714 struct bt_value_integer
*typed_integer_obj
=
715 BT_VALUE_TO_INTEGER(integer_obj
);
717 if (!integer_obj
|| !bt_value_is_integer(integer_obj
)) {
718 ret
= BT_VALUE_STATUS_INVAL
;
722 if (integer_obj
->is_frozen
) {
723 ret
= BT_VALUE_STATUS_FROZEN
;
727 typed_integer_obj
->value
= val
;
733 enum bt_value_status
bt_value_float_get(const struct bt_value
*float_obj
,
736 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
737 struct bt_value_float
*typed_float_obj
=
738 BT_VALUE_TO_FLOAT(float_obj
);
740 if (!float_obj
|| !bt_value_is_float(float_obj
) || !val
) {
741 ret
= BT_VALUE_STATUS_INVAL
;
745 *val
= typed_float_obj
->value
;
751 enum bt_value_status
bt_value_float_set(struct bt_value
*float_obj
,
754 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
755 struct bt_value_float
*typed_float_obj
=
756 BT_VALUE_TO_FLOAT(float_obj
);
758 if (!float_obj
|| !bt_value_is_float(float_obj
)) {
759 ret
= BT_VALUE_STATUS_INVAL
;
763 if (float_obj
->is_frozen
) {
764 ret
= BT_VALUE_STATUS_FROZEN
;
768 typed_float_obj
->value
= val
;
774 enum bt_value_status
bt_value_string_get(const struct bt_value
*string_obj
,
777 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
778 struct bt_value_string
*typed_string_obj
=
779 BT_VALUE_TO_STRING(string_obj
);
781 if (!string_obj
|| !bt_value_is_string(string_obj
) || !val
) {
782 ret
= BT_VALUE_STATUS_INVAL
;
786 *val
= typed_string_obj
->gstr
->str
;
792 enum bt_value_status
bt_value_string_set(struct bt_value
*string_obj
,
795 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
796 struct bt_value_string
*typed_string_obj
=
797 BT_VALUE_TO_STRING(string_obj
);
799 if (!string_obj
|| !bt_value_is_string(string_obj
) || !val
) {
800 ret
= BT_VALUE_STATUS_INVAL
;
804 if (string_obj
->is_frozen
) {
805 ret
= BT_VALUE_STATUS_FROZEN
;
809 g_string_assign(typed_string_obj
->gstr
, val
);
815 int bt_value_array_size(const struct bt_value
*array_obj
)
818 struct bt_value_array
*typed_array_obj
=
819 BT_VALUE_TO_ARRAY(array_obj
);
821 if (!array_obj
|| !bt_value_is_array(array_obj
)) {
822 ret
= BT_VALUE_STATUS_INVAL
;
826 ret
= (int) typed_array_obj
->garray
->len
;
832 bool bt_value_array_is_empty(const struct bt_value
*array_obj
)
834 return bt_value_array_size(array_obj
) == 0;
837 struct bt_value
*bt_value_array_get(const struct bt_value
*array_obj
,
840 struct bt_value
*ret
;
841 struct bt_value_array
*typed_array_obj
=
842 BT_VALUE_TO_ARRAY(array_obj
);
844 if (!array_obj
|| !bt_value_is_array(array_obj
) ||
845 index
>= typed_array_obj
->garray
->len
) {
850 ret
= g_ptr_array_index(typed_array_obj
->garray
, index
);
857 enum bt_value_status
bt_value_array_append(struct bt_value
*array_obj
,
858 struct bt_value
*element_obj
)
860 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
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
) || !element_obj
) {
865 ret
= BT_VALUE_STATUS_INVAL
;
869 if (array_obj
->is_frozen
) {
870 ret
= BT_VALUE_STATUS_FROZEN
;
874 g_ptr_array_add(typed_array_obj
->garray
, element_obj
);
881 enum bt_value_status
bt_value_array_append_bool(struct bt_value
*array_obj
,
884 enum bt_value_status ret
;
885 struct bt_value
*bool_obj
= NULL
;
887 bool_obj
= bt_value_bool_create_init(val
);
888 ret
= bt_value_array_append(array_obj
, bool_obj
);
894 enum bt_value_status
bt_value_array_append_integer(
895 struct bt_value
*array_obj
, int64_t val
)
897 enum bt_value_status ret
;
898 struct bt_value
*integer_obj
= NULL
;
900 integer_obj
= bt_value_integer_create_init(val
);
901 ret
= bt_value_array_append(array_obj
, integer_obj
);
907 enum bt_value_status
bt_value_array_append_float(struct bt_value
*array_obj
,
910 enum bt_value_status ret
;
911 struct bt_value
*float_obj
= NULL
;
913 float_obj
= bt_value_float_create_init(val
);
914 ret
= bt_value_array_append(array_obj
, float_obj
);
920 enum bt_value_status
bt_value_array_append_string(struct bt_value
*array_obj
,
923 enum bt_value_status ret
;
924 struct bt_value
*string_obj
= NULL
;
926 string_obj
= bt_value_string_create_init(val
);
927 ret
= bt_value_array_append(array_obj
, string_obj
);
933 enum bt_value_status
bt_value_array_append_empty_array(
934 struct bt_value
*array_obj
)
936 enum bt_value_status ret
;
937 struct bt_value
*empty_array_obj
= NULL
;
939 empty_array_obj
= bt_value_array_create();
940 ret
= bt_value_array_append(array_obj
, empty_array_obj
);
941 bt_put(empty_array_obj
);
946 enum bt_value_status
bt_value_array_append_empty_map(struct bt_value
*array_obj
)
948 enum bt_value_status ret
;
949 struct bt_value
*map_obj
= NULL
;
951 map_obj
= bt_value_map_create();
952 ret
= bt_value_array_append(array_obj
, map_obj
);
958 enum bt_value_status
bt_value_array_set(struct bt_value
*array_obj
,
959 size_t index
, struct bt_value
*element_obj
)
961 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
962 struct bt_value_array
*typed_array_obj
=
963 BT_VALUE_TO_ARRAY(array_obj
);
965 if (!array_obj
|| !bt_value_is_array(array_obj
) || !element_obj
||
966 index
>= typed_array_obj
->garray
->len
) {
967 ret
= BT_VALUE_STATUS_INVAL
;
971 if (array_obj
->is_frozen
) {
972 ret
= BT_VALUE_STATUS_FROZEN
;
976 bt_put(g_ptr_array_index(typed_array_obj
->garray
, index
));
977 g_ptr_array_index(typed_array_obj
->garray
, index
) = element_obj
;
984 int bt_value_map_size(const struct bt_value
*map_obj
)
987 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
989 if (!map_obj
|| !bt_value_is_map(map_obj
)) {
990 ret
= BT_VALUE_STATUS_INVAL
;
994 ret
= (int) g_hash_table_size(typed_map_obj
->ght
);
1000 bool bt_value_map_is_empty(const struct bt_value
*map_obj
)
1002 return bt_value_map_size(map_obj
) == 0;
1005 struct bt_value
*bt_value_map_get(const struct bt_value
*map_obj
,
1009 struct bt_value
*ret
;
1010 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1012 if (!map_obj
|| !bt_value_is_map(map_obj
) || !key
) {
1017 quark
= g_quark_from_string(key
);
1018 ret
= g_hash_table_lookup(typed_map_obj
->ght
, GUINT_TO_POINTER(quark
));
1028 bool bt_value_map_has_key(const struct bt_value
*map_obj
, const char *key
)
1032 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1034 if (!map_obj
|| !bt_value_is_map(map_obj
) || !key
) {
1039 quark
= g_quark_from_string(key
);
1040 ret
= bt_g_hash_table_contains(typed_map_obj
->ght
,
1041 GUINT_TO_POINTER(quark
));
1047 enum bt_value_status
bt_value_map_insert(struct bt_value
*map_obj
,
1048 const char *key
, struct bt_value
*element_obj
)
1051 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1052 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1054 if (!map_obj
|| !bt_value_is_map(map_obj
) || !key
|| !element_obj
) {
1055 ret
= BT_VALUE_STATUS_INVAL
;
1059 if (map_obj
->is_frozen
) {
1060 ret
= BT_VALUE_STATUS_FROZEN
;
1064 quark
= g_quark_from_string(key
);
1065 g_hash_table_insert(typed_map_obj
->ght
,
1066 GUINT_TO_POINTER(quark
), element_obj
);
1067 bt_get(element_obj
);
1073 enum bt_value_status
bt_value_map_insert_bool(struct bt_value
*map_obj
,
1074 const char *key
, bool val
)
1076 enum bt_value_status ret
;
1077 struct bt_value
*bool_obj
= NULL
;
1079 bool_obj
= bt_value_bool_create_init(val
);
1080 ret
= bt_value_map_insert(map_obj
, key
, bool_obj
);
1086 enum bt_value_status
bt_value_map_insert_integer(struct bt_value
*map_obj
,
1087 const char *key
, int64_t val
)
1089 enum bt_value_status ret
;
1090 struct bt_value
*integer_obj
= NULL
;
1092 integer_obj
= bt_value_integer_create_init(val
);
1093 ret
= bt_value_map_insert(map_obj
, key
, integer_obj
);
1094 bt_put(integer_obj
);
1099 enum bt_value_status
bt_value_map_insert_float(struct bt_value
*map_obj
,
1100 const char *key
, double val
)
1102 enum bt_value_status ret
;
1103 struct bt_value
*float_obj
= NULL
;
1105 float_obj
= bt_value_float_create_init(val
);
1106 ret
= bt_value_map_insert(map_obj
, key
, float_obj
);
1112 enum bt_value_status
bt_value_map_insert_string(struct bt_value
*map_obj
,
1113 const char *key
, const char *val
)
1115 enum bt_value_status ret
;
1116 struct bt_value
*string_obj
= NULL
;
1118 string_obj
= bt_value_string_create_init(val
);
1119 ret
= bt_value_map_insert(map_obj
, key
, string_obj
);
1125 enum bt_value_status
bt_value_map_insert_empty_array(struct bt_value
*map_obj
,
1128 enum bt_value_status ret
;
1129 struct bt_value
*array_obj
= NULL
;
1131 array_obj
= bt_value_array_create();
1132 ret
= bt_value_map_insert(map_obj
, key
, array_obj
);
1138 enum bt_value_status
bt_value_map_insert_empty_map(struct bt_value
*map_obj
,
1141 enum bt_value_status ret
;
1142 struct bt_value
*empty_map_obj
= NULL
;
1144 empty_map_obj
= bt_value_map_create();
1145 ret
= bt_value_map_insert(map_obj
, key
, empty_map_obj
);
1146 bt_put(empty_map_obj
);
1151 enum bt_value_status
bt_value_map_foreach(const struct bt_value
*map_obj
,
1152 bt_value_map_foreach_cb cb
, void *data
)
1154 enum bt_value_status ret
= BT_VALUE_STATUS_OK
;
1155 gpointer key
, element_obj
;
1156 GHashTableIter iter
;
1157 struct bt_value_map
*typed_map_obj
= BT_VALUE_TO_MAP(map_obj
);
1159 if (!map_obj
|| !bt_value_is_map(map_obj
) || !cb
) {
1160 ret
= BT_VALUE_STATUS_INVAL
;
1164 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
1166 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
1167 const char *key_str
= g_quark_to_string(GPOINTER_TO_UINT(key
));
1169 if (!cb(key_str
, element_obj
, data
)) {
1170 ret
= BT_VALUE_STATUS_CANCELLED
;
1179 struct extend_map_element_data
{
1180 struct bt_value
*extended_obj
;
1185 bool extend_map_element(const char *key
,
1186 struct bt_value
*extension_obj_elem
, void *data
)
1190 struct extend_map_element_data
*extend_data
= data
;
1192 /* Copy object which is to replace the current one */
1193 struct bt_value
*extension_obj_elem_copy
=
1194 bt_value_copy(extension_obj_elem
);
1196 /* Replace in extended object */
1197 if (bt_value_map_insert(extend_data
->extended_obj
, key
,
1198 extension_obj_elem_copy
)) {
1206 extend_data
->got_error
= true;
1209 BT_PUT(extension_obj_elem_copy
);
1214 struct bt_value
*bt_value_map_extend(struct bt_value
*base_map_obj
,
1215 struct bt_value
*extension_obj
)
1217 struct bt_value
*extended_obj
= NULL
;
1218 struct extend_map_element_data extend_data
= { 0 };
1220 if (!bt_value_is_map(base_map_obj
) || !bt_value_is_map(extension_obj
)) {
1224 /* Create copy of base map object to start with */
1225 extended_obj
= bt_value_copy(base_map_obj
);
1226 if (!extended_obj
) {
1231 * For each key in the extension map object, replace this key
1232 * in the copied map object.
1234 extend_data
.extended_obj
= extended_obj
;
1236 if (bt_value_map_foreach(extension_obj
, extend_map_element
,
1241 if (extend_data
.got_error
) {
1248 BT_PUT(extended_obj
);
1251 return extended_obj
;
1254 struct bt_value
*bt_value_copy(const struct bt_value
*object
)
1256 struct bt_value
*copy_obj
= NULL
;
1262 copy_obj
= copy_funcs
[object
->type
](object
);
1268 bool bt_value_compare(const struct bt_value
*object_a
,
1269 const struct bt_value
*object_b
)
1273 if (!object_a
|| !object_b
) {
1277 if (object_a
->type
!= object_b
->type
) {
1281 ret
= compare_funcs
[object_a
->type
](object_a
, object_b
);