-
Notifications
You must be signed in to change notification settings - Fork 0
/
PromiseImplementation.hpp
117 lines (106 loc) · 3.54 KB
/
PromiseImplementation.hpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//
// Created by danonofficial on 30.09.17.
//
#ifndef FUTURE_PROMISEIMPLEMENTATION_HPP
#define FUTURE_PROMISEIMPLEMENTATION_HPP
template<typename T>
void Promise<T>::Set(const T &dataToSet) {
if (data == nullptr) {
throw error::NoData("There is no shared data at adress");
}
std::lock_guard<std::mutex> lock(data->mute);
if (data->isReady) {
throw error::AbandonFuture("You will broke future after this, sorry, but i cann't allow you to do it");
}
data->value = dataToSet;
data->isReady = true;
data->condition.notify_all();
}
template<typename T>
void Promise<T>::Set(T &&dataToSet) {
if (data == nullptr) {
throw error::NoData("There is no shared data at adress");
}
std::lock_guard<std::mutex> lock(data->mute);
if (data->isReady) {
throw error::AbandonFuture("You will broke future after this, sorry, but i cann't allow you to do it");
}
data->value = dataToSet;
data->isReady = true;
data->condition.notify_all();
}
template<typename T>
void Promise<T>::SetException(const std::exception_ptr &exc) {
if (data == nullptr) {
throw error::NoData("There is no shared data at adress");
}
std::lock_guard<std::mutex> lock(data->mute);
if (data->isReady) {
throw error::AbandonFuture("You will broke future after this, sorry, So i cann't allow you to do it");
}
data->exception_ptr = std::move(exc);
data->isReady = true;
data->condition.notify_all();
}
template<typename T>
Future<T> Promise<T>::GetFuture() {
if (data == nullptr) {
throw error::NoData("There is no shared data at adress");
}
if (futureAlreadyGetted == true) {
throw error::AbandonFuture("You are trying to get already getted value");
}
futureAlreadyGetted = true;
return Future<T>(data);
}
template<typename T>
void Promise<T &>::Set(const T &dataToSet) {
if (data == nullptr) {
throw error::NoData("There is no shared data at adress");
}
std::lock_guard<std::mutex> lock(data->mute);
if (data->isReady) {
throw error::AbandonFuture("You will broke future after this, sorry, but i cann't allow you to do it");
}
*data->value = dataToSet;
data->isReady = true;
data->condition.notify_all();
}
template<typename T>
void Promise<T &>::Set(T &&dataToSet) {
if (data == nullptr) {
throw error::NoData("There is no shared data at adress");
}
std::lock_guard<std::mutex> lock(data->mute);
if (data->isReady) {
throw error::AbandonFuture("You will broke future after this, sorry, but i cann't allow you to do it");
}
data->value = dataToSet;
data->isReady = true;
data->condition.notify_all();
}
template<typename T>
void Promise<T &>::SetException(const std::exception_ptr &exc) {
if (data == nullptr) {
throw error::NoData("There is no shared data at adress");
}
std::lock_guard<std::mutex> lock(data->mute);
if (data->isReady) {
throw error::AbandonFuture("You will broke future after this, sorry, So i cann't allow you to do it");
}
data->exception_ptr = std::move(exc);
data->isReady = true;
data->condition.notify_all();
}
template<typename T>
Future<T &> Promise<T &>::GetFuture() {
if (data == nullptr) {
throw error::NoData("There is no shared data at adress");
}
if (futureAlreadyGetted == true) {
throw error::AbandonFuture("You are trying to get already getted value");
}
futureAlreadyGetted = true;
return Future<T &>(data);
}
#endif //FUTURE_PROMISEIMPLEMENTATION_HPP