Consistency in namespace-closing comments
[folly.git] / folly / detail / DiscriminatedPtrDetail.h
1 /*
2  * Copyright 2017 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 #pragma once
18
19 #include <type_traits>
20 #include <utility>
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 <size_t index, typename V, typename R, typename... Types>
114 struct ApplyVisitor1;
115
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));
120   }
121 };
122
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);
130   }
131 };
132
133 template <size_t index, typename V, typename R, typename... Types>
134 struct ApplyConstVisitor1;
135
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));
140   }
141 };
142
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);
150   }
151 };
152
153 template <typename V, typename... Types>
154 using ApplyVisitor = ApplyVisitor1<
155     sizeof...(Types),
156     V,
157     typename VisitorResult<V, Types...>::type,
158     Types...>;
159
160 template <typename V, typename... Types>
161 using ApplyConstVisitor = ApplyConstVisitor1<
162     sizeof...(Types),
163     V,
164     typename ConstVisitorResult<V, Types...>::type,
165     Types...>;
166
167 } // namespace dptr_detail
168 } // namespace folly