-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q.h
80 lines (67 loc) · 1.7 KB
/
Q.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// Q.h
//
// Created by Jami Couch on 8/15/15.
// Copyright (c) 2015 Jami Couch. All rights reserved.
//
#ifndef Q_h
#define Q_h
#include <functional>
#include <iostream>
#include <vector>
class PromiseVal {
private:
void *m_data;
bool m_promise;
public:
PromiseVal *withPromise(void *promise);
PromiseVal *withValue(void *value);
bool isPromise();
bool isNull();
template<typename T> T getData() {
return (T)m_data;
};
static PromiseVal *value(void *value);
static PromiseVal *promise(void *promise);
static PromiseVal *null();
};
typedef std::function<PromiseVal*(PromiseVal*)> Success;
typedef std::function<void(void*)> Failure;
typedef std::function<void()> Finally;
class Promise{
private:
std::vector<Success> m_successes;
Failure m_failure;
Finally m_finally;
std::vector<Promise*> m_promises;
bool resolved = false;
bool rejected = false;
PromiseVal *resolved_value;
void *rejected_value;
void _resolve(PromiseVal *value);
void _resolve_promise(PromiseVal *value);
void _resolve_value(PromiseVal *value);
void _reject(void *value);
public:
Promise *then(Success &&success, Failure &&failure = NULL, Finally &&finally = NULL);
void resolve(PromiseVal *value);
void resolve(Promise *value);
void reject(void *value);
};
class Deferred {
private:
Promise *m_promise;
public:
Deferred();
Promise *promise();
void resolve(PromiseVal *value);
void reject(void *value);
};
class Q {
public:
static Deferred *defer();
static Promise *when(PromiseVal *value);
static Promise *resolve(PromiseVal *value);
static Promise *reject(void *value);
};
#endif