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