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