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