cli/babeltrace.c: improve main loop's 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
PP
28#define BT_LOG_TAG "VALUES"
29
dac5c838
PP
30#include <stdlib.h>
31#include <string.h>
32#include <assert.h>
33#include <string.h>
2f42aa0a 34#include <inttypes.h>
3d9990ac 35#include <babeltrace/compiler-internal.h>
83509119 36#include <babeltrace/ref.h>
dac5c838 37#include <babeltrace/values.h>
3d9990ac 38#include <babeltrace/compat/glib-internal.h>
c55a9f58 39#include <babeltrace/types.h>
dac5c838 40
2f42aa0a 41#include <babeltrace/lib-logging-internal.h>
0f5e83e5 42#include <babeltrace/object-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
2f42aa0a
PP
229 BT_LOGD("Copied array value: addr=%p", array_obj);
230
dac5c838
PP
231end:
232 return copy_obj;
233}
234
235static
236struct bt_value *bt_value_map_copy(const struct bt_value *map_obj)
237{
238 int ret;
239 GHashTableIter iter;
240 gpointer key, element_obj;
241 struct bt_value *copy_obj;
242 struct bt_value *element_obj_copy;
243 struct bt_value_map *typed_map_obj;
244
2f42aa0a 245 BT_LOGD("Copying map value: addr=%p", map_obj);
dac5c838
PP
246 typed_map_obj = BT_VALUE_TO_MAP(map_obj);
247 copy_obj = bt_value_map_create();
248
249 if (!copy_obj) {
250 goto end;
251 }
252
253 g_hash_table_iter_init(&iter, typed_map_obj->ght);
254
255 while (g_hash_table_iter_next(&iter, &key, &element_obj)) {
5b44aff2 256 const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key));
dac5c838
PP
257
258 element_obj_copy = bt_value_copy(element_obj);
259
260 if (!element_obj_copy) {
2f42aa0a
PP
261 BT_LOGE("Cannot copy map value's element: "
262 "map-addr=%p, key=\"%s\"",
263 map_obj, key_str);
83509119 264 BT_PUT(copy_obj);
dac5c838
PP
265 goto end;
266 }
267
268 ret = bt_value_map_insert(copy_obj, key_str, element_obj_copy);
83509119 269 BT_PUT(element_obj_copy);
dac5c838
PP
270
271 if (ret) {
2f42aa0a
PP
272 BT_LOGE("Cannot insert into map value: addr=%p, key=\"%s\"",
273 map_obj, key_str);
83509119 274 BT_PUT(copy_obj);
dac5c838
PP
275 goto end;
276 }
277 }
278
2f42aa0a
PP
279 BT_LOGD("Copied map value: addr=%p", map_obj);
280
dac5c838
PP
281end:
282 return copy_obj;
283}
284
285static
286struct bt_value *(* const copy_funcs[])(const struct bt_value *) = {
287 [BT_VALUE_TYPE_NULL] = bt_value_null_copy,
288 [BT_VALUE_TYPE_BOOL] = bt_value_bool_copy,
289 [BT_VALUE_TYPE_INTEGER] = bt_value_integer_copy,
290 [BT_VALUE_TYPE_FLOAT] = bt_value_float_copy,
291 [BT_VALUE_TYPE_STRING] = bt_value_string_copy,
292 [BT_VALUE_TYPE_ARRAY] = bt_value_array_copy,
293 [BT_VALUE_TYPE_MAP] = bt_value_map_copy,
294};
295
296static
c55a9f58 297bt_bool bt_value_null_compare(const struct bt_value *object_a,
dac5c838
PP
298 const struct bt_value *object_b)
299{
300 /*
c55a9f58 301 * Always BT_TRUE since bt_value_compare() already checks if both
dac5c838
PP
302 * object_a and object_b have the same type, and in the case of
303 * null value objects, they're always the same if it is so.
304 */
c55a9f58 305 return BT_TRUE;
dac5c838
PP
306}
307
308static
c55a9f58 309bt_bool bt_value_bool_compare(const struct bt_value *object_a,
dac5c838
PP
310 const struct bt_value *object_b)
311{
312 return BT_VALUE_TO_BOOL(object_a)->value ==
313 BT_VALUE_TO_BOOL(object_b)->value;
314}
315
316static
c55a9f58 317bt_bool bt_value_integer_compare(const struct bt_value *object_a,
dac5c838
PP
318 const struct bt_value *object_b)
319{
320 return BT_VALUE_TO_INTEGER(object_a)->value ==
321 BT_VALUE_TO_INTEGER(object_b)->value;
322}
323
324static
c55a9f58 325bt_bool bt_value_float_compare(const struct bt_value *object_a,
dac5c838
PP
326 const struct bt_value *object_b)
327{
328 return BT_VALUE_TO_FLOAT(object_a)->value ==
329 BT_VALUE_TO_FLOAT(object_b)->value;
330}
331
332static
c55a9f58 333bt_bool bt_value_string_compare(const struct bt_value *object_a,
dac5c838
PP
334 const struct bt_value *object_b)
335{
336 return !strcmp(BT_VALUE_TO_STRING(object_a)->gstr->str,
337 BT_VALUE_TO_STRING(object_b)->gstr->str);
338}
339
340static
c55a9f58 341bt_bool bt_value_array_compare(const struct bt_value *object_a,
dac5c838
PP
342 const struct bt_value *object_b)
343{
344 int i;
c55a9f58 345 bt_bool ret = BT_TRUE;
dac5c838
PP
346 const struct bt_value_array *array_obj_a =
347 BT_VALUE_TO_ARRAY(object_a);
348
349 if (bt_value_array_size(object_a) != bt_value_array_size(object_b)) {
2f42aa0a
PP
350 BT_LOGV("Array values are different: size mismatch "
351 "value-a-addr=%p, value-b-addr=%p, "
352 "value-a-size=%" PRId64 ", value-b-size=%" PRId64,
353 object_a, object_b,
354 bt_value_array_size(object_a),
355 bt_value_array_size(object_b));
c55a9f58 356 ret = BT_FALSE;
dac5c838
PP
357 goto end;
358 }
359
360 for (i = 0; i < array_obj_a->garray->len; ++i) {
361 struct bt_value *element_obj_a;
362 struct bt_value *element_obj_b;
363
364 element_obj_a = bt_value_array_get(object_a, i);
365 element_obj_b = bt_value_array_get(object_b, i);
366
367 if (!bt_value_compare(element_obj_a, element_obj_b)) {
2f42aa0a
PP
368 BT_LOGV("Array values's elements are different: "
369 "value-a-addr=%p, value-b-addr=%p, index=%d",
370 element_obj_a, element_obj_b, index);
83509119
JG
371 BT_PUT(element_obj_a);
372 BT_PUT(element_obj_b);
c55a9f58 373 ret = BT_FALSE;
dac5c838
PP
374 goto end;
375 }
376
83509119
JG
377 BT_PUT(element_obj_a);
378 BT_PUT(element_obj_b);
dac5c838
PP
379 }
380
381end:
382 return ret;
383}
384
385static
c55a9f58 386bt_bool bt_value_map_compare(const struct bt_value *object_a,
dac5c838
PP
387 const struct bt_value *object_b)
388{
c55a9f58 389 bt_bool ret = BT_TRUE;
dac5c838
PP
390 GHashTableIter iter;
391 gpointer key, element_obj_a;
392 const struct bt_value_map *map_obj_a = BT_VALUE_TO_MAP(object_a);
393
394 if (bt_value_map_size(object_a) != bt_value_map_size(object_b)) {
2f42aa0a
PP
395 BT_LOGV("Map values are different: size mismatch "
396 "value-a-addr=%p, value-b-addr=%p, "
397 "value-a-size=%" PRId64 ", value-b-size=%" PRId64,
398 object_a, object_b,
399 bt_value_map_size(object_a),
400 bt_value_map_size(object_b));
c55a9f58 401 ret = BT_FALSE;
dac5c838
PP
402 goto end;
403 }
404
405 g_hash_table_iter_init(&iter, map_obj_a->ght);
406
407 while (g_hash_table_iter_next(&iter, &key, &element_obj_a)) {
408 struct bt_value *element_obj_b;
5b44aff2 409 const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key));
dac5c838
PP
410
411 element_obj_b = bt_value_map_get(object_b, key_str);
412
413 if (!bt_value_compare(element_obj_a, element_obj_b)) {
2f42aa0a
PP
414 BT_LOGV("Map values's elements are different: "
415 "value-a-addr=%p, value-b-addr=%p, key=\"%s\"",
416 element_obj_a, element_obj_b, key_str);
83509119 417 BT_PUT(element_obj_b);
c55a9f58 418 ret = BT_FALSE;
dac5c838
PP
419 goto end;
420 }
421
83509119 422 BT_PUT(element_obj_b);
dac5c838
PP
423 }
424
425end:
426 return ret;
427}
428
429static
c55a9f58 430bt_bool (* const compare_funcs[])(const struct bt_value *,
dac5c838
PP
431 const struct bt_value *) = {
432 [BT_VALUE_TYPE_NULL] = bt_value_null_compare,
433 [BT_VALUE_TYPE_BOOL] = bt_value_bool_compare,
434 [BT_VALUE_TYPE_INTEGER] = bt_value_integer_compare,
435 [BT_VALUE_TYPE_FLOAT] = bt_value_float_compare,
436 [BT_VALUE_TYPE_STRING] = bt_value_string_compare,
437 [BT_VALUE_TYPE_ARRAY] = bt_value_array_compare,
438 [BT_VALUE_TYPE_MAP] = bt_value_map_compare,
439};
440
441void bt_value_null_freeze(struct bt_value *object)
442{
443}
444
445void bt_value_generic_freeze(struct bt_value *object)
446{
c55a9f58 447 object->is_frozen = BT_TRUE;
dac5c838
PP
448}
449
450void bt_value_array_freeze(struct bt_value *object)
451{
452 int i;
453 struct bt_value_array *typed_array_obj =
454 BT_VALUE_TO_ARRAY(object);
455
456 for (i = 0; i < typed_array_obj->garray->len; ++i) {
457 struct bt_value *element_obj =
458 g_ptr_array_index(typed_array_obj->garray, i);
459
460 bt_value_freeze(element_obj);
461 }
462
463 bt_value_generic_freeze(object);
464}
465
466void bt_value_map_freeze(struct bt_value *object)
467{
468 GHashTableIter iter;
469 gpointer key, element_obj;
470 const struct bt_value_map *map_obj = BT_VALUE_TO_MAP(object);
471
472 g_hash_table_iter_init(&iter, map_obj->ght);
473
474 while (g_hash_table_iter_next(&iter, &key, &element_obj)) {
475 bt_value_freeze(element_obj);
476 }
477
478 bt_value_generic_freeze(object);
479}
480
481static
482void (* const freeze_funcs[])(struct bt_value *) = {
483 [BT_VALUE_TYPE_NULL] = bt_value_null_freeze,
484 [BT_VALUE_TYPE_BOOL] = bt_value_generic_freeze,
485 [BT_VALUE_TYPE_INTEGER] = bt_value_generic_freeze,
486 [BT_VALUE_TYPE_FLOAT] = bt_value_generic_freeze,
487 [BT_VALUE_TYPE_STRING] = bt_value_generic_freeze,
488 [BT_VALUE_TYPE_ARRAY] = bt_value_array_freeze,
489 [BT_VALUE_TYPE_MAP] = bt_value_map_freeze,
490};
491
492static
83509119 493void bt_value_destroy(struct bt_object *obj)
dac5c838 494{
83509119 495 struct bt_value *value;
dac5c838 496
83509119
JG
497 value = container_of(obj, struct bt_value, base);
498 assert(value->type != BT_VALUE_TYPE_UNKNOWN);
dac5c838 499
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, "
753 "type=%d", bool_obj, bool_obj->type);
dac5c838
PP
754 ret = BT_VALUE_STATUS_INVAL;
755 goto end;
756 }
757
758 *val = typed_bool_obj->value;
759
760end:
761 return ret;
762}
763
c55a9f58 764enum bt_value_status bt_value_bool_set(struct bt_value *bool_obj, bt_bool val)
dac5c838
PP
765{
766 enum bt_value_status ret = BT_VALUE_STATUS_OK;
767 struct bt_value_bool *typed_bool_obj = BT_VALUE_TO_BOOL(bool_obj);
768
2f42aa0a
PP
769 if (!bool_obj) {
770 BT_LOGW_STR("Invalid parameter: value object is NULL.");
771 ret = BT_VALUE_STATUS_INVAL;
772 goto end;
773 }
774
775 if (!bt_value_is_bool(bool_obj)) {
776 BT_LOGW("Invalid parameter: value is not a boolean value: addr=%p, "
777 "type=%d", bool_obj, bool_obj->type);
dac5c838
PP
778 ret = BT_VALUE_STATUS_INVAL;
779 goto end;
780 }
781
782 if (bool_obj->is_frozen) {
2f42aa0a
PP
783 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
784 bool_obj);
dac5c838
PP
785 ret = BT_VALUE_STATUS_FROZEN;
786 goto end;
787 }
788
789 typed_bool_obj->value = val;
2f42aa0a
PP
790 BT_LOGV("Set boolean value's raw value: value-addr=%p, value=%d",
791 bool_obj, val);
dac5c838
PP
792
793end:
794 return ret;
795}
796
797enum bt_value_status bt_value_integer_get(const struct bt_value *integer_obj,
364747d6 798 int64_t *val)
dac5c838
PP
799{
800 enum bt_value_status ret = BT_VALUE_STATUS_OK;
801 struct bt_value_integer *typed_integer_obj =
802 BT_VALUE_TO_INTEGER(integer_obj);
803
2f42aa0a
PP
804 if (!integer_obj || !val) {
805 BT_LOGW("Invalid parameter: value object or value is NULL: "
806 "value-addr=%p, raw-value-addr=%p",
807 integer_obj, val);
808 ret = BT_VALUE_STATUS_INVAL;
809 goto end;
810 }
811
812 if (!bt_value_is_integer(integer_obj)) {
813 BT_LOGW("Invalid parameter: value is not an integer value: addr=%p, "
814 "type=%d", integer_obj, integer_obj->type);
dac5c838
PP
815 ret = BT_VALUE_STATUS_INVAL;
816 goto end;
817 }
818
819 *val = typed_integer_obj->value;
820
821end:
822 return ret;
823}
824
825enum bt_value_status bt_value_integer_set(struct bt_value *integer_obj,
364747d6 826 int64_t val)
dac5c838
PP
827{
828 enum bt_value_status ret = BT_VALUE_STATUS_OK;
829 struct bt_value_integer *typed_integer_obj =
830 BT_VALUE_TO_INTEGER(integer_obj);
831
2f42aa0a
PP
832 if (!integer_obj) {
833 BT_LOGW_STR("Invalid parameter: value object is NULL.");
834 ret = BT_VALUE_STATUS_INVAL;
835 goto end;
836 }
837
838 if (!bt_value_is_integer(integer_obj)) {
839 BT_LOGW("Invalid parameter: value is not an integer value: addr=%p, "
840 "type=%d", integer_obj, integer_obj->type);
dac5c838
PP
841 ret = BT_VALUE_STATUS_INVAL;
842 goto end;
843 }
844
845 if (integer_obj->is_frozen) {
2f42aa0a
PP
846 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
847 integer_obj);
dac5c838
PP
848 ret = BT_VALUE_STATUS_FROZEN;
849 goto end;
850 }
851
852 typed_integer_obj->value = val;
2f42aa0a
PP
853 BT_LOGV("Set integer value's raw value: value-addr=%p, value=%" PRId64,
854 integer_obj, val);
dac5c838
PP
855
856end:
857 return ret;
858}
859
860enum bt_value_status bt_value_float_get(const struct bt_value *float_obj,
364747d6 861 double *val)
dac5c838
PP
862{
863 enum bt_value_status ret = BT_VALUE_STATUS_OK;
864 struct bt_value_float *typed_float_obj =
865 BT_VALUE_TO_FLOAT(float_obj);
866
2f42aa0a
PP
867 if (!float_obj || !val) {
868 BT_LOGW("Invalid parameter: value object or value is NULL: "
869 "value-addr=%p, raw-value-addr=%p",
870 float_obj, val);
871 ret = BT_VALUE_STATUS_INVAL;
872 goto end;
873 }
874
875 if (!bt_value_is_float(float_obj)) {
876 BT_LOGW("Invalid parameter: value is not a floating point number value: addr=%p, "
877 "type=%d", float_obj, float_obj->type);
dac5c838
PP
878 ret = BT_VALUE_STATUS_INVAL;
879 goto end;
880 }
881
882 *val = typed_float_obj->value;
883
884end:
885 return ret;
886}
887
888enum bt_value_status bt_value_float_set(struct bt_value *float_obj,
364747d6 889 double val)
dac5c838
PP
890{
891 enum bt_value_status ret = BT_VALUE_STATUS_OK;
892 struct bt_value_float *typed_float_obj =
893 BT_VALUE_TO_FLOAT(float_obj);
894
2f42aa0a
PP
895 if (!float_obj) {
896 BT_LOGW_STR("Invalid parameter: value object is NULL.");
897 ret = BT_VALUE_STATUS_INVAL;
898 goto end;
899 }
900
901 if (!bt_value_is_float(float_obj)) {
902 BT_LOGW("Invalid parameter: value is not a floating point number value: addr=%p, "
903 "type=%d", float_obj, float_obj->type);
dac5c838
PP
904 ret = BT_VALUE_STATUS_INVAL;
905 goto end;
906 }
907
908 if (float_obj->is_frozen) {
2f42aa0a
PP
909 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
910 float_obj);
dac5c838
PP
911 ret = BT_VALUE_STATUS_FROZEN;
912 goto end;
913 }
914
915 typed_float_obj->value = val;
2f42aa0a
PP
916 BT_LOGV("Set floating point number value's raw value: value-addr=%p, value=%f",
917 float_obj, val);
dac5c838
PP
918
919end:
920 return ret;
921}
922
923enum bt_value_status bt_value_string_get(const struct bt_value *string_obj,
364747d6 924 const char **val)
dac5c838
PP
925{
926 enum bt_value_status ret = BT_VALUE_STATUS_OK;
927 struct bt_value_string *typed_string_obj =
928 BT_VALUE_TO_STRING(string_obj);
929
2f42aa0a
PP
930 if (!string_obj || !val) {
931 BT_LOGW("Invalid parameter: value object or value is NULL: "
932 "value-addr=%p, raw-value-addr=%p",
933 string_obj, val);
934 ret = BT_VALUE_STATUS_INVAL;
935 goto end;
936 }
937
938 if (!bt_value_is_string(string_obj)) {
939 BT_LOGW("Invalid parameter: value is not a string value: addr=%p, "
940 "type=%d", string_obj, string_obj->type);
dac5c838
PP
941 ret = BT_VALUE_STATUS_INVAL;
942 goto end;
943 }
944
945 *val = typed_string_obj->gstr->str;
946
947end:
948 return ret;
949}
950
951enum bt_value_status bt_value_string_set(struct bt_value *string_obj,
364747d6 952 const char *val)
dac5c838
PP
953{
954 enum bt_value_status ret = BT_VALUE_STATUS_OK;
955 struct bt_value_string *typed_string_obj =
956 BT_VALUE_TO_STRING(string_obj);
957
2f42aa0a
PP
958 if (!string_obj || !val) {
959 BT_LOGW("Invalid parameter: value object or value is NULL: "
960 "value-addr=%p, raw-value-addr=%p",
961 string_obj, val);
962 ret = BT_VALUE_STATUS_INVAL;
963 goto end;
964 }
965
966 if (!bt_value_is_string(string_obj)) {
967 BT_LOGW("Invalid parameter: value is not a string value: addr=%p, "
968 "type=%d", string_obj, string_obj->type);
dac5c838
PP
969 ret = BT_VALUE_STATUS_INVAL;
970 goto end;
971 }
972
973 if (string_obj->is_frozen) {
2f42aa0a
PP
974 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
975 string_obj);
dac5c838
PP
976 ret = BT_VALUE_STATUS_FROZEN;
977 goto end;
978 }
979
980 g_string_assign(typed_string_obj->gstr, val);
2f42aa0a
PP
981 BT_LOGV("Set string value's raw value: value-addr=%p, raw-value-addr=%p",
982 string_obj, val);
dac5c838
PP
983
984end:
985 return ret;
986}
987
544d0515 988int64_t bt_value_array_size(const struct bt_value *array_obj)
dac5c838 989{
544d0515 990 int64_t ret;
dac5c838
PP
991 struct bt_value_array *typed_array_obj =
992 BT_VALUE_TO_ARRAY(array_obj);
993
2f42aa0a
PP
994 if (!array_obj) {
995 BT_LOGW_STR("Invalid parameter: value object is NULL.");
9ac68eb1 996 ret = (int64_t) BT_VALUE_STATUS_INVAL;
dac5c838
PP
997 goto end;
998 }
999
2f42aa0a
PP
1000 if (!bt_value_is_array(array_obj)) {
1001 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1002 "type=%d", array_obj, array_obj->type);
1003 ret = BT_VALUE_STATUS_INVAL;
1004 goto end;
1005 }
1006
544d0515 1007 ret = (int64_t) typed_array_obj->garray->len;
dac5c838
PP
1008
1009end:
1010 return ret;
1011}
1012
c55a9f58 1013bt_bool bt_value_array_is_empty(const struct bt_value *array_obj)
dac5c838
PP
1014{
1015 return bt_value_array_size(array_obj) == 0;
1016}
1017
1018struct bt_value *bt_value_array_get(const struct bt_value *array_obj,
2f42aa0a 1019 uint64_t index)
dac5c838
PP
1020{
1021 struct bt_value *ret;
1022 struct bt_value_array *typed_array_obj =
1023 BT_VALUE_TO_ARRAY(array_obj);
1024
2f42aa0a
PP
1025 if (!array_obj) {
1026 BT_LOGW("Invalid parameter: value object is NULL: index=%" PRIu64,
1027 index);
1028 ret = NULL;
1029 goto end;
1030 }
1031
1032 if (!bt_value_is_array(array_obj)) {
1033 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1034 "type=%d", array_obj, array_obj->type);
1035 ret = NULL;
1036 goto end;
1037 }
1038
1039 if (index >= typed_array_obj->garray->len) {
1040 BT_LOGW("Invalid parameter: index is out of bounds: "
1041 "addr=%p, index=%" PRIu64,
1042 array_obj, index);
dac5c838
PP
1043 ret = NULL;
1044 goto end;
1045 }
1046
1047 ret = g_ptr_array_index(typed_array_obj->garray, index);
83509119 1048 bt_get(ret);
dac5c838
PP
1049
1050end:
1051 return ret;
1052}
1053
1054enum bt_value_status bt_value_array_append(struct bt_value *array_obj,
364747d6 1055 struct bt_value *element_obj)
dac5c838
PP
1056{
1057 enum bt_value_status ret = BT_VALUE_STATUS_OK;
1058 struct bt_value_array *typed_array_obj =
1059 BT_VALUE_TO_ARRAY(array_obj);
1060
2f42aa0a
PP
1061 if (!array_obj || !element_obj) {
1062 BT_LOGW("Invalid parameter: array value or element value is NULL: "
1063 "array-value-addr=%p, element-value-addr=%p",
1064 array_obj, element_obj);
1065 ret = BT_VALUE_STATUS_INVAL;
1066 goto end;
1067 }
1068
1069 if (!bt_value_is_array(array_obj)) {
1070 BT_LOGW("Invalid parameter: value is not an array value: addr=%p, "
1071 "type=%d", array_obj, 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, "
1181 "type=%d", array_obj, array_obj->type);
1182 ret = BT_VALUE_STATUS_INVAL;
1183 goto end;
1184 }
1185
1186 if (index >= typed_array_obj->garray->len) {
1187 BT_LOGW("Invalid parameter: index is out of bounds: "
1188 "addr=%p, index=%" PRIu64,
1189 array_obj, index);
dac5c838
PP
1190 ret = BT_VALUE_STATUS_INVAL;
1191 goto end;
1192 }
1193
1194 if (array_obj->is_frozen) {
2f42aa0a
PP
1195 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1196 array_obj);
dac5c838
PP
1197 ret = BT_VALUE_STATUS_FROZEN;
1198 goto end;
1199 }
1200
83509119 1201 bt_put(g_ptr_array_index(typed_array_obj->garray, index));
dac5c838 1202 g_ptr_array_index(typed_array_obj->garray, index) = element_obj;
83509119 1203 bt_get(element_obj);
2f42aa0a
PP
1204 BT_LOGV("Set array value's element: array-value-addr=%p, "
1205 "index=%" PRIu64 ", element-value-addr=%p",
1206 array_obj, index, element_obj);
dac5c838
PP
1207
1208end:
1209 return ret;
1210}
1211
544d0515 1212int64_t bt_value_map_size(const struct bt_value *map_obj)
dac5c838 1213{
544d0515 1214 int64_t ret;
dac5c838
PP
1215 struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj);
1216
2f42aa0a
PP
1217 if (!map_obj) {
1218 BT_LOGW_STR("Invalid parameter: value object is NULL.");
1219 ret = (int64_t) BT_VALUE_STATUS_INVAL;
1220 goto end;
1221 }
1222
1223 if (!bt_value_is_map(map_obj)) {
1224 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1225 "type=%d", map_obj, map_obj->type);
9ac68eb1 1226 ret = (int64_t) BT_VALUE_STATUS_INVAL;
dac5c838
PP
1227 goto end;
1228 }
1229
544d0515 1230 ret = (int64_t) g_hash_table_size(typed_map_obj->ght);
dac5c838
PP
1231
1232end:
1233 return ret;
1234}
1235
c55a9f58 1236bt_bool bt_value_map_is_empty(const struct bt_value *map_obj)
dac5c838
PP
1237{
1238 return bt_value_map_size(map_obj) == 0;
1239}
1240
1241struct bt_value *bt_value_map_get(const struct bt_value *map_obj,
364747d6 1242 const char *key)
dac5c838
PP
1243{
1244 GQuark quark;
1245 struct bt_value *ret;
1246 struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj);
1247
2f42aa0a
PP
1248 if (!map_obj || !key) {
1249 BT_LOGW("Invalid parameter: value object or key is NULL: "
1250 "value-addr=%p, key-addr=%p", map_obj, key);
1251 ret = NULL;
1252 goto end;
1253 }
1254
1255 if (!bt_value_is_map(map_obj)) {
1256 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1257 "type=%d", map_obj, map_obj->type);
dac5c838
PP
1258 ret = NULL;
1259 goto end;
1260 }
1261
1262 quark = g_quark_from_string(key);
1263 ret = g_hash_table_lookup(typed_map_obj->ght, GUINT_TO_POINTER(quark));
1264
1265 if (ret) {
83509119 1266 bt_get(ret);
dac5c838
PP
1267 }
1268
1269end:
1270 return ret;
1271}
1272
c55a9f58 1273bt_bool bt_value_map_has_key(const struct bt_value *map_obj, const char *key)
dac5c838 1274{
c55a9f58 1275 bt_bool ret;
dac5c838
PP
1276 GQuark quark;
1277 struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj);
1278
2f42aa0a
PP
1279 if (!map_obj || !key) {
1280 BT_LOGW("Invalid parameter: value object or key is NULL: "
1281 "value-addr=%p, key-addr=%p", map_obj, key);
c55a9f58 1282 ret = BT_FALSE;
2f42aa0a
PP
1283 goto end;
1284 }
1285
1286 if (!bt_value_is_map(map_obj)) {
1287 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1288 "type=%d", map_obj, map_obj->type);
c55a9f58 1289 ret = BT_FALSE;
dac5c838
PP
1290 goto end;
1291 }
1292
1293 quark = g_quark_from_string(key);
5d5982ab 1294 ret = bt_g_hash_table_contains(typed_map_obj->ght,
dac5c838
PP
1295 GUINT_TO_POINTER(quark));
1296
1297end:
1298 return ret;
1299}
1300
1301enum bt_value_status bt_value_map_insert(struct bt_value *map_obj,
364747d6 1302 const char *key, struct bt_value *element_obj)
dac5c838
PP
1303{
1304 GQuark quark;
1305 enum bt_value_status ret = BT_VALUE_STATUS_OK;
1306 struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj);
1307
2f42aa0a
PP
1308 if (!map_obj || !key || !element_obj) {
1309 BT_LOGW("Invalid parameter: map value, key, or element value is NULL: "
1310 "map-value-addr=%p, key-addr=%p, element-value-addr=%p",
1311 map_obj, key, element_obj);
1312 ret = BT_VALUE_STATUS_INVAL;
1313 goto end;
1314 }
1315
1316 if (!bt_value_is_map(map_obj)) {
1317 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1318 "type=%d", map_obj, map_obj->type);
dac5c838
PP
1319 ret = BT_VALUE_STATUS_INVAL;
1320 goto end;
1321 }
1322
1323 if (map_obj->is_frozen) {
2f42aa0a
PP
1324 BT_LOGW("Invalid parameter: value is frozen: addr=%p",
1325 map_obj);
dac5c838
PP
1326 ret = BT_VALUE_STATUS_FROZEN;
1327 goto end;
1328 }
1329
1330 quark = g_quark_from_string(key);
1331 g_hash_table_insert(typed_map_obj->ght,
1332 GUINT_TO_POINTER(quark), element_obj);
83509119 1333 bt_get(element_obj);
2f42aa0a
PP
1334 BT_LOGV("Inserted value into map value: map-value-addr=%p, "
1335 "key=\"%s\", element-value-addr=%p",
1336 map_obj, key, element_obj);
dac5c838
PP
1337
1338end:
1339 return ret;
1340}
1341
1342enum bt_value_status bt_value_map_insert_bool(struct bt_value *map_obj,
c55a9f58 1343 const char *key, bt_bool val)
dac5c838
PP
1344{
1345 enum bt_value_status ret;
1346 struct bt_value *bool_obj = NULL;
1347
1348 bool_obj = bt_value_bool_create_init(val);
1349 ret = bt_value_map_insert(map_obj, key, bool_obj);
83509119 1350 bt_put(bool_obj);
dac5c838
PP
1351 return ret;
1352}
1353
1354enum bt_value_status bt_value_map_insert_integer(struct bt_value *map_obj,
364747d6 1355 const char *key, int64_t val)
dac5c838
PP
1356{
1357 enum bt_value_status ret;
1358 struct bt_value *integer_obj = NULL;
1359
1360 integer_obj = bt_value_integer_create_init(val);
1361 ret = bt_value_map_insert(map_obj, key, integer_obj);
83509119 1362 bt_put(integer_obj);
dac5c838
PP
1363 return ret;
1364}
1365
1366enum bt_value_status bt_value_map_insert_float(struct bt_value *map_obj,
364747d6 1367 const char *key, double val)
dac5c838
PP
1368{
1369 enum bt_value_status ret;
1370 struct bt_value *float_obj = NULL;
1371
1372 float_obj = bt_value_float_create_init(val);
1373 ret = bt_value_map_insert(map_obj, key, float_obj);
83509119 1374 bt_put(float_obj);
dac5c838
PP
1375 return ret;
1376}
1377
1378enum bt_value_status bt_value_map_insert_string(struct bt_value *map_obj,
364747d6 1379 const char *key, const char *val)
dac5c838
PP
1380{
1381 enum bt_value_status ret;
1382 struct bt_value *string_obj = NULL;
1383
1384 string_obj = bt_value_string_create_init(val);
1385 ret = bt_value_map_insert(map_obj, key, string_obj);
83509119 1386 bt_put(string_obj);
dac5c838
PP
1387 return ret;
1388}
1389
5b79e8bf 1390enum bt_value_status bt_value_map_insert_empty_array(struct bt_value *map_obj,
364747d6 1391 const char *key)
dac5c838
PP
1392{
1393 enum bt_value_status ret;
1394 struct bt_value *array_obj = NULL;
1395
1396 array_obj = bt_value_array_create();
1397 ret = bt_value_map_insert(map_obj, key, array_obj);
83509119 1398 bt_put(array_obj);
dac5c838
PP
1399 return ret;
1400}
1401
5b79e8bf 1402enum bt_value_status bt_value_map_insert_empty_map(struct bt_value *map_obj,
364747d6 1403 const char *key)
dac5c838
PP
1404{
1405 enum bt_value_status ret;
1406 struct bt_value *empty_map_obj = NULL;
1407
1408 empty_map_obj = bt_value_map_create();
1409 ret = bt_value_map_insert(map_obj, key, empty_map_obj);
83509119 1410 bt_put(empty_map_obj);
dac5c838
PP
1411 return ret;
1412}
1413
1414enum bt_value_status bt_value_map_foreach(const struct bt_value *map_obj,
364747d6 1415 bt_value_map_foreach_cb cb, void *data)
dac5c838
PP
1416{
1417 enum bt_value_status ret = BT_VALUE_STATUS_OK;
1418 gpointer key, element_obj;
1419 GHashTableIter iter;
1420 struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj);
1421
2f42aa0a
PP
1422 if (!map_obj || !cb) {
1423 BT_LOGW("Invalid parameter: map value or callback is NULL: "
1424 "value-addr=%p, cb-addr=%p", map_obj, cb);
1425 ret = BT_VALUE_STATUS_INVAL;
1426 goto end;
1427 }
1428
1429 if (!bt_value_is_map(map_obj)) {
1430 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1431 "type=%d", map_obj, map_obj->type);
dac5c838
PP
1432 ret = BT_VALUE_STATUS_INVAL;
1433 goto end;
1434 }
1435
1436 g_hash_table_iter_init(&iter, typed_map_obj->ght);
1437
1438 while (g_hash_table_iter_next(&iter, &key, &element_obj)) {
5b44aff2 1439 const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key));
dac5c838
PP
1440
1441 if (!cb(key_str, element_obj, data)) {
2f42aa0a
PP
1442 BT_LOGD("User cancelled the loop: key=\"%s\", "
1443 "value-addr=%p, data=%p",
1444 key_str, element_obj, data);
dac5c838
PP
1445 ret = BT_VALUE_STATUS_CANCELLED;
1446 break;
1447 }
1448 }
1449
1450end:
1451 return ret;
1452}
1453
770750d3
PP
1454struct extend_map_element_data {
1455 struct bt_value *extended_obj;
c55a9f58 1456 bt_bool got_error;
770750d3
PP
1457};
1458
1459static
c55a9f58 1460bt_bool extend_map_element(const char *key,
770750d3
PP
1461 struct bt_value *extension_obj_elem, void *data)
1462{
c55a9f58 1463 bt_bool ret = BT_TRUE;
770750d3
PP
1464
1465 struct extend_map_element_data *extend_data = data;
1466
1467 /* Copy object which is to replace the current one */
1468 struct bt_value *extension_obj_elem_copy =
1469 bt_value_copy(extension_obj_elem);
1470
1471 /* Replace in extended object */
1472 if (bt_value_map_insert(extend_data->extended_obj, key,
1473 extension_obj_elem_copy)) {
2f42aa0a
PP
1474 BT_LOGE("Cannot replace value in extended value: key=\"%s\", "
1475 "extended-value-addr=%p, element-value-addr=%p",
1476 key, extend_data->extended_obj,
1477 extension_obj_elem_copy);
770750d3
PP
1478 goto error;
1479 }
1480
1481 goto end;
1482
1483error:
c55a9f58
PP
1484 ret = BT_FALSE;
1485 extend_data->got_error = BT_TRUE;
770750d3
PP
1486
1487end:
1488 BT_PUT(extension_obj_elem_copy);
770750d3
PP
1489 return ret;
1490}
1491
1492struct bt_value *bt_value_map_extend(struct bt_value *base_map_obj,
1493 struct bt_value *extension_obj)
1494{
1495 struct bt_value *extended_obj = NULL;
1496 struct extend_map_element_data extend_data = { 0 };
1497
2f42aa0a
PP
1498 if (!base_map_obj || !extension_obj) {
1499 BT_LOGW("Invalid parameter: base value or extension value is NULL: "
1500 "base-value-addr=%p, extension-value-addr=%p",
1501 base_map_obj, extension_obj);
1502 goto error;
1503 }
1504
1505 if (!bt_value_is_map(base_map_obj)) {
1506 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1507 "type=%d", base_map_obj, base_map_obj->type);
770750d3
PP
1508 goto error;
1509 }
1510
2f42aa0a
PP
1511 if (!bt_value_is_map(extension_obj)) {
1512 BT_LOGW("Invalid parameter: value is not a map value: addr=%p, "
1513 "type=%d", extension_obj, extension_obj->type);
1514 goto error;
1515 }
1516
1517 BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p",
1518 base_map_obj, extension_obj);
1519
770750d3
PP
1520 /* Create copy of base map object to start with */
1521 extended_obj = bt_value_copy(base_map_obj);
1522 if (!extended_obj) {
2f42aa0a
PP
1523 BT_LOGE("Cannot copy base value: base-value-addr=%p",
1524 base_map_obj);
770750d3
PP
1525 goto error;
1526 }
1527
1528 /*
1529 * For each key in the extension map object, replace this key
1530 * in the copied map object.
1531 */
1532 extend_data.extended_obj = extended_obj;
1533
1534 if (bt_value_map_foreach(extension_obj, extend_map_element,
1535 &extend_data)) {
2f42aa0a
PP
1536 BT_LOGE("Cannot iterate on the extension object's elements: ",
1537 "extension-value-addr=%p", extension_obj);
770750d3
PP
1538 goto error;
1539 }
1540
1541 if (extend_data.got_error) {
2f42aa0a
PP
1542 BT_LOGE("Failed to successfully iterate on the extension object's elements: ",
1543 "extension-value-addr=%p", extension_obj);
770750d3
PP
1544 goto error;
1545 }
1546
2f42aa0a
PP
1547 BT_LOGD("Extended map value: extended-value-addr=%p",
1548 extended_obj);
770750d3
PP
1549 goto end;
1550
1551error:
1552 BT_PUT(extended_obj);
1553
1554end:
1555 return extended_obj;
1556}
1557
dac5c838
PP
1558struct bt_value *bt_value_copy(const struct bt_value *object)
1559{
1560 struct bt_value *copy_obj = NULL;
1561
1562 if (!object) {
2f42aa0a 1563 BT_LOGW_STR("Invalid parameter: value object is NULL.");
dac5c838
PP
1564 goto end;
1565 }
1566
2f42aa0a 1567 BT_LOGD("Copying value object: addr=%p", object);
dac5c838 1568 copy_obj = copy_funcs[object->type](object);
2f42aa0a
PP
1569 if (copy_obj) {
1570 BT_LOGD("Copied value object: copy-value-addr=%p",
1571 copy_obj);
1572 } else {
1573 BT_LOGE_STR("Failed to copy value object.");
1574 }
dac5c838
PP
1575
1576end:
1577 return copy_obj;
1578}
1579
c55a9f58 1580bt_bool bt_value_compare(const struct bt_value *object_a,
dac5c838
PP
1581 const struct bt_value *object_b)
1582{
c55a9f58 1583 bt_bool ret = BT_FALSE;
dac5c838
PP
1584
1585 if (!object_a || !object_b) {
2f42aa0a
PP
1586 BT_LOGW("Invalid parameter: value A or value B is NULL: "
1587 "value-a-addr=%p, value-b-addr=%p",
1588 object_a, object_b);
dac5c838
PP
1589 goto end;
1590 }
1591
1592 if (object_a->type != object_b->type) {
2f42aa0a
PP
1593 BT_LOGV("Values are different: type mismatch: "
1594 "value-a-addr=%p, value-b-addr=%p, "
1595 "value-a-type=%d, value-b-type=%d",
1596 object_a, object_b,
1597 object_a->type, object_b->type);
dac5c838
PP
1598 goto end;
1599 }
1600
1601 ret = compare_funcs[object_a->type](object_a, object_b);
1602
1603end:
1604 return ret;
1605}
This page took 0.097743 seconds and 4 git commands to generate.