Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / include / xbt / promise.hpp
1 /* Copyright (c) 2015-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef XBT_PROMISE_HPP
7 #define XBT_PROMISE_HPP
8
9 #include <cstddef>
10
11 #include <boost/variant.hpp>
12 #include <exception>
13 #include <functional>
14 #include <future> // std::future_error
15 #include <stdexcept>
16 #include <type_traits>
17 #include <utility>
18 #include <xbt/ex.h>
19
20 namespace simgrid::xbt {
21
22 /** A value or an exception (or nothing)
23  *
24  *  This is similar to `optional<expected<T>>`` but it with a Future/Promise
25  *  like API.
26  *
27  *  Also the name is not so great.
28  **/
29 template <class T> class Result {
30 public:
31   bool is_valid() const { return value_.which() > 0; }
32   void set_exception(std::exception_ptr e) { value_ = std::move(e); }
33   void set_value(T&& value) { value_ = std::move(value); }
34   void set_value(T const& value) { value_ = value; }
35
36   /** Extract the value from the future
37    *
38    *  After this, the value is invalid.
39    **/
40   T get()
41   {
42     switch (value_.which()) {
43       case 1: {
44         T value = std::move(boost::get<T>(value_));
45         value_  = boost::blank();
46         return value;
47       }
48       case 2: {
49         std::exception_ptr exception = std::move(boost::get<std::exception_ptr>(value_));
50         value_                       = boost::blank();
51         std::rethrow_exception(std::move(exception));
52         break;
53       }
54       default:
55         throw std::future_error(std::future_errc::no_state);
56     }
57   }
58
59 private:
60   boost::variant<boost::blank, T, std::exception_ptr> value_;
61 };
62
63 template <> class Result<void> : public Result<std::nullptr_t> {
64 public:
65   void set_value() { Result<std::nullptr_t>::set_value(nullptr); }
66   void get() { Result<std::nullptr_t>::get(); }
67 };
68
69 template <class T> class Result<T&> : public Result<std::reference_wrapper<T>> {
70 public:
71   void set_value(T& value) { Result<std::reference_wrapper<T>>::set_value(std::ref(value)); }
72   T& get() { return Result<std::reference_wrapper<T>>::get(); }
73 };
74
75 /** Execute some code and set a promise or result accordingly
76  *
77  *  Roughly this does:
78  *
79  *  <pre>
80  *  promise.set_value(code());
81  *  </pre>
82  *
83  *  but it takes care of exceptions and works with `void`.
84  *
85  *  We might need this when working with generic code because
86  *  the trivial implementation does not work with `void` (before C++1z).
87  *
88  *  @param    code  What we want to do
89  *  @param  promise Where to want to store the result
90  */
91 template <class R, class F> auto fulfill_promise(R& promise, F&& code) -> decltype(promise.set_value(code()))
92 {
93   try {
94     promise.set_value(std::forward<F>(code)());
95   } catch (...) {
96     promise.set_exception(std::current_exception());
97   }
98 }
99
100 template <class R, class F> auto fulfill_promise(R& promise, F&& code) -> decltype(promise.set_value())
101 {
102   try {
103     std::forward<F>(code)();
104     promise.set_value();
105   } catch (...) {
106     promise.set_exception(std::current_exception());
107   }
108 }
109
110 /** Set a promise/result from a future/result
111  *
112  *  Roughly this does:
113  *
114  *  <pre>promise.set_value(future);</pre>
115  *
116  *  but it takes care of exceptions and works with `void`.
117  *
118  *  We might need this when working with generic code because
119  *  the trivial implementation does not work with `void` (before C++1z).
120  *
121  *  @param promise output (a valid future or a result)
122  *  @param future  input (a ready/waitable future or a valid result)
123  */
124 template <class P, class F> inline void set_promise(P& promise, F&& future)
125 {
126   fulfill_promise(promise, [&future] { return std::forward<F>(future).get(); });
127 }
128 } // namespace simgrid::xbt
129
130 #endif