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