-
Notifications
You must be signed in to change notification settings - Fork 0
/
future.hh
110 lines (81 loc) · 2.94 KB
/
future.hh
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
#pragma once
#include <algorithm>
#include <atomic>
#include <future>
#include <chrono>
#include <condition_variable>
#include <exception>
#include <functional>
#include <list>
#include <memory>
#include <mutex>
#include <type_traits>
#include "future_traits.hh"
#include "shared_state.hh"
namespace experimental
{
using future_status = std::future_status;
template<typename T>
class promise
{
public:
promise();
promise(promise<T> && other) noexcept = default;
promise<T>& operator=(promise<T> && other) noexcept = default;
promise<T>& operator=(promise<T> const &other) = delete;
void set_value(T &&t);
void set_exception(std::exception_ptr p);
future<T> get_future();
void swap(promise<T> &other) noexcept;
private:
std::shared_ptr<impl::shared_state<T>> _state;
};
template<>
class promise<void>
{
public:
promise();
promise(promise<void> && other) noexcept = default;
promise<void>& operator=(promise<void> && other) noexcept = default;
promise<void>& operator=(promise<void> const &other) = delete;
void set_value();
void set_exception(std::exception_ptr p);
future<void> get_future();
void swap(promise<void> &other) noexcept;
private:
std::shared_ptr<impl::shared_state<void>> _state;
};
template<typename T>
class future
{
public:
future() noexcept;
future(future<T> &&f) noexcept;
future(future<T> const &f) = delete;
future<T>& operator=(future<T> && other) noexcept = default;
future<T>& operator=(future<T> const &other) = delete;
T get();
void wait() const;
template< class Rep, class Period >
future_status wait_for( const std::chrono::duration<Rep,Period>& timeout_duration) const;
template< class Clock, class Duration >
future_status wait_until( const std::chrono::time_point<Clock,Duration>& timeout_time ) const;
bool valid() const noexcept;
bool is_ready() const;
/*
* Relies on SFINAE to handle 'T == void' special case
*/
template<typename F>
auto then(F && func) -> future<typename impl::utils::unwrap_future<decltype(func())>::value_type>;
template<typename F>
auto then(F && func) -> future<typename impl::utils::unwrap_future<decltype(func(std::declval<T>()))>::value_type>;
private:
future(std::shared_ptr<impl::shared_state<T>> state);
private:
std::shared_ptr<impl::shared_state<T>> _state;
friend struct experimental::promise<T>;
template<typename>
friend class experimental::future;
};
}//!experimental
#include "future.hxx"