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