2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <type_traits>
23 namespace dptr_detail {
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
30 * GetIndex<int, void, char, int>::value == 3
31 * GetIndex<int, void, char>::value -> fails to compile
33 template <typename... Types> struct GetTypeIndex;
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;
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;
49 // Generalize std::is_same for variable number of type arguments
50 template <typename... Types>
55 static const bool value = true;
59 struct IsSameType<T> {
60 static const bool value = true;
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;
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>
74 template <typename T, typename... Types>
75 struct SameType<T, Types...> {
77 static_assert(IsSameType<T, Types...>::value,
78 "Not all types in pack are the same");
81 // Determine the result type of applying a visitor of type V on a pointer
83 template <typename V, typename T>
84 struct VisitorResult1 {
85 typedef typename std::result_of<V (T*)>::type type;
88 // Determine the result type of applying a visitor of type V on a const pointer
90 template <typename V, typename T>
91 struct ConstVisitorResult1 {
92 typedef typename std::result_of<V (const T*)>::type type;
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
98 template <typename V, typename... Types>
99 struct VisitorResult {
100 typedef typename SameType<
101 typename VisitorResult1<V,Types>::type...>::type type;
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
107 template <typename V, typename... Types>
108 struct ConstVisitorResult {
109 typedef typename SameType<
110 typename ConstVisitorResult1<V,Types>::type...>::type type;
113 template <size_t index, typename V, typename R, typename... Types>
114 struct ApplyVisitor1;
116 template <typename V, typename R, typename T, typename... Types>
117 struct ApplyVisitor1<1, V, R, T, Types...> {
118 R operator()(size_t, V&& visitor, void* ptr) const {
119 return visitor(static_cast<T*>(ptr));
123 template <size_t index, typename V, typename R, typename T, typename... Types>
124 struct ApplyVisitor1<index, V, R, T, Types...> {
125 R operator()(size_t runtimeIndex, V&& visitor, void* ptr) const {
126 return runtimeIndex == 1
127 ? visitor(static_cast<T*>(ptr))
128 : ApplyVisitor1<index - 1, V, R, Types...>()(
129 runtimeIndex - 1, std::forward<V>(visitor), ptr);
133 template <size_t index, typename V, typename R, typename... Types>
134 struct ApplyConstVisitor1;
136 template <typename V, typename R, typename T, typename... Types>
137 struct ApplyConstVisitor1<1, V, R, T, Types...> {
138 R operator()(size_t, V&& visitor, void* ptr) const {
139 return visitor(static_cast<const T*>(ptr));
143 template <size_t index, typename V, typename R, typename T, typename... Types>
144 struct ApplyConstVisitor1<index, V, R, T, Types...> {
145 R operator()(size_t runtimeIndex, V&& visitor, void* ptr) const {
146 return runtimeIndex == 1
147 ? visitor(static_cast<const T*>(ptr))
148 : ApplyConstVisitor1<index - 1, V, R, Types...>()(
149 runtimeIndex - 1, std::forward<V>(visitor), ptr);
153 template <typename V, typename... Types>
154 using ApplyVisitor = ApplyVisitor1<
157 typename VisitorResult<V, Types...>::type,
160 template <typename V, typename... Types>
161 using ApplyConstVisitor = ApplyConstVisitor1<
164 typename ConstVisitorResult<V, Types...>::type,
167 } // namespace dptr_detail