Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
shy attempt at simplifying the simcall mechanism
[simgrid.git] / include / xbt / future.hpp
index aa559db..4d2842f 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2015-2016. The SimGrid Team.
+/* Copyright (c) 2015-2019. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
 
 #include <cstddef>
 
-#include <utility>
+#include <boost/variant.hpp>
 #include <exception>
+#include <functional>
+#include <future> // std::future_error
 #include <stdexcept>
-
 #include <type_traits>
-
-
+#include <utility>
+#include <xbt/ex.h>
 
 namespace simgrid {
 namespace xbt {
 
-/** A value or an exception
+/** A value or an exception (or nothing)
  *
- *  The API is similar to the one of future and promise.
+ *  This is similar to `optional<expected<T>>`` but it with a Future/Promise
+ *  like API.
+ *
+ *  Also the name is not so great.
  **/
 template<class T>
 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;
+    return value_.which() > 0;
   }
   void set_exception(std::exception_ptr e)
   {
-    this->reset();
-    new (&exception_) std::exception_ptr(std::move(e));
-    status_ = ResultStatus::exception;
+    value_ = std::move(e);
   }
   void set_value(T&& value)
   {
-    this->reset();
-    new (&value_) T(std::move(value));
-    status_ = ResultStatus::value;
+    value_ = std::move(value);
   }
   void set_value(T const& value)
   {
-    this->reset();
-    new (&value_) T(value);
-    status_ = ResultStatus::value;
+    value_ = value;
   }
 
   /** 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);
+    switch (value_.which()) {
+      case 1: {
+        T value = std::move(boost::get<T>(value_));
+        value_  = boost::blank();
+        return value;
       }
-      case ResultStatus::exception: {
-        std::exception_ptr exception = std::move(exception_);
-        exception_.~exception_ptr();
-        status_ = ResultStatus::invalid;
+      case 2: {
+        std::exception_ptr exception = std::move(boost::get<std::exception_ptr>(value_));
+        value_                       = boost::blank();
         std::rethrow_exception(std::move(exception));
         break;
       }
+      default:
+        throw std::future_error(std::future_errc::no_state);
     }
   }
 private:
-  ResultStatus status_ = ResultStatus::invalid;
-  union {
-    T value_;
-    std::exception_ptr exception_;
-  };
+  boost::variant<boost::blank, T, std::exception_ptr> value_;
 };
 
 template<>
@@ -184,7 +110,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).
@@ -192,32 +118,26 @@ 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(...) {
+  } catch (...) {
     promise.set_exception(std::current_exception());
   }
 }
 
-template<class P, class F>
-auto fulfillPromise(P& promise, F&& code)
--> decltype(promise.set_value())
+template <class R, class F> auto fulfill_promise(R& promise, F&& code) -> decltype(promise.set_value())
 {
   try {
     std::forward<F>(code)();
     promise.set_value();
-  }
-  catch(...) {
+  } catch (...) {
     promise.set_exception(std::current_exception());
   }
 }
 
-/** Set a promise/result from a future/resul
+/** Set a promise/result from a future/result
  *
  *  Roughly this does:
  *
@@ -231,10 +151,9 @@ 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)
 {
-  fulfillPromise(promise, [&]{ return std::forward<F>(future).get(); });
+  fulfill_promise(promise, [&future] { return std::forward<F>(future).get(); });
 }
 
 }