DUNE PDELab (2.7)

future.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_COMMON_PARALLEL_FUTURE_HH
4#define DUNE_COMMON_PARALLEL_FUTURE_HH
5
6#include <memory>
8
9namespace Dune{
10
16 {};
17
18 // forward declaration
19 template<class T>
20 class PseudoFuture;
21
25 template<class T>
26 class Future{
27 // Future interface:
28 class FutureBase{
29 public:
30 virtual ~FutureBase() = default;
31 virtual void wait() = 0;
32 virtual bool ready() const = 0;
33 virtual bool valid() const = 0;
34 virtual T get() = 0;
35 };
36
37 // model class
38 template<class F>
39 class FutureModel
40 : public FutureBase
41 {
42 F _future;
43 public:
44 FutureModel(F&& f)
45 : _future(std::forward<F>(f))
46 {}
47
48 virtual void wait() override
49 {
50 _future.wait();
51 }
52
53 virtual bool ready() const override
54 {
55 return _future.ready();
56 }
57
58 virtual bool valid() const override
59 {
60 return _future.valid();
61 }
62
63 virtual T get() override{
64 return (T)_future.get();
65 }
66 };
67
68 std::unique_ptr<FutureBase> _future;
69 public:
70 template<class F>
71 Future(F&& f)
72 : _future(std::make_unique<FutureModel<F>>(std::forward<F>(f)))
73 {}
74
75 template<class U, std::enable_if_t<std::is_same<U,T>::value && !std::is_same<T,void>::value>>
76 Future(U&& data)
77 : _future(std::make_unique<FutureModel<PseudoFuture<T>>>(PseudoFuture<T>(std::forward<U>(data))))
78 {}
79
80 Future() = default;
81
85 void wait(){
86 _future->wait();
87 }
88
93 T get() {
94 return _future->get();
95 }
96
101 bool ready() const {
102 return _future->ready();
103 }
104
110 bool valid() const {
111 if(_future)
112 return _future->valid();
113 return false;
114 }
115 };
116
119 template<class T>
121 bool valid_;
122 T data_;
123 public:
124 PseudoFuture() :
125 valid_(false)
126 {}
127
128 template<class U>
129 PseudoFuture(U&& u) :
130 valid_(true),
131 data_(std::forward<U>(u))
132 {}
133
134 void wait() {
135 if(!valid_)
136 DUNE_THROW(InvalidFutureException, "The PseudoFuture is not valid");
137 }
138
139 bool ready() const {
140 if(!valid_)
141 DUNE_THROW(InvalidFutureException, "The PseudoFuture is not valid");
142 return true;
143 }
144
145 T get() {
146 if(!valid_)
147 DUNE_THROW(InvalidFutureException, "The PseudoFuture is not valid");
148 valid_ = false;
149 return std::forward<T>(data_);
150 }
151
152 bool valid() const {
153 return valid_;
154 }
155 };
156
157 template<>
158 class PseudoFuture<void>{
159 bool valid_;
160 public:
161 PseudoFuture(bool valid = false) :
162 valid_(valid)
163 {}
164
165 void wait(){
166 if(!valid_)
167 DUNE_THROW(InvalidFutureException, "The PseudoFuture is not valid");
168 }
169 bool ready() const{
170 if(!valid_)
171 DUNE_THROW(InvalidFutureException, "The PseudoFuture is not valid");
172 return true;
173 }
174
175 void get(){
176 if(!valid_)
177 DUNE_THROW(InvalidFutureException, "The PseudoFuture is not valid");
178 valid_ = false;
179 }
180
181 bool valid() const{
182 return valid_;
183 }
184 };
185}
186
187#endif
Type-erasure for future-like objects. A future-like object is a object satisfying the interface of Fu...
Definition: future.hh:26
bool ready() const
Definition: future.hh:101
void wait()
wait until the future is ready
Definition: future.hh:85
T get()
Waits until the future is ready and returns the resulting value.
Definition: future.hh:93
bool valid() const
Checks whether the future is valid. I.e. ‘get()’ was not called on that future and when it was not de...
Definition: future.hh:110
This exception is thrown when ready(), wait() or get() is called on an invalid future....
Definition: future.hh:16
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:279
A wrapper-class for a object which is ready immediately.
Definition: future.hh:120
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
Dune namespace.
Definition: alignedallocator.hh:14
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)