lib: strictly type function return status enumerations
[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) {
2f42aa0a 181 BT_LOGE_STR("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) {
2f42aa0a
PP
196 BT_LOGE("Cannot copy array value's element: "
197 "array-addr=%p, index=%d",
198 array_obj, i);
65300d60 199 BT_OBJECT_PUT_REF_AND_RESET(copy_obj);
dac5c838
PP
200 goto end;
201 }
202
601b0d3c 203 BT_ASSERT(element_obj_copy);
05e21286 204 ret = bt_value_array_append_element(copy_obj,
da91b29a 205 (void *) element_obj_copy);
65300d60 206 BT_OBJECT_PUT_REF_AND_RESET(element_obj_copy);
dac5c838 207 if (ret) {
2f42aa0a
PP
208 BT_LOGE("Cannot append to array value: addr=%p",
209 array_obj);
65300d60 210 BT_OBJECT_PUT_REF_AND_RESET(copy_obj);
dac5c838
PP
211 goto end;
212 }
213 }
214
a704fb0b
PP
215 BT_LOGD("Copied array value: original-addr=%p, copy-addr=%p",
216 array_obj, copy_obj);
2f42aa0a 217
dac5c838
PP
218end:
219 return copy_obj;
220}
221
222static
05e21286 223struct bt_value *bt_value_map_copy(const struct bt_value *map_obj)
dac5c838
PP
224{
225 int ret;
226 GHashTableIter iter;
227 gpointer key, element_obj;
05e21286
PP
228 struct bt_value *copy_obj;
229 struct bt_value *element_obj_copy = NULL;
dac5c838
PP
230 struct bt_value_map *typed_map_obj;
231
2f42aa0a 232 BT_LOGD("Copying map value: addr=%p", map_obj);
dac5c838 233 typed_map_obj = BT_VALUE_TO_MAP(map_obj);
05e21286 234 copy_obj = bt_value_map_create();
dac5c838
PP
235 if (!copy_obj) {
236 goto end;
237 }
238
239 g_hash_table_iter_init(&iter, typed_map_obj->ght);
240
241 while (g_hash_table_iter_next(&iter, &key, &element_obj)) {
5b44aff2 242 const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key));
dac5c838 243
f6ccaed9 244 BT_ASSERT(key_str);
40b59ed9
PP
245 BT_LOGD("Copying map value's element: element-addr=%p, "
246 "key=\"%s\"", element_obj, key_str);
6be5a99e 247 ret = bt_value_copy(element_obj, &element_obj_copy);
601b0d3c 248 if (ret) {
2f42aa0a
PP
249 BT_LOGE("Cannot copy map value's element: "
250 "map-addr=%p, key=\"%s\"",
251 map_obj, key_str);
65300d60 252 BT_OBJECT_PUT_REF_AND_RESET(copy_obj);
dac5c838
PP
253 goto end;
254 }
255
601b0d3c 256 BT_ASSERT(element_obj_copy);
05e21286 257 ret = bt_value_map_insert_entry(copy_obj, key_str,
da91b29a 258 (void *) element_obj_copy);
65300d60 259 BT_OBJECT_PUT_REF_AND_RESET(element_obj_copy);
dac5c838 260 if (ret) {
2f42aa0a
PP
261 BT_LOGE("Cannot insert into map value: addr=%p, key=\"%s\"",
262 map_obj, key_str);
65300d60 263 BT_OBJECT_PUT_REF_AND_RESET(copy_obj);
dac5c838
PP
264 goto end;
265 }
266 }
267
2f42aa0a
PP
268 BT_LOGD("Copied map value: addr=%p", map_obj);
269
dac5c838
PP
270end:
271 return copy_obj;
272}
273
274static
05e21286 275struct bt_value *(* const copy_funcs[])(const struct bt_value *) = {
fdd3a2da
PP
276 [BT_VALUE_TYPE_NULL] = bt_value_null_copy,
277 [BT_VALUE_TYPE_BOOL] = bt_value_bool_copy,
278 [BT_VALUE_TYPE_UNSIGNED_INTEGER] = bt_value_integer_copy,
279 [BT_VALUE_TYPE_SIGNED_INTEGER] = bt_value_integer_copy,
280 [BT_VALUE_TYPE_REAL] = bt_value_real_copy,
281 [BT_VALUE_TYPE_STRING] = bt_value_string_copy,
282 [BT_VALUE_TYPE_ARRAY] = bt_value_array_copy,
283 [BT_VALUE_TYPE_MAP] = bt_value_map_copy,
dac5c838
PP
284};
285
286static
c55a9f58 287bt_bool bt_value_null_compare(const struct bt_value *object_a,
dac5c838
PP
288 const struct bt_value *object_b)
289{
290 /*
c55a9f58 291 * Always BT_TRUE since bt_value_compare() already checks if both
dac5c838
PP
292 * object_a and object_b have the same type, and in the case of
293 * null value objects, they're always the same if it is so.
294 */
c55a9f58 295 return BT_TRUE;
dac5c838
PP
296}
297
298static
c55a9f58 299bt_bool bt_value_bool_compare(const struct bt_value *object_a,
dac5c838
PP
300 const struct bt_value *object_b)
301{
40b59ed9
PP
302 if (BT_VALUE_TO_BOOL(object_a)->value !=
303 BT_VALUE_TO_BOOL(object_b)->value) {
ef267d12 304 BT_LOGT("Boolean value objects are different: "
40b59ed9
PP
305 "bool-a-val=%d, bool-b-val=%d",
306 BT_VALUE_TO_BOOL(object_a)->value,
307 BT_VALUE_TO_BOOL(object_b)->value);
308 return BT_FALSE;
309 }
310
311 return BT_TRUE;
dac5c838
PP
312}
313
314static
c55a9f58 315bt_bool bt_value_integer_compare(const struct bt_value *object_a,
dac5c838
PP
316 const struct bt_value *object_b)
317{
fdd3a2da
PP
318 if (BT_VALUE_TO_INTEGER(object_a)->value.u !=
319 BT_VALUE_TO_INTEGER(object_b)->value.u) {
320 if (object_a->type == BT_VALUE_TYPE_UNSIGNED_INTEGER) {
ef267d12 321 BT_LOGT("Unsigned integer value objects are different: "
fdd3a2da
PP
322 "int-a-val=%" PRIu64 ", int-b-val=%" PRIu64,
323 BT_VALUE_TO_INTEGER(object_a)->value.u,
324 BT_VALUE_TO_INTEGER(object_b)->value.u);
325 } else {
ef267d12 326 BT_LOGT("Signed integer value objects are different: "
fdd3a2da
PP
327 "int-a-val=%" PRId64 ", int-b-val=%" PRId64,
328 BT_VALUE_TO_INTEGER(object_a)->value.i,
329 BT_VALUE_TO_INTEGER(object_b)->value.i);
330 }
331
40b59ed9
PP
332 return BT_FALSE;
333 }
334
335 return BT_TRUE;
dac5c838
PP
336}
337
338static
a373bf69 339bt_bool bt_value_real_compare(const struct bt_value *object_a,
dac5c838
PP
340 const struct bt_value *object_b)
341{
a373bf69
PP
342 if (BT_VALUE_TO_REAL(object_a)->value !=
343 BT_VALUE_TO_REAL(object_b)->value) {
ef267d12 344 BT_LOGT("Real number value objects are different: "
a373bf69
PP
345 "real-a-val=%f, real-b-val=%f",
346 BT_VALUE_TO_REAL(object_a)->value,
347 BT_VALUE_TO_REAL(object_b)->value);
40b59ed9
PP
348 return BT_FALSE;
349 }
350
351 return BT_TRUE;
dac5c838
PP
352}
353
354static
c55a9f58 355bt_bool bt_value_string_compare(const struct bt_value *object_a,
dac5c838
PP
356 const struct bt_value *object_b)
357{
40b59ed9
PP
358 if (strcmp(BT_VALUE_TO_STRING(object_a)->gstr->str,
359 BT_VALUE_TO_STRING(object_b)->gstr->str) != 0) {
ef267d12 360 BT_LOGT("String value objects are different: "
40b59ed9
PP
361 "string-a-val=\"%s\", string-b-val=\"%s\"",
362 BT_VALUE_TO_STRING(object_a)->gstr->str,
363 BT_VALUE_TO_STRING(object_b)->gstr->str);
364 return BT_FALSE;
365 }
366
367 return BT_TRUE;
dac5c838
PP
368}
369
370static
c55a9f58 371bt_bool bt_value_array_compare(const struct bt_value *object_a,
dac5c838
PP
372 const struct bt_value *object_b)
373{
374 int i;
c55a9f58 375 bt_bool ret = BT_TRUE;
dac5c838
PP
376 const struct bt_value_array *array_obj_a =
377 BT_VALUE_TO_ARRAY(object_a);
378
da91b29a
PP
379 if (bt_value_array_get_size(object_a) !=
380 bt_value_array_get_size(object_b)) {
ef267d12 381 BT_LOGT("Array values are different: size mismatch "
2f42aa0a
PP
382 "value-a-addr=%p, value-b-addr=%p, "
383 "value-a-size=%" PRId64 ", value-b-size=%" PRId64,
384 object_a, object_b,
07208d85
PP
385 bt_value_array_get_size(object_a),
386 bt_value_array_get_size(object_b));
c55a9f58 387 ret = BT_FALSE;
dac5c838
PP
388 goto end;
389 }
390
391 for (i = 0; i < array_obj_a->garray->len; ++i) {
05e21286
PP
392 const struct bt_value *element_obj_a;
393 const struct bt_value *element_obj_b;
dac5c838 394
05e21286 395 element_obj_a = bt_value_array_borrow_element_by_index_const(
da91b29a 396 object_a, i);
05e21286 397 element_obj_b = bt_value_array_borrow_element_by_index_const(
da91b29a 398 object_b, i);
dac5c838
PP
399
400 if (!bt_value_compare(element_obj_a, element_obj_b)) {
ef267d12 401 BT_LOGT("Array values's elements are different: "
2f42aa0a 402 "value-a-addr=%p, value-b-addr=%p, index=%d",
32e87ceb 403 element_obj_a, element_obj_b, i);
c55a9f58 404 ret = BT_FALSE;
dac5c838
PP
405 goto end;
406 }
dac5c838
PP
407 }
408
409end:
410 return ret;
411}
412
413static
c55a9f58 414bt_bool bt_value_map_compare(const struct bt_value *object_a,
dac5c838
PP
415 const struct bt_value *object_b)
416{
c55a9f58 417 bt_bool ret = BT_TRUE;
dac5c838
PP
418 GHashTableIter iter;
419 gpointer key, element_obj_a;
420 const struct bt_value_map *map_obj_a = BT_VALUE_TO_MAP(object_a);
421
601b0d3c
PP
422 if (bt_value_map_get_size(object_a) !=
423 bt_value_map_get_size(object_b)) {
ef267d12 424 BT_LOGT("Map values are different: size mismatch "
2f42aa0a
PP
425 "value-a-addr=%p, value-b-addr=%p, "
426 "value-a-size=%" PRId64 ", value-b-size=%" PRId64,
427 object_a, object_b,
07208d85
PP
428 bt_value_map_get_size(object_a),
429 bt_value_map_get_size(object_b));
c55a9f58 430 ret = BT_FALSE;
dac5c838
PP
431 goto end;
432 }
433
434 g_hash_table_iter_init(&iter, map_obj_a->ght);
435
436 while (g_hash_table_iter_next(&iter, &key, &element_obj_a)) {
05e21286 437 const struct bt_value *element_obj_b;
5b44aff2 438 const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key));
dac5c838 439
05e21286 440 element_obj_b = bt_value_map_borrow_entry_value_const(object_b,
da91b29a 441 key_str);
dac5c838
PP
442
443 if (!bt_value_compare(element_obj_a, element_obj_b)) {
ef267d12 444 BT_LOGT("Map values's elements are different: "
2f42aa0a
PP
445 "value-a-addr=%p, value-b-addr=%p, key=\"%s\"",
446 element_obj_a, element_obj_b, key_str);
c55a9f58 447 ret = BT_FALSE;
dac5c838
PP
448 goto end;
449 }
dac5c838
PP
450 }
451
452end:
453 return ret;
454}
455
456static
c55a9f58 457bt_bool (* const compare_funcs[])(const struct bt_value *,
dac5c838 458 const struct bt_value *) = {
fdd3a2da
PP
459 [BT_VALUE_TYPE_NULL] = bt_value_null_compare,
460 [BT_VALUE_TYPE_BOOL] = bt_value_bool_compare,
461 [BT_VALUE_TYPE_UNSIGNED_INTEGER] = bt_value_integer_compare,
462 [BT_VALUE_TYPE_SIGNED_INTEGER] = bt_value_integer_compare,
463 [BT_VALUE_TYPE_REAL] = bt_value_real_compare,
464 [BT_VALUE_TYPE_STRING] = bt_value_string_compare,
465 [BT_VALUE_TYPE_ARRAY] = bt_value_array_compare,
466 [BT_VALUE_TYPE_MAP] = bt_value_map_compare,
dac5c838
PP
467};
468
f6ccaed9 469static
dac5c838
PP
470void bt_value_null_freeze(struct bt_value *object)
471{
472}
473
f6ccaed9 474static
dac5c838
PP
475void bt_value_generic_freeze(struct bt_value *object)
476{
f6ccaed9 477 object->frozen = BT_TRUE;
dac5c838
PP
478}
479
f6ccaed9 480static
dac5c838
PP
481void bt_value_array_freeze(struct bt_value *object)
482{
483 int i;
484 struct bt_value_array *typed_array_obj =
485 BT_VALUE_TO_ARRAY(object);
486
487 for (i = 0; i < typed_array_obj->garray->len; ++i) {
f6ccaed9 488 bt_value_freeze(g_ptr_array_index(typed_array_obj->garray, i));
dac5c838
PP
489 }
490
491 bt_value_generic_freeze(object);
492}
493
f6ccaed9 494static
dac5c838
PP
495void bt_value_map_freeze(struct bt_value *object)
496{
497 GHashTableIter iter;
498 gpointer key, element_obj;
499 const struct bt_value_map *map_obj = BT_VALUE_TO_MAP(object);
500
501 g_hash_table_iter_init(&iter, map_obj->ght);
502
503 while (g_hash_table_iter_next(&iter, &key, &element_obj)) {
504 bt_value_freeze(element_obj);
505 }
506
507 bt_value_generic_freeze(object);
508}
509
510static
511void (* const freeze_funcs[])(struct bt_value *) = {
fdd3a2da
PP
512 [BT_VALUE_TYPE_NULL] = bt_value_null_freeze,
513 [BT_VALUE_TYPE_BOOL] = bt_value_generic_freeze,
514 [BT_VALUE_TYPE_UNSIGNED_INTEGER] = bt_value_generic_freeze,
515 [BT_VALUE_TYPE_SIGNED_INTEGER] = bt_value_generic_freeze,
516 [BT_VALUE_TYPE_REAL] = bt_value_generic_freeze,
517 [BT_VALUE_TYPE_STRING] = bt_value_generic_freeze,
518 [BT_VALUE_TYPE_ARRAY] = bt_value_array_freeze,
519 [BT_VALUE_TYPE_MAP] = bt_value_map_freeze,
dac5c838
PP
520};
521
522static
83509119 523void bt_value_destroy(struct bt_object *obj)
dac5c838 524{
83509119 525 struct bt_value *value;
dac5c838 526
83509119 527 value = container_of(obj, struct bt_value, base);
2f42aa0a
PP
528 BT_LOGD("Destroying value: addr=%p", value);
529
83509119 530 if (bt_value_is_null(value)) {
2f42aa0a 531 BT_LOGD_STR("Not destroying the null value singleton.");
dac5c838
PP
532 return;
533 }
534
83509119
JG
535 if (destroy_funcs[value->type]) {
536 destroy_funcs[value->type](value);
dac5c838
PP
537 }
538
83509119 539 g_free(value);
dac5c838
PP
540}
541
f6ccaed9 542BT_HIDDEN
d24d5663 543void _bt_value_freeze(const struct bt_value *c_object)
dac5c838 544{
05e21286 545 const struct bt_value *object = (void *) c_object;
dac5c838 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:
d24d5663 557 return;
dac5c838
PP
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;
ef267d12 787 BT_LOGT("Set boolean value's raw value: value-addr=%p, value=%d",
2f42aa0a 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);
ef267d12 821 BT_LOGT("Set unsigned integer value's raw value: "
fdd3a2da
PP
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);
ef267d12 830 BT_LOGT("Set signed integer value's raw value: "
fdd3a2da 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;
ef267d12 847 BT_LOGT("Set real number value's raw value: value-addr=%p, value=%f",
a373bf69 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
d24d5663 858enum bt_value_string_set_status bt_value_string_set(
05e21286 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);
ef267d12 865 BT_LOGT("Set string value's raw value: value-addr=%p, raw-value-addr=%p",
2f42aa0a 866 string_obj, val);
d24d5663 867 return BT_FUNC_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
d24d5663 898enum bt_value_array_append_element_status bt_value_array_append_element(
05e21286 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);
ef267d12 911 BT_LOGT("Appended element to array value: array-value-addr=%p, "
2f42aa0a
PP
912 "element-value-addr=%p, new-size=%u",
913 array_obj, element_obj, typed_array_obj->garray->len);
d24d5663 914 return BT_FUNC_STATUS_OK;
dac5c838
PP
915}
916
d24d5663
PP
917enum bt_value_array_append_element_status
918bt_value_array_append_bool_element(struct bt_value *array_obj, bt_bool val)
dac5c838 919{
d24d5663 920 enum bt_value_array_append_element_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
d24d5663
PP
930enum bt_value_array_append_element_status
931bt_value_array_append_unsigned_integer_element(struct bt_value *array_obj,
932 uint64_t val)
fdd3a2da 933{
d24d5663 934 enum bt_value_array_append_element_status ret;
fdd3a2da
PP
935 struct bt_value *integer_obj = NULL;
936
937 integer_obj = bt_value_unsigned_integer_create_init(val);
938 ret = bt_value_array_append_element(array_obj,
939 (void *) integer_obj);
940 bt_object_put_ref(integer_obj);
941 return ret;
942}
943
d24d5663
PP
944enum bt_value_array_append_element_status
945bt_value_array_append_signed_integer_element(struct bt_value *array_obj,
946 int64_t val)
dac5c838 947{
d24d5663 948 enum bt_value_array_append_element_status ret;
05e21286 949 struct bt_value *integer_obj = NULL;
dac5c838 950
fdd3a2da 951 integer_obj = bt_value_signed_integer_create_init(val);
05e21286 952 ret = bt_value_array_append_element(array_obj,
da91b29a 953 (void *) integer_obj);
65300d60 954 bt_object_put_ref(integer_obj);
dac5c838
PP
955 return ret;
956}
957
d24d5663
PP
958enum bt_value_array_append_element_status
959bt_value_array_append_real_element(struct bt_value *array_obj, double val)
dac5c838 960{
d24d5663 961 enum bt_value_array_append_element_status ret;
05e21286 962 struct bt_value *real_obj = NULL;
dac5c838 963
05e21286
PP
964 real_obj = bt_value_real_create_init(val);
965 ret = bt_value_array_append_element(array_obj,
da91b29a 966 (void *) real_obj);
65300d60 967 bt_object_put_ref(real_obj);
dac5c838
PP
968 return ret;
969}
970
d24d5663
PP
971enum bt_value_array_append_element_status
972bt_value_array_append_string_element(struct bt_value *array_obj,
973 const char *val)
dac5c838 974{
d24d5663 975 enum bt_value_array_append_element_status ret;
05e21286 976 struct bt_value *string_obj = NULL;
dac5c838 977
05e21286
PP
978 string_obj = bt_value_string_create_init(val);
979 ret = bt_value_array_append_element(array_obj,
da91b29a 980 (void *) string_obj);
65300d60 981 bt_object_put_ref(string_obj);
dac5c838
PP
982 return ret;
983}
984
d24d5663
PP
985enum bt_value_array_append_element_status
986bt_value_array_append_empty_array_element(struct bt_value *array_obj)
dac5c838 987{
d24d5663 988 enum bt_value_array_append_element_status ret;
05e21286 989 struct bt_value *empty_array_obj = NULL;
dac5c838 990
05e21286
PP
991 empty_array_obj = bt_value_array_create();
992 ret = bt_value_array_append_element(array_obj,
da91b29a 993 (void *) empty_array_obj);
65300d60 994 bt_object_put_ref(empty_array_obj);
dac5c838
PP
995 return ret;
996}
997
d24d5663
PP
998enum bt_value_array_append_element_status
999bt_value_array_append_empty_map_element(struct bt_value *array_obj)
dac5c838 1000{
d24d5663 1001 enum bt_value_array_append_element_status ret;
05e21286 1002 struct bt_value *map_obj = NULL;
dac5c838 1003
05e21286
PP
1004 map_obj = bt_value_map_create();
1005 ret = bt_value_array_append_element(array_obj,
da91b29a 1006 (void *) map_obj);
65300d60 1007 bt_object_put_ref(map_obj);
dac5c838
PP
1008 return ret;
1009}
1010
d24d5663
PP
1011enum bt_value_array_set_element_by_index_status
1012bt_value_array_set_element_by_index(struct bt_value *array_obj, uint64_t index,
da91b29a 1013 struct bt_value *element_obj)
dac5c838 1014{
dac5c838
PP
1015 struct bt_value_array *typed_array_obj =
1016 BT_VALUE_TO_ARRAY(array_obj);
1017
f6ccaed9
PP
1018 BT_ASSERT_PRE_NON_NULL(array_obj, "Array value object");
1019 BT_ASSERT_PRE_NON_NULL(element_obj, "Element value object");
1020 BT_ASSERT_PRE_VALUE_IS_TYPE(array_obj, BT_VALUE_TYPE_ARRAY);
1021 BT_ASSERT_PRE_VALUE_HOT(array_obj, "Array value object");
1022 BT_ASSERT_PRE_VALUE_INDEX_IN_BOUNDS(index,
1023 typed_array_obj->garray->len);
65300d60 1024 bt_object_put_ref(g_ptr_array_index(typed_array_obj->garray, index));
dac5c838 1025 g_ptr_array_index(typed_array_obj->garray, index) = element_obj;
65300d60 1026 bt_object_get_ref(element_obj);
ef267d12 1027 BT_LOGT("Set array value's element: array-value-addr=%p, "
2f42aa0a
PP
1028 "index=%" PRIu64 ", element-value-addr=%p",
1029 array_obj, index, element_obj);
d24d5663 1030 return BT_FUNC_STATUS_OK;
dac5c838
PP
1031}
1032
601b0d3c 1033uint64_t bt_value_map_get_size(const struct bt_value *map_obj)
dac5c838 1034{
f6ccaed9
PP
1035 BT_ASSERT_PRE_NON_NULL(map_obj, "Value object");
1036 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP);
601b0d3c 1037 return (uint64_t) g_hash_table_size(BT_VALUE_TO_MAP(map_obj)->ght);
dac5c838
PP
1038}
1039
05e21286 1040struct bt_value *bt_value_map_borrow_entry_value(struct bt_value *map_obj,
364747d6 1041 const char *key)
dac5c838 1042{
f6ccaed9
PP
1043 BT_ASSERT_PRE_NON_NULL(map_obj, "Value object");
1044 BT_ASSERT_PRE_NON_NULL(key, "Key");
1045 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP);
094ff7c0
PP
1046 return g_hash_table_lookup(BT_VALUE_TO_MAP(map_obj)->ght,
1047 GUINT_TO_POINTER(g_quark_from_string(key)));
dac5c838
PP
1048}
1049
05e21286
PP
1050const struct bt_value *bt_value_map_borrow_entry_value_const(
1051 const struct bt_value *map_obj, const char *key)
da91b29a 1052{
05e21286 1053 return bt_value_map_borrow_entry_value((void *) map_obj, key);
da91b29a
PP
1054}
1055
07208d85 1056bt_bool bt_value_map_has_entry(const struct bt_value *map_obj, const char *key)
dac5c838 1057{
f6ccaed9
PP
1058 BT_ASSERT_PRE_NON_NULL(map_obj, "Value object");
1059 BT_ASSERT_PRE_NON_NULL(key, "Key");
1060 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP);
1061 return bt_g_hash_table_contains(BT_VALUE_TO_MAP(map_obj)->ght,
1062 GUINT_TO_POINTER(g_quark_from_string(key)));
dac5c838
PP
1063}
1064
d24d5663
PP
1065enum bt_value_map_insert_entry_status bt_value_map_insert_entry(
1066 struct bt_value *map_obj, const char *key,
1067 struct bt_value *element_obj)
dac5c838 1068{
f6ccaed9
PP
1069 BT_ASSERT_PRE_NON_NULL(map_obj, "Map value object");
1070 BT_ASSERT_PRE_NON_NULL(key, "Key");
1071 BT_ASSERT_PRE_NON_NULL(element_obj, "Element value object");
1072 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP);
1073 BT_ASSERT_PRE_VALUE_HOT(map_obj, "Map value object");
1074 g_hash_table_insert(BT_VALUE_TO_MAP(map_obj)->ght,
1075 GUINT_TO_POINTER(g_quark_from_string(key)), element_obj);
65300d60 1076 bt_object_get_ref(element_obj);
ef267d12 1077 BT_LOGT("Inserted value into map value: map-value-addr=%p, "
2f42aa0a
PP
1078 "key=\"%s\", element-value-addr=%p",
1079 map_obj, key, element_obj);
d24d5663 1080 return BT_FUNC_STATUS_OK;
dac5c838
PP
1081}
1082
d24d5663 1083enum bt_value_map_insert_entry_status bt_value_map_insert_bool_entry(
05e21286 1084 struct bt_value *map_obj, const char *key, bt_bool val)
dac5c838 1085{
d24d5663 1086 enum bt_value_map_insert_entry_status ret;
05e21286 1087 struct bt_value *bool_obj = NULL;
dac5c838 1088
05e21286
PP
1089 bool_obj = bt_value_bool_create_init(val);
1090 ret = bt_value_map_insert_entry(map_obj, key,
da91b29a 1091 (void *) bool_obj);
65300d60 1092 bt_object_put_ref(bool_obj);
dac5c838
PP
1093 return ret;
1094}
1095
d24d5663
PP
1096enum bt_value_map_insert_entry_status
1097bt_value_map_insert_unsigned_integer_entry(struct bt_value *map_obj,
1098 const char *key, uint64_t val)
fdd3a2da 1099{
d24d5663 1100 enum bt_value_map_insert_entry_status ret;
fdd3a2da
PP
1101 struct bt_value *integer_obj = NULL;
1102
1103 integer_obj = bt_value_unsigned_integer_create_init(val);
1104 ret = bt_value_map_insert_entry(map_obj, key,
1105 (void *) integer_obj);
1106 bt_object_put_ref(integer_obj);
1107 return ret;
1108}
1109
d24d5663
PP
1110enum bt_value_map_insert_entry_status
1111bt_value_map_insert_signed_integer_entry(struct bt_value *map_obj,
1112 const char *key, int64_t val)
dac5c838 1113{
d24d5663 1114 enum bt_value_map_insert_entry_status ret;
05e21286 1115 struct bt_value *integer_obj = NULL;
dac5c838 1116
fdd3a2da 1117 integer_obj = bt_value_signed_integer_create_init(val);
05e21286 1118 ret = bt_value_map_insert_entry(map_obj, key,
da91b29a 1119 (void *) integer_obj);
65300d60 1120 bt_object_put_ref(integer_obj);
dac5c838
PP
1121 return ret;
1122}
1123
d24d5663 1124enum bt_value_map_insert_entry_status bt_value_map_insert_real_entry(
05e21286 1125 struct bt_value *map_obj, const char *key, double val)
dac5c838 1126{
d24d5663 1127 enum bt_value_map_insert_entry_status ret;
05e21286 1128 struct bt_value *real_obj = NULL;
dac5c838 1129
05e21286
PP
1130 real_obj = bt_value_real_create_init(val);
1131 ret = bt_value_map_insert_entry(map_obj, key,
da91b29a 1132 (void *) real_obj);
65300d60 1133 bt_object_put_ref(real_obj);
dac5c838
PP
1134 return ret;
1135}
1136
d24d5663 1137enum bt_value_map_insert_entry_status bt_value_map_insert_string_entry(
05e21286 1138 struct bt_value *map_obj, const char *key,
601b0d3c 1139 const char *val)
dac5c838 1140{
d24d5663 1141 enum bt_value_map_insert_entry_status ret;
05e21286 1142 struct bt_value *string_obj = NULL;
dac5c838 1143
05e21286
PP
1144 string_obj = bt_value_string_create_init(val);
1145 ret = bt_value_map_insert_entry(map_obj, key,
da91b29a 1146 (void *) string_obj);
65300d60 1147 bt_object_put_ref(string_obj);
dac5c838
PP
1148 return ret;
1149}
1150
d24d5663
PP
1151enum bt_value_map_insert_entry_status
1152bt_value_map_insert_empty_array_entry(
05e21286 1153 struct bt_value *map_obj, const char *key)
dac5c838 1154{
d24d5663 1155 enum bt_value_map_insert_entry_status ret;
05e21286 1156 struct bt_value *array_obj = NULL;
dac5c838 1157
05e21286
PP
1158 array_obj = bt_value_array_create();
1159 ret = bt_value_map_insert_entry(map_obj, key,
da91b29a 1160 (void *) array_obj);
65300d60 1161 bt_object_put_ref(array_obj);
dac5c838
PP
1162 return ret;
1163}
1164
d24d5663
PP
1165enum bt_value_map_insert_entry_status
1166bt_value_map_insert_empty_map_entry(struct bt_value *map_obj, const char *key)
dac5c838 1167{
d24d5663 1168 enum bt_value_map_insert_entry_status ret;
05e21286 1169 struct bt_value *empty_map_obj = NULL;
dac5c838 1170
05e21286
PP
1171 empty_map_obj = bt_value_map_create();
1172 ret = bt_value_map_insert_entry(map_obj, key,
da91b29a 1173 (void *) empty_map_obj);
65300d60 1174 bt_object_put_ref(empty_map_obj);
dac5c838
PP
1175 return ret;
1176}
1177
d24d5663
PP
1178enum bt_value_map_foreach_entry_status bt_value_map_foreach_entry(
1179 struct bt_value *map_obj, bt_value_map_foreach_entry_func func,
1180 void *data)
dac5c838 1181{
d24d5663 1182 enum bt_value_map_foreach_entry_status ret = BT_FUNC_STATUS_OK;
dac5c838
PP
1183 gpointer key, element_obj;
1184 GHashTableIter iter;
1185 struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj);
1186
f6ccaed9 1187 BT_ASSERT_PRE_NON_NULL(map_obj, "Value object");
40f4ba76 1188 BT_ASSERT_PRE_NON_NULL(func, "Callback");
f6ccaed9 1189 BT_ASSERT_PRE_VALUE_IS_TYPE(map_obj, BT_VALUE_TYPE_MAP);
dac5c838
PP
1190 g_hash_table_iter_init(&iter, typed_map_obj->ght);
1191
1192 while (g_hash_table_iter_next(&iter, &key, &element_obj)) {
5b44aff2 1193 const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key));
dac5c838 1194
40f4ba76 1195 if (!func(key_str, element_obj, data)) {
ef267d12 1196 BT_LOGT("User canceled the loop: key=\"%s\", "
2f42aa0a
PP
1197 "value-addr=%p, data=%p",
1198 key_str, element_obj, data);
d24d5663 1199 ret = BT_FUNC_STATUS_CANCELED;
dac5c838
PP
1200 break;
1201 }
1202 }
1203
dac5c838
PP
1204 return ret;
1205}
1206
d24d5663 1207enum bt_value_map_foreach_entry_const_status bt_value_map_foreach_entry_const(
05e21286 1208 const struct bt_value *map_obj,
40f4ba76 1209 bt_value_map_foreach_entry_const_func func, void *data)
da91b29a 1210{
d24d5663 1211 return (int) bt_value_map_foreach_entry((void *) map_obj,
40f4ba76 1212 (bt_value_map_foreach_entry_func) func, data);
da91b29a
PP
1213}
1214
770750d3 1215struct extend_map_element_data {
05e21286 1216 struct bt_value *extended_obj;
d24d5663 1217 int status;
770750d3
PP
1218};
1219
1220static
c55a9f58 1221bt_bool extend_map_element(const char *key,
05e21286 1222 const struct bt_value *extension_obj_elem, void *data)
770750d3 1223{
c55a9f58 1224 bt_bool ret = BT_TRUE;
770750d3 1225 struct extend_map_element_data *extend_data = data;
05e21286 1226 struct bt_value *extension_obj_elem_copy = NULL;
770750d3
PP
1227
1228 /* Copy object which is to replace the current one */
6be5a99e 1229 extend_data->status = bt_value_copy(extension_obj_elem,
d24d5663 1230 &extension_obj_elem_copy);
601b0d3c
PP
1231 if (extend_data->status) {
1232 BT_LOGE("Cannot copy map element: addr=%p",
1233 extension_obj_elem);
1234 goto error;
1235 }
1236
1237 BT_ASSERT(extension_obj_elem_copy);
770750d3
PP
1238
1239 /* Replace in extended object */
05e21286 1240 extend_data->status = bt_value_map_insert_entry(
601b0d3c
PP
1241 extend_data->extended_obj, key,
1242 (void *) extension_obj_elem_copy);
1243 if (extend_data->status) {
2f42aa0a
PP
1244 BT_LOGE("Cannot replace value in extended value: key=\"%s\", "
1245 "extended-value-addr=%p, element-value-addr=%p",
1246 key, extend_data->extended_obj,
1247 extension_obj_elem_copy);
770750d3
PP
1248 goto error;
1249 }
1250
1251 goto end;
1252
1253error:
d24d5663 1254 BT_ASSERT(extend_data->status != BT_FUNC_STATUS_OK);
c55a9f58 1255 ret = BT_FALSE;
770750d3
PP
1256
1257end:
65300d60 1258 BT_OBJECT_PUT_REF_AND_RESET(extension_obj_elem_copy);
770750d3
PP
1259 return ret;
1260}
1261
d24d5663 1262enum bt_value_map_extend_status bt_value_map_extend(
70991d9f 1263 const struct bt_value *base_map_obj,
35294e83
PP
1264 const struct bt_value *extension_obj,
1265 struct bt_value **extended_map_obj)
770750d3 1266{
601b0d3c
PP
1267 struct extend_map_element_data extend_data = {
1268 .extended_obj = NULL,
d24d5663 1269 .status = BT_FUNC_STATUS_OK,
601b0d3c 1270 };
770750d3 1271
f6ccaed9
PP
1272 BT_ASSERT_PRE_NON_NULL(base_map_obj, "Base value object");
1273 BT_ASSERT_PRE_NON_NULL(extension_obj, "Extension value object");
601b0d3c
PP
1274 BT_ASSERT_PRE_NON_NULL(extended_map_obj,
1275 "Extended value object (output)");
f6ccaed9
PP
1276 BT_ASSERT_PRE_VALUE_IS_TYPE(base_map_obj, BT_VALUE_TYPE_MAP);
1277 BT_ASSERT_PRE_VALUE_IS_TYPE(extension_obj, BT_VALUE_TYPE_MAP);
2f42aa0a
PP
1278 BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p",
1279 base_map_obj, extension_obj);
601b0d3c 1280 *extended_map_obj = NULL;
2f42aa0a 1281
770750d3 1282 /* Create copy of base map object to start with */
6be5a99e 1283 extend_data.status = bt_value_copy(base_map_obj, extended_map_obj);
601b0d3c 1284 if (extend_data.status) {
2f42aa0a
PP
1285 BT_LOGE("Cannot copy base value: base-value-addr=%p",
1286 base_map_obj);
770750d3
PP
1287 goto error;
1288 }
1289
601b0d3c
PP
1290 BT_ASSERT(extended_map_obj);
1291
770750d3
PP
1292 /*
1293 * For each key in the extension map object, replace this key
1294 * in the copied map object.
1295 */
601b0d3c 1296 extend_data.extended_obj = *extended_map_obj;
770750d3 1297
05e21286 1298 if (bt_value_map_foreach_entry_const(extension_obj, extend_map_element,
770750d3 1299 &extend_data)) {
32e87ceb 1300 BT_LOGE("Cannot iterate on the extension object's elements: "
2f42aa0a 1301 "extension-value-addr=%p", extension_obj);
770750d3
PP
1302 goto error;
1303 }
1304
601b0d3c 1305 if (extend_data.status) {
32e87ceb 1306 BT_LOGE("Failed to successfully iterate on the extension object's elements: "
2f42aa0a 1307 "extension-value-addr=%p", extension_obj);
770750d3
PP
1308 goto error;
1309 }
1310
2f42aa0a 1311 BT_LOGD("Extended map value: extended-value-addr=%p",
601b0d3c 1312 *extended_map_obj);
770750d3
PP
1313 goto end;
1314
1315error:
601b0d3c
PP
1316 BT_OBJECT_PUT_REF_AND_RESET(*extended_map_obj);
1317 *extended_map_obj = NULL;
770750d3
PP
1318
1319end:
601b0d3c 1320 return extend_data.status;
770750d3
PP
1321}
1322
d24d5663 1323enum bt_value_copy_status bt_value_copy(const struct bt_value *object,
6be5a99e 1324 struct bt_value **copy_obj)
dac5c838 1325{
d24d5663 1326 enum bt_value_copy_status status = BT_FUNC_STATUS_OK;
dac5c838 1327
f6ccaed9 1328 BT_ASSERT_PRE_NON_NULL(object, "Value object");
601b0d3c 1329 BT_ASSERT_PRE_NON_NULL(copy_obj, "Value object copy (output)");
2f42aa0a 1330 BT_LOGD("Copying value object: addr=%p", object);
601b0d3c
PP
1331 *copy_obj = copy_funcs[object->type](object);
1332 if (*copy_obj) {
2f42aa0a
PP
1333 BT_LOGD("Copied value object: copy-value-addr=%p",
1334 copy_obj);
1335 } else {
d24d5663 1336 status = BT_FUNC_STATUS_MEMORY_ERROR;
601b0d3c 1337 *copy_obj = NULL;
2f42aa0a
PP
1338 BT_LOGE_STR("Failed to copy value object.");
1339 }
dac5c838 1340
601b0d3c 1341 return status;
dac5c838
PP
1342}
1343
c55a9f58 1344bt_bool bt_value_compare(const struct bt_value *object_a,
dac5c838
PP
1345 const struct bt_value *object_b)
1346{
c55a9f58 1347 bt_bool ret = BT_FALSE;
dac5c838 1348
f6ccaed9
PP
1349 BT_ASSERT_PRE_NON_NULL(object_a, "Value object A");
1350 BT_ASSERT_PRE_NON_NULL(object_b, "Value object B");
dac5c838
PP
1351
1352 if (object_a->type != object_b->type) {
ef267d12 1353 BT_LOGT("Values are different: type mismatch: "
2f42aa0a 1354 "value-a-addr=%p, value-b-addr=%p, "
c4628760 1355 "value-a-type=%s, value-b-type=%s",
2f42aa0a 1356 object_a, object_b,
da91b29a
PP
1357 bt_common_value_type_string(object_a->type),
1358 bt_common_value_type_string(object_b->type));
dac5c838
PP
1359 goto end;
1360 }
1361
1362 ret = compare_funcs[object_a->type](object_a, object_b);
1363
1364end:
1365 return ret;
1366}
c5b9b441
PP
1367
1368void bt_value_get_ref(const struct bt_value *value)
1369{
1370 bt_object_get_ref(value);
1371}
1372
1373void bt_value_put_ref(const struct bt_value *value)
1374{
1375 bt_object_put_ref(value);
1376}
This page took 0.122414 seconds and 4 git commands to generate.