objects: introduce enum bt_object_status
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Tue, 17 Mar 2015 21:19:56 +0000 (17:19 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 19 Mar 2015 18:13:35 +0000 (14:13 -0400)
Those new statuses make it possible for
bt_object_map_foreach() to report if its loop was
cancelled by the user function or if a general error
occured.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
include/babeltrace/objects.h
lib/objects.c
tests/lib/test_bt_objects.c

index f030424f283d6ead079eb999a2af4726746308df..3d02b2571df4113adb241606510e926ca7888f8a 100644 (file)
@@ -161,6 +161,20 @@ enum bt_object_type {
        BT_OBJECT_TYPE_MAP =            6,
 };
 
+/**
+ * Status (return value of some functions).
+ */
+enum bt_object_status {
+       /** Operation cancelled. */
+       BT_OBJECT_STATUS_CANCELLED =    -2,
+
+       /** Error. */
+       BT_OBJECT_STATUS_ERROR =        -1,
+
+       /** Okay, no error. */
+       BT_OBJECT_STATUS_OK =           0,
+};
+
 /**
  * Object.
  */
@@ -716,12 +730,21 @@ extern struct bt_object *bt_object_map_get(const struct bt_object *map_obj,
  * @param map_obj      Map object
  * @param cb           User function to call back
  * @param data         User data passed to the user function
- * @returns            0 on success, or a negative value on error
- *                     (the user function breaking the loop is \b not
- *                     considered an error here)
- */
-extern int bt_object_map_foreach(const struct bt_object *map_obj,
-       bt_object_map_foreach_cb cb, void *data);
+ * @returns            \link bt_object_status::BT_OBJECT_STATUS_OK
+ *                     <code>BT_OBJECT_STATUS_OK</code>\endlink if
+ *                     there's no error and the traversal was not
+ *                     cancelled by the user function,
+ *                     \link bt_object_status::BT_OBJECT_STATUS_CANCELLED
+ *                     <code>BT_OBJECT_STATUS_CANCELLED</code>\endlink
+ *                     if the function was cancelled by the user
+ *                     function, or
+ *                     \link bt_object_status::BT_OBJECT_STATUS_ERROR
+ *                     <code>BT_OBJECT_STATUS_ERROR</code>\endlink on
+ *                     any other error
+ */
+extern enum bt_object_status bt_object_map_foreach(
+       const struct bt_object *map_obj, bt_object_map_foreach_cb cb,
+       void *data);
 
 /**
  * Returns whether or not the map object \p map_obj contains the
index 1439b4d48085374e923427a637067491e9a05e99..f59d84dc139069b0126a76abd7de21e0c2f767a2 100644 (file)
@@ -1043,13 +1043,13 @@ int bt_object_map_insert_map(struct bt_object *map_obj,
 int bt_object_map_foreach(const struct bt_object *map_obj,
        bt_object_map_foreach_cb cb, void *data)
 {
-       int ret = 0;
+       enum bt_object_status ret = BT_OBJECT_STATUS_OK;
        gpointer key, element_obj;
        GHashTableIter iter;
        struct bt_object_map *typed_map_obj = BT_OBJECT_TO_MAP(map_obj);
 
        if (!map_obj || !bt_object_is_map(map_obj) || !cb) {
-               ret = -1;
+               ret = BT_OBJECT_STATUS_ERROR;
                goto end;
        }
 
@@ -1059,6 +1059,7 @@ int bt_object_map_foreach(const struct bt_object *map_obj,
                const char *key_str = g_quark_to_string((unsigned long) key);
 
                if (!cb(key_str, element_obj, data)) {
+                       ret = BT_OBJECT_STATUS_CANCELLED;
                        break;
                }
        }
index 1589057d104299be4de3f6353349fd5374689760..07774e45652bc5d0b1446fbff1b0c0a0a5d26035 100644 (file)
@@ -668,17 +668,20 @@ void test_map(void)
                "map object has key \"map2\"");
 
        ret = bt_object_map_foreach(NULL, test_map_foreach_cb_count, &count);
-       ok(ret, "bt_object_map_foreach() fails with a map object set to NULL");
+       ok(ret == BT_OBJECT_STATUS_ERROR,
+               "bt_object_map_foreach() fails with a map object set to NULL");
        ret = bt_object_map_foreach(map_obj, NULL, &count);
-       ok(ret, "bt_object_map_foreach() fails with a user function set to NULL");
+       ok(ret == BT_OBJECT_STATUS_ERROR,
+               "bt_object_map_foreach() fails with a user function set to NULL");
        ret = bt_object_map_foreach(map_obj, test_map_foreach_cb_count, &count);
-       ok(!ret && count == 3,
+       ok(ret == BT_OBJECT_STATUS_CANCELLED && count == 3,
                "bt_object_map_foreach() breaks the loop when the user function returns false");
 
        memset(&checklist, 0, sizeof(checklist));
        ret = bt_object_map_foreach(map_obj, test_map_foreach_cb_check,
                &checklist);
-       ok(!ret, "bt_object_map_foreach() succeeds with test_map_foreach_cb_check()");
+       ok(ret == BT_OBJECT_STATUS_OK,
+               "bt_object_map_foreach() succeeds with test_map_foreach_cb_check()");
        ok(checklist.bool1 && checklist.int1 && checklist.float1 &&
                checklist.null1 && checklist.bool2 && checklist.int2 &&
                checklist.float2 && checklist.string2 &&
This page took 0.02741 seconds and 4 git commands to generate.