1 /* Copyright (c) 2015-2016. The SimGrid Team.
2 * All rights reserved. */
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. */
16 #include <type_traits>
23 /** A value or an exception
25 * The API is similar to the one of future and promise.
29 enum class ResultStatus {
36 ~Result() { this->reset(); }
38 // Copy (if T is copyable) and move:
39 Result(Result const& that)
43 Result& operator=(Result const& that)
46 switch (that.status_) {
47 case ResultStatus::invalid:
49 case ResultStatus::valid:
50 new (&value_) T(that.value);
52 case ResultStatus::exception:
53 new (&exception_) T(that.exception);
60 *this = std::move(that);
62 Result& operator=(Result&& that)
65 switch (that.status_) {
66 case ResultStatus::invalid:
68 case ResultStatus::valid:
69 new (&value_) T(std::move(that.value));
72 case ResultStatus::exception:
73 new (&exception_) T(std::move(that.exception));
74 that.exception.~exception_ptr();
77 that.status_ = ResultStatus::invalid;
83 return status_ != ResultStatus::invalid;
88 case ResultStatus::invalid:
90 case ResultStatus::value:
93 case ResultStatus::exception:
94 exception_.~exception_ptr();
97 status_ = ResultStatus::invalid;
99 void set_exception(std::exception_ptr e)
102 new (&exception_) std::exception_ptr(std::move(e));
103 status_ = ResultStatus::exception;
105 void set_value(T&& value)
108 new (&value_) T(std::move(value));
109 status_ = ResultStatus::value;
111 void set_value(T const& value)
114 new (&value_) T(value);
115 status_ = ResultStatus::value;
118 /** Extract the value from the future
120 * After this the value is invalid.
125 case ResultStatus::invalid:
127 throw std::logic_error("Invalid result");
128 case ResultStatus::value: {
129 T value = std::move(value_);
131 status_ = ResultStatus::invalid;
132 return std::move(value);
134 case ResultStatus::exception: {
135 std::exception_ptr exception = std::move(exception_);
136 exception_.~exception_ptr();
137 status_ = ResultStatus::invalid;
138 std::rethrow_exception(std::move(exception));
144 ResultStatus status_ = ResultStatus::invalid;
147 std::exception_ptr exception_;
152 class Result<void> : public Result<std::nullptr_t>
157 Result<std::nullptr_t>::set_value(nullptr);
161 Result<std::nullptr_t>::get();
166 class Result<T&> : public Result<std::reference_wrapper<T>>
169 void set_value(T& value)
171 Result<std::reference_wrapper<T>>::set_value(std::ref(value));
175 return Result<std::reference_wrapper<T>>::get();
179 /** Execute some code and set a promise or result accordingly
184 * promise.set_value(code());
187 * but it takes care of exceptions and works with void.
189 * We might need this when working with generic code because
190 * the trivial implementation does not work with `void` (before C++1z).
192 * @param code What we want to do
193 * @param promise Where to want to store the result
195 template<class R, class F>
196 auto fulfillPromise(R& promise, F&& code)
197 -> decltype(promise.set_value(code()))
200 promise.set_value(std::forward<F>(code)());
203 promise.set_exception(std::current_exception());
207 template<class P, class F>
208 auto fulfillPromise(P& promise, F&& code)
209 -> decltype(promise.set_value())
212 std::forward<F>(code)();
216 promise.set_exception(std::current_exception());
220 /** Set a promise/result from a future/resul
224 * <pre>promise.set_value(future);</pre>
226 * but it takes care of exceptions and works with `void`.
228 * We might need this when working with generic code because
229 * the trivial implementation does not work with `void` (before C++1z).
231 * @param promise output (a valid future or a result)
232 * @param future input (a ready/waitable future or a valid result)
234 template<class P, class F> inline
235 void setPromise(P& promise, F&& future)
237 fulfillPromise(promise, [&]{ return std::forward<F>(future).get(); });