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>
21 /** A value or an exception
23 * The API is similar to the one of future and promise.
27 enum class ResultStatus {
34 ~Result() { this->reset(); }
36 // Copy (if T is copyable) and move:
37 Result(Result const& that)
41 Result& operator=(Result const& that)
44 switch (that.status_) {
45 case ResultStatus::invalid:
47 case ResultStatus::valid:
48 new (&value_) T(that.value);
50 case ResultStatus::exception:
51 new (&exception_) T(that.exception);
58 *this = std::move(that);
60 Result& operator=(Result&& that)
63 switch (that.status_) {
64 case ResultStatus::invalid:
66 case ResultStatus::valid:
67 new (&value_) T(std::move(that.value));
70 case ResultStatus::exception:
71 new (&exception_) T(std::move(that.exception));
72 that.exception.~exception_ptr();
75 that.status_ = ResultStatus::invalid;
81 return status_ != ResultStatus::invalid;
86 case ResultStatus::invalid:
88 case ResultStatus::value:
91 case ResultStatus::exception:
92 exception_.~exception_ptr();
95 status_ = ResultStatus::invalid;
97 void set_exception(std::exception_ptr e)
100 new (&exception_) std::exception_ptr(std::move(e));
101 status_ = ResultStatus::exception;
103 void set_value(T&& value)
106 new (&value_) T(std::move(value));
107 status_ = ResultStatus::value;
109 void set_value(T const& value)
112 new (&value_) T(value);
113 status_ = ResultStatus::value;
116 /** Extract the value from the future
118 * After this the value is invalid.
123 case ResultStatus::invalid:
125 throw std::logic_error("Invalid result");
126 case ResultStatus::value: {
127 T value = std::move(value_);
129 status_ = ResultStatus::invalid;
130 return std::move(value);
132 case ResultStatus::exception: {
133 std::exception_ptr exception = std::move(exception_);
134 exception_.~exception_ptr();
135 status_ = ResultStatus::invalid;
136 std::rethrow_exception(std::move(exception));
142 ResultStatus status_ = ResultStatus::invalid;
145 std::exception_ptr exception_;
150 class Result<void> : public Result<std::nullptr_t>
155 Result<std::nullptr_t>::set_value(nullptr);
159 Result<std::nullptr_t>::get();
164 class Result<T&> : public Result<std::reference_wrapper<T>>
167 void set_value(T& value)
169 Result<std::reference_wrapper<T>>::set_value(std::ref(value));
173 return Result<std::reference_wrapper<T>>::get();
177 /** Execute some code and set a promise or result accordingly
182 * promise.set_value(code());
185 * but it takes care of exceptions and works with void.
187 * We might need this when working with generic code because
188 * the trivial implementation does not work with `void` (before C++1z).
190 * @param code What we want to do
191 * @param promise Where to want to store the result
193 template<class R, class F>
194 auto fulfillPromise(R& promise, F&& code)
195 -> decltype(promise.set_value(code()))
198 promise.set_value(std::forward<F>(code)());
201 promise.set_exception(std::current_exception());
205 template<class P, class F>
206 auto fulfillPromise(P& promise, F&& code)
207 -> decltype(promise.set_value())
210 std::forward<F>(code)();
214 promise.set_exception(std::current_exception());
218 /** Set a promise/result from a future/resul
222 * <pre>promise.set_value(future);</pre>
224 * but it takes care of exceptions and works with `void`.
226 * We might need this when working with generic code because
227 * the trivial implementation does not work with `void` (before C++1z).
229 * @param promise output (a valid future or a result)
230 * @param future input (a ready/waitable future or a valid result)
232 template<class P, class F> inline
233 void setPromise(P& promise, F&& future)
235 fulfillPromise(promise, [&]{ return std::forward<F>(future).get(); });