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