oshima@chromium.org | cc65491 | 2011-09-28 09:33:59 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [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 | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
mdempsky | f705f9f | 2014-12-10 20:22:23 +0900 | [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. |
deanm@google.com | 96aac0a | 2008-08-25 22:42:07 +0900 | [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 | f705f9f | 2014-12-10 20:22:23 +0900 | [diff] [blame] | 16 | // Tuple<int, const char*> tuple_a(1, "wee"); |
| 17 | // Tuple<int, const char*> tuple_b = MakeTuple(1, "wee"); |
deanm@google.com | 96aac0a | 2008-08-25 22:42:07 +0900 | [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 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 28 | #ifndef BASE_TUPLE_H__ |
| 29 | #define BASE_TUPLE_H__ |
| 30 | |
rsleevi@chromium.org | 274c722 | 2013-06-27 21:48:02 +0900 | [diff] [blame] | 31 | #include "base/bind_helpers.h" |
| 32 | |
mdempsky | 0c1d6ed | 2014-12-10 12:10:59 +0900 | [diff] [blame] | 33 | // Index sequences |
| 34 | // |
| 35 | // Minimal clone of the similarly-named C++14 functionality. |
| 36 | |
| 37 | template <size_t...> |
| 38 | struct IndexSequence {}; |
| 39 | |
| 40 | template <size_t... Ns> |
| 41 | struct MakeIndexSequenceImpl; |
| 42 | |
| 43 | template <size_t... Ns> |
| 44 | struct MakeIndexSequenceImpl<0, Ns...> { |
| 45 | using Type = IndexSequence<Ns...>; |
| 46 | }; |
| 47 | |
| 48 | template <size_t N, size_t... Ns> |
mdempsky | f705f9f | 2014-12-10 20:22:23 +0900 | [diff] [blame] | 49 | struct MakeIndexSequenceImpl<N, Ns...> |
| 50 | : MakeIndexSequenceImpl<N - 1, N - 1, Ns...> {}; |
mdempsky | 0c1d6ed | 2014-12-10 12:10:59 +0900 | [diff] [blame] | 51 | |
| 52 | template <size_t N> |
| 53 | using MakeIndexSequence = typename MakeIndexSequenceImpl<N>::Type; |
| 54 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 55 | // Traits ---------------------------------------------------------------------- |
| 56 | // |
| 57 | // A simple traits class for tuple arguments. |
| 58 | // |
| 59 | // ValueType: the bare, nonref version of a type (same as the type for nonrefs). |
| 60 | // RefType: the ref version of a type (same as the type for refs). |
| 61 | // ParamType: what type to pass to functions (refs should not be constified). |
| 62 | |
| 63 | template <class P> |
| 64 | struct TupleTraits { |
| 65 | typedef P ValueType; |
| 66 | typedef P& RefType; |
| 67 | typedef const P& ParamType; |
| 68 | }; |
| 69 | |
| 70 | template <class P> |
| 71 | struct TupleTraits<P&> { |
| 72 | typedef P ValueType; |
| 73 | typedef P& RefType; |
| 74 | typedef P& ParamType; |
| 75 | }; |
| 76 | |
| 77 | // Tuple ----------------------------------------------------------------------- |
| 78 | // |
| 79 | // This set of classes is useful for bundling 0 or more heterogeneous data types |
| 80 | // into a single variable. The advantage of this is that it greatly simplifies |
| 81 | // function objects that need to take an arbitrary number of parameters; see |
| 82 | // RunnableMethod and IPC::MessageWithTuple. |
| 83 | // |
mdempsky | f705f9f | 2014-12-10 20:22:23 +0900 | [diff] [blame] | 84 | // Tuple<> is supplied to act as a 'void' type. It can be used, for example, |
deanm@google.com | ff4b729 | 2008-08-21 19:58:08 +0900 | [diff] [blame] | 85 | // when dispatching to a function that accepts no arguments (see the |
| 86 | // Dispatchers below). |
mdempsky | f705f9f | 2014-12-10 20:22:23 +0900 | [diff] [blame] | 87 | // Tuple<A> is rarely useful. One such use is when A is non-const ref that you |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 88 | // want filled by the dispatchee, and the tuple is merely a container for that |
| 89 | // output (a "tier"). See MakeRefTuple and its usages. |
| 90 | |
mdempsky | f705f9f | 2014-12-10 20:22:23 +0900 | [diff] [blame] | 91 | template <typename IxSeq, typename... Ts> |
| 92 | struct TupleBaseImpl; |
mdempsky | 0c1d6ed | 2014-12-10 12:10:59 +0900 | [diff] [blame] | 93 | template <typename... Ts> |
mdempsky | f705f9f | 2014-12-10 20:22:23 +0900 | [diff] [blame] | 94 | using TupleBase = TupleBaseImpl<MakeIndexSequence<sizeof...(Ts)>, Ts...>; |
| 95 | template <size_t N, typename T> |
| 96 | struct TupleLeaf; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 97 | |
mdempsky | f705f9f | 2014-12-10 20:22:23 +0900 | [diff] [blame] | 98 | template <typename... Ts> |
| 99 | struct Tuple : TupleBase<Ts...> { |
| 100 | Tuple() : TupleBase<Ts...>() {} |
| 101 | explicit Tuple(typename TupleTraits<Ts>::ParamType... args) |
| 102 | : TupleBase<Ts...>(args...) {} |
| 103 | }; |
| 104 | |
| 105 | // Avoids ambiguity between Tuple's two constructors. |
mdempsky | 0c1d6ed | 2014-12-10 12:10:59 +0900 | [diff] [blame] | 106 | template <> |
| 107 | struct Tuple<> {}; |
| 108 | |
mdempsky | f705f9f | 2014-12-10 20:22:23 +0900 | [diff] [blame] | 109 | template <size_t... Ns, typename... Ts> |
| 110 | struct TupleBaseImpl<IndexSequence<Ns...>, Ts...> : TupleLeaf<Ns, Ts>... { |
| 111 | TupleBaseImpl() : TupleLeaf<Ns, Ts>()... {} |
| 112 | explicit TupleBaseImpl(typename TupleTraits<Ts>::ParamType... args) |
| 113 | : TupleLeaf<Ns, Ts>(args)... {} |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 114 | }; |
| 115 | |
mdempsky | f705f9f | 2014-12-10 20:22:23 +0900 | [diff] [blame] | 116 | template <size_t N, typename T> |
| 117 | struct TupleLeaf { |
mdempsky | 00c9723 | 2014-12-16 08:26:13 +0900 | [diff] [blame] | 118 | TupleLeaf() {} |
mdempsky | f705f9f | 2014-12-10 20:22:23 +0900 | [diff] [blame] | 119 | explicit TupleLeaf(typename TupleTraits<T>::ParamType x) : x(x) {} |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 120 | |
mdempsky | f705f9f | 2014-12-10 20:22:23 +0900 | [diff] [blame] | 121 | T& get() { return x; } |
| 122 | const T& get() const { return x; } |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 123 | |
mdempsky | f705f9f | 2014-12-10 20:22:23 +0900 | [diff] [blame] | 124 | T x; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 125 | }; |
| 126 | |
mdempsky | 0c1d6ed | 2014-12-10 12:10:59 +0900 | [diff] [blame] | 127 | // Tuple getters -------------------------------------------------------------- |
mdempsky | f705f9f | 2014-12-10 20:22:23 +0900 | [diff] [blame] | 128 | // |
| 129 | // Allows accessing an arbitrary tuple element by index. |
| 130 | // |
| 131 | // Example usage: |
| 132 | // Tuple<int, double> t2; |
| 133 | // get<0>(t2) = 42; |
| 134 | // get<1>(t2) = 3.14; |
mdempsky | 0c1d6ed | 2014-12-10 12:10:59 +0900 | [diff] [blame] | 135 | |
mdempsky | f705f9f | 2014-12-10 20:22:23 +0900 | [diff] [blame] | 136 | template <size_t I, typename T> |
| 137 | T& get(TupleLeaf<I, T>& leaf) { |
| 138 | return leaf.get(); |
mdempsky | 0c1d6ed | 2014-12-10 12:10:59 +0900 | [diff] [blame] | 139 | } |
| 140 | |
mdempsky | f705f9f | 2014-12-10 20:22:23 +0900 | [diff] [blame] | 141 | template <size_t I, typename T> |
| 142 | const T& get(const TupleLeaf<I, T>& leaf) { |
| 143 | return leaf.get(); |
mdempsky | 0c1d6ed | 2014-12-10 12:10:59 +0900 | [diff] [blame] | 144 | } |
| 145 | |
erg@google.com | e6ffcb5 | 2010-08-18 03:38:24 +0900 | [diff] [blame] | 146 | // Tuple types ---------------------------------------------------------------- |
| 147 | // |
| 148 | // Allows for selection of ValueTuple/RefTuple/ParamTuple without needing the |
| 149 | // definitions of class types the tuple takes as parameters. |
| 150 | |
mdempsky | 0c1d6ed | 2014-12-10 12:10:59 +0900 | [diff] [blame] | 151 | template <typename T> |
| 152 | struct TupleTypes; |
erg@google.com | e6ffcb5 | 2010-08-18 03:38:24 +0900 | [diff] [blame] | 153 | |
mdempsky | 0c1d6ed | 2014-12-10 12:10:59 +0900 | [diff] [blame] | 154 | template <typename... Ts> |
| 155 | struct TupleTypes<Tuple<Ts...>> { |
| 156 | using ValueTuple = Tuple<typename TupleTraits<Ts>::ValueType...>; |
| 157 | using RefTuple = Tuple<typename TupleTraits<Ts>::RefType...>; |
| 158 | using ParamTuple = Tuple<typename TupleTraits<Ts>::ParamType...>; |
erg@google.com | e6ffcb5 | 2010-08-18 03:38:24 +0900 | [diff] [blame] | 159 | }; |
| 160 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 161 | // Tuple creators ------------------------------------------------------------- |
| 162 | // |
| 163 | // Helper functions for constructing tuples while inferring the template |
| 164 | // argument types. |
| 165 | |
mdempsky | 0c1d6ed | 2014-12-10 12:10:59 +0900 | [diff] [blame] | 166 | template <typename... Ts> |
| 167 | inline Tuple<Ts...> MakeTuple(const Ts&... arg) { |
| 168 | return Tuple<Ts...>(arg...); |
phajdan.jr@chromium.org | 5a468fc | 2010-07-21 01:56:26 +0900 | [diff] [blame] | 169 | } |
| 170 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 171 | // The following set of helpers make what Boost refers to as "Tiers" - a tuple |
| 172 | // of references. |
| 173 | |
mdempsky | 0c1d6ed | 2014-12-10 12:10:59 +0900 | [diff] [blame] | 174 | template <typename... Ts> |
| 175 | inline Tuple<Ts&...> MakeRefTuple(Ts&... arg) { |
| 176 | return Tuple<Ts&...>(arg...); |
phajdan.jr@chromium.org | 5a468fc | 2010-07-21 01:56:26 +0900 | [diff] [blame] | 177 | } |
| 178 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 179 | // Dispatchers ---------------------------------------------------------------- |
| 180 | // |
| 181 | // Helper functions that call the given method on an object, with the unpacked |
| 182 | // tuple arguments. Notice that they all have the same number of arguments, |
| 183 | // so you need only write: |
| 184 | // DispatchToMethod(object, &Object::method, args); |
| 185 | // This is very useful for templated dispatchers, since they don't need to know |
| 186 | // what type |args| is. |
| 187 | |
| 188 | // Non-Static Dispatchers with no out params. |
| 189 | |
mdempsky | 0c1d6ed | 2014-12-10 12:10:59 +0900 | [diff] [blame] | 190 | template <typename ObjT, typename Method, typename A> |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 191 | inline void DispatchToMethod(ObjT* obj, Method method, const A& arg) { |
rsleevi@chromium.org | 274c722 | 2013-06-27 21:48:02 +0900 | [diff] [blame] | 192 | (obj->*method)(base::internal::UnwrapTraits<A>::Unwrap(arg)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 193 | } |
| 194 | |
mdempsky | 0c1d6ed | 2014-12-10 12:10:59 +0900 | [diff] [blame] | 195 | template <typename ObjT, typename Method, typename... Ts, size_t... Ns> |
| 196 | inline void DispatchToMethodImpl(ObjT* obj, |
| 197 | Method method, |
| 198 | const Tuple<Ts...>& arg, |
| 199 | IndexSequence<Ns...>) { |
| 200 | (obj->*method)(base::internal::UnwrapTraits<Ts>::Unwrap(get<Ns>(arg))...); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 201 | } |
| 202 | |
mdempsky | 0c1d6ed | 2014-12-10 12:10:59 +0900 | [diff] [blame] | 203 | template <typename ObjT, typename Method, typename... Ts> |
maruel@chromium.org | 8fe7adc | 2009-03-04 00:01:12 +0900 | [diff] [blame] | 204 | inline void DispatchToMethod(ObjT* obj, |
| 205 | Method method, |
mdempsky | 0c1d6ed | 2014-12-10 12:10:59 +0900 | [diff] [blame] | 206 | const Tuple<Ts...>& arg) { |
| 207 | DispatchToMethodImpl(obj, method, arg, MakeIndexSequence<sizeof...(Ts)>()); |
noelutz@google.com | dd96ed8 | 2011-02-18 04:47:13 +0900 | [diff] [blame] | 208 | } |
| 209 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 210 | // Static Dispatchers with no out params. |
| 211 | |
mdempsky | 0c1d6ed | 2014-12-10 12:10:59 +0900 | [diff] [blame] | 212 | template <typename Function, typename A> |
| 213 | inline void DispatchToMethod(Function function, const A& arg) { |
| 214 | (*function)(base::internal::UnwrapTraits<A>::Unwrap(arg)); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 215 | } |
| 216 | |
mdempsky | 0c1d6ed | 2014-12-10 12:10:59 +0900 | [diff] [blame] | 217 | template <typename Function, typename... Ts, size_t... Ns> |
| 218 | inline void DispatchToFunctionImpl(Function function, |
| 219 | const Tuple<Ts...>& arg, |
| 220 | IndexSequence<Ns...>) { |
| 221 | (*function)(base::internal::UnwrapTraits<Ts>::Unwrap(get<Ns>(arg))...); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 222 | } |
| 223 | |
mdempsky | 0c1d6ed | 2014-12-10 12:10:59 +0900 | [diff] [blame] | 224 | template <typename Function, typename... Ts> |
| 225 | inline void DispatchToFunction(Function function, const Tuple<Ts...>& arg) { |
| 226 | DispatchToFunctionImpl(function, arg, MakeIndexSequence<sizeof...(Ts)>()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 227 | } |
| 228 | |
mdempsky | 0c1d6ed | 2014-12-10 12:10:59 +0900 | [diff] [blame] | 229 | // Dispatchers with out parameters. |
| 230 | |
| 231 | template <typename ObjT, |
| 232 | typename Method, |
| 233 | typename In, |
| 234 | typename... OutTs, |
| 235 | size_t... OutNs> |
| 236 | inline void DispatchToMethodImpl(ObjT* obj, |
| 237 | Method method, |
| 238 | const In& in, |
| 239 | Tuple<OutTs...>* out, |
| 240 | IndexSequence<OutNs...>) { |
| 241 | (obj->*method)(base::internal::UnwrapTraits<In>::Unwrap(in), |
| 242 | &get<OutNs>(*out)...); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 243 | } |
| 244 | |
mdempsky | 0c1d6ed | 2014-12-10 12:10:59 +0900 | [diff] [blame] | 245 | template <typename ObjT, typename Method, typename In, typename... OutTs> |
maruel@chromium.org | 8fe7adc | 2009-03-04 00:01:12 +0900 | [diff] [blame] | 246 | inline void DispatchToMethod(ObjT* obj, |
| 247 | Method method, |
mdempsky | 0c1d6ed | 2014-12-10 12:10:59 +0900 | [diff] [blame] | 248 | const In& in, |
| 249 | Tuple<OutTs...>* out) { |
| 250 | DispatchToMethodImpl(obj, method, in, out, |
| 251 | MakeIndexSequence<sizeof...(OutTs)>()); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 252 | } |
| 253 | |
mdempsky | 0c1d6ed | 2014-12-10 12:10:59 +0900 | [diff] [blame] | 254 | template <typename ObjT, |
| 255 | typename Method, |
| 256 | typename... InTs, |
| 257 | typename... OutTs, |
| 258 | size_t... InNs, |
| 259 | size_t... OutNs> |
| 260 | inline void DispatchToMethodImpl(ObjT* obj, |
| 261 | Method method, |
| 262 | const Tuple<InTs...>& in, |
| 263 | Tuple<OutTs...>* out, |
| 264 | IndexSequence<InNs...>, |
| 265 | IndexSequence<OutNs...>) { |
| 266 | (obj->*method)(base::internal::UnwrapTraits<InTs>::Unwrap(get<InNs>(in))..., |
| 267 | &get<OutNs>(*out)...); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 268 | } |
| 269 | |
mdempsky | 0c1d6ed | 2014-12-10 12:10:59 +0900 | [diff] [blame] | 270 | template <typename ObjT, typename Method, typename... InTs, typename... OutTs> |
maruel@chromium.org | 8fe7adc | 2009-03-04 00:01:12 +0900 | [diff] [blame] | 271 | inline void DispatchToMethod(ObjT* obj, |
| 272 | Method method, |
mdempsky | 0c1d6ed | 2014-12-10 12:10:59 +0900 | [diff] [blame] | 273 | const Tuple<InTs...>& in, |
| 274 | Tuple<OutTs...>* out) { |
| 275 | DispatchToMethodImpl(obj, method, in, out, |
| 276 | MakeIndexSequence<sizeof...(InTs)>(), |
| 277 | MakeIndexSequence<sizeof...(OutTs)>()); |
ojan@google.com | 3835509 | 2008-10-10 06:58:05 +0900 | [diff] [blame] | 278 | } |
| 279 | |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 280 | #endif // BASE_TUPLE_H__ |