lib/values.c: logging: log original and copy addresses
[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>
33#include <assert.h>
34#include <string.h>
2f42aa0a 35#include <inttypes.h>
3d9990ac 36#include <babeltrace/compiler-internal.h>
83509119 37#include <babeltrace/ref.h>
dac5c838 38#include <babeltrace/values.h>
3d9990ac 39#include <babeltrace/compat/glib-internal.h>
c55a9f58 40#include <babeltrace/types.h>
0f5e83e5 41#include <babeltrace/object-internal.h>
c4628760 42#include <babeltrace/values-internal.h>
2f42aa0a 43
dac5c838
PP
44#define BT_VALUE_FROM_CONCRETE(_concrete) ((struct bt_value *) (_concrete))
45#define BT_VALUE_TO_BOOL(_base) ((struct bt_value_bool *) (_base))
46#define BT_VALUE_TO_INTEGER(_base) ((struct bt_value_integer *) (_base))
47#define BT_VALUE_TO_FLOAT(_base) ((struct bt_value_float *) (_base))
48#define BT_VALUE_TO_STRING(_base) ((struct bt_value_string *) (_base))
49#define BT_VALUE_TO_ARRAY(_base) ((struct bt_value_array *) (_base))
50#define BT_VALUE_TO_MAP(_base) ((struct bt_value_map *) (_base))
51
52struct bt_value {
83509119 53 struct bt_object base;
dac5c838 54 enum bt_value_type type;
c55a9f58 55 bt_bool is_frozen;
dac5c838
PP
56};
57
58static
59struct bt_value bt_value_null_instance = {
f685129c
PP
60 .base = {
61 .ref_count = {
62 .count = 1,
63 .release = NULL,
64 },
65 .release = NULL,
66 .parent = NULL,
67 },
dac5c838 68 .type = BT_VALUE_TYPE_NULL,
c55a9f58 69 .is_frozen = BT_TRUE,
dac5c838
PP
70};
71
72struct bt_value *bt_value_null = &bt_value_null_instance;
73
74struct bt_value_bool {
75 struct bt_value base;
c55a9f58 76 bt_bool value;
dac5c838
PP
77};
78
79struct bt_value_integer {
80 struct bt_value base;
81 int64_t value;
82};
83
84struct bt_value_float {
85 struct bt_value base;
86 double value;
87};
88
89struct bt_value_string {
90 struct bt_value base;
91 GString *gstr;
92};
93
94struct bt_value_array {
95 struct bt_value base;
96 GPtrArray *garray;
97};
98
99struct bt_value_map {
100 struct bt_value base;
101 GHashTable *ght;
102};
103
104static
83509119 105void bt_value_destroy(struct bt_object *obj);
dac5c838
PP
106
107static
108void bt_value_string_destroy(struct bt_value *object)
109{
110 g_string_free(BT_VALUE_TO_STRING(object)->gstr, TRUE);
111}
112
113static
114void bt_value_array_destroy(struct bt_value *object)
115{
116 /*
117 * Pointer array's registered value destructor will take care
118 * of putting each contained object.
119 */
120 g_ptr_array_free(BT_VALUE_TO_ARRAY(object)->garray, TRUE);
121}
122
123static
124void bt_value_map_destroy(struct bt_value *object)
125{
126 /*
127 * Hash table's registered value destructor will take care of
128 * putting each contained object. Keys are GQuarks and cannot
129 * be destroyed anyway.
130 */
131 g_hash_table_destroy(BT_VALUE_TO_MAP(object)->ght);
132}
133
134static
135void (* const destroy_funcs[])(struct bt_value *) = {
136 [BT_VALUE_TYPE_NULL] = NULL,
137 [BT_VALUE_TYPE_BOOL] = NULL,
138 [BT_VALUE_TYPE_INTEGER] = NULL,
139 [BT_VALUE_TYPE_FLOAT] = NULL,
140 [BT_VALUE_TYPE_STRING] = bt_value_string_destroy,
141 [BT_VALUE_TYPE_ARRAY] = bt_value_array_destroy,
142 [BT_VALUE_TYPE_MAP] = bt_value_map_destroy,
143};
144
145static
146struct bt_value *bt_value_null_copy(const struct bt_value *null_obj)
147{
148 return bt_value_null;
149}
150
151static
152struct bt_value *bt_value_bool_copy(const struct bt_value *bool_obj)
153{
154 return bt_value_bool_create_init(BT_VALUE_TO_BOOL(bool_obj)->value);
155}
156
157static
158struct bt_value *bt_value_integer_copy(const struct bt_value *integer_obj)
159{
160 return bt_value_integer_create_init(
161 BT_VALUE_TO_INTEGER(integer_obj)->value);
162}
163
164static
165struct bt_value *bt_value_float_copy(const struct bt_value *float_obj)
166{
167 return bt_value_float_create_init(
168 BT_VALUE_TO_FLOAT(float_obj)->value);
169}
170
171static
172struct bt_value *bt_value_string_copy(const struct bt_value *string_obj)
173{
174 return bt_value_string_create_init(
175 BT_VALUE_TO_STRING(string_obj)->gstr->str);
176}
177
178static
179struct bt_value *bt_value_array_copy(const struct bt_value *array_obj)
180{
181 int i;
182 int ret;
183 struct bt_value *copy_obj;
184 struct bt_value_array *typed_array_obj;
185
2f42aa0a 186 BT_LOGD("Copying array value: addr=%p", array_obj);
dac5c838
PP
187 typed_array_obj = BT_VALUE_TO_ARRAY(array_obj);
188 copy_obj = bt_value_array_create();
189
190 if (!copy_obj) {
2f42aa0a 191 BT_LOGE_STR("Cannot create empty array value.");
dac5c838
PP
192 goto end;
193 }
194
195 for (i = 0; i < typed_array_obj->garray->len; ++i) {
196 struct bt_value *element_obj_copy;
197 struct bt_value *element_obj = bt_value_array_get(array_obj, i);
198
199 if (!element_obj) {
2f42aa0a
PP
200 BT_LOGE("Cannot get array value's element: "
201 "addr=%p, index=%d",
202 array_obj, i);
83509119 203 BT_PUT(copy_obj);
dac5c838
PP
204 goto end;
205 }
206
207 element_obj_copy = bt_value_copy(element_obj);
83509119 208 BT_PUT(element_obj);
dac5c838
PP
209
210 if (!element_obj_copy) {
2f42aa0a
PP
211 BT_LOGE("Cannot copy array value's element: "
212 "array-addr=%p, index=%d",
213 array_obj, i);
83509119 214 BT_PUT(copy_obj);
dac5c838
PP
215 goto end;
216 }
217
218 ret = bt_value_array_append(copy_obj, element_obj_copy);
83509119 219 BT_PUT(element_obj_copy);
dac5c838
PP
220
221 if (ret) {
2f42aa0a
PP
222 BT_LOGE("Cannot append to array value: addr=%p",
223 array_obj);
83509119 224 BT_PUT(copy_obj);
dac5c838
PP
225 goto end;
226 }
227 }
228
a704fb0b
PP
229 BT_LOGD("Copied array value: original-addr=%p, copy-addr=%p",
230 array_obj, copy_obj);
2f42aa0a 231
dac5c838
PP
232end:
233 return copy_obj;
234}
235
236static
237struct bt_value *bt_value_map_copy(const struct bt_value *map_obj)
238{
239 int ret;
240 GHashTableIter iter;
241 gpointer key, element_obj;
242 struct bt_value *copy_obj;
243 struct bt_value *element_obj_copy;
244 struct bt_value_map *typed_map_obj;
245
2f42aa0a 246 BT_LOGD("Copying map value: addr=%p", map_obj);
dac5c838
PP
247 typed_map_obj = BT_VALUE_TO_MAP(map_obj);
248 copy_obj = bt_value_map_create();
249
250 if (!copy_obj) {
251 goto end;
252 }
253
254 g_hash_table_iter_init(&iter, typed_map_obj->ght);
255
256 while (g_hash_table_iter_next(&iter, &key, &element_obj)) {
5b44aff2 257 const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key));
dac5c838
PP
258
259 element_obj_copy = bt_value_copy(element_obj);
260
261 if (!element_obj_copy) {
2f42aa0a
PP
262 BT_LOGE("Cannot copy map value's element: "
263 "map-addr=%p, key=\"%s\"",
264 map_obj, key_str);
83509119 265 BT_PUT(copy_obj);
dac5c838
PP
266 goto end;
267 }
268
269 ret = bt_value_map_insert(copy_obj, key_str, element_obj_copy);
83509119 270 BT_PUT(element_obj_copy);
dac5c838
PP
271
272 if (ret) {
2f42aa0a
PP
273 BT_LOGE("Cannot insert into map value: addr=%p, key=\"%s\"",
274 map_obj, key_str);
83509119 275 BT_PUT(copy_obj);
dac5c838
PP
276 goto end;
277 }
278 }
279
2f42aa0a
PP
280 BT_LOGD("Copied map value: addr=%p", map_obj);
281
dac5c838
PP
282end:
283 return copy_obj;
284}
285
286static
287struct bt_value *(* const copy_funcs[])(const struct bt_value *) = {
288 [BT_VALUE_TYPE_NULL] = bt_value_null_copy,
289 [BT_VALUE_TYPE_BOOL] = bt_value_bool_copy,
290 [BT_VALUE_TYPE_INTEGER] = bt_value_integer_copy,
291 [BT_VALUE_TYPE_FLOAT] = bt_value_float_copy,
292 [BT_VALUE_TYPE_STRING] = bt_value_string_copy,
293 [BT_VALUE_TYPE_ARRAY] = bt_value_array_copy,
294 [BT_VALUE_TYPE_MAP] = bt_value_map_copy,
295};
296
297static
c55a9f58 298bt_bool bt_value_null_compare(const struct bt_value *object_a,
dac5c838
PP
299 const struct bt_value *object_b)
300{
301 /*
c55a9f58 302 * Always BT_TRUE since bt_value_compare() already checks if both
dac5c838
PP
303 * object_a and object_b have the same type, and in the case of
304 * null value objects, they're always the same if it is so.
305 */
c55a9f58 306 return BT_TRUE;
dac5c838
PP
307}
308
309static
c55a9f58 310bt_bool bt_value_bool_compare(const struct bt_value *object_a,
dac5c838
PP
311 const struct bt_value *object_b)
312{
313 return BT_VALUE_TO_BOOL(object_a)->value ==
314 BT_VALUE_TO_BOOL(object_b)->value;
315}
316
317static
c55a9f58 318bt_bool bt_value_integer_compare(const struct bt_value *object_a,
dac5c838
PP
319 const struct bt_value *object_b)
320{
321 return BT_VALUE_TO_INTEGER(object_a)->value ==
322 BT_VALUE_TO_INTEGER(object_b)->value;
323}
324
325static
c55a9f58 326bt_bool bt_value_float_compare(const struct bt_value *object_a,
dac5c838
PP
327 const struct bt_value *object_b)
328{
329 return BT_VALUE_TO_FLOAT(object_a)->value ==
330 BT_VALUE_TO_FLOAT(object_b)->value;
331}
332
333static
c55a9f58 334bt_bool bt_value_string_compare(const struct bt_value *object_a,
dac5c838
PP
335 const struct bt_value *object_b)
336{
337 return !strcmp(BT_VALUE_TO_STRING(object_a)->gstr->str,
338 BT_VALUE_TO_STRING(object_b)->gstr->str);
339}
340
341static
c55a9f58 342bt_bool bt_value_array_compare(const struct bt_value *object_a,
dac5c838
PP
343 const struct bt_value *object_b)
344{
345 int i;
c55a9f58 346 bt_bool ret = BT_TRUE;
dac5c838
PP
347 const struct bt_value_array *array_obj_a =
348 BT_VALUE_TO_ARRAY(object_a);
349
350 if (bt_value_array_size(object_a) != bt_value_array_size(object_b)) {
2f42aa0a
PP
351 BT_LOGV("Array values are different: size mismatch "
352 "value-a-addr=%p, value-b-addr=%p, "
353 "value-a-size=%" PRId64 ", value-b-size=%" PRId64,
354 object_a, object_b,
355 bt_value_array_size(object_a),
356 bt_value_array_size(object_b));
c55a9f58 357 ret = BT_FALSE;
dac5c838
PP
358 goto end;
359 }
360
361 for (i = 0; i < array_obj_a->garray->len; ++i) {
362 struct bt_value *element_obj_a;
363 struct bt_value *element_obj_b;
364
365 element_obj_a = bt_value_array_get(object_a, i);
366 element_obj_b = bt_value_array_get(object_b, i);
367
368 if (!bt_value_compare(element_obj_a, element_obj_b)) {
2f42aa0a
PP
369 BT_LOGV("Array values's elements are different: "
370 "value-a-addr=%p, value-b-addr=%p, index=%d",
371 element_obj_a, element_obj_b, index);
83509119
JG
372 BT_PUT(element_obj_a);
373 BT_PUT(element_obj_b);
c55a9f58 374 ret = BT_FALSE;
dac5c838
PP
375 goto end;
376 }
377
83509119
JG
378 BT_PUT(element_obj_a);
379 BT_PUT(element_obj_b);
dac5c838
PP
380 }
381
382end:
383 return ret;
384}
385
386static
c55a9f58 387bt_bool bt_value_map_compare(const struct bt_value *object_a,
dac5c838
PP
388 const struct bt_value *object_b)
389{
c55a9f58 390 bt_bool ret = BT_TRUE;
dac5c838
PP
391 GHashTableIter iter;
392 gpointer key, element_obj_a;
393 const struct bt_value_map *map_obj_a = BT_VALUE_TO_MAP(object_a);
394
395 if (bt_value_map_size(object_a) != bt_value_map_size(object_b)) {
2f42aa0a
PP
396 BT_LOGV("Map values are different: size mismatch "
397 "value-a-addr=%p, value-b-addr=%p, "
398 "value-a-size=%" PRId64 ", value-b-size=%" PRId64,
399 object_a, object_b,
400 bt_value_map_size(object_a),
401 bt_value_map_size(object_b));
c55a9f58 402 ret = BT_FALSE;
dac5c838
PP
403 goto end;
404 }
405
406 g_hash_table_iter_init(&iter, map_obj_a->ght);
407
408 while (g_hash_table_iter_next(&iter, &key, &element_obj_a)) {
409 struct bt_value *element_obj_b;
5b44aff2 410 const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key));
dac5c838
PP
411
412 element_obj_b = bt_value_map_get(object_b, key_str);
413
414 if (!bt_value_compare(element_obj_a, element_obj_b)) {
2f42aa0a
PP
415 BT_LOGV("Map values's elements are different: "
416 "value-a-addr=%p, value-b-addr=%p, key=\"%s\"",
417 element_obj_a, element_obj_b, key_str);
83509119 418 BT_PUT(element_obj_b);
c55a9f58 419 ret = BT_FALSE;
dac5c838
PP
420 goto end;
421 }
422
83509119 423 BT_PUT(element_obj_b);
dac5c838
PP
424 }
425
426end:
427 return ret;
428}
429
430static
c55a9f58 431bt_bool (* const compare_funcs[])(const struct bt_value *,
dac5c838
PP
432 const struct bt_value *) = {
433 [BT_VALUE_TYPE_NULL] = bt_value_null_compare,
434 [BT_VALUE_TYPE_BOOL] = bt_value_bool_compare,
435 [BT_VALUE_TYPE_INTEGER] = bt_value_integer_compare,
436 [BT_VALUE_TYPE_FLOAT] = bt_value_float_compare,
437 [BT_VALUE_TYPE_STRING] = bt_value_string_compare,
438 [BT_VALUE_TYPE_ARRAY] = bt_value_array_compare,
439 [BT_VALUE_TYPE_MAP] = bt_value_map_compare,
440};
441
442void bt_value_null_freeze(struct bt_value *object)
443{
444}
445
446void bt_value_generic_freeze(struct bt_value *object)
447{
c55a9f58 448 object->is_frozen = BT_TRUE;
dac5c838
PP
449}
450
451void bt_value_array_freeze(struct bt_value *object)
452{
453 int i;
454 struct bt_value_array *typed_array_obj =
455 BT_VALUE_TO_ARRAY(object);
456
457 for (i = 0; i < typed_array_obj->garray->len; ++i) {
458 struct bt_value *element_obj =
459 g_ptr_array_index(typed_array_obj->garray, i);
460
461 bt_value_freeze(element_obj);
462 }
463
464 bt_value_generic_freeze(object);
465}
466
467void bt_value_map_freeze(struct bt_value *object)
468{
469 GHashTableIter iter;
470 gpointer key, element_obj;
471 const struct bt_value_map *map_obj = BT_VALUE_TO_MAP(object);
472
473 g_hash_table_iter_init(&iter, map_obj->ght);
474
475 while (g_hash_table_iter_next(&iter, &key, &element_obj)) {
476 bt_value_freeze(element_obj);
477 }
478
479 bt_value_generic_freeze(object);
480}
481
482static
483void (* const freeze_funcs[])(struct bt_value *) = {
484 [BT_VALUE_TYPE_NULL] = bt_value_null_freeze,
485 [BT_VALUE_TYPE_BOOL] = bt_value_generic_freeze,
486 [BT_VALUE_TYPE_INTEGER] = bt_value_generic_freeze,
487 [BT_VALUE_TYPE_FLOAT] = bt_value_generic_freeze,
488 [BT_VALUE_TYPE_STRING] = bt_value_generic_freeze,
489 [BT_VALUE_TYPE_ARRAY] = bt_value_array_freeze,
490 [BT_VALUE_TYPE_MAP] = bt_value_map_freeze,
491};
492
493static
83509119 494void bt_value_destroy(struct bt_object *obj)
dac5c838 495{
83509119 496 struct bt_value *value;
dac5c838 497
83509119
JG
498 value = container_of(obj, struct bt_value, base);
499 assert(value->type != BT_VALUE_TYPE_UNKNOWN);
2f42aa0a
PP
500 BT_LOGD("Destroying value: addr=%p", value);
501
83509119 502 if (bt_value_is_null(value)) {
2f42aa0a 503 BT_LOGD_STR("Not destroying the null value singleton.");
dac5c838
PP
504 return;
505 }
506
83509119
JG
507 if (destroy_funcs[value->type]) {
508 destroy_funcs[value->type](value);
dac5c838
PP
509 }
510
83509119 511 g_free(value);
dac5c838
PP
512}
513
514enum bt_value_status bt_value_freeze(struct bt_value *object)
515{
516 enum bt_value_status ret = BT_VALUE_STATUS_OK;
517
518 if (!object) {
2f42aa0a 519 BT_LOGW_STR("Invalid parameter: value object is NULL.");
dac5c838
PP
520 ret = BT_VALUE_STATUS_INVAL;
521 goto end;
522 }
523
2f42aa0a
PP
524 if (object->is_frozen) {
525 goto end;
526 }
527
528 BT_LOGD("Freezing value: addr=%p", object);
dac5c838
PP
529 freeze_funcs[object->type](object);
530
531end:
532 return ret;
533}
534
c55a9f58 535bt_bool bt_value_is_frozen(const struct bt_value *object)
dac5c838
PP
536{
537 return object && object->is_frozen;
538}
539
540enum bt_value_type bt_value_get_type(const struct bt_value *object)
541{
542 if (!object) {
2f42aa0a 543 BT_LOGW_STR("Invalid parameter: value object is NULL.");
dac5c838
PP
544 return BT_VALUE_TYPE_UNKNOWN;
545 }
546
547 return object->type;
548}
549
550static
551struct bt_value bt_value_create_base(enum bt_value_type type)
552{
553 struct bt_value base;
554
555 base.type = type;
c55a9f58 556 base.is_frozen = BT_FALSE;
83509119 557 bt_object_init(&base, bt_value_destroy);
dac5c838
PP
558 return base;
559}
560
c55a9f58 561struct bt_value *bt_value_bool_create_init(bt_bool val)
dac5c838
PP
562{
563 struct bt_value_bool *bool_obj;
564
2f42aa0a 565 BT_LOGD("Creating boolean value object: val=%d", val);
dac5c838
PP
566 bool_obj = g_new0(struct bt_value_bool, 1);
567
568 if (!bool_obj) {
2f42aa0a 569 BT_LOGE_STR("Failed to allocate one boolean value object.");
dac5c838
PP
570 goto end;
571 }
572
573 bool_obj->base = bt_value_create_base(BT_VALUE_TYPE_BOOL);
574 bool_obj->value = val;
2f42aa0a 575 BT_LOGD("Created boolean value object: addr=%p", bool_obj);
dac5c838
PP
576
577end:
578 return BT_VALUE_FROM_CONCRETE(bool_obj);
579}
580
581struct bt_value *bt_value_bool_create(void)
582{
c55a9f58 583 return bt_value_bool_create_init(BT_FALSE);
dac5c838
PP
584}
585
586struct bt_value *bt_value_integer_create_init(int64_t val)
587{
588 struct bt_value_integer *integer_obj;
589
2f42aa0a 590 BT_LOGD("Creating integer value object: val=%" PRId64, val);
dac5c838
PP
591 integer_obj = g_new0(struct bt_value_integer, 1);
592
593 if (!integer_obj) {
2f42aa0a 594 BT_LOGE_STR("Failed to allocate one integer value object.");
dac5c838
PP
595 goto end;
596 }
597
598 integer_obj->base = bt_value_create_base(BT_VALUE_TYPE_INTEGER);
599 integer_obj->value = val;
2f42aa0a
PP
600 BT_LOGD("Created integer value object: addr=%p",
601 integer_obj);
dac5c838
PP
602
603end:
604 return BT_VALUE_FROM_CONCRETE(integer_obj);
605}
606
607struct bt_value *bt_value_integer_create(void)
608{
609 return bt_value_integer_create_init(0);
610}
611
612struct bt_value *bt_value_float_create_init(double val)
613{
614 struct bt_value_float *float_obj;
615
2f42aa0a 616 BT_LOGD("Creating floating point number value object: val=%f", val);
dac5c838
PP
617 float_obj = g_new0(struct bt_value_float, 1);
618
619 if (!float_obj) {
2f42aa0a 620 BT_LOGE_STR("Failed to allocate one floating point number value object.");
dac5c838
PP
621 goto end;
622 }
623
624 float_obj->base = bt_value_create_base(BT_VALUE_TYPE_FLOAT);
625 float_obj->value = val;
2f42aa0a
PP
626 BT_LOGD("Created floating point number value object: addr=%p",
627 float_obj);
dac5c838
PP
628
629end:
630 return BT_VALUE_FROM_CONCRETE(float_obj);
631}
632
633struct bt_value *bt_value_float_create(void)
634{
635 return bt_value_float_create_init(0.);
636}
637
638struct bt_value *bt_value_string_create_init(const char *val)
639{
640 struct bt_value_string *string_obj = NULL;
641
642 if (!val) {
2f42aa0a 643 BT_LOGW_STR("Invalid parameter: value is NULL.");
dac5c838
PP
644 goto end;
645 }
646
2f42aa0a 647 BT_LOGD("Creating string value object: val-len=%u", strlen(val));
dac5c838
PP
648 string_obj = g_new0(struct bt_value_string, 1);
649
650 if (!string_obj) {
2f42aa0a 651 BT_LOGE_STR("Failed to allocate one string object.");
dac5c838
PP
652 goto end;
653 }
654
655 string_obj->base = bt_value_create_base(BT_VALUE_TYPE_STRING);
656 string_obj->gstr = g_string_new(val);
657
658 if (!string_obj->gstr) {
2f42aa0a 659 BT_LOGE_STR("Failed to allocate a GString.");
dac5c838
PP
660 g_free(string_obj);
661 string_obj = NULL;
662 goto end;
663 }
664
2f42aa0a
PP
665 BT_LOGD("Created string value object: addr=%p",
666 string_obj);
667
dac5c838
PP
668end:
669 return BT_VALUE_FROM_CONCRETE(string_obj);
670}
671
672struct bt_value *bt_value_string_create(void)
673{
674 return bt_value_string_create_init("");
675}
676
677struct bt_value *bt_value_array_create(void)
678{
679 struct bt_value_array *array_obj;
680
2f42aa0a 681 BT_LOGD_STR("Creating empty array value object.");
dac5c838
PP
682 array_obj = g_new0(struct bt_value_array, 1);
683
684 if (!array_obj) {
2f42aa0a 685 BT_LOGE_STR("Failed to allocate one array object.");
dac5c838
PP
686 goto end;
687 }
688
689 array_obj->base = bt_value_create_base(BT_VALUE_TYPE_ARRAY);
5d5982ab 690 array_obj->garray = bt_g_ptr_array_new_full(0,
83509119 691 (GDestroyNotify) bt_put);
dac5c838
PP
692
693 if (!array_obj->garray) {
2f42aa0a 694 BT_LOGE_STR("Failed to allocate a GPtrArray.");
dac5c838
PP
695 g_free(array_obj);
696 array_obj = NULL;
697 goto end;
698 }
699
2f42aa0a
PP
700 BT_LOGD("Created array value object: addr=%p",
701 array_obj);
702
dac5c838
PP
703end:
704 return BT_VALUE_FROM_CONCRETE(array_obj);
705}
706
707struct bt_value *bt_value_map_create(void)
708{
709 struct bt_value_map *map_obj;
710
2f42aa0a 711 BT_LOGD_STR("Creating empty map value object.");
dac5c838
PP
712 map_obj = g_new0(struct bt_value_map, 1);
713
714 if (!map_obj) {
2f42aa0a 715 BT_LOGE_STR("Failed to allocate one map object.");
dac5c838
PP
716 goto end;
717 }
718
719 map_obj->base = bt_value_create_base(BT_VALUE_TYPE_MAP);
720 map_obj->ght = g_hash_table_new_full(g_direct_hash, g_direct_equal,
83509119 721 NULL, (GDestroyNotify) bt_put);
dac5c838
PP
722
723 if (!map_obj->ght) {
2f42aa0a 724 BT_LOGE_STR("Failed to allocate a GHashTable.");
dac5c838
PP
725 g_free(map_obj);
726 map_obj = NULL;
727 goto end;
728 }
729
2f42aa0a
PP
730 BT_LOGD("Created map value object: addr=%p",
731 map_obj);
732
dac5c838
PP
733end:
734 return BT_VALUE_FROM_CONCRETE(map_obj);
735}
736
737enum bt_value_status bt_value_bool_get(const struct bt_value *bool_obj,
c55a9f58 738 bt_bool *val)
dac5c838
PP
739{
740 enum bt_value_status ret = BT_VALUE_STATUS_OK;
741 struct bt_value_bool *typed_bool_obj = BT_VALUE_TO_BOOL(bool_obj);
742
2f42aa0a
PP
743 if (!bool_obj || !val) {
744 BT_LOGW("Invalid parameter: value object or value is NULL: "
745 "value-addr=%p, raw-value-addr=%p",
746 bool_obj, val);
747 ret = BT_VALUE_STATUS_INVAL;
748 goto end;
749 }
750
751 if (!bt_value_is_bool(bool_obj)) {
752 BT_LOGW("Invalid parameter: value is not a boolean value: addr=%p, "
c4628760
PP
753 "type=%s", bool_obj, bool_obj->type,
754 bt_value_type_string(bool_obj->type));
dac5c838
PP
755 ret = BT_VALUE_STATUS_INVAL;
756 goto end;
757 }
758
759 *val = typed_bool_obj->value;
760
761end:
762 return ret;
763}
764
c55a9f58 765enum bt_value_status bt_value_bool_set(struct bt_value *bool_obj, bt_bool val)
dac5c838
PP
766{
767 enum bt_value_status ret = BT_VALUE_STATUS_OK;
768 struct bt_value_bool *typed_bool_obj = BT_VALUE_TO_BOOL(bool_obj);
769
2f42aa0a
PP
770 if (!bool_obj) {
771 BT_LOGW_STR("Invalid parameter: value object is NULL.");
772 ret = BT_VALUE_STATUS_INVAL;
773 goto end;
774 }
775
776 if (!bt_value_is_bool(bool_obj)) {
777 BT_LOGW("Invalid parameter: value is not a boolean value: addr=%p, "
c4628760
PP
778 "type=%s", bool_obj,
779 bt_value_type_string(bool_obj->type));
dac5c838
PP
780 ret = BT_VALUE_STATUS_INVAL;
781 goto end;
782 }
783
784 if (bool_obj->is_frozen) {
2f42aa0a
PP
785 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
786 bool_obj);
dac5c838
PP
787 ret = BT_VALUE_STATUS_FROZEN;
788 goto end;
789 }
790
791 typed_bool_obj->value = val;
2f42aa0a
PP
792 BT_LOGV("Set boolean value's raw value: value-addr=%p, value=%d",
793 bool_obj, val);
dac5c838
PP
794
795end:
796 return ret;
797}
798
799enum bt_value_status bt_value_integer_get(const struct bt_value *integer_obj,
364747d6 800 int64_t *val)
dac5c838
PP
801{
802 enum bt_value_status ret = BT_VALUE_STATUS_OK;
803 struct bt_value_integer *typed_integer_obj =
804 BT_VALUE_TO_INTEGER(integer_obj);
805
2f42aa0a
PP
806 if (!integer_obj || !val) {
807 BT_LOGW("Invalid parameter: value object or value is NULL: "
808 "value-addr=%p, raw-value-addr=%p",
809 integer_obj, val);
810 ret = BT_VALUE_STATUS_INVAL;
811 goto end;
812 }
813
814 if (!bt_value_is_integer(integer_obj)) {
815 BT_LOGW("Invalid parameter: value is not an integer value: addr=%p, "
c4628760
PP
816 "type=%s", integer_obj,
817 bt_value_type_string(integer_obj->type));
dac5c838
PP
818 ret = BT_VALUE_STATUS_INVAL;
819 goto end;
820 }
821
822 *val = typed_integer_obj->value;
823
824end:
825 return ret;
826}
827
828enum bt_value_status bt_value_integer_set(struct bt_value *integer_obj,
364747d6 829 int64_t val)
dac5c838
PP
830{
831 enum bt_value_status ret = BT_VALUE_STATUS_OK;
832 struct bt_value_integer *typed_integer_obj =
833 BT_VALUE_TO_INTEGER(integer_obj);
834
2f42aa0a
PP
835 if (!integer_obj) {
836 BT_LOGW_STR("Invalid parameter: value object is NULL.");
837 ret = BT_VALUE_STATUS_INVAL;
838 goto end;
839 }
840
841 if (!bt_value_is_integer(integer_obj)) {
842 BT_LOGW("Invalid parameter: value is not an integer value: addr=%p, "
c4628760
PP
843 "type=%s", integer_obj,
844 bt_value_type_string(integer_obj->type));
dac5c838
PP
845 ret = BT_VALUE_STATUS_INVAL;
846 goto end;
847 }
848
849 if (integer_obj->is_frozen) {
2f42aa0a
PP
850 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
851 integer_obj);
dac5c838
PP
852 ret = BT_VALUE_STATUS_FROZEN;
853 goto end;
854 }
855
856 typed_integer_obj->value = val;
2f42aa0a
PP
857 BT_LOGV("Set integer value's raw value: value-addr=%p, value=%" PRId64,
858 integer_obj, val);
dac5c838
PP
859
860end:
861 return ret;
862}
863
864enum bt_value_status bt_value_float_get(const struct bt_value *float_obj,
364747d6 865 double *val)
dac5c838
PP
866{
867 enum bt_value_status ret = BT_VALUE_STATUS_OK;
868 struct bt_value_float *typed_float_obj =
869 BT_VALUE_TO_FLOAT(float_obj);
870
2f42aa0a
PP
871 if (!float_obj || !val) {
872 BT_LOGW("Invalid parameter: value object or value is NULL: "
873 "value-addr=%p, raw-value-addr=%p",
874 float_obj, val);
875 ret = BT_VALUE_STATUS_INVAL;
876 goto end;
877 }
878
879 if (!bt_value_is_float(float_obj)) {
880 BT_LOGW("Invalid parameter: value is not a floating point number value: addr=%p, "
c4628760
PP
881 "type=%s", float_obj,
882 bt_value_type_string(float_obj->type));
dac5c838
PP
883 ret = BT_VALUE_STATUS_INVAL;
884 goto end;
885 }
886
887 *val = typed_float_obj->value;
888
889end:
890 return ret;
891}
892
893enum bt_value_status bt_value_float_set(struct bt_value *float_obj,
364747d6 894 double val)
dac5c838
PP
895{
896 enum bt_value_status ret = BT_VALUE_STATUS_OK;
897 struct bt_value_float *typed_float_obj =
898 BT_VALUE_TO_FLOAT(float_obj);
899
2f42aa0a
PP
900 if (!float_obj) {
901 BT_LOGW_STR("Invalid parameter: value object is NULL.");
902 ret = BT_VALUE_STATUS_INVAL;
903 goto end;
904 }
905
906 if (!bt_value_is_float(float_obj)) {
907 BT_LOGW("Invalid parameter: value is not a floating point number value: addr=%p, "
c4628760
PP
908 "type=%s", float_obj,
909 bt_value_type_string(float_obj->type));
dac5c838
PP
910 ret = BT_VALUE_STATUS_INVAL;
911 goto end;
912 }
913
914 if (float_obj->is_frozen) {
2f42aa0a
PP
915 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
916 float_obj);
dac5c838
PP
917 ret = BT_VALUE_STATUS_FROZEN;
918 goto end;
919 }
920
921 typed_float_obj->value = val;
2f42aa0a
PP
922 BT_LOGV("Set floating point number value's raw value: value-addr=%p, value=%f",
923 float_obj, val);
dac5c838
PP
924
925end:
926 return ret;
927}
928
929enum bt_value_status bt_value_string_get(const struct bt_value *string_obj,
364747d6 930 const char **val)
dac5c838
PP
931{
932 enum bt_value_status ret = BT_VALUE_STATUS_OK;
933 struct bt_value_string *typed_string_obj =
934 BT_VALUE_TO_STRING(string_obj);
935
2f42aa0a
PP
936 if (!string_obj || !val) {
937 BT_LOGW("Invalid parameter: value object or value is NULL: "
938 "value-addr=%p, raw-value-addr=%p",
939 string_obj, val);
940 ret = BT_VALUE_STATUS_INVAL;
941 goto end;
942 }
943
944 if (!bt_value_is_string(string_obj)) {
945 BT_LOGW("Invalid parameter: value is not a string value: addr=%p, "
c4628760
PP
946 "type=%s", string_obj,
947 bt_value_type_string(string_obj->type));
dac5c838
PP
948 ret = BT_VALUE_STATUS_INVAL;
949 goto end;
950 }
951
952 *val = typed_string_obj->gstr->str;
953
954end:
955 return ret;
956}
957
958enum bt_value_status bt_value_string_set(struct bt_value *string_obj,
364747d6 959 const char *val)
dac5c838
PP
960{
961 enum bt_value_status ret = BT_VALUE_STATUS_OK;
962 struct bt_value_string *typed_string_obj =
963 BT_VALUE_TO_STRING(string_obj);
964
2f42aa0a
PP
965 if (!string_obj || !val) {
966 BT_LOGW("Invalid parameter: value object or value is NULL: "
967 "value-addr=%p, raw-value-addr=%p",
968 string_obj, val);
969 ret = BT_VALUE_STATUS_INVAL;
970 goto end;
971 }
972
973 if (!bt_value_is_string(string_obj)) {
974 BT_LOGW("Invalid parameter: value is not a string value: addr=%p, "
c4628760
PP
975 "type=%s", string_obj,
976 bt_value_type_string(string_obj->type));
dac5c838
PP
977 ret = BT_VALUE_STATUS_INVAL;
978 goto end;
979 }
980
981 if (string_obj->is_frozen) {
2f42aa0a
PP
982 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
983 string_obj);
dac5c838
PP
984 ret = BT_VALUE_STATUS_FROZEN;
985 goto end;
986 }
987
988 g_string_assign(typed_string_obj->gstr, val);
2f42aa0a
PP
989 BT_LOGV("Set string value's raw value: value-addr=%p, raw-value-addr=%p",
990 string_obj, val);
dac5c838
PP
991
992end:
993 return ret;
994}
995
544d0515 996int64_t bt_value_array_size(const struct bt_value *array_obj)
dac5c838 997{
544d0515 998 int64_t ret;
dac5c838
PP
999 struct bt_value_array *typed_array_obj =
1000 BT_VALUE_TO_ARRAY(array_obj);
1001
2f42aa0a
PP
1002 if (!array_obj) {
1003 BT_LOGW_STR("Invalid parameter: value object is NULL.");
9ac68eb1 1004 ret = (int64_t) BT_VALUE_STATUS_INVAL;
dac5c838
PP
1005 goto end;
1006 }
1007
2f42aa0a
PP
1008 if (!bt_value_is_array(array_obj)) {
1009 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
c4628760
PP
1010 "type=%s", array_obj,
1011 bt_value_type_string(array_obj->type));
2f42aa0a
PP
1012 ret = BT_VALUE_STATUS_INVAL;
1013 goto end;
1014 }
1015
544d0515 1016 ret = (int64_t) typed_array_obj->garray->len;
dac5c838
PP
1017
1018end:
1019 return ret;
1020}
1021
c55a9f58 1022bt_bool bt_value_array_is_empty(const struct bt_value *array_obj)
dac5c838
PP
1023{
1024 return bt_value_array_size(array_obj) == 0;
1025}
1026
1027struct bt_value *bt_value_array_get(const struct bt_value *array_obj,
2f42aa0a 1028 uint64_t index)
dac5c838
PP
1029{
1030 struct bt_value *ret;
1031 struct bt_value_array *typed_array_obj =
1032 BT_VALUE_TO_ARRAY(array_obj);
1033
2f42aa0a
PP
1034 if (!array_obj) {
1035 BT_LOGW("Invalid parameter: value object is NULL: index=%" PRIu64,
1036 index);
1037 ret = NULL;
1038 goto end;
1039 }
1040
1041 if (!bt_value_is_array(array_obj)) {
1042 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
c4628760
PP
1043 "type=%s", array_obj,
1044 bt_value_type_string(array_obj->type));
2f42aa0a
PP
1045 ret = NULL;
1046 goto end;
1047 }
1048
1049 if (index >= typed_array_obj->garray->len) {
1050 BT_LOGW("Invalid parameter: index is out of bounds: "
78c6e1b1
PP
1051 "addr=%p, index=%" PRIu64 ", size=%u",
1052 array_obj, index, typed_array_obj->garray->len);
dac5c838
PP
1053 ret = NULL;
1054 goto end;
1055 }
1056
1057 ret = g_ptr_array_index(typed_array_obj->garray, index);
83509119 1058 bt_get(ret);
dac5c838
PP
1059
1060end:
1061 return ret;
1062}
1063
1064enum bt_value_status bt_value_array_append(struct bt_value *array_obj,
364747d6 1065 struct bt_value *element_obj)
dac5c838
PP
1066{
1067 enum bt_value_status ret = BT_VALUE_STATUS_OK;
1068 struct bt_value_array *typed_array_obj =
1069 BT_VALUE_TO_ARRAY(array_obj);
1070
2f42aa0a
PP
1071 if (!array_obj || !element_obj) {
1072 BT_LOGW("Invalid parameter: array value or element value is NULL: "
1073 "array-value-addr=%p, element-value-addr=%p",
1074 array_obj, element_obj);
1075 ret = BT_VALUE_STATUS_INVAL;
1076 goto end;
1077 }
1078
1079 if (!bt_value_is_array(array_obj)) {
1080 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
c4628760
PP
1081 "type=%s", array_obj,
1082 bt_value_type_string(array_obj->type));
dac5c838
PP
1083 ret = BT_VALUE_STATUS_INVAL;
1084 goto end;
1085 }
1086
1087 if (array_obj->is_frozen) {
2f42aa0a
PP
1088 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1089 array_obj);
dac5c838
PP
1090 ret = BT_VALUE_STATUS_FROZEN;
1091 goto end;
1092 }
1093
1094 g_ptr_array_add(typed_array_obj->garray, element_obj);
83509119 1095 bt_get(element_obj);
2f42aa0a
PP
1096 BT_LOGV("Appended element to array value: array-value-addr=%p, "
1097 "element-value-addr=%p, new-size=%u",
1098 array_obj, element_obj, typed_array_obj->garray->len);
dac5c838
PP
1099
1100end:
1101 return ret;
1102}
1103
1104enum bt_value_status bt_value_array_append_bool(struct bt_value *array_obj,
c55a9f58 1105 bt_bool val)
dac5c838
PP
1106{
1107 enum bt_value_status ret;
1108 struct bt_value *bool_obj = NULL;
1109
1110 bool_obj = bt_value_bool_create_init(val);
1111 ret = bt_value_array_append(array_obj, bool_obj);
83509119 1112 bt_put(bool_obj);
dac5c838
PP
1113 return ret;
1114}
1115
1116enum bt_value_status bt_value_array_append_integer(
364747d6 1117 struct bt_value *array_obj, int64_t val)
dac5c838
PP
1118{
1119 enum bt_value_status ret;
1120 struct bt_value *integer_obj = NULL;
1121
1122 integer_obj = bt_value_integer_create_init(val);
1123 ret = bt_value_array_append(array_obj, integer_obj);
83509119 1124 bt_put(integer_obj);
dac5c838
PP
1125 return ret;
1126}
1127
1128enum bt_value_status bt_value_array_append_float(struct bt_value *array_obj,
364747d6 1129 double val)
dac5c838
PP
1130{
1131 enum bt_value_status ret;
1132 struct bt_value *float_obj = NULL;
1133
1134 float_obj = bt_value_float_create_init(val);
1135 ret = bt_value_array_append(array_obj, float_obj);
83509119 1136 bt_put(float_obj);
dac5c838
PP
1137 return ret;
1138}
1139
1140enum bt_value_status bt_value_array_append_string(struct bt_value *array_obj,
364747d6 1141 const char *val)
dac5c838
PP
1142{
1143 enum bt_value_status ret;
1144 struct bt_value *string_obj = NULL;
1145
1146 string_obj = bt_value_string_create_init(val);
1147 ret = bt_value_array_append(array_obj, string_obj);
83509119 1148 bt_put(string_obj);
dac5c838
PP
1149 return ret;
1150}
1151
5b79e8bf 1152enum bt_value_status bt_value_array_append_empty_array(
364747d6 1153 struct bt_value *array_obj)
dac5c838
PP
1154{
1155 enum bt_value_status ret;
1156 struct bt_value *empty_array_obj = NULL;
1157
1158 empty_array_obj = bt_value_array_create();
1159 ret = bt_value_array_append(array_obj, empty_array_obj);
83509119 1160 bt_put(empty_array_obj);
dac5c838
PP
1161 return ret;
1162}
1163
5b79e8bf 1164enum bt_value_status bt_value_array_append_empty_map(struct bt_value *array_obj)
dac5c838
PP
1165{
1166 enum bt_value_status ret;
1167 struct bt_value *map_obj = NULL;
1168
1169 map_obj = bt_value_map_create();
1170 ret = bt_value_array_append(array_obj, map_obj);
83509119 1171 bt_put(map_obj);
dac5c838
PP
1172 return ret;
1173}
1174
1175enum bt_value_status bt_value_array_set(struct bt_value *array_obj,
2f42aa0a 1176 uint64_t index, struct bt_value *element_obj)
dac5c838
PP
1177{
1178 enum bt_value_status ret = BT_VALUE_STATUS_OK;
1179 struct bt_value_array *typed_array_obj =
1180 BT_VALUE_TO_ARRAY(array_obj);
1181
2f42aa0a
PP
1182 if (!array_obj || !element_obj) {
1183 BT_LOGW("Invalid parameter: array value or element value is NULL: "
1184 "index=%" PRIu64 ", array-value-addr=%p, element-value-addr=%p",
1185 index, array_obj, element_obj);
1186 ret = BT_VALUE_STATUS_INVAL;
1187 goto end;
1188 }
1189
1190 if (!bt_value_is_array(array_obj)) {
1191 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
c4628760
PP
1192 "type=%s", array_obj,
1193 bt_value_type_string(array_obj->type));
2f42aa0a
PP
1194 ret = BT_VALUE_STATUS_INVAL;
1195 goto end;
1196 }
1197
1198 if (index >= typed_array_obj->garray->len) {
1199 BT_LOGW("Invalid parameter: index is out of bounds: "
78c6e1b1
PP
1200 "addr=%p, index=%" PRIu64 ", size=%u",
1201 array_obj, index, typed_array_obj->garray->len);
dac5c838
PP
1202 ret = BT_VALUE_STATUS_INVAL;
1203 goto end;
1204 }
1205
1206 if (array_obj->is_frozen) {
2f42aa0a
PP
1207 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1208 array_obj);
dac5c838
PP
1209 ret = BT_VALUE_STATUS_FROZEN;
1210 goto end;
1211 }
1212
83509119 1213 bt_put(g_ptr_array_index(typed_array_obj->garray, index));
dac5c838 1214 g_ptr_array_index(typed_array_obj->garray, index) = element_obj;
83509119 1215 bt_get(element_obj);
2f42aa0a
PP
1216 BT_LOGV("Set array value's element: array-value-addr=%p, "
1217 "index=%" PRIu64 ", element-value-addr=%p",
1218 array_obj, index, element_obj);
dac5c838
PP
1219
1220end:
1221 return ret;
1222}
1223
544d0515 1224int64_t bt_value_map_size(const struct bt_value *map_obj)
dac5c838 1225{
544d0515 1226 int64_t ret;
dac5c838
PP
1227 struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj);
1228
2f42aa0a
PP
1229 if (!map_obj) {
1230 BT_LOGW_STR("Invalid parameter: value object is NULL.");
1231 ret = (int64_t) BT_VALUE_STATUS_INVAL;
1232 goto end;
1233 }
1234
1235 if (!bt_value_is_map(map_obj)) {
1236 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
c4628760
PP
1237 "type=%s", map_obj,
1238 bt_value_type_string(map_obj->type));
9ac68eb1 1239 ret = (int64_t) BT_VALUE_STATUS_INVAL;
dac5c838
PP
1240 goto end;
1241 }
1242
544d0515 1243 ret = (int64_t) g_hash_table_size(typed_map_obj->ght);
dac5c838
PP
1244
1245end:
1246 return ret;
1247}
1248
c55a9f58 1249bt_bool bt_value_map_is_empty(const struct bt_value *map_obj)
dac5c838
PP
1250{
1251 return bt_value_map_size(map_obj) == 0;
1252}
1253
1254struct bt_value *bt_value_map_get(const struct bt_value *map_obj,
364747d6 1255 const char *key)
dac5c838
PP
1256{
1257 GQuark quark;
1258 struct bt_value *ret;
1259 struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj);
1260
2f42aa0a
PP
1261 if (!map_obj || !key) {
1262 BT_LOGW("Invalid parameter: value object or key is NULL: "
1263 "value-addr=%p, key-addr=%p", map_obj, key);
1264 ret = NULL;
1265 goto end;
1266 }
1267
1268 if (!bt_value_is_map(map_obj)) {
1269 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
c4628760
PP
1270 "type=%s", map_obj,
1271 bt_value_type_string(map_obj->type));
dac5c838
PP
1272 ret = NULL;
1273 goto end;
1274 }
1275
1276 quark = g_quark_from_string(key);
1277 ret = g_hash_table_lookup(typed_map_obj->ght, GUINT_TO_POINTER(quark));
dac5c838 1278 if (ret) {
83509119 1279 bt_get(ret);
dac5c838
PP
1280 }
1281
1282end:
1283 return ret;
1284}
1285
c55a9f58 1286bt_bool bt_value_map_has_key(const struct bt_value *map_obj, const char *key)
dac5c838 1287{
c55a9f58 1288 bt_bool ret;
dac5c838
PP
1289 GQuark quark;
1290 struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj);
1291
2f42aa0a
PP
1292 if (!map_obj || !key) {
1293 BT_LOGW("Invalid parameter: value object or key is NULL: "
1294 "value-addr=%p, key-addr=%p", map_obj, key);
c55a9f58 1295 ret = BT_FALSE;
2f42aa0a
PP
1296 goto end;
1297 }
1298
1299 if (!bt_value_is_map(map_obj)) {
1300 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
c4628760
PP
1301 "type=%s", map_obj,
1302 bt_value_type_string(map_obj->type));
c55a9f58 1303 ret = BT_FALSE;
dac5c838
PP
1304 goto end;
1305 }
1306
1307 quark = g_quark_from_string(key);
5d5982ab 1308 ret = bt_g_hash_table_contains(typed_map_obj->ght,
dac5c838
PP
1309 GUINT_TO_POINTER(quark));
1310
1311end:
1312 return ret;
1313}
1314
1315enum bt_value_status bt_value_map_insert(struct bt_value *map_obj,
364747d6 1316 const char *key, struct bt_value *element_obj)
dac5c838
PP
1317{
1318 GQuark quark;
1319 enum bt_value_status ret = BT_VALUE_STATUS_OK;
1320 struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj);
1321
2f42aa0a
PP
1322 if (!map_obj || !key || !element_obj) {
1323 BT_LOGW("Invalid parameter: map value, key, or element value is NULL: "
1324 "map-value-addr=%p, key-addr=%p, element-value-addr=%p",
1325 map_obj, key, element_obj);
1326 ret = BT_VALUE_STATUS_INVAL;
1327 goto end;
1328 }
1329
1330 if (!bt_value_is_map(map_obj)) {
1331 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
c4628760
PP
1332 "type=%s", map_obj,
1333 bt_value_type_string(map_obj->type));
dac5c838
PP
1334 ret = BT_VALUE_STATUS_INVAL;
1335 goto end;
1336 }
1337
1338 if (map_obj->is_frozen) {
2f42aa0a
PP
1339 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1340 map_obj);
dac5c838
PP
1341 ret = BT_VALUE_STATUS_FROZEN;
1342 goto end;
1343 }
1344
1345 quark = g_quark_from_string(key);
1346 g_hash_table_insert(typed_map_obj->ght,
1347 GUINT_TO_POINTER(quark), element_obj);
83509119 1348 bt_get(element_obj);
2f42aa0a
PP
1349 BT_LOGV("Inserted value into map value: map-value-addr=%p, "
1350 "key=\"%s\", element-value-addr=%p",
1351 map_obj, key, element_obj);
dac5c838
PP
1352
1353end:
1354 return ret;
1355}
1356
1357enum bt_value_status bt_value_map_insert_bool(struct bt_value *map_obj,
c55a9f58 1358 const char *key, bt_bool val)
dac5c838
PP
1359{
1360 enum bt_value_status ret;
1361 struct bt_value *bool_obj = NULL;
1362
1363 bool_obj = bt_value_bool_create_init(val);
1364 ret = bt_value_map_insert(map_obj, key, bool_obj);
83509119 1365 bt_put(bool_obj);
dac5c838
PP
1366 return ret;
1367}
1368
1369enum bt_value_status bt_value_map_insert_integer(struct bt_value *map_obj,
364747d6 1370 const char *key, int64_t val)
dac5c838
PP
1371{
1372 enum bt_value_status ret;
1373 struct bt_value *integer_obj = NULL;
1374
1375 integer_obj = bt_value_integer_create_init(val);
1376 ret = bt_value_map_insert(map_obj, key, integer_obj);
83509119 1377 bt_put(integer_obj);
dac5c838
PP
1378 return ret;
1379}
1380
1381enum bt_value_status bt_value_map_insert_float(struct bt_value *map_obj,
364747d6 1382 const char *key, double val)
dac5c838
PP
1383{
1384 enum bt_value_status ret;
1385 struct bt_value *float_obj = NULL;
1386
1387 float_obj = bt_value_float_create_init(val);
1388 ret = bt_value_map_insert(map_obj, key, float_obj);
83509119 1389 bt_put(float_obj);
dac5c838
PP
1390 return ret;
1391}
1392
1393enum bt_value_status bt_value_map_insert_string(struct bt_value *map_obj,
364747d6 1394 const char *key, const char *val)
dac5c838
PP
1395{
1396 enum bt_value_status ret;
1397 struct bt_value *string_obj = NULL;
1398
1399 string_obj = bt_value_string_create_init(val);
1400 ret = bt_value_map_insert(map_obj, key, string_obj);
83509119 1401 bt_put(string_obj);
dac5c838
PP
1402 return ret;
1403}
1404
5b79e8bf 1405enum bt_value_status bt_value_map_insert_empty_array(struct bt_value *map_obj,
364747d6 1406 const char *key)
dac5c838
PP
1407{
1408 enum bt_value_status ret;
1409 struct bt_value *array_obj = NULL;
1410
1411 array_obj = bt_value_array_create();
1412 ret = bt_value_map_insert(map_obj, key, array_obj);
83509119 1413 bt_put(array_obj);
dac5c838
PP
1414 return ret;
1415}
1416
5b79e8bf 1417enum bt_value_status bt_value_map_insert_empty_map(struct bt_value *map_obj,
364747d6 1418 const char *key)
dac5c838
PP
1419{
1420 enum bt_value_status ret;
1421 struct bt_value *empty_map_obj = NULL;
1422
1423 empty_map_obj = bt_value_map_create();
1424 ret = bt_value_map_insert(map_obj, key, empty_map_obj);
83509119 1425 bt_put(empty_map_obj);
dac5c838
PP
1426 return ret;
1427}
1428
1429enum bt_value_status bt_value_map_foreach(const struct bt_value *map_obj,
364747d6 1430 bt_value_map_foreach_cb cb, void *data)
dac5c838
PP
1431{
1432 enum bt_value_status ret = BT_VALUE_STATUS_OK;
1433 gpointer key, element_obj;
1434 GHashTableIter iter;
1435 struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj);
1436
2f42aa0a
PP
1437 if (!map_obj || !cb) {
1438 BT_LOGW("Invalid parameter: map value or callback is NULL: "
1439 "value-addr=%p, cb-addr=%p", map_obj, cb);
1440 ret = BT_VALUE_STATUS_INVAL;
1441 goto end;
1442 }
1443
1444 if (!bt_value_is_map(map_obj)) {
1445 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
c4628760
PP
1446 "type=%s", map_obj,
1447 bt_value_type_string(map_obj->type));
dac5c838
PP
1448 ret = BT_VALUE_STATUS_INVAL;
1449 goto end;
1450 }
1451
1452 g_hash_table_iter_init(&iter, typed_map_obj->ght);
1453
1454 while (g_hash_table_iter_next(&iter, &key, &element_obj)) {
5b44aff2 1455 const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key));
dac5c838
PP
1456
1457 if (!cb(key_str, element_obj, data)) {
c4628760 1458 BT_LOGV("User cancelled the loop: key=\"%s\", "
2f42aa0a
PP
1459 "value-addr=%p, data=%p",
1460 key_str, element_obj, data);
dac5c838
PP
1461 ret = BT_VALUE_STATUS_CANCELLED;
1462 break;
1463 }
1464 }
1465
1466end:
1467 return ret;
1468}
1469
770750d3
PP
1470struct extend_map_element_data {
1471 struct bt_value *extended_obj;
c55a9f58 1472 bt_bool got_error;
770750d3
PP
1473};
1474
1475static
c55a9f58 1476bt_bool extend_map_element(const char *key,
770750d3
PP
1477 struct bt_value *extension_obj_elem, void *data)
1478{
c55a9f58 1479 bt_bool ret = BT_TRUE;
770750d3
PP
1480
1481 struct extend_map_element_data *extend_data = data;
1482
1483 /* Copy object which is to replace the current one */
1484 struct bt_value *extension_obj_elem_copy =
1485 bt_value_copy(extension_obj_elem);
1486
1487 /* Replace in extended object */
1488 if (bt_value_map_insert(extend_data->extended_obj, key,
1489 extension_obj_elem_copy)) {
2f42aa0a
PP
1490 BT_LOGE("Cannot replace value in extended value: key=\"%s\", "
1491 "extended-value-addr=%p, element-value-addr=%p",
1492 key, extend_data->extended_obj,
1493 extension_obj_elem_copy);
770750d3
PP
1494 goto error;
1495 }
1496
1497 goto end;
1498
1499error:
c55a9f58
PP
1500 ret = BT_FALSE;
1501 extend_data->got_error = BT_TRUE;
770750d3
PP
1502
1503end:
1504 BT_PUT(extension_obj_elem_copy);
770750d3
PP
1505 return ret;
1506}
1507
1508struct bt_value *bt_value_map_extend(struct bt_value *base_map_obj,
1509 struct bt_value *extension_obj)
1510{
1511 struct bt_value *extended_obj = NULL;
1512 struct extend_map_element_data extend_data = { 0 };
1513
2f42aa0a
PP
1514 if (!base_map_obj || !extension_obj) {
1515 BT_LOGW("Invalid parameter: base value or extension value is NULL: "
1516 "base-value-addr=%p, extension-value-addr=%p",
1517 base_map_obj, extension_obj);
1518 goto error;
1519 }
1520
1521 if (!bt_value_is_map(base_map_obj)) {
1522 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
c4628760
PP
1523 "type=%s", base_map_obj,
1524 bt_value_type_string(base_map_obj->type));
770750d3
PP
1525 goto error;
1526 }
1527
2f42aa0a
PP
1528 if (!bt_value_is_map(extension_obj)) {
1529 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
c4628760
PP
1530 "type=%s", extension_obj,
1531 bt_value_type_string(extension_obj->type));
2f42aa0a
PP
1532 goto error;
1533 }
1534
1535 BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p",
1536 base_map_obj, extension_obj);
1537
770750d3
PP
1538 /* Create copy of base map object to start with */
1539 extended_obj = bt_value_copy(base_map_obj);
1540 if (!extended_obj) {
2f42aa0a
PP
1541 BT_LOGE("Cannot copy base value: base-value-addr=%p",
1542 base_map_obj);
770750d3
PP
1543 goto error;
1544 }
1545
1546 /*
1547 * For each key in the extension map object, replace this key
1548 * in the copied map object.
1549 */
1550 extend_data.extended_obj = extended_obj;
1551
1552 if (bt_value_map_foreach(extension_obj, extend_map_element,
1553 &extend_data)) {
2f42aa0a
PP
1554 BT_LOGE("Cannot iterate on the extension object's elements: ",
1555 "extension-value-addr=%p", extension_obj);
770750d3
PP
1556 goto error;
1557 }
1558
1559 if (extend_data.got_error) {
2f42aa0a
PP
1560 BT_LOGE("Failed to successfully iterate on the extension object's elements: ",
1561 "extension-value-addr=%p", extension_obj);
770750d3
PP
1562 goto error;
1563 }
1564
2f42aa0a
PP
1565 BT_LOGD("Extended map value: extended-value-addr=%p",
1566 extended_obj);
770750d3
PP
1567 goto end;
1568
1569error:
1570 BT_PUT(extended_obj);
1571
1572end:
1573 return extended_obj;
1574}
1575
dac5c838
PP
1576struct bt_value *bt_value_copy(const struct bt_value *object)
1577{
1578 struct bt_value *copy_obj = NULL;
1579
1580 if (!object) {
2f42aa0a 1581 BT_LOGW_STR("Invalid parameter: value object is NULL.");
dac5c838
PP
1582 goto end;
1583 }
1584
2f42aa0a 1585 BT_LOGD("Copying value object: addr=%p", object);
dac5c838 1586 copy_obj = copy_funcs[object->type](object);
2f42aa0a
PP
1587 if (copy_obj) {
1588 BT_LOGD("Copied value object: copy-value-addr=%p",
1589 copy_obj);
1590 } else {
1591 BT_LOGE_STR("Failed to copy value object.");
1592 }
dac5c838
PP
1593
1594end:
1595 return copy_obj;
1596}
1597
c55a9f58 1598bt_bool bt_value_compare(const struct bt_value *object_a,
dac5c838
PP
1599 const struct bt_value *object_b)
1600{
c55a9f58 1601 bt_bool ret = BT_FALSE;
dac5c838
PP
1602
1603 if (!object_a || !object_b) {
2f42aa0a
PP
1604 BT_LOGW("Invalid parameter: value A or value B is NULL: "
1605 "value-a-addr=%p, value-b-addr=%p",
1606 object_a, object_b);
dac5c838
PP
1607 goto end;
1608 }
1609
1610 if (object_a->type != object_b->type) {
2f42aa0a
PP
1611 BT_LOGV("Values are different: type mismatch: "
1612 "value-a-addr=%p, value-b-addr=%p, "
c4628760 1613 "value-a-type=%s, value-b-type=%s",
2f42aa0a 1614 object_a, object_b,
c4628760
PP
1615 bt_value_type_string(object_a->type),
1616 bt_value_type_string(object_b->type));
dac5c838
PP
1617 goto end;
1618 }
1619
1620 ret = compare_funcs[object_a->type](object_a, object_b);
1621
1622end:
1623 return ret;
1624}
This page took 0.104383 seconds and 4 git commands to generate.