X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/3a0a299cb69d766f851f6a3f629c46c26d1bd9cf..467a0c53018ee489de1dd7ae61a083d52048b8e8:/include/xbt/future.hpp diff --git a/include/xbt/future.hpp b/include/xbt/future.hpp index 81c88453e6..b7e52e0a8c 100644 --- a/include/xbt/future.hpp +++ b/include/xbt/future.hpp @@ -11,6 +11,9 @@ #include #include +#include + +#include namespace simgrid { namespace xbt { @@ -172,9 +175,17 @@ public: }; /** 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). + * 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 @@ -184,7 +195,7 @@ 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()); @@ -196,7 +207,7 @@ auto fulfillPromise(P& promise, F&& code) -> decltype(promise.set_value()) { try { - (code)(); + std::forward(code)(); promise.set_value(); } catch(...) { @@ -204,6 +215,26 @@ auto fulfillPromise(P& promise, F&& code) } } +/** Set a promise/result from a future/resul + * + * 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(); }); +} + } }