summaryrefslogtreecommitdiff
path: root/src/examples/ecore
diff options
context:
space:
mode:
authorCedric Bail <cedric@osg.samsung.com>2018-01-10 18:15:47 -0800
committerCedric Bail <cedric@osg.samsung.com>2018-01-10 18:16:25 -0800
commit3412ad9ca300c528b3dcb0c002c4a2a51be68c7e (patch)
tree4b060b247c811e5cfb51f955939e36764fcac943 /src/examples/ecore
parent79d7483fe75f63829792bad7fdcad6662e37c8b0 (diff)
ecore: remove use of efl_main_loop_get from ecore_promise2_example.
Diffstat (limited to 'src/examples/ecore')
-rw-r--r--src/examples/ecore/ecore_promise2_example.c157
1 files changed, 66 insertions, 91 deletions
diff --git a/src/examples/ecore/ecore_promise2_example.c b/src/examples/ecore/ecore_promise2_example.c
index 4c971c9bef..f99ef7815c 100644
--- a/src/examples/ecore/ecore_promise2_example.c
+++ b/src/examples/ecore/ecore_promise2_example.c
@@ -1,15 +1,11 @@
1#define EFL_EO_API_SUPPORT 1 1#include <Efl_Core.h>
2#define EFL_BETA_API_SUPPORT 1
3
4#include <Ecore.h>
5#include <Eina.h>
6#include <stdlib.h> 2#include <stdlib.h>
7#include <errno.h> 3#include <errno.h>
8 4
9typedef struct _Ctx { 5typedef struct _Ctx {
10 Eina_Promise *p; 6 Eina_Promise *p;
11 Eina_Bool should_fail; 7 Eina_Bool should_fail;
12 Ecore_Timer *timer; 8 Eina_Future *timer;
13 Eina_Value *value; 9 Eina_Value *value;
14} Ctx; 10} Ctx;
15 11
@@ -28,44 +24,39 @@ typedef struct _Inner_Promise_Ctx {
28 return _v; \ 24 return _v; \
29 } 25 }
30 26
31static Eina_Bool 27static Eina_Value
32_timeout(void *data) 28_timeout(void *data, const Eina_Value v, const Eina_Future *dead_future EINA_UNUSED)
33{ 29{
34 Ctx *ctx = data; 30 Ctx *ctx = data;
31 if (v.type == EINA_VALUE_TYPE_ERROR) goto end;
35 if (ctx->should_fail) eina_promise_reject(ctx->p, ENETDOWN); 32 if (ctx->should_fail) eina_promise_reject(ctx->p, ENETDOWN);
36 else 33 else
37 { 34 {
38 Eina_Value v; 35 Eina_Value tmp;
39 eina_value_copy(ctx->value, &v); 36 eina_value_copy(ctx->value, &tmp);
40 eina_promise_resolve(ctx->p, v); 37 eina_promise_resolve(ctx->p, tmp);
41 eina_value_free(ctx->value);
42 } 38 }
39 end:
40 if (ctx->value) eina_value_free(ctx->value);
41 ctx->timer = NULL;
43 free(ctx); 42 free(ctx);
44 return EINA_FALSE; 43 return v;
45} 44}
46 45
47static void 46static void
48_promise_cancel(void *data, const Eina_Promise *dead EINA_UNUSED) 47_promise_cancel(void *data, const Eina_Promise *dead EINA_UNUSED)
49{ 48{
50 Ctx *ctx = data; 49 Ctx *ctx = data;
51 if (ctx->timer) ecore_timer_del(ctx->timer); 50 if (ctx->timer) eina_future_cancel(ctx->timer);
52 eina_value_free(ctx->value);
53 free(ctx);
54}
55
56static Eina_Future_Scheduler *
57_future_scheduler_get(void)
58{
59 return efl_loop_future_scheduler_get(efl_main_loop_get());
60} 51}
61 52
62static Ctx * 53static Ctx *
63_promise_ctx_new(Eina_Value *v) 54_promise_ctx_new(Efl_Loop *loop, Eina_Value *v)
64{ 55{
65 Ctx *ctx; 56 Ctx *ctx;
66 ctx = calloc(1, sizeof(Ctx)); 57 ctx = calloc(1, sizeof(Ctx));
67 EINA_SAFETY_ON_NULL_GOTO(ctx, err_ctx); 58 EINA_SAFETY_ON_NULL_GOTO(ctx, err_ctx);
68 ctx->p = eina_promise_new(_future_scheduler_get(), _promise_cancel, ctx); 59 ctx->p = efl_loop_promise_new(loop, _promise_cancel, ctx);
69 EINA_SAFETY_ON_NULL_GOTO(ctx->p, err_timer); 60 EINA_SAFETY_ON_NULL_GOTO(ctx->p, err_timer);
70 ctx->value = v; 61 ctx->value = v;
71 return ctx; 62 return ctx;
@@ -77,13 +68,13 @@ _promise_ctx_new(Eina_Value *v)
77} 68}
78 69
79static Eina_Future * 70static Eina_Future *
80_future_get(Ctx *ctx) 71_future_get(Ctx *ctx, Efl_Loop *loop)
81{ 72{
82 Eina_Future *f; 73 Eina_Future *f;
83 74
84 f = eina_future_new(ctx->p); 75 f = eina_future_new(ctx->p);
85 EINA_SAFETY_ON_NULL_GOTO(f, err_future); 76 EINA_SAFETY_ON_NULL_GOTO(f, err_future);
86 ctx->timer = ecore_timer_add(0.1, _timeout, ctx); 77 ctx->timer = eina_future_then(efl_loop_timeout(loop, 0.1), _timeout, ctx);
87 EINA_SAFETY_ON_NULL_GOTO(ctx->timer, err_timer); 78 EINA_SAFETY_ON_NULL_GOTO(ctx->timer, err_timer);
88 return f; 79 return f;
89 80
@@ -94,32 +85,32 @@ _future_get(Ctx *ctx)
94} 85}
95 86
96static Eina_Future * 87static Eina_Future *
97_fail_future_get(void) 88_fail_future_get(Efl_Loop *loop)
98{ 89{
99 Ctx *ctx = _promise_ctx_new(NULL); 90 Ctx *ctx = _promise_ctx_new(loop, NULL);
100 EINA_SAFETY_ON_NULL_RETURN_VAL(ctx, NULL); 91 EINA_SAFETY_ON_NULL_RETURN_VAL(ctx, NULL);
101 ctx->should_fail = EINA_TRUE; 92 ctx->should_fail = EINA_TRUE;
102 return _future_get(ctx); 93 return _future_get(ctx, loop);
103} 94}
104 95
105static Eina_Future * 96static Eina_Future *
106_str_future_get(void) 97_str_future_get(Efl_Loop *loop)
107{ 98{
108 Eina_Value *v = eina_value_util_string_new(DEFAULT_MSG); 99 Eina_Value *v = eina_value_util_string_new(DEFAULT_MSG);
109 EINA_SAFETY_ON_NULL_RETURN_VAL(v, NULL); 100 EINA_SAFETY_ON_NULL_RETURN_VAL(v, NULL);
110 Ctx *ctx = _promise_ctx_new(v); 101 Ctx *ctx = _promise_ctx_new(loop, v);
111 EINA_SAFETY_ON_NULL_RETURN_VAL(ctx, NULL); 102 EINA_SAFETY_ON_NULL_RETURN_VAL(ctx, NULL);
112 return _future_get(ctx); 103 return _future_get(ctx, loop);
113} 104}
114 105
115static Eina_Future * 106static Eina_Future *
116_int_future_get(void) 107_int_future_get(Efl_Loop *loop, int i)
117{ 108{
118 Eina_Value *v= eina_value_util_int_new(0); 109 Eina_Value *v = eina_value_util_int_new(i);
119 EINA_SAFETY_ON_NULL_RETURN_VAL(v, NULL); 110 EINA_SAFETY_ON_NULL_RETURN_VAL(v, NULL);
120 Ctx *ctx = _promise_ctx_new(v); 111 Ctx *ctx = _promise_ctx_new(loop, v);
121 EINA_SAFETY_ON_NULL_RETURN_VAL(ctx, NULL); 112 EINA_SAFETY_ON_NULL_RETURN_VAL(ctx, NULL);
122 return _future_get(ctx); 113 return _future_get(ctx, loop);
123} 114}
124 115
125static Eina_Value 116static Eina_Value
@@ -155,11 +146,11 @@ _alternate_error_cb(void *data, const Eina_Value v, const Eina_Future *dead_futu
155} 146}
156 147
157static void 148static void
158_alternate_error(void) 149_alternate_error(Efl_Loop *loop)
159{ 150{
160 static Eina_Bool should_fail = EINA_TRUE; 151 static Eina_Bool should_fail = EINA_TRUE;
161 152
162 eina_future_chain(_str_future_get(), 153 eina_future_chain(_str_future_get(loop),
163 {.cb = _alternate_error_cb, .data = &should_fail}, 154 {.cb = _alternate_error_cb, .data = &should_fail},
164 {.cb = _alternate_error_cb, .data = &should_fail}, 155 {.cb = _alternate_error_cb, .data = &should_fail},
165 {.cb = _alternate_error_cb, .data = &should_fail}, 156 {.cb = _alternate_error_cb, .data = &should_fail},
@@ -175,12 +166,12 @@ _simple_err(void *data EINA_UNUSED, const Eina_Value v, const Eina_Future *dead_
175} 166}
176 167
177static void 168static void
178_simple(void) 169_simple(Efl_Loop *loop)
179{ 170{
180 eina_future_chain(_str_future_get(), 171 eina_future_chain(_str_future_get(loop),
181 eina_future_cb_console("Expecting the following message: "DEFAULT_MSG ". Got: ", NULL), 172 eina_future_cb_console("Expecting the following message: "DEFAULT_MSG ". Got: ", NULL),
182 { .cb = _simple_ok, .data = NULL }); 173 { .cb = _simple_ok, .data = NULL });
183 eina_future_chain(_fail_future_get(), 174 eina_future_chain(_fail_future_get(loop),
184 eina_future_cb_console("Expectig network down error. Got: ", NULL), 175 eina_future_cb_console("Expectig network down error. Got: ", NULL),
185 { .cb = _simple_err, .data = NULL }); 176 { .cb = _simple_err, .data = NULL });
186} 177}
@@ -204,9 +195,9 @@ _chain_no_errors_cb(void *data EINA_UNUSED, const Eina_Value v, const Eina_Futur
204} 195}
205 196
206static void 197static void
207_chain_no_errors(void) 198_chain_no_errors(Efl_Loop *loop)
208{ 199{
209 eina_future_chain(_int_future_get(), 200 eina_future_chain(_int_future_get(loop, 0),
210 eina_future_cb_console("Expecting no value. Got: ", NULL), 201 eina_future_cb_console("Expecting no value. Got: ", NULL),
211 {.cb = _chain_no_errors_cb, .data = NULL}, 202 {.cb = _chain_no_errors_cb, .data = NULL},
212 eina_future_cb_console("Expecting number 2. Got: ", NULL), 203 eina_future_cb_console("Expecting number 2. Got: ", NULL),
@@ -228,9 +219,9 @@ _chain_with_error_cb(void *data EINA_UNUSED, const Eina_Value v EINA_UNUSED, con
228} 219}
229 220
230static void 221static void
231_chain_with_error(void) 222_chain_with_error(Efl_Loop *loop)
232{ 223{
233 eina_future_chain(_int_future_get(), 224 eina_future_chain(_int_future_get(loop, 0),
234 { _chain_with_error_cb, NULL }, 225 { _chain_with_error_cb, NULL },
235 eina_future_cb_console("Expecting argument list too long. Got: ", NULL), 226 eina_future_cb_console("Expecting argument list too long. Got: ", NULL),
236 { .cb = _simple_err, .data = NULL }); 227 { .cb = _simple_err, .data = NULL });
@@ -266,19 +257,22 @@ _inner_promise_cancel(void *data, const Eina_Promise *dead EINA_UNUSED)
266} 257}
267 258
268static Eina_Value 259static Eina_Value
269_chain_inner_cb(void *data, const Eina_Value v EINA_UNUSED, const Eina_Future *dead_future EINA_UNUSED) 260_chain_inner_cb(void *data, const Eina_Value v, const Eina_Future *dead_future)
270{ 261{
271 Inner_Promise_Ctx *ctx; 262 Inner_Promise_Ctx *ctx;
272 Eina_Value r; 263 Eina_Value r;
264 int s = 0;
265
266 eina_value_int_get(&v, &s);
273 267
274 ctx = calloc(1, sizeof(Inner_Promise_Ctx)); 268 ctx = calloc(1, sizeof(Inner_Promise_Ctx));
275 EINA_SAFETY_ON_NULL_GOTO(ctx, err); 269 EINA_SAFETY_ON_NULL_GOTO(ctx, err);
276 ctx->promise = eina_promise_new(_future_scheduler_get(), _inner_promise_cancel, ctx); 270 ctx->promise = eina_promise_continue_new(dead_future, _inner_promise_cancel, ctx);
277 EINA_SAFETY_ON_NULL_GOTO(ctx->promise, err); 271 EINA_SAFETY_ON_NULL_GOTO(ctx->promise, err);
278 272
279 printf("Creating a new promise inside the future cb\n"); 273 printf("Creating a new promise inside the future cb\n");
280 ctx->future = eina_future_then(_int_future_get(), 274 ctx->future = eina_future_then(_int_future_get(data, 0),
281 !data ? _delayed_resolve : _delayed_reject, 275 !s ? _delayed_resolve : _delayed_reject,
282 ctx); 276 ctx);
283 return eina_promise_as_value(ctx->promise); 277 return eina_promise_as_value(ctx->promise);
284 278
@@ -297,30 +291,30 @@ _chain_inner_last_cb(void *data EINA_UNUSED, const Eina_Value v, const Eina_Futu
297} 291}
298 292
299static void 293static void
300_chain_inner_no_errors(void) 294_chain_inner_no_errors(Efl_Loop *loop)
301{ 295{
302 eina_future_chain(_int_future_get(), 296 eina_future_chain(_int_future_get(loop, 0),
303 { .cb = _chain_inner_cb, .data = NULL }, 297 { .cb = _chain_inner_cb, .data = loop },
304 eina_future_cb_console("Expecting message: 'Hello from inner future'. Got: ", NULL), 298 eina_future_cb_console("Expecting message: 'Hello from inner future'. Got: ", NULL),
305 { .cb = _chain_inner_last_cb, .data = NULL }); 299 { .cb = _chain_inner_last_cb, .data = NULL });
306} 300}
307 301
308static Eina_Value 302static Eina_Value
309_err_inner_chain(void *data EINA_UNUSED, const Eina_Value v, const Eina_Future *dead_future EINA_UNUSED) 303_err_inner_chain(void *data, const Eina_Value v, const Eina_Future *dead_future EINA_UNUSED)
310{ 304{
311 VALUE_TYPE_CHECK(v, EINA_VALUE_TYPE_ERROR); 305 VALUE_TYPE_CHECK(v, EINA_VALUE_TYPE_ERROR);
312 ecore_main_loop_quit(); 306 efl_loop_quit(data, EINA_VALUE_EMPTY);
313 return v; 307 return v;
314} 308}
315 309
316static void 310static void
317_chain_inner_errors(void) 311_chain_inner_errors(Efl_Loop *loop)
318{ 312{
319 313
320 eina_future_chain(_int_future_get(), 314 eina_future_chain(_int_future_get(loop, 1),
321 { .cb = _chain_inner_cb, .data = (void *)1 }, 315 { .cb = _chain_inner_cb, .data = loop },
322 eina_future_cb_console("Expection network down error. Got: ", NULL), 316 eina_future_cb_console("Expection network down error. Got: ", NULL),
323 { .cb = _err_inner_chain, .data = NULL }); 317 { .cb = _err_inner_chain, .data = loop });
324} 318}
325 319
326static Eina_Value 320static Eina_Value
@@ -331,11 +325,11 @@ _canceled_cb(void *data EINA_UNUSED, const Eina_Value v, const Eina_Future *dead
331} 325}
332 326
333static void 327static void
334_future_cancel(void) 328_future_cancel(Efl_Loop *loop)
335{ 329{
336 Eina_Future *f; 330 Eina_Future *f;
337 331
338 f = eina_future_chain(_int_future_get(), 332 f = eina_future_chain(_int_future_get(loop, 0),
339 eina_future_cb_console("Expecting cancelled operation error. Got: ", NULL), 333 eina_future_cb_console("Expecting cancelled operation error. Got: ", NULL),
340 { .cb = _canceled_cb, .data = NULL }, 334 { .cb = _canceled_cb, .data = NULL },
341 eina_future_cb_console("Expecting cancelled operation error. Got: ", NULL), 335 eina_future_cb_console("Expecting cancelled operation error. Got: ", NULL),
@@ -350,36 +344,17 @@ _future_cancel(void)
350 eina_future_cancel(f); 344 eina_future_cancel(f);
351} 345}
352 346
353int 347EAPI_MAIN void
354main(int argc EINA_UNUSED, char *argv[] EINA_UNUSED) 348efl_main(void *data EINA_UNUSED,
349 const Efl_Event *ev)
355{ 350{
356 if (!eina_init()) 351 _simple(ev->object);
357 { 352 _alternate_error(ev->object);
358 fprintf(stderr, "Could not init eina\n"); 353 _chain_no_errors(ev->object);
359 return EXIT_FAILURE; 354 _chain_with_error(ev->object);
360 } 355 _chain_inner_no_errors(ev->object);
361 356 _chain_inner_errors(ev->object);
362 if (!ecore_init()) 357 _future_cancel(ev->object);
363 {
364 fprintf(stderr, "Could not init ecore\n");
365 goto err_ecore;
366 }
367
368 _simple();
369 _alternate_error();
370 _chain_no_errors();
371 _chain_with_error();
372 _chain_inner_no_errors();
373 _chain_inner_errors();
374 _future_cancel();
375
376 ecore_main_loop_begin();
377
378 eina_shutdown();
379 ecore_shutdown();
380 return EXIT_SUCCESS;
381
382 err_ecore:
383 eina_shutdown();
384 return EXIT_FAILURE;
385} 358}
359
360EFL_MAIN();