values: make `bt_value_map_extend()` extend the provided base map
[babeltrace.git] / src / lib / value.c
index 42c4bfd1dcd403d17cc429020bfb25f4815866af..9cef6ef2cc9d903915eae3be5e78307aa75a2e17 100644 (file)
@@ -136,6 +136,9 @@ void (* const destroy_funcs[])(struct bt_value *) = {
 static
 struct bt_value *bt_value_null_copy(const struct bt_value *null_obj)
 {
+       BT_ASSERT(null_obj == bt_value_null);
+
+       bt_object_get_ref_no_null_check(bt_value_null);
        return (void *) bt_value_null;
 }
 
@@ -294,11 +297,11 @@ struct bt_value *(* const copy_funcs[])(const struct bt_value *) = {
 };
 
 static
-bt_bool bt_value_null_compare(const struct bt_value *object_a,
+bt_bool bt_value_null_is_equal(const struct bt_value *object_a,
                const struct bt_value *object_b)
 {
        /*
-        * Always BT_TRUE since bt_value_compare() already checks if both
+        * Always BT_TRUE since bt_value_is_equal() already checks if both
         * object_a and object_b have the same type, and in the case of
         * null value objects, they're always the same if it is so.
         */
@@ -306,7 +309,7 @@ bt_bool bt_value_null_compare(const struct bt_value *object_a,
 }
 
 static
-bt_bool bt_value_bool_compare(const struct bt_value *object_a,
+bt_bool bt_value_bool_is_equal(const struct bt_value *object_a,
                const struct bt_value *object_b)
 {
        if (BT_VALUE_TO_BOOL(object_a)->value !=
@@ -322,7 +325,7 @@ bt_bool bt_value_bool_compare(const struct bt_value *object_a,
 }
 
 static
-bt_bool bt_value_integer_compare(const struct bt_value *object_a,
+bt_bool bt_value_integer_is_equal(const struct bt_value *object_a,
                const struct bt_value *object_b)
 {
        if (BT_VALUE_TO_INTEGER(object_a)->value.u !=
@@ -346,7 +349,7 @@ bt_bool bt_value_integer_compare(const struct bt_value *object_a,
 }
 
 static
-bt_bool bt_value_real_compare(const struct bt_value *object_a,
+bt_bool bt_value_real_is_equal(const struct bt_value *object_a,
                const struct bt_value *object_b)
 {
        if (BT_VALUE_TO_REAL(object_a)->value !=
@@ -362,7 +365,7 @@ bt_bool bt_value_real_compare(const struct bt_value *object_a,
 }
 
 static
-bt_bool bt_value_string_compare(const struct bt_value *object_a,
+bt_bool bt_value_string_is_equal(const struct bt_value *object_a,
                const struct bt_value *object_b)
 {
        if (strcmp(BT_VALUE_TO_STRING(object_a)->gstr->str,
@@ -378,7 +381,7 @@ bt_bool bt_value_string_compare(const struct bt_value *object_a,
 }
 
 static
-bt_bool bt_value_array_compare(const struct bt_value *object_a,
+bt_bool bt_value_array_is_equal(const struct bt_value *object_a,
                const struct bt_value *object_b)
 {
        int i;
@@ -407,7 +410,7 @@ bt_bool bt_value_array_compare(const struct bt_value *object_a,
                element_obj_b = bt_value_array_borrow_element_by_index_const(
                        object_b, i);
 
-               if (!bt_value_compare(element_obj_a, element_obj_b)) {
+               if (!bt_value_is_equal(element_obj_a, element_obj_b)) {
                        BT_LOGT("Array values's elements are different: "
                                "value-a-addr=%p, value-b-addr=%p, index=%d",
                                element_obj_a, element_obj_b, i);
@@ -421,7 +424,7 @@ end:
 }
 
 static
-bt_bool bt_value_map_compare(const struct bt_value *object_a,
+bt_bool bt_value_map_is_equal(const struct bt_value *object_a,
                const struct bt_value *object_b)
 {
        bt_bool ret = BT_TRUE;
@@ -450,7 +453,7 @@ bt_bool bt_value_map_compare(const struct bt_value *object_a,
                element_obj_b = bt_value_map_borrow_entry_value_const(object_b,
                        key_str);
 
-               if (!bt_value_compare(element_obj_a, element_obj_b)) {
+               if (!bt_value_is_equal(element_obj_a, element_obj_b)) {
                        BT_LOGT("Map values's elements are different: "
                                "value-a-addr=%p, value-b-addr=%p, key=\"%s\"",
                                element_obj_a, element_obj_b, key_str);
@@ -464,16 +467,16 @@ end:
 }
 
 static
-bt_bool (* const compare_funcs[])(const struct bt_value *,
+bt_bool (* const is_equal_funcs[])(const struct bt_value *,
                const struct bt_value *) = {
-       [BT_VALUE_TYPE_NULL] =                  bt_value_null_compare,
-       [BT_VALUE_TYPE_BOOL] =                  bt_value_bool_compare,
-       [BT_VALUE_TYPE_UNSIGNED_INTEGER] =      bt_value_integer_compare,
-       [BT_VALUE_TYPE_SIGNED_INTEGER] =        bt_value_integer_compare,
-       [BT_VALUE_TYPE_REAL] =                  bt_value_real_compare,
-       [BT_VALUE_TYPE_STRING] =                bt_value_string_compare,
-       [BT_VALUE_TYPE_ARRAY] =                 bt_value_array_compare,
-       [BT_VALUE_TYPE_MAP] =                   bt_value_map_compare,
+       [BT_VALUE_TYPE_NULL] =                  bt_value_null_is_equal,
+       [BT_VALUE_TYPE_BOOL] =                  bt_value_bool_is_equal,
+       [BT_VALUE_TYPE_UNSIGNED_INTEGER] =      bt_value_integer_is_equal,
+       [BT_VALUE_TYPE_SIGNED_INTEGER] =        bt_value_integer_is_equal,
+       [BT_VALUE_TYPE_REAL] =                  bt_value_real_is_equal,
+       [BT_VALUE_TYPE_STRING] =                bt_value_string_is_equal,
+       [BT_VALUE_TYPE_ARRAY] =                 bt_value_array_is_equal,
+       [BT_VALUE_TYPE_MAP] =                   bt_value_map_is_equal,
 };
 
 static
@@ -994,7 +997,8 @@ bt_value_array_append_string_element(struct bt_value *array_obj,
 }
 
 enum bt_value_array_append_element_status
-bt_value_array_append_empty_array_element(struct bt_value *array_obj)
+bt_value_array_append_empty_array_element(struct bt_value *array_obj,
+               struct bt_value **element_obj)
 {
        enum bt_value_array_append_element_status ret;
        struct bt_value *empty_array_obj = NULL;
@@ -1002,12 +1006,18 @@ bt_value_array_append_empty_array_element(struct bt_value *array_obj)
        empty_array_obj = bt_value_array_create();
        ret = bt_value_array_append_element(array_obj,
                (void *) empty_array_obj);
+
+       if (element_obj) {
+               *element_obj = empty_array_obj;
+       }
+
        bt_object_put_ref(empty_array_obj);
        return ret;
 }
 
 enum bt_value_array_append_element_status
-bt_value_array_append_empty_map_element(struct bt_value *array_obj)
+bt_value_array_append_empty_map_element(struct bt_value *array_obj,
+               struct bt_value **element_obj)
 {
        enum bt_value_array_append_element_status ret;
        struct bt_value *map_obj = NULL;
@@ -1015,6 +1025,11 @@ bt_value_array_append_empty_map_element(struct bt_value *array_obj)
        map_obj = bt_value_map_create();
        ret = bt_value_array_append_element(array_obj,
                (void *) map_obj);
+
+       if (element_obj) {
+               *element_obj = map_obj;
+       }
+
        bt_object_put_ref(map_obj);
        return ret;
 }
@@ -1160,7 +1175,8 @@ enum bt_value_map_insert_entry_status bt_value_map_insert_string_entry(
 
 enum bt_value_map_insert_entry_status
 bt_value_map_insert_empty_array_entry(
-               struct bt_value *map_obj, const char *key)
+               struct bt_value *map_obj, const char *key,
+               bt_value **entry_obj)
 {
        enum bt_value_map_insert_entry_status ret;
        struct bt_value *array_obj = NULL;
@@ -1168,12 +1184,18 @@ bt_value_map_insert_empty_array_entry(
        array_obj = bt_value_array_create();
        ret = bt_value_map_insert_entry(map_obj, key,
                (void *) array_obj);
+
+       if (entry_obj) {
+               *entry_obj = array_obj;
+       }
+
        bt_object_put_ref(array_obj);
        return ret;
 }
 
 enum bt_value_map_insert_entry_status
-bt_value_map_insert_empty_map_entry(struct bt_value *map_obj, const char *key)
+bt_value_map_insert_empty_map_entry(struct bt_value *map_obj, const char *key,
+               bt_value **entry_obj)
 {
        enum bt_value_map_insert_entry_status ret;
        struct bt_value *empty_map_obj = NULL;
@@ -1181,6 +1203,11 @@ bt_value_map_insert_empty_map_entry(struct bt_value *map_obj, const char *key)
        empty_map_obj = bt_value_map_create();
        ret = bt_value_map_insert_entry(map_obj, key,
                (void *) empty_map_obj);
+
+       if (entry_obj) {
+               *entry_obj = empty_map_obj;
+       }
+
        bt_object_put_ref(empty_map_obj);
        return ret;
 }
@@ -1223,7 +1250,7 @@ enum bt_value_map_foreach_entry_const_status bt_value_map_foreach_entry_const(
 }
 
 struct extend_map_element_data {
-       struct bt_value *extended_obj;
+       struct bt_value *base_obj;
        int status;
 };
 
@@ -1246,15 +1273,15 @@ bt_bool extend_map_element(const char *key,
 
        BT_ASSERT(extension_obj_elem_copy);
 
-       /* Replace in extended object */
+       /* Replace in base map value. */
        extend_data->status = bt_value_map_insert_entry(
-               extend_data->extended_obj, key,
+               extend_data->base_obj, key,
                (void *) extension_obj_elem_copy);
        if (extend_data->status) {
                BT_LIB_LOGE_APPEND_CAUSE(
-                       "Cannot replace value in extended value: key=\"%s\", "
-                       "%![extended-value-]+v, %![element-value-]+v",
-                       key, extend_data->extended_obj,
+                       "Cannot replace value in base map value: key=\"%s\", "
+                       "%![base-map-value-]+v, %![element-value-]+v",
+                       key, extend_data->base_obj,
                        extension_obj_elem_copy);
                goto error;
        }
@@ -1271,66 +1298,35 @@ end:
 }
 
 enum bt_value_map_extend_status bt_value_map_extend(
-               const struct bt_value *base_map_obj,
-               const struct bt_value *extension_obj,
-               struct bt_value **extended_map_obj)
+               struct bt_value *base_map_obj,
+               const struct bt_value *extension_obj)
 {
        struct extend_map_element_data extend_data = {
-               .extended_obj = NULL,
+               .base_obj = NULL,
                .status = BT_FUNC_STATUS_OK,
        };
 
        BT_ASSERT_PRE_NON_NULL(base_map_obj, "Base value object");
+       BT_ASSERT_PRE_DEV_VALUE_HOT(base_map_obj, "Base value object");
        BT_ASSERT_PRE_NON_NULL(extension_obj, "Extension value object");
-       BT_ASSERT_PRE_NON_NULL(extended_map_obj,
-               "Extended value object (output)");
        BT_ASSERT_PRE_VALUE_IS_TYPE(base_map_obj, BT_VALUE_TYPE_MAP);
        BT_ASSERT_PRE_VALUE_IS_TYPE(extension_obj, BT_VALUE_TYPE_MAP);
        BT_LOGD("Extending map value: base-value-addr=%p, extension-value-addr=%p",
                base_map_obj, extension_obj);
-       *extended_map_obj = NULL;
-
-       /* Create copy of base map object to start with */
-       extend_data.status = bt_value_copy(base_map_obj, extended_map_obj);
-       if (extend_data.status) {
-               BT_LIB_LOGE_APPEND_CAUSE(
-                       "Cannot copy base value: %![base-value-]+v",
-                       base_map_obj);
-               goto error;
-       }
-
-       BT_ASSERT(extended_map_obj);
 
        /*
         * For each key in the extension map object, replace this key
-        * in the copied map object.
+        * in the base map object.
         */
-       extend_data.extended_obj = *extended_map_obj;
+       extend_data.base_obj = base_map_obj;
 
        if (bt_value_map_foreach_entry_const(extension_obj, extend_map_element,
                        &extend_data)) {
                BT_LIB_LOGE_APPEND_CAUSE(
                        "Cannot iterate on the extension object's elements: "
                        "%![extension-value-]+v", extension_obj);
-               goto error;
        }
 
-       if (extend_data.status) {
-               BT_LIB_LOGE_APPEND_CAUSE(
-                       "Failed to successfully iterate on the extension object's elements: "
-                       "%![extension-value-]+v", extension_obj);
-               goto error;
-       }
-
-       BT_LOGD("Extended map value: extended-value-addr=%p",
-               *extended_map_obj);
-       goto end;
-
-error:
-       BT_OBJECT_PUT_REF_AND_RESET(*extended_map_obj);
-       *extended_map_obj = NULL;
-
-end:
        return extend_data.status;
 }
 
@@ -1355,7 +1351,7 @@ enum bt_value_copy_status bt_value_copy(const struct bt_value *object,
        return status;
 }
 
-bt_bool bt_value_compare(const struct bt_value *object_a,
+bt_bool bt_value_is_equal(const struct bt_value *object_a,
        const struct bt_value *object_b)
 {
        bt_bool ret = BT_FALSE;
@@ -1373,7 +1369,7 @@ bt_bool bt_value_compare(const struct bt_value *object_a,
                goto end;
        }
 
-       ret = compare_funcs[object_a->type](object_a, object_b);
+       ret = is_equal_funcs[object_a->type](object_a, object_b);
 
 end:
        return ret;
This page took 0.029059 seconds and 4 git commands to generate.