| 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 // Use std::tuple as tuple type. This file contains helper functions for |
| 6 // and std::tuple. The convenient MakeTuple() function takes any number of | 6 // working with std::tuples. |
| 7 // arguments and will construct and return the appropriate Tuple object. The | 7 // The functions DispatchToMethod and DispatchToFunction take a function pointer |
| 8 // functions DispatchToMethod and DispatchToFunction take a function pointer or | 8 // or instance and method pointer, and unpack a tuple into arguments to the |
| 9 // instance and method pointer, and unpack a tuple into arguments to the call. | 9 // call. |
| 10 // | |
| 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. | |
| 13 // | 10 // |
| 14 // Example usage: | 11 // Example usage: |
| 15 // // These two methods of creating a Tuple are identical. | 12 // // These two methods of creating a Tuple are identical. |
| 16 // Tuple<int, const char*> tuple_a(1, "wee"); | 13 // std::tuple<int, const char*> tuple_a(1, "wee"); |
| 17 // Tuple<int, const char*> tuple_b = MakeTuple(1, "wee"); | 14 // std::tuple<int, const char*> tuple_b = std::make_tuple(1, "wee"); |
| 18 // | 15 // |
| 19 // void SomeFunc(int a, const char* b) { } | 16 // void SomeFunc(int a, const char* b) { } |
| 20 // DispatchToFunction(&SomeFunc, tuple_a); // SomeFunc(1, "wee") | 17 // DispatchToFunction(&SomeFunc, tuple_a); // SomeFunc(1, "wee") |
| 21 // DispatchToFunction( | 18 // DispatchToFunction( |
| 22 // &SomeFunc, MakeTuple(10, "foo")); // SomeFunc(10, "foo") | 19 // &SomeFunc, std::make_tuple(10, "foo")); // SomeFunc(10, "foo") |
| 23 // | 20 // |
| 24 // struct { void SomeMeth(int a, int b, int c) { } } foo; | 21 // struct { void SomeMeth(int a, int b, int c) { } } foo; |
| 25 // DispatchToMethod(&foo, &Foo::SomeMeth, MakeTuple(1, 2, 3)); | 22 // DispatchToMethod(&foo, &Foo::SomeMeth, std::make_tuple(1, 2, 3)); |
| 26 // // foo->SomeMeth(1, 2, 3); | 23 // // foo->SomeMeth(1, 2, 3); |
| 27 | 24 |
| 28 #ifndef BASE_TUPLE_H_ | 25 #ifndef BASE_TUPLE_H_ |
| 29 #define BASE_TUPLE_H_ | 26 #define BASE_TUPLE_H_ |
| 30 | 27 |
| 31 #include <stddef.h> | 28 #include <stddef.h> |
| 32 #include <tuple> | 29 #include <tuple> |
| 33 | 30 |
| 34 #include "base/bind_helpers.h" | 31 #include "base/bind_helpers.h" |
| 35 #include "build/build_config.h" | 32 #include "build/build_config.h" |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 | 100 |
| 104 template <size_t N, size_t... Ns> | 101 template <size_t N, size_t... Ns> |
| 105 struct MakeIndexSequenceImpl<N, Ns...> | 102 struct MakeIndexSequenceImpl<N, Ns...> |
| 106 : MakeIndexSequenceImpl<N - 1, N - 1, Ns...> {}; | 103 : MakeIndexSequenceImpl<N - 1, N - 1, Ns...> {}; |
| 107 | 104 |
| 108 #endif // defined(OS_WIN) && defined(_PREFAST_) | 105 #endif // defined(OS_WIN) && defined(_PREFAST_) |
| 109 | 106 |
| 110 template <size_t N> | 107 template <size_t N> |
| 111 using MakeIndexSequence = typename MakeIndexSequenceImpl<N>::Type; | 108 using MakeIndexSequence = typename MakeIndexSequenceImpl<N>::Type; |
| 112 | 109 |
| 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 ---------------------------------------------------------------- | 110 // Dispatchers ---------------------------------------------------------------- |
| 151 // | 111 // |
| 152 // Helper functions that call the given method on an object, with the unpacked | 112 // 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, | 113 // tuple arguments. Notice that they all have the same number of arguments, |
| 154 // so you need only write: | 114 // so you need only write: |
| 155 // DispatchToMethod(object, &Object::method, args); | 115 // DispatchToMethod(object, &Object::method, args); |
| 156 // This is very useful for templated dispatchers, since they don't need to know | 116 // This is very useful for templated dispatchers, since they don't need to know |
| 157 // what type |args| is. | 117 // what type |args| is. |
| 158 | 118 |
| 159 // Non-Static Dispatchers with no out params. | 119 // Non-Static Dispatchers with no out params. |
| 160 | 120 |
| 161 template <typename ObjT, typename Method, typename... Ts, size_t... Ns> | 121 template <typename ObjT, typename Method, typename... Ts, size_t... Ns> |
| 162 inline void DispatchToMethodImpl(const ObjT& obj, | 122 inline void DispatchToMethodImpl(const ObjT& obj, |
| 163 Method method, | 123 Method method, |
| 164 const Tuple<Ts...>& arg, | 124 const std::tuple<Ts...>& arg, |
| 165 IndexSequence<Ns...>) { | 125 IndexSequence<Ns...>) { |
| 166 (obj->*method)(internal::Unwrap(get<Ns>(arg))...); | 126 (obj->*method)(internal::Unwrap(std::get<Ns>(arg))...); |
| 167 } | 127 } |
| 168 | 128 |
| 169 template <typename ObjT, typename Method, typename... Ts> | 129 template <typename ObjT, typename Method, typename... Ts> |
| 170 inline void DispatchToMethod(const ObjT& obj, | 130 inline void DispatchToMethod(const ObjT& obj, |
| 171 Method method, | 131 Method method, |
| 172 const Tuple<Ts...>& arg) { | 132 const std::tuple<Ts...>& arg) { |
| 173 DispatchToMethodImpl(obj, method, arg, MakeIndexSequence<sizeof...(Ts)>()); | 133 DispatchToMethodImpl(obj, method, arg, MakeIndexSequence<sizeof...(Ts)>()); |
| 174 } | 134 } |
| 175 | 135 |
| 176 // Static Dispatchers with no out params. | 136 // Static Dispatchers with no out params. |
| 177 | 137 |
| 178 template <typename Function, typename... Ts, size_t... Ns> | 138 template <typename Function, typename... Ts, size_t... Ns> |
| 179 inline void DispatchToFunctionImpl(Function function, | 139 inline void DispatchToFunctionImpl(Function function, |
| 180 const Tuple<Ts...>& arg, | 140 const std::tuple<Ts...>& arg, |
| 181 IndexSequence<Ns...>) { | 141 IndexSequence<Ns...>) { |
| 182 (*function)(internal::Unwrap(get<Ns>(arg))...); | 142 (*function)(internal::Unwrap(std::get<Ns>(arg))...); |
| 183 } | 143 } |
| 184 | 144 |
| 185 template <typename Function, typename... Ts> | 145 template <typename Function, typename... Ts> |
| 186 inline void DispatchToFunction(Function function, const Tuple<Ts...>& arg) { | 146 inline void DispatchToFunction(Function function, |
| 147 const std::tuple<Ts...>& arg) { |
| 187 DispatchToFunctionImpl(function, arg, MakeIndexSequence<sizeof...(Ts)>()); | 148 DispatchToFunctionImpl(function, arg, MakeIndexSequence<sizeof...(Ts)>()); |
| 188 } | 149 } |
| 189 | 150 |
| 190 // Dispatchers with out parameters. | 151 // Dispatchers with out parameters. |
| 191 | 152 |
| 192 template <typename ObjT, | 153 template <typename ObjT, |
| 193 typename Method, | 154 typename Method, |
| 194 typename... InTs, | 155 typename... InTs, |
| 195 typename... OutTs, | 156 typename... OutTs, |
| 196 size_t... InNs, | 157 size_t... InNs, |
| 197 size_t... OutNs> | 158 size_t... OutNs> |
| 198 inline void DispatchToMethodImpl(const ObjT& obj, | 159 inline void DispatchToMethodImpl(const ObjT& obj, |
| 199 Method method, | 160 Method method, |
| 200 const Tuple<InTs...>& in, | 161 const std::tuple<InTs...>& in, |
| 201 Tuple<OutTs...>* out, | 162 std::tuple<OutTs...>* out, |
| 202 IndexSequence<InNs...>, | 163 IndexSequence<InNs...>, |
| 203 IndexSequence<OutNs...>) { | 164 IndexSequence<OutNs...>) { |
| 204 (obj->*method)(internal::Unwrap(get<InNs>(in))..., &get<OutNs>(*out)...); | 165 (obj->*method)(internal::Unwrap(std::get<InNs>(in))..., |
| 166 &std::get<OutNs>(*out)...); |
| 205 } | 167 } |
| 206 | 168 |
| 207 template <typename ObjT, typename Method, typename... InTs, typename... OutTs> | 169 template <typename ObjT, typename Method, typename... InTs, typename... OutTs> |
| 208 inline void DispatchToMethod(const ObjT& obj, | 170 inline void DispatchToMethod(const ObjT& obj, |
| 209 Method method, | 171 Method method, |
| 210 const Tuple<InTs...>& in, | 172 const std::tuple<InTs...>& in, |
| 211 Tuple<OutTs...>* out) { | 173 std::tuple<OutTs...>* out) { |
| 212 DispatchToMethodImpl(obj, method, in, out, | 174 DispatchToMethodImpl(obj, method, in, out, |
| 213 MakeIndexSequence<sizeof...(InTs)>(), | 175 MakeIndexSequence<sizeof...(InTs)>(), |
| 214 MakeIndexSequence<sizeof...(OutTs)>()); | 176 MakeIndexSequence<sizeof...(OutTs)>()); |
| 215 } | 177 } |
| 216 | 178 |
| 217 } // namespace base | 179 } // namespace base |
| 218 | 180 |
| 219 #endif // BASE_TUPLE_H_ | 181 #endif // BASE_TUPLE_H_ |
| OLD | NEW |