DUNE PDELab (git)

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// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5#ifndef DUNE_COMMON_PARALLEL_FUTURE_HH
6#define DUNE_COMMON_PARALLEL_FUTURE_HH
7
8#include <memory>
9#include <type_traits>
10
12
13namespace Dune{
14
20 {};
21
22 // forward declaration
23 template<class T>
24 class PseudoFuture;
25
29 template<class T>
30 class Future{
31 // Future interface:
32 class FutureBase{
33 public:
34 virtual ~FutureBase() = default;
35 virtual void wait() = 0;
36 virtual bool ready() const = 0;
37 virtual bool valid() const = 0;
38 virtual T get() = 0;
39 };
40
41 // model class
42 template<class F>
43 class FutureModel
44 : public FutureBase
45 {
46 F _future;
47 public:
48 FutureModel(F&& f)
49 : _future(std::forward<F>(f))
50 {}
51
52 virtual void wait() override
53 {
54 _future.wait();
55 }
56
57 virtual bool ready() const override
58 {
59 return _future.ready();
60 }
61
62 virtual bool valid() const override
63 {
64 return _future.valid();
65 }
66
67 virtual T get() override{
68 return (T)_future.get();
69 }
70 };
71
72 std::unique_ptr<FutureBase> _future;
73 public:
74 template<class F>
75 Future(F&& f)
76 : _future(std::make_unique<FutureModel<F>>(std::forward<F>(f)))
77 {}
78
79 template<class U, std::enable_if_t<std::is_same<U,T>::value && !std::is_same<T,void>::value>>
80 Future(U&& data)
81 : _future(std::make_unique<FutureModel<PseudoFuture<T>>>(PseudoFuture<T>(std::forward<U>(data))))
82 {}
83
84 Future() = default;
85
89 void wait(){
90 _future->wait();
91 }
92
97 T get() {
98 return _future->get();
99 }
100
105 bool ready() const {
106 return _future->ready();
107 }
108
114 bool valid() const {
115 if(_future)
116 return _future->valid();
117 return false;
118 }
119 };
120
123 template<class T>
125 bool valid_;
126 T data_;
127 public:
128 PseudoFuture() :
129 valid_(false)
130 {}
131
132 template<class U>
133 PseudoFuture(U&& u) :
134 valid_(true),
135 data_(std::forward<U>(u))
136 {}
137
138 void wait() {
139 if(!valid_)
140 DUNE_THROW(InvalidFutureException, "The PseudoFuture is not valid");
141 }
142
143 bool ready() const {
144 if(!valid_)
145 DUNE_THROW(InvalidFutureException, "The PseudoFuture is not valid");
146 return true;
147 }
148
149 T get() {
150 if(!valid_)
151 DUNE_THROW(InvalidFutureException, "The PseudoFuture is not valid");
152 valid_ = false;
153 return std::forward<T>(data_);
154 }
155
156 bool valid() const {
157 return valid_;
158 }
159 };
160
161 template<>
162 class PseudoFuture<void>{
163 bool valid_;
164 public:
165 PseudoFuture(bool valid = false) :
166 valid_(valid)
167 {}
168
169 void wait(){
170 if(!valid_)
171 DUNE_THROW(InvalidFutureException, "The PseudoFuture is not valid");
172 }
173 bool ready() const{
174 if(!valid_)
175 DUNE_THROW(InvalidFutureException, "The PseudoFuture is not valid");
176 return true;
177 }
178
179 void get(){
180 if(!valid_)
181 DUNE_THROW(InvalidFutureException, "The PseudoFuture is not valid");
182 valid_ = false;
183 }
184
185 bool valid() const{
186 return valid_;
187 }
188 };
189}
190
191#endif // DUNE_COMMON_PARALLEL_FUTURE_HH
Type-erasure for future-like objects. A future-like object is a object satisfying the interface of Fu...
Definition: future.hh:30
bool ready() const
Definition: future.hh:105
void wait()
wait until the future is ready
Definition: future.hh:89
T get()
Waits until the future is ready and returns the resulting value.
Definition: future.hh:97
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:114
This exception is thrown when ready(), wait() or get() is called on an invalid future....
Definition: future.hh:20
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:281
A wrapper-class for a object which is ready immediately.
Definition: future.hh:124
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 23, 23:29, 2024)