-
Notifications
You must be signed in to change notification settings - Fork 0
/
future_traits.hh
96 lines (84 loc) · 3.24 KB
/
future_traits.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#pragma once
#include <type_traits>
namespace experimental {
template<typename T>
class future;
namespace impl
{
namespace utils
{
/*
* Future unwrapping
*/
template <typename T>
struct unwrap_future
{
const static bool is_future = false;
using value_type = T;
};
template <template<typename> class T, typename A>
struct unwrap_future<T<A>>
{
const static bool is_future = std::is_same<T<A>, experimental::future<A>>::value;
using value_type = typename std::conditional<is_future, A, T<A>>::type;
};
template <template<typename> class T, typename A>
struct unwrap_future<T<A>&>
{
const static bool is_future = std::is_same<T<A>&, experimental::future<A>&>::value;
using value_type = typename std::conditional<is_future, A, T<A>>::type;
};
/*
* Call a given function either:
* - func(...)
* - func(...).get()
*/
template<bool retFuture>
struct call_get_if_returns_future {};
template<>
struct call_get_if_returns_future<true>
{
template<typename F, typename ...Arg>
static auto invoke(F &&func, Arg &&... arg) -> typename unwrap_future<decltype(func(arg...))>::value_type
{
return func(std::forward<Arg>(arg)...).get();
}
};
template<>
struct call_get_if_returns_future<false>
{
template<typename F, typename ... Arg>
static auto invoke(F &&func, Arg &&...arg) -> typename unwrap_future<decltype(func(arg...))>::value_type
{
return func(std::forward<Arg>(arg)...);
}
};
/*
* Set a given promise either:
* - f.set_value(func(...))
* - func(...); f.set_value();
*/
template<bool passArgument, typename Traits>
struct apply_continuation {};
template<typename Traits>
struct apply_continuation<true, Traits>
{
template<typename Future, typename Function, typename ... Arg>
static auto invoke(Future &&future, Function && func, Arg &&... arg) -> void
{
future.set_value(call_get_if_returns_future<Traits::is_future>::invoke(std::forward<Function>(func),std::forward<Arg>(arg)...));
}
};
template<typename Traits>
struct apply_continuation<false, Traits>
{
template<typename Future, typename Function, typename ... Arg>
static auto invoke(Future &&future, Function && func, Arg &&... arg) -> void
{
call_get_if_returns_future<Traits::is_future>::invoke(std::forward<Function>(func),std::forward<Arg>(arg)...);
future.set_value();
}
};
}//!utils
}//!impl
}//!experimental