75eb26bd4a940239d2fc85db01e35eaa35abdd5f
[folly.git] / folly / futures / test / ThenCompileTest.h
1 /*
2  * Copyright 2016 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 <gtest/gtest.h>
20
21 #include <folly/futures/Future.h>
22
23 #include <memory>
24
25 namespace folly {
26
27 typedef std::unique_ptr<int> A;
28 struct B {};
29
30 template <class T>
31 using EnableIfFuture = typename std::enable_if<isFuture<T>::value>::type;
32
33 template <class T>
34 using EnableUnlessFuture = typename std::enable_if<!isFuture<T>::value>::type;
35
36 template <class T>
37 Future<T> someFuture() {
38   return makeFuture(T());
39 }
40
41 template <class Ret, class... Params>
42 typename std::enable_if<isFuture<Ret>::value, Ret>::type
43 aFunction(Params...) {
44   typedef typename Ret::value_type T;
45   return makeFuture(T());
46 }
47
48 template <class Ret, class... Params>
49 typename std::enable_if<!isFuture<Ret>::value, Ret>::type
50 aFunction(Params...) {
51   return Ret();
52 }
53
54 template <class Ret, class... Params>
55 std::function<Ret(Params...)>
56 aStdFunction(
57     typename std::enable_if<!isFuture<Ret>::value, bool>::type = false) {
58   return [](Params...) -> Ret { return Ret(); };
59 }
60
61 template <class Ret, class... Params>
62 std::function<Ret(Params...)>
63 aStdFunction(typename std::enable_if<isFuture<Ret>::value, bool>::type = true) {
64   typedef typename Ret::value_type T;
65   return [](Params...) -> Future<T> { return makeFuture(T()); };
66 }
67
68 class SomeClass {
69 public:
70   template <class Ret, class... Params>
71   static
72   typename std::enable_if<!isFuture<Ret>::value, Ret>::type
73   aStaticMethod(Params...) {
74     return Ret();
75   }
76
77   template <class Ret, class... Params>
78   static
79   typename std::enable_if<isFuture<Ret>::value, Ret>::type
80   aStaticMethod(Params...) {
81     typedef typename Ret::value_type T;
82     return makeFuture(T());
83   }
84
85   template <class Ret, class... Params>
86   typename std::enable_if<!isFuture<Ret>::value, Ret>::type
87   aMethod(Params...) {
88     return Ret();
89   }
90
91   template <class Ret, class... Params>
92   typename std::enable_if<isFuture<Ret>::value, Ret>::type
93   aMethod(Params...) {
94     typedef typename Ret::value_type T;
95     return makeFuture(T());
96   }
97 };
98
99 }