-
Notifications
You must be signed in to change notification settings - Fork 1
/
promise.h
76 lines (67 loc) · 2.54 KB
/
promise.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
#pragma once
#include <functional>
#include <exception>
#include <cstddef>
#include <mutex>
#include <memory>
#include <type_traits>
#include <vector>
#include <tuple>
#include "self_managing.h"
#if defined(DEBUG)
#define SAFE_PROMISES
#endif
#include "promise/fwd.h"
#include "promise/traits.h"
#include "promise/factories.h"
#include "promise/combiners.h"
#include "promise/callback_pack.h"
#include "promise/monad.h"
#include "promise/state_base.h"
#include "promise/state.h"
namespace kaiu {
/***
* Promise class
*
* The default (no-parameter) constructor for Result type and for NextResult
* type must not throw - promise chains guarantees will break.
*/
template <typename Result>
class Promise : public PromiseLike {
public:
using DResult = typename remove_cvr<Result>::type;
using result_type = DResult;
/* Promise */
Promise();
/* Copy/move/cast constructors */
Promise(const Promise<DResult>&);
Promise(const Promise<DResult&>&);
Promise(const Promise<DResult&&>&);
Promise(Promise<Result>&&) = default;
/* Assignment */
Promise<Result>& operator =(Promise<Result>&&) = default;
Promise<Result>& operator =(const Promise<Result>&) = default;
/* Access promise state (then/except/finally/resolve/reject) */
PromiseState<DResult> *operator ->() const;
private:
friend class Promise<DResult>;
friend class Promise<DResult&>;
friend class Promise<DResult&&>;
static_assert(!std::is_void<DResult>::value, "Void promises are no longer supported");
static_assert(!std::is_same<DResult, std::exception_ptr>::value, "Promise result type cannot be std::exception_ptr");
static_assert(!is_promise<DResult>::value, "Promise<Promise<T>> is invalid, use Promise<T>/forward_to instead");
Promise(std::shared_ptr<PromiseState<DResult>> const promise);
std::shared_ptr<PromiseState<DResult>> promise;
};
static_assert(is_promise<Promise<int>>::value, "Promise traits test #1 failed");
static_assert(!is_promise<int>::value, "Promise traits test #2 failed");
static_assert(result_of_promise_is<Promise<int>, int>::value, "Promise traits test #3 failed");
static_assert(!result_of_promise_is<Promise<int>, long>::value, "Promise traits test #4 failed");
static_assert(!result_of_promise_is<int, int>::value, "Promise traits test #5 failed");
static_assert(result_of_not_promise_is<int, int>::value, "Promise traits test #6 failed");
static_assert(!result_of_not_promise_is<int, long>::value, "Promise traits test #7 failed");
static_assert(!result_of_not_promise_is<Promise<int>, int>::value, "Promise traits test #8 failed");
}
#ifndef promise_tcc
#include "promise.tcc"
#endif