2 * Copyright 2015 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 // included by Future.h, do not include directly.
23 template <class> class Promise;
26 struct isFuture : std::false_type {
31 struct isFuture<Future<T>> : std::true_type {
36 struct isTry : std::false_type {};
39 struct isTry<Try<T>> : std::true_type {};
43 template <class> class Core;
44 template <class...> struct CollectAllVariadicContext;
45 template <class...> struct CollectVariadicContext;
46 template <class> struct CollectContext;
48 template<typename F, typename... Args>
49 using resultOf = decltype(std::declval<F>()(std::declval<Args>()...));
51 template <typename...>
54 template <typename Arg, typename... Args>
55 struct ArgType<Arg, Args...> {
61 typedef void FirstArg;
64 template <bool isTry, typename F, typename... Args>
66 typedef resultOf<F, Args...> Result;
69 template<typename F, typename... Args>
72 typename = detail::resultOf<T, Args...>>
73 static constexpr std::true_type
74 check(std::nullptr_t) { return std::true_type{}; };
77 static constexpr std::false_type
78 check(...) { return std::false_type{}; };
80 typedef decltype(check<F>(nullptr)) type;
81 static constexpr bool value = type::value;
84 template<typename T, typename F>
85 struct callableResult {
86 typedef typename std::conditional<
87 callableWith<F>::value,
88 detail::argResult<false, F>,
89 typename std::conditional<
90 callableWith<F, T&&>::value,
91 detail::argResult<false, F, T&&>,
92 typename std::conditional<
93 callableWith<F, T&>::value,
94 detail::argResult<false, F, T&>,
95 typename std::conditional<
96 callableWith<F, Try<T>&&>::value,
97 detail::argResult<true, F, Try<T>&&>,
98 detail::argResult<true, F, Try<T>&>>::type>::type>::type>::type Arg;
99 typedef isFuture<typename Arg::Result> ReturnsFuture;
100 typedef Future<typename ReturnsFuture::Inner> Return;
104 struct callableResult<void, F> {
105 typedef typename std::conditional<
106 callableWith<F>::value,
107 detail::argResult<false, F>,
108 typename std::conditional<
109 callableWith<F, Try<void>&&>::value,
110 detail::argResult<true, F, Try<void>&&>,
111 detail::argResult<true, F, Try<void>&>>::type>::type Arg;
112 typedef isFuture<typename Arg::Result> ReturnsFuture;
113 typedef Future<typename ReturnsFuture::Inner> Return;
116 template <typename L>
117 struct Extract : Extract<decltype(&L::operator())> { };
119 template <typename Class, typename R, typename... Args>
120 struct Extract<R(Class::*)(Args...) const> {
121 typedef isFuture<R> ReturnsFuture;
122 typedef Future<typename ReturnsFuture::Inner> Return;
123 typedef typename ReturnsFuture::Inner RawReturn;
124 typedef typename ArgType<Args...>::FirstArg FirstArg;
127 template <typename Class, typename R, typename... Args>
128 struct Extract<R(Class::*)(Args...)> {
129 typedef isFuture<R> ReturnsFuture;
130 typedef Future<typename ReturnsFuture::Inner> Return;
131 typedef typename ReturnsFuture::Inner RawReturn;
132 typedef typename ArgType<Args...>::FirstArg FirstArg;