2 * Copyright 2016 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.
20 #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 <typename V, typename R, typename... Types> struct ApplyVisitor1;
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
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));
131 template <typename V, typename R, typename... Types> struct ApplyConstVisitor1;
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
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));
149 template <typename V, typename... Types>
152 V, typename VisitorResult<V, Types...>::type, Types...> {
155 template <typename V, typename... Types>
156 struct ApplyConstVisitor
157 : ApplyConstVisitor1<
158 V, typename ConstVisitorResult<V, Types...>::type, Types...> {
161 } // namespace dptr_detail