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