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