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