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