OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // A Tuple is a generic templatized container, similar in concept to std::pair | 5 // A Tuple is a generic templatized container, similar in concept to std::pair |
6 // and std::tuple. The convenient MakeTuple() function takes any number of | 6 // and std::tuple. The convenient MakeTuple() function takes any number of |
7 // arguments and will construct and return the appropriate Tuple object. The | 7 // arguments and will construct and return the appropriate Tuple object. The |
8 // functions DispatchToMethod and DispatchToFunction take a function pointer or | 8 // functions DispatchToMethod and DispatchToFunction take a function pointer or |
9 // instance and method pointer, and unpack a tuple into arguments to the call. | 9 // instance and method pointer, and unpack a tuple into arguments to the call. |
10 // | 10 // |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 | 103 |
104 template <size_t N, size_t... Ns> | 104 template <size_t N, size_t... Ns> |
105 struct MakeIndexSequenceImpl<N, Ns...> | 105 struct MakeIndexSequenceImpl<N, Ns...> |
106 : MakeIndexSequenceImpl<N - 1, N - 1, Ns...> {}; | 106 : MakeIndexSequenceImpl<N - 1, N - 1, Ns...> {}; |
107 | 107 |
108 #endif // defined(WIN) && defined(_PREFAST_) | 108 #endif // defined(WIN) && defined(_PREFAST_) |
109 | 109 |
110 template <size_t N> | 110 template <size_t N> |
111 using MakeIndexSequence = typename MakeIndexSequenceImpl<N>::Type; | 111 using MakeIndexSequence = typename MakeIndexSequenceImpl<N>::Type; |
112 | 112 |
113 // Traits ---------------------------------------------------------------------- | |
114 // | |
115 // A simple traits class for tuple arguments. | |
116 // | |
117 // ValueType: the bare, nonref version of a type (same as the type for nonrefs). | |
118 // RefType: the ref version of a type (same as the type for refs). | |
119 // ParamType: what type to pass to functions (refs should not be constified). | |
120 | |
121 template <class P> | |
122 struct TupleTraits { | |
123 typedef P ValueType; | |
124 typedef P& RefType; | |
125 typedef const P& ParamType; | |
126 }; | |
127 | |
128 template <class P> | |
129 struct TupleTraits<P&> { | |
130 typedef P ValueType; | |
131 typedef P& RefType; | |
132 typedef P& ParamType; | |
133 }; | |
134 | |
135 // Tuple ----------------------------------------------------------------------- | 113 // Tuple ----------------------------------------------------------------------- |
136 // | 114 // |
137 // This set of classes is useful for bundling 0 or more heterogeneous data types | 115 // This set of classes is useful for bundling 0 or more heterogeneous data types |
138 // into a single variable. The advantage of this is that it greatly simplifies | 116 // into a single variable. The advantage of this is that it greatly simplifies |
139 // function objects that need to take an arbitrary number of parameters; see | 117 // function objects that need to take an arbitrary number of parameters; see |
140 // RunnableMethod and IPC::MessageWithTuple. | 118 // RunnableMethod and IPC::MessageWithTuple. |
141 // | 119 // |
142 // Tuple<> is supplied to act as a 'void' type. It can be used, for example, | 120 // Tuple<> is supplied to act as a 'void' type. It can be used, for example, |
143 // when dispatching to a function that accepts no arguments (see the | 121 // when dispatching to a function that accepts no arguments (see the |
144 // Dispatchers below). | 122 // Dispatchers below). |
145 // Tuple<A> is rarely useful. One such use is when A is non-const ref that you | 123 // Tuple<A> is rarely useful. One such use is when A is non-const ref that you |
146 // want filled by the dispatchee, and the tuple is merely a container for that | 124 // want filled by the dispatchee, and the tuple is merely a container for that |
147 // output (a "tier"). See MakeRefTuple and its usages. | 125 // output (a "tier"). See MakeRefTuple and its usages. |
148 | 126 |
149 template <typename... Ts> | 127 template <typename... Ts> |
150 using Tuple = std::tuple<Ts...>; | 128 using Tuple = std::tuple<Ts...>; |
151 | 129 |
152 using std::get; | 130 using std::get; |
153 | 131 |
154 // Tuple types ---------------------------------------------------------------- | |
155 // | |
156 // Allows for selection of ValueTuple/RefTuple/ParamTuple without needing the | |
157 // definitions of class types the tuple takes as parameters. | |
158 | |
159 template <typename T> | |
160 struct TupleTypes; | |
161 | |
162 template <typename... Ts> | |
163 struct TupleTypes<Tuple<Ts...>> { | |
164 using ValueTuple = Tuple<typename TupleTraits<Ts>::ValueType...>; | |
165 using RefTuple = Tuple<typename TupleTraits<Ts>::RefType...>; | |
166 using ParamTuple = Tuple<typename TupleTraits<Ts>::ParamType...>; | |
167 }; | |
168 | |
169 // Tuple creators ------------------------------------------------------------- | 132 // Tuple creators ------------------------------------------------------------- |
170 // | 133 // |
171 // Helper functions for constructing tuples while inferring the template | 134 // Helper functions for constructing tuples while inferring the template |
172 // argument types. | 135 // argument types. |
173 | 136 |
174 template <typename... Ts> | 137 template <typename... Ts> |
175 inline Tuple<Ts...> MakeTuple(const Ts&... arg) { | 138 inline Tuple<Ts...> MakeTuple(const Ts&... arg) { |
176 return Tuple<Ts...>(arg...); | 139 return Tuple<Ts...>(arg...); |
177 } | 140 } |
178 | 141 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 const Tuple<InTs...>& in, | 210 const Tuple<InTs...>& in, |
248 Tuple<OutTs...>* out) { | 211 Tuple<OutTs...>* out) { |
249 DispatchToMethodImpl(obj, method, in, out, | 212 DispatchToMethodImpl(obj, method, in, out, |
250 MakeIndexSequence<sizeof...(InTs)>(), | 213 MakeIndexSequence<sizeof...(InTs)>(), |
251 MakeIndexSequence<sizeof...(OutTs)>()); | 214 MakeIndexSequence<sizeof...(OutTs)>()); |
252 } | 215 } |
253 | 216 |
254 } // namespace base | 217 } // namespace base |
255 | 218 |
256 #endif // BASE_TUPLE_H_ | 219 #endif // BASE_TUPLE_H_ |
OLD | NEW |