Xonotic
promise.qc File Reference
#include "promise.qh"
+ Include dependency graph for promise.qc:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Enumerations

enum  { PROMISE_PENDING, PROMISE_RESOLVED, PROMISE_REJECTED }
 

Functions

void _Promise_done (Promise this, Promise(Promise ret, entity result, entity userdata) onResolve, Promise(Promise ret, entity err, entity userdata) onReject, Promise ret, entity userdata)
 
void _Promise_finalize (Promise this)
 
void _Promise_handle (Promise this, PromiseHandler h)
 
Promise _Promise_onReject_default (Promise ret, entity err, entity userdata)
 
Promise _Promise_onResolve_default (Promise ret, entity result, entity userdata)
 
Promise _Promise_then (Promise this, Promise ret, Promise(Promise ret, entity result, entity userdata) onResolve, Promise(Promise ret, entity result, entity userdata) onReject, entity userdata)
 
 classfield (Promise) .int _promise_state
 
 classfield (PromiseHandler) .Promise _promise_handler_ret
 
 entityclass (PromiseHandler)
 
Promise Promise_catch_ (Promise this, Promise ret, Promise(Promise ret, entity result, entity userdata) onReject, entity userdata)
 
Promise Promise_new_ (Promise this)
 
void Promise_reject (Promise this)
 notify all Promise_catch subscribers that this promise has rejected More...
 
void Promise_resolve (Promise this)
 notify all Promise_then subscribers that this promise has resolved More...
 
Promise Promise_sleep (float n)
 
Promise Promise_then_ (Promise this, Promise ret, Promise(Promise ret, entity result, entity userdata) onResolve, entity userdata)
 
entity REF (entity this)
 
void ref_init (entity this, int init, void(entity this) finalize)
 
entity unref (Promise this)
 
 void (entity this) _ref_finalize
 

Variables

entity entity userdata _promise_handler_reject
 
entity entity userdata _promise_handler_resolve
 
int _ref_count
 
entity err
 
entity result
 

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
PROMISE_PENDING 
PROMISE_RESOLVED 
PROMISE_REJECTED 

Definition at line 30 of file promise.qc.

Function Documentation

◆ _Promise_done()

void _Promise_done ( Promise  this,
Promise(Promise ret, entity result, entity userdata)  onResolve,
Promise(Promise ret, entity err, entity userdata)  onReject,
Promise  ret,
entity  userdata 
)

Definition at line 132 of file promise.qc.

References _Promise_handle(), and new_pure.

Referenced by _Promise_then().

139 {
140  PromiseHandler h = new_pure(PromiseHandler);
141  h._promise_handler_ret = ret;
142  h._promise_handler_data = userdata;
143  h._promise_handler_resolve = onResolve;
144  h._promise_handler_reject = onReject;
145  _Promise_handle(this, h);
146 }
void _Promise_handle(Promise this, PromiseHandler h)
Definition: promise.qc:106
#define new_pure(class)
purely logical entities (.origin doesn't work)
Definition: oo.qh:62
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _Promise_finalize()

void _Promise_finalize ( Promise  this)

Definition at line 46 of file promise.qc.

Referenced by Promise_new_().

47 {
48  delete(this);
49 }
+ Here is the caller graph for this function:

◆ _Promise_handle()

void _Promise_handle ( Promise  this,
PromiseHandler  h 
)

Definition at line 106 of file promise.qc.

References _Promise_then(), func_null(), IL_NEW, IL_PUSH(), NULL, PROMISE_PENDING, PROMISE_REJECTED, and PROMISE_RESOLVED.

Referenced by _Promise_done(), Promise_new_(), Promise_reject(), and Promise_resolve().

107 {
108  switch (this._promise_state) {
109  case PROMISE_PENDING:
110  if (!this._promise_handlers) {
111  this._promise_handlers = IL_NEW();
112  }
113  IL_PUSH(this._promise_handlers, h);
114  break;
115  case PROMISE_RESOLVED: {
116  Promise ret = h._promise_handler_ret;
117  Promise p = h._promise_handler_resolve(ret, this._promise_result, h._promise_handler_data);
118  if (p != ret) _Promise_then(p, ret, func_null, func_null, NULL); // bind p -> ret
119  delete(h);
120  break;
121  }
122  case PROMISE_REJECTED: {
123  Promise ret = h._promise_handler_ret;
124  Promise p = h._promise_handler_reject(ret, this._promise_result, h._promise_handler_data);
125  if (p != ret) _Promise_then(p, ret, func_null, func_null, NULL); // bind p -> ret
126  delete(h);
127  break;
128  }
129  }
130 }
Promise _Promise_then(Promise this, Promise ret, Promise(Promise ret, entity result, entity userdata) onResolve, Promise(Promise ret, entity result, entity userdata) onReject, entity userdata)
Definition: promise.qc:162
#define IL_NEW()
ERASEABLE entity IL_PUSH(IntrusiveList this, entity it)
Push to tail.
#define NULL
Definition: post.qh:17
var void func_null()
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _Promise_onReject_default()

Promise _Promise_onReject_default ( Promise  ret,
entity  err,
entity  userdata 
)

Definition at line 155 of file promise.qc.

References err, and Promise_reject().

Referenced by _Promise_then().

156 {
157  ret._promise_result = err;
158  Promise_reject(ret);
159  return ret;
160 }
void Promise_reject(Promise this)
notify all Promise_catch subscribers that this promise has rejected
Definition: promise.qc:79
entity err
Definition: promise.qc:44
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _Promise_onResolve_default()

Promise _Promise_onResolve_default ( Promise  ret,
entity  result,
entity  userdata 
)

Definition at line 148 of file promise.qc.

References Promise_resolve(), and result.

Referenced by _Promise_then().

149 {
150  ret._promise_result = result;
151  Promise_resolve(ret);
152  return ret;
153 }
entity result
Definition: promise.qc:43
void Promise_resolve(Promise this)
notify all Promise_then subscribers that this promise has resolved
Definition: promise.qc:60
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _Promise_then()

Promise _Promise_then ( Promise  this,
Promise  ret,
Promise(Promise ret, entity result, entity userdata)  onResolve,
Promise(Promise ret, entity result, entity userdata)  onReject,
entity  userdata 
)

Definition at line 162 of file promise.qc.

References _Promise_done(), _Promise_onReject_default(), and _Promise_onResolve_default().

Referenced by _Promise_handle(), Promise_catch_(), Promise_reject(), and Promise_then_().

169 {
171  this,
172  (onResolve ? onResolve : _Promise_onResolve_default),
173  (onReject ? onReject : _Promise_onReject_default),
174  ret,
175  userdata
176  );
177  return ret;
178 }
Promise _Promise_onResolve_default(Promise ret, entity result, entity userdata)
Definition: promise.qc:148
Promise _Promise_onReject_default(Promise ret, entity err, entity userdata)
Definition: promise.qc:155
void _Promise_done(Promise this, Promise(Promise ret, entity result, entity userdata) onResolve, Promise(Promise ret, entity err, entity userdata) onReject, Promise ret, entity userdata)
Definition: promise.qc:132
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ classfield() [1/2]

classfield ( Promise  )

◆ classfield() [2/2]

classfield ( PromiseHandler  )

◆ entityclass()

entityclass ( PromiseHandler  )

◆ Promise_catch_()

Promise Promise_catch_ ( Promise  this,
Promise  ret,
Promise(Promise ret, entity result, entity userdata)  onReject,
entity  userdata 
)

Definition at line 191 of file promise.qc.

References _Promise_then(), func_null(), and unref().

197 {
198  unref(ret); // ret is a temporary
199  return _Promise_then(this, ret, func_null, onReject, userdata);
200 }
entity unref(Promise this)
Definition: promise.qc:19
Promise _Promise_then(Promise this, Promise ret, Promise(Promise ret, entity result, entity userdata) onResolve, Promise(Promise ret, entity result, entity userdata) onReject, entity userdata)
Definition: promise.qc:162
var void func_null()
+ Here is the call graph for this function:

◆ Promise_new_()

Promise Promise_new_ ( Promise  this)

Definition at line 51 of file promise.qc.

References _Promise_finalize(), _Promise_handle(), and ref_init().

52 {
53  ref_init(this, 2, _Promise_finalize);
54  this._promise_result = this; // promises default to being their own result to save on entities
55  return this;
56 }
void ref_init(entity this, int init, void(entity this) finalize)
Definition: promise.qc:6
void _Promise_finalize(Promise this)
Definition: promise.qc:46
+ Here is the call graph for this function:

◆ Promise_reject()

void Promise_reject ( Promise  this)

notify all Promise_catch subscribers that this promise has rejected

Definition at line 79 of file promise.qc.

References _Promise_handle(), _Promise_then(), entity(), IL_DELETE, IL_EACH, LOG_SEVERE, LOG_SEVEREF, PROMISE_PENDING, PROMISE_REJECTED, and unref().

Referenced by _Promise_onReject_default().

80 {
81  if (!this) {
82  LOG_SEVERE("Attempted to reject a null promise");
83  return;
84  }
85  if (this._promise_state != PROMISE_PENDING) {
86  LOG_SEVEREF("Rejected non-pending promise %i", this);
87  return;
88  }
89  this._promise_state = PROMISE_REJECTED;
90  if (this._promise_handlers) {
91  IL_EACH(this._promise_handlers, true, _Promise_handle(this, it));
92  IL_DELETE(this._promise_handlers);
93  }
94  unref(this);
95  return;
96 }
#define IL_EACH(this, cond, body)
entity unref(Promise this)
Definition: promise.qc:19
void _Promise_handle(Promise this, PromiseHandler h)
Definition: promise.qc:106
#define LOG_SEVEREF(...)
Definition: log.qh:63
#define IL_DELETE(this)
Delete the list.
#define LOG_SEVERE(...)
Definition: log.qh:62
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Promise_resolve()

void Promise_resolve ( Promise  this)

notify all Promise_then subscribers that this promise has resolved

Definition at line 60 of file promise.qc.

References _Promise_handle(), IL_DELETE, IL_EACH, LOG_SEVERE, LOG_SEVEREF, PROMISE_PENDING, PROMISE_RESOLVED, and unref().

Referenced by _Promise_onResolve_default(), and Promise_sleep().

61 {
62  if (!this) {
63  LOG_SEVERE("Attempted to resolve a null promise");
64  return;
65  }
66  if (this._promise_state != PROMISE_PENDING) {
67  LOG_SEVEREF("Resolved non-pending promise %i", this);
68  return;
69  }
70  this._promise_state = PROMISE_RESOLVED;
71  if (this._promise_handlers) {
72  IL_EACH(this._promise_handlers, true, _Promise_handle(this, it));
73  IL_DELETE(this._promise_handlers);
74  }
75  unref(this);
76  return;
77 }
#define IL_EACH(this, cond, body)
entity unref(Promise this)
Definition: promise.qc:19
void _Promise_handle(Promise this, PromiseHandler h)
Definition: promise.qc:106
#define LOG_SEVEREF(...)
Definition: log.qh:63
#define IL_DELETE(this)
Delete the list.
#define LOG_SEVERE(...)
Definition: log.qh:62
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Promise_sleep()

Promise Promise_sleep ( float  n)

Definition at line 206 of file promise.qc.

References Promise_new, Promise_resolve(), setthink, time, and unref().

207 {
208  Promise p = unref(Promise_new());
210  p.nextthink = time + n;
211  return p;
212 }
entity unref(Promise this)
Definition: promise.qc:19
#define Promise_new()
Definition: promise.qh:5
#define setthink(e, f)
float time
Definition: csprogsdefs.qc:16
void Promise_resolve(Promise this)
notify all Promise_then subscribers that this promise has resolved
Definition: promise.qc:60
+ Here is the call graph for this function:

◆ Promise_then_()

Promise Promise_then_ ( Promise  this,
Promise  ret,
Promise(Promise ret, entity result, entity userdata)  onResolve,
entity  userdata 
)

Definition at line 180 of file promise.qc.

References _Promise_then(), func_null(), and unref().

186 {
187  unref(ret); // ret is a temporary
188  return _Promise_then(this, ret, onResolve, func_null, userdata);
189 }
entity unref(Promise this)
Definition: promise.qc:19
Promise _Promise_then(Promise this, Promise ret, Promise(Promise ret, entity result, entity userdata) onResolve, Promise(Promise ret, entity result, entity userdata) onReject, entity userdata)
Definition: promise.qc:162
var void func_null()
+ Here is the call graph for this function:

◆ REF()

entity REF ( entity  this)

Definition at line 13 of file promise.qc.

References _ref_count.

14 {
15  this._ref_count += 1;
16  return this;
17 }
int _ref_count
Definition: promise.qc:3

◆ ref_init()

void ref_init ( entity  this,
int  init,
void(entity this finalize 
)

Definition at line 6 of file promise.qc.

References _ref_count.

Referenced by Promise_new_().

7 {
8  this._ref_count = init;
9  this._ref_finalize = finalize;
10 }
int _ref_count
Definition: promise.qc:3
+ Here is the caller graph for this function:

◆ unref()

entity unref ( Promise  this)

Definition at line 19 of file promise.qc.

References _ref_count, LOG_DEBUGF, NULL, and sourceLoc.

Referenced by Promise_catch_(), Promise_reject(), Promise_resolve(), Promise_sleep(), and Promise_then_().

20 {
21  this._ref_count -= 1;
22  if (!this._ref_count) {
23  LOG_DEBUGF("Finalize entity %i (from %s)", this, this.sourceLoc);
24  this._ref_finalize(this);
25  return NULL;
26  }
27  return this;
28 }
string sourceLoc
Location entity was spawned from in source.
Definition: oo.qh:21
int _ref_count
Definition: promise.qc:3
#define NULL
Definition: post.qh:17
#define LOG_DEBUGF(...)
Definition: log.qh:86
+ Here is the caller graph for this function:

◆ void()

void ( entity  this)

Variable Documentation

◆ _promise_handler_reject

entity entity userdata _promise_handler_reject

Definition at line 44 of file promise.qc.

◆ _promise_handler_resolve

entity entity userdata _promise_handler_resolve

Definition at line 43 of file promise.qc.

◆ _ref_count

int _ref_count

Definition at line 3 of file promise.qc.

Referenced by REF(), ref_init(), and unref().

◆ err

entity err

Definition at line 44 of file promise.qc.

Referenced by _Promise_onReject_default(), and waypoint_start_hardwiredlink().

◆ result