| 1 | /* |
| 2 | * Values.c: value objects |
| 3 | * |
| 4 | * Babeltrace Library |
| 5 | * |
| 6 | * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation |
| 7 | * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com> |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | * of this software and associated documentation files (the "Software"), to deal |
| 11 | * in the Software without restriction, including without limitation the rights |
| 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | * copies of the Software, and to permit persons to whom the Software is |
| 14 | * furnished to do so, subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be included in |
| 17 | * all copies or substantial portions of the Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | * SOFTWARE. |
| 26 | */ |
| 27 | |
| 28 | #define BT_LOG_TAG "VALUES" |
| 29 | #include <babeltrace/lib-logging-internal.h> |
| 30 | |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | #include <string.h> |
| 34 | #include <inttypes.h> |
| 35 | #include <babeltrace/compiler-internal.h> |
| 36 | #include <babeltrace/ref.h> |
| 37 | #include <babeltrace/values.h> |
| 38 | #include <babeltrace/compat/glib-internal.h> |
| 39 | #include <babeltrace/types.h> |
| 40 | #include <babeltrace/object-internal.h> |
| 41 | #include <babeltrace/values-internal.h> |
| 42 | #include <babeltrace/assert-internal.h> |
| 43 | #include <babeltrace/assert-pre-internal.h> |
| 44 | |
| 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 | |
| 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 | |
| 68 | struct bt_value { |
| 69 | struct bt_object base; |
| 70 | enum bt_value_type type; |
| 71 | bt_bool frozen; |
| 72 | }; |
| 73 | |
| 74 | static |
| 75 | struct bt_value bt_value_null_instance = { |
| 76 | .base = { |
| 77 | .ref_count = { |
| 78 | .count = 1, |
| 79 | .release = NULL, |
| 80 | }, |
| 81 | .release = NULL, |
| 82 | .parent = NULL, |
| 83 | .is_shared = true, |
| 84 | }, |
| 85 | .type = BT_VALUE_TYPE_NULL, |
| 86 | .frozen = BT_TRUE, |
| 87 | }; |
| 88 | |
| 89 | struct bt_value *bt_value_null = &bt_value_null_instance; |
| 90 | |
| 91 | struct bt_value_bool { |
| 92 | struct bt_value base; |
| 93 | bt_bool value; |
| 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 |
| 122 | void bt_value_destroy(struct bt_object *obj); |
| 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 | |
| 203 | BT_LOGD("Copying array value: addr=%p", array_obj); |
| 204 | typed_array_obj = BT_VALUE_TO_ARRAY(array_obj); |
| 205 | copy_obj = bt_value_array_create(); |
| 206 | if (!copy_obj) { |
| 207 | BT_LOGE_STR("Cannot create empty array value."); |
| 208 | goto end; |
| 209 | } |
| 210 | |
| 211 | for (i = 0; i < typed_array_obj->garray->len; ++i) { |
| 212 | struct bt_value *element_obj_copy; |
| 213 | struct bt_value *element_obj = bt_value_array_borrow( |
| 214 | array_obj, i); |
| 215 | |
| 216 | BT_ASSERT(element_obj); |
| 217 | BT_LOGD("Copying array value's element: element-addr=%p, " |
| 218 | "index=%d", element_obj, i); |
| 219 | element_obj_copy = bt_value_copy(element_obj); |
| 220 | if (!element_obj_copy) { |
| 221 | BT_LOGE("Cannot copy array value's element: " |
| 222 | "array-addr=%p, index=%d", |
| 223 | array_obj, i); |
| 224 | BT_PUT(copy_obj); |
| 225 | goto end; |
| 226 | } |
| 227 | |
| 228 | ret = bt_value_array_append(copy_obj, element_obj_copy); |
| 229 | BT_PUT(element_obj_copy); |
| 230 | if (ret) { |
| 231 | BT_LOGE("Cannot append to array value: addr=%p", |
| 232 | array_obj); |
| 233 | BT_PUT(copy_obj); |
| 234 | goto end; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | BT_LOGD("Copied array value: original-addr=%p, copy-addr=%p", |
| 239 | array_obj, copy_obj); |
| 240 | |
| 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 | |
| 255 | BT_LOGD("Copying map value: addr=%p", map_obj); |
| 256 | typed_map_obj = BT_VALUE_TO_MAP(map_obj); |
| 257 | copy_obj = bt_value_map_create(); |
| 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)) { |
| 265 | const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key)); |
| 266 | |
| 267 | BT_ASSERT(key_str); |
| 268 | BT_LOGD("Copying map value's element: element-addr=%p, " |
| 269 | "key=\"%s\"", element_obj, key_str); |
| 270 | element_obj_copy = bt_value_copy(element_obj); |
| 271 | if (!element_obj_copy) { |
| 272 | BT_LOGE("Cannot copy map value's element: " |
| 273 | "map-addr=%p, key=\"%s\"", |
| 274 | map_obj, key_str); |
| 275 | BT_PUT(copy_obj); |
| 276 | goto end; |
| 277 | } |
| 278 | |
| 279 | ret = bt_value_map_insert(copy_obj, key_str, element_obj_copy); |
| 280 | BT_PUT(element_obj_copy); |
| 281 | if (ret) { |
| 282 | BT_LOGE("Cannot insert into map value: addr=%p, key=\"%s\"", |
| 283 | map_obj, key_str); |
| 284 | BT_PUT(copy_obj); |
| 285 | goto end; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | BT_LOGD("Copied map value: addr=%p", map_obj); |
| 290 | |
| 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 |
| 307 | bt_bool bt_value_null_compare(const struct bt_value *object_a, |
| 308 | const struct bt_value *object_b) |
| 309 | { |
| 310 | /* |
| 311 | * Always BT_TRUE since bt_value_compare() already checks if both |
| 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 | */ |
| 315 | return BT_TRUE; |
| 316 | } |
| 317 | |
| 318 | static |
| 319 | bt_bool bt_value_bool_compare(const struct bt_value *object_a, |
| 320 | const struct bt_value *object_b) |
| 321 | { |
| 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; |
| 332 | } |
| 333 | |
| 334 | static |
| 335 | bt_bool bt_value_integer_compare(const struct bt_value *object_a, |
| 336 | const struct bt_value *object_b) |
| 337 | { |
| 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; |
| 348 | } |
| 349 | |
| 350 | static |
| 351 | bt_bool bt_value_float_compare(const struct bt_value *object_a, |
| 352 | const struct bt_value *object_b) |
| 353 | { |
| 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; |
| 364 | } |
| 365 | |
| 366 | static |
| 367 | bt_bool bt_value_string_compare(const struct bt_value *object_a, |
| 368 | const struct bt_value *object_b) |
| 369 | { |
| 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; |
| 380 | } |
| 381 | |
| 382 | static |
| 383 | bt_bool bt_value_array_compare(const struct bt_value *object_a, |
| 384 | const struct bt_value *object_b) |
| 385 | { |
| 386 | int i; |
| 387 | bt_bool ret = BT_TRUE; |
| 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)) { |
| 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)); |
| 398 | ret = BT_FALSE; |
| 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 | |
| 406 | element_obj_a = bt_value_array_borrow(object_a, i); |
| 407 | element_obj_b = bt_value_array_borrow(object_b, i); |
| 408 | |
| 409 | if (!bt_value_compare(element_obj_a, element_obj_b)) { |
| 410 | BT_LOGV("Array values's elements are different: " |
| 411 | "value-a-addr=%p, value-b-addr=%p, index=%d", |
| 412 | element_obj_a, element_obj_b, i); |
| 413 | ret = BT_FALSE; |
| 414 | goto end; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | end: |
| 419 | return ret; |
| 420 | } |
| 421 | |
| 422 | static |
| 423 | bt_bool bt_value_map_compare(const struct bt_value *object_a, |
| 424 | const struct bt_value *object_b) |
| 425 | { |
| 426 | bt_bool ret = BT_TRUE; |
| 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)) { |
| 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)); |
| 438 | ret = BT_FALSE; |
| 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; |
| 446 | const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key)); |
| 447 | |
| 448 | element_obj_b = bt_value_map_borrow(object_b, key_str); |
| 449 | |
| 450 | if (!bt_value_compare(element_obj_a, element_obj_b)) { |
| 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); |
| 454 | ret = BT_FALSE; |
| 455 | goto end; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | end: |
| 460 | return ret; |
| 461 | } |
| 462 | |
| 463 | static |
| 464 | bt_bool (* const compare_funcs[])(const struct bt_value *, |
| 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, |
| 469 | [BT_VALUE_TYPE_FLOAT] = bt_value_float_compare, |
| 470 | [BT_VALUE_TYPE_STRING] = bt_value_string_compare, |
| 471 | [BT_VALUE_TYPE_ARRAY] = bt_value_array_compare, |
| 472 | [BT_VALUE_TYPE_MAP] = bt_value_map_compare, |
| 473 | }; |
| 474 | |
| 475 | static |
| 476 | void bt_value_null_freeze(struct bt_value *object) |
| 477 | { |
| 478 | } |
| 479 | |
| 480 | static |
| 481 | void bt_value_generic_freeze(struct bt_value *object) |
| 482 | { |
| 483 | object->frozen = BT_TRUE; |
| 484 | } |
| 485 | |
| 486 | static |
| 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) { |
| 494 | bt_value_freeze(g_ptr_array_index(typed_array_obj->garray, i)); |
| 495 | } |
| 496 | |
| 497 | bt_value_generic_freeze(object); |
| 498 | } |
| 499 | |
| 500 | static |
| 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 |
| 528 | void bt_value_destroy(struct bt_object *obj) |
| 529 | { |
| 530 | struct bt_value *value; |
| 531 | |
| 532 | value = container_of(obj, struct bt_value, base); |
| 533 | BT_ASSERT(value->type != BT_VALUE_TYPE_UNKNOWN); |
| 534 | BT_LOGD("Destroying value: addr=%p", value); |
| 535 | |
| 536 | if (bt_value_is_null(value)) { |
| 537 | BT_LOGD_STR("Not destroying the null value singleton."); |
| 538 | return; |
| 539 | } |
| 540 | |
| 541 | if (destroy_funcs[value->type]) { |
| 542 | destroy_funcs[value->type](value); |
| 543 | } |
| 544 | |
| 545 | g_free(value); |
| 546 | } |
| 547 | |
| 548 | BT_HIDDEN |
| 549 | enum bt_value_status _bt_value_freeze(struct bt_value *object) |
| 550 | { |
| 551 | enum bt_value_status ret = BT_VALUE_STATUS_OK; |
| 552 | |
| 553 | BT_ASSERT(object); |
| 554 | |
| 555 | if (object->frozen) { |
| 556 | goto end; |
| 557 | } |
| 558 | |
| 559 | BT_LOGD("Freezing value: addr=%p", object); |
| 560 | freeze_funcs[object->type](object); |
| 561 | |
| 562 | end: |
| 563 | return ret; |
| 564 | } |
| 565 | |
| 566 | enum bt_value_type bt_value_get_type(const struct bt_value *object) |
| 567 | { |
| 568 | BT_ASSERT_PRE_NON_NULL(object, "Value object"); |
| 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; |
| 578 | base.frozen = BT_FALSE; |
| 579 | bt_object_init(&base, bt_value_destroy); |
| 580 | return base; |
| 581 | } |
| 582 | |
| 583 | struct bt_value *bt_value_bool_create_init(bt_bool val) |
| 584 | { |
| 585 | struct bt_value_bool *bool_obj; |
| 586 | |
| 587 | BT_LOGD("Creating boolean value object: val=%d", val); |
| 588 | bool_obj = g_new0(struct bt_value_bool, 1); |
| 589 | if (!bool_obj) { |
| 590 | BT_LOGE_STR("Failed to allocate one boolean value object."); |
| 591 | goto end; |
| 592 | } |
| 593 | |
| 594 | bool_obj->base = bt_value_create_base(BT_VALUE_TYPE_BOOL); |
| 595 | bool_obj->value = val; |
| 596 | BT_LOGD("Created boolean value object: addr=%p", bool_obj); |
| 597 | |
| 598 | end: |
| 599 | return BT_VALUE_FROM_CONCRETE(bool_obj); |
| 600 | } |
| 601 | |
| 602 | struct bt_value *bt_value_bool_create(void) |
| 603 | { |
| 604 | return bt_value_bool_create_init(BT_FALSE); |
| 605 | } |
| 606 | |
| 607 | struct bt_value *bt_value_integer_create_init(int64_t val) |
| 608 | { |
| 609 | struct bt_value_integer *integer_obj; |
| 610 | |
| 611 | BT_LOGD("Creating integer value object: val=%" PRId64, val); |
| 612 | integer_obj = g_new0(struct bt_value_integer, 1); |
| 613 | if (!integer_obj) { |
| 614 | BT_LOGE_STR("Failed to allocate one integer value object."); |
| 615 | goto end; |
| 616 | } |
| 617 | |
| 618 | integer_obj->base = bt_value_create_base(BT_VALUE_TYPE_INTEGER); |
| 619 | integer_obj->value = val; |
| 620 | BT_LOGD("Created integer value object: addr=%p", |
| 621 | integer_obj); |
| 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 | |
| 636 | BT_LOGD("Creating floating point number value object: val=%f", val); |
| 637 | float_obj = g_new0(struct bt_value_float, 1); |
| 638 | if (!float_obj) { |
| 639 | BT_LOGE_STR("Failed to allocate one floating point number value object."); |
| 640 | goto end; |
| 641 | } |
| 642 | |
| 643 | float_obj->base = bt_value_create_base(BT_VALUE_TYPE_FLOAT); |
| 644 | float_obj->value = val; |
| 645 | BT_LOGD("Created floating point number value object: addr=%p", |
| 646 | float_obj); |
| 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) { |
| 662 | BT_LOGW_STR("Invalid parameter: value is NULL."); |
| 663 | goto end; |
| 664 | } |
| 665 | |
| 666 | BT_LOGD("Creating string value object: val-len=%zu", strlen(val)); |
| 667 | string_obj = g_new0(struct bt_value_string, 1); |
| 668 | if (!string_obj) { |
| 669 | BT_LOGE_STR("Failed to allocate one string object."); |
| 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); |
| 675 | if (!string_obj->gstr) { |
| 676 | BT_LOGE_STR("Failed to allocate a GString."); |
| 677 | g_free(string_obj); |
| 678 | string_obj = NULL; |
| 679 | goto end; |
| 680 | } |
| 681 | |
| 682 | BT_LOGD("Created string value object: addr=%p", |
| 683 | string_obj); |
| 684 | |
| 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 | |
| 698 | BT_LOGD_STR("Creating empty array value object."); |
| 699 | array_obj = g_new0(struct bt_value_array, 1); |
| 700 | if (!array_obj) { |
| 701 | BT_LOGE_STR("Failed to allocate one array object."); |
| 702 | goto end; |
| 703 | } |
| 704 | |
| 705 | array_obj->base = bt_value_create_base(BT_VALUE_TYPE_ARRAY); |
| 706 | array_obj->garray = bt_g_ptr_array_new_full(0, |
| 707 | (GDestroyNotify) bt_put); |
| 708 | if (!array_obj->garray) { |
| 709 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
| 710 | g_free(array_obj); |
| 711 | array_obj = NULL; |
| 712 | goto end; |
| 713 | } |
| 714 | |
| 715 | BT_LOGD("Created array value object: addr=%p", |
| 716 | array_obj); |
| 717 | |
| 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 | |
| 726 | BT_LOGD_STR("Creating empty map value object."); |
| 727 | map_obj = g_new0(struct bt_value_map, 1); |
| 728 | if (!map_obj) { |
| 729 | BT_LOGE_STR("Failed to allocate one map object."); |
| 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, |
| 735 | NULL, (GDestroyNotify) bt_put); |
| 736 | if (!map_obj->ght) { |
| 737 | BT_LOGE_STR("Failed to allocate a GHashTable."); |
| 738 | g_free(map_obj); |
| 739 | map_obj = NULL; |
| 740 | goto end; |
| 741 | } |
| 742 | |
| 743 | BT_LOGD("Created map value object: addr=%p", |
| 744 | map_obj); |
| 745 | |
| 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, |
| 751 | bt_bool *val) |
| 752 | { |
| 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; |
| 758 | } |
| 759 | |
| 760 | enum bt_value_status bt_value_bool_set(struct bt_value *bool_obj, bt_bool val) |
| 761 | { |
| 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; |
| 766 | BT_LOGV("Set boolean value's raw value: value-addr=%p, value=%d", |
| 767 | bool_obj, val); |
| 768 | return BT_VALUE_STATUS_OK; |
| 769 | } |
| 770 | |
| 771 | enum bt_value_status bt_value_integer_get(const struct bt_value *integer_obj, |
| 772 | int64_t *val) |
| 773 | { |
| 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; |
| 779 | } |
| 780 | |
| 781 | enum bt_value_status bt_value_integer_set(struct bt_value *integer_obj, |
| 782 | int64_t val) |
| 783 | { |
| 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; |
| 788 | BT_LOGV("Set integer value's raw value: value-addr=%p, value=%" PRId64, |
| 789 | integer_obj, val); |
| 790 | return BT_VALUE_STATUS_OK; |
| 791 | } |
| 792 | |
| 793 | enum bt_value_status bt_value_float_get(const struct bt_value *float_obj, |
| 794 | double *val) |
| 795 | { |
| 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; |
| 801 | } |
| 802 | |
| 803 | enum bt_value_status bt_value_float_set(struct bt_value *float_obj, |
| 804 | double val) |
| 805 | { |
| 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; |
| 810 | BT_LOGV("Set floating point number value's raw value: value-addr=%p, value=%f", |
| 811 | float_obj, val); |
| 812 | return BT_VALUE_STATUS_OK; |
| 813 | } |
| 814 | |
| 815 | enum bt_value_status bt_value_string_get(const struct bt_value *string_obj, |
| 816 | const char **val) |
| 817 | { |
| 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; |
| 823 | } |
| 824 | |
| 825 | enum bt_value_status bt_value_string_set(struct bt_value *string_obj, |
| 826 | const char *val) |
| 827 | { |
| 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); |
| 833 | BT_LOGV("Set string value's raw value: value-addr=%p, raw-value-addr=%p", |
| 834 | string_obj, val); |
| 835 | return BT_VALUE_STATUS_OK; |
| 836 | } |
| 837 | |
| 838 | int64_t bt_value_array_size(const struct bt_value *array_obj) |
| 839 | { |
| 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; |
| 843 | } |
| 844 | |
| 845 | bt_bool bt_value_array_is_empty(const struct bt_value *array_obj) |
| 846 | { |
| 847 | return bt_value_array_size(array_obj) == 0; |
| 848 | } |
| 849 | |
| 850 | struct bt_value *bt_value_array_borrow(const struct bt_value *array_obj, |
| 851 | uint64_t index) |
| 852 | { |
| 853 | struct bt_value_array *typed_array_obj = |
| 854 | BT_VALUE_TO_ARRAY(array_obj); |
| 855 | |
| 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); |
| 860 | return g_ptr_array_index(typed_array_obj->garray, index); |
| 861 | } |
| 862 | |
| 863 | enum bt_value_status bt_value_array_append(struct bt_value *array_obj, |
| 864 | struct bt_value *element_obj) |
| 865 | { |
| 866 | struct bt_value_array *typed_array_obj = |
| 867 | BT_VALUE_TO_ARRAY(array_obj); |
| 868 | |
| 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"); |
| 873 | g_ptr_array_add(typed_array_obj->garray, element_obj); |
| 874 | bt_get(element_obj); |
| 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); |
| 878 | return BT_VALUE_STATUS_OK; |
| 879 | } |
| 880 | |
| 881 | enum bt_value_status bt_value_array_append_bool(struct bt_value *array_obj, |
| 882 | bt_bool val) |
| 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); |
| 889 | bt_put(bool_obj); |
| 890 | return ret; |
| 891 | } |
| 892 | |
| 893 | enum bt_value_status bt_value_array_append_integer( |
| 894 | struct bt_value *array_obj, int64_t val) |
| 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); |
| 901 | bt_put(integer_obj); |
| 902 | return ret; |
| 903 | } |
| 904 | |
| 905 | enum bt_value_status bt_value_array_append_float(struct bt_value *array_obj, |
| 906 | double val) |
| 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); |
| 913 | bt_put(float_obj); |
| 914 | return ret; |
| 915 | } |
| 916 | |
| 917 | enum bt_value_status bt_value_array_append_string(struct bt_value *array_obj, |
| 918 | const char *val) |
| 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); |
| 925 | bt_put(string_obj); |
| 926 | return ret; |
| 927 | } |
| 928 | |
| 929 | enum bt_value_status bt_value_array_append_empty_array( |
| 930 | struct bt_value *array_obj) |
| 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); |
| 937 | bt_put(empty_array_obj); |
| 938 | return ret; |
| 939 | } |
| 940 | |
| 941 | enum bt_value_status bt_value_array_append_empty_map(struct bt_value *array_obj) |
| 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); |
| 948 | bt_put(map_obj); |
| 949 | return ret; |
| 950 | } |
| 951 | |
| 952 | enum bt_value_status bt_value_array_set(struct bt_value *array_obj, |
| 953 | uint64_t index, struct bt_value *element_obj) |
| 954 | { |
| 955 | struct bt_value_array *typed_array_obj = |
| 956 | BT_VALUE_TO_ARRAY(array_obj); |
| 957 | |
| 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); |
| 964 | bt_put(g_ptr_array_index(typed_array_obj->garray, index)); |
| 965 | g_ptr_array_index(typed_array_obj->garray, index) = element_obj; |
| 966 | bt_get(element_obj); |
| 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); |
| 970 | return BT_VALUE_STATUS_OK; |
| 971 | } |
| 972 | |
| 973 | int64_t bt_value_map_size(const struct bt_value *map_obj) |
| 974 | { |
| 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); |
| 978 | } |
| 979 | |
| 980 | bt_bool bt_value_map_is_empty(const struct bt_value *map_obj) |
| 981 | { |
| 982 | return bt_value_map_size(map_obj) == 0; |
| 983 | } |
| 984 | |
| 985 | struct bt_value *bt_value_map_borrow(const struct bt_value *map_obj, |
| 986 | const char *key) |
| 987 | { |
| 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); |
| 991 | return g_hash_table_lookup(BT_VALUE_TO_MAP(map_obj)->ght, |
| 992 | GUINT_TO_POINTER(g_quark_from_string(key))); |
| 993 | } |
| 994 | |
| 995 | bt_bool bt_value_map_has_key(const struct bt_value *map_obj, const char *key) |
| 996 | { |
| 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))); |
| 1002 | } |
| 1003 | |
| 1004 | enum bt_value_status bt_value_map_insert(struct bt_value *map_obj, |
| 1005 | const char *key, struct bt_value *element_obj) |
| 1006 | { |
| 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); |
| 1014 | bt_get(element_obj); |
| 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); |
| 1018 | return BT_VALUE_STATUS_OK; |
| 1019 | } |
| 1020 | |
| 1021 | enum bt_value_status bt_value_map_insert_bool(struct bt_value *map_obj, |
| 1022 | const char *key, bt_bool val) |
| 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); |
| 1029 | bt_put(bool_obj); |
| 1030 | return ret; |
| 1031 | } |
| 1032 | |
| 1033 | enum bt_value_status bt_value_map_insert_integer(struct bt_value *map_obj, |
| 1034 | const char *key, int64_t val) |
| 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); |
| 1041 | bt_put(integer_obj); |
| 1042 | return ret; |
| 1043 | } |
| 1044 | |
| 1045 | enum bt_value_status bt_value_map_insert_float(struct bt_value *map_obj, |
| 1046 | const char *key, double val) |
| 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); |
| 1053 | bt_put(float_obj); |
| 1054 | return ret; |
| 1055 | } |
| 1056 | |
| 1057 | enum bt_value_status bt_value_map_insert_string(struct bt_value *map_obj, |
| 1058 | const char *key, const char *val) |
| 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); |
| 1065 | bt_put(string_obj); |
| 1066 | return ret; |
| 1067 | } |
| 1068 | |
| 1069 | enum bt_value_status bt_value_map_insert_empty_array(struct bt_value *map_obj, |
| 1070 | const char *key) |
| 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); |
| 1077 | bt_put(array_obj); |
| 1078 | return ret; |
| 1079 | } |
| 1080 | |
| 1081 | enum bt_value_status bt_value_map_insert_empty_map(struct bt_value *map_obj, |
| 1082 | const char *key) |
| 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); |
| 1089 | bt_put(empty_map_obj); |
| 1090 | return ret; |
| 1091 | } |
| 1092 | |
| 1093 | enum bt_value_status bt_value_map_foreach(const struct bt_value *map_obj, |
| 1094 | bt_value_map_foreach_cb cb, void *data) |
| 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 | |
| 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); |
| 1104 | g_hash_table_iter_init(&iter, typed_map_obj->ght); |
| 1105 | |
| 1106 | while (g_hash_table_iter_next(&iter, &key, &element_obj)) { |
| 1107 | const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key)); |
| 1108 | |
| 1109 | if (!cb(key_str, element_obj, data)) { |
| 1110 | BT_LOGV("User canceled the loop: key=\"%s\", " |
| 1111 | "value-addr=%p, data=%p", |
| 1112 | key_str, element_obj, data); |
| 1113 | ret = BT_VALUE_STATUS_CANCELED; |
| 1114 | break; |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | return ret; |
| 1119 | } |
| 1120 | |
| 1121 | struct extend_map_element_data { |
| 1122 | struct bt_value *extended_obj; |
| 1123 | bt_bool got_error; |
| 1124 | }; |
| 1125 | |
| 1126 | static |
| 1127 | bt_bool extend_map_element(const char *key, |
| 1128 | struct bt_value *extension_obj_elem, void *data) |
| 1129 | { |
| 1130 | bt_bool ret = BT_TRUE; |
| 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)) { |
| 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); |
| 1145 | goto error; |
| 1146 | } |
| 1147 | |
| 1148 | goto end; |
| 1149 | |
| 1150 | error: |
| 1151 | ret = BT_FALSE; |
| 1152 | extend_data->got_error = BT_TRUE; |
| 1153 | |
| 1154 | end: |
| 1155 | BT_PUT(extension_obj_elem_copy); |
| 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 | |
| 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); |
| 1169 | BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p", |
| 1170 | base_map_obj, extension_obj); |
| 1171 | |
| 1172 | /* Create copy of base map object to start with */ |
| 1173 | extended_obj = bt_value_copy(base_map_obj); |
| 1174 | if (!extended_obj) { |
| 1175 | BT_LOGE("Cannot copy base value: base-value-addr=%p", |
| 1176 | base_map_obj); |
| 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)) { |
| 1188 | BT_LOGE("Cannot iterate on the extension object's elements: " |
| 1189 | "extension-value-addr=%p", extension_obj); |
| 1190 | goto error; |
| 1191 | } |
| 1192 | |
| 1193 | if (extend_data.got_error) { |
| 1194 | BT_LOGE("Failed to successfully iterate on the extension object's elements: " |
| 1195 | "extension-value-addr=%p", extension_obj); |
| 1196 | goto error; |
| 1197 | } |
| 1198 | |
| 1199 | BT_LOGD("Extended map value: extended-value-addr=%p", |
| 1200 | extended_obj); |
| 1201 | goto end; |
| 1202 | |
| 1203 | error: |
| 1204 | BT_PUT(extended_obj); |
| 1205 | |
| 1206 | end: |
| 1207 | return extended_obj; |
| 1208 | } |
| 1209 | |
| 1210 | struct bt_value *bt_value_copy(const struct bt_value *object) |
| 1211 | { |
| 1212 | struct bt_value *copy_obj = NULL; |
| 1213 | |
| 1214 | BT_ASSERT_PRE_NON_NULL(object, "Value object"); |
| 1215 | BT_LOGD("Copying value object: addr=%p", object); |
| 1216 | copy_obj = copy_funcs[object->type](object); |
| 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 | } |
| 1223 | |
| 1224 | return copy_obj; |
| 1225 | } |
| 1226 | |
| 1227 | bt_bool bt_value_compare(const struct bt_value *object_a, |
| 1228 | const struct bt_value *object_b) |
| 1229 | { |
| 1230 | bt_bool ret = BT_FALSE; |
| 1231 | |
| 1232 | BT_ASSERT_PRE_NON_NULL(object_a, "Value object A"); |
| 1233 | BT_ASSERT_PRE_NON_NULL(object_b, "Value object B"); |
| 1234 | |
| 1235 | if (object_a->type != object_b->type) { |
| 1236 | BT_LOGV("Values are different: type mismatch: " |
| 1237 | "value-a-addr=%p, value-b-addr=%p, " |
| 1238 | "value-a-type=%s, value-b-type=%s", |
| 1239 | object_a, object_b, |
| 1240 | bt_value_type_string(object_a->type), |
| 1241 | bt_value_type_string(object_b->type)); |
| 1242 | goto end; |
| 1243 | } |
| 1244 | |
| 1245 | ret = compare_funcs[object_a->type](object_a, object_b); |
| 1246 | |
| 1247 | end: |
| 1248 | return ret; |
| 1249 | } |