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 11 matching lines...) Expand all Loading... |
22 // &SomeFunc, MakeTuple(10, "foo")); // SomeFunc(10, "foo") | 22 // &SomeFunc, MakeTuple(10, "foo")); // SomeFunc(10, "foo") |
23 // | 23 // |
24 // struct { void SomeMeth(int a, int b, int c) { } } foo; | 24 // struct { void SomeMeth(int a, int b, int c) { } } foo; |
25 // DispatchToMethod(&foo, &Foo::SomeMeth, MakeTuple(1, 2, 3)); | 25 // DispatchToMethod(&foo, &Foo::SomeMeth, MakeTuple(1, 2, 3)); |
26 // // foo->SomeMeth(1, 2, 3); | 26 // // foo->SomeMeth(1, 2, 3); |
27 | 27 |
28 #ifndef BASE_TUPLE_H_ | 28 #ifndef BASE_TUPLE_H_ |
29 #define BASE_TUPLE_H_ | 29 #define BASE_TUPLE_H_ |
30 | 30 |
31 #include <stddef.h> | 31 #include <stddef.h> |
| 32 #include <tuple> |
32 | 33 |
33 #include "base/bind_helpers.h" | 34 #include "base/bind_helpers.h" |
34 #include "build/build_config.h" | 35 #include "build/build_config.h" |
35 | 36 |
36 namespace base { | 37 namespace base { |
37 | 38 |
38 // Index sequences | 39 // Index sequences |
39 // | 40 // |
40 // Minimal clone of the similarly-named C++14 functionality. | 41 // Minimal clone of the similarly-named C++14 functionality. |
41 | 42 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 // function objects that need to take an arbitrary number of parameters; see | 139 // function objects that need to take an arbitrary number of parameters; see |
139 // RunnableMethod and IPC::MessageWithTuple. | 140 // RunnableMethod and IPC::MessageWithTuple. |
140 // | 141 // |
141 // Tuple<> is supplied to act as a 'void' type. It can be used, for example, | 142 // Tuple<> is supplied to act as a 'void' type. It can be used, for example, |
142 // when dispatching to a function that accepts no arguments (see the | 143 // when dispatching to a function that accepts no arguments (see the |
143 // Dispatchers below). | 144 // Dispatchers below). |
144 // Tuple<A> is rarely useful. One such use is when A is non-const ref that you | 145 // Tuple<A> is rarely useful. One such use is when A is non-const ref that you |
145 // want filled by the dispatchee, and the tuple is merely a container for that | 146 // want filled by the dispatchee, and the tuple is merely a container for that |
146 // output (a "tier"). See MakeRefTuple and its usages. | 147 // output (a "tier"). See MakeRefTuple and its usages. |
147 | 148 |
148 template <typename IxSeq, typename... Ts> | |
149 struct TupleBaseImpl; | |
150 template <typename... Ts> | 149 template <typename... Ts> |
151 using TupleBase = TupleBaseImpl<MakeIndexSequence<sizeof...(Ts)>, Ts...>; | 150 using Tuple = std::tuple<Ts...>; |
152 template <size_t N, typename T> | |
153 struct TupleLeaf; | |
154 | 151 |
155 template <typename... Ts> | 152 using std::get; |
156 struct Tuple final : TupleBase<Ts...> { | |
157 Tuple() : TupleBase<Ts...>() {} | |
158 explicit Tuple(typename TupleTraits<Ts>::ParamType... args) | |
159 : TupleBase<Ts...>(args...) {} | |
160 }; | |
161 | |
162 // Avoids ambiguity between Tuple's two constructors. | |
163 template <> | |
164 struct Tuple<> final {}; | |
165 | |
166 template <size_t... Ns, typename... Ts> | |
167 struct TupleBaseImpl<IndexSequence<Ns...>, Ts...> : TupleLeaf<Ns, Ts>... { | |
168 TupleBaseImpl() : TupleLeaf<Ns, Ts>()... {} | |
169 explicit TupleBaseImpl(typename TupleTraits<Ts>::ParamType... args) | |
170 : TupleLeaf<Ns, Ts>(args)... {} | |
171 }; | |
172 | |
173 template <size_t N, typename T> | |
174 struct TupleLeaf { | |
175 TupleLeaf() {} | |
176 explicit TupleLeaf(typename TupleTraits<T>::ParamType x) : x(x) {} | |
177 | |
178 T& get() { return x; } | |
179 const T& get() const { return x; } | |
180 | |
181 T x; | |
182 }; | |
183 | |
184 // Tuple getters -------------------------------------------------------------- | |
185 // | |
186 // Allows accessing an arbitrary tuple element by index. | |
187 // | |
188 // Example usage: | |
189 // base::Tuple<int, double> t2; | |
190 // base::get<0>(t2) = 42; | |
191 // base::get<1>(t2) = 3.14; | |
192 | |
193 template <size_t I, typename T> | |
194 T& get(TupleLeaf<I, T>& leaf) { | |
195 return leaf.get(); | |
196 } | |
197 | |
198 template <size_t I, typename T> | |
199 const T& get(const TupleLeaf<I, T>& leaf) { | |
200 return leaf.get(); | |
201 } | |
202 | 153 |
203 // Tuple types ---------------------------------------------------------------- | 154 // Tuple types ---------------------------------------------------------------- |
204 // | 155 // |
205 // Allows for selection of ValueTuple/RefTuple/ParamTuple without needing the | 156 // Allows for selection of ValueTuple/RefTuple/ParamTuple without needing the |
206 // definitions of class types the tuple takes as parameters. | 157 // definitions of class types the tuple takes as parameters. |
207 | 158 |
208 template <typename T> | 159 template <typename T> |
209 struct TupleTypes; | 160 struct TupleTypes; |
210 | 161 |
211 template <typename... Ts> | 162 template <typename... Ts> |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 const Tuple<InTs...>& in, | 248 const Tuple<InTs...>& in, |
298 Tuple<OutTs...>* out) { | 249 Tuple<OutTs...>* out) { |
299 DispatchToMethodImpl(obj, method, in, out, | 250 DispatchToMethodImpl(obj, method, in, out, |
300 MakeIndexSequence<sizeof...(InTs)>(), | 251 MakeIndexSequence<sizeof...(InTs)>(), |
301 MakeIndexSequence<sizeof...(OutTs)>()); | 252 MakeIndexSequence<sizeof...(OutTs)>()); |
302 } | 253 } |
303 | 254 |
304 } // namespace base | 255 } // namespace base |
305 | 256 |
306 #endif // BASE_TUPLE_H_ | 257 #endif // BASE_TUPLE_H_ |
OLD | NEW |