remove eof whitespace lines
[folly.git] / folly / detail / DiscriminatedPtrDetail.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 #ifndef FOLLY_DETAIL_DISCRIMINATEDPTRDETAIL_H_
18 #define FOLLY_DETAIL_DISCRIMINATEDPTRDETAIL_H_
19
20 #include <type_traits>
21
22 namespace folly {
23 namespace dptr_detail {
24
25 /**
26  * Given a target type and a list of types, return the 1-based index of the
27  * type in the list of types.  Fail to compile if the target type doesn't
28  * appear in the list.
29  *
30  * GetIndex<int, void, char, int>::value == 3
31  * GetIndex<int, void, char>::value -> fails to compile
32  */
33 template <typename... Types> struct GetTypeIndex;
34
35 // When recursing, we never reach the 0- or 1- template argument base case
36 // unless the target type is not in the list.  If the target type is in the
37 // list, we stop recursing when it is at the head of the remaining type
38 // list via the GetTypeIndex<T, T, Types...> partial specialization.
39 template <typename T, typename... Types>
40 struct GetTypeIndex<T, T, Types...> {
41   static const size_t value = 1;
42 };
43
44 template <typename T, typename U, typename... Types>
45 struct GetTypeIndex<T, U, Types...> {
46   static const size_t value = 1 + GetTypeIndex<T, Types...>::value;
47 };
48
49 // Generalize std::is_same for variable number of type arguments
50 template <typename... Types>
51 struct IsSameType;
52
53 template <>
54 struct IsSameType<> {
55   static const bool value = true;
56 };
57
58 template <typename T>
59 struct IsSameType<T> {
60   static const bool value = true;
61 };
62
63 template <typename T, typename U, typename... Types>
64 struct IsSameType<T, U, Types...> {
65   static const bool value =
66     std::is_same<T,U>::value && IsSameType<U, Types...>::value;
67 };
68
69 // Define type as the type of all T in (non-empty) Types..., asserting that
70 // all types in Types... are the same.
71 template <typename... Types>
72 struct SameType;
73
74 template <typename T, typename... Types>
75 struct SameType<T, Types...> {
76   typedef T type;
77   static_assert(IsSameType<T, Types...>::value,
78                 "Not all types in pack are the same");
79 };
80
81 // Determine the result type of applying a visitor of type V on a pointer
82 // to type T.
83 template <typename V, typename T>
84 struct VisitorResult1 {
85   typedef typename std::result_of<V (T*)>::type type;
86 };
87
88 // Determine the result type of applying a visitor of type V on a const pointer
89 // to type T.
90 template <typename V, typename T>
91 struct ConstVisitorResult1 {
92   typedef typename std::result_of<V (const T*)>::type type;
93 };
94
95 // Determine the result type of applying a visitor of type V on pointers of
96 // all types in Types..., asserting that the type is the same for all types
97 // in Types...
98 template <typename V, typename... Types>
99 struct VisitorResult {
100   typedef typename SameType<
101     typename VisitorResult1<V,Types>::type...>::type type;
102 };
103
104 // Determine the result type of applying a visitor of type V on const pointers
105 // of all types in Types..., asserting that the type is the same for all types
106 // in Types...
107 template <typename V, typename... Types>
108 struct ConstVisitorResult {
109   typedef typename SameType<
110     typename ConstVisitorResult1<V,Types>::type...>::type type;
111 };
112
113 template <typename V, typename R, typename... Types> struct ApplyVisitor1;
114
115 template <typename V, typename R>
116 struct ApplyVisitor1<V, R> {
117   R operator()(size_t index, V&& visitor, void* ptr) const {
118     CHECK(false);  // NOTREACHED
119   }
120 };
121
122 template <typename V, typename R, typename T, typename... Types>
123 struct ApplyVisitor1<V, R, T, Types...> {
124   R operator()(size_t index, V&& visitor, void* ptr) const {
125     return (index == 1 ? visitor(static_cast<T*>(ptr)) :
126             ApplyVisitor1<V, R, Types...>()(
127               index - 1, std::forward<V>(visitor), ptr));
128   }
129 };
130
131 template <typename V, typename R, typename... Types> struct ApplyConstVisitor1;
132
133 template <typename V, typename R>
134 struct ApplyConstVisitor1<V, R> {
135   R operator()(size_t index, V&& visitor, void* ptr) const {
136     CHECK(false);  // NOTREACHED
137   }
138 };
139
140 template <typename V, typename R, typename T, typename... Types>
141 struct ApplyConstVisitor1<V, R, T, Types...> {
142   R operator()(size_t index, V&& visitor, void* ptr) const {
143     return (index == 1 ? visitor(static_cast<const T*>(ptr)) :
144             ApplyConstVisitor1<V, R, Types...>()(
145               index - 1, std::forward<V>(visitor), ptr));
146   }
147 };
148
149 template <typename V, typename... Types>
150 struct ApplyVisitor
151   : ApplyVisitor1<
152       V, typename VisitorResult<V, Types...>::type, Types...> {
153 };
154
155 template <typename V, typename... Types>
156 struct ApplyConstVisitor
157   : ApplyConstVisitor1<
158       V, typename ConstVisitorResult<V, Types...>::type, Types...> {
159 };
160
161 }  // namespace dptr_detail
162 }  // namespace folly
163
164 #endif /* FOLLY_DETAIL_DISCRIMINATEDPTRDETAIL_H_ */