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/ref-internal.h>
33 #include <babeltrace/compiler.h>
34 #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_ref ref_count
;
52 struct bt_object bt_object_null_instance
= {
53 .type
= BT_OBJECT_TYPE_NULL
,
57 struct bt_object
*bt_object_null
= &bt_object_null_instance
;
59 struct bt_object_bool
{
60 struct bt_object base
;
64 struct bt_object_integer
{
65 struct bt_object base
;
69 struct bt_object_float
{
70 struct bt_object base
;
74 struct bt_object_string
{
75 struct bt_object base
;
79 struct bt_object_array
{
80 struct bt_object base
;
84 struct bt_object_map
{
85 struct bt_object base
;
90 void bt_object_destroy(struct bt_ref
*ref_count
);
93 void bt_object_string_destroy(struct bt_object
*object
)
95 g_string_free(BT_OBJECT_TO_STRING(object
)->gstr
, TRUE
);
99 void bt_object_array_destroy(struct bt_object
*object
)
102 * Pointer array's registered value destructor will take care
103 * of putting each contained object.
105 g_ptr_array_free(BT_OBJECT_TO_ARRAY(object
)->garray
, TRUE
);
109 void bt_object_map_destroy(struct bt_object
*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_OBJECT_TO_MAP(object
)->ght
);
120 void (* const destroy_funcs
[])(struct bt_object
*) = {
121 [BT_OBJECT_TYPE_NULL
] = NULL
,
122 [BT_OBJECT_TYPE_BOOL
] = NULL
,
123 [BT_OBJECT_TYPE_INTEGER
] = NULL
,
124 [BT_OBJECT_TYPE_FLOAT
] = NULL
,
125 [BT_OBJECT_TYPE_STRING
] = bt_object_string_destroy
,
126 [BT_OBJECT_TYPE_ARRAY
] = bt_object_array_destroy
,
127 [BT_OBJECT_TYPE_MAP
] = bt_object_map_destroy
,
131 struct bt_object
*bt_object_null_copy(const struct bt_object
*null_obj
)
133 return bt_object_null
;
137 struct bt_object
*bt_object_bool_copy(const struct bt_object
*bool_obj
)
139 return bt_object_bool_create_init(BT_OBJECT_TO_BOOL(bool_obj
)->value
);
143 struct bt_object
*bt_object_integer_copy(const struct bt_object
*integer_obj
)
145 return bt_object_integer_create_init(
146 BT_OBJECT_TO_INTEGER(integer_obj
)->value
);
150 struct bt_object
*bt_object_float_copy(const struct bt_object
*float_obj
)
152 return bt_object_float_create_init(
153 BT_OBJECT_TO_FLOAT(float_obj
)->value
);
157 struct bt_object
*bt_object_string_copy(const struct bt_object
*string_obj
)
159 return bt_object_string_create_init(
160 BT_OBJECT_TO_STRING(string_obj
)->gstr
->str
);
164 struct bt_object
*bt_object_array_copy(const struct bt_object
*array_obj
)
168 struct bt_object
*copy_obj
;
169 struct bt_object_array
*typed_array_obj
;
171 typed_array_obj
= BT_OBJECT_TO_ARRAY(array_obj
);
172 copy_obj
= bt_object_array_create();
178 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
179 struct bt_object
*element_obj_copy
;
180 struct bt_object
*element_obj
=
181 bt_object_array_get(array_obj
, i
);
184 BT_OBJECT_PUT(copy_obj
);
188 element_obj_copy
= bt_object_copy(element_obj
);
189 BT_OBJECT_PUT(element_obj
);
191 if (!element_obj_copy
) {
192 BT_OBJECT_PUT(copy_obj
);
196 ret
= bt_object_array_append(copy_obj
, element_obj_copy
);
197 BT_OBJECT_PUT(element_obj_copy
);
200 BT_OBJECT_PUT(copy_obj
);
210 struct bt_object
*bt_object_map_copy(const struct bt_object
*map_obj
)
214 gpointer key
, element_obj
;
215 struct bt_object
*copy_obj
;
216 struct bt_object
*element_obj_copy
;
217 struct bt_object_map
*typed_map_obj
;
219 typed_map_obj
= BT_OBJECT_TO_MAP(map_obj
);
220 copy_obj
= bt_object_map_create();
226 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
228 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
229 const char *key_str
= g_quark_to_string((unsigned long) key
);
231 element_obj_copy
= bt_object_copy(element_obj
);
233 if (!element_obj_copy
) {
234 BT_OBJECT_PUT(copy_obj
);
238 ret
= bt_object_map_insert(copy_obj
, key_str
, element_obj_copy
);
239 BT_OBJECT_PUT(element_obj_copy
);
242 BT_OBJECT_PUT(copy_obj
);
252 struct bt_object
*(* const copy_funcs
[])(const struct bt_object
*) = {
253 [BT_OBJECT_TYPE_NULL
] = bt_object_null_copy
,
254 [BT_OBJECT_TYPE_BOOL
] = bt_object_bool_copy
,
255 [BT_OBJECT_TYPE_INTEGER
] = bt_object_integer_copy
,
256 [BT_OBJECT_TYPE_FLOAT
] = bt_object_float_copy
,
257 [BT_OBJECT_TYPE_STRING
] = bt_object_string_copy
,
258 [BT_OBJECT_TYPE_ARRAY
] = bt_object_array_copy
,
259 [BT_OBJECT_TYPE_MAP
] = bt_object_map_copy
,
263 bool bt_object_null_compare(const struct bt_object
*object_a
,
264 const struct bt_object
*object_b
)
267 * Always true since bt_object_compare() already checks if both
268 * object_a and object_b have the same type, and in the case of
269 * null objects, they're always the same if it is so.
275 bool bt_object_bool_compare(const struct bt_object
*object_a
,
276 const struct bt_object
*object_b
)
278 return BT_OBJECT_TO_BOOL(object_a
)->value
==
279 BT_OBJECT_TO_BOOL(object_b
)->value
;
283 bool bt_object_integer_compare(const struct bt_object
*object_a
,
284 const struct bt_object
*object_b
)
286 return BT_OBJECT_TO_INTEGER(object_a
)->value
==
287 BT_OBJECT_TO_INTEGER(object_b
)->value
;
291 bool bt_object_float_compare(const struct bt_object
*object_a
,
292 const struct bt_object
*object_b
)
294 return BT_OBJECT_TO_FLOAT(object_a
)->value
==
295 BT_OBJECT_TO_FLOAT(object_b
)->value
;
299 bool bt_object_string_compare(const struct bt_object
*object_a
,
300 const struct bt_object
*object_b
)
302 return !strcmp(BT_OBJECT_TO_STRING(object_a
)->gstr
->str
,
303 BT_OBJECT_TO_STRING(object_b
)->gstr
->str
);
307 bool bt_object_array_compare(const struct bt_object
*object_a
,
308 const struct bt_object
*object_b
)
312 const struct bt_object_array
*array_obj_a
=
313 BT_OBJECT_TO_ARRAY(object_a
);
315 if (bt_object_array_size(object_a
) != bt_object_array_size(object_b
)) {
320 for (i
= 0; i
< array_obj_a
->garray
->len
; ++i
) {
321 struct bt_object
*element_obj_a
;
322 struct bt_object
*element_obj_b
;
324 element_obj_a
= bt_object_array_get(object_a
, i
);
325 element_obj_b
= bt_object_array_get(object_b
, i
);
327 if (!bt_object_compare(element_obj_a
, element_obj_b
)) {
328 BT_OBJECT_PUT(element_obj_a
);
329 BT_OBJECT_PUT(element_obj_b
);
334 BT_OBJECT_PUT(element_obj_a
);
335 BT_OBJECT_PUT(element_obj_b
);
343 bool bt_object_map_compare(const struct bt_object
*object_a
,
344 const struct bt_object
*object_b
)
348 gpointer key
, element_obj_a
;
349 const struct bt_object_map
*map_obj_a
= BT_OBJECT_TO_MAP(object_a
);
351 if (bt_object_map_size(object_a
) != bt_object_map_size(object_b
)) {
356 g_hash_table_iter_init(&iter
, map_obj_a
->ght
);
358 while (g_hash_table_iter_next(&iter
, &key
, &element_obj_a
)) {
359 struct bt_object
*element_obj_b
;
360 const char *key_str
= g_quark_to_string((unsigned long) key
);
362 element_obj_b
= bt_object_map_get(object_b
, key_str
);
364 if (!bt_object_compare(element_obj_a
, element_obj_b
)) {
365 BT_OBJECT_PUT(element_obj_b
);
370 BT_OBJECT_PUT(element_obj_b
);
378 bool (* const compare_funcs
[])(const struct bt_object
*,
379 const struct bt_object
*) = {
380 [BT_OBJECT_TYPE_NULL
] = bt_object_null_compare
,
381 [BT_OBJECT_TYPE_BOOL
] = bt_object_bool_compare
,
382 [BT_OBJECT_TYPE_INTEGER
] = bt_object_integer_compare
,
383 [BT_OBJECT_TYPE_FLOAT
] = bt_object_float_compare
,
384 [BT_OBJECT_TYPE_STRING
] = bt_object_string_compare
,
385 [BT_OBJECT_TYPE_ARRAY
] = bt_object_array_compare
,
386 [BT_OBJECT_TYPE_MAP
] = bt_object_map_compare
,
389 void bt_object_null_freeze(struct bt_object
*object
)
393 void bt_object_generic_freeze(struct bt_object
*object
)
395 object
->is_frozen
= true;
398 void bt_object_array_freeze(struct bt_object
*object
)
401 struct bt_object_array
*typed_array_obj
=
402 BT_OBJECT_TO_ARRAY(object
);
404 for (i
= 0; i
< typed_array_obj
->garray
->len
; ++i
) {
405 struct bt_object
*element_obj
=
406 g_ptr_array_index(typed_array_obj
->garray
, i
);
408 bt_object_freeze(element_obj
);
411 bt_object_generic_freeze(object
);
414 void bt_object_map_freeze(struct bt_object
*object
)
417 gpointer key
, element_obj
;
418 const struct bt_object_map
*map_obj
= BT_OBJECT_TO_MAP(object
);
420 g_hash_table_iter_init(&iter
, map_obj
->ght
);
422 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
423 bt_object_freeze(element_obj
);
426 bt_object_generic_freeze(object
);
430 void (* const freeze_funcs
[])(struct bt_object
*) = {
431 [BT_OBJECT_TYPE_NULL
] = bt_object_null_freeze
,
432 [BT_OBJECT_TYPE_BOOL
] = bt_object_generic_freeze
,
433 [BT_OBJECT_TYPE_INTEGER
] = bt_object_generic_freeze
,
434 [BT_OBJECT_TYPE_FLOAT
] = bt_object_generic_freeze
,
435 [BT_OBJECT_TYPE_STRING
] = bt_object_generic_freeze
,
436 [BT_OBJECT_TYPE_ARRAY
] = bt_object_array_freeze
,
437 [BT_OBJECT_TYPE_MAP
] = bt_object_map_freeze
,
441 void bt_object_destroy(struct bt_ref
*ref_count
)
443 struct bt_object
*object
;
445 object
= container_of(ref_count
, struct bt_object
, ref_count
);
446 assert(object
->type
!= BT_OBJECT_TYPE_UNKNOWN
);
448 if (bt_object_is_null(object
)) {
452 if (destroy_funcs
[object
->type
]) {
453 destroy_funcs
[object
->type
](object
);
459 void bt_object_get(struct bt_object
*object
)
461 if (object
&& !bt_object_is_null(object
)) {
462 bt_ref_get(&object
->ref_count
);
468 void bt_object_put(struct bt_object
*object
)
470 if (object
&& !bt_object_is_null(object
)) {
471 bt_ref_put(&object
->ref_count
);
475 enum bt_object_status
bt_object_freeze(struct bt_object
*object
)
477 enum bt_object_status ret
= BT_OBJECT_STATUS_OK
;
480 ret
= BT_OBJECT_STATUS_INVAL
;
484 freeze_funcs
[object
->type
](object
);
490 bool bt_object_is_frozen(const struct bt_object
*object
)
492 return object
&& object
->is_frozen
;
495 enum bt_object_type
bt_object_get_type(const struct bt_object
*object
)
498 return BT_OBJECT_TYPE_UNKNOWN
;
505 struct bt_object
bt_object_create_base(enum bt_object_type type
)
507 struct bt_object base
;
510 base
.is_frozen
= false;
511 bt_ref_init(&base
.ref_count
, bt_object_destroy
);
516 struct bt_object
*bt_object_bool_create_init(bool val
)
518 struct bt_object_bool
*bool_obj
;
520 bool_obj
= g_new0(struct bt_object_bool
, 1);
526 bool_obj
->base
= bt_object_create_base(BT_OBJECT_TYPE_BOOL
);
527 bool_obj
->value
= val
;
530 return BT_OBJECT_FROM_CONCRETE(bool_obj
);
533 struct bt_object
*bt_object_bool_create(void)
535 return bt_object_bool_create_init(false);
538 struct bt_object
*bt_object_integer_create_init(int64_t val
)
540 struct bt_object_integer
*integer_obj
;
542 integer_obj
= g_new0(struct bt_object_integer
, 1);
548 integer_obj
->base
= bt_object_create_base(BT_OBJECT_TYPE_INTEGER
);
549 integer_obj
->value
= val
;
552 return BT_OBJECT_FROM_CONCRETE(integer_obj
);
555 struct bt_object
*bt_object_integer_create(void)
557 return bt_object_integer_create_init(0);
560 struct bt_object
*bt_object_float_create_init(double val
)
562 struct bt_object_float
*float_obj
;
564 float_obj
= g_new0(struct bt_object_float
, 1);
570 float_obj
->base
= bt_object_create_base(BT_OBJECT_TYPE_FLOAT
);
571 float_obj
->value
= val
;
574 return BT_OBJECT_FROM_CONCRETE(float_obj
);
577 struct bt_object
*bt_object_float_create(void)
579 return bt_object_float_create_init(0.);
582 struct bt_object
*bt_object_string_create_init(const char *val
)
584 struct bt_object_string
*string_obj
= NULL
;
590 string_obj
= g_new0(struct bt_object_string
, 1);
596 string_obj
->base
= bt_object_create_base(BT_OBJECT_TYPE_STRING
);
597 string_obj
->gstr
= g_string_new(val
);
599 if (!string_obj
->gstr
) {
606 return BT_OBJECT_FROM_CONCRETE(string_obj
);
609 struct bt_object
*bt_object_string_create(void)
611 return bt_object_string_create_init("");
614 struct bt_object
*bt_object_array_create(void)
616 struct bt_object_array
*array_obj
;
618 array_obj
= g_new0(struct bt_object_array
, 1);
624 array_obj
->base
= bt_object_create_base(BT_OBJECT_TYPE_ARRAY
);
625 array_obj
->garray
= g_ptr_array_new_full(0,
626 (GDestroyNotify
) bt_object_put
);
628 if (!array_obj
->garray
) {
635 return BT_OBJECT_FROM_CONCRETE(array_obj
);
638 struct bt_object
*bt_object_map_create(void)
640 struct bt_object_map
*map_obj
;
642 map_obj
= g_new0(struct bt_object_map
, 1);
648 map_obj
->base
= bt_object_create_base(BT_OBJECT_TYPE_MAP
);
649 map_obj
->ght
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
650 NULL
, (GDestroyNotify
) bt_object_put
);
659 return BT_OBJECT_FROM_CONCRETE(map_obj
);
662 enum bt_object_status
bt_object_bool_get(const struct bt_object
*bool_obj
,
665 enum bt_object_status ret
= BT_OBJECT_STATUS_OK
;
666 struct bt_object_bool
*typed_bool_obj
= BT_OBJECT_TO_BOOL(bool_obj
);
668 if (!bool_obj
|| !bt_object_is_bool(bool_obj
) || !val
) {
669 ret
= BT_OBJECT_STATUS_INVAL
;
673 *val
= typed_bool_obj
->value
;
679 enum bt_object_status
bt_object_bool_set(struct bt_object
*bool_obj
, bool val
)
681 enum bt_object_status ret
= BT_OBJECT_STATUS_OK
;
682 struct bt_object_bool
*typed_bool_obj
= BT_OBJECT_TO_BOOL(bool_obj
);
684 if (!bool_obj
|| !bt_object_is_bool(bool_obj
)) {
685 ret
= BT_OBJECT_STATUS_INVAL
;
689 if (bool_obj
->is_frozen
) {
690 ret
= BT_OBJECT_STATUS_FROZEN
;
694 typed_bool_obj
->value
= val
;
700 enum bt_object_status
bt_object_integer_get(const struct bt_object
*integer_obj
,
703 enum bt_object_status ret
= BT_OBJECT_STATUS_OK
;
704 struct bt_object_integer
*typed_integer_obj
=
705 BT_OBJECT_TO_INTEGER(integer_obj
);
707 if (!integer_obj
|| !bt_object_is_integer(integer_obj
) || !val
) {
708 ret
= BT_OBJECT_STATUS_INVAL
;
712 *val
= typed_integer_obj
->value
;
718 enum bt_object_status
bt_object_integer_set(struct bt_object
*integer_obj
,
721 enum bt_object_status ret
= BT_OBJECT_STATUS_OK
;
722 struct bt_object_integer
*typed_integer_obj
=
723 BT_OBJECT_TO_INTEGER(integer_obj
);
725 if (!integer_obj
|| !bt_object_is_integer(integer_obj
)) {
726 ret
= BT_OBJECT_STATUS_INVAL
;
730 if (integer_obj
->is_frozen
) {
731 ret
= BT_OBJECT_STATUS_FROZEN
;
735 typed_integer_obj
->value
= val
;
741 enum bt_object_status
bt_object_float_get(const struct bt_object
*float_obj
,
744 enum bt_object_status ret
= BT_OBJECT_STATUS_OK
;
745 struct bt_object_float
*typed_float_obj
=
746 BT_OBJECT_TO_FLOAT(float_obj
);
748 if (!float_obj
|| !bt_object_is_float(float_obj
) || !val
) {
749 ret
= BT_OBJECT_STATUS_INVAL
;
753 *val
= typed_float_obj
->value
;
759 enum bt_object_status
bt_object_float_set(struct bt_object
*float_obj
,
762 enum bt_object_status ret
= BT_OBJECT_STATUS_OK
;
763 struct bt_object_float
*typed_float_obj
=
764 BT_OBJECT_TO_FLOAT(float_obj
);
766 if (!float_obj
|| !bt_object_is_float(float_obj
)) {
767 ret
= BT_OBJECT_STATUS_INVAL
;
771 if (float_obj
->is_frozen
) {
772 ret
= BT_OBJECT_STATUS_FROZEN
;
776 typed_float_obj
->value
= val
;
782 enum bt_object_status
bt_object_string_get(const struct bt_object
*string_obj
,
785 enum bt_object_status ret
= BT_OBJECT_STATUS_OK
;
786 struct bt_object_string
*typed_string_obj
=
787 BT_OBJECT_TO_STRING(string_obj
);
789 if (!string_obj
|| !bt_object_is_string(string_obj
) || !val
) {
790 ret
= BT_OBJECT_STATUS_INVAL
;
794 *val
= typed_string_obj
->gstr
->str
;
800 enum bt_object_status
bt_object_string_set(struct bt_object
*string_obj
,
803 enum bt_object_status ret
= BT_OBJECT_STATUS_OK
;
804 struct bt_object_string
*typed_string_obj
=
805 BT_OBJECT_TO_STRING(string_obj
);
807 if (!string_obj
|| !bt_object_is_string(string_obj
) || !val
) {
808 ret
= BT_OBJECT_STATUS_INVAL
;
812 if (string_obj
->is_frozen
) {
813 ret
= BT_OBJECT_STATUS_FROZEN
;
817 g_string_assign(typed_string_obj
->gstr
, val
);
823 int bt_object_array_size(const struct bt_object
*array_obj
)
826 struct bt_object_array
*typed_array_obj
=
827 BT_OBJECT_TO_ARRAY(array_obj
);
829 if (!array_obj
|| !bt_object_is_array(array_obj
)) {
830 ret
= BT_OBJECT_STATUS_INVAL
;
834 ret
= (int) typed_array_obj
->garray
->len
;
840 bool bt_object_array_is_empty(const struct bt_object
*array_obj
)
842 return bt_object_array_size(array_obj
) == 0;
845 struct bt_object
*bt_object_array_get(const struct bt_object
*array_obj
,
848 struct bt_object
*ret
;
849 struct bt_object_array
*typed_array_obj
=
850 BT_OBJECT_TO_ARRAY(array_obj
);
852 if (!array_obj
|| !bt_object_is_array(array_obj
) ||
853 index
>= typed_array_obj
->garray
->len
) {
858 ret
= g_ptr_array_index(typed_array_obj
->garray
, index
);
865 enum bt_object_status
bt_object_array_append(struct bt_object
*array_obj
,
866 struct bt_object
*element_obj
)
868 enum bt_object_status ret
= BT_OBJECT_STATUS_OK
;
869 struct bt_object_array
*typed_array_obj
=
870 BT_OBJECT_TO_ARRAY(array_obj
);
872 if (!array_obj
|| !bt_object_is_array(array_obj
) || !element_obj
) {
873 ret
= BT_OBJECT_STATUS_INVAL
;
877 if (array_obj
->is_frozen
) {
878 ret
= BT_OBJECT_STATUS_FROZEN
;
882 g_ptr_array_add(typed_array_obj
->garray
, element_obj
);
883 bt_object_get(element_obj
);
889 enum bt_object_status
bt_object_array_append_bool(struct bt_object
*array_obj
,
892 enum bt_object_status ret
;
893 struct bt_object
*bool_obj
= NULL
;
895 bool_obj
= bt_object_bool_create_init(val
);
896 ret
= bt_object_array_append(array_obj
, bool_obj
);
897 bt_object_put(bool_obj
);
902 enum bt_object_status
bt_object_array_append_integer(
903 struct bt_object
*array_obj
, int64_t val
)
905 enum bt_object_status ret
;
906 struct bt_object
*integer_obj
= NULL
;
908 integer_obj
= bt_object_integer_create_init(val
);
909 ret
= bt_object_array_append(array_obj
, integer_obj
);
910 bt_object_put(integer_obj
);
915 enum bt_object_status
bt_object_array_append_float(struct bt_object
*array_obj
,
918 enum bt_object_status ret
;
919 struct bt_object
*float_obj
= NULL
;
921 float_obj
= bt_object_float_create_init(val
);
922 ret
= bt_object_array_append(array_obj
, float_obj
);
923 bt_object_put(float_obj
);
928 enum bt_object_status
bt_object_array_append_string(struct bt_object
*array_obj
,
931 enum bt_object_status ret
;
932 struct bt_object
*string_obj
= NULL
;
934 string_obj
= bt_object_string_create_init(val
);
935 ret
= bt_object_array_append(array_obj
, string_obj
);
936 bt_object_put(string_obj
);
941 enum bt_object_status
bt_object_array_append_array(struct bt_object
*array_obj
)
943 enum bt_object_status ret
;
944 struct bt_object
*empty_array_obj
= NULL
;
946 empty_array_obj
= bt_object_array_create();
947 ret
= bt_object_array_append(array_obj
, empty_array_obj
);
948 bt_object_put(empty_array_obj
);
953 enum bt_object_status
bt_object_array_append_map(struct bt_object
*array_obj
)
955 enum bt_object_status ret
;
956 struct bt_object
*map_obj
= NULL
;
958 map_obj
= bt_object_map_create();
959 ret
= bt_object_array_append(array_obj
, map_obj
);
960 bt_object_put(map_obj
);
965 enum bt_object_status
bt_object_array_set(struct bt_object
*array_obj
,
966 size_t index
, struct bt_object
*element_obj
)
968 enum bt_object_status ret
= BT_OBJECT_STATUS_OK
;
969 struct bt_object_array
*typed_array_obj
=
970 BT_OBJECT_TO_ARRAY(array_obj
);
972 if (!array_obj
|| !bt_object_is_array(array_obj
) || !element_obj
||
973 index
>= typed_array_obj
->garray
->len
) {
974 ret
= BT_OBJECT_STATUS_INVAL
;
978 if (array_obj
->is_frozen
) {
979 ret
= BT_OBJECT_STATUS_FROZEN
;
983 bt_object_put(g_ptr_array_index(typed_array_obj
->garray
, index
));
984 g_ptr_array_index(typed_array_obj
->garray
, index
) = element_obj
;
985 bt_object_get(element_obj
);
991 int bt_object_map_size(const struct bt_object
*map_obj
)
994 struct bt_object_map
*typed_map_obj
= BT_OBJECT_TO_MAP(map_obj
);
996 if (!map_obj
|| !bt_object_is_map(map_obj
)) {
997 ret
= BT_OBJECT_STATUS_INVAL
;
1001 ret
= (int) g_hash_table_size(typed_map_obj
->ght
);
1007 bool bt_object_map_is_empty(const struct bt_object
*map_obj
)
1009 return bt_object_map_size(map_obj
) == 0;
1012 struct bt_object
*bt_object_map_get(const struct bt_object
*map_obj
,
1016 struct bt_object
*ret
;
1017 struct bt_object_map
*typed_map_obj
= BT_OBJECT_TO_MAP(map_obj
);
1019 if (!map_obj
|| !bt_object_is_map(map_obj
) || !key
) {
1024 quark
= g_quark_from_string(key
);
1025 ret
= g_hash_table_lookup(typed_map_obj
->ght
, GUINT_TO_POINTER(quark
));
1035 bool bt_object_map_has_key(const struct bt_object
*map_obj
, const char *key
)
1039 struct bt_object_map
*typed_map_obj
= BT_OBJECT_TO_MAP(map_obj
);
1041 if (!map_obj
|| !bt_object_is_map(map_obj
) || !key
) {
1046 quark
= g_quark_from_string(key
);
1047 ret
= g_hash_table_contains(typed_map_obj
->ght
,
1048 GUINT_TO_POINTER(quark
));
1054 enum bt_object_status
bt_object_map_insert(struct bt_object
*map_obj
,
1055 const char *key
, struct bt_object
*element_obj
)
1058 enum bt_object_status ret
= BT_OBJECT_STATUS_OK
;
1059 struct bt_object_map
*typed_map_obj
= BT_OBJECT_TO_MAP(map_obj
);
1061 if (!map_obj
|| !bt_object_is_map(map_obj
) || !key
|| !element_obj
) {
1062 ret
= BT_OBJECT_STATUS_INVAL
;
1066 if (map_obj
->is_frozen
) {
1067 ret
= BT_OBJECT_STATUS_FROZEN
;
1071 quark
= g_quark_from_string(key
);
1072 g_hash_table_insert(typed_map_obj
->ght
,
1073 GUINT_TO_POINTER(quark
), element_obj
);
1074 bt_object_get(element_obj
);
1080 enum bt_object_status
bt_object_map_insert_bool(struct bt_object
*map_obj
,
1081 const char *key
, bool val
)
1083 enum bt_object_status ret
;
1084 struct bt_object
*bool_obj
= NULL
;
1086 bool_obj
= bt_object_bool_create_init(val
);
1087 ret
= bt_object_map_insert(map_obj
, key
, bool_obj
);
1088 bt_object_put(bool_obj
);
1093 enum bt_object_status
bt_object_map_insert_integer(struct bt_object
*map_obj
,
1094 const char *key
, int64_t val
)
1096 enum bt_object_status ret
;
1097 struct bt_object
*integer_obj
= NULL
;
1099 integer_obj
= bt_object_integer_create_init(val
);
1100 ret
= bt_object_map_insert(map_obj
, key
, integer_obj
);
1101 bt_object_put(integer_obj
);
1106 enum bt_object_status
bt_object_map_insert_float(struct bt_object
*map_obj
,
1107 const char *key
, double val
)
1109 enum bt_object_status ret
;
1110 struct bt_object
*float_obj
= NULL
;
1112 float_obj
= bt_object_float_create_init(val
);
1113 ret
= bt_object_map_insert(map_obj
, key
, float_obj
);
1114 bt_object_put(float_obj
);
1119 enum bt_object_status
bt_object_map_insert_string(struct bt_object
*map_obj
,
1120 const char *key
, const char *val
)
1122 enum bt_object_status ret
;
1123 struct bt_object
*string_obj
= NULL
;
1125 string_obj
= bt_object_string_create_init(val
);
1126 ret
= bt_object_map_insert(map_obj
, key
, string_obj
);
1127 bt_object_put(string_obj
);
1132 enum bt_object_status
bt_object_map_insert_array(struct bt_object
*map_obj
,
1135 enum bt_object_status ret
;
1136 struct bt_object
*array_obj
= NULL
;
1138 array_obj
= bt_object_array_create();
1139 ret
= bt_object_map_insert(map_obj
, key
, array_obj
);
1140 bt_object_put(array_obj
);
1145 enum bt_object_status
bt_object_map_insert_map(struct bt_object
*map_obj
,
1148 enum bt_object_status ret
;
1149 struct bt_object
*empty_map_obj
= NULL
;
1151 empty_map_obj
= bt_object_map_create();
1152 ret
= bt_object_map_insert(map_obj
, key
, empty_map_obj
);
1153 bt_object_put(empty_map_obj
);
1158 enum bt_object_status
bt_object_map_foreach(const struct bt_object
*map_obj
,
1159 bt_object_map_foreach_cb cb
, void *data
)
1161 enum bt_object_status ret
= BT_OBJECT_STATUS_OK
;
1162 gpointer key
, element_obj
;
1163 GHashTableIter iter
;
1164 struct bt_object_map
*typed_map_obj
= BT_OBJECT_TO_MAP(map_obj
);
1166 if (!map_obj
|| !bt_object_is_map(map_obj
) || !cb
) {
1167 ret
= BT_OBJECT_STATUS_INVAL
;
1171 g_hash_table_iter_init(&iter
, typed_map_obj
->ght
);
1173 while (g_hash_table_iter_next(&iter
, &key
, &element_obj
)) {
1174 const char *key_str
= g_quark_to_string((unsigned long) key
);
1176 if (!cb(key_str
, element_obj
, data
)) {
1177 ret
= BT_OBJECT_STATUS_CANCELLED
;
1186 struct bt_object
*bt_object_copy(const struct bt_object
*object
)
1188 struct bt_object
*copy_obj
= NULL
;
1194 copy_obj
= copy_funcs
[object
->type
](object
);
1200 bool bt_object_compare(const struct bt_object
*object_a
,
1201 const struct bt_object
*object_b
)
1205 if (!object_a
|| !object_b
) {
1209 if (object_a
->type
!= object_b
->type
) {
1213 ret
= compare_funcs
[object_a
->type
](object_a
, object_b
);