| 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 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 struct TupleLeaf { | 117 struct TupleLeaf { |
| 118 TupleLeaf() {} | 118 TupleLeaf() {} |
| 119 explicit TupleLeaf(typename TupleTraits<T>::ParamType x) : x(x) {} | 119 explicit TupleLeaf(typename TupleTraits<T>::ParamType x) : x(x) {} |
| 120 | 120 |
| 121 T& get() { return x; } | 121 T& get() { return x; } |
| 122 const T& get() const { return x; } | 122 const T& get() const { return x; } |
| 123 | 123 |
| 124 T x; | 124 T x; |
| 125 }; | 125 }; |
| 126 | 126 |
| 127 // For legacy compatibility, we name the first 8 tuple elements "a", "b", ... | |
| 128 // TODO(mdempsky): Update users to use get<N>() (crbug.com/440675). | |
| 129 | |
| 130 #define DEFINE_TUPLE_LEAF(N, x) \ | |
| 131 template <typename T> \ | |
| 132 struct TupleLeaf<N, T> { \ | |
| 133 TupleLeaf() {} \ | |
| 134 explicit TupleLeaf(typename TupleTraits<T>::ParamType x) : x(x) {} \ | |
| 135 \ | |
| 136 T& get() { return x; } \ | |
| 137 const T& get() const { return x; } \ | |
| 138 \ | |
| 139 T x; \ | |
| 140 } | |
| 141 | |
| 142 DEFINE_TUPLE_LEAF(0, a); | |
| 143 DEFINE_TUPLE_LEAF(1, b); | |
| 144 DEFINE_TUPLE_LEAF(2, c); | |
| 145 DEFINE_TUPLE_LEAF(3, d); | |
| 146 DEFINE_TUPLE_LEAF(4, e); | |
| 147 DEFINE_TUPLE_LEAF(5, f); | |
| 148 DEFINE_TUPLE_LEAF(6, g); | |
| 149 DEFINE_TUPLE_LEAF(7, h); | |
| 150 | |
| 151 #undef DEFINE_TUPLE_LEAF | |
| 152 | |
| 153 // Deprecated compat aliases | |
| 154 // TODO(mdempsky): Update users to just use Tuple instead (crbug.com/440675). | |
| 155 | |
| 156 using Tuple0 = Tuple<>; | |
| 157 template <typename A> | |
| 158 using Tuple1 = Tuple<A>; | |
| 159 template <typename A, typename B> | |
| 160 using Tuple2 = Tuple<A, B>; | |
| 161 template <typename A, typename B, typename C> | |
| 162 using Tuple3 = Tuple<A, B, C>; | |
| 163 template <typename A, typename B, typename C, typename D> | |
| 164 using Tuple4 = Tuple<A, B, C, D>; | |
| 165 template <typename A, typename B, typename C, typename D, typename E> | |
| 166 using Tuple5 = Tuple<A, B, C, D, E>; | |
| 167 template <typename A, | |
| 168 typename B, | |
| 169 typename C, | |
| 170 typename D, | |
| 171 typename E, | |
| 172 typename F> | |
| 173 using Tuple6 = Tuple<A, B, C, D, E, F>; | |
| 174 template <typename A, | |
| 175 typename B, | |
| 176 typename C, | |
| 177 typename D, | |
| 178 typename E, | |
| 179 typename F, | |
| 180 typename G> | |
| 181 using Tuple7 = Tuple<A, B, C, D, E, F, G>; | |
| 182 template <typename A, | |
| 183 typename B, | |
| 184 typename C, | |
| 185 typename D, | |
| 186 typename E, | |
| 187 typename F, | |
| 188 typename G, | |
| 189 typename H> | |
| 190 using Tuple8 = Tuple<A, B, C, D, E, F, G, H>; | |
| 191 | |
| 192 // Tuple getters -------------------------------------------------------------- | 127 // Tuple getters -------------------------------------------------------------- |
| 193 // | 128 // |
| 194 // Allows accessing an arbitrary tuple element by index. | 129 // Allows accessing an arbitrary tuple element by index. |
| 195 // | 130 // |
| 196 // Example usage: | 131 // Example usage: |
| 197 // Tuple<int, double> t2; | 132 // Tuple<int, double> t2; |
| 198 // get<0>(t2) = 42; | 133 // get<0>(t2) = 42; |
| 199 // get<1>(t2) = 3.14; | 134 // get<1>(t2) = 3.14; |
| 200 | 135 |
| 201 template <size_t I, typename T> | 136 template <size_t I, typename T> |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 inline void DispatchToMethod(ObjT* obj, | 271 inline void DispatchToMethod(ObjT* obj, |
| 337 Method method, | 272 Method method, |
| 338 const Tuple<InTs...>& in, | 273 const Tuple<InTs...>& in, |
| 339 Tuple<OutTs...>* out) { | 274 Tuple<OutTs...>* out) { |
| 340 DispatchToMethodImpl(obj, method, in, out, | 275 DispatchToMethodImpl(obj, method, in, out, |
| 341 MakeIndexSequence<sizeof...(InTs)>(), | 276 MakeIndexSequence<sizeof...(InTs)>(), |
| 342 MakeIndexSequence<sizeof...(OutTs)>()); | 277 MakeIndexSequence<sizeof...(OutTs)>()); |
| 343 } | 278 } |
| 344 | 279 |
| 345 #endif // BASE_TUPLE_H__ | 280 #endif // BASE_TUPLE_H__ |
| OLD | NEW |