Rename: "float value" -> "real value"
[babeltrace.git] / lib / values.c
CommitLineData
dac5c838 1/*
83509119 2 * Values.c: value objects
dac5c838
PP
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
0f5e83e5 28#define BT_LOG_TAG "VALUES"
40547d22 29#include <babeltrace/lib-logging-internal.h>
0f5e83e5 30
dac5c838
PP
31#include <stdlib.h>
32#include <string.h>
dac5c838 33#include <string.h>
2f42aa0a 34#include <inttypes.h>
3d9990ac 35#include <babeltrace/compiler-internal.h>
83509119 36#include <babeltrace/ref.h>
dac5c838 37#include <babeltrace/values.h>
3d9990ac 38#include <babeltrace/compat/glib-internal.h>
c55a9f58 39#include <babeltrace/types.h>
0f5e83e5 40#include <babeltrace/object-internal.h>
c4628760 41#include <babeltrace/values-internal.h>
f6ccaed9
PP
42#include <babeltrace/assert-internal.h>
43#include <babeltrace/assert-pre-internal.h>
2f42aa0a 44
dac5c838
PP
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))
a373bf69 48#define BT_VALUE_TO_REAL(_base) ((struct bt_value_real *) (_base))
dac5c838
PP
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
f6ccaed9
PP
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) \
e22b45d0 60 BT_ASSERT_PRE_HOT((_value), (_name), ": %!+v", (_value))
f6ccaed9
PP
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
dac5c838 68struct bt_value {
83509119 69 struct bt_object base;
dac5c838 70 enum bt_value_type type;
f6ccaed9 71 bt_bool frozen;
dac5c838
PP
72};
73
3fea54f6
PP
74static
75void bt_value_null_instance_release_func(struct bt_object *obj)
76{
77 BT_LOGW("Releasing the null value singleton: addr=%p", obj);
78}
79
dac5c838
PP
80static
81struct bt_value bt_value_null_instance = {
f685129c 82 .base = {
312c056a 83 .is_shared = true,
3fea54f6
PP
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,
f685129c 89 },
dac5c838 90 .type = BT_VALUE_TYPE_NULL,
f6ccaed9 91 .frozen = BT_TRUE,
dac5c838
PP
92};
93
94struct bt_value *bt_value_null = &bt_value_null_instance;
95
96struct bt_value_bool {
97 struct bt_value base;
c55a9f58 98 bt_bool value;
dac5c838
PP
99};
100
101struct bt_value_integer {
102 struct bt_value base;
103 int64_t value;
104};
105
a373bf69 106struct bt_value_real {
dac5c838
PP
107 struct bt_value base;
108 double value;
109};
110
111struct bt_value_string {
112 struct bt_value base;
113 GString *gstr;
114};
115
116struct bt_value_array {
117 struct bt_value base;
118 GPtrArray *garray;
119};
120
121struct bt_value_map {
122 struct bt_value base;
123 GHashTable *ght;
124};
125
126static
83509119 127void bt_value_destroy(struct bt_object *obj);
dac5c838
PP
128
129static
130void bt_value_string_destroy(struct bt_value *object)
131{
132 g_string_free(BT_VALUE_TO_STRING(object)->gstr, TRUE);
133}
134
135static
136void 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
145static
146void 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
156static
157void (* const destroy_funcs[])(struct bt_value *) = {
158 [BT_VALUE_TYPE_NULL] = NULL,
159 [BT_VALUE_TYPE_BOOL] = NULL,
160 [BT_VALUE_TYPE_INTEGER] = NULL,
a373bf69 161 [BT_VALUE_TYPE_REAL] = NULL,
dac5c838
PP
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
167static
168struct bt_value *bt_value_null_copy(const struct bt_value *null_obj)
169{
170 return bt_value_null;
171}
172
173static
174struct 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
179static
180struct 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
186static
a373bf69 187struct bt_value *bt_value_real_copy(const struct bt_value *real_obj)
dac5c838 188{
a373bf69
PP
189 return bt_value_real_create_init(
190 BT_VALUE_TO_REAL(real_obj)->value);
dac5c838
PP
191}
192
193static
194struct 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
200static
201struct 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
2f42aa0a 208 BT_LOGD("Copying array value: addr=%p", array_obj);
dac5c838
PP
209 typed_array_obj = BT_VALUE_TO_ARRAY(array_obj);
210 copy_obj = bt_value_array_create();
dac5c838 211 if (!copy_obj) {
2f42aa0a 212 BT_LOGE_STR("Cannot create empty array value.");
dac5c838
PP
213 goto end;
214 }
215
216 for (i = 0; i < typed_array_obj->garray->len; ++i) {
217 struct bt_value *element_obj_copy;
094ff7c0
PP
218 struct bt_value *element_obj = bt_value_array_borrow(
219 array_obj, i);
dac5c838 220
f6ccaed9 221 BT_ASSERT(element_obj);
40b59ed9
PP
222 BT_LOGD("Copying array value's element: element-addr=%p, "
223 "index=%d", element_obj, i);
dac5c838 224 element_obj_copy = bt_value_copy(element_obj);
dac5c838 225 if (!element_obj_copy) {
2f42aa0a
PP
226 BT_LOGE("Cannot copy array value's element: "
227 "array-addr=%p, index=%d",
228 array_obj, i);
83509119 229 BT_PUT(copy_obj);
dac5c838
PP
230 goto end;
231 }
232
233 ret = bt_value_array_append(copy_obj, element_obj_copy);
83509119 234 BT_PUT(element_obj_copy);
dac5c838 235 if (ret) {
2f42aa0a
PP
236 BT_LOGE("Cannot append to array value: addr=%p",
237 array_obj);
83509119 238 BT_PUT(copy_obj);
dac5c838
PP
239 goto end;
240 }
241 }
242
a704fb0b
PP
243 BT_LOGD("Copied array value: original-addr=%p, copy-addr=%p",
244 array_obj, copy_obj);
2f42aa0a 245
dac5c838
PP
246end:
247 return copy_obj;
248}
249
250static
251struct 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
2f42aa0a 260 BT_LOGD("Copying map value: addr=%p", map_obj);
dac5c838
PP
261 typed_map_obj = BT_VALUE_TO_MAP(map_obj);
262 copy_obj = bt_value_map_create();
dac5c838
PP
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)) {
5b44aff2 270 const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key));
dac5c838 271
f6ccaed9 272 BT_ASSERT(key_str);
40b59ed9
PP
273 BT_LOGD("Copying map value's element: element-addr=%p, "
274 "key=\"%s\"", element_obj, key_str);
dac5c838 275 element_obj_copy = bt_value_copy(element_obj);
dac5c838 276 if (!element_obj_copy) {
2f42aa0a
PP
277 BT_LOGE("Cannot copy map value's element: "
278 "map-addr=%p, key=\"%s\"",
279 map_obj, key_str);
83509119 280 BT_PUT(copy_obj);
dac5c838
PP
281 goto end;
282 }
283
284 ret = bt_value_map_insert(copy_obj, key_str, element_obj_copy);
83509119 285 BT_PUT(element_obj_copy);
dac5c838 286 if (ret) {
2f42aa0a
PP
287 BT_LOGE("Cannot insert into map value: addr=%p, key=\"%s\"",
288 map_obj, key_str);
83509119 289 BT_PUT(copy_obj);
dac5c838
PP
290 goto end;
291 }
292 }
293
2f42aa0a
PP
294 BT_LOGD("Copied map value: addr=%p", map_obj);
295
dac5c838
PP
296end:
297 return copy_obj;
298}
299
300static
301struct 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,
a373bf69 305 [BT_VALUE_TYPE_REAL] = bt_value_real_copy,
dac5c838
PP
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
311static
c55a9f58 312bt_bool bt_value_null_compare(const struct bt_value *object_a,
dac5c838
PP
313 const struct bt_value *object_b)
314{
315 /*
c55a9f58 316 * Always BT_TRUE since bt_value_compare() already checks if both
dac5c838
PP
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 */
c55a9f58 320 return BT_TRUE;
dac5c838
PP
321}
322
323static
c55a9f58 324bt_bool bt_value_bool_compare(const struct bt_value *object_a,
dac5c838
PP
325 const struct bt_value *object_b)
326{
40b59ed9
PP
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;
dac5c838
PP
337}
338
339static
c55a9f58 340bt_bool bt_value_integer_compare(const struct bt_value *object_a,
dac5c838
PP
341 const struct bt_value *object_b)
342{
40b59ed9
PP
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;
dac5c838
PP
353}
354
355static
a373bf69 356bt_bool bt_value_real_compare(const struct bt_value *object_a,
dac5c838
PP
357 const struct bt_value *object_b)
358{
a373bf69
PP
359 if (BT_VALUE_TO_REAL(object_a)->value !=
360 BT_VALUE_TO_REAL(object_b)->value) {
361 BT_LOGV("Real number value objects are different: "
362 "real-a-val=%f, real-b-val=%f",
363 BT_VALUE_TO_REAL(object_a)->value,
364 BT_VALUE_TO_REAL(object_b)->value);
40b59ed9
PP
365 return BT_FALSE;
366 }
367
368 return BT_TRUE;
dac5c838
PP
369}
370
371static
c55a9f58 372bt_bool bt_value_string_compare(const struct bt_value *object_a,
dac5c838
PP
373 const struct bt_value *object_b)
374{
40b59ed9
PP
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;
dac5c838
PP
385}
386
387static
c55a9f58 388bt_bool bt_value_array_compare(const struct bt_value *object_a,
dac5c838
PP
389 const struct bt_value *object_b)
390{
391 int i;
c55a9f58 392 bt_bool ret = BT_TRUE;
dac5c838
PP
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)) {
2f42aa0a
PP
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));
c55a9f58 403 ret = BT_FALSE;
dac5c838
PP
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
094ff7c0
PP
411 element_obj_a = bt_value_array_borrow(object_a, i);
412 element_obj_b = bt_value_array_borrow(object_b, i);
dac5c838
PP
413
414 if (!bt_value_compare(element_obj_a, element_obj_b)) {
2f42aa0a
PP
415 BT_LOGV("Array values's elements are different: "
416 "value-a-addr=%p, value-b-addr=%p, index=%d",
32e87ceb 417 element_obj_a, element_obj_b, i);
c55a9f58 418 ret = BT_FALSE;
dac5c838
PP
419 goto end;
420 }
dac5c838
PP
421 }
422
423end:
424 return ret;
425}
426
427static
c55a9f58 428bt_bool bt_value_map_compare(const struct bt_value *object_a,
dac5c838
PP
429 const struct bt_value *object_b)
430{
c55a9f58 431 bt_bool ret = BT_TRUE;
dac5c838
PP
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)) {
2f42aa0a
PP
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));
c55a9f58 443 ret = BT_FALSE;
dac5c838
PP
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;
5b44aff2 451 const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key));
dac5c838 452
094ff7c0 453 element_obj_b = bt_value_map_borrow(object_b, key_str);
dac5c838
PP
454
455 if (!bt_value_compare(element_obj_a, element_obj_b)) {
2f42aa0a
PP
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);
c55a9f58 459 ret = BT_FALSE;
dac5c838
PP
460 goto end;
461 }
dac5c838
PP
462 }
463
464end:
465 return ret;
466}
467
468static
c55a9f58 469bt_bool (* const compare_funcs[])(const struct bt_value *,
dac5c838
PP
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,
a373bf69 474 [BT_VALUE_TYPE_REAL] = bt_value_real_compare,
dac5c838 475 [BT_VALUE_TYPE_STRING] = bt_value_string_compare,
40b59ed9 476 [BT_VALUE_TYPE_ARRAY] = bt_value_array_compare,
dac5c838
PP
477 [BT_VALUE_TYPE_MAP] = bt_value_map_compare,
478};
479
f6ccaed9 480static
dac5c838
PP
481void bt_value_null_freeze(struct bt_value *object)
482{
483}
484
f6ccaed9 485static
dac5c838
PP
486void bt_value_generic_freeze(struct bt_value *object)
487{
f6ccaed9 488 object->frozen = BT_TRUE;
dac5c838
PP
489}
490
f6ccaed9 491static
dac5c838
PP
492void 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) {
f6ccaed9 499 bt_value_freeze(g_ptr_array_index(typed_array_obj->garray, i));
dac5c838
PP
500 }
501
502 bt_value_generic_freeze(object);
503}
504
f6ccaed9 505static
dac5c838
PP
506void 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
521static
522void (* 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,
a373bf69 526 [BT_VALUE_TYPE_REAL] = bt_value_generic_freeze,
dac5c838
PP
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
532static
83509119 533void bt_value_destroy(struct bt_object *obj)
dac5c838 534{
83509119 535 struct bt_value *value;
dac5c838 536
83509119 537 value = container_of(obj, struct bt_value, base);
2f42aa0a
PP
538 BT_LOGD("Destroying value: addr=%p", value);
539
83509119 540 if (bt_value_is_null(value)) {
2f42aa0a 541 BT_LOGD_STR("Not destroying the null value singleton.");
dac5c838
PP
542 return;
543 }
544
83509119
JG
545 if (destroy_funcs[value->type]) {
546 destroy_funcs[value->type](value);
dac5c838
PP
547 }
548
83509119 549 g_free(value);
dac5c838
PP
550}
551
f6ccaed9
PP
552BT_HIDDEN
553enum bt_value_status _bt_value_freeze(struct bt_value *object)
dac5c838
PP
554{
555 enum bt_value_status ret = BT_VALUE_STATUS_OK;
556
f6ccaed9 557 BT_ASSERT(object);
dac5c838 558
f6ccaed9 559 if (object->frozen) {
2f42aa0a
PP
560 goto end;
561 }
562
563 BT_LOGD("Freezing value: addr=%p", object);
dac5c838
PP
564 freeze_funcs[object->type](object);
565
566end:
567 return ret;
568}
569
dac5c838
PP
570enum bt_value_type bt_value_get_type(const struct bt_value *object)
571{
f6ccaed9 572 BT_ASSERT_PRE_NON_NULL(object, "Value object");
dac5c838
PP
573 return object->type;
574}
575
576static
577struct bt_value bt_value_create_base(enum bt_value_type type)
578{
3fea54f6 579 struct bt_value value;
dac5c838 580
3fea54f6
PP
581 value.type = type;
582 value.frozen = BT_FALSE;
583 bt_object_init_shared(&value.base, bt_value_destroy);
584 return value;
dac5c838
PP
585}
586
c55a9f58 587struct bt_value *bt_value_bool_create_init(bt_bool val)
dac5c838
PP
588{
589 struct bt_value_bool *bool_obj;
590
2f42aa0a 591 BT_LOGD("Creating boolean value object: val=%d", val);
dac5c838 592 bool_obj = g_new0(struct bt_value_bool, 1);
dac5c838 593 if (!bool_obj) {
2f42aa0a 594 BT_LOGE_STR("Failed to allocate one boolean value object.");
dac5c838
PP
595 goto end;
596 }
597
598 bool_obj->base = bt_value_create_base(BT_VALUE_TYPE_BOOL);
599 bool_obj->value = val;
2f42aa0a 600 BT_LOGD("Created boolean value object: addr=%p", bool_obj);
dac5c838
PP
601
602end:
603 return BT_VALUE_FROM_CONCRETE(bool_obj);
604}
605
606struct bt_value *bt_value_bool_create(void)
607{
c55a9f58 608 return bt_value_bool_create_init(BT_FALSE);
dac5c838
PP
609}
610
611struct bt_value *bt_value_integer_create_init(int64_t val)
612{
613 struct bt_value_integer *integer_obj;
614
2f42aa0a 615 BT_LOGD("Creating integer value object: val=%" PRId64, val);
dac5c838 616 integer_obj = g_new0(struct bt_value_integer, 1);
dac5c838 617 if (!integer_obj) {
2f42aa0a 618 BT_LOGE_STR("Failed to allocate one integer value object.");
dac5c838
PP
619 goto end;
620 }
621
622 integer_obj->base = bt_value_create_base(BT_VALUE_TYPE_INTEGER);
623 integer_obj->value = val;
2f42aa0a
PP
624 BT_LOGD("Created integer value object: addr=%p",
625 integer_obj);
dac5c838
PP
626
627end:
628 return BT_VALUE_FROM_CONCRETE(integer_obj);
629}
630
631struct bt_value *bt_value_integer_create(void)
632{
633 return bt_value_integer_create_init(0);
634}
635
a373bf69 636struct bt_value *bt_value_real_create_init(double val)
dac5c838 637{
a373bf69 638 struct bt_value_real *real_obj;
dac5c838 639
a373bf69
PP
640 BT_LOGD("Creating real number value object: val=%f", val);
641 real_obj = g_new0(struct bt_value_real, 1);
642 if (!real_obj) {
643 BT_LOGE_STR("Failed to allocate one real number value object.");
dac5c838
PP
644 goto end;
645 }
646
a373bf69
PP
647 real_obj->base = bt_value_create_base(BT_VALUE_TYPE_REAL);
648 real_obj->value = val;
649 BT_LOGD("Created real number value object: addr=%p",
650 real_obj);
dac5c838
PP
651
652end:
a373bf69 653 return BT_VALUE_FROM_CONCRETE(real_obj);
dac5c838
PP
654}
655
a373bf69 656struct bt_value *bt_value_real_create(void)
dac5c838 657{
a373bf69 658 return bt_value_real_create_init(0.);
dac5c838
PP
659}
660
661struct bt_value *bt_value_string_create_init(const char *val)
662{
663 struct bt_value_string *string_obj = NULL;
664
665 if (!val) {
2f42aa0a 666 BT_LOGW_STR("Invalid parameter: value is NULL.");
dac5c838
PP
667 goto end;
668 }
669
003c7749 670 BT_LOGD("Creating string value object: val-len=%zu", strlen(val));
dac5c838 671 string_obj = g_new0(struct bt_value_string, 1);
dac5c838 672 if (!string_obj) {
2f42aa0a 673 BT_LOGE_STR("Failed to allocate one string object.");
dac5c838
PP
674 goto end;
675 }
676
677 string_obj->base = bt_value_create_base(BT_VALUE_TYPE_STRING);
678 string_obj->gstr = g_string_new(val);
dac5c838 679 if (!string_obj->gstr) {
2f42aa0a 680 BT_LOGE_STR("Failed to allocate a GString.");
dac5c838
PP
681 g_free(string_obj);
682 string_obj = NULL;
683 goto end;
684 }
685
2f42aa0a
PP
686 BT_LOGD("Created string value object: addr=%p",
687 string_obj);
688
dac5c838
PP
689end:
690 return BT_VALUE_FROM_CONCRETE(string_obj);
691}
692
693struct bt_value *bt_value_string_create(void)
694{
695 return bt_value_string_create_init("");
696}
697
698struct bt_value *bt_value_array_create(void)
699{
700 struct bt_value_array *array_obj;
701
2f42aa0a 702 BT_LOGD_STR("Creating empty array value object.");
dac5c838 703 array_obj = g_new0(struct bt_value_array, 1);
dac5c838 704 if (!array_obj) {
2f42aa0a 705 BT_LOGE_STR("Failed to allocate one array object.");
dac5c838
PP
706 goto end;
707 }
708
709 array_obj->base = bt_value_create_base(BT_VALUE_TYPE_ARRAY);
5d5982ab 710 array_obj->garray = bt_g_ptr_array_new_full(0,
83509119 711 (GDestroyNotify) bt_put);
dac5c838 712 if (!array_obj->garray) {
2f42aa0a 713 BT_LOGE_STR("Failed to allocate a GPtrArray.");
dac5c838
PP
714 g_free(array_obj);
715 array_obj = NULL;
716 goto end;
717 }
718
2f42aa0a
PP
719 BT_LOGD("Created array value object: addr=%p",
720 array_obj);
721
dac5c838
PP
722end:
723 return BT_VALUE_FROM_CONCRETE(array_obj);
724}
725
726struct bt_value *bt_value_map_create(void)
727{
728 struct bt_value_map *map_obj;
729
2f42aa0a 730 BT_LOGD_STR("Creating empty map value object.");
dac5c838 731 map_obj = g_new0(struct bt_value_map, 1);
dac5c838 732 if (!map_obj) {
2f42aa0a 733 BT_LOGE_STR("Failed to allocate one map object.");
dac5c838
PP
734 goto end;
735 }
736
737 map_obj->base = bt_value_create_base(BT_VALUE_TYPE_MAP);
738 map_obj->ght = g_hash_table_new_full(g_direct_hash, g_direct_equal,
83509119 739 NULL, (GDestroyNotify) bt_put);
dac5c838 740 if (!map_obj->ght) {
2f42aa0a 741 BT_LOGE_STR("Failed to allocate a GHashTable.");
dac5c838
PP
742 g_free(map_obj);
743 map_obj = NULL;
744 goto end;
745 }
746
2f42aa0a
PP
747 BT_LOGD("Created map value object: addr=%p",
748 map_obj);
749
dac5c838
PP
750end:
751 return BT_VALUE_FROM_CONCRETE(map_obj);
752}
753
754enum bt_value_status bt_value_bool_get(const struct bt_value *bool_obj,
c55a9f58 755 bt_bool *val)
dac5c838 756{
f6ccaed9
PP
757 BT_ASSERT_PRE_NON_NULL(bool_obj, "Value object");
758 BT_ASSERT_PRE_NON_NULL(val, "Raw value");
759 BT_ASSERT_PRE_VALUE_IS_TYPE(bool_obj, BT_VALUE_TYPE_BOOL);
760 *val = BT_VALUE_TO_BOOL(bool_obj)->value;
761 return BT_VALUE_STATUS_OK;
dac5c838
PP
762}
763
c55a9f58 764enum bt_value_status bt_value_bool_set(struct bt_value *bool_obj, bt_bool val)
dac5c838 765{
f6ccaed9
PP
766 BT_ASSERT_PRE_NON_NULL(bool_obj, "Value object");
767 BT_ASSERT_PRE_VALUE_IS_TYPE(bool_obj, BT_VALUE_TYPE_BOOL);
768 BT_ASSERT_PRE_VALUE_HOT(bool_obj, "Value object");
769 BT_VALUE_TO_BOOL(bool_obj)->value = val;
2f42aa0a
PP
770 BT_LOGV("Set boolean value's raw value: value-addr=%p, value=%d",
771 bool_obj, val);
f6ccaed9 772 return BT_VALUE_STATUS_OK;
dac5c838
PP
773}
774
775enum bt_value_status bt_value_integer_get(const struct bt_value *integer_obj,
364747d6 776 int64_t *val)
dac5c838 777{
f6ccaed9
PP
778 BT_ASSERT_PRE_NON_NULL(integer_obj, "Value object");
779 BT_ASSERT_PRE_NON_NULL(val, "Raw value");
780 BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj, BT_VALUE_TYPE_INTEGER);
781 *val = BT_VALUE_TO_INTEGER(integer_obj)->value;
782 return BT_VALUE_STATUS_OK;
dac5c838
PP
783}
784
785enum bt_value_status bt_value_integer_set(struct bt_value *integer_obj,
364747d6 786 int64_t val)
dac5c838 787{
f6ccaed9
PP
788 BT_ASSERT_PRE_NON_NULL(integer_obj, "Value object");
789 BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj, BT_VALUE_TYPE_INTEGER);
790 BT_ASSERT_PRE_VALUE_HOT(integer_obj, "Value object");
791 BT_VALUE_TO_INTEGER(integer_obj)->value = val;
2f42aa0a
PP
792 BT_LOGV("Set integer value's raw value: value-addr=%p, value=%" PRId64,
793 integer_obj, val);
f6ccaed9 794 return BT_VALUE_STATUS_OK;
dac5c838
PP
795}
796
a373bf69 797enum bt_value_status bt_value_real_get(const struct bt_value *real_obj,
364747d6 798 double *val)
dac5c838 799{
a373bf69 800 BT_ASSERT_PRE_NON_NULL(real_obj, "Value object");
f6ccaed9 801 BT_ASSERT_PRE_NON_NULL(val, "Raw value");
a373bf69
PP
802 BT_ASSERT_PRE_VALUE_IS_TYPE(real_obj, BT_VALUE_TYPE_REAL);
803 *val = BT_VALUE_TO_REAL(real_obj)->value;
f6ccaed9 804 return BT_VALUE_STATUS_OK;
dac5c838
PP
805}
806
a373bf69 807enum bt_value_status bt_value_real_set(struct bt_value *real_obj,
364747d6 808 double val)
dac5c838 809{
a373bf69
PP
810 BT_ASSERT_PRE_NON_NULL(real_obj, "Value object");
811 BT_ASSERT_PRE_VALUE_IS_TYPE(real_obj, BT_VALUE_TYPE_REAL);
812 BT_ASSERT_PRE_VALUE_HOT(real_obj, "Value object");
813 BT_VALUE_TO_REAL(real_obj)->value = val;
814 BT_LOGV("Set real number value's raw value: value-addr=%p, value=%f",
815 real_obj, val);
f6ccaed9 816 return BT_VALUE_STATUS_OK;
dac5c838
PP
817}
818
819enum bt_value_status bt_value_string_get(const struct bt_value *string_obj,
364747d6 820 const char **val)
dac5c838 821{
f6ccaed9
PP
822 BT_ASSERT_PRE_NON_NULL(string_obj, "Value object");
823 BT_ASSERT_PRE_NON_NULL(val, "Raw value");
824 BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj, BT_VALUE_TYPE_STRING);
825 *val = BT_VALUE_TO_STRING(string_obj)->gstr->str;
826 return BT_VALUE_STATUS_OK;
dac5c838
PP
827}
828
829enum bt_value_status bt_value_string_set(struct bt_value *string_obj,
364747d6 830 const char *val)
dac5c838 831{
f6ccaed9
PP
832 BT_ASSERT_PRE_NON_NULL(string_obj, "Value object");
833 BT_ASSERT_PRE_NON_NULL(val, "Raw value");
834 BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj, BT_VALUE_TYPE_STRING);
835 BT_ASSERT_PRE_VALUE_HOT(string_obj, "Value object");
836 g_string_assign(BT_VALUE_TO_STRING(string_obj)->gstr, val);
2f42aa0a
PP
837 BT_LOGV("Set string value's raw value: value-addr=%p, raw-value-addr=%p",
838 string_obj, val);
f6ccaed9 839 return BT_VALUE_STATUS_OK;
dac5c838
PP
840}
841
544d0515 842int64_t bt_value_array_size(const struct bt_value *array_obj)
dac5c838 843{
f6ccaed9
PP
844 BT_ASSERT_PRE_NON_NULL(array_obj, "Value object");
845 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj, BT_VALUE_TYPE_ARRAY);
846 return (int64_t) BT_VALUE_TO_ARRAY(array_obj)->garray->len;
dac5c838
PP
847}
848
c55a9f58 849bt_bool bt_value_array_is_empty(const struct bt_value *array_obj)
dac5c838
PP
850{
851 return bt_value_array_size(array_obj) == 0;
852}
853
094ff7c0 854struct bt_value *bt_value_array_borrow(const struct bt_value *array_obj,
2f42aa0a 855 uint64_t index)
dac5c838 856{
dac5c838
PP
857 struct bt_value_array *typed_array_obj =
858 BT_VALUE_TO_ARRAY(array_obj);
859
f6ccaed9
PP
860 BT_ASSERT_PRE_NON_NULL(array_obj, "Value object");
861 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj, BT_VALUE_TYPE_ARRAY);
862 BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index,
863 typed_array_obj->garray->len);
094ff7c0 864 return g_ptr_array_index(typed_array_obj->garray, index);
dac5c838
PP
865}
866
867enum bt_value_status bt_value_array_append(struct bt_value *array_obj,
364747d6 868 struct bt_value *element_obj)
dac5c838 869{
dac5c838
PP
870 struct bt_value_array *typed_array_obj =
871 BT_VALUE_TO_ARRAY(array_obj);
872
f6ccaed9
PP
873 BT_ASSERT_PRE_NON_NULL(array_obj, "Array value object");
874 BT_ASSERT_PRE_NON_NULL(element_obj, "Element value object");
875 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj, BT_VALUE_TYPE_ARRAY);
876 BT_ASSERT_PRE_VALUE_HOT(array_obj, "Array value object");
dac5c838 877 g_ptr_array_add(typed_array_obj->garray, element_obj);
83509119 878 bt_get(element_obj);
2f42aa0a
PP
879 BT_LOGV("Appended element to array value: array-value-addr=%p, "
880 "element-value-addr=%p, new-size=%u",
881 array_obj, element_obj, typed_array_obj->garray->len);
f6ccaed9 882 return BT_VALUE_STATUS_OK;
dac5c838
PP
883}
884
885enum bt_value_status bt_value_array_append_bool(struct bt_value *array_obj,
c55a9f58 886 bt_bool val)
dac5c838
PP
887{
888 enum bt_value_status ret;
889 struct bt_value *bool_obj = NULL;
890
891 bool_obj = bt_value_bool_create_init(val);
892 ret = bt_value_array_append(array_obj, bool_obj);
83509119 893 bt_put(bool_obj);
dac5c838
PP
894 return ret;
895}
896
897enum bt_value_status bt_value_array_append_integer(
364747d6 898 struct bt_value *array_obj, int64_t val)
dac5c838
PP
899{
900 enum bt_value_status ret;
901 struct bt_value *integer_obj = NULL;
902
903 integer_obj = bt_value_integer_create_init(val);
904 ret = bt_value_array_append(array_obj, integer_obj);
83509119 905 bt_put(integer_obj);
dac5c838
PP
906 return ret;
907}
908
a373bf69 909enum bt_value_status bt_value_array_append_real(struct bt_value *array_obj,
364747d6 910 double val)
dac5c838
PP
911{
912 enum bt_value_status ret;
a373bf69 913 struct bt_value *real_obj = NULL;
dac5c838 914
a373bf69
PP
915 real_obj = bt_value_real_create_init(val);
916 ret = bt_value_array_append(array_obj, real_obj);
917 bt_put(real_obj);
dac5c838
PP
918 return ret;
919}
920
921enum bt_value_status bt_value_array_append_string(struct bt_value *array_obj,
364747d6 922 const char *val)
dac5c838
PP
923{
924 enum bt_value_status ret;
925 struct bt_value *string_obj = NULL;
926
927 string_obj = bt_value_string_create_init(val);
928 ret = bt_value_array_append(array_obj, string_obj);
83509119 929 bt_put(string_obj);
dac5c838
PP
930 return ret;
931}
932
5b79e8bf 933enum bt_value_status bt_value_array_append_empty_array(
364747d6 934 struct bt_value *array_obj)
dac5c838
PP
935{
936 enum bt_value_status ret;
937 struct bt_value *empty_array_obj = NULL;
938
939 empty_array_obj = bt_value_array_create();
940 ret = bt_value_array_append(array_obj, empty_array_obj);
83509119 941 bt_put(empty_array_obj);
dac5c838
PP
942 return ret;
943}
944
5b79e8bf 945enum bt_value_status bt_value_array_append_empty_map(struct bt_value *array_obj)
dac5c838
PP
946{
947 enum bt_value_status ret;
948 struct bt_value *map_obj = NULL;
949
950 map_obj = bt_value_map_create();
951 ret = bt_value_array_append(array_obj, map_obj);
83509119 952 bt_put(map_obj);
dac5c838
PP
953 return ret;
954}
955
956enum bt_value_status bt_value_array_set(struct bt_value *array_obj,
2f42aa0a 957 uint64_t index, struct bt_value *element_obj)
dac5c838 958{
dac5c838
PP
959 struct bt_value_array *typed_array_obj =
960 BT_VALUE_TO_ARRAY(array_obj);
961
f6ccaed9
PP
962 BT_ASSERT_PRE_NON_NULL(array_obj, "Array value object");
963 BT_ASSERT_PRE_NON_NULL(element_obj, "Element value object");
964 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj, BT_VALUE_TYPE_ARRAY);
965 BT_ASSERT_PRE_VALUE_HOT(array_obj, "Array value object");
966 BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index,
967 typed_array_obj->garray->len);
83509119 968 bt_put(g_ptr_array_index(typed_array_obj->garray, index));
dac5c838 969 g_ptr_array_index(typed_array_obj->garray, index) = element_obj;
83509119 970 bt_get(element_obj);
2f42aa0a
PP
971 BT_LOGV("Set array value's element: array-value-addr=%p, "
972 "index=%" PRIu64 ", element-value-addr=%p",
973 array_obj, index, element_obj);
f6ccaed9 974 return BT_VALUE_STATUS_OK;
dac5c838
PP
975}
976
544d0515 977int64_t bt_value_map_size(const struct bt_value *map_obj)
dac5c838 978{
f6ccaed9
PP
979 BT_ASSERT_PRE_NON_NULL(map_obj, "Value object");
980 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP);
981 return (int64_t) g_hash_table_size(BT_VALUE_TO_MAP(map_obj)->ght);
dac5c838
PP
982}
983
c55a9f58 984bt_bool bt_value_map_is_empty(const struct bt_value *map_obj)
dac5c838
PP
985{
986 return bt_value_map_size(map_obj) == 0;
987}
988
094ff7c0 989struct bt_value *bt_value_map_borrow(const struct bt_value *map_obj,
364747d6 990 const char *key)
dac5c838 991{
f6ccaed9
PP
992 BT_ASSERT_PRE_NON_NULL(map_obj, "Value object");
993 BT_ASSERT_PRE_NON_NULL(key, "Key");
994 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP);
094ff7c0
PP
995 return g_hash_table_lookup(BT_VALUE_TO_MAP(map_obj)->ght,
996 GUINT_TO_POINTER(g_quark_from_string(key)));
dac5c838
PP
997}
998
c55a9f58 999bt_bool bt_value_map_has_key(const struct bt_value *map_obj, const char *key)
dac5c838 1000{
f6ccaed9
PP
1001 BT_ASSERT_PRE_NON_NULL(map_obj, "Value object");
1002 BT_ASSERT_PRE_NON_NULL(key, "Key");
1003 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP);
1004 return bt_g_hash_table_contains(BT_VALUE_TO_MAP(map_obj)->ght,
1005 GUINT_TO_POINTER(g_quark_from_string(key)));
dac5c838
PP
1006}
1007
1008enum bt_value_status bt_value_map_insert(struct bt_value *map_obj,
364747d6 1009 const char *key, struct bt_value *element_obj)
dac5c838 1010{
f6ccaed9
PP
1011 BT_ASSERT_PRE_NON_NULL(map_obj, "Map value object");
1012 BT_ASSERT_PRE_NON_NULL(key, "Key");
1013 BT_ASSERT_PRE_NON_NULL(element_obj, "Element value object");
1014 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP);
1015 BT_ASSERT_PRE_VALUE_HOT(map_obj, "Map value object");
1016 g_hash_table_insert(BT_VALUE_TO_MAP(map_obj)->ght,
1017 GUINT_TO_POINTER(g_quark_from_string(key)), element_obj);
83509119 1018 bt_get(element_obj);
2f42aa0a
PP
1019 BT_LOGV("Inserted value into map value: map-value-addr=%p, "
1020 "key=\"%s\", element-value-addr=%p",
1021 map_obj, key, element_obj);
f6ccaed9 1022 return BT_VALUE_STATUS_OK;
dac5c838
PP
1023}
1024
1025enum bt_value_status bt_value_map_insert_bool(struct bt_value *map_obj,
c55a9f58 1026 const char *key, bt_bool val)
dac5c838
PP
1027{
1028 enum bt_value_status ret;
1029 struct bt_value *bool_obj = NULL;
1030
1031 bool_obj = bt_value_bool_create_init(val);
1032 ret = bt_value_map_insert(map_obj, key, bool_obj);
83509119 1033 bt_put(bool_obj);
dac5c838
PP
1034 return ret;
1035}
1036
1037enum bt_value_status bt_value_map_insert_integer(struct bt_value *map_obj,
364747d6 1038 const char *key, int64_t val)
dac5c838
PP
1039{
1040 enum bt_value_status ret;
1041 struct bt_value *integer_obj = NULL;
1042
1043 integer_obj = bt_value_integer_create_init(val);
1044 ret = bt_value_map_insert(map_obj, key, integer_obj);
83509119 1045 bt_put(integer_obj);
dac5c838
PP
1046 return ret;
1047}
1048
a373bf69 1049enum bt_value_status bt_value_map_insert_real(struct bt_value *map_obj,
364747d6 1050 const char *key, double val)
dac5c838
PP
1051{
1052 enum bt_value_status ret;
a373bf69 1053 struct bt_value *real_obj = NULL;
dac5c838 1054
a373bf69
PP
1055 real_obj = bt_value_real_create_init(val);
1056 ret = bt_value_map_insert(map_obj, key, real_obj);
1057 bt_put(real_obj);
dac5c838
PP
1058 return ret;
1059}
1060
1061enum bt_value_status bt_value_map_insert_string(struct bt_value *map_obj,
364747d6 1062 const char *key, const char *val)
dac5c838
PP
1063{
1064 enum bt_value_status ret;
1065 struct bt_value *string_obj = NULL;
1066
1067 string_obj = bt_value_string_create_init(val);
1068 ret = bt_value_map_insert(map_obj, key, string_obj);
83509119 1069 bt_put(string_obj);
dac5c838
PP
1070 return ret;
1071}
1072
5b79e8bf 1073enum bt_value_status bt_value_map_insert_empty_array(struct bt_value *map_obj,
364747d6 1074 const char *key)
dac5c838
PP
1075{
1076 enum bt_value_status ret;
1077 struct bt_value *array_obj = NULL;
1078
1079 array_obj = bt_value_array_create();
1080 ret = bt_value_map_insert(map_obj, key, array_obj);
83509119 1081 bt_put(array_obj);
dac5c838
PP
1082 return ret;
1083}
1084
5b79e8bf 1085enum bt_value_status bt_value_map_insert_empty_map(struct bt_value *map_obj,
364747d6 1086 const char *key)
dac5c838
PP
1087{
1088 enum bt_value_status ret;
1089 struct bt_value *empty_map_obj = NULL;
1090
1091 empty_map_obj = bt_value_map_create();
1092 ret = bt_value_map_insert(map_obj, key, empty_map_obj);
83509119 1093 bt_put(empty_map_obj);
dac5c838
PP
1094 return ret;
1095}
1096
1097enum bt_value_status bt_value_map_foreach(const struct bt_value *map_obj,
364747d6 1098 bt_value_map_foreach_cb cb, void *data)
dac5c838
PP
1099{
1100 enum bt_value_status ret = BT_VALUE_STATUS_OK;
1101 gpointer key, element_obj;
1102 GHashTableIter iter;
1103 struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj);
1104
f6ccaed9
PP
1105 BT_ASSERT_PRE_NON_NULL(map_obj, "Value object");
1106 BT_ASSERT_PRE_NON_NULL(cb, "Callback");
1107 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP);
dac5c838
PP
1108 g_hash_table_iter_init(&iter, typed_map_obj->ght);
1109
1110 while (g_hash_table_iter_next(&iter, &key, &element_obj)) {
5b44aff2 1111 const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key));
dac5c838
PP
1112
1113 if (!cb(key_str, element_obj, data)) {
f6ccaed9 1114 BT_LOGV("User canceled the loop: key=\"%s\", "
2f42aa0a
PP
1115 "value-addr=%p, data=%p",
1116 key_str, element_obj, data);
f6ccaed9 1117 ret = BT_VALUE_STATUS_CANCELED;
dac5c838
PP
1118 break;
1119 }
1120 }
1121
dac5c838
PP
1122 return ret;
1123}
1124
770750d3
PP
1125struct extend_map_element_data {
1126 struct bt_value *extended_obj;
c55a9f58 1127 bt_bool got_error;
770750d3
PP
1128};
1129
1130static
c55a9f58 1131bt_bool extend_map_element(const char *key,
770750d3
PP
1132 struct bt_value *extension_obj_elem, void *data)
1133{
c55a9f58 1134 bt_bool ret = BT_TRUE;
770750d3
PP
1135
1136 struct extend_map_element_data *extend_data = data;
1137
1138 /* Copy object which is to replace the current one */
1139 struct bt_value *extension_obj_elem_copy =
1140 bt_value_copy(extension_obj_elem);
1141
1142 /* Replace in extended object */
1143 if (bt_value_map_insert(extend_data->extended_obj, key,
1144 extension_obj_elem_copy)) {
2f42aa0a
PP
1145 BT_LOGE("Cannot replace value in extended value: key=\"%s\", "
1146 "extended-value-addr=%p, element-value-addr=%p",
1147 key, extend_data->extended_obj,
1148 extension_obj_elem_copy);
770750d3
PP
1149 goto error;
1150 }
1151
1152 goto end;
1153
1154error:
c55a9f58
PP
1155 ret = BT_FALSE;
1156 extend_data->got_error = BT_TRUE;
770750d3
PP
1157
1158end:
1159 BT_PUT(extension_obj_elem_copy);
770750d3
PP
1160 return ret;
1161}
1162
1163struct bt_value *bt_value_map_extend(struct bt_value *base_map_obj,
1164 struct bt_value *extension_obj)
1165{
1166 struct bt_value *extended_obj = NULL;
1167 struct extend_map_element_data extend_data = { 0 };
1168
f6ccaed9
PP
1169 BT_ASSERT_PRE_NON_NULL(base_map_obj, "Base value object");
1170 BT_ASSERT_PRE_NON_NULL(extension_obj, "Extension value object");
1171 BT_ASSERT_PRE_VALUE_IS_TYPE(base_map_obj, BT_VALUE_TYPE_MAP);
1172 BT_ASSERT_PRE_VALUE_IS_TYPE(extension_obj, BT_VALUE_TYPE_MAP);
2f42aa0a
PP
1173 BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p",
1174 base_map_obj, extension_obj);
1175
770750d3
PP
1176 /* Create copy of base map object to start with */
1177 extended_obj = bt_value_copy(base_map_obj);
1178 if (!extended_obj) {
2f42aa0a
PP
1179 BT_LOGE("Cannot copy base value: base-value-addr=%p",
1180 base_map_obj);
770750d3
PP
1181 goto error;
1182 }
1183
1184 /*
1185 * For each key in the extension map object, replace this key
1186 * in the copied map object.
1187 */
1188 extend_data.extended_obj = extended_obj;
1189
1190 if (bt_value_map_foreach(extension_obj, extend_map_element,
1191 &extend_data)) {
32e87ceb 1192 BT_LOGE("Cannot iterate on the extension object's elements: "
2f42aa0a 1193 "extension-value-addr=%p", extension_obj);
770750d3
PP
1194 goto error;
1195 }
1196
1197 if (extend_data.got_error) {
32e87ceb 1198 BT_LOGE("Failed to successfully iterate on the extension object's elements: "
2f42aa0a 1199 "extension-value-addr=%p", extension_obj);
770750d3
PP
1200 goto error;
1201 }
1202
2f42aa0a
PP
1203 BT_LOGD("Extended map value: extended-value-addr=%p",
1204 extended_obj);
770750d3
PP
1205 goto end;
1206
1207error:
1208 BT_PUT(extended_obj);
1209
1210end:
1211 return extended_obj;
1212}
1213
dac5c838
PP
1214struct bt_value *bt_value_copy(const struct bt_value *object)
1215{
1216 struct bt_value *copy_obj = NULL;
1217
f6ccaed9 1218 BT_ASSERT_PRE_NON_NULL(object, "Value object");
2f42aa0a 1219 BT_LOGD("Copying value object: addr=%p", object);
dac5c838 1220 copy_obj = copy_funcs[object->type](object);
2f42aa0a
PP
1221 if (copy_obj) {
1222 BT_LOGD("Copied value object: copy-value-addr=%p",
1223 copy_obj);
1224 } else {
1225 BT_LOGE_STR("Failed to copy value object.");
1226 }
dac5c838 1227
dac5c838
PP
1228 return copy_obj;
1229}
1230
c55a9f58 1231bt_bool bt_value_compare(const struct bt_value *object_a,
dac5c838
PP
1232 const struct bt_value *object_b)
1233{
c55a9f58 1234 bt_bool ret = BT_FALSE;
dac5c838 1235
f6ccaed9
PP
1236 BT_ASSERT_PRE_NON_NULL(object_a, "Value object A");
1237 BT_ASSERT_PRE_NON_NULL(object_b, "Value object B");
dac5c838
PP
1238
1239 if (object_a->type != object_b->type) {
2f42aa0a
PP
1240 BT_LOGV("Values are different: type mismatch: "
1241 "value-a-addr=%p, value-b-addr=%p, "
c4628760 1242 "value-a-type=%s, value-b-type=%s",
2f42aa0a 1243 object_a, object_b,
c4628760
PP
1244 bt_value_type_string(object_a->type),
1245 bt_value_type_string(object_b->type));
dac5c838
PP
1246 goto end;
1247 }
1248
1249 ret = compare_funcs[object_a->type](object_a, object_b);
1250
1251end:
1252 return ret;
1253}
This page took 0.098962 seconds and 4 git commands to generate.