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