Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d5a31a55ce1b9c0a3a380a698cb772a7cb5468be
[simgrid.git] / include / xbt / future.hpp
1 /* Copyright (c) 2015-2016. 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_FUTURE_HPP
8 #define XBT_FUTURE_HPP
9
10 #include <future>
11 #include <utility>
12 #include <exception>
13
14 namespace simgrid {
15 namespace xbt {
16
17 /** Fulfill a promise by executing a given code */
18 template<class R, class F>
19 void fulfillPromise(std::promise<R>& promise, F code)
20 {
21   try {
22     promise.set_value(code());
23   }
24   catch(...) {
25     promise.set_exception(std::current_exception());
26   }
27 }
28
29 /** Fulfill a promise by executing a given code
30  *
31  *  This is a special version for `std::promise<void>` because the default
32  *  version does not compile in this case.
33  */
34 template<class F>
35 void fulfillPromise(std::promise<void>& promise, F code)
36 {
37   try {
38     (code)();
39     promise.set_value();
40   }
41   catch(...) {
42     promise.set_exception(std::current_exception());
43   }
44 }
45
46 }
47 }
48
49 #endif