Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix gcc 9 warnings. Have to check if the std::move removal is harmful
[simgrid.git] / include / xbt / future.hpp
index 578e7b2..521b02b 100644 (file)
@@ -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
 #include <stdexcept>
 #include <type_traits>
 #include <utility>
+#include <xbt/ex.h>
 
 namespace simgrid {
 namespace xbt {
 
 /** A value or an exception (or nothing)
  *
- *  This is similar to optional<expected<T>> but it with a Future/Promise
+ *  This is similar to `optional<expected<T>>`` but it with a Future/Promise
  *  like API.
  *
- *  Also the name it not so great.
+ *  Also the name is not so great.
  **/
 template<class T>
 class Result {
@@ -33,7 +34,7 @@ class Result {
     exception,
   };
 public:
-  Result() {}
+  Result() { /* Nothing to do */}
   ~Result() { this->reset(); }
 
   // Copy (if T is copyable) and move:
@@ -47,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;
   }
@@ -66,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;
@@ -74,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;
@@ -94,6 +99,8 @@ public:
       case ResultStatus::exception:
         exception_.~exception_ptr();
         break;
+      default:
+        THROW_IMPOSSIBLE;
     }
     status_ = ResultStatus::invalid;
   }
@@ -118,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_);
@@ -139,6 +143,8 @@ public:
         std::rethrow_exception(std::move(exception));
         break;
       }
+      default:
+        throw std::logic_error("Invalid result");
     }
   }
 private:
@@ -185,7 +191,7 @@ public:
  *  promise.set_value(code());
  *  </pre>
  *
- *  but it takes care of exceptions and works with void.
+ *  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).
@@ -193,9 +199,17 @@ public:
  *  @param    code  What we want to do
  *  @param  promise Where to want to store the result
  */
-template<class R, class F>
-auto fulfillPromise(R& promise, F&& code)
--> decltype(promise.set_value(code()))
+template <class R, class F> auto fulfill_promise(R& promise, F&& code) -> decltype(promise.set_value(code()))
+{
+  try {
+    promise.set_value(std::forward<F>(code)());
+  } catch (...) {
+    promise.set_exception(std::current_exception());
+  }
+}
+template <class R, class F>
+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<F>(code)());
@@ -205,9 +219,18 @@ auto fulfillPromise(R& promise, F&& code)
   }
 }
 
-template<class P, class F>
-auto fulfillPromise(P& promise, F&& code)
--> decltype(promise.set_value())
+template <class P, class F> auto fulfill_promise(P& promise, F&& code) -> decltype(promise.set_value())
+{
+  try {
+    std::forward<F>(code)();
+    promise.set_value();
+  } catch (...) {
+    promise.set_exception(std::current_exception());
+  }
+}
+template <class P, class F>
+XBT_ATTRIB_DEPRECATED_v323("Please use xbt::fulfill_promise()") auto fulfillPromise(P& promise, F&& code)
+    -> decltype(promise.set_value())
 {
   try {
     std::forward<F>(code)();
@@ -218,7 +241,7 @@ auto fulfillPromise(P& promise, F&& code)
   }
 }
 
-/** Set a promise/result from a future/resul
+/** Set a promise/result from a future/result
  *
  *  Roughly this does:
  *
@@ -232,10 +255,14 @@ auto fulfillPromise(P& promise, F&& code)
  *  @param promise output (a valid future or a result)
  *  @param future  input (a ready/waitable future or a valid result)
  */
-template<class P, class F> inline
-void setPromise(P& promise, F&& future)
+template <class P, class F> inline void set_promise(P& promise, F&& future)
+{
+  fulfill_promise(promise, [&] { return std::forward<F>(future).get(); });
+}
+template <class P, class F>
+inline XBT_ATTRIB_DEPRECATED_v323("Please use xbt::set_promise()") void setPromise(P& promise, F&& future)
 {
-  fulfillPromise(promise, [&]{ return std::forward<F>(future).get(); });
+  fulfill_promise(promise, [&] { return std::forward<F>(future).get(); });
 }
 
 }