2 * Copyright 2015 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.
17 #ifndef FOLLY_DETAIL_DISCRIMINATEDPTRDETAIL_H_
18 #define FOLLY_DETAIL_DISCRIMINATEDPTRDETAIL_H_
21 #include <type_traits>
24 namespace dptr_detail {
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
31 * GetIndex<int, void, char, int>::value == 3
32 * GetIndex<int, void, char>::value -> fails to compile
34 template <typename... Types> struct GetTypeIndex;
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;
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;
50 // Generalize std::is_same for variable number of type arguments
51 template <typename... Types>
56 static const bool value = true;
60 struct IsSameType<T> {
61 static const bool value = true;
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;
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>
75 template <typename T, typename... Types>
76 struct SameType<T, Types...> {
78 static_assert(IsSameType<T, Types...>::value,
79 "Not all types in pack are the same");
82 // Determine the result type of applying a visitor of type V on a pointer
84 template <typename V, typename T>
85 struct VisitorResult1 {
86 typedef typename std::result_of<V (T*)>::type type;
89 // Determine the result type of applying a visitor of type V on a const pointer
91 template <typename V, typename T>
92 struct ConstVisitorResult1 {
93 typedef typename std::result_of<V (const T*)>::type type;
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
99 template <typename V, typename... Types>
100 struct VisitorResult {
101 typedef typename SameType<
102 typename VisitorResult1<V,Types>::type...>::type type;
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
108 template <typename V, typename... Types>
109 struct ConstVisitorResult {
110 typedef typename SameType<
111 typename ConstVisitorResult1<V,Types>::type...>::type type;
114 template <typename V, typename R, typename... Types> struct ApplyVisitor1;
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
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));
132 template <typename V, typename R, typename... Types> struct ApplyConstVisitor1;
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
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));
150 template <typename V, typename... Types>
153 V, typename VisitorResult<V, Types...>::type, Types...> {
156 template <typename V, typename... Types>
157 struct ApplyConstVisitor
158 : ApplyConstVisitor1<
159 V, typename ConstVisitorResult<V, Types...>::type, Types...> {
162 } // namespace dptr_detail
165 #endif /* FOLLY_DETAIL_DISCRIMINATEDPTRDETAIL_H_ */