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 g_string_free(BT_OBJECT_TO_STRING(object
)->gstr
, TRUE
);
94 void bt_object_array_destroy(struct bt_object
*object
)
97 * Pointer array's registered value destructor will take care
98 * of putting each contained object.
100 g_ptr_array_free(BT_OBJECT_TO_ARRAY(object
)->garray
, TRUE
);
104 void bt_object_map_destroy(struct bt_object
*object
)
107 * Hash table's registered value destructor will take care of
108 * putting each contained object. Keys are GQuarks and cannot
109 * be destroyed anyway.
111 g_hash_table_destroy(BT_OBJECT_TO_MAP(object
)->ght
);
115 void (* const destroy_funcs
[])(struct bt_object
*) = {
116 [BT_OBJECT_TYPE_NULL
] = NULL
,
117 [BT_OBJECT_TYPE_BOOL
] = NULL
,
118 [BT_OBJECT_TYPE_INTEGER
] = NULL
,
119 [BT_OBJECT_TYPE_FLOAT
] = NULL
,
120 [BT_OBJECT_TYPE_STRING
] = bt_object_string_destroy
,
121 [BT_OBJECT_TYPE_ARRAY
] = bt_object_array_destroy
,
122 [BT_OBJECT_TYPE_MAP
] = bt_object_map_destroy
,
126 struct bt_object
*bt_object_null_copy(const struct bt_object
*null_obj
)
128 return bt_object_null
;
132 struct bt_object
*bt_object_bool_copy(const struct bt_object
*bool_obj
)
134 return bt_object_bool_create_init(BT_OBJECT_TO_BOOL(bool_obj
)->value
);
138 struct bt_object
*bt_object_integer_copy(const struct bt_object
*integer_obj
)
140 return bt_object_integer_create_init(
141 BT_OBJECT_TO_INTEGER(integer_obj
)->value
);
145 struct bt_object
*bt_object_float_copy(const struct bt_object
*float_obj
)
147 return bt_object_float_create_init(
148 BT_OBJECT_TO_FLOAT(float_obj
)->value
);
152 struct bt_object
*bt_object_string_copy(const struct bt_object
*string_obj
)
154 return bt_object_string_create_init(
155 BT_OBJECT_TO_STRING(string_obj
)->gstr
->str
);
159 struct bt_object
*bt_object_array_copy(const struct bt_object
*array_obj
)
163 struct bt_object
*copy_obj
;
164 struct bt_object_array
*typed_array_obj
;
166 typed_array_obj
= BT_OBJECT_TO_ARRAY(array_obj
);
167 copy_obj
= bt_object_array_create();
173 for (x
= 0; x
< typed_array_obj
->garray
->len
; ++x
) {
174 struct bt_object
*element_obj_copy
;
175 struct bt_object
*element_obj
=
176 bt_object_array_get(array_obj
, x
);
179 BT_OBJECT_PUT(copy_obj
);
183 element_obj_copy
= bt_object_copy(element_obj
);
184 BT_OBJECT_PUT(element_obj
);
186 if (!element_obj_copy
) {
187 BT_OBJECT_PUT(copy_obj
);
191 ret
= bt_object_array_append(copy_obj
, element_obj_copy
);
192 BT_OBJECT_PUT(element_obj_copy
);
195 BT_OBJECT_PUT(copy_obj
);
205 struct bt_object
*bt_object_map_copy(const struct bt_object
*map_obj
)
209 gpointer key
, element_obj
;
210 struct bt_object
*copy_obj
;
211 struct bt_object
*element_obj_copy
;
212 struct bt_object_map
*typed_map_obj
;
214 typed_map_obj
= BT_OBJECT_TO_MAP(map_obj
);
215 copy_obj
= bt_object_map_create();
221 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
223 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
224 const char *key_str
= g_quark_to_string((unsigned long) key
);
226 element_obj_copy
= bt_object_copy(element_obj
);
228 if (!element_obj_copy
) {
229 BT_OBJECT_PUT(copy_obj
);
233 ret
= bt_object_map_insert(copy_obj
, key_str
, element_obj_copy
);
234 BT_OBJECT_PUT(element_obj_copy
);
237 BT_OBJECT_PUT(copy_obj
);
247 struct bt_object
*(* const copy_funcs
[])(const struct bt_object
*) = {
248 [BT_OBJECT_TYPE_NULL
] = bt_object_null_copy
,
249 [BT_OBJECT_TYPE_BOOL
] = bt_object_bool_copy
,
250 [BT_OBJECT_TYPE_INTEGER
] = bt_object_integer_copy
,
251 [BT_OBJECT_TYPE_FLOAT
] = bt_object_float_copy
,
252 [BT_OBJECT_TYPE_STRING
] = bt_object_string_copy
,
253 [BT_OBJECT_TYPE_ARRAY
] = bt_object_array_copy
,
254 [BT_OBJECT_TYPE_MAP
] = bt_object_map_copy
,
258 bool bt_object_null_compare(const struct bt_object
*object_a
,
259 const struct bt_object
*object_b
)
262 * Always true since bt_object_compare() already checks if both
263 * object_a and object_b have the same type, and in the case of
264 * null objects, they're always the same if it is so.
270 bool bt_object_bool_compare(const struct bt_object
*object_a
,
271 const struct bt_object
*object_b
)
273 return BT_OBJECT_TO_BOOL(object_a
)->value
==
274 BT_OBJECT_TO_BOOL(object_b
)->value
;
278 bool bt_object_integer_compare(const struct bt_object
*object_a
,
279 const struct bt_object
*object_b
)
281 return BT_OBJECT_TO_INTEGER(object_a
)->value
==
282 BT_OBJECT_TO_INTEGER(object_b
)->value
;
286 bool bt_object_float_compare(const struct bt_object
*object_a
,
287 const struct bt_object
*object_b
)
289 return BT_OBJECT_TO_FLOAT(object_a
)->value
==
290 BT_OBJECT_TO_FLOAT(object_b
)->value
;
294 bool bt_object_string_compare(const struct bt_object
*object_a
,
295 const struct bt_object
*object_b
)
297 return !strcmp(BT_OBJECT_TO_STRING(object_a
)->gstr
->str
,
298 BT_OBJECT_TO_STRING(object_b
)->gstr
->str
);
302 bool bt_object_array_compare(const struct bt_object
*object_a
,
303 const struct bt_object
*object_b
)
307 const struct bt_object_array
*array_obj_a
=
308 BT_OBJECT_TO_ARRAY(object_a
);
310 if (bt_object_array_size(object_a
) != bt_object_array_size(object_b
)) {
315 for (x
= 0; x
< array_obj_a
->garray
->len
; ++x
) {
316 struct bt_object
*element_obj_a
;
317 struct bt_object
*element_obj_b
;
319 element_obj_a
= bt_object_array_get(object_a
, x
);
320 element_obj_b
= bt_object_array_get(object_b
, x
);
322 if (!bt_object_compare(element_obj_a
, element_obj_b
)) {
323 BT_OBJECT_PUT(element_obj_a
);
324 BT_OBJECT_PUT(element_obj_b
);
329 BT_OBJECT_PUT(element_obj_a
);
330 BT_OBJECT_PUT(element_obj_b
);
338 bool bt_object_map_compare(const struct bt_object
*object_a
,
339 const struct bt_object
*object_b
)
343 gpointer key
, element_obj_a
;
344 const struct bt_object_map
*map_obj_a
= BT_OBJECT_TO_MAP(object_a
);
346 if (bt_object_map_size(object_a
) != bt_object_map_size(object_b
)) {
351 g_hash_table_iter_init(&iter
, map_obj_a
->ght
);
353 while (g_hash_table_iter_next(&iter
, &key
, &element_obj_a
)) {
354 struct bt_object
*element_obj_b
;
355 const char *key_str
= g_quark_to_string((unsigned long) key
);
357 element_obj_b
= bt_object_map_get(object_b
, key_str
);
359 if (!bt_object_compare(element_obj_a
, element_obj_b
)) {
360 BT_OBJECT_PUT(element_obj_b
);
365 BT_OBJECT_PUT(element_obj_b
);
373 bool (* const compare_funcs
[])(const struct bt_object
*,
374 const struct bt_object
*) = {
375 [BT_OBJECT_TYPE_NULL
] = bt_object_null_compare
,
376 [BT_OBJECT_TYPE_BOOL
] = bt_object_bool_compare
,
377 [BT_OBJECT_TYPE_INTEGER
] = bt_object_integer_compare
,
378 [BT_OBJECT_TYPE_FLOAT
] = bt_object_float_compare
,
379 [BT_OBJECT_TYPE_STRING
] = bt_object_string_compare
,
380 [BT_OBJECT_TYPE_ARRAY
] = bt_object_array_compare
,
381 [BT_OBJECT_TYPE_MAP
] = bt_object_map_compare
,
385 void bt_object_destroy(struct bt_ctf_ref
*ref_count
)
387 struct bt_object
*object
;
389 object
= container_of(ref_count
, struct bt_object
, ref_count
);
390 assert(object
->type
!= BT_OBJECT_TYPE_UNKNOWN
);
392 if (bt_object_is_null(object
)) {
396 if (destroy_funcs
[object
->type
]) {
397 destroy_funcs
[object
->type
](object
);
403 void bt_object_get(struct bt_object
*object
)
409 bt_ctf_ref_get(&object
->ref_count
);
415 void bt_object_put(struct bt_object
*object
)
421 bt_ctf_ref_put(&object
->ref_count
, bt_object_destroy
);
427 enum bt_object_type
bt_object_get_type(const struct bt_object
*object
)
430 return BT_OBJECT_TYPE_UNKNOWN
;
437 struct bt_object
bt_object_create_base(enum bt_object_type type
)
439 struct bt_object base
;
442 bt_ctf_ref_init(&base
.ref_count
);
447 struct bt_object
*bt_object_bool_create_init(bool val
)
449 struct bt_object_bool
*bool_obj
;
451 bool_obj
= g_new0(struct bt_object_bool
, 1);
457 bool_obj
->base
= bt_object_create_base(BT_OBJECT_TYPE_BOOL
);
458 bool_obj
->value
= val
;
461 return BT_OBJECT_FROM_CONCRETE(bool_obj
);
464 struct bt_object
*bt_object_bool_create(void)
466 return bt_object_bool_create_init(false);
469 struct bt_object
*bt_object_integer_create_init(int64_t val
)
471 struct bt_object_integer
*integer_obj
;
473 integer_obj
= g_new0(struct bt_object_integer
, 1);
479 integer_obj
->base
= bt_object_create_base(BT_OBJECT_TYPE_INTEGER
);
480 integer_obj
->value
= val
;
483 return BT_OBJECT_FROM_CONCRETE(integer_obj
);
486 struct bt_object
*bt_object_integer_create(void)
488 return bt_object_integer_create_init(0);
491 struct bt_object
*bt_object_float_create_init(double val
)
493 struct bt_object_float
*float_obj
;
495 float_obj
= g_new0(struct bt_object_float
, 1);
501 float_obj
->base
= bt_object_create_base(BT_OBJECT_TYPE_FLOAT
);
502 float_obj
->value
= val
;
505 return BT_OBJECT_FROM_CONCRETE(float_obj
);
508 struct bt_object
*bt_object_float_create(void)
510 return bt_object_float_create_init(0.);
513 struct bt_object
*bt_object_string_create_init(const char *val
)
515 struct bt_object_string
*string_obj
= NULL
;
521 string_obj
= g_new0(struct bt_object_string
, 1);
527 string_obj
->base
= bt_object_create_base(BT_OBJECT_TYPE_STRING
);
528 string_obj
->gstr
= g_string_new(val
);
530 if (!string_obj
->gstr
) {
537 return BT_OBJECT_FROM_CONCRETE(string_obj
);
540 struct bt_object
*bt_object_string_create(void)
542 return bt_object_string_create_init("");
545 struct bt_object
*bt_object_array_create(void)
547 struct bt_object_array
*array_obj
;
549 array_obj
= g_new0(struct bt_object_array
, 1);
555 array_obj
->base
= bt_object_create_base(BT_OBJECT_TYPE_ARRAY
);
556 array_obj
->garray
= g_ptr_array_new_full(0,
557 (GDestroyNotify
) bt_object_put
);
559 if (!array_obj
->garray
) {
566 return BT_OBJECT_FROM_CONCRETE(array_obj
);
569 struct bt_object
*bt_object_map_create(void)
571 struct bt_object_map
*map_obj
;
573 map_obj
= g_new0(struct bt_object_map
, 1);
579 map_obj
->base
= bt_object_create_base(BT_OBJECT_TYPE_MAP
);
580 map_obj
->ght
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
581 NULL
, (GDestroyNotify
) bt_object_put
);
590 return BT_OBJECT_FROM_CONCRETE(map_obj
);
593 int bt_object_bool_get(const struct bt_object
*bool_obj
, bool *val
)
596 struct bt_object_bool
*typed_bool_obj
= BT_OBJECT_TO_BOOL(bool_obj
);
598 if (!bool_obj
|| !bt_object_is_bool(bool_obj
)) {
603 *val
= typed_bool_obj
->value
;
609 int bt_object_bool_set(struct bt_object
*bool_obj
, bool val
)
612 struct bt_object_bool
*typed_bool_obj
= BT_OBJECT_TO_BOOL(bool_obj
);
614 if (!bool_obj
|| !bt_object_is_bool(bool_obj
)) {
619 typed_bool_obj
->value
= val
;
625 int bt_object_integer_get(const struct bt_object
*integer_obj
, int64_t *val
)
628 struct bt_object_integer
*typed_integer_obj
=
629 BT_OBJECT_TO_INTEGER(integer_obj
);
631 if (!integer_obj
|| !bt_object_is_integer(integer_obj
)) {
636 *val
= typed_integer_obj
->value
;
642 int bt_object_integer_set(struct bt_object
*integer_obj
, int64_t val
)
645 struct bt_object_integer
*typed_integer_obj
=
646 BT_OBJECT_TO_INTEGER(integer_obj
);
648 if (!integer_obj
|| !bt_object_is_integer(integer_obj
)) {
653 typed_integer_obj
->value
= val
;
659 int bt_object_float_get(const struct bt_object
*float_obj
, double *val
)
662 struct bt_object_float
*typed_float_obj
=
663 BT_OBJECT_TO_FLOAT(float_obj
);
665 if (!float_obj
|| !bt_object_is_float(float_obj
)) {
670 *val
= typed_float_obj
->value
;
676 int bt_object_float_set(struct bt_object
*float_obj
, double val
)
679 struct bt_object_float
*typed_float_obj
=
680 BT_OBJECT_TO_FLOAT(float_obj
);
682 if (!float_obj
|| !bt_object_is_float(float_obj
)) {
687 typed_float_obj
->value
= val
;
693 const char *bt_object_string_get(const struct bt_object
*string_obj
)
696 struct bt_object_string
*typed_string_obj
=
697 BT_OBJECT_TO_STRING(string_obj
);
699 if (!string_obj
|| !bt_object_is_string(string_obj
)) {
704 ret
= typed_string_obj
->gstr
->str
;
710 int bt_object_string_set(struct bt_object
*string_obj
, const char *val
)
713 struct bt_object_string
*typed_string_obj
=
714 BT_OBJECT_TO_STRING(string_obj
);
716 if (!string_obj
|| !bt_object_is_string(string_obj
) || !val
) {
721 g_string_assign(typed_string_obj
->gstr
, val
);
727 int bt_object_array_size(const struct bt_object
*array_obj
)
730 struct bt_object_array
*typed_array_obj
=
731 BT_OBJECT_TO_ARRAY(array_obj
);
733 if (!array_obj
|| !bt_object_is_array(array_obj
)) {
738 ret
= (int) typed_array_obj
->garray
->len
;
744 bool bt_object_array_is_empty(const struct bt_object
*array_obj
)
746 return bt_object_array_size(array_obj
) == 0;
749 struct bt_object
*bt_object_array_get(const struct bt_object
*array_obj
,
752 struct bt_object
*ret
;
753 struct bt_object_array
*typed_array_obj
=
754 BT_OBJECT_TO_ARRAY(array_obj
);
756 if (!array_obj
|| !bt_object_is_array(array_obj
) ||
757 index
>= typed_array_obj
->garray
->len
) {
762 ret
= g_ptr_array_index(typed_array_obj
->garray
, index
);
769 int bt_object_array_append(struct bt_object
*array_obj
,
770 struct bt_object
*element_obj
)
773 struct bt_object_array
*typed_array_obj
=
774 BT_OBJECT_TO_ARRAY(array_obj
);
776 if (!array_obj
|| !bt_object_is_array(array_obj
) || !element_obj
) {
781 g_ptr_array_add(typed_array_obj
->garray
, element_obj
);
782 bt_object_get(element_obj
);
788 int bt_object_array_append_bool(struct bt_object
*array_obj
, bool val
)
791 struct bt_object
*bool_obj
= NULL
;
793 bool_obj
= bt_object_bool_create_init(val
);
794 ret
= bt_object_array_append(array_obj
, bool_obj
);
795 bt_object_put(bool_obj
);
800 int bt_object_array_append_integer(struct bt_object
*array_obj
, int64_t val
)
803 struct bt_object
*integer_obj
= NULL
;
805 integer_obj
= bt_object_integer_create_init(val
);
806 ret
= bt_object_array_append(array_obj
, integer_obj
);
807 bt_object_put(integer_obj
);
812 int bt_object_array_append_float(struct bt_object
*array_obj
, double val
)
815 struct bt_object
*float_obj
= NULL
;
817 float_obj
= bt_object_float_create_init(val
);
818 ret
= bt_object_array_append(array_obj
, float_obj
);
819 bt_object_put(float_obj
);
824 int bt_object_array_append_string(struct bt_object
*array_obj
, const char *val
)
827 struct bt_object
*string_obj
= NULL
;
829 string_obj
= bt_object_string_create_init(val
);
830 ret
= bt_object_array_append(array_obj
, string_obj
);
831 bt_object_put(string_obj
);
836 int bt_object_array_append_array(struct bt_object
*array_obj
)
839 struct bt_object
*empty_array_obj
= NULL
;
841 empty_array_obj
= bt_object_array_create();
842 ret
= bt_object_array_append(array_obj
, empty_array_obj
);
843 bt_object_put(empty_array_obj
);
848 int bt_object_array_append_map(struct bt_object
*array_obj
)
851 struct bt_object
*map_obj
= NULL
;
853 map_obj
= bt_object_map_create();
854 ret
= bt_object_array_append(array_obj
, map_obj
);
855 bt_object_put(map_obj
);
860 int bt_object_array_set(struct bt_object
*array_obj
, size_t index
,
861 struct bt_object
*element_obj
)
864 struct bt_object_array
*typed_array_obj
=
865 BT_OBJECT_TO_ARRAY(array_obj
);
867 if (!array_obj
|| !bt_object_is_array(array_obj
) || !element_obj
||
868 index
>= typed_array_obj
->garray
->len
) {
873 bt_object_put(g_ptr_array_index(typed_array_obj
->garray
, index
));
874 g_ptr_array_index(typed_array_obj
->garray
, index
) = element_obj
;
875 bt_object_get(element_obj
);
881 int bt_object_map_size(const struct bt_object
*map_obj
)
884 struct bt_object_map
*typed_map_obj
= BT_OBJECT_TO_MAP(map_obj
);
886 if (!map_obj
|| !bt_object_is_map(map_obj
)) {
891 ret
= (int) g_hash_table_size(typed_map_obj
->ght
);
897 bool bt_object_map_is_empty(const struct bt_object
*map_obj
)
899 return bt_object_map_size(map_obj
) == 0;
902 struct bt_object
*bt_object_map_get(const struct bt_object
*map_obj
,
906 struct bt_object
*ret
;
907 struct bt_object_map
*typed_map_obj
= BT_OBJECT_TO_MAP(map_obj
);
909 if (!map_obj
|| !bt_object_is_map(map_obj
) || !key
) {
914 quark
= g_quark_from_string(key
);
915 ret
= g_hash_table_lookup(typed_map_obj
->ght
, GUINT_TO_POINTER(quark
));
925 bool bt_object_map_has_key(const struct bt_object
*map_obj
, const char *key
)
929 struct bt_object_map
*typed_map_obj
= BT_OBJECT_TO_MAP(map_obj
);
931 if (!map_obj
|| !bt_object_is_map(map_obj
) || !key
) {
936 quark
= g_quark_from_string(key
);
937 ret
= g_hash_table_contains(typed_map_obj
->ght
,
938 GUINT_TO_POINTER(quark
));
944 int bt_object_map_insert(struct bt_object
*map_obj
, const char *key
,
945 struct bt_object
*element_obj
)
949 struct bt_object_map
*typed_map_obj
= BT_OBJECT_TO_MAP(map_obj
);
951 if (!map_obj
|| !bt_object_is_map(map_obj
) || !key
|| !element_obj
) {
956 quark
= g_quark_from_string(key
);
957 g_hash_table_insert(typed_map_obj
->ght
,
958 GUINT_TO_POINTER(quark
), element_obj
);
959 bt_object_get(element_obj
);
965 int bt_object_map_insert_bool(struct bt_object
*map_obj
,
966 const char *key
, bool val
)
969 struct bt_object
*bool_obj
= NULL
;
971 bool_obj
= bt_object_bool_create_init(val
);
972 ret
= bt_object_map_insert(map_obj
, key
, bool_obj
);
973 bt_object_put(bool_obj
);
978 int bt_object_map_insert_integer(struct bt_object
*map_obj
,
979 const char *key
, int64_t val
)
982 struct bt_object
*integer_obj
= NULL
;
984 integer_obj
= bt_object_integer_create_init(val
);
985 ret
= bt_object_map_insert(map_obj
, key
, integer_obj
);
986 bt_object_put(integer_obj
);
991 int bt_object_map_insert_float(struct bt_object
*map_obj
,
992 const char *key
, double val
)
995 struct bt_object
*float_obj
= NULL
;
997 float_obj
= bt_object_float_create_init(val
);
998 ret
= bt_object_map_insert(map_obj
, key
, float_obj
);
999 bt_object_put(float_obj
);
1004 int bt_object_map_insert_string(struct bt_object
*map_obj
,
1005 const char *key
, const char *val
)
1008 struct bt_object
*string_obj
= NULL
;
1010 string_obj
= bt_object_string_create_init(val
);
1011 ret
= bt_object_map_insert(map_obj
, key
, string_obj
);
1012 bt_object_put(string_obj
);
1017 int bt_object_map_insert_array(struct bt_object
*map_obj
,
1021 struct bt_object
*array_obj
= NULL
;
1023 array_obj
= bt_object_array_create();
1024 ret
= bt_object_map_insert(map_obj
, key
, array_obj
);
1025 bt_object_put(array_obj
);
1030 int bt_object_map_insert_map(struct bt_object
*map_obj
,
1034 struct bt_object
*empty_map_obj
= NULL
;
1036 empty_map_obj
= bt_object_map_create();
1037 ret
= bt_object_map_insert(map_obj
, key
, empty_map_obj
);
1038 bt_object_put(empty_map_obj
);
1043 int bt_object_map_foreach(const struct bt_object
*map_obj
,
1044 bt_object_map_foreach_cb cb
, void *data
)
1047 gpointer key
, element_obj
;
1048 GHashTableIter iter
;
1049 struct bt_object_map
*typed_map_obj
= BT_OBJECT_TO_MAP(map_obj
);
1051 if (!map_obj
|| !bt_object_is_map(map_obj
) || !cb
) {
1056 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
1058 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
1059 const char *key_str
= g_quark_to_string((unsigned long) key
);
1061 if (!cb(key_str
, element_obj
, data
)) {
1070 struct bt_object
*bt_object_copy(const struct bt_object
*object
)
1072 struct bt_object
*copy_obj
= NULL
;
1078 copy_obj
= copy_funcs
[object
->type
](object
);
1084 bool bt_object_compare(const struct bt_object
*object_a
,
1085 const struct bt_object
*object_b
)
1089 if (!object_a
|| !object_b
) {
1093 if (object_a
->type
!= object_b
->type
) {
1097 ret
= compare_funcs
[object_a
->type
](object_a
, object_b
);