[email protected] | 6b9b771c | 2011-09-28 00:33:59 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
mdempsky | 06c626a | 2014-12-10 11:22:23 | [diff] [blame] | 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 |
| 7 | // arguments and will construct and return the appropriate Tuple object. The |
| 8 | // functions DispatchToMethod and DispatchToFunction take a function pointer or |
| 9 | // instance and method pointer, and unpack a tuple into arguments to the call. |
[email protected] | 302bdc13 | 2008-08-25 13:42:07 | [diff] [blame] | 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 | // |
| 14 | // Example usage: |
| 15 | // // These two methods of creating a Tuple are identical. |
mdempsky | 06c626a | 2014-12-10 11:22:23 | [diff] [blame] | 16 | // Tuple<int, const char*> tuple_a(1, "wee"); |
| 17 | // Tuple<int, const char*> tuple_b = MakeTuple(1, "wee"); |
[email protected] | 302bdc13 | 2008-08-25 13:42:07 | [diff] [blame] | 18 | // |
| 19 | // void SomeFunc(int a, const char* b) { } |
| 20 | // DispatchToFunction(&SomeFunc, tuple_a); // SomeFunc(1, "wee") |
| 21 | // DispatchToFunction( |
| 22 | // &SomeFunc, MakeTuple(10, "foo")); // SomeFunc(10, "foo") |
| 23 | // |
| 24 | // struct { void SomeMeth(int a, int b, int c) { } } foo; |
| 25 | // DispatchToMethod(&foo, &Foo::SomeMeth, MakeTuple(1, 2, 3)); |
| 26 | // // foo->SomeMeth(1, 2, 3); |
| 27 | |
tfarina | a3116351 | 2015-05-13 22:10:15 | [diff] [blame] | 28 | #ifndef BASE_TUPLE_H_ |
| 29 | #define BASE_TUPLE_H_ |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 30 | |
[email protected] | 58ae630 | 2013-06-27 12:48:02 | [diff] [blame] | 31 | #include "base/bind_helpers.h" |
| 32 | |
brettw | d5ca2bc | 2015-05-29 22:15:47 | [diff] [blame^] | 33 | namespace base { |
| 34 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 35 | // Index sequences |
| 36 | // |
| 37 | // Minimal clone of the similarly-named C++14 functionality. |
| 38 | |
| 39 | template <size_t...> |
| 40 | struct IndexSequence {}; |
| 41 | |
| 42 | template <size_t... Ns> |
| 43 | struct MakeIndexSequenceImpl; |
| 44 | |
brucedawson | b132ccd | 2015-01-21 22:02:16 | [diff] [blame] | 45 | #if defined(_PREFAST_) && defined(OS_WIN) |
| 46 | |
| 47 | // Work around VC++ 2013 /analyze internal compiler error: |
| 48 | // https://connect.microsoft.com/VisualStudio/feedback/details/1053626 |
| 49 | |
| 50 | template <> struct MakeIndexSequenceImpl<0> { |
| 51 | using Type = IndexSequence<>; |
| 52 | }; |
| 53 | template <> struct MakeIndexSequenceImpl<1> { |
| 54 | using Type = IndexSequence<0>; |
| 55 | }; |
| 56 | template <> struct MakeIndexSequenceImpl<2> { |
| 57 | using Type = IndexSequence<0,1>; |
| 58 | }; |
| 59 | template <> struct MakeIndexSequenceImpl<3> { |
| 60 | using Type = IndexSequence<0,1,2>; |
| 61 | }; |
| 62 | template <> struct MakeIndexSequenceImpl<4> { |
| 63 | using Type = IndexSequence<0,1,2,3>; |
| 64 | }; |
| 65 | template <> struct MakeIndexSequenceImpl<5> { |
| 66 | using Type = IndexSequence<0,1,2,3,4>; |
| 67 | }; |
| 68 | template <> struct MakeIndexSequenceImpl<6> { |
| 69 | using Type = IndexSequence<0,1,2,3,4,5>; |
| 70 | }; |
| 71 | template <> struct MakeIndexSequenceImpl<7> { |
| 72 | using Type = IndexSequence<0,1,2,3,4,5,6>; |
| 73 | }; |
| 74 | template <> struct MakeIndexSequenceImpl<8> { |
| 75 | using Type = IndexSequence<0,1,2,3,4,5,6,7>; |
| 76 | }; |
| 77 | template <> struct MakeIndexSequenceImpl<9> { |
| 78 | using Type = IndexSequence<0,1,2,3,4,5,6,7,8>; |
| 79 | }; |
| 80 | template <> struct MakeIndexSequenceImpl<10> { |
| 81 | using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9>; |
| 82 | }; |
| 83 | template <> struct MakeIndexSequenceImpl<11> { |
| 84 | using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9,10>; |
| 85 | }; |
brucedawson | 35709e17 | 2015-04-20 18:20:37 | [diff] [blame] | 86 | template <> struct MakeIndexSequenceImpl<12> { |
| 87 | using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9,10,11>; |
| 88 | }; |
| 89 | template <> struct MakeIndexSequenceImpl<13> { |
| 90 | using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9,10,11,12>; |
| 91 | }; |
brucedawson | b132ccd | 2015-01-21 22:02:16 | [diff] [blame] | 92 | |
| 93 | #else // defined(WIN) && defined(_PREFAST_) |
| 94 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 95 | template <size_t... Ns> |
| 96 | struct MakeIndexSequenceImpl<0, Ns...> { |
| 97 | using Type = IndexSequence<Ns...>; |
| 98 | }; |
| 99 | |
| 100 | template <size_t N, size_t... Ns> |
mdempsky | 06c626a | 2014-12-10 11:22:23 | [diff] [blame] | 101 | struct MakeIndexSequenceImpl<N, Ns...> |
| 102 | : MakeIndexSequenceImpl<N - 1, N - 1, Ns...> {}; |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 103 | |
brucedawson | b132ccd | 2015-01-21 22:02:16 | [diff] [blame] | 104 | #endif // defined(WIN) && defined(_PREFAST_) |
| 105 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 106 | template <size_t N> |
| 107 | using MakeIndexSequence = typename MakeIndexSequenceImpl<N>::Type; |
| 108 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 109 | // Traits ---------------------------------------------------------------------- |
| 110 | // |
| 111 | // A simple traits class for tuple arguments. |
| 112 | // |
| 113 | // ValueType: the bare, nonref version of a type (same as the type for nonrefs). |
| 114 | // RefType: the ref version of a type (same as the type for refs). |
| 115 | // ParamType: what type to pass to functions (refs should not be constified). |
| 116 | |
| 117 | template <class P> |
| 118 | struct TupleTraits { |
| 119 | typedef P ValueType; |
| 120 | typedef P& RefType; |
| 121 | typedef const P& ParamType; |
| 122 | }; |
| 123 | |
| 124 | template <class P> |
| 125 | struct TupleTraits<P&> { |
| 126 | typedef P ValueType; |
| 127 | typedef P& RefType; |
| 128 | typedef P& ParamType; |
| 129 | }; |
| 130 | |
| 131 | // Tuple ----------------------------------------------------------------------- |
| 132 | // |
| 133 | // This set of classes is useful for bundling 0 or more heterogeneous data types |
| 134 | // into a single variable. The advantage of this is that it greatly simplifies |
| 135 | // function objects that need to take an arbitrary number of parameters; see |
| 136 | // RunnableMethod and IPC::MessageWithTuple. |
| 137 | // |
mdempsky | 06c626a | 2014-12-10 11:22:23 | [diff] [blame] | 138 | // Tuple<> is supplied to act as a 'void' type. It can be used, for example, |
[email protected] | 45871198 | 2008-08-21 10:58:08 | [diff] [blame] | 139 | // when dispatching to a function that accepts no arguments (see the |
| 140 | // Dispatchers below). |
mdempsky | 06c626a | 2014-12-10 11:22:23 | [diff] [blame] | 141 | // Tuple<A> is rarely useful. One such use is when A is non-const ref that you |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 142 | // want filled by the dispatchee, and the tuple is merely a container for that |
| 143 | // output (a "tier"). See MakeRefTuple and its usages. |
| 144 | |
mdempsky | 06c626a | 2014-12-10 11:22:23 | [diff] [blame] | 145 | template <typename IxSeq, typename... Ts> |
| 146 | struct TupleBaseImpl; |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 147 | template <typename... Ts> |
mdempsky | 06c626a | 2014-12-10 11:22:23 | [diff] [blame] | 148 | using TupleBase = TupleBaseImpl<MakeIndexSequence<sizeof...(Ts)>, Ts...>; |
| 149 | template <size_t N, typename T> |
| 150 | struct TupleLeaf; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 151 | |
mdempsky | 06c626a | 2014-12-10 11:22:23 | [diff] [blame] | 152 | template <typename... Ts> |
| 153 | struct Tuple : TupleBase<Ts...> { |
| 154 | Tuple() : TupleBase<Ts...>() {} |
| 155 | explicit Tuple(typename TupleTraits<Ts>::ParamType... args) |
| 156 | : TupleBase<Ts...>(args...) {} |
| 157 | }; |
| 158 | |
| 159 | // Avoids ambiguity between Tuple's two constructors. |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 160 | template <> |
| 161 | struct Tuple<> {}; |
| 162 | |
mdempsky | 06c626a | 2014-12-10 11:22:23 | [diff] [blame] | 163 | template <size_t... Ns, typename... Ts> |
| 164 | struct TupleBaseImpl<IndexSequence<Ns...>, Ts...> : TupleLeaf<Ns, Ts>... { |
| 165 | TupleBaseImpl() : TupleLeaf<Ns, Ts>()... {} |
| 166 | explicit TupleBaseImpl(typename TupleTraits<Ts>::ParamType... args) |
| 167 | : TupleLeaf<Ns, Ts>(args)... {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 168 | }; |
| 169 | |
mdempsky | 06c626a | 2014-12-10 11:22:23 | [diff] [blame] | 170 | template <size_t N, typename T> |
| 171 | struct TupleLeaf { |
mdempsky | f2c5add | 2014-12-15 23:26:13 | [diff] [blame] | 172 | TupleLeaf() {} |
mdempsky | 06c626a | 2014-12-10 11:22:23 | [diff] [blame] | 173 | explicit TupleLeaf(typename TupleTraits<T>::ParamType x) : x(x) {} |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 174 | |
mdempsky | 06c626a | 2014-12-10 11:22:23 | [diff] [blame] | 175 | T& get() { return x; } |
| 176 | const T& get() const { return x; } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 177 | |
mdempsky | 06c626a | 2014-12-10 11:22:23 | [diff] [blame] | 178 | T x; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 179 | }; |
| 180 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 181 | // Tuple getters -------------------------------------------------------------- |
mdempsky | 06c626a | 2014-12-10 11:22:23 | [diff] [blame] | 182 | // |
| 183 | // Allows accessing an arbitrary tuple element by index. |
| 184 | // |
| 185 | // Example usage: |
brettw | d5ca2bc | 2015-05-29 22:15:47 | [diff] [blame^] | 186 | // base::Tuple<int, double> t2; |
| 187 | // base::get<0>(t2) = 42; |
| 188 | // base::get<1>(t2) = 3.14; |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 189 | |
mdempsky | 06c626a | 2014-12-10 11:22:23 | [diff] [blame] | 190 | template <size_t I, typename T> |
| 191 | T& get(TupleLeaf<I, T>& leaf) { |
| 192 | return leaf.get(); |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 193 | } |
| 194 | |
mdempsky | 06c626a | 2014-12-10 11:22:23 | [diff] [blame] | 195 | template <size_t I, typename T> |
| 196 | const T& get(const TupleLeaf<I, T>& leaf) { |
| 197 | return leaf.get(); |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 198 | } |
| 199 | |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 200 | // Tuple types ---------------------------------------------------------------- |
| 201 | // |
| 202 | // Allows for selection of ValueTuple/RefTuple/ParamTuple without needing the |
| 203 | // definitions of class types the tuple takes as parameters. |
| 204 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 205 | template <typename T> |
| 206 | struct TupleTypes; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 207 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 208 | template <typename... Ts> |
| 209 | struct TupleTypes<Tuple<Ts...>> { |
| 210 | using ValueTuple = Tuple<typename TupleTraits<Ts>::ValueType...>; |
| 211 | using RefTuple = Tuple<typename TupleTraits<Ts>::RefType...>; |
| 212 | using ParamTuple = Tuple<typename TupleTraits<Ts>::ParamType...>; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 213 | }; |
| 214 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 215 | // Tuple creators ------------------------------------------------------------- |
| 216 | // |
| 217 | // Helper functions for constructing tuples while inferring the template |
| 218 | // argument types. |
| 219 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 220 | template <typename... Ts> |
| 221 | inline Tuple<Ts...> MakeTuple(const Ts&... arg) { |
| 222 | return Tuple<Ts...>(arg...); |
[email protected] | ae894519 | 2010-07-20 16:56:26 | [diff] [blame] | 223 | } |
| 224 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 225 | // The following set of helpers make what Boost refers to as "Tiers" - a tuple |
| 226 | // of references. |
| 227 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 228 | template <typename... Ts> |
| 229 | inline Tuple<Ts&...> MakeRefTuple(Ts&... arg) { |
| 230 | return Tuple<Ts&...>(arg...); |
[email protected] | ae894519 | 2010-07-20 16:56:26 | [diff] [blame] | 231 | } |
| 232 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 233 | // Dispatchers ---------------------------------------------------------------- |
| 234 | // |
| 235 | // Helper functions that call the given method on an object, with the unpacked |
| 236 | // tuple arguments. Notice that they all have the same number of arguments, |
| 237 | // so you need only write: |
| 238 | // DispatchToMethod(object, &Object::method, args); |
| 239 | // This is very useful for templated dispatchers, since they don't need to know |
| 240 | // what type |args| is. |
| 241 | |
| 242 | // Non-Static Dispatchers with no out params. |
| 243 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 244 | template <typename ObjT, typename Method, typename A> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 245 | inline void DispatchToMethod(ObjT* obj, Method method, const A& arg) { |
[email protected] | 58ae630 | 2013-06-27 12:48:02 | [diff] [blame] | 246 | (obj->*method)(base::internal::UnwrapTraits<A>::Unwrap(arg)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 247 | } |
| 248 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 249 | template <typename ObjT, typename Method, typename... Ts, size_t... Ns> |
| 250 | inline void DispatchToMethodImpl(ObjT* obj, |
| 251 | Method method, |
| 252 | const Tuple<Ts...>& arg, |
| 253 | IndexSequence<Ns...>) { |
| 254 | (obj->*method)(base::internal::UnwrapTraits<Ts>::Unwrap(get<Ns>(arg))...); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 255 | } |
| 256 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 257 | template <typename ObjT, typename Method, typename... Ts> |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 258 | inline void DispatchToMethod(ObjT* obj, |
| 259 | Method method, |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 260 | const Tuple<Ts...>& arg) { |
| 261 | DispatchToMethodImpl(obj, method, arg, MakeIndexSequence<sizeof...(Ts)>()); |
[email protected] | fa685ff | 2011-02-17 19:47:13 | [diff] [blame] | 262 | } |
| 263 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 264 | // Static Dispatchers with no out params. |
| 265 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 266 | template <typename Function, typename A> |
| 267 | inline void DispatchToMethod(Function function, const A& arg) { |
| 268 | (*function)(base::internal::UnwrapTraits<A>::Unwrap(arg)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 269 | } |
| 270 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 271 | template <typename Function, typename... Ts, size_t... Ns> |
| 272 | inline void DispatchToFunctionImpl(Function function, |
| 273 | const Tuple<Ts...>& arg, |
| 274 | IndexSequence<Ns...>) { |
| 275 | (*function)(base::internal::UnwrapTraits<Ts>::Unwrap(get<Ns>(arg))...); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 276 | } |
| 277 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 278 | template <typename Function, typename... Ts> |
| 279 | inline void DispatchToFunction(Function function, const Tuple<Ts...>& arg) { |
| 280 | DispatchToFunctionImpl(function, arg, MakeIndexSequence<sizeof...(Ts)>()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 281 | } |
| 282 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 283 | // Dispatchers with out parameters. |
| 284 | |
| 285 | template <typename ObjT, |
| 286 | typename Method, |
| 287 | typename In, |
| 288 | typename... OutTs, |
| 289 | size_t... OutNs> |
| 290 | inline void DispatchToMethodImpl(ObjT* obj, |
| 291 | Method method, |
| 292 | const In& in, |
| 293 | Tuple<OutTs...>* out, |
| 294 | IndexSequence<OutNs...>) { |
| 295 | (obj->*method)(base::internal::UnwrapTraits<In>::Unwrap(in), |
| 296 | &get<OutNs>(*out)...); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 297 | } |
| 298 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 299 | template <typename ObjT, typename Method, typename In, typename... OutTs> |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 300 | inline void DispatchToMethod(ObjT* obj, |
| 301 | Method method, |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 302 | const In& in, |
| 303 | Tuple<OutTs...>* out) { |
| 304 | DispatchToMethodImpl(obj, method, in, out, |
| 305 | MakeIndexSequence<sizeof...(OutTs)>()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 306 | } |
| 307 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 308 | template <typename ObjT, |
| 309 | typename Method, |
| 310 | typename... InTs, |
| 311 | typename... OutTs, |
| 312 | size_t... InNs, |
| 313 | size_t... OutNs> |
| 314 | inline void DispatchToMethodImpl(ObjT* obj, |
| 315 | Method method, |
| 316 | const Tuple<InTs...>& in, |
| 317 | Tuple<OutTs...>* out, |
| 318 | IndexSequence<InNs...>, |
| 319 | IndexSequence<OutNs...>) { |
| 320 | (obj->*method)(base::internal::UnwrapTraits<InTs>::Unwrap(get<InNs>(in))..., |
| 321 | &get<OutNs>(*out)...); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 322 | } |
| 323 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 324 | template <typename ObjT, typename Method, typename... InTs, typename... OutTs> |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 325 | inline void DispatchToMethod(ObjT* obj, |
| 326 | Method method, |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 327 | const Tuple<InTs...>& in, |
| 328 | Tuple<OutTs...>* out) { |
| 329 | DispatchToMethodImpl(obj, method, in, out, |
| 330 | MakeIndexSequence<sizeof...(InTs)>(), |
| 331 | MakeIndexSequence<sizeof...(OutTs)>()); |
[email protected] | 8a2820a | 2008-10-09 21:58:05 | [diff] [blame] | 332 | } |
| 333 | |
brettw | d5ca2bc | 2015-05-29 22:15:47 | [diff] [blame^] | 334 | } // namespace base |
| 335 | |
tfarina | a3116351 | 2015-05-13 22:10:15 | [diff] [blame] | 336 | #endif // BASE_TUPLE_H_ |