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 |
Nico
2016/06/02 16:56:18
This needs updating.
// Use std::tuple as tuple t
tzik
2016/06/03 04:44:19
Done.
| |
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 // |
11 // Tuple elements are copied by value, and stored in the tuple. See the unit | 11 // Tuple elements are copied by value, and stored in the tuple. See the unit |
12 // tests for more details of how/when the values are copied. | 12 // tests for more details of how/when the values are copied. |
13 // | 13 // |
14 // Example usage: | 14 // Example usage: |
15 // // These two methods of creating a Tuple are identical. | 15 // // These two methods of creating a Tuple are identical. |
16 // Tuple<int, const char*> tuple_a(1, "wee"); | 16 // Tuple<int, const char*> tuple_a(1, "wee"); |
Nico
2016/06/02 16:56:18
std::tuple
tzik
2016/06/03 04:44:19
Done.
| |
17 // Tuple<int, const char*> tuple_b = MakeTuple(1, "wee"); | 17 // Tuple<int, const char*> tuple_b = MakeTuple(1, "wee"); |
18 // | 18 // |
19 // void SomeFunc(int a, const char* b) { } | 19 // void SomeFunc(int a, const char* b) { } |
20 // DispatchToFunction(&SomeFunc, tuple_a); // SomeFunc(1, "wee") | 20 // DispatchToFunction(&SomeFunc, tuple_a); // SomeFunc(1, "wee") |
21 // DispatchToFunction( | 21 // DispatchToFunction( |
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); |
(...skipping 76 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(OS_WIN) && defined(_PREFAST_) | 108 #endif // defined(OS_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 // Tuple ----------------------------------------------------------------------- | |
114 // | |
115 // This set of classes is useful for bundling 0 or more heterogeneous data types | |
116 // into a single variable. The advantage of this is that it greatly simplifies | |
117 // function objects that need to take an arbitrary number of parameters; see | |
118 // RunnableMethod and IPC::MessageWithTuple. | |
119 // | |
120 // Tuple<> is supplied to act as a 'void' type. It can be used, for example, | |
121 // when dispatching to a function that accepts no arguments (see the | |
122 // Dispatchers below). | |
123 // Tuple<A> is rarely useful. One such use is when A is non-const ref that you | |
124 // want filled by the dispatchee, and the tuple is merely a container for that | |
125 // output (a "tier"). See MakeRefTuple and its usages. | |
126 | |
127 template <typename... Ts> | |
128 using Tuple = std::tuple<Ts...>; | |
129 | |
130 using std::get; | |
131 | |
132 // Tuple creators ------------------------------------------------------------- | |
133 // | |
134 // Helper functions for constructing tuples while inferring the template | |
135 // argument types. | |
136 | |
137 template <typename... Ts> | |
138 inline Tuple<Ts...> MakeTuple(const Ts&... arg) { | |
139 return Tuple<Ts...>(arg...); | |
140 } | |
141 | |
142 // The following set of helpers make what Boost refers to as "Tiers" - a tuple | |
143 // of references. | |
144 | |
145 template <typename... Ts> | |
146 inline Tuple<Ts&...> MakeRefTuple(Ts&... arg) { | |
147 return Tuple<Ts&...>(arg...); | |
148 } | |
149 | |
150 // Dispatchers ---------------------------------------------------------------- | 113 // Dispatchers ---------------------------------------------------------------- |
151 // | 114 // |
152 // Helper functions that call the given method on an object, with the unpacked | 115 // Helper functions that call the given method on an object, with the unpacked |
153 // tuple arguments. Notice that they all have the same number of arguments, | 116 // tuple arguments. Notice that they all have the same number of arguments, |
154 // so you need only write: | 117 // so you need only write: |
155 // DispatchToMethod(object, &Object::method, args); | 118 // DispatchToMethod(object, &Object::method, args); |
156 // This is very useful for templated dispatchers, since they don't need to know | 119 // This is very useful for templated dispatchers, since they don't need to know |
157 // what type |args| is. | 120 // what type |args| is. |
158 | 121 |
159 // Non-Static Dispatchers with no out params. | 122 // Non-Static Dispatchers with no out params. |
160 | 123 |
161 template <typename ObjT, typename Method, typename... Ts, size_t... Ns> | 124 template <typename ObjT, typename Method, typename... Ts, size_t... Ns> |
162 inline void DispatchToMethodImpl(const ObjT& obj, | 125 inline void DispatchToMethodImpl(const ObjT& obj, |
163 Method method, | 126 Method method, |
164 const Tuple<Ts...>& arg, | 127 const std::tuple<Ts...>& arg, |
165 IndexSequence<Ns...>) { | 128 IndexSequence<Ns...>) { |
166 (obj->*method)(internal::Unwrap(get<Ns>(arg))...); | 129 (obj->*method)(internal::Unwrap(std::get<Ns>(arg))...); |
167 } | 130 } |
168 | 131 |
169 template <typename ObjT, typename Method, typename... Ts> | 132 template <typename ObjT, typename Method, typename... Ts> |
170 inline void DispatchToMethod(const ObjT& obj, | 133 inline void DispatchToMethod(const ObjT& obj, |
171 Method method, | 134 Method method, |
172 const Tuple<Ts...>& arg) { | 135 const std::tuple<Ts...>& arg) { |
173 DispatchToMethodImpl(obj, method, arg, MakeIndexSequence<sizeof...(Ts)>()); | 136 DispatchToMethodImpl(obj, method, arg, MakeIndexSequence<sizeof...(Ts)>()); |
174 } | 137 } |
175 | 138 |
176 // Static Dispatchers with no out params. | 139 // Static Dispatchers with no out params. |
177 | 140 |
178 template <typename Function, typename... Ts, size_t... Ns> | 141 template <typename Function, typename... Ts, size_t... Ns> |
179 inline void DispatchToFunctionImpl(Function function, | 142 inline void DispatchToFunctionImpl(Function function, |
180 const Tuple<Ts...>& arg, | 143 const std::tuple<Ts...>& arg, |
181 IndexSequence<Ns...>) { | 144 IndexSequence<Ns...>) { |
182 (*function)(internal::Unwrap(get<Ns>(arg))...); | 145 (*function)(internal::Unwrap(std::get<Ns>(arg))...); |
183 } | 146 } |
184 | 147 |
185 template <typename Function, typename... Ts> | 148 template <typename Function, typename... Ts> |
186 inline void DispatchToFunction(Function function, const Tuple<Ts...>& arg) { | 149 inline void DispatchToFunction(Function function, |
150 const std::tuple<Ts...>& arg) { | |
187 DispatchToFunctionImpl(function, arg, MakeIndexSequence<sizeof...(Ts)>()); | 151 DispatchToFunctionImpl(function, arg, MakeIndexSequence<sizeof...(Ts)>()); |
188 } | 152 } |
189 | 153 |
190 // Dispatchers with out parameters. | 154 // Dispatchers with out parameters. |
191 | 155 |
192 template <typename ObjT, | 156 template <typename ObjT, |
193 typename Method, | 157 typename Method, |
194 typename... InTs, | 158 typename... InTs, |
195 typename... OutTs, | 159 typename... OutTs, |
196 size_t... InNs, | 160 size_t... InNs, |
197 size_t... OutNs> | 161 size_t... OutNs> |
198 inline void DispatchToMethodImpl(const ObjT& obj, | 162 inline void DispatchToMethodImpl(const ObjT& obj, |
199 Method method, | 163 Method method, |
200 const Tuple<InTs...>& in, | 164 const std::tuple<InTs...>& in, |
201 Tuple<OutTs...>* out, | 165 std::tuple<OutTs...>* out, |
202 IndexSequence<InNs...>, | 166 IndexSequence<InNs...>, |
203 IndexSequence<OutNs...>) { | 167 IndexSequence<OutNs...>) { |
204 (obj->*method)(internal::Unwrap(get<InNs>(in))..., &get<OutNs>(*out)...); | 168 (obj->*method)(internal::Unwrap(std::get<InNs>(in))..., |
169 &std::get<OutNs>(*out)...); | |
205 } | 170 } |
206 | 171 |
207 template <typename ObjT, typename Method, typename... InTs, typename... OutTs> | 172 template <typename ObjT, typename Method, typename... InTs, typename... OutTs> |
208 inline void DispatchToMethod(const ObjT& obj, | 173 inline void DispatchToMethod(const ObjT& obj, |
209 Method method, | 174 Method method, |
210 const Tuple<InTs...>& in, | 175 const std::tuple<InTs...>& in, |
211 Tuple<OutTs...>* out) { | 176 std::tuple<OutTs...>* out) { |
212 DispatchToMethodImpl(obj, method, in, out, | 177 DispatchToMethodImpl(obj, method, in, out, |
213 MakeIndexSequence<sizeof...(InTs)>(), | 178 MakeIndexSequence<sizeof...(InTs)>(), |
214 MakeIndexSequence<sizeof...(OutTs)>()); | 179 MakeIndexSequence<sizeof...(OutTs)>()); |
215 } | 180 } |
216 | 181 |
217 } // namespace base | 182 } // namespace base |
218 | 183 |
219 #endif // BASE_TUPLE_H_ | 184 #endif // BASE_TUPLE_H_ |
OLD | NEW |