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 (or nothing)
25 * This is similar to optional<expected<T>> but it with a Future/Promise
28 * Also the name it not so great.
32 enum class ResultStatus {
39 ~Result() { this->reset(); }
41 // Copy (if T is copyable) and move:
42 Result(Result const& that)
46 Result& operator=(Result const& that)
49 switch (that.status_) {
50 case ResultStatus::invalid:
52 case ResultStatus::valid:
53 new (&value_) T(that.value);
55 case ResultStatus::exception:
56 new (&exception_) T(that.exception);
63 *this = std::move(that);
65 Result& operator=(Result&& that)
68 switch (that.status_) {
69 case ResultStatus::invalid:
71 case ResultStatus::valid:
72 new (&value_) T(std::move(that.value));
75 case ResultStatus::exception:
76 new (&exception_) T(std::move(that.exception));
77 that.exception.~exception_ptr();
80 that.status_ = ResultStatus::invalid;
86 return status_ != ResultStatus::invalid;
91 case ResultStatus::invalid:
93 case ResultStatus::value:
96 case ResultStatus::exception:
97 exception_.~exception_ptr();
100 status_ = ResultStatus::invalid;
102 void set_exception(std::exception_ptr e)
105 new (&exception_) std::exception_ptr(std::move(e));
106 status_ = ResultStatus::exception;
108 void set_value(T&& value)
111 new (&value_) T(std::move(value));
112 status_ = ResultStatus::value;
114 void set_value(T const& value)
117 new (&value_) T(value);
118 status_ = ResultStatus::value;
121 /** Extract the value from the future
123 * After this the value is invalid.
128 case ResultStatus::invalid:
130 throw std::logic_error("Invalid result");
131 case ResultStatus::value: {
132 T value = std::move(value_);
134 status_ = ResultStatus::invalid;
135 return std::move(value);
137 case ResultStatus::exception: {
138 std::exception_ptr exception = std::move(exception_);
139 exception_.~exception_ptr();
140 status_ = ResultStatus::invalid;
141 std::rethrow_exception(std::move(exception));
147 ResultStatus status_ = ResultStatus::invalid;
150 std::exception_ptr exception_;
155 class Result<void> : public Result<std::nullptr_t>
160 Result<std::nullptr_t>::set_value(nullptr);
164 Result<std::nullptr_t>::get();
169 class Result<T&> : public Result<std::reference_wrapper<T>>
172 void set_value(T& value)
174 Result<std::reference_wrapper<T>>::set_value(std::ref(value));
178 return Result<std::reference_wrapper<T>>::get();
182 /** Execute some code and set a promise or result accordingly
187 * promise.set_value(code());
190 * but it takes care of exceptions and works with void.
192 * We might need this when working with generic code because
193 * the trivial implementation does not work with `void` (before C++1z).
195 * @param code What we want to do
196 * @param promise Where to want to store the result
198 template<class R, class F>
199 auto fulfillPromise(R& promise, F&& code)
200 -> decltype(promise.set_value(code()))
203 promise.set_value(std::forward<F>(code)());
206 promise.set_exception(std::current_exception());
210 template<class P, class F>
211 auto fulfillPromise(P& promise, F&& code)
212 -> decltype(promise.set_value())
215 std::forward<F>(code)();
219 promise.set_exception(std::current_exception());
223 /** Set a promise/result from a future/resul
227 * <pre>promise.set_value(future);</pre>
229 * but it takes care of exceptions and works with `void`.
231 * We might need this when working with generic code because
232 * the trivial implementation does not work with `void` (before C++1z).
234 * @param promise output (a valid future or a result)
235 * @param future input (a ready/waitable future or a valid result)
237 template<class P, class F> inline
238 void setPromise(P& promise, F&& future)
240 fulfillPromise(promise, [&]{ return std::forward<F>(future).get(); });