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