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