X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/a15797ea55151ddfdbae48147e74159efe01b411..d22f65624615098a556403243579dd7c4ceb69dd:/include/xbt/future.hpp diff --git a/include/xbt/future.hpp b/include/xbt/future.hpp index d5a31a55ce..a374bec117 100644 --- a/include/xbt/future.hpp +++ b/include/xbt/future.hpp @@ -7,35 +7,210 @@ #ifndef XBT_FUTURE_HPP #define XBT_FUTURE_HPP -#include -#include +#include + #include +#include +#include +#include +#include namespace simgrid { namespace xbt { -/** Fulfill a promise by executing a given code */ +/** A value or an exception (or nothing) + * + * This is similar to `optional>`` but it with a Future/Promise + * like API. + * + * Also the name is not so great. + **/ +template +class Result { + enum class ResultStatus { + invalid, + value, + exception, + }; +public: + Result() {} + ~Result() { this->reset(); } + + // Copy (if T is copyable) and move: + Result(Result const& that) + { + (*this) = that; + } + Result& operator=(Result const& that) + { + this->reset(); + switch (that.status_) { + case ResultStatus::invalid: + break; + case ResultStatus::valid: + new (&value_) T(that.value); + break; + case ResultStatus::exception: + new (&exception_) T(that.exception); + break; + } + return *this; + } + Result(Result&& that) + { + *this = std::move(that); + } + Result& operator=(Result&& that) + { + this->reset(); + switch (that.status_) { + case ResultStatus::invalid: + break; + case ResultStatus::valid: + new (&value_) T(std::move(that.value)); + that.value.~T(); + break; + case ResultStatus::exception: + new (&exception_) T(std::move(that.exception)); + that.exception.~exception_ptr(); + break; + } + that.status_ = ResultStatus::invalid; + return *this; + } + + bool is_valid() const + { + return status_ != ResultStatus::invalid; + } + void reset() + { + switch (status_) { + case ResultStatus::invalid: + break; + case ResultStatus::value: + value_.~T(); + break; + case ResultStatus::exception: + exception_.~exception_ptr(); + break; + } + status_ = ResultStatus::invalid; + } + void set_exception(std::exception_ptr e) + { + this->reset(); + new (&exception_) std::exception_ptr(std::move(e)); + status_ = ResultStatus::exception; + } + void set_value(T&& value) + { + this->reset(); + new (&value_) T(std::move(value)); + status_ = ResultStatus::value; + } + void set_value(T const& value) + { + this->reset(); + new (&value_) T(value); + status_ = ResultStatus::value; + } + + /** Extract the value from the future + * + * After this, the value is invalid. + **/ + T get() + { + switch (status_) { + case ResultStatus::invalid: + default: + throw std::logic_error("Invalid result"); + case ResultStatus::value: { + T value = std::move(value_); + value_.~T(); + status_ = ResultStatus::invalid; + return std::move(value); + } + case ResultStatus::exception: { + std::exception_ptr exception = std::move(exception_); + exception_.~exception_ptr(); + status_ = ResultStatus::invalid; + std::rethrow_exception(std::move(exception)); + break; + } + } + } +private: + ResultStatus status_ = ResultStatus::invalid; + union { + T value_; + std::exception_ptr exception_; + }; +}; + +template<> +class Result : public Result +{ +public: + void set_value() + { + Result::set_value(nullptr); + } + void get() + { + Result::get(); + } +}; + +template +class Result : public Result> +{ +public: + void set_value(T& value) + { + Result>::set_value(std::ref(value)); + } + T& get() + { + return Result>::get(); + } +}; + +/** Execute some code and set a promise or result accordingly + * + * Roughly this does: + * + *
+ *  promise.set_value(code());
+ *  
+ * + * but it takes care of exceptions and works with `void`. + * + * We might need this when working with generic code because + * the trivial implementation does not work with `void` (before C++1z). + * + * @param code What we want to do + * @param promise Where to want to store the result + */ template -void fulfillPromise(std::promise& promise, F code) +auto fulfillPromise(R& promise, F&& code) +-> decltype(promise.set_value(code())) { try { - promise.set_value(code()); + promise.set_value(std::forward(code)()); } catch(...) { promise.set_exception(std::current_exception()); } } -/** Fulfill a promise by executing a given code - * - * This is a special version for `std::promise` because the default - * version does not compile in this case. - */ -template -void fulfillPromise(std::promise& promise, F code) +template +auto fulfillPromise(P& promise, F&& code) +-> decltype(promise.set_value()) { try { - (code)(); + std::forward(code)(); promise.set_value(); } catch(...) { @@ -43,6 +218,26 @@ void fulfillPromise(std::promise& promise, F code) } } +/** Set a promise/result from a future/result + * + * Roughly this does: + * + *
promise.set_value(future);
+ * + * but it takes care of exceptions and works with `void`. + * + * We might need this when working with generic code because + * the trivial implementation does not work with `void` (before C++1z). + * + * @param promise output (a valid future or a result) + * @param future input (a ready/waitable future or a valid result) + */ +template inline +void setPromise(P& promise, F&& future) +{ + fulfillPromise(promise, [&]{ return std::forward(future).get(); }); +} + } }