Logo AND Algorithmique Numérique Distribuée

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