/*
- * Copyright (c) 2015-2018 Philippe Proulx <pproulx@efficios.com>
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
+ * SPDX-License-Identifier: MIT
*
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
+ * Copyright (c) 2015-2018 Philippe Proulx <pproulx@efficios.com>
*/
#define BT_LOG_TAG "LIB/VALUE"
#include "compat/compiler.h"
#include "common/common.h"
#include "compat/glib.h"
-#include "lib/assert-pre.h"
+#include "lib/assert-cond.h"
#include "lib/value.h"
#include "common/assert.h"
#include "func-status.h"
+#define BT_ASSERT_PRE_DEV_VALUE_HOT(_value, _name) \
+ BT_ASSERT_PRE_DEV_HOT(((struct bt_value *) (_value)), (_name), \
+ ": %!+v", (_value))
+
#define BT_VALUE_TO_BOOL(_base) ((struct bt_value_bool *) (_base))
#define BT_VALUE_TO_INTEGER(_base) ((struct bt_value_integer *) (_base))
#define BT_VALUE_TO_REAL(_base) ((struct bt_value_real *) (_base))
#define BT_VALUE_TO_ARRAY(_base) ((struct bt_value_array *) (_base))
#define BT_VALUE_TO_MAP(_base) ((struct bt_value_map *) (_base))
-#define _BT_ASSERT_PRE_VALUE_IS_TYPE_COND(_value, _type) \
- (((struct bt_value *) (_value))->type == (_type))
-
-#define _BT_ASSERT_PRE_VALUE_IS_TYPE_FMT \
- "Value has the wrong type ID: expected-type=%s, %![value-]+v"
-
-#define BT_ASSERT_PRE_VALUE_IS_TYPE(_value, _type) \
- BT_ASSERT_PRE( \
- _BT_ASSERT_PRE_VALUE_IS_TYPE_COND((_value), (_type)), \
- _BT_ASSERT_PRE_VALUE_IS_TYPE_FMT, \
- bt_common_value_type_string(_type), (_value))
-
-#define BT_ASSERT_PRE_DEV_VALUE_IS_TYPE(_value, _type) \
- BT_ASSERT_PRE_DEV( \
- _BT_ASSERT_PRE_VALUE_IS_TYPE_COND((_value), (_type)), \
- _BT_ASSERT_PRE_VALUE_IS_TYPE_FMT, \
- bt_common_value_type_string(_type), (_value))
-
-#define BT_ASSERT_PRE_DEV_VALUE_HOT(_value, _name) \
- BT_ASSERT_PRE_DEV_HOT(((struct bt_value *) (_value)), (_name), \
- ": %!+v", (_value))
-
static
void bt_value_null_instance_release_func(struct bt_object *obj)
{
struct bt_value *map_obj, bt_value_map_foreach_entry_func func,
void *data)
{
- enum bt_value_map_foreach_entry_status ret = BT_FUNC_STATUS_OK;
+ int status = BT_FUNC_STATUS_OK;
gpointer key, element_obj;
GHashTableIter iter;
struct bt_value_map *typed_map_obj = BT_VALUE_TO_MAP(map_obj);
while (g_hash_table_iter_next(&iter, &key, &element_obj)) {
const char *key_str = g_quark_to_string(GPOINTER_TO_UINT(key));
- if (!func(key_str, element_obj, data)) {
- BT_LOGT("User interrupted the loop: key=\"%s\", "
- "value-addr=%p, data=%p",
- key_str, element_obj, data);
- ret = BT_FUNC_STATUS_INTERRUPTED;
+ status = func(key_str, element_obj, data);
+ BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status);
+ if (status != BT_FUNC_STATUS_OK) {
+ if (status < 0) {
+ BT_LIB_LOGE_APPEND_CAUSE(
+ "User function failed while iterating "
+ "map value entries: "
+ "status=%s, key=\"%s\", "
+ "value-addr=%p, data=%p",
+ bt_common_func_status_string(status),
+ key_str, element_obj, data);
+
+ if (status == BT_FUNC_STATUS_ERROR) {
+ /*
+ * User function error becomes a
+ * user error from this
+ * function's caller's
+ * perspective.
+ */
+ status = BT_FUNC_STATUS_USER_ERROR;
+ }
+ } else {
+ BT_ASSERT(status == BT_FUNC_STATUS_INTERRUPTED);
+ BT_LOGT("User interrupted the loop: status=%s, "
+ "key=\"%s\", value-addr=%p, data=%p",
+ bt_common_func_status_string(status),
+ key_str, element_obj, data);
+ }
+
break;
}
}
- return ret;
+ return status;
}
enum bt_value_map_foreach_entry_const_status bt_value_map_foreach_entry_const(
struct extend_map_element_data {
struct bt_value *base_obj;
- int status;
};
static
-bt_bool extend_map_element(const char *key,
- const struct bt_value *extension_obj_elem, void *data)
+bt_value_map_foreach_entry_const_func_status extend_map_element(
+ const char *key, const struct bt_value *extension_obj_elem,
+ void *data)
{
- bt_bool ret = BT_TRUE;
+ int status;
struct extend_map_element_data *extend_data = data;
struct bt_value *extension_obj_elem_copy = NULL;
/* Copy object which is to replace the current one */
- extend_data->status = bt_value_copy(extension_obj_elem,
- &extension_obj_elem_copy);
- if (extend_data->status) {
+ status = bt_value_copy(extension_obj_elem, &extension_obj_elem_copy);
+ if (status) {
BT_LIB_LOGE_APPEND_CAUSE("Cannot copy map element: %!+v",
extension_obj_elem);
goto error;
BT_ASSERT(extension_obj_elem_copy);
/* Replace in base map value. */
- extend_data->status = bt_value_map_insert_entry(
- extend_data->base_obj, key,
- (void *) extension_obj_elem_copy);
- if (extend_data->status) {
+ status = bt_value_map_insert_entry(extend_data->base_obj, key,
+ extension_obj_elem_copy);
+ if (status) {
BT_LIB_LOGE_APPEND_CAUSE(
"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);
+ key, extend_data->base_obj, extension_obj_elem_copy);
goto error;
}
goto end;
error:
- BT_ASSERT(extend_data->status != BT_FUNC_STATUS_OK);
- ret = BT_FALSE;
+ BT_ASSERT(status < 0);
end:
BT_OBJECT_PUT_REF_AND_RESET(extension_obj_elem_copy);
- return ret;
+ BT_ASSERT(status == BT_FUNC_STATUS_OK ||
+ status == BT_FUNC_STATUS_MEMORY_ERROR);
+ return status;
}
enum bt_value_map_extend_status bt_value_map_extend(
struct bt_value *base_map_obj,
const struct bt_value *extension_obj)
{
+ int status = BT_FUNC_STATUS_OK;
struct extend_map_element_data extend_data = {
.base_obj = NULL,
- .status = BT_FUNC_STATUS_OK,
};
BT_ASSERT_PRE_NO_ERROR();
* in the base map object.
*/
extend_data.base_obj = base_map_obj;
-
- if (bt_value_map_foreach_entry_const(extension_obj, extend_map_element,
- &extend_data)) {
+ status = bt_value_map_foreach_entry_const(extension_obj,
+ extend_map_element, &extend_data);
+ if (status != BT_FUNC_STATUS_OK) {
+ BT_ASSERT(status == BT_FUNC_STATUS_MEMORY_ERROR);
BT_LIB_LOGE_APPEND_CAUSE(
"Cannot iterate on the extension object's elements: "
"%![extension-value-]+v", extension_obj);
}
- return extend_data.status;
+ return status;
}
enum bt_value_copy_status bt_value_copy(const struct bt_value *object,