1 | /* |
2 | * Copyright 2014-present Facebook, Inc. |
3 | * |
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 |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
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. |
15 | */ |
16 | |
17 | #pragma once |
18 | |
19 | #include <folly/futures/Future.h> |
20 | #include <folly/portability/GTest.h> |
21 | |
22 | #include <memory> |
23 | |
24 | namespace folly { |
25 | |
26 | typedef std::unique_ptr<int> A; |
27 | struct B {}; |
28 | |
29 | template <class T> |
30 | using EnableIfFuture = typename std::enable_if<isFuture<T>::value>::type; |
31 | |
32 | template <class T> |
33 | using EnableUnlessFuture = typename std::enable_if<!isFuture<T>::value>::type; |
34 | |
35 | template <class T> |
36 | Future<T> someFuture() { |
37 | return makeFuture(T()); |
38 | } |
39 | |
40 | template <class Ret, class... Params> |
41 | typename std::enable_if<isFuture<Ret>::value, Ret>::type aFunction(Params...) { |
42 | typedef typename Ret::value_type T; |
43 | return makeFuture(T()); |
44 | } |
45 | |
46 | template <class Ret, class... Params> |
47 | typename std::enable_if<!isFuture<Ret>::value, Ret>::type aFunction(Params...) { |
48 | return Ret(); |
49 | } |
50 | |
51 | template <class Ret, class... Params> |
52 | std::function<Ret(Params...)> aStdFunction( |
53 | typename std::enable_if<!isFuture<Ret>::value, bool>::type = false) { |
54 | return [](Params...) -> Ret { return Ret(); }; |
55 | } |
56 | |
57 | template <class Ret, class... Params> |
58 | std::function<Ret(Params...)> aStdFunction( |
59 | typename std::enable_if<isFuture<Ret>::value, bool>::type = true) { |
60 | typedef typename Ret::value_type T; |
61 | return [](Params...) -> Future<T> { return makeFuture(T()); }; |
62 | } |
63 | |
64 | class SomeClass { |
65 | public: |
66 | template <class Ret, class... Params> |
67 | static typename std::enable_if<!isFuture<Ret>::value, Ret>::type |
68 | aStaticMethod(Params...) { |
69 | return Ret(); |
70 | } |
71 | |
72 | template <class Ret, class... Params> |
73 | static typename std::enable_if<isFuture<Ret>::value, Ret>::type aStaticMethod( |
74 | Params...) { |
75 | typedef typename Ret::value_type T; |
76 | return makeFuture(T()); |
77 | } |
78 | |
79 | template <class Ret, class... Params> |
80 | typename std::enable_if<!isFuture<Ret>::value, Ret>::type aMethod(Params...) { |
81 | return Ret(); |
82 | } |
83 | |
84 | template <class Ret, class... Params> |
85 | typename std::enable_if<isFuture<Ret>::value, Ret>::type aMethod(Params...) { |
86 | typedef typename Ret::value_type T; |
87 | return makeFuture(T()); |
88 | } |
89 | }; |
90 | |
91 | } // namespace folly |
92 | |