lib: merge `assert-pre.h` and `assert-post.h` into `assert-cond.h`
[babeltrace.git] / src / lib / value.c
CommitLineData
dac5c838 1/*
0235b0db 2 * SPDX-License-Identifier: MIT
dac5c838 3 *
0235b0db 4 * Copyright (c) 2015-2018 Philippe Proulx <pproulx@efficios.com>
dac5c838
PP
5 */
6
350ad6c1 7#define BT_LOG_TAG "LIB/VALUE"
c2d9d9cf 8#include "lib/logging.h"
0f5e83e5 9
dac5c838
PP
10#include <stdlib.h>
11#include <string.h>
dac5c838 12#include <string.h>
2f42aa0a 13#include <inttypes.h>
4fa90f32
PP
14#include <babeltrace2/babeltrace.h>
15
578e048b
MJ
16#include "compat/compiler.h"
17#include "common/common.h"
578e048b 18#include "compat/glib.h"
d98421f2 19#include "lib/assert-cond.h"
578e048b
MJ
20#include "lib/value.h"
21#include "common/assert.h"
d24d5663 22#include "func-status.h"
2f42aa0a 23
dac5c838
PP
24#define BT_VALUE_TO_BOOL(_base) ((struct bt_value_bool *) (_base))
25#define BT_VALUE_TO_INTEGER(_base) ((struct bt_value_integer *) (_base))
a373bf69 26#define BT_VALUE_TO_REAL(_base) ((struct bt_value_real *) (_base))
dac5c838
PP
27#define BT_VALUE_TO_STRING(_base) ((struct bt_value_string *) (_base))
28#define BT_VALUE_TO_ARRAY(_base) ((struct bt_value_array *) (_base))
29#define BT_VALUE_TO_MAP(_base) ((struct bt_value_map *) (_base))
30
bdb288b3
PP
31#define _BT_ASSERT_PRE_VALUE_IS_TYPE_COND(_value, _type) \
32 (((struct bt_value *) (_value))->type == (_type))
f6ccaed9 33
bdb288b3
PP
34#define _BT_ASSERT_PRE_VALUE_IS_TYPE_FMT \
35 "Value has the wrong type ID: expected-type=%s, %![value-]+v"
f6ccaed9 36
bdb288b3
PP
37#define BT_ASSERT_PRE_VALUE_IS_TYPE(_value, _type) \
38 BT_ASSERT_PRE( \
39 _BT_ASSERT_PRE_VALUE_IS_TYPE_COND((_value), (_type)), \
40 _BT_ASSERT_PRE_VALUE_IS_TYPE_FMT, \
41 bt_common_value_type_string(_type), (_value))
42
43#define BT_ASSERT_PRE_DEV_VALUE_IS_TYPE(_value, _type) \
44 BT_ASSERT_PRE_DEV( \
45 _BT_ASSERT_PRE_VALUE_IS_TYPE_COND((_value), (_type)), \
46 _BT_ASSERT_PRE_VALUE_IS_TYPE_FMT, \
47 bt_common_value_type_string(_type), (_value))
48
49#define BT_ASSERT_PRE_DEV_VALUE_HOT(_value, _name) \
50 BT_ASSERT_PRE_DEV_HOT(((struct bt_value *) (_value)), (_name), \
51 ": %!+v", (_value))
f6ccaed9 52
3fea54f6
PP
53static
54void bt_value_null_instance_release_func(struct bt_object *obj)
55{
56 BT_LOGW("Releasing the null value singleton: addr=%p", obj);
57}
58
dac5c838
PP
59static
60struct bt_value bt_value_null_instance = {
f685129c 61 .base = {
312c056a 62 .is_shared = true,
3fea54f6
PP
63 .ref_count = 1,
64 .release_func = bt_value_null_instance_release_func,
65 .spec_release_func = NULL,
66 .parent_is_owner_listener_func = NULL,
67 .parent = NULL,
f685129c 68 },
dac5c838 69 .type = BT_VALUE_TYPE_NULL,
f6ccaed9 70 .frozen = BT_TRUE,
dac5c838
PP
71};
72
211796dc 73struct bt_value *const bt_value_null = &bt_value_null_instance;
dac5c838 74
dac5c838 75static
83509119 76void bt_value_destroy(struct bt_object *obj);
dac5c838
PP
77
78static
79void bt_value_string_destroy(struct bt_value *object)
80{
81 g_string_free(BT_VALUE_TO_STRING(object)->gstr, TRUE);
238b7404 82 BT_VALUE_TO_STRING(object)->gstr = NULL;
dac5c838
PP
83}
84
85static
86void bt_value_array_destroy(struct bt_value *object)
87{
88 /*
89 * Pointer array's registered value destructor will take care
90 * of putting each contained object.
91 */
92 g_ptr_array_free(BT_VALUE_TO_ARRAY(object)->garray, TRUE);
238b7404 93 BT_VALUE_TO_ARRAY(object)->garray = NULL;
dac5c838
PP
94}
95
96static
97void bt_value_map_destroy(struct bt_value *object)
98{
99 /*
100 * Hash table's registered value destructor will take care of
101 * putting each contained object. Keys are GQuarks and cannot
102 * be destroyed anyway.
103 */
104 g_hash_table_destroy(BT_VALUE_TO_MAP(object)->ght);
238b7404 105 BT_VALUE_TO_MAP(object)->ght = NULL;
dac5c838
PP
106}
107
108static
109void (* const destroy_funcs[])(struct bt_value *) = {
fdd3a2da
PP
110 [BT_VALUE_TYPE_NULL] = NULL,
111 [BT_VALUE_TYPE_BOOL] = NULL,
112 [BT_VALUE_TYPE_UNSIGNED_INTEGER] = NULL,
113 [BT_VALUE_TYPE_SIGNED_INTEGER] = NULL,
114 [BT_VALUE_TYPE_REAL] = NULL,
115 [BT_VALUE_TYPE_STRING] = bt_value_string_destroy,
116 [BT_VALUE_TYPE_ARRAY] = bt_value_array_destroy,
117 [BT_VALUE_TYPE_MAP] = bt_value_map_destroy,
dac5c838
PP
118};
119
120static
05e21286 121struct bt_value *bt_value_null_copy(const struct bt_value *null_obj)
dac5c838 122{
d164ff16
SM
123 BT_ASSERT(null_obj == bt_value_null);
124
6871026b 125 bt_object_get_ref_no_null_check(bt_value_null);
da91b29a 126 return (void *) bt_value_null;
dac5c838
PP
127}
128
129static
05e21286 130struct bt_value *bt_value_bool_copy(const struct bt_value *bool_obj)
dac5c838 131{
05e21286 132 return bt_value_bool_create_init(
601b0d3c 133 BT_VALUE_TO_BOOL(bool_obj)->value);
dac5c838
PP
134}
135
fdd3a2da
PP
136static inline
137struct bt_value *bt_value_integer_create_init(enum bt_value_type type,
138 uint64_t uval);
139
dac5c838 140static
05e21286 141struct bt_value *bt_value_integer_copy(
da91b29a 142 const struct bt_value *integer_obj)
dac5c838 143{
fdd3a2da
PP
144 return bt_value_integer_create_init(integer_obj->type,
145 BT_VALUE_TO_INTEGER(integer_obj)->value.u);
dac5c838
PP
146}
147
148static
05e21286 149struct bt_value *bt_value_real_copy(const struct bt_value *real_obj)
dac5c838 150{
05e21286 151 return bt_value_real_create_init(
a373bf69 152 BT_VALUE_TO_REAL(real_obj)->value);
dac5c838
PP
153}
154
155static
05e21286 156struct bt_value *bt_value_string_copy(const struct bt_value *string_obj)
dac5c838 157{
05e21286 158 return bt_value_string_create_init(
dac5c838
PP
159 BT_VALUE_TO_STRING(string_obj)->gstr->str);
160}
161
162static
05e21286 163struct bt_value *bt_value_array_copy(const struct bt_value *array_obj)
dac5c838
PP
164{
165 int i;
166 int ret;
05e21286 167 struct bt_value *copy_obj;
dac5c838
PP
168 struct bt_value_array *typed_array_obj;
169
2f42aa0a 170 BT_LOGD("Copying array value: addr=%p", array_obj);
dac5c838 171 typed_array_obj = BT_VALUE_TO_ARRAY(array_obj);
05e21286 172 copy_obj = bt_value_array_create();
dac5c838 173 if (!copy_obj) {
870631a2 174 BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty array value.");
dac5c838
PP
175 goto end;
176 }
177
178 for (i = 0; i < typed_array_obj->garray->len; ++i) {
05e21286
PP
179 struct bt_value *element_obj_copy = NULL;
180 const struct bt_value *element_obj =
181 bt_value_array_borrow_element_by_index_const(
601b0d3c 182 array_obj, i);
dac5c838 183
40b59ed9
PP
184 BT_LOGD("Copying array value's element: element-addr=%p, "
185 "index=%d", element_obj, i);
6be5a99e 186 ret = bt_value_copy(element_obj, &element_obj_copy);
601b0d3c 187 if (ret) {
870631a2
PP
188 BT_LIB_LOGE_APPEND_CAUSE(
189 "Cannot copy array value's element: "
2f42aa0a
PP
190 "array-addr=%p, index=%d",
191 array_obj, i);
65300d60 192 BT_OBJECT_PUT_REF_AND_RESET(copy_obj);
dac5c838
PP
193 goto end;
194 }
195
601b0d3c 196 BT_ASSERT(element_obj_copy);
05e21286 197 ret = bt_value_array_append_element(copy_obj,
da91b29a 198 (void *) element_obj_copy);
65300d60 199 BT_OBJECT_PUT_REF_AND_RESET(element_obj_copy);
dac5c838 200 if (ret) {
870631a2
PP
201 BT_LIB_LOGE_APPEND_CAUSE(
202 "Cannot append to array value: addr=%p",
2f42aa0a 203 array_obj);
65300d60 204 BT_OBJECT_PUT_REF_AND_RESET(copy_obj);
dac5c838
PP
205 goto end;
206 }
207 }
208
a704fb0b
PP
209 BT_LOGD("Copied array value: original-addr=%p, copy-addr=%p",
210 array_obj, copy_obj);
2f42aa0a 211
dac5c838
PP
212end:
213 return copy_obj;
214}
215
216static
05e21286 217struct bt_value *bt_value_map_copy(const struct bt_value *map_obj)
dac5c838
PP
218{
219 int ret;
220 GHashTableIter iter;
221 gpointer key, element_obj;
05e21286
PP
222 struct bt_value *copy_obj;
223 struct bt_value *element_obj_copy = NULL;
dac5c838
PP
224 struct bt_value_map *typed_map_obj;
225
2f42aa0a 226 BT_LOGD("Copying map value: addr=%p", map_obj);
dac5c838 227 typed_map_obj = BT_VALUE_TO_MAP(map_obj);
05e21286 228 copy_obj = bt_value_map_create();
dac5c838
PP
229 if (!copy_obj) {
230 goto end;
231 }
232
233 g_hash_table_iter_init(&iter, typed_map_obj->ght);
234
235 while (g_hash_table_iter_next(&iter, &key, &element_obj)) {
5b44aff2 236 const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key));
dac5c838 237
f6ccaed9 238 BT_ASSERT(key_str);
40b59ed9
PP
239 BT_LOGD("Copying map value's element: element-addr=%p, "
240 "key=\"%s\"", element_obj, key_str);
6be5a99e 241 ret = bt_value_copy(element_obj, &element_obj_copy);
601b0d3c 242 if (ret) {
870631a2
PP
243 BT_LIB_LOGE_APPEND_CAUSE(
244 "Cannot copy map value's element: "
2f42aa0a
PP
245 "map-addr=%p, key=\"%s\"",
246 map_obj, key_str);
65300d60 247 BT_OBJECT_PUT_REF_AND_RESET(copy_obj);
dac5c838
PP
248 goto end;
249 }
250
601b0d3c 251 BT_ASSERT(element_obj_copy);
05e21286 252 ret = bt_value_map_insert_entry(copy_obj, key_str,
da91b29a 253 (void *) element_obj_copy);
65300d60 254 BT_OBJECT_PUT_REF_AND_RESET(element_obj_copy);
dac5c838 255 if (ret) {
870631a2
PP
256 BT_LIB_LOGE_APPEND_CAUSE(
257 "Cannot insert into map value: addr=%p, key=\"%s\"",
2f42aa0a 258 map_obj, key_str);
65300d60 259 BT_OBJECT_PUT_REF_AND_RESET(copy_obj);
dac5c838
PP
260 goto end;
261 }
262 }
263
2f42aa0a
PP
264 BT_LOGD("Copied map value: addr=%p", map_obj);
265
dac5c838
PP
266end:
267 return copy_obj;
268}
269
270static
05e21286 271struct bt_value *(* const copy_funcs[])(const struct bt_value *) = {
fdd3a2da
PP
272 [BT_VALUE_TYPE_NULL] = bt_value_null_copy,
273 [BT_VALUE_TYPE_BOOL] = bt_value_bool_copy,
274 [BT_VALUE_TYPE_UNSIGNED_INTEGER] = bt_value_integer_copy,
275 [BT_VALUE_TYPE_SIGNED_INTEGER] = bt_value_integer_copy,
276 [BT_VALUE_TYPE_REAL] = bt_value_real_copy,
277 [BT_VALUE_TYPE_STRING] = bt_value_string_copy,
278 [BT_VALUE_TYPE_ARRAY] = bt_value_array_copy,
279 [BT_VALUE_TYPE_MAP] = bt_value_map_copy,
dac5c838
PP
280};
281
282static
cd933d89 283bt_bool bt_value_null_is_equal(const struct bt_value *object_a,
dac5c838
PP
284 const struct bt_value *object_b)
285{
286 /*
cd933d89 287 * Always BT_TRUE since bt_value_is_equal() already checks if both
dac5c838
PP
288 * object_a and object_b have the same type, and in the case of
289 * null value objects, they're always the same if it is so.
290 */
c55a9f58 291 return BT_TRUE;
dac5c838
PP
292}
293
294static
cd933d89 295bt_bool bt_value_bool_is_equal(const struct bt_value *object_a,
dac5c838
PP
296 const struct bt_value *object_b)
297{
40b59ed9
PP
298 if (BT_VALUE_TO_BOOL(object_a)->value !=
299 BT_VALUE_TO_BOOL(object_b)->value) {
ef267d12 300 BT_LOGT("Boolean value objects are different: "
40b59ed9
PP
301 "bool-a-val=%d, bool-b-val=%d",
302 BT_VALUE_TO_BOOL(object_a)->value,
303 BT_VALUE_TO_BOOL(object_b)->value);
304 return BT_FALSE;
305 }
306
307 return BT_TRUE;
dac5c838
PP
308}
309
310static
cd933d89 311bt_bool bt_value_integer_is_equal(const struct bt_value *object_a,
dac5c838
PP
312 const struct bt_value *object_b)
313{
fdd3a2da
PP
314 if (BT_VALUE_TO_INTEGER(object_a)->value.u !=
315 BT_VALUE_TO_INTEGER(object_b)->value.u) {
316 if (object_a->type == BT_VALUE_TYPE_UNSIGNED_INTEGER) {
ef267d12 317 BT_LOGT("Unsigned integer value objects are different: "
fdd3a2da
PP
318 "int-a-val=%" PRIu64 ", int-b-val=%" PRIu64,
319 BT_VALUE_TO_INTEGER(object_a)->value.u,
320 BT_VALUE_TO_INTEGER(object_b)->value.u);
321 } else {
ef267d12 322 BT_LOGT("Signed integer value objects are different: "
fdd3a2da
PP
323 "int-a-val=%" PRId64 ", int-b-val=%" PRId64,
324 BT_VALUE_TO_INTEGER(object_a)->value.i,
325 BT_VALUE_TO_INTEGER(object_b)->value.i);
326 }
327
40b59ed9
PP
328 return BT_FALSE;
329 }
330
331 return BT_TRUE;
dac5c838
PP
332}
333
334static
cd933d89 335bt_bool bt_value_real_is_equal(const struct bt_value *object_a,
dac5c838
PP
336 const struct bt_value *object_b)
337{
a373bf69
PP
338 if (BT_VALUE_TO_REAL(object_a)->value !=
339 BT_VALUE_TO_REAL(object_b)->value) {
ef267d12 340 BT_LOGT("Real number value objects are different: "
a373bf69
PP
341 "real-a-val=%f, real-b-val=%f",
342 BT_VALUE_TO_REAL(object_a)->value,
343 BT_VALUE_TO_REAL(object_b)->value);
40b59ed9
PP
344 return BT_FALSE;
345 }
346
347 return BT_TRUE;
dac5c838
PP
348}
349
350static
cd933d89 351bt_bool bt_value_string_is_equal(const struct bt_value *object_a,
dac5c838
PP
352 const struct bt_value *object_b)
353{
40b59ed9
PP
354 if (strcmp(BT_VALUE_TO_STRING(object_a)->gstr->str,
355 BT_VALUE_TO_STRING(object_b)->gstr->str) != 0) {
ef267d12 356 BT_LOGT("String value objects are different: "
40b59ed9
PP
357 "string-a-val=\"%s\", string-b-val=\"%s\"",
358 BT_VALUE_TO_STRING(object_a)->gstr->str,
359 BT_VALUE_TO_STRING(object_b)->gstr->str);
360 return BT_FALSE;
361 }
362
363 return BT_TRUE;
dac5c838
PP
364}
365
366static
cd933d89 367bt_bool bt_value_array_is_equal(const struct bt_value *object_a,
dac5c838
PP
368 const struct bt_value *object_b)
369{
370 int i;
c55a9f58 371 bt_bool ret = BT_TRUE;
dac5c838
PP
372 const struct bt_value_array *array_obj_a =
373 BT_VALUE_TO_ARRAY(object_a);
374
393729a6
PP
375 if (bt_value_array_get_length(object_a) !=
376 bt_value_array_get_length(object_b)) {
ef267d12 377 BT_LOGT("Array values are different: size mismatch "
2f42aa0a
PP
378 "value-a-addr=%p, value-b-addr=%p, "
379 "value-a-size=%" PRId64 ", value-b-size=%" PRId64,
380 object_a, object_b,
393729a6
PP
381 bt_value_array_get_length(object_a),
382 bt_value_array_get_length(object_b));
c55a9f58 383 ret = BT_FALSE;
dac5c838
PP
384 goto end;
385 }
386
387 for (i = 0; i < array_obj_a->garray->len; ++i) {
05e21286
PP
388 const struct bt_value *element_obj_a;
389 const struct bt_value *element_obj_b;
dac5c838 390
05e21286 391 element_obj_a = bt_value_array_borrow_element_by_index_const(
da91b29a 392 object_a, i);
05e21286 393 element_obj_b = bt_value_array_borrow_element_by_index_const(
da91b29a 394 object_b, i);
dac5c838 395
cd933d89 396 if (!bt_value_is_equal(element_obj_a, element_obj_b)) {
ef267d12 397 BT_LOGT("Array values's elements are different: "
2f42aa0a 398 "value-a-addr=%p, value-b-addr=%p, index=%d",
32e87ceb 399 element_obj_a, element_obj_b, i);
c55a9f58 400 ret = BT_FALSE;
dac5c838
PP
401 goto end;
402 }
dac5c838
PP
403 }
404
405end:
406 return ret;
407}
408
409static
cd933d89 410bt_bool bt_value_map_is_equal(const struct bt_value *object_a,
dac5c838
PP
411 const struct bt_value *object_b)
412{
c55a9f58 413 bt_bool ret = BT_TRUE;
dac5c838
PP
414 GHashTableIter iter;
415 gpointer key, element_obj_a;
416 const struct bt_value_map *map_obj_a = BT_VALUE_TO_MAP(object_a);
417
601b0d3c
PP
418 if (bt_value_map_get_size(object_a) !=
419 bt_value_map_get_size(object_b)) {
ef267d12 420 BT_LOGT("Map values are different: size mismatch "
2f42aa0a
PP
421 "value-a-addr=%p, value-b-addr=%p, "
422 "value-a-size=%" PRId64 ", value-b-size=%" PRId64,
423 object_a, object_b,
07208d85
PP
424 bt_value_map_get_size(object_a),
425 bt_value_map_get_size(object_b));
c55a9f58 426 ret = BT_FALSE;
dac5c838
PP
427 goto end;
428 }
429
430 g_hash_table_iter_init(&iter, map_obj_a->ght);
431
432 while (g_hash_table_iter_next(&iter, &key, &element_obj_a)) {
05e21286 433 const struct bt_value *element_obj_b;
5b44aff2 434 const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key));
dac5c838 435
05e21286 436 element_obj_b = bt_value_map_borrow_entry_value_const(object_b,
da91b29a 437 key_str);
dac5c838 438
cd933d89 439 if (!bt_value_is_equal(element_obj_a, element_obj_b)) {
ef267d12 440 BT_LOGT("Map values's elements are different: "
2f42aa0a
PP
441 "value-a-addr=%p, value-b-addr=%p, key=\"%s\"",
442 element_obj_a, element_obj_b, key_str);
c55a9f58 443 ret = BT_FALSE;
dac5c838
PP
444 goto end;
445 }
dac5c838
PP
446 }
447
448end:
449 return ret;
450}
451
452static
cd933d89 453bt_bool (* const is_equal_funcs[])(const struct bt_value *,
dac5c838 454 const struct bt_value *) = {
cd933d89
FD
455 [BT_VALUE_TYPE_NULL] = bt_value_null_is_equal,
456 [BT_VALUE_TYPE_BOOL] = bt_value_bool_is_equal,
457 [BT_VALUE_TYPE_UNSIGNED_INTEGER] = bt_value_integer_is_equal,
458 [BT_VALUE_TYPE_SIGNED_INTEGER] = bt_value_integer_is_equal,
459 [BT_VALUE_TYPE_REAL] = bt_value_real_is_equal,
460 [BT_VALUE_TYPE_STRING] = bt_value_string_is_equal,
461 [BT_VALUE_TYPE_ARRAY] = bt_value_array_is_equal,
462 [BT_VALUE_TYPE_MAP] = bt_value_map_is_equal,
dac5c838
PP
463};
464
f6ccaed9 465static
dac5c838
PP
466void bt_value_null_freeze(struct bt_value *object)
467{
468}
469
f6ccaed9 470static
dac5c838
PP
471void bt_value_generic_freeze(struct bt_value *object)
472{
f6ccaed9 473 object->frozen = BT_TRUE;
dac5c838
PP
474}
475
f6ccaed9 476static
dac5c838
PP
477void bt_value_array_freeze(struct bt_value *object)
478{
479 int i;
480 struct bt_value_array *typed_array_obj =
481 BT_VALUE_TO_ARRAY(object);
482
483 for (i = 0; i < typed_array_obj->garray->len; ++i) {
f6ccaed9 484 bt_value_freeze(g_ptr_array_index(typed_array_obj->garray, i));
dac5c838
PP
485 }
486
487 bt_value_generic_freeze(object);
488}
489
f6ccaed9 490static
dac5c838
PP
491void bt_value_map_freeze(struct bt_value *object)
492{
493 GHashTableIter iter;
494 gpointer key, element_obj;
495 const struct bt_value_map *map_obj = BT_VALUE_TO_MAP(object);
496
497 g_hash_table_iter_init(&iter, map_obj->ght);
498
499 while (g_hash_table_iter_next(&iter, &key, &element_obj)) {
500 bt_value_freeze(element_obj);
501 }
502
503 bt_value_generic_freeze(object);
504}
505
506static
507void (* const freeze_funcs[])(struct bt_value *) = {
fdd3a2da
PP
508 [BT_VALUE_TYPE_NULL] = bt_value_null_freeze,
509 [BT_VALUE_TYPE_BOOL] = bt_value_generic_freeze,
510 [BT_VALUE_TYPE_UNSIGNED_INTEGER] = bt_value_generic_freeze,
511 [BT_VALUE_TYPE_SIGNED_INTEGER] = bt_value_generic_freeze,
512 [BT_VALUE_TYPE_REAL] = bt_value_generic_freeze,
513 [BT_VALUE_TYPE_STRING] = bt_value_generic_freeze,
514 [BT_VALUE_TYPE_ARRAY] = bt_value_array_freeze,
515 [BT_VALUE_TYPE_MAP] = bt_value_map_freeze,
dac5c838
PP
516};
517
518static
83509119 519void bt_value_destroy(struct bt_object *obj)
dac5c838 520{
83509119 521 struct bt_value *value;
dac5c838 522
83509119 523 value = container_of(obj, struct bt_value, base);
2f42aa0a
PP
524 BT_LOGD("Destroying value: addr=%p", value);
525
83509119 526 if (bt_value_is_null(value)) {
2f42aa0a 527 BT_LOGD_STR("Not destroying the null value singleton.");
dac5c838
PP
528 return;
529 }
530
83509119
JG
531 if (destroy_funcs[value->type]) {
532 destroy_funcs[value->type](value);
dac5c838
PP
533 }
534
83509119 535 g_free(value);
dac5c838
PP
536}
537
f6ccaed9 538BT_HIDDEN
d24d5663 539void _bt_value_freeze(const struct bt_value *c_object)
dac5c838 540{
05e21286 541 const struct bt_value *object = (void *) c_object;
dac5c838 542
f6ccaed9 543 BT_ASSERT(object);
dac5c838 544
f6ccaed9 545 if (object->frozen) {
2f42aa0a
PP
546 goto end;
547 }
548
549 BT_LOGD("Freezing value: addr=%p", object);
05e21286 550 freeze_funcs[object->type]((void *) object);
dac5c838
PP
551
552end:
d24d5663 553 return;
dac5c838
PP
554}
555
dac5c838
PP
556enum bt_value_type bt_value_get_type(const struct bt_value *object)
557{
bdb288b3 558 BT_ASSERT_PRE_DEV_NON_NULL(object, "Value object");
dac5c838
PP
559 return object->type;
560}
561
562static
563struct bt_value bt_value_create_base(enum bt_value_type type)
564{
3fea54f6 565 struct bt_value value;
dac5c838 566
3fea54f6
PP
567 value.type = type;
568 value.frozen = BT_FALSE;
569 bt_object_init_shared(&value.base, bt_value_destroy);
570 return value;
dac5c838
PP
571}
572
05e21286 573struct bt_value *bt_value_bool_create_init(bt_bool val)
dac5c838
PP
574{
575 struct bt_value_bool *bool_obj;
576
17f3083a
SM
577 BT_ASSERT_PRE_NO_ERROR();
578
2f42aa0a 579 BT_LOGD("Creating boolean value object: val=%d", val);
dac5c838 580 bool_obj = g_new0(struct bt_value_bool, 1);
dac5c838 581 if (!bool_obj) {
870631a2
PP
582 BT_LIB_LOGE_APPEND_CAUSE(
583 "Failed to allocate one boolean value object.");
dac5c838
PP
584 goto end;
585 }
586
587 bool_obj->base = bt_value_create_base(BT_VALUE_TYPE_BOOL);
588 bool_obj->value = val;
2f42aa0a 589 BT_LOGD("Created boolean value object: addr=%p", bool_obj);
dac5c838
PP
590
591end:
11cd69be 592 return (void *) bool_obj;
dac5c838
PP
593}
594
05e21286 595struct bt_value *bt_value_bool_create(void)
dac5c838 596{
17f3083a
SM
597 BT_ASSERT_PRE_NO_ERROR();
598
05e21286 599 return bt_value_bool_create_init(BT_FALSE);
dac5c838
PP
600}
601
fdd3a2da
PP
602static inline
603struct bt_value *bt_value_integer_create_init(enum bt_value_type type,
604 uint64_t uval)
dac5c838
PP
605{
606 struct bt_value_integer *integer_obj;
607
fdd3a2da
PP
608 BT_ASSERT(type == BT_VALUE_TYPE_UNSIGNED_INTEGER ||
609 type == BT_VALUE_TYPE_SIGNED_INTEGER);
610
611 if (type == BT_VALUE_TYPE_UNSIGNED_INTEGER) {
612 BT_LOGD("Creating unsigned integer value object: val=%" PRIu64,
613 uval);
614 } else {
615 BT_LOGD("Creating signed integer value object: val=%" PRId64,
616 (int64_t) uval);
617 }
618
dac5c838 619 integer_obj = g_new0(struct bt_value_integer, 1);
dac5c838 620 if (!integer_obj) {
870631a2
PP
621 BT_LIB_LOGE_APPEND_CAUSE(
622 "Failed to allocate one integer value object.");
dac5c838
PP
623 goto end;
624 }
625
fdd3a2da
PP
626 integer_obj->base = bt_value_create_base(type);
627 integer_obj->value.u = uval;
628 BT_LOGD("Created %ssigned integer value object: addr=%p",
629 type == BT_VALUE_TYPE_UNSIGNED_INTEGER ? "un" : "",
2f42aa0a 630 integer_obj);
dac5c838
PP
631
632end:
11cd69be 633 return (void *) integer_obj;
dac5c838
PP
634}
635
9c08c816 636struct bt_value *bt_value_integer_unsigned_create_init(uint64_t val)
fdd3a2da 637{
17f3083a
SM
638 BT_ASSERT_PRE_NO_ERROR();
639
fdd3a2da
PP
640 return bt_value_integer_create_init(BT_VALUE_TYPE_UNSIGNED_INTEGER,
641 val);
642}
643
9c08c816 644struct bt_value *bt_value_integer_unsigned_create(void)
fdd3a2da 645{
17f3083a
SM
646 BT_ASSERT_PRE_NO_ERROR();
647
9c08c816 648 return bt_value_integer_unsigned_create_init(0);
fdd3a2da
PP
649}
650
9c08c816 651struct bt_value *bt_value_integer_signed_create_init(int64_t val)
dac5c838 652{
17f3083a
SM
653 BT_ASSERT_PRE_NO_ERROR();
654
fdd3a2da
PP
655 return bt_value_integer_create_init(BT_VALUE_TYPE_SIGNED_INTEGER,
656 (uint64_t) val);
657}
658
9c08c816 659struct bt_value *bt_value_integer_signed_create(void)
fdd3a2da 660{
17f3083a
SM
661 BT_ASSERT_PRE_NO_ERROR();
662
9c08c816 663 return bt_value_integer_signed_create_init(0);
dac5c838
PP
664}
665
05e21286 666struct bt_value *bt_value_real_create_init(double val)
dac5c838 667{
a373bf69 668 struct bt_value_real *real_obj;
dac5c838 669
17f3083a
SM
670 BT_ASSERT_PRE_NO_ERROR();
671
a373bf69
PP
672 BT_LOGD("Creating real number value object: val=%f", val);
673 real_obj = g_new0(struct bt_value_real, 1);
674 if (!real_obj) {
870631a2
PP
675 BT_LIB_LOGE_APPEND_CAUSE(
676 "Failed to allocate one real number value object.");
dac5c838
PP
677 goto end;
678 }
679
a373bf69
PP
680 real_obj->base = bt_value_create_base(BT_VALUE_TYPE_REAL);
681 real_obj->value = val;
682 BT_LOGD("Created real number value object: addr=%p",
683 real_obj);
dac5c838
PP
684
685end:
11cd69be 686 return (void *) real_obj;
dac5c838
PP
687}
688
05e21286 689struct bt_value *bt_value_real_create(void)
dac5c838 690{
17f3083a
SM
691 BT_ASSERT_PRE_NO_ERROR();
692
05e21286 693 return bt_value_real_create_init(0.);
dac5c838
PP
694}
695
05e21286 696struct bt_value *bt_value_string_create_init(const char *val)
dac5c838
PP
697{
698 struct bt_value_string *string_obj = NULL;
699
17f3083a 700 BT_ASSERT_PRE_NO_ERROR();
870631a2 701 BT_ASSERT_PRE_NON_NULL(val, "Value");
17f3083a 702
003c7749 703 BT_LOGD("Creating string value object: val-len=%zu", strlen(val));
dac5c838 704 string_obj = g_new0(struct bt_value_string, 1);
dac5c838 705 if (!string_obj) {
870631a2
PP
706 BT_LIB_LOGE_APPEND_CAUSE(
707 "Failed to allocate one string object.");
dac5c838
PP
708 goto end;
709 }
710
711 string_obj->base = bt_value_create_base(BT_VALUE_TYPE_STRING);
712 string_obj->gstr = g_string_new(val);
dac5c838 713 if (!string_obj->gstr) {
870631a2
PP
714 BT_LIB_LOGE_APPEND_CAUSE(
715 "Failed to allocate a GString.");
dac5c838
PP
716 g_free(string_obj);
717 string_obj = NULL;
718 goto end;
719 }
720
2f42aa0a
PP
721 BT_LOGD("Created string value object: addr=%p",
722 string_obj);
723
dac5c838 724end:
11cd69be 725 return (void *) string_obj;
dac5c838
PP
726}
727
05e21286 728struct bt_value *bt_value_string_create(void)
dac5c838 729{
17f3083a
SM
730 BT_ASSERT_PRE_NO_ERROR();
731
05e21286 732 return bt_value_string_create_init("");
dac5c838
PP
733}
734
05e21286 735struct bt_value *bt_value_array_create(void)
dac5c838
PP
736{
737 struct bt_value_array *array_obj;
738
17f3083a
SM
739 BT_ASSERT_PRE_NO_ERROR();
740
2f42aa0a 741 BT_LOGD_STR("Creating empty array value object.");
dac5c838 742 array_obj = g_new0(struct bt_value_array, 1);
dac5c838 743 if (!array_obj) {
870631a2
PP
744 BT_LIB_LOGE_APPEND_CAUSE(
745 "Failed to allocate one array object.");
dac5c838
PP
746 goto end;
747 }
748
749 array_obj->base = bt_value_create_base(BT_VALUE_TYPE_ARRAY);
5d5982ab 750 array_obj->garray = bt_g_ptr_array_new_full(0,
65300d60 751 (GDestroyNotify) bt_object_put_ref);
dac5c838 752 if (!array_obj->garray) {
870631a2 753 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
dac5c838
PP
754 g_free(array_obj);
755 array_obj = NULL;
756 goto end;
757 }
758
2f42aa0a
PP
759 BT_LOGD("Created array value object: addr=%p",
760 array_obj);
761
dac5c838 762end:
11cd69be 763 return (void *) array_obj;
dac5c838
PP
764}
765
05e21286 766struct bt_value *bt_value_map_create(void)
dac5c838
PP
767{
768 struct bt_value_map *map_obj;
769
17f3083a
SM
770 BT_ASSERT_PRE_NO_ERROR();
771
2f42aa0a 772 BT_LOGD_STR("Creating empty map value object.");
dac5c838 773 map_obj = g_new0(struct bt_value_map, 1);
dac5c838 774 if (!map_obj) {
870631a2 775 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one map object.");
dac5c838
PP
776 goto end;
777 }
778
779 map_obj->base = bt_value_create_base(BT_VALUE_TYPE_MAP);
780 map_obj->ght = g_hash_table_new_full(g_direct_hash, g_direct_equal,
65300d60 781 NULL, (GDestroyNotify) bt_object_put_ref);
dac5c838 782 if (!map_obj->ght) {
870631a2 783 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GHashTable.");
dac5c838
PP
784 g_free(map_obj);
785 map_obj = NULL;
786 goto end;
787 }
788
2f42aa0a
PP
789 BT_LOGD("Created map value object: addr=%p",
790 map_obj);
791
dac5c838 792end:
11cd69be 793 return (void *) map_obj;
dac5c838
PP
794}
795
601b0d3c 796bt_bool bt_value_bool_get(const struct bt_value *bool_obj)
dac5c838 797{
bdb288b3
PP
798 BT_ASSERT_PRE_DEV_NON_NULL(bool_obj, "Value object");
799 BT_ASSERT_PRE_DEV_VALUE_IS_TYPE(bool_obj, BT_VALUE_TYPE_BOOL);
601b0d3c 800 return BT_VALUE_TO_BOOL(bool_obj)->value;
dac5c838
PP
801}
802
05e21286 803void bt_value_bool_set(struct bt_value *bool_obj, bt_bool val)
dac5c838 804{
f6ccaed9
PP
805 BT_ASSERT_PRE_NON_NULL(bool_obj, "Value object");
806 BT_ASSERT_PRE_VALUE_IS_TYPE(bool_obj, BT_VALUE_TYPE_BOOL);
bdb288b3 807 BT_ASSERT_PRE_DEV_VALUE_HOT(bool_obj, "Value object");
f6ccaed9 808 BT_VALUE_TO_BOOL(bool_obj)->value = val;
ef267d12 809 BT_LOGT("Set boolean value's raw value: value-addr=%p, value=%d",
2f42aa0a 810 bool_obj, val);
dac5c838
PP
811}
812
9c08c816 813uint64_t bt_value_integer_unsigned_get(const struct bt_value *integer_obj)
fdd3a2da 814{
bdb288b3
PP
815 BT_ASSERT_PRE_DEV_NON_NULL(integer_obj, "Value object");
816 BT_ASSERT_PRE_DEV_VALUE_IS_TYPE(integer_obj,
fdd3a2da
PP
817 BT_VALUE_TYPE_UNSIGNED_INTEGER);
818 return BT_VALUE_TO_INTEGER(integer_obj)->value.u;
819}
820
9c08c816 821int64_t bt_value_integer_signed_get(const struct bt_value *integer_obj)
dac5c838 822{
bdb288b3
PP
823 BT_ASSERT_PRE_DEV_NON_NULL(integer_obj, "Value object");
824 BT_ASSERT_PRE_DEV_VALUE_IS_TYPE(integer_obj,
fdd3a2da
PP
825 BT_VALUE_TYPE_SIGNED_INTEGER);
826 return BT_VALUE_TO_INTEGER(integer_obj)->value.i;
dac5c838
PP
827}
828
fdd3a2da 829static inline
05e21286 830void bt_value_integer_set(struct bt_value *integer_obj,
fdd3a2da 831 enum bt_value_type expected_type, uint64_t uval)
dac5c838 832{
f6ccaed9 833 BT_ASSERT_PRE_NON_NULL(integer_obj, "Value object");
fdd3a2da 834 BT_ASSERT_PRE_VALUE_IS_TYPE(integer_obj, expected_type);
bdb288b3 835 BT_ASSERT_PRE_DEV_VALUE_HOT(integer_obj, "Value object");
fdd3a2da
PP
836 BT_VALUE_TO_INTEGER(integer_obj)->value.u = uval;
837}
838
9c08c816 839void bt_value_integer_unsigned_set(struct bt_value *integer_obj,
fdd3a2da
PP
840 uint64_t val)
841{
842 bt_value_integer_set(integer_obj, BT_VALUE_TYPE_UNSIGNED_INTEGER, val);
ef267d12 843 BT_LOGT("Set unsigned integer value's raw value: "
fdd3a2da
PP
844 "value-addr=%p, value=%" PRIu64, integer_obj, val);
845}
846
9c08c816 847void bt_value_integer_signed_set(struct bt_value *integer_obj,
fdd3a2da
PP
848 int64_t val)
849{
850 bt_value_integer_set(integer_obj, BT_VALUE_TYPE_SIGNED_INTEGER,
851 (uint64_t) val);
ef267d12 852 BT_LOGT("Set signed integer value's raw value: "
fdd3a2da 853 "value-addr=%p, value=%" PRId64, integer_obj, val);
dac5c838
PP
854}
855
601b0d3c 856double bt_value_real_get(const struct bt_value *real_obj)
dac5c838 857{
bdb288b3
PP
858 BT_ASSERT_PRE_DEV_NON_NULL(real_obj, "Value object");
859 BT_ASSERT_PRE_DEV_VALUE_IS_TYPE(real_obj, BT_VALUE_TYPE_REAL);
601b0d3c 860 return BT_VALUE_TO_REAL(real_obj)->value;
dac5c838
PP
861}
862
05e21286 863void bt_value_real_set(struct bt_value *real_obj, double val)
dac5c838 864{
a373bf69
PP
865 BT_ASSERT_PRE_NON_NULL(real_obj, "Value object");
866 BT_ASSERT_PRE_VALUE_IS_TYPE(real_obj, BT_VALUE_TYPE_REAL);
bdb288b3 867 BT_ASSERT_PRE_DEV_VALUE_HOT(real_obj, "Value object");
a373bf69 868 BT_VALUE_TO_REAL(real_obj)->value = val;
ef267d12 869 BT_LOGT("Set real number value's raw value: value-addr=%p, value=%f",
a373bf69 870 real_obj, val);
dac5c838
PP
871}
872
601b0d3c 873const char *bt_value_string_get(const struct bt_value *string_obj)
dac5c838 874{
bdb288b3
PP
875 BT_ASSERT_PRE_DEV_NON_NULL(string_obj, "Value object");
876 BT_ASSERT_PRE_DEV_VALUE_IS_TYPE(string_obj, BT_VALUE_TYPE_STRING);
601b0d3c 877 return BT_VALUE_TO_STRING(string_obj)->gstr->str;
dac5c838
PP
878}
879
d24d5663 880enum bt_value_string_set_status bt_value_string_set(
05e21286 881 struct bt_value *string_obj, const char *val)
dac5c838 882{
17f3083a 883 BT_ASSERT_PRE_NO_ERROR();
f6ccaed9 884 BT_ASSERT_PRE_NON_NULL(string_obj, "Value object");
f6ccaed9 885 BT_ASSERT_PRE_VALUE_IS_TYPE(string_obj, BT_VALUE_TYPE_STRING);
bdb288b3 886 BT_ASSERT_PRE_DEV_VALUE_HOT(string_obj, "Value object");
f6ccaed9 887 g_string_assign(BT_VALUE_TO_STRING(string_obj)->gstr, val);
ef267d12 888 BT_LOGT("Set string value's raw value: value-addr=%p, raw-value-addr=%p",
2f42aa0a 889 string_obj, val);
d24d5663 890 return BT_FUNC_STATUS_OK;
dac5c838
PP
891}
892
393729a6 893uint64_t bt_value_array_get_length(const struct bt_value *array_obj)
dac5c838 894{
bdb288b3
PP
895 BT_ASSERT_PRE_DEV_NON_NULL(array_obj, "Value object");
896 BT_ASSERT_PRE_DEV_VALUE_IS_TYPE(array_obj, BT_VALUE_TYPE_ARRAY);
601b0d3c 897 return (uint64_t) BT_VALUE_TO_ARRAY(array_obj)->garray->len;
dac5c838
PP
898}
899
da91b29a 900struct bt_value *bt_value_array_borrow_element_by_index(
05e21286 901 struct bt_value *array_obj, uint64_t index)
dac5c838 902{
dac5c838
PP
903 struct bt_value_array *typed_array_obj =
904 BT_VALUE_TO_ARRAY(array_obj);
905
bdb288b3
PP
906 BT_ASSERT_PRE_DEV_NON_NULL(array_obj, "Value object");
907 BT_ASSERT_PRE_DEV_VALUE_IS_TYPE(array_obj, BT_VALUE_TYPE_ARRAY);
908 BT_ASSERT_PRE_DEV_VALID_INDEX(index, typed_array_obj->garray->len);
094ff7c0 909 return g_ptr_array_index(typed_array_obj->garray, index);
dac5c838
PP
910}
911
05e21286
PP
912const struct bt_value *bt_value_array_borrow_element_by_index_const(
913 const struct bt_value *array_obj,
da91b29a
PP
914 uint64_t index)
915{
05e21286 916 return bt_value_array_borrow_element_by_index(
da91b29a
PP
917 (void *) array_obj, index);
918}
919
d24d5663 920enum bt_value_array_append_element_status bt_value_array_append_element(
05e21286 921 struct bt_value *array_obj,
364747d6 922 struct bt_value *element_obj)
dac5c838 923{
dac5c838
PP
924 struct bt_value_array *typed_array_obj =
925 BT_VALUE_TO_ARRAY(array_obj);
926
17f3083a 927 BT_ASSERT_PRE_NO_ERROR();
f6ccaed9
PP
928 BT_ASSERT_PRE_NON_NULL(array_obj, "Array value object");
929 BT_ASSERT_PRE_NON_NULL(element_obj, "Element value object");
930 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj, BT_VALUE_TYPE_ARRAY);
bdb288b3 931 BT_ASSERT_PRE_DEV_VALUE_HOT(array_obj, "Array value object");
dac5c838 932 g_ptr_array_add(typed_array_obj->garray, element_obj);
65300d60 933 bt_object_get_ref(element_obj);
ef267d12 934 BT_LOGT("Appended element to array value: array-value-addr=%p, "
2f42aa0a
PP
935 "element-value-addr=%p, new-size=%u",
936 array_obj, element_obj, typed_array_obj->garray->len);
d24d5663 937 return BT_FUNC_STATUS_OK;
dac5c838
PP
938}
939
d24d5663
PP
940enum bt_value_array_append_element_status
941bt_value_array_append_bool_element(struct bt_value *array_obj, bt_bool val)
dac5c838 942{
d24d5663 943 enum bt_value_array_append_element_status ret;
05e21286 944 struct bt_value *bool_obj = NULL;
dac5c838 945
17f3083a
SM
946 BT_ASSERT_PRE_NO_ERROR();
947
05e21286
PP
948 bool_obj = bt_value_bool_create_init(val);
949 ret = bt_value_array_append_element(array_obj,
da91b29a 950 (void *) bool_obj);
65300d60 951 bt_object_put_ref(bool_obj);
dac5c838
PP
952 return ret;
953}
954
d24d5663
PP
955enum bt_value_array_append_element_status
956bt_value_array_append_unsigned_integer_element(struct bt_value *array_obj,
957 uint64_t val)
fdd3a2da 958{
d24d5663 959 enum bt_value_array_append_element_status ret;
fdd3a2da
PP
960 struct bt_value *integer_obj = NULL;
961
17f3083a
SM
962 BT_ASSERT_PRE_NO_ERROR();
963
9c08c816 964 integer_obj = bt_value_integer_unsigned_create_init(val);
fdd3a2da
PP
965 ret = bt_value_array_append_element(array_obj,
966 (void *) integer_obj);
967 bt_object_put_ref(integer_obj);
968 return ret;
969}
970
d24d5663
PP
971enum bt_value_array_append_element_status
972bt_value_array_append_signed_integer_element(struct bt_value *array_obj,
973 int64_t val)
dac5c838 974{
d24d5663 975 enum bt_value_array_append_element_status ret;
05e21286 976 struct bt_value *integer_obj = NULL;
dac5c838 977
17f3083a
SM
978 BT_ASSERT_PRE_NO_ERROR();
979
9c08c816 980 integer_obj = bt_value_integer_signed_create_init(val);
05e21286 981 ret = bt_value_array_append_element(array_obj,
da91b29a 982 (void *) integer_obj);
65300d60 983 bt_object_put_ref(integer_obj);
dac5c838
PP
984 return ret;
985}
986
d24d5663
PP
987enum bt_value_array_append_element_status
988bt_value_array_append_real_element(struct bt_value *array_obj, double val)
dac5c838 989{
d24d5663 990 enum bt_value_array_append_element_status ret;
05e21286 991 struct bt_value *real_obj = NULL;
dac5c838 992
17f3083a
SM
993 BT_ASSERT_PRE_NO_ERROR();
994
05e21286
PP
995 real_obj = bt_value_real_create_init(val);
996 ret = bt_value_array_append_element(array_obj,
da91b29a 997 (void *) real_obj);
65300d60 998 bt_object_put_ref(real_obj);
dac5c838
PP
999 return ret;
1000}
1001
d24d5663
PP
1002enum bt_value_array_append_element_status
1003bt_value_array_append_string_element(struct bt_value *array_obj,
1004 const char *val)
dac5c838 1005{
d24d5663 1006 enum bt_value_array_append_element_status ret;
05e21286 1007 struct bt_value *string_obj = NULL;
dac5c838 1008
17f3083a
SM
1009 BT_ASSERT_PRE_NO_ERROR();
1010
05e21286
PP
1011 string_obj = bt_value_string_create_init(val);
1012 ret = bt_value_array_append_element(array_obj,
da91b29a 1013 (void *) string_obj);
65300d60 1014 bt_object_put_ref(string_obj);
dac5c838
PP
1015 return ret;
1016}
1017
d24d5663 1018enum bt_value_array_append_element_status
847ab606
PP
1019bt_value_array_append_empty_array_element(struct bt_value *array_obj,
1020 struct bt_value **element_obj)
dac5c838 1021{
d24d5663 1022 enum bt_value_array_append_element_status ret;
05e21286 1023 struct bt_value *empty_array_obj = NULL;
dac5c838 1024
17f3083a
SM
1025 BT_ASSERT_PRE_NO_ERROR();
1026
05e21286
PP
1027 empty_array_obj = bt_value_array_create();
1028 ret = bt_value_array_append_element(array_obj,
da91b29a 1029 (void *) empty_array_obj);
847ab606
PP
1030
1031 if (element_obj) {
1032 *element_obj = empty_array_obj;
1033 }
1034
65300d60 1035 bt_object_put_ref(empty_array_obj);
dac5c838
PP
1036 return ret;
1037}
1038
d24d5663 1039enum bt_value_array_append_element_status
847ab606
PP
1040bt_value_array_append_empty_map_element(struct bt_value *array_obj,
1041 struct bt_value **element_obj)
dac5c838 1042{
d24d5663 1043 enum bt_value_array_append_element_status ret;
05e21286 1044 struct bt_value *map_obj = NULL;
dac5c838 1045
17f3083a
SM
1046 BT_ASSERT_PRE_NO_ERROR();
1047
05e21286
PP
1048 map_obj = bt_value_map_create();
1049 ret = bt_value_array_append_element(array_obj,
da91b29a 1050 (void *) map_obj);
847ab606
PP
1051
1052 if (element_obj) {
1053 *element_obj = map_obj;
1054 }
1055
65300d60 1056 bt_object_put_ref(map_obj);
dac5c838
PP
1057 return ret;
1058}
1059
d24d5663
PP
1060enum bt_value_array_set_element_by_index_status
1061bt_value_array_set_element_by_index(struct bt_value *array_obj, uint64_t index,
da91b29a 1062 struct bt_value *element_obj)
dac5c838 1063{
dac5c838
PP
1064 struct bt_value_array *typed_array_obj =
1065 BT_VALUE_TO_ARRAY(array_obj);
1066
17f3083a 1067 BT_ASSERT_PRE_NO_ERROR();
f6ccaed9
PP
1068 BT_ASSERT_PRE_NON_NULL(array_obj, "Array value object");
1069 BT_ASSERT_PRE_NON_NULL(element_obj, "Element value object");
1070 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj, BT_VALUE_TYPE_ARRAY);
bdb288b3
PP
1071 BT_ASSERT_PRE_DEV_VALUE_HOT(array_obj, "Array value object");
1072 BT_ASSERT_PRE_VALID_INDEX(index, typed_array_obj->garray->len);
65300d60 1073 bt_object_put_ref(g_ptr_array_index(typed_array_obj->garray, index));
dac5c838 1074 g_ptr_array_index(typed_array_obj->garray, index) = element_obj;
65300d60 1075 bt_object_get_ref(element_obj);
ef267d12 1076 BT_LOGT("Set array value's element: array-value-addr=%p, "
2f42aa0a
PP
1077 "index=%" PRIu64 ", element-value-addr=%p",
1078 array_obj, index, element_obj);
d24d5663 1079 return BT_FUNC_STATUS_OK;
dac5c838
PP
1080}
1081
601b0d3c 1082uint64_t bt_value_map_get_size(const struct bt_value *map_obj)
dac5c838 1083{
bdb288b3
PP
1084 BT_ASSERT_PRE_DEV_NON_NULL(map_obj, "Value object");
1085 BT_ASSERT_PRE_DEV_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP);
601b0d3c 1086 return (uint64_t) g_hash_table_size(BT_VALUE_TO_MAP(map_obj)->ght);
dac5c838
PP
1087}
1088
05e21286 1089struct bt_value *bt_value_map_borrow_entry_value(struct bt_value *map_obj,
364747d6 1090 const char *key)
dac5c838 1091{
bdb288b3
PP
1092 BT_ASSERT_PRE_DEV_NON_NULL(map_obj, "Value object");
1093 BT_ASSERT_PRE_DEV_NON_NULL(key, "Key");
1094 BT_ASSERT_PRE_DEV_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP);
094ff7c0
PP
1095 return g_hash_table_lookup(BT_VALUE_TO_MAP(map_obj)->ght,
1096 GUINT_TO_POINTER(g_quark_from_string(key)));
dac5c838
PP
1097}
1098
05e21286
PP
1099const struct bt_value *bt_value_map_borrow_entry_value_const(
1100 const struct bt_value *map_obj, const char *key)
da91b29a 1101{
05e21286 1102 return bt_value_map_borrow_entry_value((void *) map_obj, key);
da91b29a
PP
1103}
1104
07208d85 1105bt_bool bt_value_map_has_entry(const struct bt_value *map_obj, const char *key)
dac5c838 1106{
bdb288b3
PP
1107 BT_ASSERT_PRE_DEV_NON_NULL(map_obj, "Value object");
1108 BT_ASSERT_PRE_DEV_NON_NULL(key, "Key");
1109 BT_ASSERT_PRE_DEV_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP);
f6ccaed9
PP
1110 return bt_g_hash_table_contains(BT_VALUE_TO_MAP(map_obj)->ght,
1111 GUINT_TO_POINTER(g_quark_from_string(key)));
dac5c838
PP
1112}
1113
d24d5663
PP
1114enum bt_value_map_insert_entry_status bt_value_map_insert_entry(
1115 struct bt_value *map_obj, const char *key,
1116 struct bt_value *element_obj)
dac5c838 1117{
17f3083a 1118 BT_ASSERT_PRE_NO_ERROR();
f6ccaed9
PP
1119 BT_ASSERT_PRE_NON_NULL(map_obj, "Map value object");
1120 BT_ASSERT_PRE_NON_NULL(key, "Key");
1121 BT_ASSERT_PRE_NON_NULL(element_obj, "Element value object");
1122 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP);
bdb288b3 1123 BT_ASSERT_PRE_DEV_VALUE_HOT(map_obj, "Map value object");
f6ccaed9
PP
1124 g_hash_table_insert(BT_VALUE_TO_MAP(map_obj)->ght,
1125 GUINT_TO_POINTER(g_quark_from_string(key)), element_obj);
65300d60 1126 bt_object_get_ref(element_obj);
ef267d12 1127 BT_LOGT("Inserted value into map value: map-value-addr=%p, "
2f42aa0a
PP
1128 "key=\"%s\", element-value-addr=%p",
1129 map_obj, key, element_obj);
d24d5663 1130 return BT_FUNC_STATUS_OK;
dac5c838
PP
1131}
1132
d24d5663 1133enum bt_value_map_insert_entry_status bt_value_map_insert_bool_entry(
05e21286 1134 struct bt_value *map_obj, const char *key, bt_bool val)
dac5c838 1135{
d24d5663 1136 enum bt_value_map_insert_entry_status ret;
05e21286 1137 struct bt_value *bool_obj = NULL;
dac5c838 1138
17f3083a
SM
1139 BT_ASSERT_PRE_NO_ERROR();
1140
05e21286
PP
1141 bool_obj = bt_value_bool_create_init(val);
1142 ret = bt_value_map_insert_entry(map_obj, key,
da91b29a 1143 (void *) bool_obj);
65300d60 1144 bt_object_put_ref(bool_obj);
dac5c838
PP
1145 return ret;
1146}
1147
d24d5663
PP
1148enum bt_value_map_insert_entry_status
1149bt_value_map_insert_unsigned_integer_entry(struct bt_value *map_obj,
1150 const char *key, uint64_t val)
fdd3a2da 1151{
d24d5663 1152 enum bt_value_map_insert_entry_status ret;
fdd3a2da
PP
1153 struct bt_value *integer_obj = NULL;
1154
17f3083a
SM
1155 BT_ASSERT_PRE_NO_ERROR();
1156
9c08c816 1157 integer_obj = bt_value_integer_unsigned_create_init(val);
fdd3a2da
PP
1158 ret = bt_value_map_insert_entry(map_obj, key,
1159 (void *) integer_obj);
1160 bt_object_put_ref(integer_obj);
1161 return ret;
1162}
1163
d24d5663
PP
1164enum bt_value_map_insert_entry_status
1165bt_value_map_insert_signed_integer_entry(struct bt_value *map_obj,
1166 const char *key, int64_t val)
dac5c838 1167{
d24d5663 1168 enum bt_value_map_insert_entry_status ret;
05e21286 1169 struct bt_value *integer_obj = NULL;
dac5c838 1170
17f3083a
SM
1171 BT_ASSERT_PRE_NO_ERROR();
1172
9c08c816 1173 integer_obj = bt_value_integer_signed_create_init(val);
05e21286 1174 ret = bt_value_map_insert_entry(map_obj, key,
da91b29a 1175 (void *) integer_obj);
65300d60 1176 bt_object_put_ref(integer_obj);
dac5c838
PP
1177 return ret;
1178}
1179
d24d5663 1180enum bt_value_map_insert_entry_status bt_value_map_insert_real_entry(
05e21286 1181 struct bt_value *map_obj, const char *key, double val)
dac5c838 1182{
d24d5663 1183 enum bt_value_map_insert_entry_status ret;
05e21286 1184 struct bt_value *real_obj = NULL;
dac5c838 1185
17f3083a
SM
1186 BT_ASSERT_PRE_NO_ERROR();
1187
05e21286
PP
1188 real_obj = bt_value_real_create_init(val);
1189 ret = bt_value_map_insert_entry(map_obj, key,
da91b29a 1190 (void *) real_obj);
65300d60 1191 bt_object_put_ref(real_obj);
dac5c838
PP
1192 return ret;
1193}
1194
d24d5663 1195enum bt_value_map_insert_entry_status bt_value_map_insert_string_entry(
05e21286 1196 struct bt_value *map_obj, const char *key,
601b0d3c 1197 const char *val)
dac5c838 1198{
d24d5663 1199 enum bt_value_map_insert_entry_status ret;
05e21286 1200 struct bt_value *string_obj = NULL;
dac5c838 1201
17f3083a
SM
1202 BT_ASSERT_PRE_NO_ERROR();
1203
05e21286
PP
1204 string_obj = bt_value_string_create_init(val);
1205 ret = bt_value_map_insert_entry(map_obj, key,
da91b29a 1206 (void *) string_obj);
65300d60 1207 bt_object_put_ref(string_obj);
dac5c838
PP
1208 return ret;
1209}
1210
d24d5663
PP
1211enum bt_value_map_insert_entry_status
1212bt_value_map_insert_empty_array_entry(
847ab606
PP
1213 struct bt_value *map_obj, const char *key,
1214 bt_value **entry_obj)
dac5c838 1215{
d24d5663 1216 enum bt_value_map_insert_entry_status ret;
05e21286 1217 struct bt_value *array_obj = NULL;
dac5c838 1218
17f3083a
SM
1219 BT_ASSERT_PRE_NO_ERROR();
1220
05e21286
PP
1221 array_obj = bt_value_array_create();
1222 ret = bt_value_map_insert_entry(map_obj, key,
da91b29a 1223 (void *) array_obj);
847ab606
PP
1224
1225 if (entry_obj) {
1226 *entry_obj = array_obj;
1227 }
1228
65300d60 1229 bt_object_put_ref(array_obj);
dac5c838
PP
1230 return ret;
1231}
1232
d24d5663 1233enum bt_value_map_insert_entry_status
847ab606
PP
1234bt_value_map_insert_empty_map_entry(struct bt_value *map_obj, const char *key,
1235 bt_value **entry_obj)
dac5c838 1236{
d24d5663 1237 enum bt_value_map_insert_entry_status ret;
05e21286 1238 struct bt_value *empty_map_obj = NULL;
dac5c838 1239
17f3083a
SM
1240 BT_ASSERT_PRE_NO_ERROR();
1241
05e21286
PP
1242 empty_map_obj = bt_value_map_create();
1243 ret = bt_value_map_insert_entry(map_obj, key,
da91b29a 1244 (void *) empty_map_obj);
847ab606
PP
1245
1246 if (entry_obj) {
1247 *entry_obj = empty_map_obj;
1248 }
1249
65300d60 1250 bt_object_put_ref(empty_map_obj);
dac5c838
PP
1251 return ret;
1252}
1253
d24d5663
PP
1254enum bt_value_map_foreach_entry_status bt_value_map_foreach_entry(
1255 struct bt_value *map_obj, bt_value_map_foreach_entry_func func,
1256 void *data)
dac5c838 1257{
27c61ce8 1258 int status = BT_FUNC_STATUS_OK;
dac5c838
PP
1259 gpointer key, element_obj;
1260 GHashTableIter iter;
1261 struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj);
1262
17f3083a
SM
1263 BT_ASSERT_PRE_NO_ERROR();
1264
bdb288b3
PP
1265 BT_ASSERT_PRE_DEV_NON_NULL(map_obj, "Value object");
1266 BT_ASSERT_PRE_DEV_NON_NULL(func, "Callback");
1267 BT_ASSERT_PRE_DEV_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP);
dac5c838
PP
1268 g_hash_table_iter_init(&iter, typed_map_obj->ght);
1269
1270 while (g_hash_table_iter_next(&iter, &key, &element_obj)) {
5b44aff2 1271 const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key));
dac5c838 1272
27c61ce8
PP
1273 status = func(key_str, element_obj, data);
1274 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status);
1275 if (status != BT_FUNC_STATUS_OK) {
1276 if (status < 0) {
1277 BT_LIB_LOGE_APPEND_CAUSE(
1278 "User function failed while iterating "
1279 "map value entries: "
1280 "status=%s, key=\"%s\", "
1281 "value-addr=%p, data=%p",
1282 bt_common_func_status_string(status),
1283 key_str, element_obj, data);
1284
1285 if (status == BT_FUNC_STATUS_ERROR) {
1286 /*
1287 * User function error becomes a
1288 * user error from this
1289 * function's caller's
1290 * perspective.
1291 */
1292 status = BT_FUNC_STATUS_USER_ERROR;
1293 }
1294 } else {
1295 BT_ASSERT(status == BT_FUNC_STATUS_INTERRUPTED);
1296 BT_LOGT("User interrupted the loop: status=%s, "
1297 "key=\"%s\", value-addr=%p, data=%p",
1298 bt_common_func_status_string(status),
1299 key_str, element_obj, data);
1300 }
1301
dac5c838
PP
1302 break;
1303 }
1304 }
1305
27c61ce8 1306 return status;
dac5c838
PP
1307}
1308
d24d5663 1309enum bt_value_map_foreach_entry_const_status bt_value_map_foreach_entry_const(
05e21286 1310 const struct bt_value *map_obj,
40f4ba76 1311 bt_value_map_foreach_entry_const_func func, void *data)
da91b29a 1312{
17f3083a
SM
1313 BT_ASSERT_PRE_NO_ERROR();
1314
d24d5663 1315 return (int) bt_value_map_foreach_entry((void *) map_obj,
40f4ba76 1316 (bt_value_map_foreach_entry_func) func, data);
da91b29a
PP
1317}
1318
770750d3 1319struct extend_map_element_data {
ab26240f 1320 struct bt_value *base_obj;
770750d3
PP
1321};
1322
1323static
27c61ce8
PP
1324bt_value_map_foreach_entry_const_func_status extend_map_element(
1325 const char *key, const struct bt_value *extension_obj_elem,
1326 void *data)
770750d3 1327{
27c61ce8 1328 int status;
770750d3 1329 struct extend_map_element_data *extend_data = data;
05e21286 1330 struct bt_value *extension_obj_elem_copy = NULL;
770750d3
PP
1331
1332 /* Copy object which is to replace the current one */
27c61ce8
PP
1333 status = bt_value_copy(extension_obj_elem, &extension_obj_elem_copy);
1334 if (status) {
870631a2 1335 BT_LIB_LOGE_APPEND_CAUSE("Cannot copy map element: %!+v",
601b0d3c
PP
1336 extension_obj_elem);
1337 goto error;
1338 }
1339
1340 BT_ASSERT(extension_obj_elem_copy);
770750d3 1341
ab26240f 1342 /* Replace in base map value. */
27c61ce8 1343 status = bt_value_map_insert_entry(extend_data->base_obj, key,
e7279cae 1344 extension_obj_elem_copy);
27c61ce8 1345 if (status) {
870631a2 1346 BT_LIB_LOGE_APPEND_CAUSE(
ab26240f
FD
1347 "Cannot replace value in base map value: key=\"%s\", "
1348 "%![base-map-value-]+v, %![element-value-]+v",
27c61ce8 1349 key, extend_data->base_obj, extension_obj_elem_copy);
770750d3
PP
1350 goto error;
1351 }
1352
1353 goto end;
1354
1355error:
27c61ce8 1356 BT_ASSERT(status < 0);
770750d3
PP
1357
1358end:
65300d60 1359 BT_OBJECT_PUT_REF_AND_RESET(extension_obj_elem_copy);
27c61ce8
PP
1360 BT_ASSERT(status == BT_FUNC_STATUS_OK ||
1361 status == BT_FUNC_STATUS_MEMORY_ERROR);
1362 return status;
770750d3
PP
1363}
1364
d24d5663 1365enum bt_value_map_extend_status bt_value_map_extend(
ab26240f
FD
1366 struct bt_value *base_map_obj,
1367 const struct bt_value *extension_obj)
770750d3 1368{
27c61ce8 1369 int status = BT_FUNC_STATUS_OK;
601b0d3c 1370 struct extend_map_element_data extend_data = {
ab26240f 1371 .base_obj = NULL,
601b0d3c 1372 };
770750d3 1373
17f3083a 1374 BT_ASSERT_PRE_NO_ERROR();
f6ccaed9 1375 BT_ASSERT_PRE_NON_NULL(base_map_obj, "Base value object");
ab26240f 1376 BT_ASSERT_PRE_DEV_VALUE_HOT(base_map_obj, "Base value object");
f6ccaed9
PP
1377 BT_ASSERT_PRE_NON_NULL(extension_obj, "Extension value object");
1378 BT_ASSERT_PRE_VALUE_IS_TYPE(base_map_obj, BT_VALUE_TYPE_MAP);
1379 BT_ASSERT_PRE_VALUE_IS_TYPE(extension_obj, BT_VALUE_TYPE_MAP);
2f42aa0a
PP
1380 BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p",
1381 base_map_obj, extension_obj);
601b0d3c 1382
770750d3
PP
1383 /*
1384 * For each key in the extension map object, replace this key
ab26240f 1385 * in the base map object.
770750d3 1386 */
ab26240f 1387 extend_data.base_obj = base_map_obj;
27c61ce8
PP
1388 status = bt_value_map_foreach_entry_const(extension_obj,
1389 extend_map_element, &extend_data);
1390 if (status != BT_FUNC_STATUS_OK) {
1391 BT_ASSERT(status == BT_FUNC_STATUS_MEMORY_ERROR);
870631a2
PP
1392 BT_LIB_LOGE_APPEND_CAUSE(
1393 "Cannot iterate on the extension object's elements: "
1394 "%![extension-value-]+v", extension_obj);
770750d3
PP
1395 }
1396
27c61ce8 1397 return status;
770750d3
PP
1398}
1399
d24d5663 1400enum bt_value_copy_status bt_value_copy(const struct bt_value *object,
6be5a99e 1401 struct bt_value **copy_obj)
dac5c838 1402{
d24d5663 1403 enum bt_value_copy_status status = BT_FUNC_STATUS_OK;
dac5c838 1404
17f3083a 1405 BT_ASSERT_PRE_NO_ERROR();
f6ccaed9 1406 BT_ASSERT_PRE_NON_NULL(object, "Value object");
601b0d3c 1407 BT_ASSERT_PRE_NON_NULL(copy_obj, "Value object copy (output)");
2f42aa0a 1408 BT_LOGD("Copying value object: addr=%p", object);
601b0d3c
PP
1409 *copy_obj = copy_funcs[object->type](object);
1410 if (*copy_obj) {
2f42aa0a
PP
1411 BT_LOGD("Copied value object: copy-value-addr=%p",
1412 copy_obj);
1413 } else {
d24d5663 1414 status = BT_FUNC_STATUS_MEMORY_ERROR;
601b0d3c 1415 *copy_obj = NULL;
870631a2 1416 BT_LIB_LOGE_APPEND_CAUSE("Failed to copy value object.");
2f42aa0a 1417 }
dac5c838 1418
601b0d3c 1419 return status;
dac5c838
PP
1420}
1421
cd933d89 1422bt_bool bt_value_is_equal(const struct bt_value *object_a,
dac5c838
PP
1423 const struct bt_value *object_b)
1424{
c55a9f58 1425 bt_bool ret = BT_FALSE;
dac5c838 1426
bdb288b3
PP
1427 BT_ASSERT_PRE_DEV_NON_NULL(object_a, "Value object A");
1428 BT_ASSERT_PRE_DEV_NON_NULL(object_b, "Value object B");
dac5c838
PP
1429
1430 if (object_a->type != object_b->type) {
ef267d12 1431 BT_LOGT("Values are different: type mismatch: "
2f42aa0a 1432 "value-a-addr=%p, value-b-addr=%p, "
c4628760 1433 "value-a-type=%s, value-b-type=%s",
2f42aa0a 1434 object_a, object_b,
da91b29a
PP
1435 bt_common_value_type_string(object_a->type),
1436 bt_common_value_type_string(object_b->type));
dac5c838
PP
1437 goto end;
1438 }
1439
cd933d89 1440 ret = is_equal_funcs[object_a->type](object_a, object_b);
dac5c838
PP
1441
1442end:
1443 return ret;
1444}
c5b9b441
PP
1445
1446void bt_value_get_ref(const struct bt_value *value)
1447{
1448 bt_object_get_ref(value);
1449}
1450
1451void bt_value_put_ref(const struct bt_value *value)
1452{
1453 bt_object_put_ref(value);
1454}
This page took 0.161926 seconds and 4 git commands to generate.