folly/Bits.h (BitIterator): avoid -Wsign-compare error
[folly.git] / folly / wangle / futures / test / Thens.h
1 /*
2  * Copyright 2014 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 #include <gtest/gtest.h>
19 #include <memory>
20 #include <folly/wangle/futures/Future.h>
21 #include <folly/Executor.h>
22
23 using namespace folly::wangle;
24 using namespace std;
25 using namespace testing;
26
27 typedef 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, typename = void>
42 Ret aFunction(Params...);
43
44 template <class Ret, class... Params>
45 typename std::enable_if<isFuture<Ret>::value, Ret>::type
46 aFunction(Params...) {
47   typedef typename Ret::value_type T;
48   return makeFuture(T());
49 }
50
51 template <class Ret, class... Params>
52 typename std::enable_if<!isFuture<Ret>::value, Ret>::type
53 aFunction(Params...) {
54   return Ret();
55 }
56
57 template <class Ret, class... Params>
58 std::function<Ret(Params...)>
59 aStdFunction(
60     typename std::enable_if<!isFuture<Ret>::value, bool>::type = false) {
61   return [](Params...) -> Ret { return Ret(); };
62 }
63
64 template <class Ret, class... Params>
65 std::function<Ret(Params...)>
66 aStdFunction(typename std::enable_if<isFuture<Ret>::value, bool>::type = true) {
67   typedef typename Ret::value_type T;
68   return [](Params...) -> Future<T> { return makeFuture(T()); };
69 }
70
71 class SomeClass {
72   B b;
73 public:
74   template <class Ret, class... Params>
75   static Ret aStaticMethod(Params...);
76
77   template <class Ret, class... Params>
78   static
79   typename std::enable_if<!isFuture<Ret>::value, Ret>::type
80   aStaticMethod(Params...) {
81     return Ret();
82   }
83
84   template <class Ret, class... Params>
85   static
86   typename std::enable_if<isFuture<Ret>::value, Ret>::type
87   aStaticMethod(Params...) {
88     typedef typename Ret::value_type T;
89     return makeFuture(T());
90   }
91
92   template <class Ret, class... Params>
93   Ret aMethod(Params...);
94
95   template <class Ret, class... Params>
96   typename std::enable_if<!isFuture<Ret>::value, Ret>::type
97   aMethod(Params...) {
98     return Ret();
99   }
100
101   template <class Ret, class... Params>
102   typename std::enable_if<isFuture<Ret>::value, Ret>::type
103   aMethod(Params...) {
104     typedef typename Ret::value_type T;
105     return makeFuture(T());
106   }
107 };