-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise.hpp
72 lines (56 loc) · 1.56 KB
/
promise.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
#pragma once
#include "future.hpp"
#include "shared_state.hpp"
namespace cubbit
{
template <typename T>
class promise
{
std::shared_ptr<shared_state<T>> _state = std::make_shared<shared_state<T>>();
bool _obtained{false};
public:
~promise() = default;
future<T> get_future()
{
if(this->_obtained)
throw std::future_error(std::future_errc::future_already_retrieved);
this->_obtained = true;
return future<T>{_state};
}
void set_value(const T& value)
{
this->_state->set_value(value);
}
void set_value(T&& value)
{
this->_state->set_value(std::move(value));
}
void set_exception(std::exception_ptr exception)
{
this->_state->set_exception(exception);
}
};
template <>
class promise<void>
{
std::shared_ptr<shared_state<void>> _state = std::make_shared<shared_state<void>>();
bool _obtained{false};
public:
~promise() = default;
future<void> get_future()
{
if(this->_obtained)
throw std::future_error(std::future_errc::future_already_retrieved);
this->_obtained = true;
return future<void>{_state};
}
void set_value()
{
this->_state->set_value();
}
void set_exception(std::exception_ptr exception)
{
this->_state->set_exception(exception);
}
};
} // namespace cubbit