lib/values.c: use assert() instead of checking error
[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 #define BT_LOG_TAG "VALUES"
29 #include <babeltrace/lib-logging-internal.h>
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <assert.h>
34 #include <string.h>
35 #include <inttypes.h>
36 #include <babeltrace/compiler-internal.h>
37 #include <babeltrace/ref.h>
38 #include <babeltrace/values.h>
39 #include <babeltrace/compat/glib-internal.h>
40 #include <babeltrace/types.h>
41 #include <babeltrace/object-internal.h>
42 #include <babeltrace/values-internal.h>
43
44 #define BT_VALUE_FROM_CONCRETE(_concrete) ((struct bt_value *) (_concrete))
45 #define BT_VALUE_TO_BOOL(_base) ((struct bt_value_bool *) (_base))
46 #define BT_VALUE_TO_INTEGER(_base) ((struct bt_value_integer *) (_base))
47 #define BT_VALUE_TO_FLOAT(_base) ((struct bt_value_float *) (_base))
48 #define BT_VALUE_TO_STRING(_base) ((struct bt_value_string *) (_base))
49 #define BT_VALUE_TO_ARRAY(_base) ((struct bt_value_array *) (_base))
50 #define BT_VALUE_TO_MAP(_base) ((struct bt_value_map *) (_base))
51
52 struct bt_value {
53 struct bt_object base;
54 enum bt_value_type type;
55 bt_bool is_frozen;
56 };
57
58 static
59 struct bt_value bt_value_null_instance = {
60 .base = {
61 .ref_count = {
62 .count = 1,
63 .release = NULL,
64 },
65 .release = NULL,
66 .parent = NULL,
67 },
68 .type = BT_VALUE_TYPE_NULL,
69 .is_frozen = BT_TRUE,
70 };
71
72 struct bt_value *bt_value_null = &bt_value_null_instance;
73
74 struct bt_value_bool {
75 struct bt_value base;
76 bt_bool value;
77 };
78
79 struct bt_value_integer {
80 struct bt_value base;
81 int64_t value;
82 };
83
84 struct bt_value_float {
85 struct bt_value base;
86 double value;
87 };
88
89 struct bt_value_string {
90 struct bt_value base;
91 GString *gstr;
92 };
93
94 struct bt_value_array {
95 struct bt_value base;
96 GPtrArray *garray;
97 };
98
99 struct bt_value_map {
100 struct bt_value base;
101 GHashTable *ght;
102 };
103
104 static
105 void bt_value_destroy(struct bt_object *obj);
106
107 static
108 void bt_value_string_destroy(struct bt_value *object)
109 {
110 g_string_free(BT_VALUE_TO_STRING(object)->gstr, TRUE);
111 }
112
113 static
114 void bt_value_array_destroy(struct bt_value *object)
115 {
116 /*
117 * Pointer array's registered value destructor will take care
118 * of putting each contained object.
119 */
120 g_ptr_array_free(BT_VALUE_TO_ARRAY(object)->garray, TRUE);
121 }
122
123 static
124 void bt_value_map_destroy(struct bt_value *object)
125 {
126 /*
127 * Hash table's registered value destructor will take care of
128 * putting each contained object. Keys are GQuarks and cannot
129 * be destroyed anyway.
130 */
131 g_hash_table_destroy(BT_VALUE_TO_MAP(object)->ght);
132 }
133
134 static
135 void (* const destroy_funcs[])(struct bt_value *) = {
136 [BT_VALUE_TYPE_NULL] = NULL,
137 [BT_VALUE_TYPE_BOOL] = NULL,
138 [BT_VALUE_TYPE_INTEGER] = NULL,
139 [BT_VALUE_TYPE_FLOAT] = NULL,
140 [BT_VALUE_TYPE_STRING] = bt_value_string_destroy,
141 [BT_VALUE_TYPE_ARRAY] = bt_value_array_destroy,
142 [BT_VALUE_TYPE_MAP] = bt_value_map_destroy,
143 };
144
145 static
146 struct bt_value *bt_value_null_copy(const struct bt_value *null_obj)
147 {
148 return bt_value_null;
149 }
150
151 static
152 struct bt_value *bt_value_bool_copy(const struct bt_value *bool_obj)
153 {
154 return bt_value_bool_create_init(BT_VALUE_TO_BOOL(bool_obj)->value);
155 }
156
157 static
158 struct bt_value *bt_value_integer_copy(const struct bt_value *integer_obj)
159 {
160 return bt_value_integer_create_init(
161 BT_VALUE_TO_INTEGER(integer_obj)->value);
162 }
163
164 static
165 struct bt_value *bt_value_float_copy(const struct bt_value *float_obj)
166 {
167 return bt_value_float_create_init(
168 BT_VALUE_TO_FLOAT(float_obj)->value);
169 }
170
171 static
172 struct bt_value *bt_value_string_copy(const struct bt_value *string_obj)
173 {
174 return bt_value_string_create_init(
175 BT_VALUE_TO_STRING(string_obj)->gstr->str);
176 }
177
178 static
179 struct bt_value *bt_value_array_copy(const struct bt_value *array_obj)
180 {
181 int i;
182 int ret;
183 struct bt_value *copy_obj;
184 struct bt_value_array *typed_array_obj;
185
186 BT_LOGD("Copying array value: addr=%p", array_obj);
187 typed_array_obj = BT_VALUE_TO_ARRAY(array_obj);
188 copy_obj = bt_value_array_create();
189
190 if (!copy_obj) {
191 BT_LOGE_STR("Cannot create empty array value.");
192 goto end;
193 }
194
195 for (i = 0; i < typed_array_obj->garray->len; ++i) {
196 struct bt_value *element_obj_copy;
197 struct bt_value *element_obj = bt_value_array_get(array_obj, i);
198
199 assert(element_obj);
200 element_obj_copy = bt_value_copy(element_obj);
201 BT_PUT(element_obj);
202 if (!element_obj_copy) {
203 BT_LOGE("Cannot copy array value's element: "
204 "array-addr=%p, index=%d",
205 array_obj, i);
206 BT_PUT(copy_obj);
207 goto end;
208 }
209
210 ret = bt_value_array_append(copy_obj, element_obj_copy);
211 BT_PUT(element_obj_copy);
212 if (ret) {
213 BT_LOGE("Cannot append to array value: addr=%p",
214 array_obj);
215 BT_PUT(copy_obj);
216 goto end;
217 }
218 }
219
220 BT_LOGD("Copied array value: original-addr=%p, copy-addr=%p",
221 array_obj, copy_obj);
222
223 end:
224 return copy_obj;
225 }
226
227 static
228 struct bt_value *bt_value_map_copy(const struct bt_value *map_obj)
229 {
230 int ret;
231 GHashTableIter iter;
232 gpointer key, element_obj;
233 struct bt_value *copy_obj;
234 struct bt_value *element_obj_copy;
235 struct bt_value_map *typed_map_obj;
236
237 BT_LOGD("Copying map value: addr=%p", map_obj);
238 typed_map_obj = BT_VALUE_TO_MAP(map_obj);
239 copy_obj = bt_value_map_create();
240
241 if (!copy_obj) {
242 goto end;
243 }
244
245 g_hash_table_iter_init(&iter, typed_map_obj->ght);
246
247 while (g_hash_table_iter_next(&iter, &key, &element_obj)) {
248 const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key));
249
250 element_obj_copy = bt_value_copy(element_obj);
251 if (!element_obj_copy) {
252 BT_LOGE("Cannot copy map value's element: "
253 "map-addr=%p, key=\"%s\"",
254 map_obj, key_str);
255 BT_PUT(copy_obj);
256 goto end;
257 }
258
259 ret = bt_value_map_insert(copy_obj, key_str, element_obj_copy);
260 BT_PUT(element_obj_copy);
261 if (ret) {
262 BT_LOGE("Cannot insert into map value: addr=%p, key=\"%s\"",
263 map_obj, key_str);
264 BT_PUT(copy_obj);
265 goto end;
266 }
267 }
268
269 BT_LOGD("Copied map value: addr=%p", map_obj);
270
271 end:
272 return copy_obj;
273 }
274
275 static
276 struct bt_value *(* const copy_funcs[])(const struct bt_value *) = {
277 [BT_VALUE_TYPE_NULL] = bt_value_null_copy,
278 [BT_VALUE_TYPE_BOOL] = bt_value_bool_copy,
279 [BT_VALUE_TYPE_INTEGER] = bt_value_integer_copy,
280 [BT_VALUE_TYPE_FLOAT] = bt_value_float_copy,
281 [BT_VALUE_TYPE_STRING] = bt_value_string_copy,
282 [BT_VALUE_TYPE_ARRAY] = bt_value_array_copy,
283 [BT_VALUE_TYPE_MAP] = bt_value_map_copy,
284 };
285
286 static
287 bt_bool bt_value_null_compare(const struct bt_value *object_a,
288 const struct bt_value *object_b)
289 {
290 /*
291 * Always BT_TRUE since bt_value_compare() already checks if both
292 * object_a and object_b have the same type, and in the case of
293 * null value objects, they're always the same if it is so.
294 */
295 return BT_TRUE;
296 }
297
298 static
299 bt_bool bt_value_bool_compare(const struct bt_value *object_a,
300 const struct bt_value *object_b)
301 {
302 return BT_VALUE_TO_BOOL(object_a)->value ==
303 BT_VALUE_TO_BOOL(object_b)->value;
304 }
305
306 static
307 bt_bool bt_value_integer_compare(const struct bt_value *object_a,
308 const struct bt_value *object_b)
309 {
310 return BT_VALUE_TO_INTEGER(object_a)->value ==
311 BT_VALUE_TO_INTEGER(object_b)->value;
312 }
313
314 static
315 bt_bool bt_value_float_compare(const struct bt_value *object_a,
316 const struct bt_value *object_b)
317 {
318 return BT_VALUE_TO_FLOAT(object_a)->value ==
319 BT_VALUE_TO_FLOAT(object_b)->value;
320 }
321
322 static
323 bt_bool bt_value_string_compare(const struct bt_value *object_a,
324 const struct bt_value *object_b)
325 {
326 return !strcmp(BT_VALUE_TO_STRING(object_a)->gstr->str,
327 BT_VALUE_TO_STRING(object_b)->gstr->str);
328 }
329
330 static
331 bt_bool bt_value_array_compare(const struct bt_value *object_a,
332 const struct bt_value *object_b)
333 {
334 int i;
335 bt_bool ret = BT_TRUE;
336 const struct bt_value_array *array_obj_a =
337 BT_VALUE_TO_ARRAY(object_a);
338
339 if (bt_value_array_size(object_a) != bt_value_array_size(object_b)) {
340 BT_LOGV("Array values are different: size mismatch "
341 "value-a-addr=%p, value-b-addr=%p, "
342 "value-a-size=%" PRId64 ", value-b-size=%" PRId64,
343 object_a, object_b,
344 bt_value_array_size(object_a),
345 bt_value_array_size(object_b));
346 ret = BT_FALSE;
347 goto end;
348 }
349
350 for (i = 0; i < array_obj_a->garray->len; ++i) {
351 struct bt_value *element_obj_a;
352 struct bt_value *element_obj_b;
353
354 element_obj_a = bt_value_array_get(object_a, i);
355 element_obj_b = bt_value_array_get(object_b, i);
356
357 if (!bt_value_compare(element_obj_a, element_obj_b)) {
358 BT_LOGV("Array values's elements are different: "
359 "value-a-addr=%p, value-b-addr=%p, index=%d",
360 element_obj_a, element_obj_b, index);
361 BT_PUT(element_obj_a);
362 BT_PUT(element_obj_b);
363 ret = BT_FALSE;
364 goto end;
365 }
366
367 BT_PUT(element_obj_a);
368 BT_PUT(element_obj_b);
369 }
370
371 end:
372 return ret;
373 }
374
375 static
376 bt_bool bt_value_map_compare(const struct bt_value *object_a,
377 const struct bt_value *object_b)
378 {
379 bt_bool ret = BT_TRUE;
380 GHashTableIter iter;
381 gpointer key, element_obj_a;
382 const struct bt_value_map *map_obj_a = BT_VALUE_TO_MAP(object_a);
383
384 if (bt_value_map_size(object_a) != bt_value_map_size(object_b)) {
385 BT_LOGV("Map values are different: size mismatch "
386 "value-a-addr=%p, value-b-addr=%p, "
387 "value-a-size=%" PRId64 ", value-b-size=%" PRId64,
388 object_a, object_b,
389 bt_value_map_size(object_a),
390 bt_value_map_size(object_b));
391 ret = BT_FALSE;
392 goto end;
393 }
394
395 g_hash_table_iter_init(&iter, map_obj_a->ght);
396
397 while (g_hash_table_iter_next(&iter, &key, &element_obj_a)) {
398 struct bt_value *element_obj_b;
399 const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key));
400
401 element_obj_b = bt_value_map_get(object_b, key_str);
402
403 if (!bt_value_compare(element_obj_a, element_obj_b)) {
404 BT_LOGV("Map values's elements are different: "
405 "value-a-addr=%p, value-b-addr=%p, key=\"%s\"",
406 element_obj_a, element_obj_b, key_str);
407 BT_PUT(element_obj_b);
408 ret = BT_FALSE;
409 goto end;
410 }
411
412 BT_PUT(element_obj_b);
413 }
414
415 end:
416 return ret;
417 }
418
419 static
420 bt_bool (* const compare_funcs[])(const struct bt_value *,
421 const struct bt_value *) = {
422 [BT_VALUE_TYPE_NULL] = bt_value_null_compare,
423 [BT_VALUE_TYPE_BOOL] = bt_value_bool_compare,
424 [BT_VALUE_TYPE_INTEGER] = bt_value_integer_compare,
425 [BT_VALUE_TYPE_FLOAT] = bt_value_float_compare,
426 [BT_VALUE_TYPE_STRING] = bt_value_string_compare,
427 [BT_VALUE_TYPE_ARRAY] = bt_value_array_compare,
428 [BT_VALUE_TYPE_MAP] = bt_value_map_compare,
429 };
430
431 void bt_value_null_freeze(struct bt_value *object)
432 {
433 }
434
435 void bt_value_generic_freeze(struct bt_value *object)
436 {
437 object->is_frozen = BT_TRUE;
438 }
439
440 void bt_value_array_freeze(struct bt_value *object)
441 {
442 int i;
443 struct bt_value_array *typed_array_obj =
444 BT_VALUE_TO_ARRAY(object);
445
446 for (i = 0; i < typed_array_obj->garray->len; ++i) {
447 struct bt_value *element_obj =
448 g_ptr_array_index(typed_array_obj->garray, i);
449
450 bt_value_freeze(element_obj);
451 }
452
453 bt_value_generic_freeze(object);
454 }
455
456 void bt_value_map_freeze(struct bt_value *object)
457 {
458 GHashTableIter iter;
459 gpointer key, element_obj;
460 const struct bt_value_map *map_obj = BT_VALUE_TO_MAP(object);
461
462 g_hash_table_iter_init(&iter, map_obj->ght);
463
464 while (g_hash_table_iter_next(&iter, &key, &element_obj)) {
465 bt_value_freeze(element_obj);
466 }
467
468 bt_value_generic_freeze(object);
469 }
470
471 static
472 void (* const freeze_funcs[])(struct bt_value *) = {
473 [BT_VALUE_TYPE_NULL] = bt_value_null_freeze,
474 [BT_VALUE_TYPE_BOOL] = bt_value_generic_freeze,
475 [BT_VALUE_TYPE_INTEGER] = bt_value_generic_freeze,
476 [BT_VALUE_TYPE_FLOAT] = bt_value_generic_freeze,
477 [BT_VALUE_TYPE_STRING] = bt_value_generic_freeze,
478 [BT_VALUE_TYPE_ARRAY] = bt_value_array_freeze,
479 [BT_VALUE_TYPE_MAP] = bt_value_map_freeze,
480 };
481
482 static
483 void bt_value_destroy(struct bt_object *obj)
484 {
485 struct bt_value *value;
486
487 value = container_of(obj, struct bt_value, base);
488 assert(value->type != BT_VALUE_TYPE_UNKNOWN);
489 BT_LOGD("Destroying value: addr=%p", value);
490
491 if (bt_value_is_null(value)) {
492 BT_LOGD_STR("Not destroying the null value singleton.");
493 return;
494 }
495
496 if (destroy_funcs[value->type]) {
497 destroy_funcs[value->type](value);
498 }
499
500 g_free(value);
501 }
502
503 enum bt_value_status bt_value_freeze(struct bt_value *object)
504 {
505 enum bt_value_status ret = BT_VALUE_STATUS_OK;
506
507 if (!object) {
508 BT_LOGW_STR("Invalid parameter: value object is NULL.");
509 ret = BT_VALUE_STATUS_INVAL;
510 goto end;
511 }
512
513 if (object->is_frozen) {
514 goto end;
515 }
516
517 BT_LOGD("Freezing value: addr=%p", object);
518 freeze_funcs[object->type](object);
519
520 end:
521 return ret;
522 }
523
524 bt_bool bt_value_is_frozen(const struct bt_value *object)
525 {
526 return object && object->is_frozen;
527 }
528
529 enum bt_value_type bt_value_get_type(const struct bt_value *object)
530 {
531 if (!object) {
532 BT_LOGW_STR("Invalid parameter: value object is NULL.");
533 return BT_VALUE_TYPE_UNKNOWN;
534 }
535
536 return object->type;
537 }
538
539 static
540 struct bt_value bt_value_create_base(enum bt_value_type type)
541 {
542 struct bt_value base;
543
544 base.type = type;
545 base.is_frozen = BT_FALSE;
546 bt_object_init(&base, bt_value_destroy);
547 return base;
548 }
549
550 struct bt_value *bt_value_bool_create_init(bt_bool val)
551 {
552 struct bt_value_bool *bool_obj;
553
554 BT_LOGD("Creating boolean value object: val=%d", val);
555 bool_obj = g_new0(struct bt_value_bool, 1);
556
557 if (!bool_obj) {
558 BT_LOGE_STR("Failed to allocate one boolean value object.");
559 goto end;
560 }
561
562 bool_obj->base = bt_value_create_base(BT_VALUE_TYPE_BOOL);
563 bool_obj->value = val;
564 BT_LOGD("Created boolean value object: addr=%p", bool_obj);
565
566 end:
567 return BT_VALUE_FROM_CONCRETE(bool_obj);
568 }
569
570 struct bt_value *bt_value_bool_create(void)
571 {
572 return bt_value_bool_create_init(BT_FALSE);
573 }
574
575 struct bt_value *bt_value_integer_create_init(int64_t val)
576 {
577 struct bt_value_integer *integer_obj;
578
579 BT_LOGD("Creating integer value object: val=%" PRId64, val);
580 integer_obj = g_new0(struct bt_value_integer, 1);
581
582 if (!integer_obj) {
583 BT_LOGE_STR("Failed to allocate one integer value object.");
584 goto end;
585 }
586
587 integer_obj->base = bt_value_create_base(BT_VALUE_TYPE_INTEGER);
588 integer_obj->value = val;
589 BT_LOGD("Created integer value object: addr=%p",
590 integer_obj);
591
592 end:
593 return BT_VALUE_FROM_CONCRETE(integer_obj);
594 }
595
596 struct bt_value *bt_value_integer_create(void)
597 {
598 return bt_value_integer_create_init(0);
599 }
600
601 struct bt_value *bt_value_float_create_init(double val)
602 {
603 struct bt_value_float *float_obj;
604
605 BT_LOGD("Creating floating point number value object: val=%f", val);
606 float_obj = g_new0(struct bt_value_float, 1);
607
608 if (!float_obj) {
609 BT_LOGE_STR("Failed to allocate one floating point number value object.");
610 goto end;
611 }
612
613 float_obj->base = bt_value_create_base(BT_VALUE_TYPE_FLOAT);
614 float_obj->value = val;
615 BT_LOGD("Created floating point number value object: addr=%p",
616 float_obj);
617
618 end:
619 return BT_VALUE_FROM_CONCRETE(float_obj);
620 }
621
622 struct bt_value *bt_value_float_create(void)
623 {
624 return bt_value_float_create_init(0.);
625 }
626
627 struct bt_value *bt_value_string_create_init(const char *val)
628 {
629 struct bt_value_string *string_obj = NULL;
630
631 if (!val) {
632 BT_LOGW_STR("Invalid parameter: value is NULL.");
633 goto end;
634 }
635
636 BT_LOGD("Creating string value object: val-len=%u", strlen(val));
637 string_obj = g_new0(struct bt_value_string, 1);
638
639 if (!string_obj) {
640 BT_LOGE_STR("Failed to allocate one string object.");
641 goto end;
642 }
643
644 string_obj->base = bt_value_create_base(BT_VALUE_TYPE_STRING);
645 string_obj->gstr = g_string_new(val);
646
647 if (!string_obj->gstr) {
648 BT_LOGE_STR("Failed to allocate a GString.");
649 g_free(string_obj);
650 string_obj = NULL;
651 goto end;
652 }
653
654 BT_LOGD("Created string value object: addr=%p",
655 string_obj);
656
657 end:
658 return BT_VALUE_FROM_CONCRETE(string_obj);
659 }
660
661 struct bt_value *bt_value_string_create(void)
662 {
663 return bt_value_string_create_init("");
664 }
665
666 struct bt_value *bt_value_array_create(void)
667 {
668 struct bt_value_array *array_obj;
669
670 BT_LOGD_STR("Creating empty array value object.");
671 array_obj = g_new0(struct bt_value_array, 1);
672
673 if (!array_obj) {
674 BT_LOGE_STR("Failed to allocate one array object.");
675 goto end;
676 }
677
678 array_obj->base = bt_value_create_base(BT_VALUE_TYPE_ARRAY);
679 array_obj->garray = bt_g_ptr_array_new_full(0,
680 (GDestroyNotify) bt_put);
681
682 if (!array_obj->garray) {
683 BT_LOGE_STR("Failed to allocate a GPtrArray.");
684 g_free(array_obj);
685 array_obj = NULL;
686 goto end;
687 }
688
689 BT_LOGD("Created array value object: addr=%p",
690 array_obj);
691
692 end:
693 return BT_VALUE_FROM_CONCRETE(array_obj);
694 }
695
696 struct bt_value *bt_value_map_create(void)
697 {
698 struct bt_value_map *map_obj;
699
700 BT_LOGD_STR("Creating empty map value object.");
701 map_obj = g_new0(struct bt_value_map, 1);
702
703 if (!map_obj) {
704 BT_LOGE_STR("Failed to allocate one map object.");
705 goto end;
706 }
707
708 map_obj->base = bt_value_create_base(BT_VALUE_TYPE_MAP);
709 map_obj->ght = g_hash_table_new_full(g_direct_hash, g_direct_equal,
710 NULL, (GDestroyNotify) bt_put);
711
712 if (!map_obj->ght) {
713 BT_LOGE_STR("Failed to allocate a GHashTable.");
714 g_free(map_obj);
715 map_obj = NULL;
716 goto end;
717 }
718
719 BT_LOGD("Created map value object: addr=%p",
720 map_obj);
721
722 end:
723 return BT_VALUE_FROM_CONCRETE(map_obj);
724 }
725
726 enum bt_value_status bt_value_bool_get(const struct bt_value *bool_obj,
727 bt_bool *val)
728 {
729 enum bt_value_status ret = BT_VALUE_STATUS_OK;
730 struct bt_value_bool *typed_bool_obj = BT_VALUE_TO_BOOL(bool_obj);
731
732 if (!bool_obj || !val) {
733 BT_LOGW("Invalid parameter: value object or value is NULL: "
734 "value-addr=%p, raw-value-addr=%p",
735 bool_obj, val);
736 ret = BT_VALUE_STATUS_INVAL;
737 goto end;
738 }
739
740 if (!bt_value_is_bool(bool_obj)) {
741 BT_LOGW("Invalid parameter: value is not a boolean value: addr=%p, "
742 "type=%s", bool_obj, bool_obj->type,
743 bt_value_type_string(bool_obj->type));
744 ret = BT_VALUE_STATUS_INVAL;
745 goto end;
746 }
747
748 *val = typed_bool_obj->value;
749
750 end:
751 return ret;
752 }
753
754 enum bt_value_status bt_value_bool_set(struct bt_value *bool_obj, bt_bool val)
755 {
756 enum bt_value_status ret = BT_VALUE_STATUS_OK;
757 struct bt_value_bool *typed_bool_obj = BT_VALUE_TO_BOOL(bool_obj);
758
759 if (!bool_obj) {
760 BT_LOGW_STR("Invalid parameter: value object is NULL.");
761 ret = BT_VALUE_STATUS_INVAL;
762 goto end;
763 }
764
765 if (!bt_value_is_bool(bool_obj)) {
766 BT_LOGW("Invalid parameter: value is not a boolean value: addr=%p, "
767 "type=%s", bool_obj,
768 bt_value_type_string(bool_obj->type));
769 ret = BT_VALUE_STATUS_INVAL;
770 goto end;
771 }
772
773 if (bool_obj->is_frozen) {
774 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
775 bool_obj);
776 ret = BT_VALUE_STATUS_FROZEN;
777 goto end;
778 }
779
780 typed_bool_obj->value = val;
781 BT_LOGV("Set boolean value's raw value: value-addr=%p, value=%d",
782 bool_obj, val);
783
784 end:
785 return ret;
786 }
787
788 enum bt_value_status bt_value_integer_get(const struct bt_value *integer_obj,
789 int64_t *val)
790 {
791 enum bt_value_status ret = BT_VALUE_STATUS_OK;
792 struct bt_value_integer *typed_integer_obj =
793 BT_VALUE_TO_INTEGER(integer_obj);
794
795 if (!integer_obj || !val) {
796 BT_LOGW("Invalid parameter: value object or value is NULL: "
797 "value-addr=%p, raw-value-addr=%p",
798 integer_obj, val);
799 ret = BT_VALUE_STATUS_INVAL;
800 goto end;
801 }
802
803 if (!bt_value_is_integer(integer_obj)) {
804 BT_LOGW("Invalid parameter: value is not an integer value: addr=%p, "
805 "type=%s", integer_obj,
806 bt_value_type_string(integer_obj->type));
807 ret = BT_VALUE_STATUS_INVAL;
808 goto end;
809 }
810
811 *val = typed_integer_obj->value;
812
813 end:
814 return ret;
815 }
816
817 enum bt_value_status bt_value_integer_set(struct bt_value *integer_obj,
818 int64_t val)
819 {
820 enum bt_value_status ret = BT_VALUE_STATUS_OK;
821 struct bt_value_integer *typed_integer_obj =
822 BT_VALUE_TO_INTEGER(integer_obj);
823
824 if (!integer_obj) {
825 BT_LOGW_STR("Invalid parameter: value object is NULL.");
826 ret = BT_VALUE_STATUS_INVAL;
827 goto end;
828 }
829
830 if (!bt_value_is_integer(integer_obj)) {
831 BT_LOGW("Invalid parameter: value is not an integer value: addr=%p, "
832 "type=%s", integer_obj,
833 bt_value_type_string(integer_obj->type));
834 ret = BT_VALUE_STATUS_INVAL;
835 goto end;
836 }
837
838 if (integer_obj->is_frozen) {
839 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
840 integer_obj);
841 ret = BT_VALUE_STATUS_FROZEN;
842 goto end;
843 }
844
845 typed_integer_obj->value = val;
846 BT_LOGV("Set integer value's raw value: value-addr=%p, value=%" PRId64,
847 integer_obj, val);
848
849 end:
850 return ret;
851 }
852
853 enum bt_value_status bt_value_float_get(const struct bt_value *float_obj,
854 double *val)
855 {
856 enum bt_value_status ret = BT_VALUE_STATUS_OK;
857 struct bt_value_float *typed_float_obj =
858 BT_VALUE_TO_FLOAT(float_obj);
859
860 if (!float_obj || !val) {
861 BT_LOGW("Invalid parameter: value object or value is NULL: "
862 "value-addr=%p, raw-value-addr=%p",
863 float_obj, val);
864 ret = BT_VALUE_STATUS_INVAL;
865 goto end;
866 }
867
868 if (!bt_value_is_float(float_obj)) {
869 BT_LOGW("Invalid parameter: value is not a floating point number value: addr=%p, "
870 "type=%s", float_obj,
871 bt_value_type_string(float_obj->type));
872 ret = BT_VALUE_STATUS_INVAL;
873 goto end;
874 }
875
876 *val = typed_float_obj->value;
877
878 end:
879 return ret;
880 }
881
882 enum bt_value_status bt_value_float_set(struct bt_value *float_obj,
883 double val)
884 {
885 enum bt_value_status ret = BT_VALUE_STATUS_OK;
886 struct bt_value_float *typed_float_obj =
887 BT_VALUE_TO_FLOAT(float_obj);
888
889 if (!float_obj) {
890 BT_LOGW_STR("Invalid parameter: value object is NULL.");
891 ret = BT_VALUE_STATUS_INVAL;
892 goto end;
893 }
894
895 if (!bt_value_is_float(float_obj)) {
896 BT_LOGW("Invalid parameter: value is not a floating point number value: addr=%p, "
897 "type=%s", float_obj,
898 bt_value_type_string(float_obj->type));
899 ret = BT_VALUE_STATUS_INVAL;
900 goto end;
901 }
902
903 if (float_obj->is_frozen) {
904 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
905 float_obj);
906 ret = BT_VALUE_STATUS_FROZEN;
907 goto end;
908 }
909
910 typed_float_obj->value = val;
911 BT_LOGV("Set floating point number value's raw value: value-addr=%p, value=%f",
912 float_obj, val);
913
914 end:
915 return ret;
916 }
917
918 enum bt_value_status bt_value_string_get(const struct bt_value *string_obj,
919 const char **val)
920 {
921 enum bt_value_status ret = BT_VALUE_STATUS_OK;
922 struct bt_value_string *typed_string_obj =
923 BT_VALUE_TO_STRING(string_obj);
924
925 if (!string_obj || !val) {
926 BT_LOGW("Invalid parameter: value object or value is NULL: "
927 "value-addr=%p, raw-value-addr=%p",
928 string_obj, val);
929 ret = BT_VALUE_STATUS_INVAL;
930 goto end;
931 }
932
933 if (!bt_value_is_string(string_obj)) {
934 BT_LOGW("Invalid parameter: value is not a string value: addr=%p, "
935 "type=%s", string_obj,
936 bt_value_type_string(string_obj->type));
937 ret = BT_VALUE_STATUS_INVAL;
938 goto end;
939 }
940
941 *val = typed_string_obj->gstr->str;
942
943 end:
944 return ret;
945 }
946
947 enum bt_value_status bt_value_string_set(struct bt_value *string_obj,
948 const char *val)
949 {
950 enum bt_value_status ret = BT_VALUE_STATUS_OK;
951 struct bt_value_string *typed_string_obj =
952 BT_VALUE_TO_STRING(string_obj);
953
954 if (!string_obj || !val) {
955 BT_LOGW("Invalid parameter: value object or value is NULL: "
956 "value-addr=%p, raw-value-addr=%p",
957 string_obj, val);
958 ret = BT_VALUE_STATUS_INVAL;
959 goto end;
960 }
961
962 if (!bt_value_is_string(string_obj)) {
963 BT_LOGW("Invalid parameter: value is not a string value: addr=%p, "
964 "type=%s", string_obj,
965 bt_value_type_string(string_obj->type));
966 ret = BT_VALUE_STATUS_INVAL;
967 goto end;
968 }
969
970 if (string_obj->is_frozen) {
971 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
972 string_obj);
973 ret = BT_VALUE_STATUS_FROZEN;
974 goto end;
975 }
976
977 g_string_assign(typed_string_obj->gstr, val);
978 BT_LOGV("Set string value's raw value: value-addr=%p, raw-value-addr=%p",
979 string_obj, val);
980
981 end:
982 return ret;
983 }
984
985 int64_t bt_value_array_size(const struct bt_value *array_obj)
986 {
987 int64_t ret;
988 struct bt_value_array *typed_array_obj =
989 BT_VALUE_TO_ARRAY(array_obj);
990
991 if (!array_obj) {
992 BT_LOGW_STR("Invalid parameter: value object is NULL.");
993 ret = (int64_t) BT_VALUE_STATUS_INVAL;
994 goto end;
995 }
996
997 if (!bt_value_is_array(array_obj)) {
998 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
999 "type=%s", array_obj,
1000 bt_value_type_string(array_obj->type));
1001 ret = BT_VALUE_STATUS_INVAL;
1002 goto end;
1003 }
1004
1005 ret = (int64_t) typed_array_obj->garray->len;
1006
1007 end:
1008 return ret;
1009 }
1010
1011 bt_bool bt_value_array_is_empty(const struct bt_value *array_obj)
1012 {
1013 return bt_value_array_size(array_obj) == 0;
1014 }
1015
1016 struct bt_value *bt_value_array_get(const struct bt_value *array_obj,
1017 uint64_t index)
1018 {
1019 struct bt_value *ret;
1020 struct bt_value_array *typed_array_obj =
1021 BT_VALUE_TO_ARRAY(array_obj);
1022
1023 if (!array_obj) {
1024 BT_LOGW("Invalid parameter: value object is NULL: index=%" PRIu64,
1025 index);
1026 ret = NULL;
1027 goto end;
1028 }
1029
1030 if (!bt_value_is_array(array_obj)) {
1031 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1032 "type=%s", array_obj,
1033 bt_value_type_string(array_obj->type));
1034 ret = NULL;
1035 goto end;
1036 }
1037
1038 if (index >= typed_array_obj->garray->len) {
1039 BT_LOGW("Invalid parameter: index is out of bounds: "
1040 "addr=%p, index=%" PRIu64 ", size=%u",
1041 array_obj, index, typed_array_obj->garray->len);
1042 ret = NULL;
1043 goto end;
1044 }
1045
1046 ret = g_ptr_array_index(typed_array_obj->garray, index);
1047 bt_get(ret);
1048
1049 end:
1050 return ret;
1051 }
1052
1053 enum bt_value_status bt_value_array_append(struct bt_value *array_obj,
1054 struct bt_value *element_obj)
1055 {
1056 enum bt_value_status ret = BT_VALUE_STATUS_OK;
1057 struct bt_value_array *typed_array_obj =
1058 BT_VALUE_TO_ARRAY(array_obj);
1059
1060 if (!array_obj || !element_obj) {
1061 BT_LOGW("Invalid parameter: array value or element value is NULL: "
1062 "array-value-addr=%p, element-value-addr=%p",
1063 array_obj, element_obj);
1064 ret = BT_VALUE_STATUS_INVAL;
1065 goto end;
1066 }
1067
1068 if (!bt_value_is_array(array_obj)) {
1069 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1070 "type=%s", array_obj,
1071 bt_value_type_string(array_obj->type));
1072 ret = BT_VALUE_STATUS_INVAL;
1073 goto end;
1074 }
1075
1076 if (array_obj->is_frozen) {
1077 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1078 array_obj);
1079 ret = BT_VALUE_STATUS_FROZEN;
1080 goto end;
1081 }
1082
1083 g_ptr_array_add(typed_array_obj->garray, element_obj);
1084 bt_get(element_obj);
1085 BT_LOGV("Appended element to array value: array-value-addr=%p, "
1086 "element-value-addr=%p, new-size=%u",
1087 array_obj, element_obj, typed_array_obj->garray->len);
1088
1089 end:
1090 return ret;
1091 }
1092
1093 enum bt_value_status bt_value_array_append_bool(struct bt_value *array_obj,
1094 bt_bool val)
1095 {
1096 enum bt_value_status ret;
1097 struct bt_value *bool_obj = NULL;
1098
1099 bool_obj = bt_value_bool_create_init(val);
1100 ret = bt_value_array_append(array_obj, bool_obj);
1101 bt_put(bool_obj);
1102 return ret;
1103 }
1104
1105 enum bt_value_status bt_value_array_append_integer(
1106 struct bt_value *array_obj, int64_t val)
1107 {
1108 enum bt_value_status ret;
1109 struct bt_value *integer_obj = NULL;
1110
1111 integer_obj = bt_value_integer_create_init(val);
1112 ret = bt_value_array_append(array_obj, integer_obj);
1113 bt_put(integer_obj);
1114 return ret;
1115 }
1116
1117 enum bt_value_status bt_value_array_append_float(struct bt_value *array_obj,
1118 double val)
1119 {
1120 enum bt_value_status ret;
1121 struct bt_value *float_obj = NULL;
1122
1123 float_obj = bt_value_float_create_init(val);
1124 ret = bt_value_array_append(array_obj, float_obj);
1125 bt_put(float_obj);
1126 return ret;
1127 }
1128
1129 enum bt_value_status bt_value_array_append_string(struct bt_value *array_obj,
1130 const char *val)
1131 {
1132 enum bt_value_status ret;
1133 struct bt_value *string_obj = NULL;
1134
1135 string_obj = bt_value_string_create_init(val);
1136 ret = bt_value_array_append(array_obj, string_obj);
1137 bt_put(string_obj);
1138 return ret;
1139 }
1140
1141 enum bt_value_status bt_value_array_append_empty_array(
1142 struct bt_value *array_obj)
1143 {
1144 enum bt_value_status ret;
1145 struct bt_value *empty_array_obj = NULL;
1146
1147 empty_array_obj = bt_value_array_create();
1148 ret = bt_value_array_append(array_obj, empty_array_obj);
1149 bt_put(empty_array_obj);
1150 return ret;
1151 }
1152
1153 enum bt_value_status bt_value_array_append_empty_map(struct bt_value *array_obj)
1154 {
1155 enum bt_value_status ret;
1156 struct bt_value *map_obj = NULL;
1157
1158 map_obj = bt_value_map_create();
1159 ret = bt_value_array_append(array_obj, map_obj);
1160 bt_put(map_obj);
1161 return ret;
1162 }
1163
1164 enum bt_value_status bt_value_array_set(struct bt_value *array_obj,
1165 uint64_t index, struct bt_value *element_obj)
1166 {
1167 enum bt_value_status ret = BT_VALUE_STATUS_OK;
1168 struct bt_value_array *typed_array_obj =
1169 BT_VALUE_TO_ARRAY(array_obj);
1170
1171 if (!array_obj || !element_obj) {
1172 BT_LOGW("Invalid parameter: array value or element value is NULL: "
1173 "index=%" PRIu64 ", array-value-addr=%p, element-value-addr=%p",
1174 index, array_obj, element_obj);
1175 ret = BT_VALUE_STATUS_INVAL;
1176 goto end;
1177 }
1178
1179 if (!bt_value_is_array(array_obj)) {
1180 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1181 "type=%s", array_obj,
1182 bt_value_type_string(array_obj->type));
1183 ret = BT_VALUE_STATUS_INVAL;
1184 goto end;
1185 }
1186
1187 if (index >= typed_array_obj->garray->len) {
1188 BT_LOGW("Invalid parameter: index is out of bounds: "
1189 "addr=%p, index=%" PRIu64 ", size=%u",
1190 array_obj, index, typed_array_obj->garray->len);
1191 ret = BT_VALUE_STATUS_INVAL;
1192 goto end;
1193 }
1194
1195 if (array_obj->is_frozen) {
1196 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1197 array_obj);
1198 ret = BT_VALUE_STATUS_FROZEN;
1199 goto end;
1200 }
1201
1202 bt_put(g_ptr_array_index(typed_array_obj->garray, index));
1203 g_ptr_array_index(typed_array_obj->garray, index) = element_obj;
1204 bt_get(element_obj);
1205 BT_LOGV("Set array value's element: array-value-addr=%p, "
1206 "index=%" PRIu64 ", element-value-addr=%p",
1207 array_obj, index, element_obj);
1208
1209 end:
1210 return ret;
1211 }
1212
1213 int64_t bt_value_map_size(const struct bt_value *map_obj)
1214 {
1215 int64_t ret;
1216 struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj);
1217
1218 if (!map_obj) {
1219 BT_LOGW_STR("Invalid parameter: value object is NULL.");
1220 ret = (int64_t) BT_VALUE_STATUS_INVAL;
1221 goto end;
1222 }
1223
1224 if (!bt_value_is_map(map_obj)) {
1225 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1226 "type=%s", map_obj,
1227 bt_value_type_string(map_obj->type));
1228 ret = (int64_t) BT_VALUE_STATUS_INVAL;
1229 goto end;
1230 }
1231
1232 ret = (int64_t) g_hash_table_size(typed_map_obj->ght);
1233
1234 end:
1235 return ret;
1236 }
1237
1238 bt_bool bt_value_map_is_empty(const struct bt_value *map_obj)
1239 {
1240 return bt_value_map_size(map_obj) == 0;
1241 }
1242
1243 struct bt_value *bt_value_map_get(const struct bt_value *map_obj,
1244 const char *key)
1245 {
1246 GQuark quark;
1247 struct bt_value *ret;
1248 struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj);
1249
1250 if (!map_obj || !key) {
1251 BT_LOGW("Invalid parameter: value object or key is NULL: "
1252 "value-addr=%p, key-addr=%p", map_obj, key);
1253 ret = NULL;
1254 goto end;
1255 }
1256
1257 if (!bt_value_is_map(map_obj)) {
1258 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1259 "type=%s", map_obj,
1260 bt_value_type_string(map_obj->type));
1261 ret = NULL;
1262 goto end;
1263 }
1264
1265 quark = g_quark_from_string(key);
1266 ret = g_hash_table_lookup(typed_map_obj->ght, GUINT_TO_POINTER(quark));
1267 if (ret) {
1268 bt_get(ret);
1269 }
1270
1271 end:
1272 return ret;
1273 }
1274
1275 bt_bool bt_value_map_has_key(const struct bt_value *map_obj, const char *key)
1276 {
1277 bt_bool ret;
1278 GQuark quark;
1279 struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj);
1280
1281 if (!map_obj || !key) {
1282 BT_LOGW("Invalid parameter: value object or key is NULL: "
1283 "value-addr=%p, key-addr=%p", map_obj, key);
1284 ret = BT_FALSE;
1285 goto end;
1286 }
1287
1288 if (!bt_value_is_map(map_obj)) {
1289 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1290 "type=%s", map_obj,
1291 bt_value_type_string(map_obj->type));
1292 ret = BT_FALSE;
1293 goto end;
1294 }
1295
1296 quark = g_quark_from_string(key);
1297 ret = bt_g_hash_table_contains(typed_map_obj->ght,
1298 GUINT_TO_POINTER(quark));
1299
1300 end:
1301 return ret;
1302 }
1303
1304 enum bt_value_status bt_value_map_insert(struct bt_value *map_obj,
1305 const char *key, struct bt_value *element_obj)
1306 {
1307 GQuark quark;
1308 enum bt_value_status ret = BT_VALUE_STATUS_OK;
1309 struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj);
1310
1311 if (!map_obj || !key || !element_obj) {
1312 BT_LOGW("Invalid parameter: map value, key, or element value is NULL: "
1313 "map-value-addr=%p, key-addr=%p, element-value-addr=%p",
1314 map_obj, key, element_obj);
1315 ret = BT_VALUE_STATUS_INVAL;
1316 goto end;
1317 }
1318
1319 if (!bt_value_is_map(map_obj)) {
1320 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1321 "type=%s", map_obj,
1322 bt_value_type_string(map_obj->type));
1323 ret = BT_VALUE_STATUS_INVAL;
1324 goto end;
1325 }
1326
1327 if (map_obj->is_frozen) {
1328 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1329 map_obj);
1330 ret = BT_VALUE_STATUS_FROZEN;
1331 goto end;
1332 }
1333
1334 quark = g_quark_from_string(key);
1335 g_hash_table_insert(typed_map_obj->ght,
1336 GUINT_TO_POINTER(quark), element_obj);
1337 bt_get(element_obj);
1338 BT_LOGV("Inserted value into map value: map-value-addr=%p, "
1339 "key=\"%s\", element-value-addr=%p",
1340 map_obj, key, element_obj);
1341
1342 end:
1343 return ret;
1344 }
1345
1346 enum bt_value_status bt_value_map_insert_bool(struct bt_value *map_obj,
1347 const char *key, bt_bool val)
1348 {
1349 enum bt_value_status ret;
1350 struct bt_value *bool_obj = NULL;
1351
1352 bool_obj = bt_value_bool_create_init(val);
1353 ret = bt_value_map_insert(map_obj, key, bool_obj);
1354 bt_put(bool_obj);
1355 return ret;
1356 }
1357
1358 enum bt_value_status bt_value_map_insert_integer(struct bt_value *map_obj,
1359 const char *key, int64_t val)
1360 {
1361 enum bt_value_status ret;
1362 struct bt_value *integer_obj = NULL;
1363
1364 integer_obj = bt_value_integer_create_init(val);
1365 ret = bt_value_map_insert(map_obj, key, integer_obj);
1366 bt_put(integer_obj);
1367 return ret;
1368 }
1369
1370 enum bt_value_status bt_value_map_insert_float(struct bt_value *map_obj,
1371 const char *key, double val)
1372 {
1373 enum bt_value_status ret;
1374 struct bt_value *float_obj = NULL;
1375
1376 float_obj = bt_value_float_create_init(val);
1377 ret = bt_value_map_insert(map_obj, key, float_obj);
1378 bt_put(float_obj);
1379 return ret;
1380 }
1381
1382 enum bt_value_status bt_value_map_insert_string(struct bt_value *map_obj,
1383 const char *key, const char *val)
1384 {
1385 enum bt_value_status ret;
1386 struct bt_value *string_obj = NULL;
1387
1388 string_obj = bt_value_string_create_init(val);
1389 ret = bt_value_map_insert(map_obj, key, string_obj);
1390 bt_put(string_obj);
1391 return ret;
1392 }
1393
1394 enum bt_value_status bt_value_map_insert_empty_array(struct bt_value *map_obj,
1395 const char *key)
1396 {
1397 enum bt_value_status ret;
1398 struct bt_value *array_obj = NULL;
1399
1400 array_obj = bt_value_array_create();
1401 ret = bt_value_map_insert(map_obj, key, array_obj);
1402 bt_put(array_obj);
1403 return ret;
1404 }
1405
1406 enum bt_value_status bt_value_map_insert_empty_map(struct bt_value *map_obj,
1407 const char *key)
1408 {
1409 enum bt_value_status ret;
1410 struct bt_value *empty_map_obj = NULL;
1411
1412 empty_map_obj = bt_value_map_create();
1413 ret = bt_value_map_insert(map_obj, key, empty_map_obj);
1414 bt_put(empty_map_obj);
1415 return ret;
1416 }
1417
1418 enum bt_value_status bt_value_map_foreach(const struct bt_value *map_obj,
1419 bt_value_map_foreach_cb cb, void *data)
1420 {
1421 enum bt_value_status ret = BT_VALUE_STATUS_OK;
1422 gpointer key, element_obj;
1423 GHashTableIter iter;
1424 struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj);
1425
1426 if (!map_obj || !cb) {
1427 BT_LOGW("Invalid parameter: map value or callback is NULL: "
1428 "value-addr=%p, cb-addr=%p", map_obj, cb);
1429 ret = BT_VALUE_STATUS_INVAL;
1430 goto end;
1431 }
1432
1433 if (!bt_value_is_map(map_obj)) {
1434 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1435 "type=%s", map_obj,
1436 bt_value_type_string(map_obj->type));
1437 ret = BT_VALUE_STATUS_INVAL;
1438 goto end;
1439 }
1440
1441 g_hash_table_iter_init(&iter, typed_map_obj->ght);
1442
1443 while (g_hash_table_iter_next(&iter, &key, &element_obj)) {
1444 const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key));
1445
1446 if (!cb(key_str, element_obj, data)) {
1447 BT_LOGV("User cancelled the loop: key=\"%s\", "
1448 "value-addr=%p, data=%p",
1449 key_str, element_obj, data);
1450 ret = BT_VALUE_STATUS_CANCELLED;
1451 break;
1452 }
1453 }
1454
1455 end:
1456 return ret;
1457 }
1458
1459 struct extend_map_element_data {
1460 struct bt_value *extended_obj;
1461 bt_bool got_error;
1462 };
1463
1464 static
1465 bt_bool extend_map_element(const char *key,
1466 struct bt_value *extension_obj_elem, void *data)
1467 {
1468 bt_bool ret = BT_TRUE;
1469
1470 struct extend_map_element_data *extend_data = data;
1471
1472 /* Copy object which is to replace the current one */
1473 struct bt_value *extension_obj_elem_copy =
1474 bt_value_copy(extension_obj_elem);
1475
1476 /* Replace in extended object */
1477 if (bt_value_map_insert(extend_data->extended_obj, key,
1478 extension_obj_elem_copy)) {
1479 BT_LOGE("Cannot replace value in extended value: key=\"%s\", "
1480 "extended-value-addr=%p, element-value-addr=%p",
1481 key, extend_data->extended_obj,
1482 extension_obj_elem_copy);
1483 goto error;
1484 }
1485
1486 goto end;
1487
1488 error:
1489 ret = BT_FALSE;
1490 extend_data->got_error = BT_TRUE;
1491
1492 end:
1493 BT_PUT(extension_obj_elem_copy);
1494 return ret;
1495 }
1496
1497 struct bt_value *bt_value_map_extend(struct bt_value *base_map_obj,
1498 struct bt_value *extension_obj)
1499 {
1500 struct bt_value *extended_obj = NULL;
1501 struct extend_map_element_data extend_data = { 0 };
1502
1503 if (!base_map_obj || !extension_obj) {
1504 BT_LOGW("Invalid parameter: base value or extension value is NULL: "
1505 "base-value-addr=%p, extension-value-addr=%p",
1506 base_map_obj, extension_obj);
1507 goto error;
1508 }
1509
1510 if (!bt_value_is_map(base_map_obj)) {
1511 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1512 "type=%s", base_map_obj,
1513 bt_value_type_string(base_map_obj->type));
1514 goto error;
1515 }
1516
1517 if (!bt_value_is_map(extension_obj)) {
1518 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1519 "type=%s", extension_obj,
1520 bt_value_type_string(extension_obj->type));
1521 goto error;
1522 }
1523
1524 BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p",
1525 base_map_obj, extension_obj);
1526
1527 /* Create copy of base map object to start with */
1528 extended_obj = bt_value_copy(base_map_obj);
1529 if (!extended_obj) {
1530 BT_LOGE("Cannot copy base value: base-value-addr=%p",
1531 base_map_obj);
1532 goto error;
1533 }
1534
1535 /*
1536 * For each key in the extension map object, replace this key
1537 * in the copied map object.
1538 */
1539 extend_data.extended_obj = extended_obj;
1540
1541 if (bt_value_map_foreach(extension_obj, extend_map_element,
1542 &extend_data)) {
1543 BT_LOGE("Cannot iterate on the extension object's elements: ",
1544 "extension-value-addr=%p", extension_obj);
1545 goto error;
1546 }
1547
1548 if (extend_data.got_error) {
1549 BT_LOGE("Failed to successfully iterate on the extension object's elements: ",
1550 "extension-value-addr=%p", extension_obj);
1551 goto error;
1552 }
1553
1554 BT_LOGD("Extended map value: extended-value-addr=%p",
1555 extended_obj);
1556 goto end;
1557
1558 error:
1559 BT_PUT(extended_obj);
1560
1561 end:
1562 return extended_obj;
1563 }
1564
1565 struct bt_value *bt_value_copy(const struct bt_value *object)
1566 {
1567 struct bt_value *copy_obj = NULL;
1568
1569 if (!object) {
1570 BT_LOGW_STR("Invalid parameter: value object is NULL.");
1571 goto end;
1572 }
1573
1574 BT_LOGD("Copying value object: addr=%p", object);
1575 copy_obj = copy_funcs[object->type](object);
1576 if (copy_obj) {
1577 BT_LOGD("Copied value object: copy-value-addr=%p",
1578 copy_obj);
1579 } else {
1580 BT_LOGE_STR("Failed to copy value object.");
1581 }
1582
1583 end:
1584 return copy_obj;
1585 }
1586
1587 bt_bool bt_value_compare(const struct bt_value *object_a,
1588 const struct bt_value *object_b)
1589 {
1590 bt_bool ret = BT_FALSE;
1591
1592 if (!object_a || !object_b) {
1593 BT_LOGW("Invalid parameter: value A or value B is NULL: "
1594 "value-a-addr=%p, value-b-addr=%p",
1595 object_a, object_b);
1596 goto end;
1597 }
1598
1599 if (object_a->type != object_b->type) {
1600 BT_LOGV("Values are different: type mismatch: "
1601 "value-a-addr=%p, value-b-addr=%p, "
1602 "value-a-type=%s, value-b-type=%s",
1603 object_a, object_b,
1604 bt_value_type_string(object_a->type),
1605 bt_value_type_string(object_b->type));
1606 goto end;
1607 }
1608
1609 ret = compare_funcs[object_a->type](object_a, object_b);
1610
1611 end:
1612 return ret;
1613 }
This page took 0.088315 seconds and 5 git commands to generate.