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 |
127 // Tuple getters -------------------------------------------------------------- | 192 // Tuple getters -------------------------------------------------------------- |
128 // | 193 // |
129 // Allows accessing an arbitrary tuple element by index. | 194 // Allows accessing an arbitrary tuple element by index. |
130 // | 195 // |
131 // Example usage: | 196 // Example usage: |
132 // Tuple<int, double> t2; | 197 // Tuple<int, double> t2; |
133 // get<0>(t2) = 42; | 198 // get<0>(t2) = 42; |
134 // get<1>(t2) = 3.14; | 199 // get<1>(t2) = 3.14; |
135 | 200 |
136 template <size_t I, typename T> | 201 template <size_t I, typename T> |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 inline void DispatchToMethod(ObjT* obj, | 336 inline void DispatchToMethod(ObjT* obj, |
272 Method method, | 337 Method method, |
273 const Tuple<InTs...>& in, | 338 const Tuple<InTs...>& in, |
274 Tuple<OutTs...>* out) { | 339 Tuple<OutTs...>* out) { |
275 DispatchToMethodImpl(obj, method, in, out, | 340 DispatchToMethodImpl(obj, method, in, out, |
276 MakeIndexSequence<sizeof...(InTs)>(), | 341 MakeIndexSequence<sizeof...(InTs)>(), |
277 MakeIndexSequence<sizeof...(OutTs)>()); | 342 MakeIndexSequence<sizeof...(OutTs)>()); |
278 } | 343 } |
279 | 344 |
280 #endif // BASE_TUPLE_H__ | 345 #endif // BASE_TUPLE_H__ |
OLD | NEW |