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. */
15 #include <type_traits>
21 /** A value or an exception (or nothing)
23 * This is similar to `optional<expected<T>>`` but it with a Future/Promise
26 * Also the name is not so great.
30 enum class ResultStatus {
37 ~Result() { this->reset(); }
39 // Copy (if T is copyable) and move:
40 Result(Result const& that)
44 Result& operator=(Result const& that)
47 switch (that.status_) {
48 case ResultStatus::invalid:
50 case ResultStatus::valid:
51 new (&value_) T(that.value);
53 case ResultStatus::exception:
54 new (&exception_) T(that.exception);
61 *this = std::move(that);
63 Result& operator=(Result&& that)
66 switch (that.status_) {
67 case ResultStatus::invalid:
69 case ResultStatus::valid:
70 new (&value_) T(std::move(that.value));
73 case ResultStatus::exception:
74 new (&exception_) T(std::move(that.exception));
75 that.exception.~exception_ptr();
78 that.status_ = ResultStatus::invalid;
84 return status_ != ResultStatus::invalid;
89 case ResultStatus::invalid:
91 case ResultStatus::value:
94 case ResultStatus::exception:
95 exception_.~exception_ptr();
98 status_ = ResultStatus::invalid;
100 void set_exception(std::exception_ptr e)
103 new (&exception_) std::exception_ptr(std::move(e));
104 status_ = ResultStatus::exception;
106 void set_value(T&& value)
109 new (&value_) T(std::move(value));
110 status_ = ResultStatus::value;
112 void set_value(T const& value)
115 new (&value_) T(value);
116 status_ = ResultStatus::value;
119 /** Extract the value from the future
121 * After this, the value is invalid.
126 case ResultStatus::invalid:
128 throw std::logic_error("Invalid result");
129 case ResultStatus::value: {
130 T value = std::move(value_);
132 status_ = ResultStatus::invalid;
133 return std::move(value);
135 case ResultStatus::exception: {
136 std::exception_ptr exception = std::move(exception_);
137 exception_.~exception_ptr();
138 status_ = ResultStatus::invalid;
139 std::rethrow_exception(std::move(exception));
145 ResultStatus status_ = ResultStatus::invalid;
148 std::exception_ptr exception_;
153 class Result<void> : public Result<std::nullptr_t>
158 Result<std::nullptr_t>::set_value(nullptr);
162 Result<std::nullptr_t>::get();
167 class Result<T&> : public Result<std::reference_wrapper<T>>
170 void set_value(T& value)
172 Result<std::reference_wrapper<T>>::set_value(std::ref(value));
176 return Result<std::reference_wrapper<T>>::get();
180 /** Execute some code and set a promise or result accordingly
185 * promise.set_value(code());
188 * but it takes care of exceptions and works with `void`.
190 * We might need this when working with generic code because
191 * the trivial implementation does not work with `void` (before C++1z).
193 * @param code What we want to do
194 * @param promise Where to want to store the result
196 template<class R, class F>
197 auto fulfillPromise(R& promise, F&& code)
198 -> decltype(promise.set_value(code()))
201 promise.set_value(std::forward<F>(code)());
204 promise.set_exception(std::current_exception());
208 template<class P, class F>
209 auto fulfillPromise(P& promise, F&& code)
210 -> decltype(promise.set_value())
213 std::forward<F>(code)();
217 promise.set_exception(std::current_exception());
221 /** Set a promise/result from a future/result
225 * <pre>promise.set_value(future);</pre>
227 * but it takes care of exceptions and works with `void`.
229 * We might need this when working with generic code because
230 * the trivial implementation does not work with `void` (before C++1z).
232 * @param promise output (a valid future or a result)
233 * @param future input (a ready/waitable future or a valid result)
235 template<class P, class F> inline
236 void setPromise(P& promise, F&& future)
238 fulfillPromise(promise, [&]{ return std::forward<F>(future).get(); });