µEvLoop
A fast and lightweight event loop aimed at embedded platforms in C99.
promise.h
Go to the documentation of this file.
1 
10 #ifndef UEL_PROMISE_H
11 #define UEL_PROMISE_H
12 
13 #include "uevloop/config.h"
14 #include "uevloop/utils/closure.h"
16 
23 };
26 
40 typedef struct uel_promise_segment uel_promise_segment_t;
43  uel_closure_t resolve;
45  uel_closure_t reject;
47  uel_promise_segment_t *next;
48 };
49 
51 struct uel_promise_store;
52 
72 typedef struct uel_promise uel_promise_t;
73 struct uel_promise {
79  void *value;
81  uel_promise_segment_t *first_segment;
83  uel_promise_segment_t *last_segment;
84 };
85 
89 typedef struct uel_promise_store uel_promise_store_t;
92  uel_objpool_t *promise_pool;
94  uel_objpool_t *segment_pool;
95 };
96 
103 uel_promise_store_t uel_promise_store_create(
104  uel_objpool_t *promise_pool,
105  uel_objpool_t *segment_pool
106 );
107 
116 uel_promise_t *uel_promise_create(uel_promise_store_t *store, uel_closure_t closure);
117 
123 void uel_promise_destroy(uel_promise_t *promise);
124 
131 void uel_promise_then(uel_promise_t *promise, uel_closure_t resolve);
132 
139 void uel_promise_catch(uel_promise_t *promise, uel_closure_t reject);
140 
147 void uel_promise_always(uel_promise_t *promise, uel_closure_t always);
148 
156 void uel_promise_after(
157  uel_promise_t *promise,
158  uel_closure_t resolve,
159  uel_closure_t reject
160 );
161 
171 void uel_promise_resolve(uel_promise_t *promise, void *value);
172 
182 void uel_promise_reject(uel_promise_t *promise, void *value);
183 
197  uel_promise_t *promise,
198  uel_promise_state_t state,
199  void *value
200 );
201 
208 uel_closure_t uel_promise_resolver(uel_promise_t *promise);
209 
216 uel_closure_t uel_promise_rejecter(uel_promise_t *promise);
217 
224 uel_closure_t uel_promise_destroyer(uel_promise_t *promise);
225 
226 #ifdef UEL_PROMISE_SHORTCUTS
227 
228 #define THEN uel_promise_then
229 #define CATCH uel_promise_catch
230 #define AFTER uel_promise_after
231 #define ALWAYS uel_promise_always
232 
233 
234 #endif /* UEL_PROMISE_SHORTCUTS */
235 
236 #endif /* end of include guard: UEL_PROMISE_H */
uel_promise_store
An issuer of promises. Contains references to pools for promises and segments.
Definition: promise.h:90
uel_promise_resettle
void uel_promise_resettle(uel_promise_t *promise, uel_promise_state_t state, void *value)
Resettles a promise as the supplied state. Unlike uel_promise_resolve() and uel_promise_reject(),...
uel_promise_store::promise_pool
uel_objpool_t * promise_pool
A reference to the promise pool.
Definition: promise.h:92
uel_promise_create
uel_promise_t * uel_promise_create(uel_promise_store_t *store, uel_closure_t closure)
Acquires a new promise from the supplied store and binds it to the asynchronous operation started by ...
UEL_PROMISE_RESOLVED
@ UEL_PROMISE_RESOLVED
A promise that has been resolved with some value.
Definition: promise.h:21
uel_promise::value
void * value
The value this promise holds. Only meaningful if already settled.
Definition: promise.h:79
uel_promise_store::segment_pool
uel_objpool_t * segment_pool
A reference to the segment pool.
Definition: promise.h:94
closure.h
Defines closures, objects that bind functions to creating and calling contexts.
uel_promise::last_segment
uel_promise_segment_t * last_segment
The last segment to be processed when this promise settles.
Definition: promise.h:83
uel_promise_after
void uel_promise_after(uel_promise_t *promise, uel_closure_t resolve, uel_closure_t reject)
Adds a new synchronous segment to the promise. Either of its closures will be invoked,...
uel_promise_destroyer
uel_closure_t uel_promise_destroyer(uel_promise_t *promise)
Creates a closure bound to a promise. When the closure is invoked, the promise is destroyed....
uel_promise::state
uel_promise_state_t state
The current state of this promise.
Definition: promise.h:77
uel_promise_segment::next
uel_promise_segment_t * next
The next synchronous segment to be processed.
Definition: promise.h:47
uel_promise_then
void uel_promise_then(uel_promise_t *promise, uel_closure_t resolve)
Adds a new synchronous segment to the promise. It will be invoked upon promise resolution....
UEL_PROMISE_REJECTED
@ UEL_PROMISE_REJECTED
A promise that has been rejected with some error.
Definition: promise.h:22
object-pool.h
Defines object pools, arrays of pre-allocated objects for dynamic use.
uel_promise::first_segment
uel_promise_segment_t * first_segment
The first segment to be processed when this promise settles.
Definition: promise.h:81
uel_promise_always
void uel_promise_always(uel_promise_t *promise, uel_closure_t always)
Adds a new synchronous segment to the promise. The same closure will be invoked on promise settling r...
uel_promise::source
struct uel_promise_store * source
The promise store from where this promise was issued.
Definition: promise.h:75
UEL_PROMISE_PENDING
@ UEL_PROMISE_PENDING
A promise that has not been resolved nor rejected.
Definition: promise.h:20
uel_promise_segment::resolve
uel_closure_t resolve
The closure to be invoked when the promise is resolved.
Definition: promise.h:43
uel_promise_store_create
uel_promise_store_t uel_promise_store_create(uel_objpool_t *promise_pool, uel_objpool_t *segment_pool)
Creates a new promise store from the promise and segment pools.
uel_promise_state_t
enum uel_promise_state uel_promise_state_t
Alias to the uel_promise_state enum.
Definition: promise.h:25
uel_promise_reject
void uel_promise_reject(uel_promise_t *promise, void *value)
Settles a promise as rejected and, synchronously, invokes the reject closures of each segment in the ...
uel_promise_state
uel_promise_state
Defines the possible states for a prommise.
Definition: promise.h:19
uel_promise_rejecter
uel_closure_t uel_promise_rejecter(uel_promise_t *promise)
Creates a closure bound to a promise. When the closure is invoked with some parameter,...
uel_promise
A promise is association of an asynchronous operation to the possible execution paths that follow its...
Definition: promise.h:73
uel_promise_segment::reject
uel_closure_t reject
The closure to be invoked when the promise is rejected.
Definition: promise.h:45
uel_promise_resolver
uel_closure_t uel_promise_resolver(uel_promise_t *promise)
Creates a closure bound to a promise. When the closure is invoked with some parameter,...
uel_promise_segment
Defines a single synchronous operation to be invoked when the promise is either resolved or rejected.
Definition: promise.h:41
uel_promise_destroy
void uel_promise_destroy(uel_promise_t *promise)
Destroys a promise and all of its segments. Settling this promise afterwards yields undefined behavio...
uel_promise_catch
void uel_promise_catch(uel_promise_t *promise, uel_closure_t reject)
Adds a new synchronous segment to the promise. It will be invoked upon promise rejection....
config.h
Central repository of system configuration. This is meant to be edited by the programmer as needed.
uel_promise_resolve
void uel_promise_resolve(uel_promise_t *promise, void *value)
Settles a promise as resolved and, synchronously, invokes the resolve closures of each segment in the...