lib: strictly type function return status enumerations
[babeltrace.git] / src / lib / value.c
1 /*
2 * Copyright (c) 2015-2018 Philippe Proulx <pproulx@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #define BT_LOG_TAG "LIB/VALUE"
24 #include "lib/logging.h"
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <string.h>
29 #include <inttypes.h>
30 #include "compat/compiler.h"
31 #include "common/common.h"
32 #include <babeltrace2/value-const.h>
33 #include <babeltrace2/value.h>
34 #include "compat/glib.h"
35 #include <babeltrace2/types.h>
36 #include "lib/assert-pre.h"
37 #include "lib/value.h"
38 #include "common/assert.h"
39 #include "func-status.h"
40
41 #define BT_VALUE_TO_BOOL(_base) ((struct bt_value_bool *) (_base))
42 #define BT_VALUE_TO_INTEGER(_base) ((struct bt_value_integer *) (_base))
43 #define BT_VALUE_TO_REAL(_base) ((struct bt_value_real *) (_base))
44 #define BT_VALUE_TO_STRING(_base) ((struct bt_value_string *) (_base))
45 #define BT_VALUE_TO_ARRAY(_base) ((struct bt_value_array *) (_base))
46 #define BT_VALUE_TO_MAP(_base) ((struct bt_value_map *) (_base))
47
48 #define BT_ASSERT_PRE_VALUE_IS_TYPE(_value, _type) \
49 BT_ASSERT_PRE(((struct bt_value *) (_value))->type == (_type), \
50 "Value has the wrong type ID: expected-type=%s, " \
51 "%![value-]+v", bt_common_value_type_string(_type), \
52 (_value))
53
54 #define BT_ASSERT_PRE_VALUE_HOT(_value, _name) \
55 BT_ASSERT_PRE_HOT(((struct bt_value *) (_value)), (_name), \
56 ": %!+v", (_value))
57
58 #define BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(_index, _count) \
59 BT_ASSERT_PRE((_index) < (_count), \
60 "Index is out of bound: " \
61 "index=%" PRIu64 ", count=%u", (_index), (_count));
62
63 static
64 void bt_value_null_instance_release_func(struct bt_object *obj)
65 {
66 BT_LOGW("Releasing the null value singleton: addr=%p", obj);
67 }
68
69 static
70 struct bt_value bt_value_null_instance = {
71 .base = {
72 .is_shared = true,
73 .ref_count = 1,
74 .release_func = bt_value_null_instance_release_func,
75 .spec_release_func = NULL,
76 .parent_is_owner_listener_func = NULL,
77 .parent = NULL,
78 },
79 .type = BT_VALUE_TYPE_NULL,
80 .frozen = BT_TRUE,
81 };
82
83 struct bt_value *const bt_value_null = &bt_value_null_instance;
84
85 static
86 void bt_value_destroy(struct bt_object *obj);
87
88 static
89 void bt_value_string_destroy(struct bt_value *object)
90 {
91 g_string_free(BT_VALUE_TO_STRING(object)->gstr, TRUE);
92 BT_VALUE_TO_STRING(object)->gstr = NULL;
93 }
94
95 static
96 void bt_value_array_destroy(struct bt_value *object)
97 {
98 /*
99 * Pointer array's registered value destructor will take care
100 * of putting each contained object.
101 */
102 g_ptr_array_free(BT_VALUE_TO_ARRAY(object)->garray, TRUE);
103 BT_VALUE_TO_ARRAY(object)->garray = NULL;
104 }
105
106 static
107 void bt_value_map_destroy(struct bt_value *object)
108 {
109 /*
110 * Hash table's registered value destructor will take care of
111 * putting each contained object. Keys are GQuarks and cannot
112 * be destroyed anyway.
113 */
114 g_hash_table_destroy(BT_VALUE_TO_MAP(object)->ght);
115 BT_VALUE_TO_MAP(object)->ght = NULL;
116 }
117
118 static
119 void (* const destroy_funcs[])(struct bt_value *) = {
120 [BT_VALUE_TYPE_NULL] = NULL,
121 [BT_VALUE_TYPE_BOOL] = NULL,
122 [BT_VALUE_TYPE_UNSIGNED_INTEGER] = NULL,
123 [BT_VALUE_TYPE_SIGNED_INTEGER] = NULL,
124 [BT_VALUE_TYPE_REAL] = NULL,
125 [BT_VALUE_TYPE_STRING] = bt_value_string_destroy,
126 [BT_VALUE_TYPE_ARRAY] = bt_value_array_destroy,
127 [BT_VALUE_TYPE_MAP] = bt_value_map_destroy,
128 };
129
130 static
131 struct bt_value *bt_value_null_copy(const struct bt_value *null_obj)
132 {
133 return (void *) bt_value_null;
134 }
135
136 static
137 struct bt_value *bt_value_bool_copy(const struct bt_value *bool_obj)
138 {
139 return bt_value_bool_create_init(
140 BT_VALUE_TO_BOOL(bool_obj)->value);
141 }
142
143 static inline
144 struct bt_value *bt_value_integer_create_init(enum bt_value_type type,
145 uint64_t uval);
146
147 static
148 struct bt_value *bt_value_integer_copy(
149 const struct bt_value *integer_obj)
150 {
151 return bt_value_integer_create_init(integer_obj->type,
152 BT_VALUE_TO_INTEGER(integer_obj)->value.u);
153 }
154
155 static
156 struct bt_value *bt_value_real_copy(const struct bt_value *real_obj)
157 {
158 return bt_value_real_create_init(
159 BT_VALUE_TO_REAL(real_obj)->value);
160 }
161
162 static
163 struct bt_value *bt_value_string_copy(const struct bt_value *string_obj)
164 {
165 return bt_value_string_create_init(
166 BT_VALUE_TO_STRING(string_obj)->gstr->str);
167 }
168
169 static
170 struct bt_value *bt_value_array_copy(const struct bt_value *array_obj)
171 {
172 int i;
173 int ret;
174 struct bt_value *copy_obj;
175 struct bt_value_array *typed_array_obj;
176
177 BT_LOGD("Copying array value: addr=%p", array_obj);
178 typed_array_obj = BT_VALUE_TO_ARRAY(array_obj);
179 copy_obj = bt_value_array_create();
180 if (!copy_obj) {
181 BT_LOGE_STR("Cannot create empty array value.");
182 goto end;
183 }
184
185 for (i = 0; i < typed_array_obj->garray->len; ++i) {
186 struct bt_value *element_obj_copy = NULL;
187 const struct bt_value *element_obj =
188 bt_value_array_borrow_element_by_index_const(
189 array_obj, i);
190
191 BT_ASSERT(element_obj);
192 BT_LOGD("Copying array value's element: element-addr=%p, "
193 "index=%d", element_obj, i);
194 ret = bt_value_copy(element_obj, &element_obj_copy);
195 if (ret) {
196 BT_LOGE("Cannot copy array value's element: "
197 "array-addr=%p, index=%d",
198 array_obj, i);
199 BT_OBJECT_PUT_REF_AND_RESET(copy_obj);
200 goto end;
201 }
202
203 BT_ASSERT(element_obj_copy);
204 ret = bt_value_array_append_element(copy_obj,
205 (void *) element_obj_copy);
206 BT_OBJECT_PUT_REF_AND_RESET(element_obj_copy);
207 if (ret) {
208 BT_LOGE("Cannot append to array value: addr=%p",
209 array_obj);
210 BT_OBJECT_PUT_REF_AND_RESET(copy_obj);
211 goto end;
212 }
213 }
214
215 BT_LOGD("Copied array value: original-addr=%p, copy-addr=%p",
216 array_obj, copy_obj);
217
218 end:
219 return copy_obj;
220 }
221
222 static
223 struct bt_value *bt_value_map_copy(const struct bt_value *map_obj)
224 {
225 int ret;
226 GHashTableIter iter;
227 gpointer key, element_obj;
228 struct bt_value *copy_obj;
229 struct bt_value *element_obj_copy = NULL;
230 struct bt_value_map *typed_map_obj;
231
232 BT_LOGD("Copying map value: addr=%p", map_obj);
233 typed_map_obj = BT_VALUE_TO_MAP(map_obj);
234 copy_obj = bt_value_map_create();
235 if (!copy_obj) {
236 goto end;
237 }
238
239 g_hash_table_iter_init(&iter, typed_map_obj->ght);
240
241 while (g_hash_table_iter_next(&iter, &key, &element_obj)) {
242 const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key));
243
244 BT_ASSERT(key_str);
245 BT_LOGD("Copying map value's element: element-addr=%p, "
246 "key=\"%s\"", element_obj, key_str);
247 ret = bt_value_copy(element_obj, &element_obj_copy);
248 if (ret) {
249 BT_LOGE("Cannot copy map value's element: "
250 "map-addr=%p, key=\"%s\"",
251 map_obj, key_str);
252 BT_OBJECT_PUT_REF_AND_RESET(copy_obj);
253 goto end;
254 }
255
256 BT_ASSERT(element_obj_copy);
257 ret = bt_value_map_insert_entry(copy_obj, key_str,
258 (void *) element_obj_copy);
259 BT_OBJECT_PUT_REF_AND_RESET(element_obj_copy);
260 if (ret) {
261 BT_LOGE("Cannot insert into map value: addr=%p, key=\"%s\"",
262 map_obj, key_str);
263 BT_OBJECT_PUT_REF_AND_RESET(copy_obj);
264 goto end;
265 }
266 }
267
268 BT_LOGD("Copied map value: addr=%p", map_obj);
269
270 end:
271 return copy_obj;
272 }
273
274 static
275 struct bt_value *(* const copy_funcs[])(const struct bt_value *) = {
276 [BT_VALUE_TYPE_NULL] = bt_value_null_copy,
277 [BT_VALUE_TYPE_BOOL] = bt_value_bool_copy,
278 [BT_VALUE_TYPE_UNSIGNED_INTEGER] = bt_value_integer_copy,
279 [BT_VALUE_TYPE_SIGNED_INTEGER] = bt_value_integer_copy,
280 [BT_VALUE_TYPE_REAL] = bt_value_real_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 if (BT_VALUE_TO_BOOL(object_a)->value !=
303 BT_VALUE_TO_BOOL(object_b)->value) {
304 BT_LOGT("Boolean value objects are different: "
305 "bool-a-val=%d, bool-b-val=%d",
306 BT_VALUE_TO_BOOL(object_a)->value,
307 BT_VALUE_TO_BOOL(object_b)->value);
308 return BT_FALSE;
309 }
310
311 return BT_TRUE;
312 }
313
314 static
315 bt_bool bt_value_integer_compare(const struct bt_value *object_a,
316 const struct bt_value *object_b)
317 {
318 if (BT_VALUE_TO_INTEGER(object_a)->value.u !=
319 BT_VALUE_TO_INTEGER(object_b)->value.u) {
320 if (object_a->type == BT_VALUE_TYPE_UNSIGNED_INTEGER) {
321 BT_LOGT("Unsigned integer value objects are different: "
322 "int-a-val=%" PRIu64 ", int-b-val=%" PRIu64,
323 BT_VALUE_TO_INTEGER(object_a)->value.u,
324 BT_VALUE_TO_INTEGER(object_b)->value.u);
325 } else {
326 BT_LOGT("Signed integer value objects are different: "
327 "int-a-val=%" PRId64 ", int-b-val=%" PRId64,
328 BT_VALUE_TO_INTEGER(object_a)->value.i,
329 BT_VALUE_TO_INTEGER(object_b)->value.i);
330 }
331
332 return BT_FALSE;
333 }
334
335 return BT_TRUE;
336 }
337
338 static
339 bt_bool bt_value_real_compare(const struct bt_value *object_a,
340 const struct bt_value *object_b)
341 {
342 if (BT_VALUE_TO_REAL(object_a)->value !=
343 BT_VALUE_TO_REAL(object_b)->value) {
344 BT_LOGT("Real number value objects are different: "
345 "real-a-val=%f, real-b-val=%f",
346 BT_VALUE_TO_REAL(object_a)->value,
347 BT_VALUE_TO_REAL(object_b)->value);
348 return BT_FALSE;
349 }
350
351 return BT_TRUE;
352 }
353
354 static
355 bt_bool bt_value_string_compare(const struct bt_value *object_a,
356 const struct bt_value *object_b)
357 {
358 if (strcmp(BT_VALUE_TO_STRING(object_a)->gstr->str,
359 BT_VALUE_TO_STRING(object_b)->gstr->str) != 0) {
360 BT_LOGT("String value objects are different: "
361 "string-a-val=\"%s\", string-b-val=\"%s\"",
362 BT_VALUE_TO_STRING(object_a)->gstr->str,
363 BT_VALUE_TO_STRING(object_b)->gstr->str);
364 return BT_FALSE;
365 }
366
367 return BT_TRUE;
368 }
369
370 static
371 bt_bool bt_value_array_compare(const struct bt_value *object_a,
372 const struct bt_value *object_b)
373 {
374 int i;
375 bt_bool ret = BT_TRUE;
376 const struct bt_value_array *array_obj_a =
377 BT_VALUE_TO_ARRAY(object_a);
378
379 if (bt_value_array_get_size(object_a) !=
380 bt_value_array_get_size(object_b)) {
381 BT_LOGT("Array values are different: size mismatch "
382 "value-a-addr=%p, value-b-addr=%p, "
383 "value-a-size=%" PRId64 ", value-b-size=%" PRId64,
384 object_a, object_b,
385 bt_value_array_get_size(object_a),
386 bt_value_array_get_size(object_b));
387 ret = BT_FALSE;
388 goto end;
389 }
390
391 for (i = 0; i < array_obj_a->garray->len; ++i) {
392 const struct bt_value *element_obj_a;
393 const struct bt_value *element_obj_b;
394
395 element_obj_a = bt_value_array_borrow_element_by_index_const(
396 object_a, i);
397 element_obj_b = bt_value_array_borrow_element_by_index_const(
398 object_b, i);
399
400 if (!bt_value_compare(element_obj_a, element_obj_b)) {
401 BT_LOGT("Array values's elements are different: "
402 "value-a-addr=%p, value-b-addr=%p, index=%d",
403 element_obj_a, element_obj_b, i);
404 ret = BT_FALSE;
405 goto end;
406 }
407 }
408
409 end:
410 return ret;
411 }
412
413 static
414 bt_bool bt_value_map_compare(const struct bt_value *object_a,
415 const struct bt_value *object_b)
416 {
417 bt_bool ret = BT_TRUE;
418 GHashTableIter iter;
419 gpointer key, element_obj_a;
420 const struct bt_value_map *map_obj_a = BT_VALUE_TO_MAP(object_a);
421
422 if (bt_value_map_get_size(object_a) !=
423 bt_value_map_get_size(object_b)) {
424 BT_LOGT("Map values are different: size mismatch "
425 "value-a-addr=%p, value-b-addr=%p, "
426 "value-a-size=%" PRId64 ", value-b-size=%" PRId64,
427 object_a, object_b,
428 bt_value_map_get_size(object_a),
429 bt_value_map_get_size(object_b));
430 ret = BT_FALSE;
431 goto end;
432 }
433
434 g_hash_table_iter_init(&iter, map_obj_a->ght);
435
436 while (g_hash_table_iter_next(&iter, &key, &element_obj_a)) {
437 const struct bt_value *element_obj_b;
438 const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key));
439
440 element_obj_b = bt_value_map_borrow_entry_value_const(object_b,
441 key_str);
442
443 if (!bt_value_compare(element_obj_a, element_obj_b)) {
444 BT_LOGT("Map values's elements are different: "
445 "value-a-addr=%p, value-b-addr=%p, key=\"%s\"",
446 element_obj_a, element_obj_b, key_str);
447 ret = BT_FALSE;
448 goto end;
449 }
450 }
451
452 end:
453 return ret;
454 }
455
456 static
457 bt_bool (* const compare_funcs[])(const struct bt_value *,
458 const struct bt_value *) = {
459 [BT_VALUE_TYPE_NULL] = bt_value_null_compare,
460 [BT_VALUE_TYPE_BOOL] = bt_value_bool_compare,
461 [BT_VALUE_TYPE_UNSIGNED_INTEGER] = bt_value_integer_compare,
462 [BT_VALUE_TYPE_SIGNED_INTEGER] = bt_value_integer_compare,
463 [BT_VALUE_TYPE_REAL] = bt_value_real_compare,
464 [BT_VALUE_TYPE_STRING] = bt_value_string_compare,
465 [BT_VALUE_TYPE_ARRAY] = bt_value_array_compare,
466 [BT_VALUE_TYPE_MAP] = bt_value_map_compare,
467 };
468
469 static
470 void bt_value_null_freeze(struct bt_value *object)
471 {
472 }
473
474 static
475 void bt_value_generic_freeze(struct bt_value *object)
476 {
477 object->frozen = BT_TRUE;
478 }
479
480 static
481 void bt_value_array_freeze(struct bt_value *object)
482 {
483 int i;
484 struct bt_value_array *typed_array_obj =
485 BT_VALUE_TO_ARRAY(object);
486
487 for (i = 0; i < typed_array_obj->garray->len; ++i) {
488 bt_value_freeze(g_ptr_array_index(typed_array_obj->garray, i));
489 }
490
491 bt_value_generic_freeze(object);
492 }
493
494 static
495 void bt_value_map_freeze(struct bt_value *object)
496 {
497 GHashTableIter iter;
498 gpointer key, element_obj;
499 const struct bt_value_map *map_obj = BT_VALUE_TO_MAP(object);
500
501 g_hash_table_iter_init(&iter, map_obj->ght);
502
503 while (g_hash_table_iter_next(&iter, &key, &element_obj)) {
504 bt_value_freeze(element_obj);
505 }
506
507 bt_value_generic_freeze(object);
508 }
509
510 static
511 void (* const freeze_funcs[])(struct bt_value *) = {
512 [BT_VALUE_TYPE_NULL] = bt_value_null_freeze,
513 [BT_VALUE_TYPE_BOOL] = bt_value_generic_freeze,
514 [BT_VALUE_TYPE_UNSIGNED_INTEGER] = bt_value_generic_freeze,
515 [BT_VALUE_TYPE_SIGNED_INTEGER] = bt_value_generic_freeze,
516 [BT_VALUE_TYPE_REAL] = bt_value_generic_freeze,
517 [BT_VALUE_TYPE_STRING] = bt_value_generic_freeze,
518 [BT_VALUE_TYPE_ARRAY] = bt_value_array_freeze,
519 [BT_VALUE_TYPE_MAP] = bt_value_map_freeze,
520 };
521
522 static
523 void bt_value_destroy(struct bt_object *obj)
524 {
525 struct bt_value *value;
526
527 value = container_of(obj, struct bt_value, base);
528 BT_LOGD("Destroying value: addr=%p", value);
529
530 if (bt_value_is_null(value)) {
531 BT_LOGD_STR("Not destroying the null value singleton.");
532 return;
533 }
534
535 if (destroy_funcs[value->type]) {
536 destroy_funcs[value->type](value);
537 }
538
539 g_free(value);
540 }
541
542 BT_HIDDEN
543 void _bt_value_freeze(const struct bt_value *c_object)
544 {
545 const struct bt_value *object = (void *) c_object;
546
547 BT_ASSERT(object);
548
549 if (object->frozen) {
550 goto end;
551 }
552
553 BT_LOGD("Freezing value: addr=%p", object);
554 freeze_funcs[object->type]((void *) object);
555
556 end:
557 return;
558 }
559
560 enum bt_value_type bt_value_get_type(const struct bt_value *object)
561 {
562 BT_ASSERT_PRE_NON_NULL(object, "Value object");
563 return object->type;
564 }
565
566 static
567 struct bt_value bt_value_create_base(enum bt_value_type type)
568 {
569 struct bt_value value;
570
571 value.type = type;
572 value.frozen = BT_FALSE;
573 bt_object_init_shared(&value.base, bt_value_destroy);
574 return value;
575 }
576
577 struct bt_value *bt_value_bool_create_init(bt_bool val)
578 {
579 struct bt_value_bool *bool_obj;
580
581 BT_LOGD("Creating boolean value object: val=%d", val);
582 bool_obj = g_new0(struct bt_value_bool, 1);
583 if (!bool_obj) {
584 BT_LOGE_STR("Failed to allocate one boolean value object.");
585 goto end;
586 }
587
588 bool_obj->base = bt_value_create_base(BT_VALUE_TYPE_BOOL);
589 bool_obj->value = val;
590 BT_LOGD("Created boolean value object: addr=%p", bool_obj);
591
592 end:
593 return (void *) bool_obj;
594 }
595
596 struct bt_value *bt_value_bool_create(void)
597 {
598 return bt_value_bool_create_init(BT_FALSE);
599 }
600
601 static inline
602 struct bt_value *bt_value_integer_create_init(enum bt_value_type type,
603 uint64_t uval)
604 {
605 struct bt_value_integer *integer_obj;
606
607 BT_ASSERT(type == BT_VALUE_TYPE_UNSIGNED_INTEGER ||
608 type == BT_VALUE_TYPE_SIGNED_INTEGER);
609
610 if (type == BT_VALUE_TYPE_UNSIGNED_INTEGER) {
611 BT_LOGD("Creating unsigned integer value object: val=%" PRIu64,
612 uval);
613 } else {
614 BT_LOGD("Creating signed integer value object: val=%" PRId64,
615 (int64_t) uval);
616 }
617
618 integer_obj = g_new0(struct bt_value_integer, 1);
619 if (!integer_obj) {
620 BT_LOGE_STR("Failed to allocate one integer value object.");
621 goto end;
622 }
623
624 integer_obj->base = bt_value_create_base(type);
625 integer_obj->value.u = uval;
626 BT_LOGD("Created %ssigned integer value object: addr=%p",
627 type == BT_VALUE_TYPE_UNSIGNED_INTEGER ? "un" : "",
628 integer_obj);
629
630 end:
631 return (void *) integer_obj;
632 }
633
634 struct bt_value *bt_value_unsigned_integer_create_init(uint64_t val)
635 {
636 return bt_value_integer_create_init(BT_VALUE_TYPE_UNSIGNED_INTEGER,
637 val);
638 }
639
640 struct bt_value *bt_value_unsigned_integer_create(void)
641 {
642 return bt_value_unsigned_integer_create_init(0);
643 }
644
645 struct bt_value *bt_value_signed_integer_create_init(int64_t val)
646 {
647 return bt_value_integer_create_init(BT_VALUE_TYPE_SIGNED_INTEGER,
648 (uint64_t) val);
649 }
650
651 struct bt_value *bt_value_signed_integer_create(void)
652 {
653 return bt_value_signed_integer_create_init(0);
654 }
655
656 struct bt_value *bt_value_real_create_init(double val)
657 {
658 struct bt_value_real *real_obj;
659
660 BT_LOGD("Creating real number value object: val=%f", val);
661 real_obj = g_new0(struct bt_value_real, 1);
662 if (!real_obj) {
663 BT_LOGE_STR("Failed to allocate one real number value object.");
664 goto end;
665 }
666
667 real_obj->base = bt_value_create_base(BT_VALUE_TYPE_REAL);
668 real_obj->value = val;
669 BT_LOGD("Created real number value object: addr=%p",
670 real_obj);
671
672 end:
673 return (void *) real_obj;
674 }
675
676 struct bt_value *bt_value_real_create(void)
677 {
678 return bt_value_real_create_init(0.);
679 }
680
681 struct bt_value *bt_value_string_create_init(const char *val)
682 {
683 struct bt_value_string *string_obj = NULL;
684
685 if (!val) {
686 BT_LOGW_STR("Invalid parameter: value is NULL.");
687 goto end;
688 }
689
690 BT_LOGD("Creating string value object: val-len=%zu", strlen(val));
691 string_obj = g_new0(struct bt_value_string, 1);
692 if (!string_obj) {
693 BT_LOGE_STR("Failed to allocate one string object.");
694 goto end;
695 }
696
697 string_obj->base = bt_value_create_base(BT_VALUE_TYPE_STRING);
698 string_obj->gstr = g_string_new(val);
699 if (!string_obj->gstr) {
700 BT_LOGE_STR("Failed to allocate a GString.");
701 g_free(string_obj);
702 string_obj = NULL;
703 goto end;
704 }
705
706 BT_LOGD("Created string value object: addr=%p",
707 string_obj);
708
709 end:
710 return (void *) string_obj;
711 }
712
713 struct bt_value *bt_value_string_create(void)
714 {
715 return bt_value_string_create_init("");
716 }
717
718 struct bt_value *bt_value_array_create(void)
719 {
720 struct bt_value_array *array_obj;
721
722 BT_LOGD_STR("Creating empty array value object.");
723 array_obj = g_new0(struct bt_value_array, 1);
724 if (!array_obj) {
725 BT_LOGE_STR("Failed to allocate one array object.");
726 goto end;
727 }
728
729 array_obj->base = bt_value_create_base(BT_VALUE_TYPE_ARRAY);
730 array_obj->garray = bt_g_ptr_array_new_full(0,
731 (GDestroyNotify) bt_object_put_ref);
732 if (!array_obj->garray) {
733 BT_LOGE_STR("Failed to allocate a GPtrArray.");
734 g_free(array_obj);
735 array_obj = NULL;
736 goto end;
737 }
738
739 BT_LOGD("Created array value object: addr=%p",
740 array_obj);
741
742 end:
743 return (void *) array_obj;
744 }
745
746 struct bt_value *bt_value_map_create(void)
747 {
748 struct bt_value_map *map_obj;
749
750 BT_LOGD_STR("Creating empty map value object.");
751 map_obj = g_new0(struct bt_value_map, 1);
752 if (!map_obj) {
753 BT_LOGE_STR("Failed to allocate one map object.");
754 goto end;
755 }
756
757 map_obj->base = bt_value_create_base(BT_VALUE_TYPE_MAP);
758 map_obj->ght = g_hash_table_new_full(g_direct_hash, g_direct_equal,
759 NULL, (GDestroyNotify) bt_object_put_ref);
760 if (!map_obj->ght) {
761 BT_LOGE_STR("Failed to allocate a GHashTable.");
762 g_free(map_obj);
763 map_obj = NULL;
764 goto end;
765 }
766
767 BT_LOGD("Created map value object: addr=%p",
768 map_obj);
769
770 end:
771 return (void *) map_obj;
772 }
773
774 bt_bool bt_value_bool_get(const struct bt_value *bool_obj)
775 {
776 BT_ASSERT_PRE_NON_NULL(bool_obj, "Value object");
777 BT_ASSERT_PRE_VALUE_IS_TYPE(bool_obj, BT_VALUE_TYPE_BOOL);
778 return BT_VALUE_TO_BOOL(bool_obj)->value;
779 }
780
781 void bt_value_bool_set(struct bt_value *bool_obj, bt_bool val)
782 {
783 BT_ASSERT_PRE_NON_NULL(bool_obj, "Value object");
784 BT_ASSERT_PRE_VALUE_IS_TYPE(bool_obj, BT_VALUE_TYPE_BOOL);
785 BT_ASSERT_PRE_VALUE_HOT(bool_obj, "Value object");
786 BT_VALUE_TO_BOOL(bool_obj)->value = val;
787 BT_LOGT("Set boolean value's raw value: value-addr=%p, value=%d",
788 bool_obj, val);
789 }
790
791 uint64_t bt_value_unsigned_integer_get(const struct bt_value *integer_obj)
792 {
793 BT_ASSERT_PRE_NON_NULL(integer_obj, "Value object");
794 BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj,
795 BT_VALUE_TYPE_UNSIGNED_INTEGER);
796 return BT_VALUE_TO_INTEGER(integer_obj)->value.u;
797 }
798
799 int64_t bt_value_signed_integer_get(const struct bt_value *integer_obj)
800 {
801 BT_ASSERT_PRE_NON_NULL(integer_obj, "Value object");
802 BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj,
803 BT_VALUE_TYPE_SIGNED_INTEGER);
804 return BT_VALUE_TO_INTEGER(integer_obj)->value.i;
805 }
806
807 static inline
808 void bt_value_integer_set(struct bt_value *integer_obj,
809 enum bt_value_type expected_type, uint64_t uval)
810 {
811 BT_ASSERT_PRE_NON_NULL(integer_obj, "Value object");
812 BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj, expected_type);
813 BT_ASSERT_PRE_VALUE_HOT(integer_obj, "Value object");
814 BT_VALUE_TO_INTEGER(integer_obj)->value.u = uval;
815 }
816
817 void bt_value_unsigned_integer_set(struct bt_value *integer_obj,
818 uint64_t val)
819 {
820 bt_value_integer_set(integer_obj, BT_VALUE_TYPE_UNSIGNED_INTEGER, val);
821 BT_LOGT("Set unsigned integer value's raw value: "
822 "value-addr=%p, value=%" PRIu64, integer_obj, val);
823 }
824
825 void bt_value_signed_integer_set(struct bt_value *integer_obj,
826 int64_t val)
827 {
828 bt_value_integer_set(integer_obj, BT_VALUE_TYPE_SIGNED_INTEGER,
829 (uint64_t) val);
830 BT_LOGT("Set signed integer value's raw value: "
831 "value-addr=%p, value=%" PRId64, integer_obj, val);
832 }
833
834 double bt_value_real_get(const struct bt_value *real_obj)
835 {
836 BT_ASSERT_PRE_NON_NULL(real_obj, "Value object");
837 BT_ASSERT_PRE_VALUE_IS_TYPE(real_obj, BT_VALUE_TYPE_REAL);
838 return BT_VALUE_TO_REAL(real_obj)->value;
839 }
840
841 void bt_value_real_set(struct bt_value *real_obj, double val)
842 {
843 BT_ASSERT_PRE_NON_NULL(real_obj, "Value object");
844 BT_ASSERT_PRE_VALUE_IS_TYPE(real_obj, BT_VALUE_TYPE_REAL);
845 BT_ASSERT_PRE_VALUE_HOT(real_obj, "Value object");
846 BT_VALUE_TO_REAL(real_obj)->value = val;
847 BT_LOGT("Set real number value's raw value: value-addr=%p, value=%f",
848 real_obj, val);
849 }
850
851 const char *bt_value_string_get(const struct bt_value *string_obj)
852 {
853 BT_ASSERT_PRE_NON_NULL(string_obj, "Value object");
854 BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj, BT_VALUE_TYPE_STRING);
855 return BT_VALUE_TO_STRING(string_obj)->gstr->str;
856 }
857
858 enum bt_value_string_set_status bt_value_string_set(
859 struct bt_value *string_obj, const char *val)
860 {
861 BT_ASSERT_PRE_NON_NULL(string_obj, "Value object");
862 BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj, BT_VALUE_TYPE_STRING);
863 BT_ASSERT_PRE_VALUE_HOT(string_obj, "Value object");
864 g_string_assign(BT_VALUE_TO_STRING(string_obj)->gstr, val);
865 BT_LOGT("Set string value's raw value: value-addr=%p, raw-value-addr=%p",
866 string_obj, val);
867 return BT_FUNC_STATUS_OK;
868 }
869
870 uint64_t bt_value_array_get_size(const struct bt_value *array_obj)
871 {
872 BT_ASSERT_PRE_NON_NULL(array_obj, "Value object");
873 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj, BT_VALUE_TYPE_ARRAY);
874 return (uint64_t) BT_VALUE_TO_ARRAY(array_obj)->garray->len;
875 }
876
877 struct bt_value *bt_value_array_borrow_element_by_index(
878 struct bt_value *array_obj, uint64_t index)
879 {
880 struct bt_value_array *typed_array_obj =
881 BT_VALUE_TO_ARRAY(array_obj);
882
883 BT_ASSERT_PRE_NON_NULL(array_obj, "Value object");
884 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj, BT_VALUE_TYPE_ARRAY);
885 BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index,
886 typed_array_obj->garray->len);
887 return g_ptr_array_index(typed_array_obj->garray, index);
888 }
889
890 const struct bt_value *bt_value_array_borrow_element_by_index_const(
891 const struct bt_value *array_obj,
892 uint64_t index)
893 {
894 return bt_value_array_borrow_element_by_index(
895 (void *) array_obj, index);
896 }
897
898 enum bt_value_array_append_element_status bt_value_array_append_element(
899 struct bt_value *array_obj,
900 struct bt_value *element_obj)
901 {
902 struct bt_value_array *typed_array_obj =
903 BT_VALUE_TO_ARRAY(array_obj);
904
905 BT_ASSERT_PRE_NON_NULL(array_obj, "Array value object");
906 BT_ASSERT_PRE_NON_NULL(element_obj, "Element value object");
907 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj, BT_VALUE_TYPE_ARRAY);
908 BT_ASSERT_PRE_VALUE_HOT(array_obj, "Array value object");
909 g_ptr_array_add(typed_array_obj->garray, element_obj);
910 bt_object_get_ref(element_obj);
911 BT_LOGT("Appended element to array value: array-value-addr=%p, "
912 "element-value-addr=%p, new-size=%u",
913 array_obj, element_obj, typed_array_obj->garray->len);
914 return BT_FUNC_STATUS_OK;
915 }
916
917 enum bt_value_array_append_element_status
918 bt_value_array_append_bool_element(struct bt_value *array_obj, bt_bool val)
919 {
920 enum bt_value_array_append_element_status ret;
921 struct bt_value *bool_obj = NULL;
922
923 bool_obj = bt_value_bool_create_init(val);
924 ret = bt_value_array_append_element(array_obj,
925 (void *) bool_obj);
926 bt_object_put_ref(bool_obj);
927 return ret;
928 }
929
930 enum bt_value_array_append_element_status
931 bt_value_array_append_unsigned_integer_element(struct bt_value *array_obj,
932 uint64_t val)
933 {
934 enum bt_value_array_append_element_status ret;
935 struct bt_value *integer_obj = NULL;
936
937 integer_obj = bt_value_unsigned_integer_create_init(val);
938 ret = bt_value_array_append_element(array_obj,
939 (void *) integer_obj);
940 bt_object_put_ref(integer_obj);
941 return ret;
942 }
943
944 enum bt_value_array_append_element_status
945 bt_value_array_append_signed_integer_element(struct bt_value *array_obj,
946 int64_t val)
947 {
948 enum bt_value_array_append_element_status ret;
949 struct bt_value *integer_obj = NULL;
950
951 integer_obj = bt_value_signed_integer_create_init(val);
952 ret = bt_value_array_append_element(array_obj,
953 (void *) integer_obj);
954 bt_object_put_ref(integer_obj);
955 return ret;
956 }
957
958 enum bt_value_array_append_element_status
959 bt_value_array_append_real_element(struct bt_value *array_obj, double val)
960 {
961 enum bt_value_array_append_element_status ret;
962 struct bt_value *real_obj = NULL;
963
964 real_obj = bt_value_real_create_init(val);
965 ret = bt_value_array_append_element(array_obj,
966 (void *) real_obj);
967 bt_object_put_ref(real_obj);
968 return ret;
969 }
970
971 enum bt_value_array_append_element_status
972 bt_value_array_append_string_element(struct bt_value *array_obj,
973 const char *val)
974 {
975 enum bt_value_array_append_element_status ret;
976 struct bt_value *string_obj = NULL;
977
978 string_obj = bt_value_string_create_init(val);
979 ret = bt_value_array_append_element(array_obj,
980 (void *) string_obj);
981 bt_object_put_ref(string_obj);
982 return ret;
983 }
984
985 enum bt_value_array_append_element_status
986 bt_value_array_append_empty_array_element(struct bt_value *array_obj)
987 {
988 enum bt_value_array_append_element_status ret;
989 struct bt_value *empty_array_obj = NULL;
990
991 empty_array_obj = bt_value_array_create();
992 ret = bt_value_array_append_element(array_obj,
993 (void *) empty_array_obj);
994 bt_object_put_ref(empty_array_obj);
995 return ret;
996 }
997
998 enum bt_value_array_append_element_status
999 bt_value_array_append_empty_map_element(struct bt_value *array_obj)
1000 {
1001 enum bt_value_array_append_element_status ret;
1002 struct bt_value *map_obj = NULL;
1003
1004 map_obj = bt_value_map_create();
1005 ret = bt_value_array_append_element(array_obj,
1006 (void *) map_obj);
1007 bt_object_put_ref(map_obj);
1008 return ret;
1009 }
1010
1011 enum bt_value_array_set_element_by_index_status
1012 bt_value_array_set_element_by_index(struct bt_value *array_obj, uint64_t index,
1013 struct bt_value *element_obj)
1014 {
1015 struct bt_value_array *typed_array_obj =
1016 BT_VALUE_TO_ARRAY(array_obj);
1017
1018 BT_ASSERT_PRE_NON_NULL(array_obj, "Array value object");
1019 BT_ASSERT_PRE_NON_NULL(element_obj, "Element value object");
1020 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj, BT_VALUE_TYPE_ARRAY);
1021 BT_ASSERT_PRE_VALUE_HOT(array_obj, "Array value object");
1022 BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index,
1023 typed_array_obj->garray->len);
1024 bt_object_put_ref(g_ptr_array_index(typed_array_obj->garray, index));
1025 g_ptr_array_index(typed_array_obj->garray, index) = element_obj;
1026 bt_object_get_ref(element_obj);
1027 BT_LOGT("Set array value's element: array-value-addr=%p, "
1028 "index=%" PRIu64 ", element-value-addr=%p",
1029 array_obj, index, element_obj);
1030 return BT_FUNC_STATUS_OK;
1031 }
1032
1033 uint64_t bt_value_map_get_size(const struct bt_value *map_obj)
1034 {
1035 BT_ASSERT_PRE_NON_NULL(map_obj, "Value object");
1036 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP);
1037 return (uint64_t) g_hash_table_size(BT_VALUE_TO_MAP(map_obj)->ght);
1038 }
1039
1040 struct bt_value *bt_value_map_borrow_entry_value(struct bt_value *map_obj,
1041 const char *key)
1042 {
1043 BT_ASSERT_PRE_NON_NULL(map_obj, "Value object");
1044 BT_ASSERT_PRE_NON_NULL(key, "Key");
1045 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP);
1046 return g_hash_table_lookup(BT_VALUE_TO_MAP(map_obj)->ght,
1047 GUINT_TO_POINTER(g_quark_from_string(key)));
1048 }
1049
1050 const struct bt_value *bt_value_map_borrow_entry_value_const(
1051 const struct bt_value *map_obj, const char *key)
1052 {
1053 return bt_value_map_borrow_entry_value((void *) map_obj, key);
1054 }
1055
1056 bt_bool bt_value_map_has_entry(const struct bt_value *map_obj, const char *key)
1057 {
1058 BT_ASSERT_PRE_NON_NULL(map_obj, "Value object");
1059 BT_ASSERT_PRE_NON_NULL(key, "Key");
1060 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP);
1061 return bt_g_hash_table_contains(BT_VALUE_TO_MAP(map_obj)->ght,
1062 GUINT_TO_POINTER(g_quark_from_string(key)));
1063 }
1064
1065 enum bt_value_map_insert_entry_status bt_value_map_insert_entry(
1066 struct bt_value *map_obj, const char *key,
1067 struct bt_value *element_obj)
1068 {
1069 BT_ASSERT_PRE_NON_NULL(map_obj, "Map value object");
1070 BT_ASSERT_PRE_NON_NULL(key, "Key");
1071 BT_ASSERT_PRE_NON_NULL(element_obj, "Element value object");
1072 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP);
1073 BT_ASSERT_PRE_VALUE_HOT(map_obj, "Map value object");
1074 g_hash_table_insert(BT_VALUE_TO_MAP(map_obj)->ght,
1075 GUINT_TO_POINTER(g_quark_from_string(key)), element_obj);
1076 bt_object_get_ref(element_obj);
1077 BT_LOGT("Inserted value into map value: map-value-addr=%p, "
1078 "key=\"%s\", element-value-addr=%p",
1079 map_obj, key, element_obj);
1080 return BT_FUNC_STATUS_OK;
1081 }
1082
1083 enum bt_value_map_insert_entry_status bt_value_map_insert_bool_entry(
1084 struct bt_value *map_obj, const char *key, bt_bool val)
1085 {
1086 enum bt_value_map_insert_entry_status ret;
1087 struct bt_value *bool_obj = NULL;
1088
1089 bool_obj = bt_value_bool_create_init(val);
1090 ret = bt_value_map_insert_entry(map_obj, key,
1091 (void *) bool_obj);
1092 bt_object_put_ref(bool_obj);
1093 return ret;
1094 }
1095
1096 enum bt_value_map_insert_entry_status
1097 bt_value_map_insert_unsigned_integer_entry(struct bt_value *map_obj,
1098 const char *key, uint64_t val)
1099 {
1100 enum bt_value_map_insert_entry_status ret;
1101 struct bt_value *integer_obj = NULL;
1102
1103 integer_obj = bt_value_unsigned_integer_create_init(val);
1104 ret = bt_value_map_insert_entry(map_obj, key,
1105 (void *) integer_obj);
1106 bt_object_put_ref(integer_obj);
1107 return ret;
1108 }
1109
1110 enum bt_value_map_insert_entry_status
1111 bt_value_map_insert_signed_integer_entry(struct bt_value *map_obj,
1112 const char *key, int64_t val)
1113 {
1114 enum bt_value_map_insert_entry_status ret;
1115 struct bt_value *integer_obj = NULL;
1116
1117 integer_obj = bt_value_signed_integer_create_init(val);
1118 ret = bt_value_map_insert_entry(map_obj, key,
1119 (void *) integer_obj);
1120 bt_object_put_ref(integer_obj);
1121 return ret;
1122 }
1123
1124 enum bt_value_map_insert_entry_status bt_value_map_insert_real_entry(
1125 struct bt_value *map_obj, const char *key, double val)
1126 {
1127 enum bt_value_map_insert_entry_status ret;
1128 struct bt_value *real_obj = NULL;
1129
1130 real_obj = bt_value_real_create_init(val);
1131 ret = bt_value_map_insert_entry(map_obj, key,
1132 (void *) real_obj);
1133 bt_object_put_ref(real_obj);
1134 return ret;
1135 }
1136
1137 enum bt_value_map_insert_entry_status bt_value_map_insert_string_entry(
1138 struct bt_value *map_obj, const char *key,
1139 const char *val)
1140 {
1141 enum bt_value_map_insert_entry_status ret;
1142 struct bt_value *string_obj = NULL;
1143
1144 string_obj = bt_value_string_create_init(val);
1145 ret = bt_value_map_insert_entry(map_obj, key,
1146 (void *) string_obj);
1147 bt_object_put_ref(string_obj);
1148 return ret;
1149 }
1150
1151 enum bt_value_map_insert_entry_status
1152 bt_value_map_insert_empty_array_entry(
1153 struct bt_value *map_obj, const char *key)
1154 {
1155 enum bt_value_map_insert_entry_status ret;
1156 struct bt_value *array_obj = NULL;
1157
1158 array_obj = bt_value_array_create();
1159 ret = bt_value_map_insert_entry(map_obj, key,
1160 (void *) array_obj);
1161 bt_object_put_ref(array_obj);
1162 return ret;
1163 }
1164
1165 enum bt_value_map_insert_entry_status
1166 bt_value_map_insert_empty_map_entry(struct bt_value *map_obj, const char *key)
1167 {
1168 enum bt_value_map_insert_entry_status ret;
1169 struct bt_value *empty_map_obj = NULL;
1170
1171 empty_map_obj = bt_value_map_create();
1172 ret = bt_value_map_insert_entry(map_obj, key,
1173 (void *) empty_map_obj);
1174 bt_object_put_ref(empty_map_obj);
1175 return ret;
1176 }
1177
1178 enum bt_value_map_foreach_entry_status bt_value_map_foreach_entry(
1179 struct bt_value *map_obj, bt_value_map_foreach_entry_func func,
1180 void *data)
1181 {
1182 enum bt_value_map_foreach_entry_status ret = BT_FUNC_STATUS_OK;
1183 gpointer key, element_obj;
1184 GHashTableIter iter;
1185 struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj);
1186
1187 BT_ASSERT_PRE_NON_NULL(map_obj, "Value object");
1188 BT_ASSERT_PRE_NON_NULL(func, "Callback");
1189 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP);
1190 g_hash_table_iter_init(&iter, typed_map_obj->ght);
1191
1192 while (g_hash_table_iter_next(&iter, &key, &element_obj)) {
1193 const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key));
1194
1195 if (!func(key_str, element_obj, data)) {
1196 BT_LOGT("User canceled the loop: key=\"%s\", "
1197 "value-addr=%p, data=%p",
1198 key_str, element_obj, data);
1199 ret = BT_FUNC_STATUS_CANCELED;
1200 break;
1201 }
1202 }
1203
1204 return ret;
1205 }
1206
1207 enum bt_value_map_foreach_entry_const_status bt_value_map_foreach_entry_const(
1208 const struct bt_value *map_obj,
1209 bt_value_map_foreach_entry_const_func func, void *data)
1210 {
1211 return (int) bt_value_map_foreach_entry((void *) map_obj,
1212 (bt_value_map_foreach_entry_func) func, data);
1213 }
1214
1215 struct extend_map_element_data {
1216 struct bt_value *extended_obj;
1217 int status;
1218 };
1219
1220 static
1221 bt_bool extend_map_element(const char *key,
1222 const struct bt_value *extension_obj_elem, void *data)
1223 {
1224 bt_bool ret = BT_TRUE;
1225 struct extend_map_element_data *extend_data = data;
1226 struct bt_value *extension_obj_elem_copy = NULL;
1227
1228 /* Copy object which is to replace the current one */
1229 extend_data->status = bt_value_copy(extension_obj_elem,
1230 &extension_obj_elem_copy);
1231 if (extend_data->status) {
1232 BT_LOGE("Cannot copy map element: addr=%p",
1233 extension_obj_elem);
1234 goto error;
1235 }
1236
1237 BT_ASSERT(extension_obj_elem_copy);
1238
1239 /* Replace in extended object */
1240 extend_data->status = bt_value_map_insert_entry(
1241 extend_data->extended_obj, key,
1242 (void *) extension_obj_elem_copy);
1243 if (extend_data->status) {
1244 BT_LOGE("Cannot replace value in extended value: key=\"%s\", "
1245 "extended-value-addr=%p, element-value-addr=%p",
1246 key, extend_data->extended_obj,
1247 extension_obj_elem_copy);
1248 goto error;
1249 }
1250
1251 goto end;
1252
1253 error:
1254 BT_ASSERT(extend_data->status != BT_FUNC_STATUS_OK);
1255 ret = BT_FALSE;
1256
1257 end:
1258 BT_OBJECT_PUT_REF_AND_RESET(extension_obj_elem_copy);
1259 return ret;
1260 }
1261
1262 enum bt_value_map_extend_status bt_value_map_extend(
1263 const struct bt_value *base_map_obj,
1264 const struct bt_value *extension_obj,
1265 struct bt_value **extended_map_obj)
1266 {
1267 struct extend_map_element_data extend_data = {
1268 .extended_obj = NULL,
1269 .status = BT_FUNC_STATUS_OK,
1270 };
1271
1272 BT_ASSERT_PRE_NON_NULL(base_map_obj, "Base value object");
1273 BT_ASSERT_PRE_NON_NULL(extension_obj, "Extension value object");
1274 BT_ASSERT_PRE_NON_NULL(extended_map_obj,
1275 "Extended value object (output)");
1276 BT_ASSERT_PRE_VALUE_IS_TYPE(base_map_obj, BT_VALUE_TYPE_MAP);
1277 BT_ASSERT_PRE_VALUE_IS_TYPE(extension_obj, BT_VALUE_TYPE_MAP);
1278 BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p",
1279 base_map_obj, extension_obj);
1280 *extended_map_obj = NULL;
1281
1282 /* Create copy of base map object to start with */
1283 extend_data.status = bt_value_copy(base_map_obj, extended_map_obj);
1284 if (extend_data.status) {
1285 BT_LOGE("Cannot copy base value: base-value-addr=%p",
1286 base_map_obj);
1287 goto error;
1288 }
1289
1290 BT_ASSERT(extended_map_obj);
1291
1292 /*
1293 * For each key in the extension map object, replace this key
1294 * in the copied map object.
1295 */
1296 extend_data.extended_obj = *extended_map_obj;
1297
1298 if (bt_value_map_foreach_entry_const(extension_obj, extend_map_element,
1299 &extend_data)) {
1300 BT_LOGE("Cannot iterate on the extension object's elements: "
1301 "extension-value-addr=%p", extension_obj);
1302 goto error;
1303 }
1304
1305 if (extend_data.status) {
1306 BT_LOGE("Failed to successfully iterate on the extension object's elements: "
1307 "extension-value-addr=%p", extension_obj);
1308 goto error;
1309 }
1310
1311 BT_LOGD("Extended map value: extended-value-addr=%p",
1312 *extended_map_obj);
1313 goto end;
1314
1315 error:
1316 BT_OBJECT_PUT_REF_AND_RESET(*extended_map_obj);
1317 *extended_map_obj = NULL;
1318
1319 end:
1320 return extend_data.status;
1321 }
1322
1323 enum bt_value_copy_status bt_value_copy(const struct bt_value *object,
1324 struct bt_value **copy_obj)
1325 {
1326 enum bt_value_copy_status status = BT_FUNC_STATUS_OK;
1327
1328 BT_ASSERT_PRE_NON_NULL(object, "Value object");
1329 BT_ASSERT_PRE_NON_NULL(copy_obj, "Value object copy (output)");
1330 BT_LOGD("Copying value object: addr=%p", object);
1331 *copy_obj = copy_funcs[object->type](object);
1332 if (*copy_obj) {
1333 BT_LOGD("Copied value object: copy-value-addr=%p",
1334 copy_obj);
1335 } else {
1336 status = BT_FUNC_STATUS_MEMORY_ERROR;
1337 *copy_obj = NULL;
1338 BT_LOGE_STR("Failed to copy value object.");
1339 }
1340
1341 return status;
1342 }
1343
1344 bt_bool bt_value_compare(const struct bt_value *object_a,
1345 const struct bt_value *object_b)
1346 {
1347 bt_bool ret = BT_FALSE;
1348
1349 BT_ASSERT_PRE_NON_NULL(object_a, "Value object A");
1350 BT_ASSERT_PRE_NON_NULL(object_b, "Value object B");
1351
1352 if (object_a->type != object_b->type) {
1353 BT_LOGT("Values are different: type mismatch: "
1354 "value-a-addr=%p, value-b-addr=%p, "
1355 "value-a-type=%s, value-b-type=%s",
1356 object_a, object_b,
1357 bt_common_value_type_string(object_a->type),
1358 bt_common_value_type_string(object_b->type));
1359 goto end;
1360 }
1361
1362 ret = compare_funcs[object_a->type](object_a, object_b);
1363
1364 end:
1365 return ret;
1366 }
1367
1368 void bt_value_get_ref(const struct bt_value *value)
1369 {
1370 bt_object_get_ref(value);
1371 }
1372
1373 void bt_value_put_ref(const struct bt_value *value)
1374 {
1375 bt_object_put_ref(value);
1376 }
This page took 0.061503 seconds and 4 git commands to generate.