2 * objects.c: basic object system
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/ctf-writer/ref-internal.h>
33 #include <babeltrace/compiler.h>
35 #include <babeltrace/objects.h>
37 #define BT_OBJECT_FROM_CONCRETE(_concrete) ((struct bt_object *) (_concrete))
38 #define BT_OBJECT_TO_BOOL(_base) ((struct bt_object_bool *) (_base))
39 #define BT_OBJECT_TO_INTEGER(_base) ((struct bt_object_integer *) (_base))
40 #define BT_OBJECT_TO_FLOAT(_base) ((struct bt_object_float *) (_base))
41 #define BT_OBJECT_TO_STRING(_base) ((struct bt_object_string *) (_base))
42 #define BT_OBJECT_TO_ARRAY(_base) ((struct bt_object_array *) (_base))
43 #define BT_OBJECT_TO_MAP(_base) ((struct bt_object_map *) (_base))
46 enum bt_object_type type
;
47 struct bt_ctf_ref ref_count
;
51 struct bt_object bt_object_null_instance
= {
52 .type
= BT_OBJECT_TYPE_NULL
,
55 struct bt_object
*bt_object_null
= &bt_object_null_instance
;
57 struct bt_object_bool
{
58 struct bt_object base
;
62 struct bt_object_integer
{
63 struct bt_object base
;
67 struct bt_object_float
{
68 struct bt_object base
;
72 struct bt_object_string
{
73 struct bt_object base
;
77 struct bt_object_array
{
78 struct bt_object base
;
82 struct bt_object_map
{
83 struct bt_object base
;
88 void bt_object_string_destroy(struct bt_object
*object
)
90 struct bt_object_string
*string_obj
= BT_OBJECT_TO_STRING(object
);
92 g_string_free(string_obj
->gstr
, TRUE
);
96 void bt_object_array_destroy(struct bt_object
*object
)
99 struct bt_object_array
*array_obj
= BT_OBJECT_TO_ARRAY(object
);
101 for (x
= 0; x
< array_obj
->garray
->len
; ++x
) {
102 struct bt_object
*el_obj
;
104 el_obj
= g_array_index(array_obj
->garray
,
105 struct bt_object
*, x
);
106 bt_object_put(el_obj
);
109 g_array_free(array_obj
->garray
, TRUE
);
113 void bt_object_map_destroy(struct bt_object
*object
)
115 struct bt_object_map
*map
= BT_OBJECT_TO_MAP(object
);
118 * Hash table's registered value destructor will take care of
119 * putting each contained object. Keys are GQuarks and cannot
120 * be destroyed anyway.
122 g_hash_table_destroy(map
->ght
);
126 void (* const destroy_funcs
[])(struct bt_object
*) = {
127 [BT_OBJECT_TYPE_NULL
] = NULL
,
128 [BT_OBJECT_TYPE_BOOL
] = NULL
,
129 [BT_OBJECT_TYPE_INTEGER
] = NULL
,
130 [BT_OBJECT_TYPE_FLOAT
] = NULL
,
131 [BT_OBJECT_TYPE_STRING
] = bt_object_string_destroy
,
132 [BT_OBJECT_TYPE_ARRAY
] = bt_object_array_destroy
,
133 [BT_OBJECT_TYPE_MAP
] = bt_object_map_destroy
,
137 struct bt_object
*bt_object_null_copy(const struct bt_object
*null_obj
)
139 return bt_object_null
;
143 struct bt_object
*bt_object_bool_copy(const struct bt_object
*bool_obj
)
145 return bt_object_bool_create_init(BT_OBJECT_TO_BOOL(bool_obj
)->value
);
149 struct bt_object
*bt_object_integer_copy(const struct bt_object
*integer_obj
)
151 return bt_object_integer_create_init(
152 BT_OBJECT_TO_INTEGER(integer_obj
)->value
);
156 struct bt_object
*bt_object_float_copy(const struct bt_object
*float_obj
)
158 return bt_object_float_create_init(
159 BT_OBJECT_TO_FLOAT(float_obj
)->value
);
163 struct bt_object
*bt_object_string_copy(const struct bt_object
*string_obj
)
165 return bt_object_string_create_init(
166 BT_OBJECT_TO_STRING(string_obj
)->gstr
->str
);
170 struct bt_object
*bt_object_array_copy(const struct bt_object
*array_obj
)
174 struct bt_object
*copy_obj
;
175 struct bt_object_array
*typed_array_obj
;
177 typed_array_obj
= BT_OBJECT_TO_ARRAY(array_obj
);
178 copy_obj
= bt_object_array_create();
184 for (x
= 0; x
< typed_array_obj
->garray
->len
; ++x
) {
185 struct bt_object
*element_obj_copy
;
186 struct bt_object
*element_obj
=
187 bt_object_array_get(array_obj
, x
);
190 BT_OBJECT_PUT(copy_obj
);
194 element_obj_copy
= bt_object_copy(element_obj
);
195 BT_OBJECT_PUT(element_obj
);
197 if (!element_obj_copy
) {
198 BT_OBJECT_PUT(copy_obj
);
202 ret
= bt_object_array_append(copy_obj
, element_obj_copy
);
203 BT_OBJECT_PUT(element_obj_copy
);
206 BT_OBJECT_PUT(copy_obj
);
216 struct bt_object
*bt_object_map_copy(const struct bt_object
*map_obj
)
220 gpointer key
, element_obj
;
221 struct bt_object
*copy_obj
;
222 struct bt_object
*element_obj_copy
;
223 struct bt_object_map
*typed_map_obj
;
225 typed_map_obj
= BT_OBJECT_TO_MAP(map_obj
);
226 copy_obj
= bt_object_map_create();
232 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
234 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
235 const char *key_str
= g_quark_to_string((unsigned long) key
);
237 element_obj_copy
= bt_object_copy(element_obj
);
239 if (!element_obj_copy
) {
240 BT_OBJECT_PUT(copy_obj
);
244 ret
= bt_object_map_insert(copy_obj
, key_str
, element_obj_copy
);
245 BT_OBJECT_PUT(element_obj_copy
);
248 BT_OBJECT_PUT(copy_obj
);
258 struct bt_object
*(* const copy_funcs
[])(const struct bt_object
*) = {
259 [BT_OBJECT_TYPE_NULL
] = bt_object_null_copy
,
260 [BT_OBJECT_TYPE_BOOL
] = bt_object_bool_copy
,
261 [BT_OBJECT_TYPE_INTEGER
] = bt_object_integer_copy
,
262 [BT_OBJECT_TYPE_FLOAT
] = bt_object_float_copy
,
263 [BT_OBJECT_TYPE_STRING
] = bt_object_string_copy
,
264 [BT_OBJECT_TYPE_ARRAY
] = bt_object_array_copy
,
265 [BT_OBJECT_TYPE_MAP
] = bt_object_map_copy
,
269 bool bt_object_null_compare(const struct bt_object
*object_a
,
270 const struct bt_object
*object_b
)
273 * Always true since bt_object_compare() already checks if both
274 * object_a and object_b have the same type, and in the case of
275 * null objects, they're always the same if it is so.
281 bool bt_object_bool_compare(const struct bt_object
*object_a
,
282 const struct bt_object
*object_b
)
284 return BT_OBJECT_TO_BOOL(object_a
)->value
==
285 BT_OBJECT_TO_BOOL(object_b
)->value
;
289 bool bt_object_integer_compare(const struct bt_object
*object_a
,
290 const struct bt_object
*object_b
)
292 return BT_OBJECT_TO_INTEGER(object_a
)->value
==
293 BT_OBJECT_TO_INTEGER(object_b
)->value
;
297 bool bt_object_float_compare(const struct bt_object
*object_a
,
298 const struct bt_object
*object_b
)
300 return BT_OBJECT_TO_FLOAT(object_a
)->value
==
301 BT_OBJECT_TO_FLOAT(object_b
)->value
;
305 bool bt_object_string_compare(const struct bt_object
*object_a
,
306 const struct bt_object
*object_b
)
308 return !strcmp(BT_OBJECT_TO_STRING(object_a
)->gstr
->str
,
309 BT_OBJECT_TO_STRING(object_b
)->gstr
->str
);
313 bool bt_object_array_compare(const struct bt_object
*object_a
,
314 const struct bt_object
*object_b
)
318 const struct bt_object_array
*array_obj_a
=
319 BT_OBJECT_TO_ARRAY(object_a
);
321 if (bt_object_array_size(object_a
) != bt_object_array_size(object_b
)) {
326 for (x
= 0; x
< array_obj_a
->garray
->len
; ++x
) {
327 struct bt_object
*element_obj_a
;
328 struct bt_object
*element_obj_b
;
330 element_obj_a
= bt_object_array_get(object_a
, x
);
331 element_obj_b
= bt_object_array_get(object_b
, x
);
333 if (!bt_object_compare(element_obj_a
, element_obj_b
)) {
334 BT_OBJECT_PUT(element_obj_a
);
335 BT_OBJECT_PUT(element_obj_b
);
340 BT_OBJECT_PUT(element_obj_a
);
341 BT_OBJECT_PUT(element_obj_b
);
349 bool bt_object_map_compare(const struct bt_object
*object_a
,
350 const struct bt_object
*object_b
)
354 gpointer key
, element_obj_a
;
355 const struct bt_object_map
*map_obj_a
= BT_OBJECT_TO_MAP(object_a
);
357 if (bt_object_map_size(object_a
) != bt_object_map_size(object_b
)) {
362 g_hash_table_iter_init(&iter
, map_obj_a
->ght
);
364 while (g_hash_table_iter_next(&iter
, &key
, &element_obj_a
)) {
365 struct bt_object
*element_obj_b
;
366 const char *key_str
= g_quark_to_string((unsigned long) key
);
368 element_obj_b
= bt_object_map_get(object_b
, key_str
);
370 if (!bt_object_compare(element_obj_a
, element_obj_b
)) {
371 BT_OBJECT_PUT(element_obj_b
);
376 BT_OBJECT_PUT(element_obj_b
);
384 bool (* const compare_funcs
[])(const struct bt_object
*,
385 const struct bt_object
*) = {
386 [BT_OBJECT_TYPE_NULL
] = bt_object_null_compare
,
387 [BT_OBJECT_TYPE_BOOL
] = bt_object_bool_compare
,
388 [BT_OBJECT_TYPE_INTEGER
] = bt_object_integer_compare
,
389 [BT_OBJECT_TYPE_FLOAT
] = bt_object_float_compare
,
390 [BT_OBJECT_TYPE_STRING
] = bt_object_string_compare
,
391 [BT_OBJECT_TYPE_ARRAY
] = bt_object_array_compare
,
392 [BT_OBJECT_TYPE_MAP
] = bt_object_map_compare
,
396 void bt_object_destroy(struct bt_ctf_ref
*ref_count
)
398 struct bt_object
*object
;
400 object
= container_of(ref_count
, struct bt_object
, ref_count
);
401 assert(object
->type
!= BT_OBJECT_TYPE_UNKNOWN
);
403 if (bt_object_is_null(object
)) {
407 if (destroy_funcs
[object
->type
]) {
408 destroy_funcs
[object
->type
](object
);
414 void bt_object_get(struct bt_object
*object
)
420 bt_ctf_ref_get(&object
->ref_count
);
426 void bt_object_put(struct bt_object
*object
)
432 bt_ctf_ref_put(&object
->ref_count
, bt_object_destroy
);
438 enum bt_object_type
bt_object_get_type(const struct bt_object
*object
)
441 return BT_OBJECT_TYPE_UNKNOWN
;
448 struct bt_object
bt_object_create_base(enum bt_object_type type
)
450 struct bt_object base
;
453 bt_ctf_ref_init(&base
.ref_count
);
458 struct bt_object
*bt_object_bool_create_init(bool val
)
460 struct bt_object_bool
*bool_obj
;
462 bool_obj
= g_new0(struct bt_object_bool
, 1);
468 bool_obj
->base
= bt_object_create_base(BT_OBJECT_TYPE_BOOL
);
469 bool_obj
->value
= val
;
472 return BT_OBJECT_FROM_CONCRETE(bool_obj
);
475 struct bt_object
*bt_object_bool_create(void)
477 return bt_object_bool_create_init(false);
480 struct bt_object
*bt_object_integer_create_init(int64_t val
)
482 struct bt_object_integer
*integer_obj
;
484 integer_obj
= g_new0(struct bt_object_integer
, 1);
490 integer_obj
->base
= bt_object_create_base(BT_OBJECT_TYPE_INTEGER
);
491 integer_obj
->value
= val
;
494 return BT_OBJECT_FROM_CONCRETE(integer_obj
);
497 struct bt_object
*bt_object_integer_create(void)
499 return bt_object_integer_create_init(0);
502 struct bt_object
*bt_object_float_create_init(double val
)
504 struct bt_object_float
*float_obj
;
506 float_obj
= g_new0(struct bt_object_float
, 1);
512 float_obj
->base
= bt_object_create_base(BT_OBJECT_TYPE_FLOAT
);
513 float_obj
->value
= val
;
516 return BT_OBJECT_FROM_CONCRETE(float_obj
);
519 struct bt_object
*bt_object_float_create(void)
521 return bt_object_float_create_init(0.);
524 struct bt_object
*bt_object_string_create_init(const char *val
)
526 struct bt_object_string
*string_obj
= NULL
;
532 string_obj
= g_new0(struct bt_object_string
, 1);
538 string_obj
->base
= bt_object_create_base(BT_OBJECT_TYPE_STRING
);
539 string_obj
->gstr
= g_string_new(val
);
541 if (!string_obj
->gstr
) {
548 return BT_OBJECT_FROM_CONCRETE(string_obj
);
551 struct bt_object
*bt_object_string_create(void)
553 return bt_object_string_create_init("");
556 struct bt_object
*bt_object_array_create(void)
558 struct bt_object_array
*array_obj
;
560 array_obj
= g_new0(struct bt_object_array
, 1);
566 array_obj
->base
= bt_object_create_base(BT_OBJECT_TYPE_ARRAY
);
567 array_obj
->garray
= g_array_new(FALSE
, FALSE
,
568 sizeof(struct bt_object
*));
570 if (!array_obj
->garray
) {
577 return BT_OBJECT_FROM_CONCRETE(array_obj
);
580 struct bt_object
*bt_object_map_create(void)
582 struct bt_object_map
*map_obj
;
584 map_obj
= g_new0(struct bt_object_map
, 1);
590 map_obj
->base
= bt_object_create_base(BT_OBJECT_TYPE_MAP
);
591 map_obj
->ght
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
592 NULL
, (GDestroyNotify
) bt_object_put
);
601 return BT_OBJECT_FROM_CONCRETE(map_obj
);
604 int bt_object_bool_get(const struct bt_object
*bool_obj
, bool *val
)
607 struct bt_object_bool
*typed_bool_obj
= BT_OBJECT_TO_BOOL(bool_obj
);
609 if (!bool_obj
|| !bt_object_is_bool(bool_obj
)) {
614 *val
= typed_bool_obj
->value
;
620 int bt_object_bool_set(struct bt_object
*bool_obj
, bool val
)
623 struct bt_object_bool
*typed_bool_obj
= BT_OBJECT_TO_BOOL(bool_obj
);
625 if (!bool_obj
|| !bt_object_is_bool(bool_obj
)) {
630 typed_bool_obj
->value
= val
;
636 int bt_object_integer_get(const struct bt_object
*integer_obj
, int64_t *val
)
639 struct bt_object_integer
*typed_integer_obj
=
640 BT_OBJECT_TO_INTEGER(integer_obj
);
642 if (!integer_obj
|| !bt_object_is_integer(integer_obj
)) {
647 *val
= typed_integer_obj
->value
;
653 int bt_object_integer_set(struct bt_object
*integer_obj
, int64_t val
)
656 struct bt_object_integer
*typed_integer_obj
=
657 BT_OBJECT_TO_INTEGER(integer_obj
);
659 if (!integer_obj
|| !bt_object_is_integer(integer_obj
)) {
664 typed_integer_obj
->value
= val
;
670 int bt_object_float_get(const struct bt_object
*float_obj
, double *val
)
673 struct bt_object_float
*typed_float_obj
=
674 BT_OBJECT_TO_FLOAT(float_obj
);
676 if (!float_obj
|| !bt_object_is_float(float_obj
)) {
681 *val
= typed_float_obj
->value
;
687 int bt_object_float_set(struct bt_object
*float_obj
, double val
)
690 struct bt_object_float
*typed_float_obj
=
691 BT_OBJECT_TO_FLOAT(float_obj
);
693 if (!float_obj
|| !bt_object_is_float(float_obj
)) {
698 typed_float_obj
->value
= val
;
704 const char *bt_object_string_get(const struct bt_object
*string_obj
)
707 struct bt_object_string
*typed_string_obj
=
708 BT_OBJECT_TO_STRING(string_obj
);
710 if (!string_obj
|| !bt_object_is_string(string_obj
)) {
715 ret
= typed_string_obj
->gstr
->str
;
721 int bt_object_string_set(struct bt_object
*string_obj
, const char *val
)
724 struct bt_object_string
*typed_string_obj
=
725 BT_OBJECT_TO_STRING(string_obj
);
727 if (!string_obj
|| !bt_object_is_string(string_obj
) || !val
) {
732 g_string_assign(typed_string_obj
->gstr
, val
);
738 int bt_object_array_size(const struct bt_object
*array_obj
)
741 struct bt_object_array
*typed_array_obj
=
742 BT_OBJECT_TO_ARRAY(array_obj
);
744 if (!array_obj
|| !bt_object_is_array(array_obj
)) {
749 ret
= (int) typed_array_obj
->garray
->len
;
755 bool bt_object_array_is_empty(const struct bt_object
*array_obj
)
757 return bt_object_array_size(array_obj
) == 0;
760 struct bt_object
*bt_object_array_get(const struct bt_object
*array_obj
,
763 struct bt_object
*ret
;
764 struct bt_object_array
*typed_array_obj
=
765 BT_OBJECT_TO_ARRAY(array_obj
);
767 if (!array_obj
|| !bt_object_is_array(array_obj
) ||
768 index
>= typed_array_obj
->garray
->len
) {
773 ret
= g_array_index(typed_array_obj
->garray
,
774 struct bt_object
*, index
);
781 int bt_object_array_append(struct bt_object
*array_obj
,
782 struct bt_object
*element_obj
)
785 struct bt_object_array
*typed_array_obj
=
786 BT_OBJECT_TO_ARRAY(array_obj
);
788 if (!array_obj
|| !bt_object_is_array(array_obj
) || !element_obj
) {
793 g_array_append_val(typed_array_obj
->garray
, element_obj
);
794 bt_object_get(element_obj
);
800 int bt_object_array_append_bool(struct bt_object
*array_obj
, bool val
)
803 struct bt_object
*bool_obj
= NULL
;
805 bool_obj
= bt_object_bool_create_init(val
);
806 ret
= bt_object_array_append(array_obj
, bool_obj
);
807 bt_object_put(bool_obj
);
812 int bt_object_array_append_integer(struct bt_object
*array_obj
, int64_t val
)
815 struct bt_object
*integer_obj
= NULL
;
817 integer_obj
= bt_object_integer_create_init(val
);
818 ret
= bt_object_array_append(array_obj
, integer_obj
);
819 bt_object_put(integer_obj
);
824 int bt_object_array_append_float(struct bt_object
*array_obj
, double val
)
827 struct bt_object
*float_obj
= NULL
;
829 float_obj
= bt_object_float_create_init(val
);
830 ret
= bt_object_array_append(array_obj
, float_obj
);
831 bt_object_put(float_obj
);
836 int bt_object_array_append_string(struct bt_object
*array_obj
, const char *val
)
839 struct bt_object
*string_obj
= NULL
;
841 string_obj
= bt_object_string_create_init(val
);
842 ret
= bt_object_array_append(array_obj
, string_obj
);
843 bt_object_put(string_obj
);
848 int bt_object_array_append_array(struct bt_object
*array_obj
)
851 struct bt_object
*empty_array_obj
= NULL
;
853 empty_array_obj
= bt_object_array_create();
854 ret
= bt_object_array_append(array_obj
, empty_array_obj
);
855 bt_object_put(empty_array_obj
);
860 int bt_object_array_append_map(struct bt_object
*array_obj
)
863 struct bt_object
*map_obj
= NULL
;
865 map_obj
= bt_object_map_create();
866 ret
= bt_object_array_append(array_obj
, map_obj
);
867 bt_object_put(map_obj
);
872 int bt_object_map_size(const struct bt_object
*map_obj
)
875 struct bt_object_map
*typed_map_obj
= BT_OBJECT_TO_MAP(map_obj
);
877 if (!map_obj
|| !bt_object_is_map(map_obj
)) {
882 ret
= (int) g_hash_table_size(typed_map_obj
->ght
);
888 bool bt_object_map_is_empty(const struct bt_object
*map_obj
)
890 return bt_object_map_size(map_obj
) == 0;
893 struct bt_object
*bt_object_map_get(const struct bt_object
*map_obj
,
897 struct bt_object
*ret
;
898 struct bt_object_map
*typed_map_obj
= BT_OBJECT_TO_MAP(map_obj
);
900 if (!map_obj
|| !bt_object_is_map(map_obj
) || !key
) {
905 quark
= g_quark_from_string(key
);
906 ret
= g_hash_table_lookup(typed_map_obj
->ght
, GUINT_TO_POINTER(quark
));
916 bool bt_object_map_has_key(const struct bt_object
*map_obj
, const char *key
)
920 struct bt_object_map
*typed_map_obj
= BT_OBJECT_TO_MAP(map_obj
);
922 if (!map_obj
|| !bt_object_is_map(map_obj
) || !key
) {
927 quark
= g_quark_from_string(key
);
928 ret
= g_hash_table_contains(typed_map_obj
->ght
,
929 GUINT_TO_POINTER(quark
));
935 int bt_object_map_insert(struct bt_object
*map_obj
, const char *key
,
936 struct bt_object
*element_obj
)
940 struct bt_object_map
*typed_map_obj
= BT_OBJECT_TO_MAP(map_obj
);
942 if (!map_obj
|| !bt_object_is_map(map_obj
) || !key
|| !element_obj
) {
947 quark
= g_quark_from_string(key
);
948 g_hash_table_insert(typed_map_obj
->ght
,
949 GUINT_TO_POINTER(quark
), element_obj
);
950 bt_object_get(element_obj
);
956 int bt_object_map_insert_bool(struct bt_object
*map_obj
,
957 const char *key
, bool val
)
960 struct bt_object
*bool_obj
= NULL
;
962 bool_obj
= bt_object_bool_create_init(val
);
963 ret
= bt_object_map_insert(map_obj
, key
, bool_obj
);
964 bt_object_put(bool_obj
);
969 int bt_object_map_insert_integer(struct bt_object
*map_obj
,
970 const char *key
, int64_t val
)
973 struct bt_object
*integer_obj
= NULL
;
975 integer_obj
= bt_object_integer_create_init(val
);
976 ret
= bt_object_map_insert(map_obj
, key
, integer_obj
);
977 bt_object_put(integer_obj
);
982 int bt_object_map_insert_float(struct bt_object
*map_obj
,
983 const char *key
, double val
)
986 struct bt_object
*float_obj
= NULL
;
988 float_obj
= bt_object_float_create_init(val
);
989 ret
= bt_object_map_insert(map_obj
, key
, float_obj
);
990 bt_object_put(float_obj
);
995 int bt_object_map_insert_string(struct bt_object
*map_obj
,
996 const char *key
, const char *val
)
999 struct bt_object
*string_obj
= NULL
;
1001 string_obj
= bt_object_string_create_init(val
);
1002 ret
= bt_object_map_insert(map_obj
, key
, string_obj
);
1003 bt_object_put(string_obj
);
1008 int bt_object_map_insert_array(struct bt_object
*map_obj
,
1012 struct bt_object
*array_obj
= NULL
;
1014 array_obj
= bt_object_array_create();
1015 ret
= bt_object_map_insert(map_obj
, key
, array_obj
);
1016 bt_object_put(array_obj
);
1021 int bt_object_map_insert_map(struct bt_object
*map_obj
,
1025 struct bt_object
*empty_map_obj
= NULL
;
1027 empty_map_obj
= bt_object_map_create();
1028 ret
= bt_object_map_insert(map_obj
, key
, empty_map_obj
);
1029 bt_object_put(empty_map_obj
);
1034 int bt_object_map_foreach(const struct bt_object
*map_obj
,
1035 bt_object_map_foreach_cb cb
, void *data
)
1038 gpointer key
, element_obj
;
1039 GHashTableIter iter
;
1040 struct bt_object_map
*typed_map_obj
= BT_OBJECT_TO_MAP(map_obj
);
1042 if (!map_obj
|| !bt_object_is_map(map_obj
) || !cb
) {
1047 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
1049 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
1050 const char *key_str
= g_quark_to_string((unsigned long) key
);
1052 if (!cb(key_str
, element_obj
, data
)) {
1061 struct bt_object
*bt_object_copy(const struct bt_object
*object
)
1063 struct bt_object
*copy_obj
= NULL
;
1069 copy_obj
= copy_funcs
[object
->type
](object
);
1075 bool bt_object_compare(const struct bt_object
*object_a
,
1076 const struct bt_object
*object_b
)
1080 if (!object_a
|| !object_b
) {
1084 if (object_a
->type
!= object_b
->type
) {
1088 ret
= compare_funcs
[object_a
->type
](object_a
, object_b
);