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