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