From 3695540ce102f230aafd344f3716420d497112cf Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Thu, 19 Mar 2015 14:10:41 -0400 Subject: [PATCH] objects: add bt_object_array_set() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Philippe Proulx Signed-off-by: Jérémie Galarneau --- include/babeltrace/objects.h | 17 +++++++++++++++++ lib/objects.c | 21 +++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/include/babeltrace/objects.h b/include/babeltrace/objects.h index 325afe83..f030424f 100644 --- a/include/babeltrace/objects.h +++ b/include/babeltrace/objects.h @@ -651,6 +651,23 @@ extern int bt_object_array_append_array(struct bt_object *array_obj); */ extern int bt_object_array_append_map(struct bt_object *array_obj); +/** + * Replaces the element object at index \p index of the array object + * \p array_obj by \p element_obj. + * + * The replaced object's reference count is decremented, unless it's + * a null object. The reference count of \p element_obj is incremented, + * unless it's a null object. + * + * @param array_obj Array object + * @param index Index of element object to replace + * @param element_obj New element object at position \p index of + * \p array_obj + * @returns 0 on success, or a negative value on error + */ +extern int bt_object_array_set(struct bt_object *array_obj, size_t index, + struct bt_object *element_obj); + /** * Gets the size of a map object, that is, the number of elements * contained in a map object. diff --git a/lib/objects.c b/lib/objects.c index cd86f976..1439b4d4 100644 --- a/lib/objects.c +++ b/lib/objects.c @@ -857,6 +857,27 @@ int bt_object_array_append_map(struct bt_object *array_obj) return ret; } +int bt_object_array_set(struct bt_object *array_obj, size_t index, + struct bt_object *element_obj) +{ + int ret = 0; + struct bt_object_array *typed_array_obj = + BT_OBJECT_TO_ARRAY(array_obj); + + if (!array_obj || !bt_object_is_array(array_obj) || !element_obj || + index >= typed_array_obj->garray->len) { + ret = -1; + goto end; + } + + bt_object_put(g_ptr_array_index(typed_array_obj->garray, index)); + g_ptr_array_index(typed_array_obj->garray, index) = element_obj; + bt_object_get(element_obj); + +end: + return ret; +} + int bt_object_map_size(const struct bt_object *map_obj) { int ret; -- 2.34.1