X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/30fc130fdb83edd3ab2837fa367c6bf45523220c..4f2e9b5f5dd58b3493e5ce66014e52c76f68a777:/include/xbt/future.hpp diff --git a/include/xbt/future.hpp b/include/xbt/future.hpp index 316f68f675..521b02ba94 100644 --- a/include/xbt/future.hpp +++ b/include/xbt/future.hpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2015-2016. The SimGrid Team. +/* Copyright (c) 2015-2018. The SimGrid Team. * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it @@ -9,15 +9,22 @@ #include -#include #include +#include +#include +#include +#include +#include namespace simgrid { namespace xbt { -/** A value or an exception +/** A value or an exception (or nothing) + * + * This is similar to `optional>`` but it with a Future/Promise + * like API. * - * The API is similar to the one of future and promise. + * Also the name is not so great. **/ template class Result { @@ -27,7 +34,7 @@ class Result { exception, }; public: - Result() {} + Result() { /* Nothing to do */} ~Result() { this->reset(); } // Copy (if T is copyable) and move: @@ -41,12 +48,14 @@ public: switch (that.status_) { case ResultStatus::invalid: break; - case ResultStatus::valid: + case ResultStatus::value: new (&value_) T(that.value); break; case ResultStatus::exception: new (&exception_) T(that.exception); break; + default: + THROW_IMPOSSIBLE; } return *this; } @@ -60,7 +69,7 @@ public: switch (that.status_) { case ResultStatus::invalid: break; - case ResultStatus::valid: + case ResultStatus::value: new (&value_) T(std::move(that.value)); that.value.~T(); break; @@ -68,6 +77,8 @@ public: new (&exception_) T(std::move(that.exception)); that.exception.~exception_ptr(); break; + default: + THROW_IMPOSSIBLE; } that.status_ = ResultStatus::invalid; return *this; @@ -88,6 +99,8 @@ public: case ResultStatus::exception: exception_.~exception_ptr(); break; + default: + THROW_IMPOSSIBLE; } status_ = ResultStatus::invalid; } @@ -112,19 +125,16 @@ public: /** Extract the value from the future * - * After this the value is invalid. + * 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); + return value; } case ResultStatus::exception: { std::exception_ptr exception = std::move(exception_); @@ -133,6 +143,8 @@ public: std::rethrow_exception(std::move(exception)); break; } + default: + throw std::logic_error("Invalid result"); } } private: @@ -171,30 +183,57 @@ public: } }; -/** Fulfill a promise by executing a given code */ -template -auto fulfillPromise(R& promise, F&& code) --> decltype(promise.set_value(code())) +/** 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 auto fulfill_promise(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()); + } +} +template +XBT_ATTRIB_DEPRECATED_v323("Please use xbt::fulfill_promise()") auto fulfillPromise(R& promise, F&& code) + -> decltype(promise.set_value(code())) +{ + try { + 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 -auto fulfillPromise(P& promise, F&& code) --> decltype(promise.set_value()) +template auto fulfill_promise(P& promise, F&& code) -> decltype(promise.set_value()) { try { - (code)(); + std::forward(code)(); + promise.set_value(); + } catch (...) { + promise.set_exception(std::current_exception()); + } +} +template +XBT_ATTRIB_DEPRECATED_v323("Please use xbt::fulfill_promise()") auto fulfillPromise(P& promise, F&& code) + -> decltype(promise.set_value()) +{ + try { + std::forward(code)(); promise.set_value(); } catch(...) { @@ -202,6 +241,30 @@ auto fulfillPromise(P& 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 set_promise(P& promise, F&& future) +{ + fulfill_promise(promise, [&] { return std::forward(future).get(); }); +} +template +inline XBT_ATTRIB_DEPRECATED_v323("Please use xbt::set_promise()") void setPromise(P& promise, F&& future) +{ + fulfill_promise(promise, [&] { return std::forward(future).get(); }); +} + } }