forked from tonttu/pledge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Future.hpp
83 lines (62 loc) · 2.15 KB
/
Future.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
#pragma once
#include <memory>
#include "Executor.hpp"
#include "details/FutureData.hpp"
namespace Pledge {
template <typename T>
class Future
{
public:
using ValueType = T;
Future(std::shared_ptr<FutureDataType<T>> data);
Future() = delete;
Future(const Future&) = delete;
Future& operator=(const Future&) = delete;
Future(Future&&) = default;
Future& operator=(Future&&) = default;
// Constructs a ready future.
template <typename Y>
Future(Y&& t);
// Changes the future executor and returns *this for chaining.
Future<T>&& via(Executor* executor) &&;
// Block the current thread until the future is ready, and either moves out
// the value or throws the future error. You can move the value out just
// once, and the future must be rvalue when doing so. You can either add
// continuations or call this function.
T get() &&;
// Returns true if calling get() would return the value immediately.
bool hasValue() const;
// Returns true if calling get() would immediately throw the error.
bool hasError() const;
// Atomic way to call hasValue() || hasError()
bool isReady() const;
// Add a continuation which is called from the current executor once
// the future has an error.
template <typename F>
auto error(F&& f) && -> Future<T>;
// Add a continuation which is called from the current executor once
// the future has a value. Returns a future with the same executor.
template <typename F>
auto then(F&& f) && -> FutureType<typename Type<F>::Ret>;
protected:
std::shared_ptr<FutureDataType<T>> m_data;
};
// A special case for a void future. When continuing a void future, then()
// continuations do not take in any arguments. If a continuation callback
// doesn't return a value, the next link becomes a void future.
template <>
class Future<void> : Future<void_type>
{
public:
using Base = Future<void_type>;
using Base::error;
using Base::hasError;
using Base::hasValue;
using Base::isReady;
using Base::then;
Future(std::shared_ptr<FutureDataType<void>> data);
void get() && { std::move(*this).Base::get(); }
Future<>&& via(Executor* executor) &&;
};
}
#include "details/FutureImpl.hpp"