Chromium Code Reviews| 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 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 189 // Helper functions that call the given method on an object, with the unpacked | 189 // Helper functions that call the given method on an object, with the unpacked |
| 190 // tuple arguments. Notice that they all have the same number of arguments, | 190 // tuple arguments. Notice that they all have the same number of arguments, |
| 191 // so you need only write: | 191 // so you need only write: |
| 192 // DispatchToMethod(object, &Object::method, args); | 192 // DispatchToMethod(object, &Object::method, args); |
| 193 // This is very useful for templated dispatchers, since they don't need to know | 193 // This is very useful for templated dispatchers, since they don't need to know |
| 194 // what type |args| is. | 194 // what type |args| is. |
| 195 | 195 |
| 196 // Non-Static Dispatchers with no out params. | 196 // Non-Static Dispatchers with no out params. |
| 197 | 197 |
| 198 template <typename ObjT, typename Method, typename... Ts, size_t... Ns> | 198 template <typename ObjT, typename Method, typename... Ts, size_t... Ns> |
| 199 inline void DispatchToMethodImpl(ObjT* obj, | 199 inline void DispatchToMethodImpl(const ObjT& obj, |
| 200 Method method, | 200 Method method, |
| 201 const Tuple<Ts...>& arg, | 201 const Tuple<Ts...>& arg, |
| 202 IndexSequence<Ns...>) { | 202 IndexSequence<Ns...>) { |
| 203 (obj->*method)(base::internal::UnwrapTraits<Ts>::Unwrap(get<Ns>(arg))...); | 203 (obj->*method)(base::internal::UnwrapTraits<Ts>::Unwrap(get<Ns>(arg))...); |
|
Nico
2016/02/15 15:22:34
Why doesn't this have to be (obj.*method) now?
tzik
2016/02/15 20:46:26
In new code, obj will be a const ref to a T* or a
| |
| 204 } | 204 } |
| 205 | 205 |
| 206 template <typename ObjT, typename Method, typename... Ts> | 206 template <typename ObjT, typename Method, typename... Ts> |
| 207 inline void DispatchToMethod(ObjT* obj, | 207 inline void DispatchToMethod(const ObjT& obj, |
| 208 Method method, | 208 Method method, |
| 209 const Tuple<Ts...>& arg) { | 209 const Tuple<Ts...>& arg) { |
| 210 DispatchToMethodImpl(obj, method, arg, MakeIndexSequence<sizeof...(Ts)>()); | 210 DispatchToMethodImpl(obj, method, arg, MakeIndexSequence<sizeof...(Ts)>()); |
| 211 } | 211 } |
| 212 | 212 |
| 213 // Static Dispatchers with no out params. | 213 // Static Dispatchers with no out params. |
| 214 | 214 |
| 215 template <typename Function, typename... Ts, size_t... Ns> | 215 template <typename Function, typename... Ts, size_t... Ns> |
| 216 inline void DispatchToFunctionImpl(Function function, | 216 inline void DispatchToFunctionImpl(Function function, |
| 217 const Tuple<Ts...>& arg, | 217 const Tuple<Ts...>& arg, |
| 218 IndexSequence<Ns...>) { | 218 IndexSequence<Ns...>) { |
| 219 (*function)(base::internal::UnwrapTraits<Ts>::Unwrap(get<Ns>(arg))...); | 219 (*function)(base::internal::UnwrapTraits<Ts>::Unwrap(get<Ns>(arg))...); |
| 220 } | 220 } |
| 221 | 221 |
| 222 template <typename Function, typename... Ts> | 222 template <typename Function, typename... Ts> |
| 223 inline void DispatchToFunction(Function function, const Tuple<Ts...>& arg) { | 223 inline void DispatchToFunction(Function function, const Tuple<Ts...>& arg) { |
| 224 DispatchToFunctionImpl(function, arg, MakeIndexSequence<sizeof...(Ts)>()); | 224 DispatchToFunctionImpl(function, arg, MakeIndexSequence<sizeof...(Ts)>()); |
| 225 } | 225 } |
| 226 | 226 |
| 227 // Dispatchers with out parameters. | 227 // Dispatchers with out parameters. |
| 228 | 228 |
| 229 template <typename ObjT, | 229 template <typename ObjT, |
| 230 typename Method, | 230 typename Method, |
| 231 typename... InTs, | 231 typename... InTs, |
| 232 typename... OutTs, | 232 typename... OutTs, |
| 233 size_t... InNs, | 233 size_t... InNs, |
| 234 size_t... OutNs> | 234 size_t... OutNs> |
| 235 inline void DispatchToMethodImpl(ObjT* obj, | 235 inline void DispatchToMethodImpl(const ObjT& obj, |
| 236 Method method, | 236 Method method, |
| 237 const Tuple<InTs...>& in, | 237 const Tuple<InTs...>& in, |
| 238 Tuple<OutTs...>* out, | 238 Tuple<OutTs...>* out, |
| 239 IndexSequence<InNs...>, | 239 IndexSequence<InNs...>, |
| 240 IndexSequence<OutNs...>) { | 240 IndexSequence<OutNs...>) { |
| 241 (obj->*method)(base::internal::UnwrapTraits<InTs>::Unwrap(get<InNs>(in))..., | 241 (obj->*method)(base::internal::UnwrapTraits<InTs>::Unwrap(get<InNs>(in))..., |
|
Nico
2016/02/15 15:22:34
same question
| |
| 242 &get<OutNs>(*out)...); | 242 &get<OutNs>(*out)...); |
| 243 } | 243 } |
| 244 | 244 |
| 245 template <typename ObjT, typename Method, typename... InTs, typename... OutTs> | 245 template <typename ObjT, typename Method, typename... InTs, typename... OutTs> |
| 246 inline void DispatchToMethod(ObjT* obj, | 246 inline void DispatchToMethod(const ObjT& obj, |
| 247 Method method, | 247 Method method, |
| 248 const Tuple<InTs...>& in, | 248 const Tuple<InTs...>& in, |
| 249 Tuple<OutTs...>* out) { | 249 Tuple<OutTs...>* out) { |
| 250 DispatchToMethodImpl(obj, method, in, out, | 250 DispatchToMethodImpl(obj, method, in, out, |
| 251 MakeIndexSequence<sizeof...(InTs)>(), | 251 MakeIndexSequence<sizeof...(InTs)>(), |
| 252 MakeIndexSequence<sizeof...(OutTs)>()); | 252 MakeIndexSequence<sizeof...(OutTs)>()); |
| 253 } | 253 } |
| 254 | 254 |
| 255 } // namespace base | 255 } // namespace base |
| 256 | 256 |
| 257 #endif // BASE_TUPLE_H_ | 257 #endif // BASE_TUPLE_H_ |
| OLD | NEW |