]> git.sesse.net Git - casparcg/blob - common/future.h
Remove most of boost::lexical_cast.
[casparcg] / common / future.h
1 #pragma once
2
3 #include <boost/function.hpp>
4 #include <boost/optional.hpp>
5
6 #include <functional>
7 #include <future>
8
9 namespace caspar {
10
11 template<typename T>
12 auto flatten(std::future<T>&& f) -> std::future<typename std::decay<decltype(f.get().get())>::type>
13 {
14         auto shared_f = f.share();
15         return std::async(std::launch::deferred, [=]() mutable -> typename std::decay<decltype(f.get().get())>::type
16         {
17                 return shared_f.get().get();
18         });
19 }
20
21 template<typename F>
22 bool is_ready(const F& future)
23 {
24         return future.wait_for(std::chrono::seconds(0)) == std::future_status::ready;
25 }
26
27 /**
28  * Wrap a value in a future with an already known result.
29  * <p>
30  * Useful when the result of an operation is already known at the time of
31  * calling.
32  *
33  * @param value The r-value to wrap.
34  *
35  * @return The future with the result set.
36  */
37 template<class R>
38 std::future<R> make_ready_future(R&& value)
39 {
40         std::promise<R> p;
41
42         p.set_value(value);
43
44         return p.get_future();
45 }
46
47 static std::future<void> make_ready_future()
48 {
49         std::promise<void> p;
50
51         p.set_value();
52
53         return p.get_future();
54 }
55
56 }