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