eio: fix multiple open/close chain example.
authorCedric Bail <cedric@osg.samsung.com>
Tue, 10 Oct 2017 01:27:56 +0000 (18:27 -0700)
committerCedric Bail <cedric@osg.samsung.com>
Tue, 10 Oct 2017 01:27:56 +0000 (18:27 -0700)
src/examples/eio/efl_io_manager_open_multi.c

index cedb916..9937b9b 100644 (file)
@@ -21,48 +21,62 @@ void error_cb(void *data, const Efl_Event *ev)
    ecore_main_loop_quit();
 }
 
-void done_closing_cb(void *data, const Efl_Event *ev EINA_UNUSED)
+static Eina_Value
+_close_cb(void *data EINA_UNUSED, const Eina_Value array, const Eina_Future *dead EINA_UNUSED)
 {
-   Efl_Io_Manager *job = data;
-   (void) job;
-
    printf("%s closed all files.\n", __FUNCTION__);
 
    ecore_main_loop_quit();
+
+   return array;
 }
 
-void done_open_cb(void *data, const Efl_Event *ev)
+static Eina_Value
+_open_cb(void *data, const Eina_Value array, const Eina_Future *dead EINA_UNUSED)
 {
-   Efl_Future_Event_Success *s = ev->info;
    Efl_Io_Manager *job = data;
-   Eina_Accessor *ac = s->value;
-   Eina_Iterator *it;
-   Eina_Array stack;
-   Eina_File *f;
-   unsigned int i;
+   unsigned int i, len;
+   Eina_Value v = EINA_VALUE_EMPTY;
+   Eina_Future *futures[eina_value_array_count(&array) + 1];
 
-   eina_array_step_set(&stack, sizeof (Eina_Array), 4);
-
-   EINA_ACCESSOR_FOREACH(ac, i, f)
+   EINA_VALUE_ARRAY_FOREACH(&array, len, i, &v)
      {
-        printf("%s opened file %s [%i]\n", __FUNCTION__, eina_file_filename_get(f), i);
-        eina_array_push(&stack, efl_io_manager_close(job, f));
+        if (v.type == EINA_VALUE_TYPE_ERROR)
+          {
+             Eina_Error err;
+
+             eina_value_get(&v, &err);
+             fprintf(stderr, "Something has gone wrong: %s at index: %i\n", eina_error_msg_get(err), i);
+             goto on_error;
+          }
+        else if (v.type == EINA_VALUE_TYPE_FILE)
+          {
+             Eina_File *f;
+
+             eina_value_get(&v, &f);
+
+             futures[i] = efl_io_manager_close(job, f);
+          }
+        else
+          {
+             goto on_error;
+          }
      }
 
-   it = eina_array_iterator_new(&stack);
-   efl_future_then(efl_future_iterator_all(it), &done_closing_cb, &error_cb, NULL, job);
+   futures[i] = EINA_FUTURE_SENTINEL;
 
-   eina_array_flush(&stack);
-}
+   return eina_future_as_value(eina_future_all_array(futures));
 
-Efl_Future *open_file(Efl_Io_Manager *job, const char *path)
-{
-   return efl_io_manager_open(job, path, EINA_FALSE);
+ on_error:
+   for (; i > 0; i--)
+     eina_future_cancel(futures[i - 1]);
+   return v;
 }
 
 int main(int argc, char const *argv[])
 {
    Efl_Io_Manager *job;
+   Eina_Future *futures[3] = { NULL, NULL, EINA_FUTURE_SENTINEL };
    const char *path;
    const char *path2;
 
@@ -79,8 +93,12 @@ int main(int argc, char const *argv[])
    if (argc > 2)
      path2 = argv[2];
 
-   efl_future_then(efl_future_all(open_file(job, path), open_file(job, path2)),
-                   &done_open_cb, &error_cb, NULL, job);
+   futures[0] = efl_io_manager_open(job, path, EINA_FALSE);
+   futures[1] = efl_io_manager_open(job, path2, EINA_FALSE);
+
+   eina_future_chain(eina_future_all_array(futures),
+                     { .cb = _open_cb, .data = job },
+                     { .cb = _close_cb, .data = NULL });
 
    ecore_main_loop_begin();