Commit | Line | Data |
---|---|---|
dac5c838 | 1 | /* |
83509119 | 2 | * Values.c: value objects |
dac5c838 PP |
3 | * |
4 | * Babeltrace Library | |
5 | * | |
6 | * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation | |
7 | * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com> | |
8 | * | |
9 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
10 | * of this software and associated documentation files (the "Software"), to deal | |
11 | * in the Software without restriction, including without limitation the rights | |
12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
13 | * copies of the Software, and to permit persons to whom the Software is | |
14 | * furnished to do so, subject to the following conditions: | |
15 | * | |
16 | * The above copyright notice and this permission notice shall be included in | |
17 | * all copies or substantial portions of the Software. | |
18 | * | |
19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
25 | * SOFTWARE. | |
26 | */ | |
27 | ||
28 | #include <stdlib.h> | |
29 | #include <string.h> | |
30 | #include <assert.h> | |
31 | #include <string.h> | |
2f42aa0a | 32 | #include <inttypes.h> |
3d9990ac | 33 | #include <babeltrace/compiler-internal.h> |
83509119 JG |
34 | #include <babeltrace/object-internal.h> |
35 | #include <babeltrace/ref.h> | |
dac5c838 | 36 | #include <babeltrace/values.h> |
3d9990ac | 37 | #include <babeltrace/compat/glib-internal.h> |
dac5c838 | 38 | |
2f42aa0a PP |
39 | #define BT_LOG_TAG "VALUES" |
40 | #include <babeltrace/lib-logging-internal.h> | |
41 | ||
dac5c838 PP |
42 | #define BT_VALUE_FROM_CONCRETE(_concrete) ((struct bt_value *) (_concrete)) |
43 | #define BT_VALUE_TO_BOOL(_base) ((struct bt_value_bool *) (_base)) | |
44 | #define BT_VALUE_TO_INTEGER(_base) ((struct bt_value_integer *) (_base)) | |
45 | #define BT_VALUE_TO_FLOAT(_base) ((struct bt_value_float *) (_base)) | |
46 | #define BT_VALUE_TO_STRING(_base) ((struct bt_value_string *) (_base)) | |
47 | #define BT_VALUE_TO_ARRAY(_base) ((struct bt_value_array *) (_base)) | |
48 | #define BT_VALUE_TO_MAP(_base) ((struct bt_value_map *) (_base)) | |
49 | ||
50 | struct bt_value { | |
83509119 | 51 | struct bt_object base; |
dac5c838 | 52 | enum bt_value_type type; |
dac5c838 PP |
53 | bool is_frozen; |
54 | }; | |
55 | ||
56 | static | |
57 | struct bt_value bt_value_null_instance = { | |
f685129c PP |
58 | .base = { |
59 | .ref_count = { | |
60 | .count = 1, | |
61 | .release = NULL, | |
62 | }, | |
63 | .release = NULL, | |
64 | .parent = NULL, | |
65 | }, | |
dac5c838 PP |
66 | .type = BT_VALUE_TYPE_NULL, |
67 | .is_frozen = true, | |
68 | }; | |
69 | ||
70 | struct bt_value *bt_value_null = &bt_value_null_instance; | |
71 | ||
72 | struct bt_value_bool { | |
73 | struct bt_value base; | |
74 | bool value; | |
75 | }; | |
76 | ||
77 | struct bt_value_integer { | |
78 | struct bt_value base; | |
79 | int64_t value; | |
80 | }; | |
81 | ||
82 | struct bt_value_float { | |
83 | struct bt_value base; | |
84 | double value; | |
85 | }; | |
86 | ||
87 | struct bt_value_string { | |
88 | struct bt_value base; | |
89 | GString *gstr; | |
90 | }; | |
91 | ||
92 | struct bt_value_array { | |
93 | struct bt_value base; | |
94 | GPtrArray *garray; | |
95 | }; | |
96 | ||
97 | struct bt_value_map { | |
98 | struct bt_value base; | |
99 | GHashTable *ght; | |
100 | }; | |
101 | ||
102 | static | |
83509119 | 103 | void bt_value_destroy(struct bt_object *obj); |
dac5c838 PP |
104 | |
105 | static | |
106 | void bt_value_string_destroy(struct bt_value *object) | |
107 | { | |
108 | g_string_free(BT_VALUE_TO_STRING(object)->gstr, TRUE); | |
109 | } | |
110 | ||
111 | static | |
112 | void bt_value_array_destroy(struct bt_value *object) | |
113 | { | |
114 | /* | |
115 | * Pointer array's registered value destructor will take care | |
116 | * of putting each contained object. | |
117 | */ | |
118 | g_ptr_array_free(BT_VALUE_TO_ARRAY(object)->garray, TRUE); | |
119 | } | |
120 | ||
121 | static | |
122 | void bt_value_map_destroy(struct bt_value *object) | |
123 | { | |
124 | /* | |
125 | * Hash table's registered value destructor will take care of | |
126 | * putting each contained object. Keys are GQuarks and cannot | |
127 | * be destroyed anyway. | |
128 | */ | |
129 | g_hash_table_destroy(BT_VALUE_TO_MAP(object)->ght); | |
130 | } | |
131 | ||
132 | static | |
133 | void (* const destroy_funcs[])(struct bt_value *) = { | |
134 | [BT_VALUE_TYPE_NULL] = NULL, | |
135 | [BT_VALUE_TYPE_BOOL] = NULL, | |
136 | [BT_VALUE_TYPE_INTEGER] = NULL, | |
137 | [BT_VALUE_TYPE_FLOAT] = NULL, | |
138 | [BT_VALUE_TYPE_STRING] = bt_value_string_destroy, | |
139 | [BT_VALUE_TYPE_ARRAY] = bt_value_array_destroy, | |
140 | [BT_VALUE_TYPE_MAP] = bt_value_map_destroy, | |
141 | }; | |
142 | ||
143 | static | |
144 | struct bt_value *bt_value_null_copy(const struct bt_value *null_obj) | |
145 | { | |
146 | return bt_value_null; | |
147 | } | |
148 | ||
149 | static | |
150 | struct bt_value *bt_value_bool_copy(const struct bt_value *bool_obj) | |
151 | { | |
152 | return bt_value_bool_create_init(BT_VALUE_TO_BOOL(bool_obj)->value); | |
153 | } | |
154 | ||
155 | static | |
156 | struct bt_value *bt_value_integer_copy(const struct bt_value *integer_obj) | |
157 | { | |
158 | return bt_value_integer_create_init( | |
159 | BT_VALUE_TO_INTEGER(integer_obj)->value); | |
160 | } | |
161 | ||
162 | static | |
163 | struct bt_value *bt_value_float_copy(const struct bt_value *float_obj) | |
164 | { | |
165 | return bt_value_float_create_init( | |
166 | BT_VALUE_TO_FLOAT(float_obj)->value); | |
167 | } | |
168 | ||
169 | static | |
170 | struct bt_value *bt_value_string_copy(const struct bt_value *string_obj) | |
171 | { | |
172 | return bt_value_string_create_init( | |
173 | BT_VALUE_TO_STRING(string_obj)->gstr->str); | |
174 | } | |
175 | ||
176 | static | |
177 | struct bt_value *bt_value_array_copy(const struct bt_value *array_obj) | |
178 | { | |
179 | int i; | |
180 | int ret; | |
181 | struct bt_value *copy_obj; | |
182 | struct bt_value_array *typed_array_obj; | |
183 | ||
2f42aa0a | 184 | BT_LOGD("Copying array value: addr=%p", array_obj); |
dac5c838 PP |
185 | typed_array_obj = BT_VALUE_TO_ARRAY(array_obj); |
186 | copy_obj = bt_value_array_create(); | |
187 | ||
188 | if (!copy_obj) { | |
2f42aa0a | 189 | BT_LOGE_STR("Cannot create empty array value."); |
dac5c838 PP |
190 | goto end; |
191 | } | |
192 | ||
193 | for (i = 0; i < typed_array_obj->garray->len; ++i) { | |
194 | struct bt_value *element_obj_copy; | |
195 | struct bt_value *element_obj = bt_value_array_get(array_obj, i); | |
196 | ||
197 | if (!element_obj) { | |
2f42aa0a PP |
198 | BT_LOGE("Cannot get array value's element: " |
199 | "addr=%p, index=%d", | |
200 | array_obj, i); | |
83509119 | 201 | BT_PUT(copy_obj); |
dac5c838 PP |
202 | goto end; |
203 | } | |
204 | ||
205 | element_obj_copy = bt_value_copy(element_obj); | |
83509119 | 206 | BT_PUT(element_obj); |
dac5c838 PP |
207 | |
208 | if (!element_obj_copy) { | |
2f42aa0a PP |
209 | BT_LOGE("Cannot copy array value's element: " |
210 | "array-addr=%p, index=%d", | |
211 | array_obj, i); | |
83509119 | 212 | BT_PUT(copy_obj); |
dac5c838 PP |
213 | goto end; |
214 | } | |
215 | ||
216 | ret = bt_value_array_append(copy_obj, element_obj_copy); | |
83509119 | 217 | BT_PUT(element_obj_copy); |
dac5c838 PP |
218 | |
219 | if (ret) { | |
2f42aa0a PP |
220 | BT_LOGE("Cannot append to array value: addr=%p", |
221 | array_obj); | |
83509119 | 222 | BT_PUT(copy_obj); |
dac5c838 PP |
223 | goto end; |
224 | } | |
225 | } | |
226 | ||
2f42aa0a PP |
227 | BT_LOGD("Copied array value: addr=%p", array_obj); |
228 | ||
dac5c838 PP |
229 | end: |
230 | return copy_obj; | |
231 | } | |
232 | ||
233 | static | |
234 | struct bt_value *bt_value_map_copy(const struct bt_value *map_obj) | |
235 | { | |
236 | int ret; | |
237 | GHashTableIter iter; | |
238 | gpointer key, element_obj; | |
239 | struct bt_value *copy_obj; | |
240 | struct bt_value *element_obj_copy; | |
241 | struct bt_value_map *typed_map_obj; | |
242 | ||
2f42aa0a | 243 | BT_LOGD("Copying map value: addr=%p", map_obj); |
dac5c838 PP |
244 | typed_map_obj = BT_VALUE_TO_MAP(map_obj); |
245 | copy_obj = bt_value_map_create(); | |
246 | ||
247 | if (!copy_obj) { | |
248 | goto end; | |
249 | } | |
250 | ||
251 | g_hash_table_iter_init(&iter, typed_map_obj->ght); | |
252 | ||
253 | while (g_hash_table_iter_next(&iter, &key, &element_obj)) { | |
5b44aff2 | 254 | const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key)); |
dac5c838 PP |
255 | |
256 | element_obj_copy = bt_value_copy(element_obj); | |
257 | ||
258 | if (!element_obj_copy) { | |
2f42aa0a PP |
259 | BT_LOGE("Cannot copy map value's element: " |
260 | "map-addr=%p, key=\"%s\"", | |
261 | map_obj, key_str); | |
83509119 | 262 | BT_PUT(copy_obj); |
dac5c838 PP |
263 | goto end; |
264 | } | |
265 | ||
266 | ret = bt_value_map_insert(copy_obj, key_str, element_obj_copy); | |
83509119 | 267 | BT_PUT(element_obj_copy); |
dac5c838 PP |
268 | |
269 | if (ret) { | |
2f42aa0a PP |
270 | BT_LOGE("Cannot insert into map value: addr=%p, key=\"%s\"", |
271 | map_obj, key_str); | |
83509119 | 272 | BT_PUT(copy_obj); |
dac5c838 PP |
273 | goto end; |
274 | } | |
275 | } | |
276 | ||
2f42aa0a PP |
277 | BT_LOGD("Copied map value: addr=%p", map_obj); |
278 | ||
dac5c838 PP |
279 | end: |
280 | return copy_obj; | |
281 | } | |
282 | ||
283 | static | |
284 | struct bt_value *(* const copy_funcs[])(const struct bt_value *) = { | |
285 | [BT_VALUE_TYPE_NULL] = bt_value_null_copy, | |
286 | [BT_VALUE_TYPE_BOOL] = bt_value_bool_copy, | |
287 | [BT_VALUE_TYPE_INTEGER] = bt_value_integer_copy, | |
288 | [BT_VALUE_TYPE_FLOAT] = bt_value_float_copy, | |
289 | [BT_VALUE_TYPE_STRING] = bt_value_string_copy, | |
290 | [BT_VALUE_TYPE_ARRAY] = bt_value_array_copy, | |
291 | [BT_VALUE_TYPE_MAP] = bt_value_map_copy, | |
292 | }; | |
293 | ||
294 | static | |
295 | bool bt_value_null_compare(const struct bt_value *object_a, | |
296 | const struct bt_value *object_b) | |
297 | { | |
298 | /* | |
299 | * Always true since bt_value_compare() already checks if both | |
300 | * object_a and object_b have the same type, and in the case of | |
301 | * null value objects, they're always the same if it is so. | |
302 | */ | |
303 | return true; | |
304 | } | |
305 | ||
306 | static | |
307 | bool bt_value_bool_compare(const struct bt_value *object_a, | |
308 | const struct bt_value *object_b) | |
309 | { | |
310 | return BT_VALUE_TO_BOOL(object_a)->value == | |
311 | BT_VALUE_TO_BOOL(object_b)->value; | |
312 | } | |
313 | ||
314 | static | |
315 | bool bt_value_integer_compare(const struct bt_value *object_a, | |
316 | const struct bt_value *object_b) | |
317 | { | |
318 | return BT_VALUE_TO_INTEGER(object_a)->value == | |
319 | BT_VALUE_TO_INTEGER(object_b)->value; | |
320 | } | |
321 | ||
322 | static | |
323 | bool bt_value_float_compare(const struct bt_value *object_a, | |
324 | const struct bt_value *object_b) | |
325 | { | |
326 | return BT_VALUE_TO_FLOAT(object_a)->value == | |
327 | BT_VALUE_TO_FLOAT(object_b)->value; | |
328 | } | |
329 | ||
330 | static | |
331 | bool bt_value_string_compare(const struct bt_value *object_a, | |
332 | const struct bt_value *object_b) | |
333 | { | |
334 | return !strcmp(BT_VALUE_TO_STRING(object_a)->gstr->str, | |
335 | BT_VALUE_TO_STRING(object_b)->gstr->str); | |
336 | } | |
337 | ||
338 | static | |
339 | bool bt_value_array_compare(const struct bt_value *object_a, | |
340 | const struct bt_value *object_b) | |
341 | { | |
342 | int i; | |
343 | bool ret = true; | |
344 | const struct bt_value_array *array_obj_a = | |
345 | BT_VALUE_TO_ARRAY(object_a); | |
346 | ||
347 | if (bt_value_array_size(object_a) != bt_value_array_size(object_b)) { | |
2f42aa0a PP |
348 | BT_LOGV("Array values are different: size mismatch " |
349 | "value-a-addr=%p, value-b-addr=%p, " | |
350 | "value-a-size=%" PRId64 ", value-b-size=%" PRId64, | |
351 | object_a, object_b, | |
352 | bt_value_array_size(object_a), | |
353 | bt_value_array_size(object_b)); | |
dac5c838 PP |
354 | ret = false; |
355 | goto end; | |
356 | } | |
357 | ||
358 | for (i = 0; i < array_obj_a->garray->len; ++i) { | |
359 | struct bt_value *element_obj_a; | |
360 | struct bt_value *element_obj_b; | |
361 | ||
362 | element_obj_a = bt_value_array_get(object_a, i); | |
363 | element_obj_b = bt_value_array_get(object_b, i); | |
364 | ||
365 | if (!bt_value_compare(element_obj_a, element_obj_b)) { | |
2f42aa0a PP |
366 | BT_LOGV("Array values's elements are different: " |
367 | "value-a-addr=%p, value-b-addr=%p, index=%d", | |
368 | element_obj_a, element_obj_b, index); | |
83509119 JG |
369 | BT_PUT(element_obj_a); |
370 | BT_PUT(element_obj_b); | |
dac5c838 PP |
371 | ret = false; |
372 | goto end; | |
373 | } | |
374 | ||
83509119 JG |
375 | BT_PUT(element_obj_a); |
376 | BT_PUT(element_obj_b); | |
dac5c838 PP |
377 | } |
378 | ||
379 | end: | |
380 | return ret; | |
381 | } | |
382 | ||
383 | static | |
384 | bool bt_value_map_compare(const struct bt_value *object_a, | |
385 | const struct bt_value *object_b) | |
386 | { | |
387 | bool ret = true; | |
388 | GHashTableIter iter; | |
389 | gpointer key, element_obj_a; | |
390 | const struct bt_value_map *map_obj_a = BT_VALUE_TO_MAP(object_a); | |
391 | ||
392 | if (bt_value_map_size(object_a) != bt_value_map_size(object_b)) { | |
2f42aa0a PP |
393 | BT_LOGV("Map values are different: size mismatch " |
394 | "value-a-addr=%p, value-b-addr=%p, " | |
395 | "value-a-size=%" PRId64 ", value-b-size=%" PRId64, | |
396 | object_a, object_b, | |
397 | bt_value_map_size(object_a), | |
398 | bt_value_map_size(object_b)); | |
dac5c838 PP |
399 | ret = false; |
400 | goto end; | |
401 | } | |
402 | ||
403 | g_hash_table_iter_init(&iter, map_obj_a->ght); | |
404 | ||
405 | while (g_hash_table_iter_next(&iter, &key, &element_obj_a)) { | |
406 | struct bt_value *element_obj_b; | |
5b44aff2 | 407 | const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key)); |
dac5c838 PP |
408 | |
409 | element_obj_b = bt_value_map_get(object_b, key_str); | |
410 | ||
411 | if (!bt_value_compare(element_obj_a, element_obj_b)) { | |
2f42aa0a PP |
412 | BT_LOGV("Map values's elements are different: " |
413 | "value-a-addr=%p, value-b-addr=%p, key=\"%s\"", | |
414 | element_obj_a, element_obj_b, key_str); | |
83509119 | 415 | BT_PUT(element_obj_b); |
dac5c838 PP |
416 | ret = false; |
417 | goto end; | |
418 | } | |
419 | ||
83509119 | 420 | BT_PUT(element_obj_b); |
dac5c838 PP |
421 | } |
422 | ||
423 | end: | |
424 | return ret; | |
425 | } | |
426 | ||
427 | static | |
428 | bool (* const compare_funcs[])(const struct bt_value *, | |
429 | const struct bt_value *) = { | |
430 | [BT_VALUE_TYPE_NULL] = bt_value_null_compare, | |
431 | [BT_VALUE_TYPE_BOOL] = bt_value_bool_compare, | |
432 | [BT_VALUE_TYPE_INTEGER] = bt_value_integer_compare, | |
433 | [BT_VALUE_TYPE_FLOAT] = bt_value_float_compare, | |
434 | [BT_VALUE_TYPE_STRING] = bt_value_string_compare, | |
435 | [BT_VALUE_TYPE_ARRAY] = bt_value_array_compare, | |
436 | [BT_VALUE_TYPE_MAP] = bt_value_map_compare, | |
437 | }; | |
438 | ||
439 | void bt_value_null_freeze(struct bt_value *object) | |
440 | { | |
441 | } | |
442 | ||
443 | void bt_value_generic_freeze(struct bt_value *object) | |
444 | { | |
445 | object->is_frozen = true; | |
446 | } | |
447 | ||
448 | void bt_value_array_freeze(struct bt_value *object) | |
449 | { | |
450 | int i; | |
451 | struct bt_value_array *typed_array_obj = | |
452 | BT_VALUE_TO_ARRAY(object); | |
453 | ||
454 | for (i = 0; i < typed_array_obj->garray->len; ++i) { | |
455 | struct bt_value *element_obj = | |
456 | g_ptr_array_index(typed_array_obj->garray, i); | |
457 | ||
458 | bt_value_freeze(element_obj); | |
459 | } | |
460 | ||
461 | bt_value_generic_freeze(object); | |
462 | } | |
463 | ||
464 | void bt_value_map_freeze(struct bt_value *object) | |
465 | { | |
466 | GHashTableIter iter; | |
467 | gpointer key, element_obj; | |
468 | const struct bt_value_map *map_obj = BT_VALUE_TO_MAP(object); | |
469 | ||
470 | g_hash_table_iter_init(&iter, map_obj->ght); | |
471 | ||
472 | while (g_hash_table_iter_next(&iter, &key, &element_obj)) { | |
473 | bt_value_freeze(element_obj); | |
474 | } | |
475 | ||
476 | bt_value_generic_freeze(object); | |
477 | } | |
478 | ||
479 | static | |
480 | void (* const freeze_funcs[])(struct bt_value *) = { | |
481 | [BT_VALUE_TYPE_NULL] = bt_value_null_freeze, | |
482 | [BT_VALUE_TYPE_BOOL] = bt_value_generic_freeze, | |
483 | [BT_VALUE_TYPE_INTEGER] = bt_value_generic_freeze, | |
484 | [BT_VALUE_TYPE_FLOAT] = bt_value_generic_freeze, | |
485 | [BT_VALUE_TYPE_STRING] = bt_value_generic_freeze, | |
486 | [BT_VALUE_TYPE_ARRAY] = bt_value_array_freeze, | |
487 | [BT_VALUE_TYPE_MAP] = bt_value_map_freeze, | |
488 | }; | |
489 | ||
490 | static | |
83509119 | 491 | void bt_value_destroy(struct bt_object *obj) |
dac5c838 | 492 | { |
83509119 | 493 | struct bt_value *value; |
dac5c838 | 494 | |
83509119 JG |
495 | value = container_of(obj, struct bt_value, base); |
496 | assert(value->type != BT_VALUE_TYPE_UNKNOWN); | |
dac5c838 | 497 | |
2f42aa0a PP |
498 | BT_LOGD("Destroying value: addr=%p", value); |
499 | ||
83509119 | 500 | if (bt_value_is_null(value)) { |
2f42aa0a | 501 | BT_LOGD_STR("Not destroying the null value singleton."); |
dac5c838 PP |
502 | return; |
503 | } | |
504 | ||
83509119 JG |
505 | if (destroy_funcs[value->type]) { |
506 | destroy_funcs[value->type](value); | |
dac5c838 PP |
507 | } |
508 | ||
83509119 | 509 | g_free(value); |
dac5c838 PP |
510 | } |
511 | ||
512 | enum bt_value_status bt_value_freeze(struct bt_value *object) | |
513 | { | |
514 | enum bt_value_status ret = BT_VALUE_STATUS_OK; | |
515 | ||
516 | if (!object) { | |
2f42aa0a | 517 | BT_LOGW_STR("Invalid parameter: value object is NULL."); |
dac5c838 PP |
518 | ret = BT_VALUE_STATUS_INVAL; |
519 | goto end; | |
520 | } | |
521 | ||
2f42aa0a PP |
522 | if (object->is_frozen) { |
523 | goto end; | |
524 | } | |
525 | ||
526 | BT_LOGD("Freezing value: addr=%p", object); | |
dac5c838 PP |
527 | freeze_funcs[object->type](object); |
528 | ||
529 | end: | |
530 | return ret; | |
531 | } | |
532 | ||
533 | bool bt_value_is_frozen(const struct bt_value *object) | |
534 | { | |
535 | return object && object->is_frozen; | |
536 | } | |
537 | ||
538 | enum bt_value_type bt_value_get_type(const struct bt_value *object) | |
539 | { | |
540 | if (!object) { | |
2f42aa0a | 541 | BT_LOGW_STR("Invalid parameter: value object is NULL."); |
dac5c838 PP |
542 | return BT_VALUE_TYPE_UNKNOWN; |
543 | } | |
544 | ||
545 | return object->type; | |
546 | } | |
547 | ||
548 | static | |
549 | struct bt_value bt_value_create_base(enum bt_value_type type) | |
550 | { | |
551 | struct bt_value base; | |
552 | ||
553 | base.type = type; | |
554 | base.is_frozen = false; | |
83509119 | 555 | bt_object_init(&base, bt_value_destroy); |
dac5c838 PP |
556 | return base; |
557 | } | |
558 | ||
559 | struct bt_value *bt_value_bool_create_init(bool val) | |
560 | { | |
561 | struct bt_value_bool *bool_obj; | |
562 | ||
2f42aa0a | 563 | BT_LOGD("Creating boolean value object: val=%d", val); |
dac5c838 PP |
564 | bool_obj = g_new0(struct bt_value_bool, 1); |
565 | ||
566 | if (!bool_obj) { | |
2f42aa0a | 567 | BT_LOGE_STR("Failed to allocate one boolean value object."); |
dac5c838 PP |
568 | goto end; |
569 | } | |
570 | ||
571 | bool_obj->base = bt_value_create_base(BT_VALUE_TYPE_BOOL); | |
572 | bool_obj->value = val; | |
2f42aa0a | 573 | BT_LOGD("Created boolean value object: addr=%p", bool_obj); |
dac5c838 PP |
574 | |
575 | end: | |
576 | return BT_VALUE_FROM_CONCRETE(bool_obj); | |
577 | } | |
578 | ||
579 | struct bt_value *bt_value_bool_create(void) | |
580 | { | |
581 | return bt_value_bool_create_init(false); | |
582 | } | |
583 | ||
584 | struct bt_value *bt_value_integer_create_init(int64_t val) | |
585 | { | |
586 | struct bt_value_integer *integer_obj; | |
587 | ||
2f42aa0a | 588 | BT_LOGD("Creating integer value object: val=%" PRId64, val); |
dac5c838 PP |
589 | integer_obj = g_new0(struct bt_value_integer, 1); |
590 | ||
591 | if (!integer_obj) { | |
2f42aa0a | 592 | BT_LOGE_STR("Failed to allocate one integer value object."); |
dac5c838 PP |
593 | goto end; |
594 | } | |
595 | ||
596 | integer_obj->base = bt_value_create_base(BT_VALUE_TYPE_INTEGER); | |
597 | integer_obj->value = val; | |
2f42aa0a PP |
598 | BT_LOGD("Created integer value object: addr=%p", |
599 | integer_obj); | |
dac5c838 PP |
600 | |
601 | end: | |
602 | return BT_VALUE_FROM_CONCRETE(integer_obj); | |
603 | } | |
604 | ||
605 | struct bt_value *bt_value_integer_create(void) | |
606 | { | |
607 | return bt_value_integer_create_init(0); | |
608 | } | |
609 | ||
610 | struct bt_value *bt_value_float_create_init(double val) | |
611 | { | |
612 | struct bt_value_float *float_obj; | |
613 | ||
2f42aa0a | 614 | BT_LOGD("Creating floating point number value object: val=%f", val); |
dac5c838 PP |
615 | float_obj = g_new0(struct bt_value_float, 1); |
616 | ||
617 | if (!float_obj) { | |
2f42aa0a | 618 | BT_LOGE_STR("Failed to allocate one floating point number value object."); |
dac5c838 PP |
619 | goto end; |
620 | } | |
621 | ||
622 | float_obj->base = bt_value_create_base(BT_VALUE_TYPE_FLOAT); | |
623 | float_obj->value = val; | |
2f42aa0a PP |
624 | BT_LOGD("Created floating point number value object: addr=%p", |
625 | float_obj); | |
dac5c838 PP |
626 | |
627 | end: | |
628 | return BT_VALUE_FROM_CONCRETE(float_obj); | |
629 | } | |
630 | ||
631 | struct bt_value *bt_value_float_create(void) | |
632 | { | |
633 | return bt_value_float_create_init(0.); | |
634 | } | |
635 | ||
636 | struct bt_value *bt_value_string_create_init(const char *val) | |
637 | { | |
638 | struct bt_value_string *string_obj = NULL; | |
639 | ||
640 | if (!val) { | |
2f42aa0a | 641 | BT_LOGW_STR("Invalid parameter: value is NULL."); |
dac5c838 PP |
642 | goto end; |
643 | } | |
644 | ||
2f42aa0a | 645 | BT_LOGD("Creating string value object: val-len=%u", strlen(val)); |
dac5c838 PP |
646 | string_obj = g_new0(struct bt_value_string, 1); |
647 | ||
648 | if (!string_obj) { | |
2f42aa0a | 649 | BT_LOGE_STR("Failed to allocate one string object."); |
dac5c838 PP |
650 | goto end; |
651 | } | |
652 | ||
653 | string_obj->base = bt_value_create_base(BT_VALUE_TYPE_STRING); | |
654 | string_obj->gstr = g_string_new(val); | |
655 | ||
656 | if (!string_obj->gstr) { | |
2f42aa0a | 657 | BT_LOGE_STR("Failed to allocate a GString."); |
dac5c838 PP |
658 | g_free(string_obj); |
659 | string_obj = NULL; | |
660 | goto end; | |
661 | } | |
662 | ||
2f42aa0a PP |
663 | BT_LOGD("Created string value object: addr=%p", |
664 | string_obj); | |
665 | ||
dac5c838 PP |
666 | end: |
667 | return BT_VALUE_FROM_CONCRETE(string_obj); | |
668 | } | |
669 | ||
670 | struct bt_value *bt_value_string_create(void) | |
671 | { | |
672 | return bt_value_string_create_init(""); | |
673 | } | |
674 | ||
675 | struct bt_value *bt_value_array_create(void) | |
676 | { | |
677 | struct bt_value_array *array_obj; | |
678 | ||
2f42aa0a | 679 | BT_LOGD_STR("Creating empty array value object."); |
dac5c838 PP |
680 | array_obj = g_new0(struct bt_value_array, 1); |
681 | ||
682 | if (!array_obj) { | |
2f42aa0a | 683 | BT_LOGE_STR("Failed to allocate one array object."); |
dac5c838 PP |
684 | goto end; |
685 | } | |
686 | ||
687 | array_obj->base = bt_value_create_base(BT_VALUE_TYPE_ARRAY); | |
5d5982ab | 688 | array_obj->garray = bt_g_ptr_array_new_full(0, |
83509119 | 689 | (GDestroyNotify) bt_put); |
dac5c838 PP |
690 | |
691 | if (!array_obj->garray) { | |
2f42aa0a | 692 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
dac5c838 PP |
693 | g_free(array_obj); |
694 | array_obj = NULL; | |
695 | goto end; | |
696 | } | |
697 | ||
2f42aa0a PP |
698 | BT_LOGD("Created array value object: addr=%p", |
699 | array_obj); | |
700 | ||
dac5c838 PP |
701 | end: |
702 | return BT_VALUE_FROM_CONCRETE(array_obj); | |
703 | } | |
704 | ||
705 | struct bt_value *bt_value_map_create(void) | |
706 | { | |
707 | struct bt_value_map *map_obj; | |
708 | ||
2f42aa0a | 709 | BT_LOGD_STR("Creating empty map value object."); |
dac5c838 PP |
710 | map_obj = g_new0(struct bt_value_map, 1); |
711 | ||
712 | if (!map_obj) { | |
2f42aa0a | 713 | BT_LOGE_STR("Failed to allocate one map object."); |
dac5c838 PP |
714 | goto end; |
715 | } | |
716 | ||
717 | map_obj->base = bt_value_create_base(BT_VALUE_TYPE_MAP); | |
718 | map_obj->ght = g_hash_table_new_full(g_direct_hash, g_direct_equal, | |
83509119 | 719 | NULL, (GDestroyNotify) bt_put); |
dac5c838 PP |
720 | |
721 | if (!map_obj->ght) { | |
2f42aa0a | 722 | BT_LOGE_STR("Failed to allocate a GHashTable."); |
dac5c838 PP |
723 | g_free(map_obj); |
724 | map_obj = NULL; | |
725 | goto end; | |
726 | } | |
727 | ||
2f42aa0a PP |
728 | BT_LOGD("Created map value object: addr=%p", |
729 | map_obj); | |
730 | ||
dac5c838 PP |
731 | end: |
732 | return BT_VALUE_FROM_CONCRETE(map_obj); | |
733 | } | |
734 | ||
735 | enum bt_value_status bt_value_bool_get(const struct bt_value *bool_obj, | |
364747d6 | 736 | bool *val) |
dac5c838 PP |
737 | { |
738 | enum bt_value_status ret = BT_VALUE_STATUS_OK; | |
739 | struct bt_value_bool *typed_bool_obj = BT_VALUE_TO_BOOL(bool_obj); | |
740 | ||
2f42aa0a PP |
741 | if (!bool_obj || !val) { |
742 | BT_LOGW("Invalid parameter: value object or value is NULL: " | |
743 | "value-addr=%p, raw-value-addr=%p", | |
744 | bool_obj, val); | |
745 | ret = BT_VALUE_STATUS_INVAL; | |
746 | goto end; | |
747 | } | |
748 | ||
749 | if (!bt_value_is_bool(bool_obj)) { | |
750 | BT_LOGW("Invalid parameter: value is not a boolean value: addr=%p, " | |
751 | "type=%d", bool_obj, bool_obj->type); | |
dac5c838 PP |
752 | ret = BT_VALUE_STATUS_INVAL; |
753 | goto end; | |
754 | } | |
755 | ||
756 | *val = typed_bool_obj->value; | |
757 | ||
758 | end: | |
759 | return ret; | |
760 | } | |
761 | ||
762 | enum bt_value_status bt_value_bool_set(struct bt_value *bool_obj, bool val) | |
763 | { | |
764 | enum bt_value_status ret = BT_VALUE_STATUS_OK; | |
765 | struct bt_value_bool *typed_bool_obj = BT_VALUE_TO_BOOL(bool_obj); | |
766 | ||
2f42aa0a PP |
767 | if (!bool_obj) { |
768 | BT_LOGW_STR("Invalid parameter: value object is NULL."); | |
769 | ret = BT_VALUE_STATUS_INVAL; | |
770 | goto end; | |
771 | } | |
772 | ||
773 | if (!bt_value_is_bool(bool_obj)) { | |
774 | BT_LOGW("Invalid parameter: value is not a boolean value: addr=%p, " | |
775 | "type=%d", bool_obj, bool_obj->type); | |
dac5c838 PP |
776 | ret = BT_VALUE_STATUS_INVAL; |
777 | goto end; | |
778 | } | |
779 | ||
780 | if (bool_obj->is_frozen) { | |
2f42aa0a PP |
781 | BT_LOGW("Invalid parameter: value is frozen: addr=%p", |
782 | bool_obj); | |
dac5c838 PP |
783 | ret = BT_VALUE_STATUS_FROZEN; |
784 | goto end; | |
785 | } | |
786 | ||
787 | typed_bool_obj->value = val; | |
2f42aa0a PP |
788 | BT_LOGV("Set boolean value's raw value: value-addr=%p, value=%d", |
789 | bool_obj, val); | |
dac5c838 PP |
790 | |
791 | end: | |
792 | return ret; | |
793 | } | |
794 | ||
795 | enum bt_value_status bt_value_integer_get(const struct bt_value *integer_obj, | |
364747d6 | 796 | int64_t *val) |
dac5c838 PP |
797 | { |
798 | enum bt_value_status ret = BT_VALUE_STATUS_OK; | |
799 | struct bt_value_integer *typed_integer_obj = | |
800 | BT_VALUE_TO_INTEGER(integer_obj); | |
801 | ||
2f42aa0a PP |
802 | if (!integer_obj || !val) { |
803 | BT_LOGW("Invalid parameter: value object or value is NULL: " | |
804 | "value-addr=%p, raw-value-addr=%p", | |
805 | integer_obj, val); | |
806 | ret = BT_VALUE_STATUS_INVAL; | |
807 | goto end; | |
808 | } | |
809 | ||
810 | if (!bt_value_is_integer(integer_obj)) { | |
811 | BT_LOGW("Invalid parameter: value is not an integer value: addr=%p, " | |
812 | "type=%d", integer_obj, integer_obj->type); | |
dac5c838 PP |
813 | ret = BT_VALUE_STATUS_INVAL; |
814 | goto end; | |
815 | } | |
816 | ||
817 | *val = typed_integer_obj->value; | |
818 | ||
819 | end: | |
820 | return ret; | |
821 | } | |
822 | ||
823 | enum bt_value_status bt_value_integer_set(struct bt_value *integer_obj, | |
364747d6 | 824 | int64_t val) |
dac5c838 PP |
825 | { |
826 | enum bt_value_status ret = BT_VALUE_STATUS_OK; | |
827 | struct bt_value_integer *typed_integer_obj = | |
828 | BT_VALUE_TO_INTEGER(integer_obj); | |
829 | ||
2f42aa0a PP |
830 | if (!integer_obj) { |
831 | BT_LOGW_STR("Invalid parameter: value object is NULL."); | |
832 | ret = BT_VALUE_STATUS_INVAL; | |
833 | goto end; | |
834 | } | |
835 | ||
836 | if (!bt_value_is_integer(integer_obj)) { | |
837 | BT_LOGW("Invalid parameter: value is not an integer value: addr=%p, " | |
838 | "type=%d", integer_obj, integer_obj->type); | |
dac5c838 PP |
839 | ret = BT_VALUE_STATUS_INVAL; |
840 | goto end; | |
841 | } | |
842 | ||
843 | if (integer_obj->is_frozen) { | |
2f42aa0a PP |
844 | BT_LOGW("Invalid parameter: value is frozen: addr=%p", |
845 | integer_obj); | |
dac5c838 PP |
846 | ret = BT_VALUE_STATUS_FROZEN; |
847 | goto end; | |
848 | } | |
849 | ||
850 | typed_integer_obj->value = val; | |
2f42aa0a PP |
851 | BT_LOGV("Set integer value's raw value: value-addr=%p, value=%" PRId64, |
852 | integer_obj, val); | |
dac5c838 PP |
853 | |
854 | end: | |
855 | return ret; | |
856 | } | |
857 | ||
858 | enum bt_value_status bt_value_float_get(const struct bt_value *float_obj, | |
364747d6 | 859 | double *val) |
dac5c838 PP |
860 | { |
861 | enum bt_value_status ret = BT_VALUE_STATUS_OK; | |
862 | struct bt_value_float *typed_float_obj = | |
863 | BT_VALUE_TO_FLOAT(float_obj); | |
864 | ||
2f42aa0a PP |
865 | if (!float_obj || !val) { |
866 | BT_LOGW("Invalid parameter: value object or value is NULL: " | |
867 | "value-addr=%p, raw-value-addr=%p", | |
868 | float_obj, val); | |
869 | ret = BT_VALUE_STATUS_INVAL; | |
870 | goto end; | |
871 | } | |
872 | ||
873 | if (!bt_value_is_float(float_obj)) { | |
874 | BT_LOGW("Invalid parameter: value is not a floating point number value: addr=%p, " | |
875 | "type=%d", float_obj, float_obj->type); | |
dac5c838 PP |
876 | ret = BT_VALUE_STATUS_INVAL; |
877 | goto end; | |
878 | } | |
879 | ||
880 | *val = typed_float_obj->value; | |
881 | ||
882 | end: | |
883 | return ret; | |
884 | } | |
885 | ||
886 | enum bt_value_status bt_value_float_set(struct bt_value *float_obj, | |
364747d6 | 887 | double val) |
dac5c838 PP |
888 | { |
889 | enum bt_value_status ret = BT_VALUE_STATUS_OK; | |
890 | struct bt_value_float *typed_float_obj = | |
891 | BT_VALUE_TO_FLOAT(float_obj); | |
892 | ||
2f42aa0a PP |
893 | if (!float_obj) { |
894 | BT_LOGW_STR("Invalid parameter: value object is NULL."); | |
895 | ret = BT_VALUE_STATUS_INVAL; | |
896 | goto end; | |
897 | } | |
898 | ||
899 | if (!bt_value_is_float(float_obj)) { | |
900 | BT_LOGW("Invalid parameter: value is not a floating point number value: addr=%p, " | |
901 | "type=%d", float_obj, float_obj->type); | |
dac5c838 PP |
902 | ret = BT_VALUE_STATUS_INVAL; |
903 | goto end; | |
904 | } | |
905 | ||
906 | if (float_obj->is_frozen) { | |
2f42aa0a PP |
907 | BT_LOGW("Invalid parameter: value is frozen: addr=%p", |
908 | float_obj); | |
dac5c838 PP |
909 | ret = BT_VALUE_STATUS_FROZEN; |
910 | goto end; | |
911 | } | |
912 | ||
913 | typed_float_obj->value = val; | |
2f42aa0a PP |
914 | BT_LOGV("Set floating point number value's raw value: value-addr=%p, value=%f", |
915 | float_obj, val); | |
dac5c838 PP |
916 | |
917 | end: | |
918 | return ret; | |
919 | } | |
920 | ||
921 | enum bt_value_status bt_value_string_get(const struct bt_value *string_obj, | |
364747d6 | 922 | const char **val) |
dac5c838 PP |
923 | { |
924 | enum bt_value_status ret = BT_VALUE_STATUS_OK; | |
925 | struct bt_value_string *typed_string_obj = | |
926 | BT_VALUE_TO_STRING(string_obj); | |
927 | ||
2f42aa0a PP |
928 | if (!string_obj || !val) { |
929 | BT_LOGW("Invalid parameter: value object or value is NULL: " | |
930 | "value-addr=%p, raw-value-addr=%p", | |
931 | string_obj, val); | |
932 | ret = BT_VALUE_STATUS_INVAL; | |
933 | goto end; | |
934 | } | |
935 | ||
936 | if (!bt_value_is_string(string_obj)) { | |
937 | BT_LOGW("Invalid parameter: value is not a string value: addr=%p, " | |
938 | "type=%d", string_obj, string_obj->type); | |
dac5c838 PP |
939 | ret = BT_VALUE_STATUS_INVAL; |
940 | goto end; | |
941 | } | |
942 | ||
943 | *val = typed_string_obj->gstr->str; | |
944 | ||
945 | end: | |
946 | return ret; | |
947 | } | |
948 | ||
949 | enum bt_value_status bt_value_string_set(struct bt_value *string_obj, | |
364747d6 | 950 | const char *val) |
dac5c838 PP |
951 | { |
952 | enum bt_value_status ret = BT_VALUE_STATUS_OK; | |
953 | struct bt_value_string *typed_string_obj = | |
954 | BT_VALUE_TO_STRING(string_obj); | |
955 | ||
2f42aa0a PP |
956 | if (!string_obj || !val) { |
957 | BT_LOGW("Invalid parameter: value object or value is NULL: " | |
958 | "value-addr=%p, raw-value-addr=%p", | |
959 | string_obj, val); | |
960 | ret = BT_VALUE_STATUS_INVAL; | |
961 | goto end; | |
962 | } | |
963 | ||
964 | if (!bt_value_is_string(string_obj)) { | |
965 | BT_LOGW("Invalid parameter: value is not a string value: addr=%p, " | |
966 | "type=%d", string_obj, string_obj->type); | |
dac5c838 PP |
967 | ret = BT_VALUE_STATUS_INVAL; |
968 | goto end; | |
969 | } | |
970 | ||
971 | if (string_obj->is_frozen) { | |
2f42aa0a PP |
972 | BT_LOGW("Invalid parameter: value is frozen: addr=%p", |
973 | string_obj); | |
dac5c838 PP |
974 | ret = BT_VALUE_STATUS_FROZEN; |
975 | goto end; | |
976 | } | |
977 | ||
978 | g_string_assign(typed_string_obj->gstr, val); | |
2f42aa0a PP |
979 | BT_LOGV("Set string value's raw value: value-addr=%p, raw-value-addr=%p", |
980 | string_obj, val); | |
dac5c838 PP |
981 | |
982 | end: | |
983 | return ret; | |
984 | } | |
985 | ||
544d0515 | 986 | int64_t bt_value_array_size(const struct bt_value *array_obj) |
dac5c838 | 987 | { |
544d0515 | 988 | int64_t ret; |
dac5c838 PP |
989 | struct bt_value_array *typed_array_obj = |
990 | BT_VALUE_TO_ARRAY(array_obj); | |
991 | ||
2f42aa0a PP |
992 | if (!array_obj) { |
993 | BT_LOGW_STR("Invalid parameter: value object is NULL."); | |
9ac68eb1 | 994 | ret = (int64_t) BT_VALUE_STATUS_INVAL; |
dac5c838 PP |
995 | goto end; |
996 | } | |
997 | ||
2f42aa0a PP |
998 | if (!bt_value_is_array(array_obj)) { |
999 | BT_LOGW("Invalid parameter: value is not an array value: addr=%p, " | |
1000 | "type=%d", array_obj, array_obj->type); | |
1001 | ret = BT_VALUE_STATUS_INVAL; | |
1002 | goto end; | |
1003 | } | |
1004 | ||
544d0515 | 1005 | ret = (int64_t) typed_array_obj->garray->len; |
dac5c838 PP |
1006 | |
1007 | end: | |
1008 | return ret; | |
1009 | } | |
1010 | ||
1011 | bool bt_value_array_is_empty(const struct bt_value *array_obj) | |
1012 | { | |
1013 | return bt_value_array_size(array_obj) == 0; | |
1014 | } | |
1015 | ||
1016 | struct bt_value *bt_value_array_get(const struct bt_value *array_obj, | |
2f42aa0a | 1017 | uint64_t index) |
dac5c838 PP |
1018 | { |
1019 | struct bt_value *ret; | |
1020 | struct bt_value_array *typed_array_obj = | |
1021 | BT_VALUE_TO_ARRAY(array_obj); | |
1022 | ||
2f42aa0a PP |
1023 | if (!array_obj) { |
1024 | BT_LOGW("Invalid parameter: value object is NULL: index=%" PRIu64, | |
1025 | index); | |
1026 | ret = NULL; | |
1027 | goto end; | |
1028 | } | |
1029 | ||
1030 | if (!bt_value_is_array(array_obj)) { | |
1031 | BT_LOGW("Invalid parameter: value is not an array value: addr=%p, " | |
1032 | "type=%d", array_obj, array_obj->type); | |
1033 | ret = NULL; | |
1034 | goto end; | |
1035 | } | |
1036 | ||
1037 | if (index >= typed_array_obj->garray->len) { | |
1038 | BT_LOGW("Invalid parameter: index is out of bounds: " | |
1039 | "addr=%p, index=%" PRIu64, | |
1040 | array_obj, index); | |
dac5c838 PP |
1041 | ret = NULL; |
1042 | goto end; | |
1043 | } | |
1044 | ||
1045 | ret = g_ptr_array_index(typed_array_obj->garray, index); | |
83509119 | 1046 | bt_get(ret); |
dac5c838 PP |
1047 | |
1048 | end: | |
1049 | return ret; | |
1050 | } | |
1051 | ||
1052 | enum bt_value_status bt_value_array_append(struct bt_value *array_obj, | |
364747d6 | 1053 | struct bt_value *element_obj) |
dac5c838 PP |
1054 | { |
1055 | enum bt_value_status ret = BT_VALUE_STATUS_OK; | |
1056 | struct bt_value_array *typed_array_obj = | |
1057 | BT_VALUE_TO_ARRAY(array_obj); | |
1058 | ||
2f42aa0a PP |
1059 | if (!array_obj || !element_obj) { |
1060 | BT_LOGW("Invalid parameter: array value or element value is NULL: " | |
1061 | "array-value-addr=%p, element-value-addr=%p", | |
1062 | array_obj, element_obj); | |
1063 | ret = BT_VALUE_STATUS_INVAL; | |
1064 | goto end; | |
1065 | } | |
1066 | ||
1067 | if (!bt_value_is_array(array_obj)) { | |
1068 | BT_LOGW("Invalid parameter: value is not an array value: addr=%p, " | |
1069 | "type=%d", array_obj, array_obj->type); | |
dac5c838 PP |
1070 | ret = BT_VALUE_STATUS_INVAL; |
1071 | goto end; | |
1072 | } | |
1073 | ||
1074 | if (array_obj->is_frozen) { | |
2f42aa0a PP |
1075 | BT_LOGW("Invalid parameter: value is frozen: addr=%p", |
1076 | array_obj); | |
dac5c838 PP |
1077 | ret = BT_VALUE_STATUS_FROZEN; |
1078 | goto end; | |
1079 | } | |
1080 | ||
1081 | g_ptr_array_add(typed_array_obj->garray, element_obj); | |
83509119 | 1082 | bt_get(element_obj); |
2f42aa0a PP |
1083 | BT_LOGV("Appended element to array value: array-value-addr=%p, " |
1084 | "element-value-addr=%p, new-size=%u", | |
1085 | array_obj, element_obj, typed_array_obj->garray->len); | |
dac5c838 PP |
1086 | |
1087 | end: | |
1088 | return ret; | |
1089 | } | |
1090 | ||
1091 | enum bt_value_status bt_value_array_append_bool(struct bt_value *array_obj, | |
364747d6 | 1092 | bool val) |
dac5c838 PP |
1093 | { |
1094 | enum bt_value_status ret; | |
1095 | struct bt_value *bool_obj = NULL; | |
1096 | ||
1097 | bool_obj = bt_value_bool_create_init(val); | |
1098 | ret = bt_value_array_append(array_obj, bool_obj); | |
83509119 | 1099 | bt_put(bool_obj); |
dac5c838 PP |
1100 | return ret; |
1101 | } | |
1102 | ||
1103 | enum bt_value_status bt_value_array_append_integer( | |
364747d6 | 1104 | struct bt_value *array_obj, int64_t val) |
dac5c838 PP |
1105 | { |
1106 | enum bt_value_status ret; | |
1107 | struct bt_value *integer_obj = NULL; | |
1108 | ||
1109 | integer_obj = bt_value_integer_create_init(val); | |
1110 | ret = bt_value_array_append(array_obj, integer_obj); | |
83509119 | 1111 | bt_put(integer_obj); |
dac5c838 PP |
1112 | return ret; |
1113 | } | |
1114 | ||
1115 | enum bt_value_status bt_value_array_append_float(struct bt_value *array_obj, | |
364747d6 | 1116 | double val) |
dac5c838 PP |
1117 | { |
1118 | enum bt_value_status ret; | |
1119 | struct bt_value *float_obj = NULL; | |
1120 | ||
1121 | float_obj = bt_value_float_create_init(val); | |
1122 | ret = bt_value_array_append(array_obj, float_obj); | |
83509119 | 1123 | bt_put(float_obj); |
dac5c838 PP |
1124 | return ret; |
1125 | } | |
1126 | ||
1127 | enum bt_value_status bt_value_array_append_string(struct bt_value *array_obj, | |
364747d6 | 1128 | const char *val) |
dac5c838 PP |
1129 | { |
1130 | enum bt_value_status ret; | |
1131 | struct bt_value *string_obj = NULL; | |
1132 | ||
1133 | string_obj = bt_value_string_create_init(val); | |
1134 | ret = bt_value_array_append(array_obj, string_obj); | |
83509119 | 1135 | bt_put(string_obj); |
dac5c838 PP |
1136 | return ret; |
1137 | } | |
1138 | ||
5b79e8bf | 1139 | enum bt_value_status bt_value_array_append_empty_array( |
364747d6 | 1140 | struct bt_value *array_obj) |
dac5c838 PP |
1141 | { |
1142 | enum bt_value_status ret; | |
1143 | struct bt_value *empty_array_obj = NULL; | |
1144 | ||
1145 | empty_array_obj = bt_value_array_create(); | |
1146 | ret = bt_value_array_append(array_obj, empty_array_obj); | |
83509119 | 1147 | bt_put(empty_array_obj); |
dac5c838 PP |
1148 | return ret; |
1149 | } | |
1150 | ||
5b79e8bf | 1151 | enum bt_value_status bt_value_array_append_empty_map(struct bt_value *array_obj) |
dac5c838 PP |
1152 | { |
1153 | enum bt_value_status ret; | |
1154 | struct bt_value *map_obj = NULL; | |
1155 | ||
1156 | map_obj = bt_value_map_create(); | |
1157 | ret = bt_value_array_append(array_obj, map_obj); | |
83509119 | 1158 | bt_put(map_obj); |
dac5c838 PP |
1159 | return ret; |
1160 | } | |
1161 | ||
1162 | enum bt_value_status bt_value_array_set(struct bt_value *array_obj, | |
2f42aa0a | 1163 | uint64_t index, struct bt_value *element_obj) |
dac5c838 PP |
1164 | { |
1165 | enum bt_value_status ret = BT_VALUE_STATUS_OK; | |
1166 | struct bt_value_array *typed_array_obj = | |
1167 | BT_VALUE_TO_ARRAY(array_obj); | |
1168 | ||
2f42aa0a PP |
1169 | if (!array_obj || !element_obj) { |
1170 | BT_LOGW("Invalid parameter: array value or element value is NULL: " | |
1171 | "index=%" PRIu64 ", array-value-addr=%p, element-value-addr=%p", | |
1172 | index, array_obj, element_obj); | |
1173 | ret = BT_VALUE_STATUS_INVAL; | |
1174 | goto end; | |
1175 | } | |
1176 | ||
1177 | if (!bt_value_is_array(array_obj)) { | |
1178 | BT_LOGW("Invalid parameter: value is not an array value: addr=%p, " | |
1179 | "type=%d", array_obj, array_obj->type); | |
1180 | ret = BT_VALUE_STATUS_INVAL; | |
1181 | goto end; | |
1182 | } | |
1183 | ||
1184 | if (index >= typed_array_obj->garray->len) { | |
1185 | BT_LOGW("Invalid parameter: index is out of bounds: " | |
1186 | "addr=%p, index=%" PRIu64, | |
1187 | array_obj, index); | |
dac5c838 PP |
1188 | ret = BT_VALUE_STATUS_INVAL; |
1189 | goto end; | |
1190 | } | |
1191 | ||
1192 | if (array_obj->is_frozen) { | |
2f42aa0a PP |
1193 | BT_LOGW("Invalid parameter: value is frozen: addr=%p", |
1194 | array_obj); | |
dac5c838 PP |
1195 | ret = BT_VALUE_STATUS_FROZEN; |
1196 | goto end; | |
1197 | } | |
1198 | ||
83509119 | 1199 | bt_put(g_ptr_array_index(typed_array_obj->garray, index)); |
dac5c838 | 1200 | g_ptr_array_index(typed_array_obj->garray, index) = element_obj; |
83509119 | 1201 | bt_get(element_obj); |
2f42aa0a PP |
1202 | BT_LOGV("Set array value's element: array-value-addr=%p, " |
1203 | "index=%" PRIu64 ", element-value-addr=%p", | |
1204 | array_obj, index, element_obj); | |
dac5c838 PP |
1205 | |
1206 | end: | |
1207 | return ret; | |
1208 | } | |
1209 | ||
544d0515 | 1210 | int64_t bt_value_map_size(const struct bt_value *map_obj) |
dac5c838 | 1211 | { |
544d0515 | 1212 | int64_t ret; |
dac5c838 PP |
1213 | struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj); |
1214 | ||
2f42aa0a PP |
1215 | if (!map_obj) { |
1216 | BT_LOGW_STR("Invalid parameter: value object is NULL."); | |
1217 | ret = (int64_t) BT_VALUE_STATUS_INVAL; | |
1218 | goto end; | |
1219 | } | |
1220 | ||
1221 | if (!bt_value_is_map(map_obj)) { | |
1222 | BT_LOGW("Invalid parameter: value is not a map value: addr=%p, " | |
1223 | "type=%d", map_obj, map_obj->type); | |
9ac68eb1 | 1224 | ret = (int64_t) BT_VALUE_STATUS_INVAL; |
dac5c838 PP |
1225 | goto end; |
1226 | } | |
1227 | ||
544d0515 | 1228 | ret = (int64_t) g_hash_table_size(typed_map_obj->ght); |
dac5c838 PP |
1229 | |
1230 | end: | |
1231 | return ret; | |
1232 | } | |
1233 | ||
1234 | bool bt_value_map_is_empty(const struct bt_value *map_obj) | |
1235 | { | |
1236 | return bt_value_map_size(map_obj) == 0; | |
1237 | } | |
1238 | ||
1239 | struct bt_value *bt_value_map_get(const struct bt_value *map_obj, | |
364747d6 | 1240 | const char *key) |
dac5c838 PP |
1241 | { |
1242 | GQuark quark; | |
1243 | struct bt_value *ret; | |
1244 | struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj); | |
1245 | ||
2f42aa0a PP |
1246 | if (!map_obj || !key) { |
1247 | BT_LOGW("Invalid parameter: value object or key is NULL: " | |
1248 | "value-addr=%p, key-addr=%p", map_obj, key); | |
1249 | ret = NULL; | |
1250 | goto end; | |
1251 | } | |
1252 | ||
1253 | if (!bt_value_is_map(map_obj)) { | |
1254 | BT_LOGW("Invalid parameter: value is not a map value: addr=%p, " | |
1255 | "type=%d", map_obj, map_obj->type); | |
dac5c838 PP |
1256 | ret = NULL; |
1257 | goto end; | |
1258 | } | |
1259 | ||
1260 | quark = g_quark_from_string(key); | |
1261 | ret = g_hash_table_lookup(typed_map_obj->ght, GUINT_TO_POINTER(quark)); | |
1262 | ||
1263 | if (ret) { | |
83509119 | 1264 | bt_get(ret); |
dac5c838 PP |
1265 | } |
1266 | ||
1267 | end: | |
1268 | return ret; | |
1269 | } | |
1270 | ||
1271 | bool bt_value_map_has_key(const struct bt_value *map_obj, const char *key) | |
1272 | { | |
1273 | bool ret; | |
1274 | GQuark quark; | |
1275 | struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj); | |
1276 | ||
2f42aa0a PP |
1277 | if (!map_obj || !key) { |
1278 | BT_LOGW("Invalid parameter: value object or key is NULL: " | |
1279 | "value-addr=%p, key-addr=%p", map_obj, key); | |
1280 | ret = false; | |
1281 | goto end; | |
1282 | } | |
1283 | ||
1284 | if (!bt_value_is_map(map_obj)) { | |
1285 | BT_LOGW("Invalid parameter: value is not a map value: addr=%p, " | |
1286 | "type=%d", map_obj, map_obj->type); | |
dac5c838 PP |
1287 | ret = false; |
1288 | goto end; | |
1289 | } | |
1290 | ||
1291 | quark = g_quark_from_string(key); | |
5d5982ab | 1292 | ret = bt_g_hash_table_contains(typed_map_obj->ght, |
dac5c838 PP |
1293 | GUINT_TO_POINTER(quark)); |
1294 | ||
1295 | end: | |
1296 | return ret; | |
1297 | } | |
1298 | ||
1299 | enum bt_value_status bt_value_map_insert(struct bt_value *map_obj, | |
364747d6 | 1300 | const char *key, struct bt_value *element_obj) |
dac5c838 PP |
1301 | { |
1302 | GQuark quark; | |
1303 | enum bt_value_status ret = BT_VALUE_STATUS_OK; | |
1304 | struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj); | |
1305 | ||
2f42aa0a PP |
1306 | if (!map_obj || !key || !element_obj) { |
1307 | BT_LOGW("Invalid parameter: map value, key, or element value is NULL: " | |
1308 | "map-value-addr=%p, key-addr=%p, element-value-addr=%p", | |
1309 | map_obj, key, element_obj); | |
1310 | ret = BT_VALUE_STATUS_INVAL; | |
1311 | goto end; | |
1312 | } | |
1313 | ||
1314 | if (!bt_value_is_map(map_obj)) { | |
1315 | BT_LOGW("Invalid parameter: value is not a map value: addr=%p, " | |
1316 | "type=%d", map_obj, map_obj->type); | |
dac5c838 PP |
1317 | ret = BT_VALUE_STATUS_INVAL; |
1318 | goto end; | |
1319 | } | |
1320 | ||
1321 | if (map_obj->is_frozen) { | |
2f42aa0a PP |
1322 | BT_LOGW("Invalid parameter: value is frozen: addr=%p", |
1323 | map_obj); | |
dac5c838 PP |
1324 | ret = BT_VALUE_STATUS_FROZEN; |
1325 | goto end; | |
1326 | } | |
1327 | ||
1328 | quark = g_quark_from_string(key); | |
1329 | g_hash_table_insert(typed_map_obj->ght, | |
1330 | GUINT_TO_POINTER(quark), element_obj); | |
83509119 | 1331 | bt_get(element_obj); |
2f42aa0a PP |
1332 | BT_LOGV("Inserted value into map value: map-value-addr=%p, " |
1333 | "key=\"%s\", element-value-addr=%p", | |
1334 | map_obj, key, element_obj); | |
dac5c838 PP |
1335 | |
1336 | end: | |
1337 | return ret; | |
1338 | } | |
1339 | ||
1340 | enum bt_value_status bt_value_map_insert_bool(struct bt_value *map_obj, | |
364747d6 | 1341 | const char *key, bool val) |
dac5c838 PP |
1342 | { |
1343 | enum bt_value_status ret; | |
1344 | struct bt_value *bool_obj = NULL; | |
1345 | ||
1346 | bool_obj = bt_value_bool_create_init(val); | |
1347 | ret = bt_value_map_insert(map_obj, key, bool_obj); | |
83509119 | 1348 | bt_put(bool_obj); |
dac5c838 PP |
1349 | return ret; |
1350 | } | |
1351 | ||
1352 | enum bt_value_status bt_value_map_insert_integer(struct bt_value *map_obj, | |
364747d6 | 1353 | const char *key, int64_t val) |
dac5c838 PP |
1354 | { |
1355 | enum bt_value_status ret; | |
1356 | struct bt_value *integer_obj = NULL; | |
1357 | ||
1358 | integer_obj = bt_value_integer_create_init(val); | |
1359 | ret = bt_value_map_insert(map_obj, key, integer_obj); | |
83509119 | 1360 | bt_put(integer_obj); |
dac5c838 PP |
1361 | return ret; |
1362 | } | |
1363 | ||
1364 | enum bt_value_status bt_value_map_insert_float(struct bt_value *map_obj, | |
364747d6 | 1365 | const char *key, double val) |
dac5c838 PP |
1366 | { |
1367 | enum bt_value_status ret; | |
1368 | struct bt_value *float_obj = NULL; | |
1369 | ||
1370 | float_obj = bt_value_float_create_init(val); | |
1371 | ret = bt_value_map_insert(map_obj, key, float_obj); | |
83509119 | 1372 | bt_put(float_obj); |
dac5c838 PP |
1373 | return ret; |
1374 | } | |
1375 | ||
1376 | enum bt_value_status bt_value_map_insert_string(struct bt_value *map_obj, | |
364747d6 | 1377 | const char *key, const char *val) |
dac5c838 PP |
1378 | { |
1379 | enum bt_value_status ret; | |
1380 | struct bt_value *string_obj = NULL; | |
1381 | ||
1382 | string_obj = bt_value_string_create_init(val); | |
1383 | ret = bt_value_map_insert(map_obj, key, string_obj); | |
83509119 | 1384 | bt_put(string_obj); |
dac5c838 PP |
1385 | return ret; |
1386 | } | |
1387 | ||
5b79e8bf | 1388 | enum bt_value_status bt_value_map_insert_empty_array(struct bt_value *map_obj, |
364747d6 | 1389 | const char *key) |
dac5c838 PP |
1390 | { |
1391 | enum bt_value_status ret; | |
1392 | struct bt_value *array_obj = NULL; | |
1393 | ||
1394 | array_obj = bt_value_array_create(); | |
1395 | ret = bt_value_map_insert(map_obj, key, array_obj); | |
83509119 | 1396 | bt_put(array_obj); |
dac5c838 PP |
1397 | return ret; |
1398 | } | |
1399 | ||
5b79e8bf | 1400 | enum bt_value_status bt_value_map_insert_empty_map(struct bt_value *map_obj, |
364747d6 | 1401 | const char *key) |
dac5c838 PP |
1402 | { |
1403 | enum bt_value_status ret; | |
1404 | struct bt_value *empty_map_obj = NULL; | |
1405 | ||
1406 | empty_map_obj = bt_value_map_create(); | |
1407 | ret = bt_value_map_insert(map_obj, key, empty_map_obj); | |
83509119 | 1408 | bt_put(empty_map_obj); |
dac5c838 PP |
1409 | return ret; |
1410 | } | |
1411 | ||
1412 | enum bt_value_status bt_value_map_foreach(const struct bt_value *map_obj, | |
364747d6 | 1413 | bt_value_map_foreach_cb cb, void *data) |
dac5c838 PP |
1414 | { |
1415 | enum bt_value_status ret = BT_VALUE_STATUS_OK; | |
1416 | gpointer key, element_obj; | |
1417 | GHashTableIter iter; | |
1418 | struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj); | |
1419 | ||
2f42aa0a PP |
1420 | if (!map_obj || !cb) { |
1421 | BT_LOGW("Invalid parameter: map value or callback is NULL: " | |
1422 | "value-addr=%p, cb-addr=%p", map_obj, cb); | |
1423 | ret = BT_VALUE_STATUS_INVAL; | |
1424 | goto end; | |
1425 | } | |
1426 | ||
1427 | if (!bt_value_is_map(map_obj)) { | |
1428 | BT_LOGW("Invalid parameter: value is not a map value: addr=%p, " | |
1429 | "type=%d", map_obj, map_obj->type); | |
dac5c838 PP |
1430 | ret = BT_VALUE_STATUS_INVAL; |
1431 | goto end; | |
1432 | } | |
1433 | ||
1434 | g_hash_table_iter_init(&iter, typed_map_obj->ght); | |
1435 | ||
1436 | while (g_hash_table_iter_next(&iter, &key, &element_obj)) { | |
5b44aff2 | 1437 | const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key)); |
dac5c838 PP |
1438 | |
1439 | if (!cb(key_str, element_obj, data)) { | |
2f42aa0a PP |
1440 | BT_LOGD("User cancelled the loop: key=\"%s\", " |
1441 | "value-addr=%p, data=%p", | |
1442 | key_str, element_obj, data); | |
dac5c838 PP |
1443 | ret = BT_VALUE_STATUS_CANCELLED; |
1444 | break; | |
1445 | } | |
1446 | } | |
1447 | ||
1448 | end: | |
1449 | return ret; | |
1450 | } | |
1451 | ||
770750d3 PP |
1452 | struct extend_map_element_data { |
1453 | struct bt_value *extended_obj; | |
1454 | bool got_error; | |
1455 | }; | |
1456 | ||
1457 | static | |
1458 | bool extend_map_element(const char *key, | |
1459 | struct bt_value *extension_obj_elem, void *data) | |
1460 | { | |
1461 | bool ret = true; | |
1462 | ||
1463 | struct extend_map_element_data *extend_data = data; | |
1464 | ||
1465 | /* Copy object which is to replace the current one */ | |
1466 | struct bt_value *extension_obj_elem_copy = | |
1467 | bt_value_copy(extension_obj_elem); | |
1468 | ||
1469 | /* Replace in extended object */ | |
1470 | if (bt_value_map_insert(extend_data->extended_obj, key, | |
1471 | extension_obj_elem_copy)) { | |
2f42aa0a PP |
1472 | BT_LOGE("Cannot replace value in extended value: key=\"%s\", " |
1473 | "extended-value-addr=%p, element-value-addr=%p", | |
1474 | key, extend_data->extended_obj, | |
1475 | extension_obj_elem_copy); | |
770750d3 PP |
1476 | goto error; |
1477 | } | |
1478 | ||
1479 | goto end; | |
1480 | ||
1481 | error: | |
1482 | ret = false; | |
1483 | extend_data->got_error = true; | |
1484 | ||
1485 | end: | |
1486 | BT_PUT(extension_obj_elem_copy); | |
770750d3 PP |
1487 | return ret; |
1488 | } | |
1489 | ||
1490 | struct bt_value *bt_value_map_extend(struct bt_value *base_map_obj, | |
1491 | struct bt_value *extension_obj) | |
1492 | { | |
1493 | struct bt_value *extended_obj = NULL; | |
1494 | struct extend_map_element_data extend_data = { 0 }; | |
1495 | ||
2f42aa0a PP |
1496 | if (!base_map_obj || !extension_obj) { |
1497 | BT_LOGW("Invalid parameter: base value or extension value is NULL: " | |
1498 | "base-value-addr=%p, extension-value-addr=%p", | |
1499 | base_map_obj, extension_obj); | |
1500 | goto error; | |
1501 | } | |
1502 | ||
1503 | if (!bt_value_is_map(base_map_obj)) { | |
1504 | BT_LOGW("Invalid parameter: value is not a map value: addr=%p, " | |
1505 | "type=%d", base_map_obj, base_map_obj->type); | |
770750d3 PP |
1506 | goto error; |
1507 | } | |
1508 | ||
2f42aa0a PP |
1509 | if (!bt_value_is_map(extension_obj)) { |
1510 | BT_LOGW("Invalid parameter: value is not a map value: addr=%p, " | |
1511 | "type=%d", extension_obj, extension_obj->type); | |
1512 | goto error; | |
1513 | } | |
1514 | ||
1515 | BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p", | |
1516 | base_map_obj, extension_obj); | |
1517 | ||
770750d3 PP |
1518 | /* Create copy of base map object to start with */ |
1519 | extended_obj = bt_value_copy(base_map_obj); | |
1520 | if (!extended_obj) { | |
2f42aa0a PP |
1521 | BT_LOGE("Cannot copy base value: base-value-addr=%p", |
1522 | base_map_obj); | |
770750d3 PP |
1523 | goto error; |
1524 | } | |
1525 | ||
1526 | /* | |
1527 | * For each key in the extension map object, replace this key | |
1528 | * in the copied map object. | |
1529 | */ | |
1530 | extend_data.extended_obj = extended_obj; | |
1531 | ||
1532 | if (bt_value_map_foreach(extension_obj, extend_map_element, | |
1533 | &extend_data)) { | |
2f42aa0a PP |
1534 | BT_LOGE("Cannot iterate on the extension object's elements: ", |
1535 | "extension-value-addr=%p", extension_obj); | |
770750d3 PP |
1536 | goto error; |
1537 | } | |
1538 | ||
1539 | if (extend_data.got_error) { | |
2f42aa0a PP |
1540 | BT_LOGE("Failed to successfully iterate on the extension object's elements: ", |
1541 | "extension-value-addr=%p", extension_obj); | |
770750d3 PP |
1542 | goto error; |
1543 | } | |
1544 | ||
2f42aa0a PP |
1545 | BT_LOGD("Extended map value: extended-value-addr=%p", |
1546 | extended_obj); | |
770750d3 PP |
1547 | goto end; |
1548 | ||
1549 | error: | |
1550 | BT_PUT(extended_obj); | |
1551 | ||
1552 | end: | |
1553 | return extended_obj; | |
1554 | } | |
1555 | ||
dac5c838 PP |
1556 | struct bt_value *bt_value_copy(const struct bt_value *object) |
1557 | { | |
1558 | struct bt_value *copy_obj = NULL; | |
1559 | ||
1560 | if (!object) { | |
2f42aa0a | 1561 | BT_LOGW_STR("Invalid parameter: value object is NULL."); |
dac5c838 PP |
1562 | goto end; |
1563 | } | |
1564 | ||
2f42aa0a | 1565 | BT_LOGD("Copying value object: addr=%p", object); |
dac5c838 | 1566 | copy_obj = copy_funcs[object->type](object); |
2f42aa0a PP |
1567 | if (copy_obj) { |
1568 | BT_LOGD("Copied value object: copy-value-addr=%p", | |
1569 | copy_obj); | |
1570 | } else { | |
1571 | BT_LOGE_STR("Failed to copy value object."); | |
1572 | } | |
dac5c838 PP |
1573 | |
1574 | end: | |
1575 | return copy_obj; | |
1576 | } | |
1577 | ||
1578 | bool bt_value_compare(const struct bt_value *object_a, | |
1579 | const struct bt_value *object_b) | |
1580 | { | |
1581 | bool ret = false; | |
1582 | ||
1583 | if (!object_a || !object_b) { | |
2f42aa0a PP |
1584 | BT_LOGW("Invalid parameter: value A or value B is NULL: " |
1585 | "value-a-addr=%p, value-b-addr=%p", | |
1586 | object_a, object_b); | |
dac5c838 PP |
1587 | goto end; |
1588 | } | |
1589 | ||
1590 | if (object_a->type != object_b->type) { | |
2f42aa0a PP |
1591 | BT_LOGV("Values are different: type mismatch: " |
1592 | "value-a-addr=%p, value-b-addr=%p, " | |
1593 | "value-a-type=%d, value-b-type=%d", | |
1594 | object_a, object_b, | |
1595 | object_a->type, object_b->type); | |
dac5c838 PP |
1596 | goto end; |
1597 | } | |
1598 | ||
1599 | ret = compare_funcs[object_a->type](object_a, object_b); | |
1600 | ||
1601 | end: | |
1602 | return ret; | |
1603 | } |