Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
122d59093bffb7617c26931ce39aea51b9c40591
[simgrid.git] / include / xbt / future.hpp
1 /* Copyright (c) 2015-2016. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef XBT_FUTURE_HPP
8 #define XBT_FUTURE_HPP
9
10 #include <cstddef>
11
12 #include <utility>
13 #include <exception>
14
15 namespace simgrid {
16 namespace xbt {
17
18 /** A value or an exception
19  *
20  *  The API is similar to the one of future and promise.
21  **/
22 template<class T>
23 class Result {
24   enum class ResultStatus {
25     invalid,
26     value,
27     exception,
28   };
29 public:
30   Result() {}
31   ~Result() { this->reset(); }
32
33   // Copy (if T is copyable) and move:
34   Result(Result const& that)
35   {
36     (*this) = that;
37   }
38   Result& operator=(Result const& that)
39   {
40     this->reset();
41     switch (that.status_) {
42       case ResultStatus::invalid:
43         break;
44       case ResultStatus::valid:
45         new (&value_) T(that.value);
46         break;
47       case ResultStatus::exception:
48         new (&exception_) T(that.exception);
49         break;
50     }
51     return *this;
52   }
53   Result(Result&& that)
54   {
55     *this = std::move(that);
56   }
57   Result& operator=(Result&& that)
58   {
59     this->reset();
60     switch (that.status_) {
61       case ResultStatus::invalid:
62         break;
63       case ResultStatus::valid:
64         new (&value_) T(std::move(that.value));
65         that.value.~T();
66         break;
67       case ResultStatus::exception:
68         new (&exception_) T(std::move(that.exception));
69         that.exception.~exception_ptr();
70         break;
71     }
72     that.status_ = ResultStatus::invalid;
73     return *this;
74   }
75
76   bool is_valid() const
77   {
78     return status_ != ResultStatus::invalid;
79   }
80   void reset()
81   {
82     switch (status_) {
83       case ResultStatus::invalid:
84         break;
85       case ResultStatus::value:
86         value_.~T();
87         break;
88       case ResultStatus::exception:
89         exception_.~exception_ptr();
90         break;
91     }
92     status_ = ResultStatus::invalid;
93   }
94   void set_exception(std::exception_ptr e)
95   {
96     this->reset();
97     new (&exception_) std::exception_ptr(std::move(e));
98     status_ = ResultStatus::exception;
99   }
100   void set_value(T&& value)
101   {
102     this->reset();
103     new (&value_) T(std::move(value));
104     status_ = ResultStatus::value;
105   }
106   void set_value(T const& value)
107   {
108     this->reset();
109     new (&value_) T(value);
110     status_ = ResultStatus::value;
111   }
112
113   /** Extract the value from the future
114    *
115    *  After this the value is invalid.
116    **/
117   T get()
118   {
119     switch (status_) {
120       case ResultStatus::invalid:
121       default:
122         throw std::logic_error("Invalid result");
123       case ResultStatus::value: {
124         T value = std::move(value_);
125         value_.~T();
126         status_ = ResultStatus::invalid;
127         return std::move(value);
128       }
129       case ResultStatus::exception: {
130         std::exception_ptr exception = std::move(exception_);
131         exception_.~exception_ptr();
132         status_ = ResultStatus::invalid;
133         std::rethrow_exception(std::move(exception));
134         break;
135       }
136     }
137   }
138 private:
139   ResultStatus status_ = ResultStatus::invalid;
140   union {
141     T value_;
142     std::exception_ptr exception_;
143   };
144 };
145
146 template<>
147 class Result<void> : public Result<nullptr_t>
148 {
149 public:
150   void set_value()
151   {
152     Result<std::nullptr_t>::set_value(nullptr);
153   }
154   void get()
155   {
156     Result<nullptr_t>::get();
157   }
158 };
159
160 template<class T>
161 class Result<T&> : public Result<std::reference_wrapper<T>>
162 {
163 public:
164   void set_value(T& value)
165   {
166     Result<std::reference_wrapper<T>>::set_value(std::ref(value));
167   }
168   T& get()
169   {
170     return Result<std::reference_wrapper<T>>::get();
171   }
172 };
173
174 /** Fulfill a promise by executing a given code */
175 template<class R, class F>
176 auto fulfillPromise(R& promise, F&& code)
177 -> decltype(promise.set_value(code()))
178 {
179   try {
180     promise.set_value(code());
181   }
182   catch(...) {
183     promise.set_exception(std::current_exception());
184   }
185 }
186
187 /** Fulfill a promise by executing a given code
188  *
189  *  This is a special version for `std::promise<void>` because the default
190  *  version does not compile in this case.
191  */
192 template<class P, class F>
193 auto fulfillPromise(P& promise, F&& code)
194 -> decltype(promise.set_value())
195 {
196   try {
197     (code)();
198     promise.set_value();
199   }
200   catch(...) {
201     promise.set_exception(std::current_exception());
202   }
203 }
204
205 }
206 }
207
208 #endif