1 #ifndef _EINA_PROMISE_H_
2 #define _EINA_PROMISE_H_
3 
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7 
8 #include "eina_safety_checks.h"
9 #include "eina_types.h"
10 #include "eina_value.h"
11 
12 /** @file */
13 
14 /** @struct Eina_Promise
15  * @ingroup Eina_Promise
16  * An opaque structure representing a piece of data that will be available at a later point.
17  * @typedef struct _Eina_Promise Eina_Promise
18  * Convenience wrapper.
19  */
20 typedef struct _Eina_Promise Eina_Promise;
21 
22 /** @struct Eina_Future
23  * @ingroup Eina_Future
24  * An opaque structure representing a callback to be called when a promise is fulfilled.
25  * @typedef struct _Eina_Future Eina_Future
26  * Convenience wrapper.
27  */
28 typedef struct _Eina_Future Eina_Future;
29 
30 /** @typedef struct _Eina_Future_Desc Eina_Future_Desc
31  * @ingroup Eina_Future
32  * Convenience wrapper over #_Eina_Future_Desc. */
33 typedef struct _Eina_Future_Desc Eina_Future_Desc;
34 
35 /** @typedef struct _Eina_Future_Race_Result Eina_Future_Race_Result
36  * @ingroup Eina_Future
37  * Convenience wrapper over #_Eina_Future_Race_Result. */
38 typedef struct _Eina_Future_Race_Result Eina_Future_Race_Result;
39 
40 /**
41  * @defgroup Eina_Future_Callbacks Eina Future Callbacks
42  * @ingroup Eina_Future
43  * @brief Methods and structures dealing with #Eina_Future callbacks
44  * @{
45  */
46 
47 /** @typedef struct _Eina_Future_Cb_Easy_Desc Eina_Future_Cb_Easy_Desc
48  * Convenience wrapper over #_Eina_Future_Cb_Easy_Desc. */
49 typedef struct _Eina_Future_Cb_Easy_Desc Eina_Future_Cb_Easy_Desc;
50 
51 /** @typedef struct _Eina_Future_Cb_Console_Desc Eina_Future_Cb_Console_Desc
52  * Convenience wrapper over _Eina_Future_Cb_Console_Desc. */
53 typedef struct _Eina_Future_Cb_Console_Desc Eina_Future_Cb_Console_Desc;
54 
55 /** @typedef struct _Eina_Future_Scheduler Eina_Future_Scheduler
56  * Convenience wrapper over _Eina_Future_Scheduler. */
57 typedef struct _Eina_Future_Scheduler Eina_Future_Scheduler;
58 
59 /** @typedef struct _Eina_Future_Schedule_Entry Eina_Future_Schedule_Entry
60  * Convenience wrapper over _Eina_Future_Schedule_Entry. */
61 typedef struct _Eina_Future_Schedule_Entry Eina_Future_Schedule_Entry;
62 
63 /** @typedef struct _Eina_Future_Cb_Log_Desc Eina_Future_Cb_Log_Desc
64  * Convenience wrapper over _Eina_Future_Cb_Log_Desc. */
65 typedef struct _Eina_Future_Cb_Log_Desc Eina_Future_Cb_Log_Desc;
66 
67 /**
68  * @typedef Eina_Future_Cb
69 
70  * A callback used to inform that a future was resolved.
71  * Usually this callback is called from a clean context, that is, from the
72  * main loop or some platform defined safe context. However there are
73  * 2 exceptions:
74  *
75  * @li eina_future_cancel() was used, it's called immediately in the
76  * context that called cancel using `ECANCELED` as error.
77  *
78  * @li eina_future_then(), eina_future_then_from_desc(), eina_future_chain(), eina_future_chain_array()
79  * or similar failed due to invalid pointer or memory allocation. Then the callback is called from the
80  * failed context using `EINVAL` or `ENOMEM` as errors and @p dead_future will be @c NULL.
81  *
82  * @param[in] data The data provided by the user
83  *
84  * @param[in] value An #Eina_Value which contains the operation result. Before using
85  * the @p value, its type must be checked in order to avoid errors. This is needed because
86  * if an operation fails the #Eina_Value type will be #EINA_VALUE_TYPE_ERROR
87  * which is a different type than the expected operation result.
88  *
89  * @param[in] dead_future A pointer to the future that was completed.
90  *
91  * @return An #Eina_Value to pass to the next #Eina_Future in the chain (if any).
92  * If there is no need to convert the received value, it's @b recommended
93  * to passthrough the @p value argument. If you need to convert to a different type
94  * or generate a new value, use @c eina_value_setup() on @b another #Eina_Value
95  * and return it. By returning a promise #Eina_Value (eina_promise_as_value()) the
96  * whole chain will wait until the promise is resolved in
97  * order to continue its execution.
98  * Note that the value contents must survive this function scope,
99  * that is, do @b not use stack allocated blobs, arrays, structures or types that
100  * keep references to memory you give. Values will be automatically cleaned up
101  * using @c eina_value_flush() once they are unused (no more future or futures
102  * returned a new value).
103  *
104  * @note The returned value @b can be an #EINA_VALUE_TYPE_PROMISE! (see eina_promise_as_value() and
105  * eina_future_as_value()) In this case the future chain will wait until the promise is resolved.
106  *
107  * @see eina_future_cancel()
108  * @see eina_future_then()
109  * @see eina_future_then_from_desc()
110  * @see eina_future_then_easy()
111  * @see eina_future_chain()
112  * @see eina_future_chain_array()
113  * @see eina_future_as_value()
114  * @see eina_promise_as_value()
115  */
116 typedef Eina_Value (*Eina_Future_Cb)(void *data, const Eina_Value value, const Eina_Future *dead_future);
117 
118 /**
119  * @struct _Eina_Future_Schedule_Entry
120  *
121  * A struct that represents an scheduled event.
122  * This struct may be used by Eina to cancel
123  * a scheduled future.
124  *
125  * @see eina_promise_new()
126  *
127  * @see Eina_Future_Scheduler
128  * @see Eina_Future_Scheduler_Cb
129  */
130 struct _Eina_Future_Schedule_Entry {
131    /**
132     * The scheduler used to create the entry.
133     * @note This must not be @c NULL.
134     */
135    Eina_Future_Scheduler *scheduler;
136 };
137 
138 
139 /**
140  * @typedef Eina_Future_Scheduler_Cb
141  * A callback used by the #Eina_Future_Scheduler to deliver
142  * the future operation result.
143  *
144  * @param[out] f The delivered future.
145  * @param[in] value The future result
146  *
147  *
148  * @see eina_promise_new()
149  *
150  * @see Eina_Future_Schedule_Entry
151  * @see Eina_Future_Scheduler
152  */
153 typedef void (*Eina_Future_Scheduler_Cb)(Eina_Future *f, Eina_Value value);
154 
155 /**
156  * @struct _Eina_Future_Scheduler
157  * This struct is used as a bridge between Eina and the future scheduler.
158  * By using the functions provided by _Eina_Future_Scheduler Eina can
159  * schedule futures resolutions, rejections and cancellations to a safe context.
160  *
161  * @see eina_promise_new()
162  * @see Eina_Future_Schedule_Entry
163  * @see Eina_Future_Scheduler_Cb
164  */
165 struct _Eina_Future_Scheduler {
166    /**
167     * Called by @p Eina_Future when a delivery must be scheduled to a safe context.
168     * i.e.: after @p eina_promise_resolve()
169     *
170     * @note Must not be @c NULL
171     *
172     * Must call back from a safe context using @p cb(f,value)
173     * @param[in,out] scheduler The scheduler to use.
174     * @param[in] cb The Eina_Future_Scheduler_Cb to be called and deliver the @p f and @p value.
175     * @param[in] f The future to be delivered to @p cb
176     * @param[in] value The value to be delivered to @p cb
177     * @return A scheduled entry or @c NULL on error
178     */
179    Eina_Future_Schedule_Entry *(*schedule)(Eina_Future_Scheduler *scheduler, Eina_Future_Scheduler_Cb cb, Eina_Future *f, Eina_Value value);
180    /**
181     * Called by @p Eina_Future when a delivery must be canceled.
182     * i.e.: after @p eina_future_cancel()
183     *
184     * @note Must not be @c NULL.
185     *
186     * @param[in,out] entry The scheduled event to cancel
187     */
188    void (*recall)(Eina_Future_Schedule_Entry *entry);
189 };
190 
191 /**
192  * @typedef void (*Eina_Promise_Cancel_Cb) (void *data, const Eina_Promise *dead_promise)
193  *
194  * A callback used to inform that a promise was canceled.
195  *
196  * A promise may be canceled by the user calling `eina_future_cancel()`
197  * on any #Eina_Future that is part of the chain that uses an #Eina_Promise,
198  * that will cancel the whole chain and then the promise.
199  *
200  * It should stop all asynchronous operations or at least mark them to be
201  * discarded instead of resolved. Actually it can't be resolved once
202  * canceled since the given pointer @c dead_promise is now invalid.
203  *
204  * @note @li This callback is @b mandatory for a reason, do not provide an empty
205  *       callback as it'll likely result in memory corruption and invalid access.
206  *       If impossible to cancel an asynchronous task, then create an
207  *       intermediate memory to hold #Eina_Promise and make it @c NULL,
208  *       in this callback. Then prior to resolution check if the pointer is set.
209  *
210  * @note @li This callback is @b not called if eina_promise_resolve() or
211  *       eina_promise_reject() are used. If any cleanup is needed, then
212  *       call yourself. It's only meant as cancellation, not a general
213  *       "free callback".
214  *
215  * @param[in] data The data provided by the user.
216  * @param[in] dead_promise The canceled promise.
217  * @see eina_promise_reject()
218  * @see eina_promise_resolve()
219  * @see eina_future_cancel()
220  */
221 typedef void (*Eina_Promise_Cancel_Cb) (void *data, const Eina_Promise *dead_promise);
222 
223 /**
224  * @typedef Eina_Future_Success_Cb
225  *
226  * A callback used to inform that the future completed with success.
227  *
228  * Unlike #Eina_Future_Cb this callback is only called if the future operation was successful, this is,
229  * no errors occurred (@p value type differs from #EINA_VALUE_TYPE_ERROR)
230  * and the @p value matches _Eina_Future_Cb_Easy_Desc::success_type (if given).
231  * In case _Eina_Future_Cb_Easy_Desc::success_type was not supplied (it's @c NULL) the @p value type
232  * must be checked before using it.
233  *
234  * @note This function is always called from a safe context (main loop or some platform defined safe context).
235  *
236  * @param[in] data The data provided by the user.
237  * @param[in] value The operation result
238  * @return An Eina_Value to pass to the next Eina_Future in the chain (if any).
239  * If there is no need to convert the received value, it's @b recommended
240  * to passthrough @p value argument. If you need to convert to a different type
241  * or generate a new value, use @c eina_value_setup() on @b another Eina_Value
242  * and return it. By returning a promise Eina_Value (eina_promise_as_value()) the
243  * whole chain will wait until the promise is resolved in
244  * order to continue its execution.
245  * Note that the value contents must survive this function scope,
246  * that is, do @b not use stack allocated blobs, arrays, structures or types that
247  * keep references to memory you give. Values will be automatically cleaned up
248  * using @c eina_value_flush() once they are unused (no more future or futures
249  * returned a new value).
250  * @see eina_future_cb_easy_from_desc()
251  * @see eina_future_cb_easy()
252  */
253 typedef Eina_Value (*Eina_Future_Success_Cb)(void *data, const Eina_Value value);
254 
255 /**
256  * @typedef Eina_Future_Error_Cb
257  *
258  * A callback used to inform that the future completed with failure.
259  *
260  * Unlike #Eina_Future_Success_Cb this function is only called when an error
261  * occurs during the future process or when _Eina_Future_Cb_Easy_Desc::success_type
262  * differs from the future result.
263  * On future creation errors and future cancellation this function will be called
264  * from the current context with the following errors: `EINVAL`, `ENOMEM` and  `ECANCELED`.
265  * Otherwise this function is called from a safe context.
266  *
267  * If it was possible to recover from an error this function should return an empty value
268  * #EINA_VALUE_EMPTY or any other #Eina_Value type that differs from #EINA_VALUE_TYPE_ERROR.
269  * In this case the error will not be reported by the other futures in the chain (if any), otherwise
270  * if an #Eina_Value type #EINA_VALUE_TYPE_ERROR is returned the error will continue to be reported
271  * to the other futures in the chain.
272  *
273  * @param[in] data The data provided by the user.
274  * @param[in] error The operation error
275  * @return An #Eina_Value to pass to the next #Eina_Future in the chain (if any).
276  * If you need to convert to a different type or generate a new value,
277  * use eina_value_setup() on @b another Eina_Value
278  * and return it. By returning a promise Eina_Value (eina_promise_as_value()) the
279  * whole chain will wait until the promise is resolved in
280  * order to continue its execution.
281  * Note that the value contents must survive this function scope,
282  * that is, do @b not use stack allocated blobs, arrays, structures or types that
283  * keep references to memory you give. Values will be automatically cleaned up
284  * using eina_value_flush() once they are unused (no more future or futures
285  * returned a new value).
286  * @see eina_future_cb_easy_from_desc()
287  * @see eina_future_cb_easy()
288  */
289 typedef Eina_Value (*Eina_Future_Error_Cb)(void *data, const Eina_Error error);
290 
291 /**
292  * @typedef Eina_Future_Free_Cb
293  *
294  * A callback used to inform that the future was freed and the user should also @c free the @p data.
295  * This callback may be called from an unsafe context if the future was canceled or an error
296  * occurred.
297  *
298  * @note This callback is always called, even if #Eina_Future_Error_Cb and/or #Eina_Future_Success_Cb
299  * were not provided, which can also be used to monitor when a future ends.
300  *
301  * @param[in] data The data provided by the user.
302  * @param[in] dead_future The future that was freed.
303  *
304  * @see eina_future_cb_easy_from_desc()
305  * @see eina_future_cb_easy()
306  */
307 typedef void (*Eina_Future_Free_Cb)(void *data, const Eina_Future *dead_future);
308 
309 /**
310  * @struct _Eina_Future_Cb_Easy_Desc
311  *
312  * A struct with callbacks to be used by eina_future_cb_easy_from_desc() and eina_future_cb_easy()
313  *
314  * @see eina_future_cb_easy_from_desc()
315  * @see eina_future_cb_easy()
316  */
317 struct _Eina_Future_Cb_Easy_Desc {
318    /**
319     * Called on success (value.type is not @c EINA_VALUE_TYPE_ERROR).
320     *
321     * if @c success_type is not NULL, then the value is guaranteed to be of that type,
322     * if it's not, then it will trigger @c error with @c EINVAL.
323     *
324     * After this function returns, @c free callback is called if provided.
325     */
326    Eina_Future_Success_Cb success;
327    /**
328     * Called on error (value.type is @c EINA_VALUE_TYPE_ERROR).
329     *
330     * This function can return another error, propagating or converting it. However it
331     * may also return a non-error, in this case the next future in chain will receive a regular
332     * value, which may call its @c success.
333     *
334     * If this function is not provided, then it will passthrough the error to the next error handler.
335     *
336     * It may be called with @c EINVAL if @c success_type is provided and doesn't
337     * match the received type.
338     *
339     * It may be called with @c ECANCELED if future was canceled.
340     *
341     * It may be called with @c ENOMEM if memory allocation failed during callback creation.
342     *
343     * After this function returns, @c free callback is called if provided.
344     */
345    Eina_Future_Error_Cb error;
346    /**
347     * Called on @b all situations to notify future destruction.
348     *
349     * This is called after @c success or @c error, as well as it's called if none of them are
350     * provided. Thus can be used as a "weak ref" mechanism.
351     */
352    Eina_Future_Free_Cb free;
353    /**
354     * If provided, then @c success will only be called if the value type matches the given pointer.
355     *
356     * If provided and doesn't match, then @c error will be called with @c EINVAL. If no @c error,
357     * then it will be propagated to the next future in the chain.
358     */
359    const Eina_Value_Type *success_type;
360    /**
361     * Context data given to every callback.
362     *
363     * This must be freed @b only by @c free callback as it's called from every case,
364     * otherwise it may lead to memory leaks.
365     */
366    const void *data;
367 };
368 
369 /**
370  * @struct _Eina_Future_Cb_Console_Desc
371  *
372  * A struct used to define the prefix and suffix to be printed
373  * along side the a future value. This struct is used by
374  * eina_future_cb_console_from_desc()
375  *
376  * @see eina_future_cb_console_from_desc()
377  */
378 struct _Eina_Future_Cb_Console_Desc {
379    /**
380     * The prefix to be printed. If @c NULL an empty string ("") is used.
381     */
382    const char *prefix;
383    /**
384     * The suffix the be printed. If @c NULL '\n' is used.
385     */
386    const char *suffix;
387 };
388 
389 /**
390  * @struct _Eina_Future_Cb_Log_Desc
391  *
392  * This struct is used by eina_future_cb_log_from_desc() and
393  * its contents will be routed to eina_log_print() along side
394  * the future value.
395  *
396  * @see eina_future_cb_log_from_desc()
397  */
398 struct _Eina_Future_Cb_Log_Desc {
399    /**
400     * The prefix to be printed. If @c NULL an empty string ("") is used.
401     */
402    const char *prefix;
403    /**
404     * The suffix the be printed. If @c NULL '\n' is used.
405     */
406    const char *suffix;
407    /**
408     * The file name to be passed to eina_log_print(). if @c NULL "Unknown file" is used.
409     */
410    const char *file;
411    /**
412     * The file name to be passed to eina_log_print(). if @c NULL "Unknown function" is used.
413     */
414    const char *func;
415    /**
416     * The Eina_Log_Level to use.
417     */
418    Eina_Log_Level level;
419    /**
420     * The log domain to use.
421     */
422    int domain;
423    /**
424     * The line number to be passed to eina_log_print().
425     */
426    int line;
427 };
428 
429 /**
430  * @}
431  */
432 
433 /**
434  * @struct _Eina_Future_Desc
435  * @ingroup Eina_Future
436  * A struct used to define a callback and data for a future.
437  *
438  * This struct contains a future completion callback and a data to the future
439  * completion callback which is used by eina_future_then(), eina_future_chain()
440  * and friends to inform the user about the future result. The _Eina_Future_Desc::data
441  * variable should be freed when _Eina_Future_Desc::cb is called, otherwise it will leak.
442  *
443  * @note If eina_future_then(), eina_future_chain() and friends fails to return a valid future
444  * (in other words: @c NULL is returned) the _Eina_Future_Desc::cb will be called
445  * report an error like `EINVAL` or `ENOMEM` so _Eina_Future_Desc::data can be freed.
446  *
447  * @see eina_future_then()
448  * @see eina_future_chain_array()
449  * @see eina_future_cb_convert_to()
450  * @see eina_future_cb_console_from_desc()
451  * @see eina_future_cb_ignore_error()
452  * @see eina_future_cb_easy_from_desc()
453  * @see eina_future_cb_log_from_desc()
454  */
455 struct _Eina_Future_Desc {
456    /**
457     * Called when the future is resolved or rejected.
458     *
459     * Once a future is resolved or rejected this function is called passing the future result
460     * to inform the user that the future operation has ended. Normally this
461     * function is called from a safe context (main loop or some platform defined safe context),
462     * however in case of a future cancellation (eina_future_cancel()) or if eina_future_then(),
463     * eina_future_chain() and friends fails to create a new future,
464     * this function is called from the current context.
465     *
466     * Use this function to free @p data if necessary.
467     * @see eina_future_chain()
468     * @see eina_future_then()
469     * @see eina_future_cancel()
470     */
471    Eina_Future_Cb cb;
472    /**
473     * Context data to @p cb. The @p data should be freed inside @p cb, otherwise
474     * its memory will leak!
475     */
476    const void *data;
477 
478    /**
479     * The storage will be used by Eina to store a pointer to the
480     * created future. It can be @c NULL.
481     */
482    Eina_Future **storage;
483 };
484 
485 /**
486  * @defgroup Eina_Promise Eina Promises
487  * @ingroup Eina
488  *
489  * @brief Promises are a programming paradigm that simplifies synchronization when concurrent execution is present.
490  *
491  * Since C does not natively support Promises the Eina library provides the Eina_Promise and Eina_Future objects.
492  *
493  * In procedural languages like C if you need a value which is not yet available, for instance because
494  * it takes a long time to calculate or has to be fetched from a remote server, you typically have to wait.
495  *
496  * Other languages however can return a Promise and continue execution immediately. A promise acts as a placeholder
497  * for the requested value: the value is not available yet but will be at some point in the future.
498  *
499  * With a promise in hand you can attach callbacks to be triggered when the value becomes available (i.e. when
500  * the promise is fulfilled) and then continue your calculations. You can even pass the promise to other methods
501  * which will then be executed as values become available, forming complex chains.
502  *
503  * An Eina_Promise can be considered as an object with the sole purpose of emitting the "Promise Resolved" event.
504  * Eina_Future are callbacks attached to this object to be called when the event is emitted. The promised value is
505  * passed to the callbacks whenever it's available.
506  *
507  * Here's a typical example:
508  *
509  * @code
510  * #include <Ecore.h>
511  *
512  * static void
513  * _promise_cancel(void *data, Eina_Promise *p EINA_UNUSED)
514  * {
515  *   Ctx *ctx = data;
516  *   // In case the promise is canceled we must stop the timer!
517  *   ecore_timer_del(ctx->timer);
518  *   free(ctx);
519  * }
520  *
521  * static Eina_Bool
522  * _promise_resolve(void *data)
523  * {
524  *    Ctx *ctx = data;
525  *    Eina_Value v;
526  *    eina_value_setup(&v, EINA_VALUE_TYPE_STRING);
527  *    eina_value_set(&v, "Promise resolved");
528  *    eina_promise_resolve(ctx->p, v);
529  *    free(ctx);
530  *    return EINA_FALSE;
531  * }
532  *
533  * Eina_Promise *
534  * promise_create(Eina_Future_Scheduler *scheduler)
535  * {
536  *    Ctx *ctx = malloc(sizeof(Ctx));
537  *    // A timer is scheduled in order to resolve the promise
538  *    ctx->timer = ecore_timer_add(122, _promise_resolve, ctx);
539  *    // The _promise_cancel() will be used to clean ctx if the promise is canceled.
540  *    ctx->p = eina_promise_new(scheduler, _promise_cancel, ctx);
541  *    return ctx->p;
542  * }
543  * @endcode
544  *
545  * @{
546  */
547 
548 /**
549  * Value type for #Eina_Value's containing an #Eina_Promise
550  */
551 EAPI extern const Eina_Value_Type EINA_VALUE_TYPE_PROMISE;
552 
553 /**
554  * Creates a new promise.
555  *
556  * This function creates a new promise which can be used to create a future
557  * using eina_future_new(). Every time a promise is created an #Eina_Promise_Cancel_Cb
558  * must be provided which is used to free resources that were created.
559  *
560  * A promise may be canceled directly by calling:
561  * @code
562  * eina_future_cancel(eina_future_new(eina_promise_new(...)))
563  * @endcode
564  * That is, canceling any future that is chained to receive the results.
565  *
566  * However promises can be canceled indirectly by other entities.
567  * These other entities will call eina_future_cancel() themselves,
568  * however you may not be aware of that. Some common sources
569  * of indirect cancellations:
570  *
571  * @li A subsystem was shutdown, canceling all pending futures (i.e.: ecore_shutdown())
572  *
573  * @li An EO object was linked to the promise or future, then if the object dies (last reference
574  * is gone), then the pending promises and futures will be canceled.
575  *
576  * @li Some other entity (library provider or library user) chained and canceled his future,
577  * which will result in your future being canceled.
578  *
579  * Since a promise may be canceled indirectly (by code sections that goes beyond your scope)
580  * you should always provide a cancel callback, even if you think you'll not need it.
581  *
582  * If you already have a value and want to create a future that will
583  * resolve to it directly use the eina_future_resolved(), it has the
584  * same effect as creating a promise and immediately resolving it.
585  *
586  * @param[in,out] scheduler The scheduler.
587  * @param[in] cancel_cb A callback used to inform that the promise was canceled. Use
588  * this callback to @c free @p data. @p cancel_cb must not be @c NULL !
589  * @param[in] data Data to @p cancel_cb.
590  * @return A promise or @c NULL on error.
591  * @see eina_future_cancel()
592  * @see eina_future_new()
593  * @see eina_promise_continue_new()
594  * @see eina_promise_resolve()
595  * @see eina_promise_reject()
596  * @see eina_promise_as_value()
597  * @see Eina_Future_Scheduler
598  * @see Eina_Future_Schedule_Entry
599  * @see Eina_Future_Scheduler_Cb
600  */
601 EAPI Eina_Promise *eina_promise_new(Eina_Future_Scheduler *scheduler, Eina_Promise_Cancel_Cb cancel_cb, const void *data) EINA_ARG_NONNULL(1, 2) EINA_WARN_UNUSED_RESULT;
602 
603 /**
604  * Creates a new promise from a dead_future.
605  *
606  * This function creates a new promise from a future currently being resolved which can be
607  * used to create a #Eina_Value with eina_promise_as_value(). Every time a promise is
608  * created an #Eina_Promise_Cancel_Cb must be provided which is used to free resources
609  * that were created.
610  *
611  * A promise may be canceled directly by calling:
612  * @code
613  * eina_future_cancel(eina_future_new(eina_promise_new(...)))
614  * @endcode
615  * That is, canceling any future that is chained to receive the results.
616  *
617  * However promises can be canceled indirectly by other entities.
618  * These other entities will call eina_future_cancel() themselves,
619  * however you may not be aware of that. Some common sources
620  * of indirect cancellations:
621  *
622  * @li A subsystem was shutdown, canceling all pending futures (i.e.: ecore_shutdown())
623  *
624  * @li An EO object was linked to the promise or future, then if the object dies (last reference
625  * is gone), then the pending promises and futures will be canceled.
626  *
627  * @li Some other entity (library provider or library user) chained and canceled his future,
628  * which will result in your future being canceled.
629  *
630  * Since a promise may be canceled indirectly (by code sections that goes beyond your scope)
631  * you should always provide a cancel callback, even if you think you'll not need it.
632  *
633  * Here's a typical example:
634  *
635  * @code
636  * Eina_Value
637  * _future_resolve(void *data, const Eina_Value v, const Eina_Future *dead_future)
638  * {
639  *    Eina_Promise *p;
640  *    p = eina_promise_continue_new(dead_future, _promise_cancel, NULL);
641  *    return eina_promise_as_value(p);
642  * }
643  * @endcode
644  *
645  * If you already have a value and want to create a future that will
646  * resolve to it directly use the eina_future_resolved(), it has the
647  * same effect as creating a promise and immediately resolving it.
648  *
649  * @note This function is to be used solely inside of a future resolve callback with
650  * the #Eina_Value being returned from it.
651  *
652  * @param[in] dead_future The future being resolved to get a scheduler from.
653  * @param[in] cancel_cb A callback used to inform that the promise was canceled. Use
654  * this callback to @c free @p data. @p cancel_cb must not be @c NULL.
655  * @param[in] data Data to @p cancel_cb.
656  * @return A promise or @c NULL on error.
657  * @see eina_future_cancel()
658  * @see eina_future_new()
659  * @see eina_promise_new()
660  * @see eina_promise_resolve()
661  * @see eina_promise_reject()
662  * @see eina_promise_as_value()
663  * @see Eina_Future_Scheduler
664  * @see Eina_Future_Schedule_Entry
665  * @see Eina_Future_Scheduler_Cb
666  */
667 EAPI Eina_Promise *eina_promise_continue_new(const Eina_Future *dead_future, Eina_Promise_Cancel_Cb cancel_cb, const void *data) EINA_ARG_NONNULL(1, 2) EINA_WARN_UNUSED_RESULT;
668 
669 /**
670  * Resolves a promise.
671  *
672  * This function schedules a resolve event in a
673  * safe context (main loop or some platform defined safe context),
674  * whenever possible the future callbacks will be dispatched.
675  *
676  * @param[in,out] p A promise to resolve.
677  * @param[in] value The value to be delivered.
678  *
679  * Note that the @p value contents must survive this function scope,
680  * that is, do @b not use stack allocated blobs, arrays, structures or types that
681  * keep references to memory you give. Values will be automatically cleaned up
682  * using @c eina_value_flush() once they are unused (no more future or futures
683  * returned a new value).
684  *
685  * @see eina_promise_new()
686  * @see eina_promise_reject()
687  * @see eina_promise_as_value()
688  */
689 EAPI void eina_promise_resolve(Eina_Promise *p, Eina_Value value) EINA_ARG_NONNULL(1);
690 
691 /**
692  * Rejects a promise.
693  *
694  * This function schedules a reject event in a
695  * safe context (main loop or some platform defined safe context),
696  * whenever possible the future callbacks will be dispatched.
697  *
698  * @param[in,out] p A promise to reject.
699  * @param[in] err An Eina_Error value
700  * @note Internally this function will create an #Eina_Value with type #EINA_VALUE_TYPE_ERROR.
701  *
702  * @see eina_promise_new()
703  * @see eina_promise_resolve()
704  * @see eina_promise_as_value()
705  */
706 EAPI void eina_promise_reject(Eina_Promise *p, Eina_Error err) EINA_ARG_NONNULL(1);
707 
708 
709 /**
710  * @}
711  */
712 
713 /**
714  * @defgroup Eina_Future Eina Futures
715  * @ingroup Eina_Promise
716  * @brief Methods and structures dealing with #Eina_Future
717  * @{
718  */
719 
720 /**
721  * Cancels a future.
722  *
723  * This function will cancel the whole future chain immediately (it will not be schedule to the next mainloop pass)
724  * and it will also cancel the promise linked against it. The #Eina_Future_Cb will be called
725  * with an #Eina_Value typed as #EINA_VALUE_TYPE_ERROR, with its value set to @c ECANCELED
726  * @param[in,out] f The future to cancel.
727  */
728 EAPI void eina_future_cancel(Eina_Future *f) EINA_ARG_NONNULL(1);
729 
730 /**
731  * Flushes an #Eina_Future_Desc
732  *
733  * This function is mainly used by bindings to flush an #Eina_Future_Desc.
734  * It will call the #Eina_Future_Cb with @c ENOMEM and zero the @p desc contents.
735  *
736  * @param[in,out] desc The #Eina_Future_Desc to flush, if @c NULL this is a noop.
737  */
738 EAPI void eina_future_desc_flush(Eina_Future_Desc *desc);
739 
740 /**
741  * Flushes an #Eina_Future_Cb_Easy_Desc
742  *
743  * This function is mainly used by bindings to flush an #Eina_Future_Cb_Easy_Desc.
744  * It will call the #Eina_Future_Error_Cb with @c ENOMEM, the #Eina_Future_Free_Cb and
745  * zero the @p desc contents.
746  *
747  * @param[in,out] desc The #Eina_Future_Cb_Easy_Desc to flush, if @c NULL this is a noop.
748  */
749 EAPI void eina_future_cb_easy_desc_flush(Eina_Future_Cb_Easy_Desc *desc);
750 
751 /**
752  * Creates a new #Eina_Value from a promise.
753  *
754  * This function creates a new #Eina_Value that will store an #Eina_Promise
755  * in it. This function is useful for dealing with promises inside
756  * an #Eina_Future_Cb. By returning a promise inside the #Eina_Value the
757  * whole chain will wait until the promise is resolved in
758  * order to continue its execution. Example:
759  *
760  * @code
761  * static Eina_Value
762  * _file_data_ready(const *data EINA_UNUSED, const Eina_Value v, const Eina_Future *dead EINA_UNUSED)
763  * {
764  *    const char *file_data;
765  *    Eina_Promise *p;
766  *    // It was not possible to fetch the file size.
767  *    if (v.type == EINA_VALUE_TYPE_ERROR)
768  *    {
769  *       Eina_Error err;
770  *       eina_value_get(&v, &err);
771  *       fprintf(stderr, "Could not get the file data. Reason: %s\n", eina_error_msg_get(err));
772  *       ecore_main_loop_quit();
773  *       return v;
774  *    }
775  *    else if (v.type != EINA_VALUE_TYPE_STRING)
776  *    {
777  *      fprintf(stderr, "Expecting type '%s' - received '%s'\n", EINA_VALUE_TYPE_STRING->name, v.type->name);
778  *      ecore_main_loop_quit();
779  *      return v;
780  *    }
781  *
782  *    eina_value_get(&v, &file_data);
783  *    // count_words will count the words in the background and resolve the promise once it is over...
784  *    p = count_words(file_data);
785  *    return eina_promise_as_value(p);
786  * }
787  *
788  * static Eina_Value
789  * _word_count_ready(const *data EINA_UNUSED, const Eina_Value v, const Eina_Future *dead EINA_UNUSED)
790  * {
791  *    // The _word_count_ready will only be called once count_words() resolves/rejects the promise returned by _file_data_ready()
792  *    int count;
793  *    if (v.type == EINA_VALUE_TYPE_ERROR)
794  *    {
795  *       Eina_Error err;
796  *       eina_value_get(&v, &err);
797  *       fprintf(stderr, "Could not count the file words. Reason: %s\n", eina_error_msg_get(err));
798  *       ecore_main_loop_quit();
799  *       return v;
800  *    }
801  *    else if (v.type != EINA_VALUE_TYPE_INT)
802  *    {
803  *      fprintf(stderr, "Expecting type '%s' - received '%s'\n", EINA_VALUE_TYPE_INT->name, v.type->name);
804  *      ecore_main_loop_quit();
805  *      return v;
806  *    }
807  *    eina_value_get(&v, &count);
808  *    printf("File word count %d\n", count);
809  *    return v;
810  * }
811  *
812  * void
813  * file_operation(void)
814  * {
815  *    Eina_Future *f = get_file_data("/MyFile.txt");
816  *    eina_future_chain(f, {.cb = _file_data_ready, .data = NULL},
817  *                         {.cb = _word_count_ready, .data = NULL});
818  * }
819  * @endcode
820  *
821  * @param[in,out] p The promise object to wrap in a value.
822  * @return An #Eina_Value. On error the value's type will be @c NULL.
823  *
824  * @note If an error happens the promise will be CANCELED.
825  * @see eina_future_as_value()
826  * @see eina_promise_reject()
827  * @see eina_promise_resolve()
828  */
829 EAPI Eina_Value eina_promise_as_value(Eina_Promise *p) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
830 
831 /**
832  * Creates an #Eina_Value from a future.
833  *
834  * This function is used for the same purposes as eina_promise_as_value(),
835  * but receives an #Eina_Future instead.
836  *
837  * @param[in,out] f A future to create a Eina_Value from.
838  * @return An Eina_Value. On error the value's type will be @c NULL.
839  * @note If an error happens the future @p f will be CANCELED
840  * @see eina_promise_as_value()
841  */
842 EAPI Eina_Value eina_future_as_value(Eina_Future *f)EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
843 
844 /**
845  * Creates a new future.
846  *
847  * This function creates a new future and can be used to report
848  * that an operation has succeeded or failed using
849  * eina_promise_resolve() or eina_promise_reject().
850  *
851  * Futures can also be canceled using eina_future_cancel(), which
852  * will cause the whole chain to be canceled alongside with any pending promise.
853  *
854  * @note A promise can only have one future attached to it, calling
855  * eina_future_new() on the same promise twice will
856  * result in an error (@c NULL is returned) and the future
857  * attached to the promise will be canceled!
858  *
859  * @param[in,out] p A promise used to attach a future. May not be @c NULL.
860  * @return The future or @c NULL on error.
861  * @note If an error happens this function will CANCEL the promise.
862  * @see eina_promise_new()
863  * @see eina_promise_reject()
864  * @see eina_promise_resolve()
865  * @see eina_future_cancel()
866  */
867 EAPI Eina_Future *eina_future_new(Eina_Promise *p) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
868 
869 /**
870  * Creates a new future that is already resolved to a value.
871  *
872  * This function creates a new future with an already known value,
873  * that will be resolved and dispatched by the given @p scheduler as
874  * usual.
875  *
876  * This is a helper that behaves the same as eina_promise_new()
877  * followed by eina_future_new() and then eina_promise_resolve().
878  *
879  * Futures can also be canceled using eina_future_cancel(), which will
880  * cause the whole chain to be canceled alongside with any pending
881  * promise.
882  *
883  * @param[in,out] scheduler The scheduler to use.
884  * @param[in] value The value to be delivered. Note that the value
885  * contents must survive this function scope, that is, do @b not use
886  * stack allocated blobs, arrays, structures or types that keep
887  * references to memory you give. Values will be automatically cleaned
888  * up using @c eina_value_flush() once they are unused (no more future
889  * or futures returned a new value).
890  *
891  * @return The future or @c NULL on error.
892  *
893  * @see eina_promise_new()
894  * @see eina_future_new()
895  * @see eina_promise_reject()
896  * @see eina_promise_resolve()
897  * @see eina_future_cancel()
898  */
899 EAPI Eina_Future *eina_future_resolved(Eina_Future_Scheduler *scheduler, Eina_Value value) EINA_ARG_NONNULL(1);
900 
901 /**
902  * Creates a new future that is already rejected to a specified error.
903  *
904  * This function creates a new future with an already known error,
905  * that will be resolved and dispatched by the given @p scheduler as
906  * usual.
907  *
908  * This is a helper that behaves the same as eina_promise_new()
909  * followed by eina_future_new() and then eina_promise_reject().
910  *
911  * Futures can also be canceled using eina_future_cancel(), which will
912  * cause the whole chain to be canceled alongside with any pending
913  * promise.
914  *
915  * @param[in,out] scheduler The scheduler to use.
916  * @param[in] err An #Eina_Error value
917  *
918  * @return The future or @c NULL on error.
919  *
920  * @see eina_promise_new()
921  * @see eina_future_new()
922  * @see eina_promise_reject()
923  * @see eina_promise_resolve()
924  * @see eina_future_cancel()
925  */
926 EAPI Eina_Future *eina_future_rejected(Eina_Future_Scheduler *scheduler, Eina_Error err);
927 
928 /**
929  * Register an #Eina_Future_Desc to be used when the future is resolved/rejected.
930  *
931  * With this function a callback and data is attached to the future and then
932  * once it's resolved or rejected the callback will be informed.
933  *
934  * If during the future creation an error happens this function will return @c NULL,
935  * and the Eina_Future_Desc::cb will be called reporting an error (@c EINVAL or @c ENOMEM),
936  * so the user has a chance to free Eina_Future_Desc::data.
937  *
938  * In case a future in the chain is canceled, the whole chain will be canceled immediately
939  * and the error @c ECANCELED will be reported.
940  *
941  * Here's a simple usage of this function.
942  *
943  * @code
944  * static Eina_Value
945  * _file_ready(const *data, const Eina_Value v, const Eina_Future *dead EINA_UNUSED)
946  * {
947  *    Ctx *ctx = data;
948  *    // It was not possible to fetch the file size.
949  *    if (v.type == EINA_VALUE_TYPE_ERROR)
950  *    {
951  *       Eina_Error err;
952  *       eina_value_get(&v, &err);
953  *       fprintf(stderr, "Could not read the file size. Reason: %s\n", eina_error_msg_get(err));
954  *       ecore_main_loop_quit();
955  *       return v;
956  *    }
957  *    else if (v.type != EINA_VALUE_TYPE_INT)
958  *    {
959  *      fprintf(stderr, "Expecting type '%s' - received '%s'\n", EINA_VALUE_TYPE_INT->name, v.type->name);
960  *      ecore_main_loop_quit();
961  *      return v;
962  *    }
963  *    eina_value_get(&v, &ctx->size);
964  *    printf("File size is %d bytes\n", ctx->size);
965  *    return v;
966  * }
967  *
968  * void
969  * file_operation(void)
970  * {
971  *    Eina_Future *f = get_file_size_async("/MyFile.txt");
972  *    eina_future_then_from_desc(f, (const Eina_Future_Desc){.cb = _size_ready, .data = NULL});
973  *    // There's a helper macro called eina_future_then() which simplifies the usage.
974  *    // The code below has the same effect.
975  *    // eina_future_then(f, _size_ready, NULL);
976  * }
977  * @endcode
978  *
979  * Although the code presented at `_size_ready()` is very simple, most of it
980  * is just used to check the #Eina_Value type. In order
981  * to avoid this type of code the function eina_future_cb_easy_from_desc()
982  * was created. Please, check its documentation for more information.
983  *
984  * This function can also be used to create a future chain, making
985  * it possible to execute the future result in a cascade order. When
986  * using a future chain the #Eina_Value returned by a Eina_Future_Desc::cb
987  * will be delivered to the next Eina_Future_Desc::cb in the chain.
988  *
989  * Here's an example:
990  *
991  * @code
992  * static Eina_Value
993  * _future_cb1(const *data EINA_UNUSED, const Eina_Value v)
994  * {
995  *    Eina_Value new_v;
996  *    int i;
997  *
998  *    // There's no need to check the Eina_Value type since we're using eina_future_cb_easy()
999  *    eina_value_get(&v, &i);
1000  *    printf("File size as int: %d\n", i);
1001  *    eina_value_setup(&new_v, EINA_VALUE_TYPE_STRING);
1002  *    // Convert the file size to string
1003  *    eina_value_convert(&v, &new_v);
1004  *    return new_v;
1005  * }
1006  *
1007  * static Eina_Value
1008  * _future_cb2(const *data EINA_UNUSED, const Eina_Value v)
1009  * {
1010  *    Eina_Value new_v;
1011  *    const char *file_size_str;
1012  *
1013  *    // There's no need to check the Eina_Value type since we're using eina_future_cb_easy()
1014  *    eina_value_get(&v, &file_size_str);
1015  *    printf("File size as string: %s\n", file_size_str);
1016  *    eina_value_setup(&new_v, EINA_VALUE_TYPE_DOUBLE);
1017  *    eina_value_convert(&v, &new_v);
1018  *    return new_v;
1019  * }
1020  *
1021  * static Eina_Value
1022  * _future_cb3(const *data EINA_UNUSED, const Eina_Value v)
1023  * {
1024  *    double d;
1025  *
1026  *    // There's no need to check the Eina_Value type since we're using eina_future_cb_easy()
1027  *    eina_value_get(&v, &d);
1028  *    printf("File size as double: %g\n", d);
1029  *    return v;
1030  * }
1031  *
1032  * static Eina_Value
1033  * _future_err(void *data EINA_UNUSED, Eina_Error err)
1034  * {
1035  *    // This function is called if future result type does not match or another error occurred
1036  *    Eina_Value new_v;
1037  *    eina_value_setup(&new_v, EINA_VALUE_TYPE_ERROR);
1038  *    eina_value_set(&new_v, err);
1039  *    fprintf(stderr, "Error during future process. Reason: %s\n", eina_error_msg_get(err));
1040  *    // Pass the error to the next future in the chain..
1041  *    return new_v;
1042  * }
1043  *
1044  * void chain(void)
1045  * {
1046  *   Eina_Future *f = get_file_size_async("/MyFile.txt");
1047  *   f = eina_future_then_easy(f, .success = _future_cb1, .success_type = EINA_VALUE_TYPE_INT);
1048  *   // _future_cb2 will be executed after _future_cb1()
1049  *   f = eina_future_then_easy(f, .success = _future_cb2, .success_type = EINA_VALUE_TYPE_STRING);
1050  *   // _future_cb3 will be executed after _future_cb2()
1051  *   f = eina_future_then_easy(f, .success = _future_cb3, .success_type = EINA_VALUE_TYPE_DOUBLE);
1052  *   // If an error happens _future_err will be called
1053  *   eina_future_then_easy(f, .error = _future_err);
1054  * }
1055  * @endcode
1056  *
1057  * Although it's possible to create a future chain using eina_future_then() and eina_future_then_from_desc()
1058  * there's a function that makes this task much easier, please check eina_future_chain_array() for more
1059  * information.
1060  * @note This example does manual conversion and print, however Eina offers
1061  * eina_future_cb_convert_to() and eina_future_cb_console_from_desc() to make those common case easier.
1062  *
1063  * @param[in,out] prev A future to link against
1064  * @param[in] desc A description struct containing the callback and data.
1065  * @return A new future or @c NULL on error.
1066  * @note If an error happens the whole future chain will CANCELED and
1067  * desc.cb will be called in order to free desc.data.
1068  * @see eina_future_new()
1069  * @see eina_future_then()
1070  * @see Eina_Future_Desc
1071  * @see eina_future_chain_array()
1072  * @see eina_future_chain()
1073  * @see eina_future_cb_console_from_desc()
1074  * @see eina_future_cb_easy_from_desc()
1075  * @see eina_future_cb_easy()
1076  * @see eina_future_cb_convert_to()
1077  * @see eina_future_cancel()
1078  * @see eina_future_then_easy()
1079  * @see eina_future_cb_log_from_desc()
1080  */
1081 EAPI Eina_Future *eina_future_then_from_desc(Eina_Future *prev, const Eina_Future_Desc desc) EINA_ARG_NONNULL(1);
1082 
1083 /**
1084  * Creates an Eina_Future_Desc that will log the previous future resolved value.
1085  *
1086  * This function can be used to quickly log the value that an Eina_Future_Desc::cb
1087  * is returning. The returned value will be passed to the next future in the chain without
1088  * modifications.
1089  *
1090  * There are some helper macros like eina_future_cb_log_dbg() which will automatically
1091  * fill the following fields:
1092  *
1093  * @li Eina_Future_Cb_Log_Desc::file: The `__FILE__` function will be used.
1094  * @li Eina_Future_Cb_Log_Desc::func: The `__func__` macro will be used.
1095  * @li Eina_Future_Cb_Log_Desc::level: `EINA_LOG_LEVEL_DBG` will be used.
1096  * @li Eina_Future_Cb_Log_Desc::domain: `EINA_LOG_DOMAIN_DEFAULT` will be used.
1097  * @li Eina_Future_Cb_Log_Desc::line: The `__LINE__` macro will be used.
1098  *
1099  * @param[in] desc The description data to be used.
1100  * @return An #Eina_Future_Desc
1101  *
1102  * @see Eina_Future_Cb_Log_Desc
1103  * @see eina_future_then()
1104  * @see eina_future_chain()
1105  * @see eina_future_cb_log_dbg()
1106  * @see eina_future_cb_log_crit()
1107  * @see eina_future_cb_log_err()
1108  * @see eina_future_cb_log_info()
1109  * @see eina_future_cb_log_warn()
1110  * @see eina_future_cb_console_from_desc()
1111  */
1112 EAPI Eina_Future_Desc eina_future_cb_log_from_desc(const Eina_Future_Cb_Log_Desc desc) EINA_WARN_UNUSED_RESULT;
1113 
1114 /**
1115  * Creates a future chain.
1116  *
1117  * This behaves exactly like eina_future_then_from_desc(), but makes it easier to create future chains.
1118  *
1119  * If during the future chain creation an error happens this function will return @c NULL,
1120  * and the Eina_Future_Desc::cb from the @p descs array will be called reporting an error (`EINVAL` or `ENOMEM`),
1121  * so the user has a chance to free Eina_Future_Desc::data.
1122  *
1123  * Just like eina_future_then_from_desc(), the value returned by a Eina_Future_Desc::cb callback will
1124  * be delivered to the next Eina_Future_Desc::cb in the chain.
1125  *
1126  * In case a future in the chain is canceled, the whole chain will be canceled immediately
1127  * and the error `ECANCELED` will be reported.
1128  *
1129  * Here's an example:
1130  *
1131  * @code
1132  * // callbacks code....
1133  *
1134  * Eina_Future* chain(void)
1135  * {
1136  *   Eina_Future *f = get_file_size_async("/MyFile.txt");
1137  *   return eina_future_chain(f, eina_future_cb_easy(_future_cb1, _future_err, NULL, EINA_VALUE_TYPE_INT, NULL),
1138  *                               eina_future_cb_easy(_future_cb2, _future_err, NULL, EINA_VALUE_TYPE_STRING, NULL),
1139  *                               eina_future_cb_easy(_future_cb3, _future_err, NULL, EINA_VALUE_TYPE_DOUBLE, NULL),
1140  *                               {.cb = _future_cb4, .data = NULL });
1141  * }
1142  * @endcode
1143  *
1144  * @param[in,out] prev The previous future
1145  * @param[in] descs An array of descriptions.
1146                     The last element of the array must have the Eina_Future_Desc::cb set to @c NULL
1147  * @return A future or @c NULL on error.
1148  * @note If an error happens the whole future chain will CANCELED and
1149  * desc.cb will be called in order to free desc.data.
1150  * @see eina_future_new()
1151  * @see eina_future_then()
1152  * @see Eina_Future_Desc
1153  * @see eina_future_chain()
1154  * @see eina_future_cb_ignore_error()
1155  * @see eina_future_cb_console_from_desc()
1156  * @see eina_future_cb_log_from_desc()
1157  * @see eina_future_cb_easy_from_desc()
1158  * @see eina_future_cb_easy()
1159  * @see eina_future_then_from_desc()
1160  * @see eina_future_then_easy()
1161  * @see eina_future_cb_convert_to()
1162  */
1163 EAPI Eina_Future *eina_future_chain_array(Eina_Future *prev, const Eina_Future_Desc descs[]) EINA_ARG_NONNULL(1, 2);
1164 
1165 
1166 /**
1167  * Wrapper around eina_future_chain_array() and eina_future_cb_easy_from_desc()
1168  *
1169  * This functions makes it easier to use  eina_future_chain_array() with eina_future_cb_easy_from_desc(),
1170  * check the macro eina_future_chain_easy() for a syntax sugar.
1171  *
1172  *
1173  * @param[in,out] prev The previous future
1174  * @param[in] descs An array of descriptions. The last element of the array must have the Eina_Future_Desc::cb set to @c NULL
1175  * @return A future or @c NULL on error.
1176  * @note If an error happens the whole future chain will CANCELED and
1177  * desc.cb will be called in order to free desc.data.
1178  * @see eina_future_chain_easy()
1179  * @see eina_future_chain()
1180  * @see eina_future_cb_easy()
1181  * @see eina_future_chain_array()
1182  * @see eina_future_cb_easy_from_desc()
1183  */
1184 EAPI Eina_Future *eina_future_chain_easy_array(Eina_Future *prev, const Eina_Future_Cb_Easy_Desc descs[]) EINA_ARG_NONNULL(1, 2);
1185 
1186 /**
1187  * Creates an #Eina_Future_Desc that will print the previous future's resolved value.
1188  *
1189  * This function can be used to quickly inspect the value that an Eina_Future_Desc::cb
1190  * is returning. The returned value will be passed to the next future in the chain without
1191  * modifications.
1192  *
1193  * There's also a helper macro called eina_future_cb_console() which makes this
1194  * function easier to use.
1195  *
1196  * Example:
1197  *
1198  * @code
1199  *
1200  * eina_future_chain(a_future, {.cb = cb1, .data = NULL},
1201  *                             //Inspect the cb1 value and pass to cb2 without modifications
1202  *                             eina_future_cb_console("cb1 value:"),
1203  *                             {.cb = cb2, .data = NULL},
1204  *                             //Inspect the cb2 value
1205  *                             eina_future_cb_console("cb2 value:", " cb2 value suffix\n"))
1206  * @endcode
1207  *
1208  * @param[in] desc Description object with contextual information.
1209  * @return An #Eina_Future_Desc
1210  *
1211  * The description object, @p desc, can (optionally) include a prefix, suffix,
1212  * filename and function name. If these are @c NULL, empty strings ("") are used
1213  * instead.  If @p desc->suffix is provided, the '\\n' should be provided to ensure
1214  * the printed string ends with a line feed.
1215  *
1216  * @see eina_future_then()
1217  * @see Eina_Future_Desc
1218  * @see eina_future_chain()
1219  * @see eina_future_cb_easy_from_desc()
1220  * @see eina_future_cb_easy()
1221  * @see eina_future_then_from_desc()
1222  * @see eina_future_then_easy()
1223  * @see eina_future_cb_console()
1224  * @see eina_future_cb_ignore_error()
1225  * @see eina_future_cb_log_from_desc()
1226  */
1227 EAPI Eina_Future_Desc eina_future_cb_console_from_desc(const Eina_Future_Cb_Console_Desc desc) EINA_WARN_UNUSED_RESULT;
1228 
1229 /**
1230  * Returns an #Eina_Future_Desc that ignores an error.
1231  *
1232  * This function may be used if one wants to ignore an error. If the
1233  * specified error happens an #EINA_VALUE_EMPTY will be delivered to the
1234  * next future in the chain.
1235  *
1236  * @param[in] err The error to be ignored.
1237  * @return A future descriptor to be used with eina_future_then() or eina_future_chain()
1238  */
1239 EAPI Eina_Future_Desc eina_future_cb_ignore_error(Eina_Error err);
1240 
1241 /**
1242  * Creates an #Eina_Future_Desc which will convert the received eina value to a given type.
1243  *
1244  * @param[in] type The #Eina_Value_Type to convert to.
1245  * @return An #Eina_Future_Desc
1246  * @see eina_future_then()
1247  * @see Eina_Future_Desc
1248  * @see eina_future_chain()
1249  * @see eina_future_cb_easy_from_desc()
1250  * @see eina_future_cb_easy()
1251  * @see eina_future_then_from_desc()
1252  * @see eina_future_then_easy()
1253  */
1254 EAPI Eina_Future_Desc eina_future_cb_convert_to(const Eina_Value_Type *type);
1255 
1256 /**
1257  * Creates an #Eina_Future_Desc based on an #Eina_Future_Cb_Easy_Desc.
1258  *
1259  * This function aims to be used in conjunction with eina_future_chain(),
1260  * eina_future_then_from_desc() and friends and its main objective is to simplify
1261  * error handling and #Eina_Value type checks.
1262  * It uses three callbacks to inform the user about the future's
1263  * result and life cycle. They are:
1264  *
1265  * @li Eina_Future_Cb_Easy_Desc::success: This callback is called when
1266  * the future execution was successful, that is, no errors occurred and
1267  * the result type matches Eina_Future_Cb_Easy_Desc::success_type. In case
1268  * Eina_Future_Cb_Easy_Desc::success_type is @c NULL, this function will
1269  * only be called if the future did not report an error. The value returned
1270  * by this function will be propagated to the next future in the chain (if any).
1271  *
1272  * @li Eina_Future_Cb_Easy_Desc::error: This callback is called when
1273  * the future result is an error or Eina_Future_Cb_Easy_Desc::success_type
1274  * does not match the future result type. The value returned
1275  * by this function will be propagated to the next future in the chain (if any).
1276  *
1277  * @li Eina_Future_Cb_Easy_Desc::free: Called after the future was freed and any resources
1278  * allocated must be freed at this point. This callback is always called.
1279  *
1280  * Check the example below for a sample usage:
1281  *
1282  * @code
1283  * static Eina_Value
1284  * _file_size_ok(void *data, Eina_Value v)
1285  * {
1286  *   Ctx *ctx = data;
1287  *   // Since an Eina_Future_Cb_Easy_Desc::success_type was provided, there's no need to check the value type
1288  *   int s;
1289  *   eina_value_get(&v, &s);
1290  *   printf("File size is %d bytes\n", s);
1291  *   ctx->file_size = s;
1292  *   return v;
1293  * }
1294  *
1295  * static Eina_Value
1296  * _file_size_err(void *data, Eina_Error err)
1297  * {
1298  *   fprintf(stderr, "Could not read the file size. Reason: %s\n", eina_error_msg_get(err));
1299  *   // Stop propagating the error.
1300  *   return EINA_VALUE_EMPTY;
1301  * }
1302  *
1303  * static void
1304  * _future_freed(void *data, const Eina_Future dead)
1305  * {
1306  *   Ctx *ctx = data;
1307  *   printf("Future %p deleted\n", dead);
1308  *   ctx->file_size_handler_cb(ctx->file_size);
1309  *   free(ctx);
1310  * }
1311  *
1312  * void do_work(File_Size_Handler_Cb cb)
1313  * {
1314  *   Ctx *ctx = malloc(sizeof(Ctx));
1315  *   ctx->f = get_file_size("/tmp/todo.txt");
1316  *   ctx->file_size = -1;
1317  *   ctx->file_size_handler_cb = cb;
1318  *   eina_future_then_easy(f, _file_size_ok, _file_size_err, _future_freed, EINA_VALUE_TYPE_INT, ctx);
1319  * }
1320  * @endcode
1321  *
1322  * @param[in] desc The easy callback's description.
1323  * @return An #Eina_Future_Desc
1324  *
1325  * @see eina_future_chain()
1326  * @see eina_future_then()
1327  * @see eina_future_cb_easy()
1328  */
1329 EAPI Eina_Future_Desc eina_future_cb_easy_from_desc(const Eina_Future_Cb_Easy_Desc desc) EINA_WARN_UNUSED_RESULT;
1330 
1331 /**
1332  * Creates an all promise.
1333  *
1334  * Creates a promise that is resolved once all the futures
1335  * from the @p array are resolved.
1336  * The promise is resolved with an #Eina_Value type array which
1337  * contains #EINA_VALUE_TYPE_VALUE elements. The result array is
1338  * ordered according to the @p array argument. Example:
1339  *
1340  * @code
1341  * static const char *
1342  * _get_operation_name_by_index(int idx)
1343  * {
1344  *   switch (idx)
1345  *   {
1346  *      case 0: return "Get file data";
1347  *      case 1: return "Get file size";
1348  *      default: return "sum";
1349  *   }
1350  * }
1351  *
1352  * static Eina_Value
1353  * _all_cb(const void *data EINA_UNUSED, const Eina_Value array, const Eina_Future *dead EINA_UNUSED)
1354  * {
1355  *    Eina_Error err;
1356  *    unsigned int i, len;
1357  *
1358  *    if (array.type == EINA_VALUE_TYPE_ERROR)
1359  *     {
1360  *       eina_value_get(&array, &err);
1361  *       fprintf(stderr, "Could not complete all operations. Reason: %s\n", eina_error_msg_get(err));
1362  *       return array;
1363  *     }
1364  *    len = eina_value_array_count(&array);
1365  *    for (i = 0; i < len; i++)
1366  *     {
1367  *       Eina_Value v;
1368  *       eina_value_array_get(&array, i, &v);
1369  *       if (v.type == EINA_VALUE_TYPE_ERROR)
1370  *        {
1371  *          eina_value_get(&v, &err);
1372  *          fprintf(stderr, "Could not complete operation '%s'. Reason: %s\n", _get_operation_name_by_index(i), eina_error_msg_get(err));
1373  *          continue;
1374  *        }
1375  *       if (!i)
1376  *        {
1377  *           const char *msg;
1378  *           if (v.type != EINA_VALUE_TYPE_STRING)
1379  *           {
1380  *             fprintf(stderr, "Operation %s expects '%s' - received '%s'\n", _get_operation_name_by_index(i), EINA_VALUE_TYPE_STRING->name, v.type->name);
1381  *             continue;
1382  *           }
1383  *           eina_value_get(&v, &msg);
1384  *           printf("File content:%s\n", msg);
1385  *        }
1386  *       else if (i == 1)
1387  *        {
1388  *           int i;
1389  *           if (v.type != EINA_VALUE_TYPE_INT)
1390  *           {
1391  *             fprintf(stderr, "Operation %s expects '%s' - received '%s'\n", _get_operation_name_by_index(i), EINA_VALUE_TYPE_INT->name, v.type->name);
1392  *             continue;
1393  *           }
1394  *           eina_value_get(&v, &i);
1395  *           printf("File size: %d\n", i);
1396  *        }
1397  *        else
1398  *        {
1399  *           double p;
1400  *           if (v.type != EINA_VALUE_TYPE_DOUBLE)
1401  *           {
1402  *             fprintf(stderr, "Operation %s expects '%s' - received '%s'\n", _get_operation_name_by_index(i), EINA_VALUE_TYPE_DOUBLE->name, v.type->name);
1403  *             continue;
1404  *           }
1405  *           eina_value_get(&v, &p);
1406  *           printf("50 places of PI: %f\n", p);
1407  *        }
1408  *     }
1409  *    return array;
1410  * }
1411  *
1412  * void func(void)
1413  * {
1414  *   Eina_Future *f1, *f2, *f3, f_all;
1415  *
1416  *   f1 = read_file("/tmp/todo.txt");
1417  *   f2 = get_file_size("/tmp/file.txt");
1418  *   // calculates 50 places of PI
1419  *   f3 = calc_pi(50);
1420  *   f_all = eina_future_all(f1, f2, f3);
1421  *   eina_future_then(f_all, _all_cb, NULL);
1422  * }
1423  * @endcode
1424  *
1425  * @param[in,out] array An array of futures, terminated with #EINA_FUTURE_SENTINEL.
1426  * @return A promise or @c NULL on error.
1427  * @note On error all the futures will be CANCELED.
1428  * @see eina_future_all_array()
1429  */
1430 EAPI Eina_Promise *eina_promise_all_array(Eina_Future *array[]) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
1431 
1432 /**
1433  * Creates an all promise from an iterator.
1434  *
1435  * Creates a promise that is resolved once all the futures
1436  * from the @p iterator are resolved.
1437  * The promise is resolved with an #Eina_Value type array which
1438  * contains #EINA_VALUE_TYPE_VALUE elements. The result array is
1439  * ordered according to the @p iterator order.
1440  *
1441  * @param[in] iterator An iterator of futures. Will be destroyed after the call.
1442  * @return A promise or @c NULL on error.
1443  * @note On error all the futures will be CANCELED.
1444  * @see eina_future_all_iterator()
1445  */
1446 EAPI Eina_Promise *eina_promise_all_iterator(Eina_Iterator *iterator) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
1447 
1448 /**
1449  * Creates a race promise.
1450  *
1451  * Creates a promise that resolves when a future from the @p array
1452  * is completed. The remaining futures will be canceled with the
1453  * error code `ECANCELED`
1454  *
1455  * The resulting value is an #EINA_VALUE_TYPE_STRUCT with two fields:
1456  *
1457  * @li A field named @c value containing an #Eina_Value with the result itself.
1458  * @li A field named @c index containing the index of the completed
1459  * function relative to the @p array.
1460  *
1461  * Example:
1462  *
1463  * @code
1464  * static const char *
1465  * _get_operation_name_by_index(int idx)
1466  * {
1467  *   switch (idx)
1468  *   {
1469  *      case 0: return "Get file data";
1470  *      case 1: return "Get file size";
1471  *      default: return "sum";
1472  *   }
1473  * }
1474  *
1475  * static Eina_Value
1476  * _race_cb(const void *data EINA_UNUSED, const Eina_Value v)
1477  * {
1478  *    unsigned int i;
1479  *    Eina_Value result;
1480  *    Eina_Error err;
1481  *    Eina_Value_Struct *st;
1482  *
1483  *     // No need to check for the 'v' type. eina_future_cb_easy() did that for us.
1484  *     // However we should check if the struct has the correct description
1485  *     st = eina_value_memory_get(&v);
1486  *     if (st->desc != EINA_PROMISE_RACE_STRUCT_DESC)
1487  *      {
1488  *         fprintf(stderr, "Eina_Value is not a race struct\n");
1489  *         return v;
1490  *      }
1491  *     eina_value_struct_get(&v, "index", &i);
1492  *     // Get the operation result
1493  *     eina_value_struct_get(&v, "value", &result);
1494  *     if (!i)
1495  *      {
1496  *        const char *msg;
1497  *        if (result.type != EINA_VALUE_TYPE_STRING)
1498  *          fprintf(stderr, "Operation %s expects '%s' - received '%s'\n", _get_operation_name_by_index(i), EINA_VALUE_TYPE_STRING->name, result.type->name);
1499  *        else
1500  *        {
1501  *          eina_value_get(&result, &msg);
1502  *          printf("File content:%s\n", msg);
1503  *        }
1504  *      }
1505  *     else if (i == 1)
1506  *       {
1507  *         int i;
1508  *         if (result.type != EINA_VALUE_TYPE_INT)
1509  *           fprintf(stderr, "Operation %s expects '%s' - received '%s'\n", _get_operation_name_by_index(i), EINA_VALUE_TYPE_INT->name, v.type->name);
1510  *         else
1511  *         {
1512  *           eina_value_get(&result, &i);
1513  *           printf("File size: %d\n", i);
1514  *         }
1515  *       }
1516  *      else
1517  *       {
1518  *         double p;
1519  *         if (result.type != EINA_VALUE_TYPE_DOUBLE)
1520  *            fprintf(stderr, "Operation %s expects '%s' - received '%s'\n", _get_operation_name_by_index(i), EINA_VALUE_TYPE_DOUBLE->name, result.type->name);
1521  *         else
1522  *          {
1523  *            eina_value_get(&result, &p);
1524  *            printf("50 places of PI: %f\n", p);
1525  *          }
1526  *       }
1527  *     eina_value_flush(&result);
1528  *     return v;
1529  * }
1530  *
1531  * static Eina_Value
1532  * _race_err(void *data, Eina_Error err)
1533  * {
1534  *    fprintf(stderr, "Could not complete the race future. Reason: %s\n", eina_error_msg_get(err));
1535  *    return EINA_VALUE_EMPTY;
1536  * }
1537  *
1538  * void func(void)
1539  * {
1540  *   static const *Eina_Future[] = {NULL, NULL, NULL, NULL};
1541  *   Eina_List *l = NULL;
1542  *
1543  *   futures[0] = read_file("/tmp/todo.txt");
1544  *   futures[1] = get_file_size("/tmp/file.txt");
1545  *   // calculates 50 places of PI
1546  *   futures[2] = calc_pi(50);
1547  *   f_race = eina_future_race_array(futures);
1548  *   eina_future_then_easy(f_race, _race_cb, _race_err, NULL, EINA_VALUE_TYPE_STRUCT, NULL);
1549  * }
1550  * @endcode
1551  *
1552  * @param[in,out] array An array of futures, terminated by #EINA_FUTURE_SENTINEL.
1553  * @return A promise or @c NULL on error.
1554  * @note On error all the futures will be CANCELED.
1555  * @see eina_future_race_array()
1556  * @see _Eina_Future_Race_Result
1557  */
1558 EAPI Eina_Promise *eina_promise_race_array(Eina_Future *array[]) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
1559 
1560 /**
1561  * @struct _Eina_Future_Race_Result
1562  * The struct that is used to store the race result.
1563  *
1564  * When using eina_promise_race_array() and friends, the future result
1565  * will be reported as a struct. The values can be obtained using
1566  * eina_value_struct_get() or one could access the struct directly
1567  * such as this example:
1568  *
1569  * @code
1570  * static Eina_Value
1571  * _race_cb(const void *data EINA_UNUSED, const Eina_Value v)
1572  * {
1573  *    //code...
1574  *    Eina_Value_Struct st;
1575  *    Eina_Future_Race_Result *rr;
1576  *    eina_value_get(v, &st);
1577  *    rr = st.memory;
1578  *    printf("Winning future index: %u\n", rr->index);
1579  *    //more code..
1580  *    return v;
1581  * }
1582  * @endcode
1583  *
1584  * @see eina_promise_race_array()
1585  * @see eina_future_race_array()
1586  * @see eina_promise_race()
1587  * @see eina_future_race()
1588  * @see EINA_PROMISE_RACE_STRUCT_DESC
1589  */
1590 struct _Eina_Future_Race_Result {
1591    /**
1592     * The race result.
1593     */
1594    Eina_Value value;
1595    /**
1596     * The future index that won the race.
1597     */
1598    unsigned int index;
1599 };
1600 
1601 /**
1602  * @var const Eina_Value_Struct_Desc *EINA_PROMISE_RACE_STRUCT_DESC
1603  *
1604  * A pointer to the race struct description, which
1605  * is used by eina_promise_race_array().
1606  *
1607  * This struct contains two members:
1608  * @li @c value: An #EINA_VALUE_TYPE_VALUE that contains the future result that won the race.
1609  * @li @c index: An #EINA_VALUE_TYPE_UINT that contains the future index that won the race.
1610  *
1611  * @see eina_promise_race_array()
1612  * @see _Eina_Future_Race_Result
1613  */
1614 EAPI extern const Eina_Value_Struct_Desc *EINA_PROMISE_RACE_STRUCT_DESC;
1615 
1616 /**
1617  * Creates a future that will be resolved once all futures from @p array is resolved.
1618  * This is a helper over eina_promise_all_array()
1619  *
1620  * @param[in,out] array A future array, must be terminated with #EINA_FUTURE_SENTINEL.
1621  * @return A future.
1622  * @see eina_promise_all_array()
1623  * @see EINA_FUTURE_SENTINEL
1624  */
1625 static inline Eina_Future *
eina_future_all_array(Eina_Future * array[])1626 eina_future_all_array(Eina_Future *array[])
1627 {
1628    Eina_Promise *p = eina_promise_all_array(array);
1629    if (!p) return NULL;
1630    return eina_future_new(p);
1631 }
1632 
1633 /**
1634  * Creates a future that will be resolved once all futures from @p iterator are resolved.
1635  * This is a helper over eina_promise_all_iterator()
1636  *
1637  * @param[in] iterator An iterator containing futures. Will be destroyed on exit of the function.
1638  * @return A future.
1639  * @see eina_promise_all_iterator()
1640  */
1641 static inline Eina_Future *
eina_future_all_iterator(Eina_Iterator * iterator)1642 eina_future_all_iterator(Eina_Iterator *iterator)
1643 {
1644    Eina_Promise *p = eina_promise_all_iterator(iterator);
1645    if (!p) return NULL;
1646    return eina_future_new(p);
1647 }
1648 
1649 /**
1650  * Creates a future that will be resolved once a future @p array is resolved.
1651  * This is a helper over eina_promise_race_array()
1652  *
1653  * @param[in,out] array A future array, must be terminated with #EINA_FUTURE_SENTINEL
1654  * @return A future.
1655  * @see eina_promise_race_array()
1656  * @see EINA_FUTURE_SENTINEL
1657  */
1658 static inline Eina_Future *
eina_future_race_array(Eina_Future * array[])1659 eina_future_race_array(Eina_Future *array[])
1660 {
1661    Eina_Promise *p = eina_promise_race_array(array);
1662    if (!p) return NULL;
1663    return eina_future_new(p);
1664 }
1665 
1666 /**
1667  * Used by eina_promise_race_array() and eina_promise_all_array() and
1668  * friends to flag the end of the array.
1669  *
1670  * @see eina_promise_race_array()
1671  * @see eina_promise_all_array()
1672  */
1673 #define EINA_FUTURE_SENTINEL ((void *)(unsigned long)-1)
1674 
1675 /**
1676  * A syntactic sugar over eina_promise_race_array().
1677  * Usage:
1678  * @code
1679  * promise = eina_promise_race(future1, future2, future3, future4);
1680  * @endcode
1681  * @see eina_promise_race_array()
1682  */
1683 #define eina_promise_race(...) eina_promise_race_array((Eina_Future *[]){__VA_ARGS__, EINA_FUTURE_SENTINEL})
1684 /**
1685  * A syntactic sugar over eina_future_race_array().
1686  * Usage:
1687  * @code
1688  * future = eina_future_race(future1, future2, future3, future4);
1689  * @endcode
1690  * @see eina_future_race_array()
1691  */
1692 #define eina_future_race(...) eina_future_race_array((Eina_Future *[]){__VA_ARGS__, EINA_FUTURE_SENTINEL})
1693 /**
1694  * A syntactic sugar over eina_future_all_array().
1695  * Usage:
1696  * @code
1697  * future = eina_future_all(future1, future2, future3, future4);
1698  * @endcode
1699  * @see eina_future_all_array()
1700  */
1701 #define eina_future_all(...) eina_future_all_array((Eina_Future *[]){__VA_ARGS__, EINA_FUTURE_SENTINEL})
1702 /**
1703  * A syntactic sugar over eina_promise_all_array().
1704  * Usage:
1705  * @code
1706  * promise = eina_promise_all(future1, future2, future3, future4);
1707  * @endcode
1708  * @see eina_promise_all_array()
1709  */
1710 #define eina_promise_all(...) eina_promise_all_array((Eina_Future *[]){__VA_ARGS__, EINA_FUTURE_SENTINEL})
1711 /**
1712  * A syntactic sugar over eina_future_cb_easy_from_desc().
1713  * Usage:
1714  * @code
1715  * future_desc = eina_future_cb_easy(_success_cb, _error_cb, _free_cb, EINA_VALUE_TYPE_INT, my_data);
1716  * @endcode
1717  * @see eina_future_cb_easy_from_desc()
1718  */
1719 #define eina_future_cb_easy(...) eina_future_cb_easy_from_desc((Eina_Future_Cb_Easy_Desc){__VA_ARGS__})
1720 /**
1721  * A syntactic sugar over eina_future_chain_array().
1722  * Usage:
1723  * @code
1724  * future = eina_future_chain(future, {.cb = _my_cb, .data = my_data}, {.cb = _my_another_cb, .data = NULL});
1725  * @endcode
1726  * @see eina_future_chain_array()
1727  */
1728 #define eina_future_chain(_prev, ...) eina_future_chain_array(_prev, (Eina_Future_Desc[]){__VA_ARGS__, {.cb = NULL, .data = NULL}})
1729 /**
1730  * A syntactic sugar over eina_future_then_from_desc().
1731  * Usage:
1732  * @code
1733  * future = eina_future_then(future, _my_cb, my_data);
1734  * @endcode
1735  * @see eina_future_then_from_desc()
1736  * @see eina_future_then_easy()
1737  */
1738 #define eina_future_then(_prev, ...) eina_future_then_from_desc(_prev, (Eina_Future_Desc){__VA_ARGS__})
1739 /**
1740  * A syntactic sugar over eina_future_cb_console_from_desc().
1741  * Usage:
1742  * @code
1743  * desc = eina_future_cb_console(.prefix = "prefix", .suffix = "suffix");
1744  * @endcode
1745  * @see eina_future_cb_console_from_desc()
1746  */
1747 #define eina_future_cb_console(...) eina_future_cb_console_from_desc((Eina_Future_Cb_Console_Desc){__VA_ARGS__})
1748 
1749 /**
1750  * A syntactic sugar over eina_future_cb_log_from_desc().
1751  *
1752  * This macro will set the following fields of the #Eina_Future_Cb_Log_Desc:
1753  *
1754  * @li Eina_Future_Cb_Log_Desc::file: The `__FILE__` function will be used.
1755  * @li Eina_Future_Cb_Log_Desc::func: The `__func__` macro will be used.
1756  * @li Eina_Future_Cb_Log_Desc::level: `EINA_LOG_LEVEL_DBG` will be used.
1757  * @li Eina_Future_Cb_Log_Desc::domain: `EINA_LOG_DOMAIN_DEFAULT` will be used.
1758  * @li Eina_Future_Cb_Log_Desc::line: The `__LINE__` macro will be used.
1759  *
1760  * Usage:
1761  * @code
1762  * desc = eina_future_cb_log_dbg(.prefix = "prefix", .suffix = "suffix");
1763  * @endcode
1764  * @see eina_future_cb_log_from_desc()
1765  */
1766 #define eina_future_cb_log_dbg(_prefix, _suffix)                        \
1767   eina_future_cb_log_from_desc((Eina_Future_Cb_Log_Desc){_prefix, _suffix, __FILE__, \
1768          __func__, EINA_LOG_LEVEL_DBG, EINA_LOG_DOMAIN_DEFAULT, __LINE__})
1769 
1770 /**
1771  * A syntactic sugar over eina_future_cb_log_from_desc().
1772  *
1773  * This macro will set the following fields of the Eina_Future_Cb_Log_Desc:
1774  *
1775  * @li Eina_Future_Cb_Log_Desc::file: The __FILE__ function will be used.
1776  * @li Eina_Future_Cb_Log_Desc::func: The __func__ macro will be used.
1777  * @li Eina_Future_Cb_Log_Desc::level: EINA_LOG_LEVEL_CRITICAL will be used.
1778  * @li Eina_Future_Cb_Log_Desc::domain: EINA_LOG_DOMAIN_DEFAULT will be used.
1779  * @li Eina_Future_Cb_Log_Desc::line: The __LINE__ macro will be used.
1780  *
1781  * Usage:
1782  * @code
1783  * desc = eina_future_cb_log_crit(.prefix = "prefix", .suffix = "suffix");
1784  * @endcode
1785  * @see eina_future_cb_log_from_desc()
1786  */
1787 #define eina_future_cb_log_crit(_prefix, _suffix)                       \
1788   eina_future_cb_log_from_desc((Eina_Future_Cb_Log_Desc){_prefix, _suffix, __FILE__, \
1789          __func__, EINA_LOG_LEVEL_CRITICAL, EINA_LOG_DOMAIN_DEFAULT, __LINE__})
1790 
1791 /**
1792  * A syntactic sugar over eina_future_cb_log_from_desc().
1793  *
1794  * This macro will set the following fields of the Eina_Future_Cb_Log_Desc:
1795  *
1796  * @li Eina_Future_Cb_Log_Desc::file: The __FILE__ function will be used.
1797  * @li Eina_Future_Cb_Log_Desc::func: The __func__ macro will be used.
1798  * @li Eina_Future_Cb_Log_Desc::level: EINA_LOG_LEVEL_ERR will be used.
1799  * @li Eina_Future_Cb_Log_Desc::domain: EINA_LOG_DOMAIN_DEFAULT will be used.
1800  * @li Eina_Future_Cb_Log_Desc::line: The __LINE__ macro will be used.
1801  *
1802  * Usage:
1803  * @code
1804  * desc = eina_future_cb_log_err(.prefix = "prefix", .suffix = "suffix");
1805  * @endcode
1806  * @see eina_future_cb_log_from_desc()
1807  */
1808 #define eina_future_cb_log_err(_prefix, _suffix)                        \
1809   eina_future_cb_log_from_desc((Eina_Future_Cb_Log_Desc){_prefix, _suffix, __FILE__, \
1810          __func__, EINA_LOG_LEVEL_ERR, EINA_LOG_DOMAIN_DEFAULT, __LINE__})
1811 
1812 /**
1813  * A syntactic sugar over eina_future_cb_log_from_desc().
1814  *
1815  * This macro will set the following fields of the Eina_Future_Cb_Log_Desc:
1816  *
1817  * @li Eina_Future_Cb_Log_Desc::file: The __FILE__ function will be used.
1818  * @li Eina_Future_Cb_Log_Desc::func: The __func__ macro will be used.
1819  * @li Eina_Future_Cb_Log_Desc::level: EINA_LOG_LEVEL_INFO will be used.
1820  * @li Eina_Future_Cb_Log_Desc::domain: EINA_LOG_DOMAIN_DEFAULT will be used.
1821  * @li Eina_Future_Cb_Log_Desc::line: The __LINE__ macro will be used.
1822  *
1823  * Usage:
1824  * @code
1825  * desc = eina_future_cb_log_info(.prefix = "prefix", .suffix = "suffix");
1826  * @endcode
1827  * @see eina_future_cb_log_from_desc()
1828  */
1829 #define eina_future_cb_log_info(_prefix, _suffix)                       \
1830   eina_future_cb_log_from_desc((Eina_Future_Cb_Log_Desc){_prefix, _suffix, __FILE__, \
1831          __func__, EINA_LOG_LEVEL_INFO, EINA_LOG_DOMAIN_DEFAULT, __LINE__})
1832 
1833 /**
1834  * A syntactic sugar over eina_future_cb_log_from_desc().
1835  *
1836  * This macro will set the following fields of the Eina_Future_Cb_Log_Desc:
1837  *
1838  * @li Eina_Future_Cb_Log_Desc::file: The __FILE__ function will be used.
1839  * @li Eina_Future_Cb_Log_Desc::func: The __func__ macro will be used.
1840  * @li Eina_Future_Cb_Log_Desc::level: EINA_LOG_LEVEL_WARN will be used.
1841  * @li Eina_Future_Cb_Log_Desc::domain: EINA_LOG_DOMAIN_DEFAULT will be used.
1842  * @li Eina_Future_Cb_Log_Desc::line: The __LINE__ macro will be used.
1843  *
1844  * Usage:
1845  * @code
1846  * desc = eina_future_cb_log_warn(.prefix = "prefix", .suffix = "suffix");
1847  * @endcode
1848  * @param _prefix PREFIX
1849  * @param _suffix SUFFIX
1850  * @returns SOMETHING
1851  * @see eina_future_cb_log_from_desc()
1852  */
1853 #define eina_future_cb_log_warn(_prefix, _suffix)                       \
1854   eina_future_cb_log_from_desc((Eina_Future_Cb_Log_Desc){_prefix, _suffix, __FILE__, \
1855          __func__, EINA_LOG_LEVEL_WARN, EINA_LOG_DOMAIN_DEFAULT, __LINE__})
1856 
1857 /**
1858  * A syntactic sugar over eina_future_then() and eina_future_cb_easy().
1859  *
1860  * Usage:
1861  * @code
1862  * f = eina_future_then_easy(f, .success = _success_cb, .success_type = EINA_VALUE_TYPE_DOUBLE, .data = NULL, );
1863  * @endcode
1864  * @see eina_future_then_from_desc()
1865  * @see eina_future_easy()
1866  * @see eina_future_then()
1867  * @see eina_future_cb_easy_from_desc()
1868  */
1869 #define eina_future_then_easy(_prev, ...) eina_future_then_from_desc(_prev, eina_future_cb_easy(__VA_ARGS__))
1870 
1871 /**
1872  * A syntactic sugar over eina_future_chain() and eina_future_cb_easy().
1873  *
1874  * Usage:
1875  * @code
1876  * f = eina_future_chain_easy(f, {.success = _success_cb, .success_type = EINA_VALUE_TYPE_DOUBLE, .data = NULL},
1877  *                               { .success = _success2_cb }, {.error = error_cb});
1878  * @endcode
1879  * @see eina_future_chain_array()
1880  * @see eina_future_easy()
1881  * @see eina_future_chain_easy_array()
1882  * @see eina_future_cb_easy_from_desc()
1883  */
1884 #define eina_future_chain_easy(_prev, ...) eina_future_chain_easy_array(_prev, (Eina_Future_Cb_Easy_Desc[]) {__VA_ARGS__, {NULL, NULL, NULL, NULL, NULL}})
1885 
1886 /**
1887  * @}
1888  */
1889 
1890 #ifdef __cplusplus
1891 }
1892 #endif
1893 #endif
1894