ISO C: empty file needs at least one declaration
[babeltrace.git] / lib / values.c
CommitLineData
dac5c838 1/*
83509119 2 * Values.c: value objects
dac5c838
PP
3 *
4 * Babeltrace Library
5 *
6 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
7 * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
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
25 * SOFTWARE.
26 */
27
28#include <stdlib.h>
29#include <string.h>
30#include <assert.h>
31#include <string.h>
3d9990ac 32#include <babeltrace/compiler-internal.h>
83509119
JG
33#include <babeltrace/object-internal.h>
34#include <babeltrace/ref.h>
dac5c838 35#include <babeltrace/values.h>
3d9990ac 36#include <babeltrace/compat/glib-internal.h>
dac5c838
PP
37
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))
45
46struct bt_value {
83509119 47 struct bt_object base;
dac5c838 48 enum bt_value_type type;
dac5c838
PP
49 bool is_frozen;
50};
51
52static
53struct bt_value bt_value_null_instance = {
f685129c
PP
54 .base = {
55 .ref_count = {
56 .count = 1,
57 .release = NULL,
58 },
59 .release = NULL,
60 .parent = NULL,
61 },
dac5c838
PP
62 .type = BT_VALUE_TYPE_NULL,
63 .is_frozen = true,
64};
65
66struct bt_value *bt_value_null = &bt_value_null_instance;
67
68struct bt_value_bool {
69 struct bt_value base;
70 bool value;
71};
72
73struct bt_value_integer {
74 struct bt_value base;
75 int64_t value;
76};
77
78struct bt_value_float {
79 struct bt_value base;
80 double value;
81};
82
83struct bt_value_string {
84 struct bt_value base;
85 GString *gstr;
86};
87
88struct bt_value_array {
89 struct bt_value base;
90 GPtrArray *garray;
91};
92
93struct bt_value_map {
94 struct bt_value base;
95 GHashTable *ght;
96};
97
98static
83509119 99void bt_value_destroy(struct bt_object *obj);
dac5c838
PP
100
101static
102void bt_value_string_destroy(struct bt_value *object)
103{
104 g_string_free(BT_VALUE_TO_STRING(object)->gstr, TRUE);
105}
106
107static
108void bt_value_array_destroy(struct bt_value *object)
109{
110 /*
111 * Pointer array's registered value destructor will take care
112 * of putting each contained object.
113 */
114 g_ptr_array_free(BT_VALUE_TO_ARRAY(object)->garray, TRUE);
115}
116
117static
118void bt_value_map_destroy(struct bt_value *object)
119{
120 /*
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.
124 */
125 g_hash_table_destroy(BT_VALUE_TO_MAP(object)->ght);
126}
127
128static
129void (* 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,
137};
138
139static
140struct bt_value *bt_value_null_copy(const struct bt_value *null_obj)
141{
142 return bt_value_null;
143}
144
145static
146struct bt_value *bt_value_bool_copy(const struct bt_value *bool_obj)
147{
148 return bt_value_bool_create_init(BT_VALUE_TO_BOOL(bool_obj)->value);
149}
150
151static
152struct bt_value *bt_value_integer_copy(const struct bt_value *integer_obj)
153{
154 return bt_value_integer_create_init(
155 BT_VALUE_TO_INTEGER(integer_obj)->value);
156}
157
158static
159struct bt_value *bt_value_float_copy(const struct bt_value *float_obj)
160{
161 return bt_value_float_create_init(
162 BT_VALUE_TO_FLOAT(float_obj)->value);
163}
164
165static
166struct bt_value *bt_value_string_copy(const struct bt_value *string_obj)
167{
168 return bt_value_string_create_init(
169 BT_VALUE_TO_STRING(string_obj)->gstr->str);
170}
171
172static
173struct bt_value *bt_value_array_copy(const struct bt_value *array_obj)
174{
175 int i;
176 int ret;
177 struct bt_value *copy_obj;
178 struct bt_value_array *typed_array_obj;
179
180 typed_array_obj = BT_VALUE_TO_ARRAY(array_obj);
181 copy_obj = bt_value_array_create();
182
183 if (!copy_obj) {
184 goto end;
185 }
186
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);
190
191 if (!element_obj) {
83509119 192 BT_PUT(copy_obj);
dac5c838
PP
193 goto end;
194 }
195
196 element_obj_copy = bt_value_copy(element_obj);
83509119 197 BT_PUT(element_obj);
dac5c838
PP
198
199 if (!element_obj_copy) {
83509119 200 BT_PUT(copy_obj);
dac5c838
PP
201 goto end;
202 }
203
204 ret = bt_value_array_append(copy_obj, element_obj_copy);
83509119 205 BT_PUT(element_obj_copy);
dac5c838
PP
206
207 if (ret) {
83509119 208 BT_PUT(copy_obj);
dac5c838
PP
209 goto end;
210 }
211 }
212
213end:
214 return copy_obj;
215}
216
217static
218struct bt_value *bt_value_map_copy(const struct bt_value *map_obj)
219{
220 int ret;
221 GHashTableIter iter;
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;
226
227 typed_map_obj = BT_VALUE_TO_MAP(map_obj);
228 copy_obj = bt_value_map_create();
229
230 if (!copy_obj) {
231 goto end;
232 }
233
234 g_hash_table_iter_init(&iter, typed_map_obj->ght);
235
236 while (g_hash_table_iter_next(&iter, &key, &element_obj)) {
5b44aff2 237 const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key));
dac5c838
PP
238
239 element_obj_copy = bt_value_copy(element_obj);
240
241 if (!element_obj_copy) {
83509119 242 BT_PUT(copy_obj);
dac5c838
PP
243 goto end;
244 }
245
246 ret = bt_value_map_insert(copy_obj, key_str, element_obj_copy);
83509119 247 BT_PUT(element_obj_copy);
dac5c838
PP
248
249 if (ret) {
83509119 250 BT_PUT(copy_obj);
dac5c838
PP
251 goto end;
252 }
253 }
254
255end:
256 return copy_obj;
257}
258
259static
260struct 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,
268};
269
270static
271bool bt_value_null_compare(const struct bt_value *object_a,
272 const struct bt_value *object_b)
273{
274 /*
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.
278 */
279 return true;
280}
281
282static
283bool bt_value_bool_compare(const struct bt_value *object_a,
284 const struct bt_value *object_b)
285{
286 return BT_VALUE_TO_BOOL(object_a)->value ==
287 BT_VALUE_TO_BOOL(object_b)->value;
288}
289
290static
291bool bt_value_integer_compare(const struct bt_value *object_a,
292 const struct bt_value *object_b)
293{
294 return BT_VALUE_TO_INTEGER(object_a)->value ==
295 BT_VALUE_TO_INTEGER(object_b)->value;
296}
297
298static
299bool bt_value_float_compare(const struct bt_value *object_a,
300 const struct bt_value *object_b)
301{
302 return BT_VALUE_TO_FLOAT(object_a)->value ==
303 BT_VALUE_TO_FLOAT(object_b)->value;
304}
305
306static
307bool bt_value_string_compare(const struct bt_value *object_a,
308 const struct bt_value *object_b)
309{
310 return !strcmp(BT_VALUE_TO_STRING(object_a)->gstr->str,
311 BT_VALUE_TO_STRING(object_b)->gstr->str);
312}
313
314static
315bool bt_value_array_compare(const struct bt_value *object_a,
316 const struct bt_value *object_b)
317{
318 int i;
319 bool ret = true;
320 const struct bt_value_array *array_obj_a =
321 BT_VALUE_TO_ARRAY(object_a);
322
323 if (bt_value_array_size(object_a) != bt_value_array_size(object_b)) {
324 ret = false;
325 goto end;
326 }
327
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;
331
332 element_obj_a = bt_value_array_get(object_a, i);
333 element_obj_b = bt_value_array_get(object_b, i);
334
335 if (!bt_value_compare(element_obj_a, element_obj_b)) {
83509119
JG
336 BT_PUT(element_obj_a);
337 BT_PUT(element_obj_b);
dac5c838
PP
338 ret = false;
339 goto end;
340 }
341
83509119
JG
342 BT_PUT(element_obj_a);
343 BT_PUT(element_obj_b);
dac5c838
PP
344 }
345
346end:
347 return ret;
348}
349
350static
351bool bt_value_map_compare(const struct bt_value *object_a,
352 const struct bt_value *object_b)
353{
354 bool ret = true;
355 GHashTableIter iter;
356 gpointer key, element_obj_a;
357 const struct bt_value_map *map_obj_a = BT_VALUE_TO_MAP(object_a);
358
359 if (bt_value_map_size(object_a) != bt_value_map_size(object_b)) {
360 ret = false;
361 goto end;
362 }
363
364 g_hash_table_iter_init(&iter, map_obj_a->ght);
365
366 while (g_hash_table_iter_next(&iter, &key, &element_obj_a)) {
367 struct bt_value *element_obj_b;
5b44aff2 368 const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key));
dac5c838
PP
369
370 element_obj_b = bt_value_map_get(object_b, key_str);
371
372 if (!bt_value_compare(element_obj_a, element_obj_b)) {
83509119 373 BT_PUT(element_obj_b);
dac5c838
PP
374 ret = false;
375 goto end;
376 }
377
83509119 378 BT_PUT(element_obj_b);
dac5c838
PP
379 }
380
381end:
382 return ret;
383}
384
385static
386bool (* 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,
395};
396
397void bt_value_null_freeze(struct bt_value *object)
398{
399}
400
401void bt_value_generic_freeze(struct bt_value *object)
402{
403 object->is_frozen = true;
404}
405
406void bt_value_array_freeze(struct bt_value *object)
407{
408 int i;
409 struct bt_value_array *typed_array_obj =
410 BT_VALUE_TO_ARRAY(object);
411
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);
415
416 bt_value_freeze(element_obj);
417 }
418
419 bt_value_generic_freeze(object);
420}
421
422void bt_value_map_freeze(struct bt_value *object)
423{
424 GHashTableIter iter;
425 gpointer key, element_obj;
426 const struct bt_value_map *map_obj = BT_VALUE_TO_MAP(object);
427
428 g_hash_table_iter_init(&iter, map_obj->ght);
429
430 while (g_hash_table_iter_next(&iter, &key, &element_obj)) {
431 bt_value_freeze(element_obj);
432 }
433
434 bt_value_generic_freeze(object);
435}
436
437static
438void (* 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,
446};
447
448static
83509119 449void bt_value_destroy(struct bt_object *obj)
dac5c838 450{
83509119 451 struct bt_value *value;
dac5c838 452
83509119
JG
453 value = container_of(obj, struct bt_value, base);
454 assert(value->type != BT_VALUE_TYPE_UNKNOWN);
dac5c838 455
83509119 456 if (bt_value_is_null(value)) {
dac5c838
PP
457 return;
458 }
459
83509119
JG
460 if (destroy_funcs[value->type]) {
461 destroy_funcs[value->type](value);
dac5c838
PP
462 }
463
83509119 464 g_free(value);
dac5c838
PP
465}
466
467enum bt_value_status bt_value_freeze(struct bt_value *object)
468{
469 enum bt_value_status ret = BT_VALUE_STATUS_OK;
470
471 if (!object) {
472 ret = BT_VALUE_STATUS_INVAL;
473 goto end;
474 }
475
476 freeze_funcs[object->type](object);
477
478end:
479 return ret;
480}
481
482bool bt_value_is_frozen(const struct bt_value *object)
483{
484 return object && object->is_frozen;
485}
486
487enum bt_value_type bt_value_get_type(const struct bt_value *object)
488{
489 if (!object) {
490 return BT_VALUE_TYPE_UNKNOWN;
491 }
492
493 return object->type;
494}
495
496static
497struct bt_value bt_value_create_base(enum bt_value_type type)
498{
499 struct bt_value base;
500
501 base.type = type;
502 base.is_frozen = false;
83509119 503 bt_object_init(&base, bt_value_destroy);
dac5c838
PP
504
505 return base;
506}
507
508struct bt_value *bt_value_bool_create_init(bool val)
509{
510 struct bt_value_bool *bool_obj;
511
512 bool_obj = g_new0(struct bt_value_bool, 1);
513
514 if (!bool_obj) {
515 goto end;
516 }
517
518 bool_obj->base = bt_value_create_base(BT_VALUE_TYPE_BOOL);
519 bool_obj->value = val;
520
521end:
522 return BT_VALUE_FROM_CONCRETE(bool_obj);
523}
524
525struct bt_value *bt_value_bool_create(void)
526{
527 return bt_value_bool_create_init(false);
528}
529
530struct bt_value *bt_value_integer_create_init(int64_t val)
531{
532 struct bt_value_integer *integer_obj;
533
534 integer_obj = g_new0(struct bt_value_integer, 1);
535
536 if (!integer_obj) {
537 goto end;
538 }
539
540 integer_obj->base = bt_value_create_base(BT_VALUE_TYPE_INTEGER);
541 integer_obj->value = val;
542
543end:
544 return BT_VALUE_FROM_CONCRETE(integer_obj);
545}
546
547struct bt_value *bt_value_integer_create(void)
548{
549 return bt_value_integer_create_init(0);
550}
551
552struct bt_value *bt_value_float_create_init(double val)
553{
554 struct bt_value_float *float_obj;
555
556 float_obj = g_new0(struct bt_value_float, 1);
557
558 if (!float_obj) {
559 goto end;
560 }
561
562 float_obj->base = bt_value_create_base(BT_VALUE_TYPE_FLOAT);
563 float_obj->value = val;
564
565end:
566 return BT_VALUE_FROM_CONCRETE(float_obj);
567}
568
569struct bt_value *bt_value_float_create(void)
570{
571 return bt_value_float_create_init(0.);
572}
573
574struct bt_value *bt_value_string_create_init(const char *val)
575{
576 struct bt_value_string *string_obj = NULL;
577
578 if (!val) {
579 goto end;
580 }
581
582 string_obj = g_new0(struct bt_value_string, 1);
583
584 if (!string_obj) {
585 goto end;
586 }
587
588 string_obj->base = bt_value_create_base(BT_VALUE_TYPE_STRING);
589 string_obj->gstr = g_string_new(val);
590
591 if (!string_obj->gstr) {
592 g_free(string_obj);
593 string_obj = NULL;
594 goto end;
595 }
596
597end:
598 return BT_VALUE_FROM_CONCRETE(string_obj);
599}
600
601struct bt_value *bt_value_string_create(void)
602{
603 return bt_value_string_create_init("");
604}
605
606struct bt_value *bt_value_array_create(void)
607{
608 struct bt_value_array *array_obj;
609
610 array_obj = g_new0(struct bt_value_array, 1);
611
612 if (!array_obj) {
613 goto end;
614 }
615
616 array_obj->base = bt_value_create_base(BT_VALUE_TYPE_ARRAY);
5d5982ab 617 array_obj->garray = bt_g_ptr_array_new_full(0,
83509119 618 (GDestroyNotify) bt_put);
dac5c838
PP
619
620 if (!array_obj->garray) {
621 g_free(array_obj);
622 array_obj = NULL;
623 goto end;
624 }
625
626end:
627 return BT_VALUE_FROM_CONCRETE(array_obj);
628}
629
630struct bt_value *bt_value_map_create(void)
631{
632 struct bt_value_map *map_obj;
633
634 map_obj = g_new0(struct bt_value_map, 1);
635
636 if (!map_obj) {
637 goto end;
638 }
639
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,
83509119 642 NULL, (GDestroyNotify) bt_put);
dac5c838
PP
643
644 if (!map_obj->ght) {
645 g_free(map_obj);
646 map_obj = NULL;
647 goto end;
648 }
649
650end:
651 return BT_VALUE_FROM_CONCRETE(map_obj);
652}
653
654enum bt_value_status bt_value_bool_get(const struct bt_value *bool_obj,
364747d6 655 bool *val)
dac5c838
PP
656{
657 enum bt_value_status ret = BT_VALUE_STATUS_OK;
658 struct bt_value_bool *typed_bool_obj = BT_VALUE_TO_BOOL(bool_obj);
659
660 if (!bool_obj || !bt_value_is_bool(bool_obj) || !val) {
661 ret = BT_VALUE_STATUS_INVAL;
662 goto end;
663 }
664
665 *val = typed_bool_obj->value;
666
667end:
668 return ret;
669}
670
671enum bt_value_status bt_value_bool_set(struct bt_value *bool_obj, bool val)
672{
673 enum bt_value_status ret = BT_VALUE_STATUS_OK;
674 struct bt_value_bool *typed_bool_obj = BT_VALUE_TO_BOOL(bool_obj);
675
676 if (!bool_obj || !bt_value_is_bool(bool_obj)) {
677 ret = BT_VALUE_STATUS_INVAL;
678 goto end;
679 }
680
681 if (bool_obj->is_frozen) {
682 ret = BT_VALUE_STATUS_FROZEN;
683 goto end;
684 }
685
686 typed_bool_obj->value = val;
687
688end:
689 return ret;
690}
691
692enum bt_value_status bt_value_integer_get(const struct bt_value *integer_obj,
364747d6 693 int64_t *val)
dac5c838
PP
694{
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);
698
699 if (!integer_obj || !bt_value_is_integer(integer_obj) || !val) {
700 ret = BT_VALUE_STATUS_INVAL;
701 goto end;
702 }
703
704 *val = typed_integer_obj->value;
705
706end:
707 return ret;
708}
709
710enum bt_value_status bt_value_integer_set(struct bt_value *integer_obj,
364747d6 711 int64_t val)
dac5c838
PP
712{
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);
716
717 if (!integer_obj || !bt_value_is_integer(integer_obj)) {
718 ret = BT_VALUE_STATUS_INVAL;
719 goto end;
720 }
721
722 if (integer_obj->is_frozen) {
723 ret = BT_VALUE_STATUS_FROZEN;
724 goto end;
725 }
726
727 typed_integer_obj->value = val;
728
729end:
730 return ret;
731}
732
733enum bt_value_status bt_value_float_get(const struct bt_value *float_obj,
364747d6 734 double *val)
dac5c838
PP
735{
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);
739
740 if (!float_obj || !bt_value_is_float(float_obj) || !val) {
741 ret = BT_VALUE_STATUS_INVAL;
742 goto end;
743 }
744
745 *val = typed_float_obj->value;
746
747end:
748 return ret;
749}
750
751enum bt_value_status bt_value_float_set(struct bt_value *float_obj,
364747d6 752 double val)
dac5c838
PP
753{
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);
757
758 if (!float_obj || !bt_value_is_float(float_obj)) {
759 ret = BT_VALUE_STATUS_INVAL;
760 goto end;
761 }
762
763 if (float_obj->is_frozen) {
764 ret = BT_VALUE_STATUS_FROZEN;
765 goto end;
766 }
767
768 typed_float_obj->value = val;
769
770end:
771 return ret;
772}
773
774enum bt_value_status bt_value_string_get(const struct bt_value *string_obj,
364747d6 775 const char **val)
dac5c838
PP
776{
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);
780
781 if (!string_obj || !bt_value_is_string(string_obj) || !val) {
782 ret = BT_VALUE_STATUS_INVAL;
783 goto end;
784 }
785
786 *val = typed_string_obj->gstr->str;
787
788end:
789 return ret;
790}
791
792enum bt_value_status bt_value_string_set(struct bt_value *string_obj,
364747d6 793 const char *val)
dac5c838
PP
794{
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);
798
799 if (!string_obj || !bt_value_is_string(string_obj) || !val) {
800 ret = BT_VALUE_STATUS_INVAL;
801 goto end;
802 }
803
804 if (string_obj->is_frozen) {
805 ret = BT_VALUE_STATUS_FROZEN;
806 goto end;
807 }
808
809 g_string_assign(typed_string_obj->gstr, val);
810
811end:
812 return ret;
813}
814
815int bt_value_array_size(const struct bt_value *array_obj)
816{
817 int ret;
818 struct bt_value_array *typed_array_obj =
819 BT_VALUE_TO_ARRAY(array_obj);
820
821 if (!array_obj || !bt_value_is_array(array_obj)) {
822 ret = BT_VALUE_STATUS_INVAL;
823 goto end;
824 }
825
826 ret = (int) typed_array_obj->garray->len;
827
828end:
829 return ret;
830}
831
832bool bt_value_array_is_empty(const struct bt_value *array_obj)
833{
834 return bt_value_array_size(array_obj) == 0;
835}
836
837struct bt_value *bt_value_array_get(const struct bt_value *array_obj,
364747d6 838 size_t index)
dac5c838
PP
839{
840 struct bt_value *ret;
841 struct bt_value_array *typed_array_obj =
842 BT_VALUE_TO_ARRAY(array_obj);
843
844 if (!array_obj || !bt_value_is_array(array_obj) ||
845 index >= typed_array_obj->garray->len) {
846 ret = NULL;
847 goto end;
848 }
849
850 ret = g_ptr_array_index(typed_array_obj->garray, index);
83509119 851 bt_get(ret);
dac5c838
PP
852
853end:
854 return ret;
855}
856
857enum bt_value_status bt_value_array_append(struct bt_value *array_obj,
364747d6 858 struct bt_value *element_obj)
dac5c838
PP
859{
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);
863
864 if (!array_obj || !bt_value_is_array(array_obj) || !element_obj) {
865 ret = BT_VALUE_STATUS_INVAL;
866 goto end;
867 }
868
869 if (array_obj->is_frozen) {
870 ret = BT_VALUE_STATUS_FROZEN;
871 goto end;
872 }
873
874 g_ptr_array_add(typed_array_obj->garray, element_obj);
83509119 875 bt_get(element_obj);
dac5c838
PP
876
877end:
878 return ret;
879}
880
881enum bt_value_status bt_value_array_append_bool(struct bt_value *array_obj,
364747d6 882 bool val)
dac5c838
PP
883{
884 enum bt_value_status ret;
885 struct bt_value *bool_obj = NULL;
886
887 bool_obj = bt_value_bool_create_init(val);
888 ret = bt_value_array_append(array_obj, bool_obj);
83509119 889 bt_put(bool_obj);
dac5c838
PP
890
891 return ret;
892}
893
894enum bt_value_status bt_value_array_append_integer(
364747d6 895 struct bt_value *array_obj, int64_t val)
dac5c838
PP
896{
897 enum bt_value_status ret;
898 struct bt_value *integer_obj = NULL;
899
900 integer_obj = bt_value_integer_create_init(val);
901 ret = bt_value_array_append(array_obj, integer_obj);
83509119 902 bt_put(integer_obj);
dac5c838
PP
903
904 return ret;
905}
906
907enum bt_value_status bt_value_array_append_float(struct bt_value *array_obj,
364747d6 908 double val)
dac5c838
PP
909{
910 enum bt_value_status ret;
911 struct bt_value *float_obj = NULL;
912
913 float_obj = bt_value_float_create_init(val);
914 ret = bt_value_array_append(array_obj, float_obj);
83509119 915 bt_put(float_obj);
dac5c838
PP
916
917 return ret;
918}
919
920enum bt_value_status bt_value_array_append_string(struct bt_value *array_obj,
364747d6 921 const char *val)
dac5c838
PP
922{
923 enum bt_value_status ret;
924 struct bt_value *string_obj = NULL;
925
926 string_obj = bt_value_string_create_init(val);
927 ret = bt_value_array_append(array_obj, string_obj);
83509119 928 bt_put(string_obj);
dac5c838
PP
929
930 return ret;
931}
932
5b79e8bf 933enum bt_value_status bt_value_array_append_empty_array(
364747d6 934 struct bt_value *array_obj)
dac5c838
PP
935{
936 enum bt_value_status ret;
937 struct bt_value *empty_array_obj = NULL;
938
939 empty_array_obj = bt_value_array_create();
940 ret = bt_value_array_append(array_obj, empty_array_obj);
83509119 941 bt_put(empty_array_obj);
dac5c838
PP
942
943 return ret;
944}
945
5b79e8bf 946enum bt_value_status bt_value_array_append_empty_map(struct bt_value *array_obj)
dac5c838
PP
947{
948 enum bt_value_status ret;
949 struct bt_value *map_obj = NULL;
950
951 map_obj = bt_value_map_create();
952 ret = bt_value_array_append(array_obj, map_obj);
83509119 953 bt_put(map_obj);
dac5c838
PP
954
955 return ret;
956}
957
958enum bt_value_status bt_value_array_set(struct bt_value *array_obj,
364747d6 959 size_t index, struct bt_value *element_obj)
dac5c838
PP
960{
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);
964
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;
968 goto end;
969 }
970
971 if (array_obj->is_frozen) {
972 ret = BT_VALUE_STATUS_FROZEN;
973 goto end;
974 }
975
83509119 976 bt_put(g_ptr_array_index(typed_array_obj->garray, index));
dac5c838 977 g_ptr_array_index(typed_array_obj->garray, index) = element_obj;
83509119 978 bt_get(element_obj);
dac5c838
PP
979
980end:
981 return ret;
982}
983
984int bt_value_map_size(const struct bt_value *map_obj)
985{
986 int ret;
987 struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj);
988
989 if (!map_obj || !bt_value_is_map(map_obj)) {
990 ret = BT_VALUE_STATUS_INVAL;
991 goto end;
992 }
993
994 ret = (int) g_hash_table_size(typed_map_obj->ght);
995
996end:
997 return ret;
998}
999
1000bool bt_value_map_is_empty(const struct bt_value *map_obj)
1001{
1002 return bt_value_map_size(map_obj) == 0;
1003}
1004
1005struct bt_value *bt_value_map_get(const struct bt_value *map_obj,
364747d6 1006 const char *key)
dac5c838
PP
1007{
1008 GQuark quark;
1009 struct bt_value *ret;
1010 struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj);
1011
1012 if (!map_obj || !bt_value_is_map(map_obj) || !key) {
1013 ret = NULL;
1014 goto end;
1015 }
1016
1017 quark = g_quark_from_string(key);
1018 ret = g_hash_table_lookup(typed_map_obj->ght, GUINT_TO_POINTER(quark));
1019
1020 if (ret) {
83509119 1021 bt_get(ret);
dac5c838
PP
1022 }
1023
1024end:
1025 return ret;
1026}
1027
1028bool bt_value_map_has_key(const struct bt_value *map_obj, const char *key)
1029{
1030 bool ret;
1031 GQuark quark;
1032 struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj);
1033
1034 if (!map_obj || !bt_value_is_map(map_obj) || !key) {
1035 ret = false;
1036 goto end;
1037 }
1038
1039 quark = g_quark_from_string(key);
5d5982ab 1040 ret = bt_g_hash_table_contains(typed_map_obj->ght,
dac5c838
PP
1041 GUINT_TO_POINTER(quark));
1042
1043end:
1044 return ret;
1045}
1046
1047enum bt_value_status bt_value_map_insert(struct bt_value *map_obj,
364747d6 1048 const char *key, struct bt_value *element_obj)
dac5c838
PP
1049{
1050 GQuark quark;
1051 enum bt_value_status ret = BT_VALUE_STATUS_OK;
1052 struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj);
1053
1054 if (!map_obj || !bt_value_is_map(map_obj) || !key || !element_obj) {
1055 ret = BT_VALUE_STATUS_INVAL;
1056 goto end;
1057 }
1058
1059 if (map_obj->is_frozen) {
1060 ret = BT_VALUE_STATUS_FROZEN;
1061 goto end;
1062 }
1063
1064 quark = g_quark_from_string(key);
1065 g_hash_table_insert(typed_map_obj->ght,
1066 GUINT_TO_POINTER(quark), element_obj);
83509119 1067 bt_get(element_obj);
dac5c838
PP
1068
1069end:
1070 return ret;
1071}
1072
1073enum bt_value_status bt_value_map_insert_bool(struct bt_value *map_obj,
364747d6 1074 const char *key, bool val)
dac5c838
PP
1075{
1076 enum bt_value_status ret;
1077 struct bt_value *bool_obj = NULL;
1078
1079 bool_obj = bt_value_bool_create_init(val);
1080 ret = bt_value_map_insert(map_obj, key, bool_obj);
83509119 1081 bt_put(bool_obj);
dac5c838
PP
1082
1083 return ret;
1084}
1085
1086enum bt_value_status bt_value_map_insert_integer(struct bt_value *map_obj,
364747d6 1087 const char *key, int64_t val)
dac5c838
PP
1088{
1089 enum bt_value_status ret;
1090 struct bt_value *integer_obj = NULL;
1091
1092 integer_obj = bt_value_integer_create_init(val);
1093 ret = bt_value_map_insert(map_obj, key, integer_obj);
83509119 1094 bt_put(integer_obj);
dac5c838
PP
1095
1096 return ret;
1097}
1098
1099enum bt_value_status bt_value_map_insert_float(struct bt_value *map_obj,
364747d6 1100 const char *key, double val)
dac5c838
PP
1101{
1102 enum bt_value_status ret;
1103 struct bt_value *float_obj = NULL;
1104
1105 float_obj = bt_value_float_create_init(val);
1106 ret = bt_value_map_insert(map_obj, key, float_obj);
83509119 1107 bt_put(float_obj);
dac5c838
PP
1108
1109 return ret;
1110}
1111
1112enum bt_value_status bt_value_map_insert_string(struct bt_value *map_obj,
364747d6 1113 const char *key, const char *val)
dac5c838
PP
1114{
1115 enum bt_value_status ret;
1116 struct bt_value *string_obj = NULL;
1117
1118 string_obj = bt_value_string_create_init(val);
1119 ret = bt_value_map_insert(map_obj, key, string_obj);
83509119 1120 bt_put(string_obj);
dac5c838
PP
1121
1122 return ret;
1123}
1124
5b79e8bf 1125enum bt_value_status bt_value_map_insert_empty_array(struct bt_value *map_obj,
364747d6 1126 const char *key)
dac5c838
PP
1127{
1128 enum bt_value_status ret;
1129 struct bt_value *array_obj = NULL;
1130
1131 array_obj = bt_value_array_create();
1132 ret = bt_value_map_insert(map_obj, key, array_obj);
83509119 1133 bt_put(array_obj);
dac5c838
PP
1134
1135 return ret;
1136}
1137
5b79e8bf 1138enum bt_value_status bt_value_map_insert_empty_map(struct bt_value *map_obj,
364747d6 1139 const char *key)
dac5c838
PP
1140{
1141 enum bt_value_status ret;
1142 struct bt_value *empty_map_obj = NULL;
1143
1144 empty_map_obj = bt_value_map_create();
1145 ret = bt_value_map_insert(map_obj, key, empty_map_obj);
83509119 1146 bt_put(empty_map_obj);
dac5c838
PP
1147
1148 return ret;
1149}
1150
1151enum bt_value_status bt_value_map_foreach(const struct bt_value *map_obj,
364747d6 1152 bt_value_map_foreach_cb cb, void *data)
dac5c838
PP
1153{
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);
1158
1159 if (!map_obj || !bt_value_is_map(map_obj) || !cb) {
1160 ret = BT_VALUE_STATUS_INVAL;
1161 goto end;
1162 }
1163
1164 g_hash_table_iter_init(&iter, typed_map_obj->ght);
1165
1166 while (g_hash_table_iter_next(&iter, &key, &element_obj)) {
5b44aff2 1167 const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key));
dac5c838
PP
1168
1169 if (!cb(key_str, element_obj, data)) {
1170 ret = BT_VALUE_STATUS_CANCELLED;
1171 break;
1172 }
1173 }
1174
1175end:
1176 return ret;
1177}
1178
770750d3
PP
1179struct extend_map_element_data {
1180 struct bt_value *extended_obj;
1181 bool got_error;
1182};
1183
1184static
1185bool extend_map_element(const char *key,
1186 struct bt_value *extension_obj_elem, void *data)
1187{
1188 bool ret = true;
1189
1190 struct extend_map_element_data *extend_data = data;
1191
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);
1195
1196 /* Replace in extended object */
1197 if (bt_value_map_insert(extend_data->extended_obj, key,
1198 extension_obj_elem_copy)) {
1199 goto error;
1200 }
1201
1202 goto end;
1203
1204error:
1205 ret = false;
1206 extend_data->got_error = true;
1207
1208end:
1209 BT_PUT(extension_obj_elem_copy);
1210
1211 return ret;
1212}
1213
1214struct bt_value *bt_value_map_extend(struct bt_value *base_map_obj,
1215 struct bt_value *extension_obj)
1216{
1217 struct bt_value *extended_obj = NULL;
1218 struct extend_map_element_data extend_data = { 0 };
1219
1220 if (!bt_value_is_map(base_map_obj) || !bt_value_is_map(extension_obj)) {
1221 goto error;
1222 }
1223
1224 /* Create copy of base map object to start with */
1225 extended_obj = bt_value_copy(base_map_obj);
1226 if (!extended_obj) {
1227 goto error;
1228 }
1229
1230 /*
1231 * For each key in the extension map object, replace this key
1232 * in the copied map object.
1233 */
1234 extend_data.extended_obj = extended_obj;
1235
1236 if (bt_value_map_foreach(extension_obj, extend_map_element,
1237 &extend_data)) {
1238 goto error;
1239 }
1240
1241 if (extend_data.got_error) {
1242 goto error;
1243 }
1244
1245 goto end;
1246
1247error:
1248 BT_PUT(extended_obj);
1249
1250end:
1251 return extended_obj;
1252}
1253
dac5c838
PP
1254struct bt_value *bt_value_copy(const struct bt_value *object)
1255{
1256 struct bt_value *copy_obj = NULL;
1257
1258 if (!object) {
1259 goto end;
1260 }
1261
1262 copy_obj = copy_funcs[object->type](object);
1263
1264end:
1265 return copy_obj;
1266}
1267
1268bool bt_value_compare(const struct bt_value *object_a,
1269 const struct bt_value *object_b)
1270{
1271 bool ret = false;
1272
1273 if (!object_a || !object_b) {
1274 goto end;
1275 }
1276
1277 if (object_a->type != object_b->type) {
1278 goto end;
1279 }
1280
1281 ret = compare_funcs[object_a->type](object_a, object_b);
1282
1283end:
1284 return ret;
1285}
This page took 0.079763 seconds and 4 git commands to generate.