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