Dune Core Modules (2.9.1)

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 (C) 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>
10
11namespace Dune{
12
18 {};
19
20 // forward declaration
21 template<class T>
22 class PseudoFuture;
23
27 template<class T>
28 class Future{
29 // Future interface:
30 class FutureBase{
31 public:
32 virtual ~FutureBase() = default;
33 virtual void wait() = 0;
34 virtual bool ready() const = 0;
35 virtual bool valid() const = 0;
36 virtual T get() = 0;
37 };
38
39 // model class
40 template<class F>
41 class FutureModel
42 : public FutureBase
43 {
44 F _future;
45 public:
46 FutureModel(F&& f)
47 : _future(std::forward<F>(f))
48 {}
49
50 virtual void wait() override
51 {
52 _future.wait();
53 }
54
55 virtual bool ready() const override
56 {
57 return _future.ready();
58 }
59
60 virtual bool valid() const override
61 {
62 return _future.valid();
63 }
64
65 virtual T get() override{
66 return (T)_future.get();
67 }
68 };
69
70 std::unique_ptr<FutureBase> _future;
71 public:
72 template<class F>
73 Future(F&& f)
74 : _future(std::make_unique<FutureModel<F>>(std::forward<F>(f)))
75 {}
76
77 template<class U, std::enable_if_t<std::is_same<U,T>::value && !std::is_same<T,void>::value>>
78 Future(U&& data)
79 : _future(std::make_unique<FutureModel<PseudoFuture<T>>>(PseudoFuture<T>(std::forward<U>(data))))
80 {}
81
82 Future() = default;
83
87 void wait(){
88 _future->wait();
89 }
90
95 T get() {
96 return _future->get();
97 }
98
103 bool ready() const {
104 return _future->ready();
105 }
106
112 bool valid() const {
113 if(_future)
114 return _future->valid();
115 return false;
116 }
117 };
118
121 template<class T>
123 bool valid_;
124 T data_;
125 public:
126 PseudoFuture() :
127 valid_(false)
128 {}
129
130 template<class U>
131 PseudoFuture(U&& u) :
132 valid_(true),
133 data_(std::forward<U>(u))
134 {}
135
136 void wait() {
137 if(!valid_)
138 DUNE_THROW(InvalidFutureException, "The PseudoFuture is not valid");
139 }
140
141 bool ready() const {
142 if(!valid_)
143 DUNE_THROW(InvalidFutureException, "The PseudoFuture is not valid");
144 return true;
145 }
146
147 T get() {
148 if(!valid_)
149 DUNE_THROW(InvalidFutureException, "The PseudoFuture is not valid");
150 valid_ = false;
151 return std::forward<T>(data_);
152 }
153
154 bool valid() const {
155 return valid_;
156 }
157 };
158
159 template<>
160 class PseudoFuture<void>{
161 bool valid_;
162 public:
163 PseudoFuture(bool valid = false) :
164 valid_(valid)
165 {}
166
167 void wait(){
168 if(!valid_)
169 DUNE_THROW(InvalidFutureException, "The PseudoFuture is not valid");
170 }
171 bool ready() const{
172 if(!valid_)
173 DUNE_THROW(InvalidFutureException, "The PseudoFuture is not valid");
174 return true;
175 }
176
177 void get(){
178 if(!valid_)
179 DUNE_THROW(InvalidFutureException, "The PseudoFuture is not valid");
180 valid_ = false;
181 }
182
183 bool valid() const{
184 return valid_;
185 }
186 };
187}
188
189#endif
Type-erasure for future-like objects. A future-like object is a object satisfying the interface of Fu...
Definition: future.hh:28
bool ready() const
Definition: future.hh:103
void wait()
wait until the future is ready
Definition: future.hh:87
T get()
Waits until the future is ready and returns the resulting value.
Definition: future.hh:95
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:112
This exception is thrown when ready(), wait() or get() is called on an invalid future....
Definition: future.hh:18
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:122
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 (Jul 15, 22:36, 2024)