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