[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 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 31 | #include <stddef.h> |
tzik | 9ca30219 | 2016-02-11 10:24:45 | [diff] [blame] | 32 | #include <tuple> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 33 | |
[email protected] | 58ae630 | 2013-06-27 12:48:02 | [diff] [blame] | 34 | #include "base/bind_helpers.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 35 | #include "build/build_config.h" |
[email protected] | 58ae630 | 2013-06-27 12:48:02 | [diff] [blame] | 36 | |
brettw | d5ca2bc | 2015-05-29 22:15:47 | [diff] [blame] | 37 | namespace base { |
| 38 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 39 | // Index sequences |
| 40 | // |
| 41 | // Minimal clone of the similarly-named C++14 functionality. |
| 42 | |
| 43 | template <size_t...> |
| 44 | struct IndexSequence {}; |
| 45 | |
| 46 | template <size_t... Ns> |
| 47 | struct MakeIndexSequenceImpl; |
| 48 | |
brucedawson | b132ccd | 2015-01-21 22:02:16 | [diff] [blame] | 49 | #if defined(_PREFAST_) && defined(OS_WIN) |
| 50 | |
| 51 | // Work around VC++ 2013 /analyze internal compiler error: |
| 52 | // https://connect.microsoft.com/VisualStudio/feedback/details/1053626 |
| 53 | |
| 54 | template <> struct MakeIndexSequenceImpl<0> { |
| 55 | using Type = IndexSequence<>; |
| 56 | }; |
| 57 | template <> struct MakeIndexSequenceImpl<1> { |
| 58 | using Type = IndexSequence<0>; |
| 59 | }; |
| 60 | template <> struct MakeIndexSequenceImpl<2> { |
| 61 | using Type = IndexSequence<0,1>; |
| 62 | }; |
| 63 | template <> struct MakeIndexSequenceImpl<3> { |
| 64 | using Type = IndexSequence<0,1,2>; |
| 65 | }; |
| 66 | template <> struct MakeIndexSequenceImpl<4> { |
| 67 | using Type = IndexSequence<0,1,2,3>; |
| 68 | }; |
| 69 | template <> struct MakeIndexSequenceImpl<5> { |
| 70 | using Type = IndexSequence<0,1,2,3,4>; |
| 71 | }; |
| 72 | template <> struct MakeIndexSequenceImpl<6> { |
| 73 | using Type = IndexSequence<0,1,2,3,4,5>; |
| 74 | }; |
| 75 | template <> struct MakeIndexSequenceImpl<7> { |
| 76 | using Type = IndexSequence<0,1,2,3,4,5,6>; |
| 77 | }; |
| 78 | template <> struct MakeIndexSequenceImpl<8> { |
| 79 | using Type = IndexSequence<0,1,2,3,4,5,6,7>; |
| 80 | }; |
| 81 | template <> struct MakeIndexSequenceImpl<9> { |
| 82 | using Type = IndexSequence<0,1,2,3,4,5,6,7,8>; |
| 83 | }; |
| 84 | template <> struct MakeIndexSequenceImpl<10> { |
| 85 | using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9>; |
| 86 | }; |
| 87 | template <> struct MakeIndexSequenceImpl<11> { |
| 88 | using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9,10>; |
| 89 | }; |
brucedawson | 35709e17 | 2015-04-20 18:20:37 | [diff] [blame] | 90 | template <> struct MakeIndexSequenceImpl<12> { |
| 91 | using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9,10,11>; |
| 92 | }; |
| 93 | template <> struct MakeIndexSequenceImpl<13> { |
| 94 | using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9,10,11,12>; |
| 95 | }; |
brucedawson | b132ccd | 2015-01-21 22:02:16 | [diff] [blame] | 96 | |
| 97 | #else // defined(WIN) && defined(_PREFAST_) |
| 98 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 99 | template <size_t... Ns> |
| 100 | struct MakeIndexSequenceImpl<0, Ns...> { |
| 101 | using Type = IndexSequence<Ns...>; |
| 102 | }; |
| 103 | |
| 104 | template <size_t N, size_t... Ns> |
mdempsky | 06c626a | 2014-12-10 11:22:23 | [diff] [blame] | 105 | struct MakeIndexSequenceImpl<N, Ns...> |
| 106 | : MakeIndexSequenceImpl<N - 1, N - 1, Ns...> {}; |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 107 | |
brucedawson | b132ccd | 2015-01-21 22:02:16 | [diff] [blame] | 108 | #endif // defined(WIN) && defined(_PREFAST_) |
| 109 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 110 | template <size_t N> |
| 111 | using MakeIndexSequence = typename MakeIndexSequenceImpl<N>::Type; |
| 112 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 113 | // Traits ---------------------------------------------------------------------- |
| 114 | // |
| 115 | // A simple traits class for tuple arguments. |
| 116 | // |
| 117 | // ValueType: the bare, nonref version of a type (same as the type for nonrefs). |
| 118 | // RefType: the ref version of a type (same as the type for refs). |
| 119 | // ParamType: what type to pass to functions (refs should not be constified). |
| 120 | |
| 121 | template <class P> |
| 122 | struct TupleTraits { |
| 123 | typedef P ValueType; |
| 124 | typedef P& RefType; |
| 125 | typedef const P& ParamType; |
| 126 | }; |
| 127 | |
| 128 | template <class P> |
| 129 | struct TupleTraits<P&> { |
| 130 | typedef P ValueType; |
| 131 | typedef P& RefType; |
| 132 | typedef P& ParamType; |
| 133 | }; |
| 134 | |
| 135 | // Tuple ----------------------------------------------------------------------- |
| 136 | // |
| 137 | // This set of classes is useful for bundling 0 or more heterogeneous data types |
| 138 | // into a single variable. The advantage of this is that it greatly simplifies |
| 139 | // function objects that need to take an arbitrary number of parameters; see |
| 140 | // RunnableMethod and IPC::MessageWithTuple. |
| 141 | // |
mdempsky | 06c626a | 2014-12-10 11:22:23 | [diff] [blame] | 142 | // 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] | 143 | // when dispatching to a function that accepts no arguments (see the |
| 144 | // Dispatchers below). |
mdempsky | 06c626a | 2014-12-10 11:22:23 | [diff] [blame] | 145 | // 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] | 146 | // want filled by the dispatchee, and the tuple is merely a container for that |
| 147 | // output (a "tier"). See MakeRefTuple and its usages. |
| 148 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 149 | template <typename... Ts> |
tzik | 9ca30219 | 2016-02-11 10:24:45 | [diff] [blame] | 150 | using Tuple = std::tuple<Ts...>; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 151 | |
tzik | 9ca30219 | 2016-02-11 10:24:45 | [diff] [blame] | 152 | using std::get; |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 153 | |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 154 | // Tuple types ---------------------------------------------------------------- |
| 155 | // |
| 156 | // Allows for selection of ValueTuple/RefTuple/ParamTuple without needing the |
| 157 | // definitions of class types the tuple takes as parameters. |
| 158 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 159 | template <typename T> |
| 160 | struct TupleTypes; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 161 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 162 | template <typename... Ts> |
| 163 | struct TupleTypes<Tuple<Ts...>> { |
| 164 | using ValueTuple = Tuple<typename TupleTraits<Ts>::ValueType...>; |
| 165 | using RefTuple = Tuple<typename TupleTraits<Ts>::RefType...>; |
| 166 | using ParamTuple = Tuple<typename TupleTraits<Ts>::ParamType...>; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 167 | }; |
| 168 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 169 | // Tuple creators ------------------------------------------------------------- |
| 170 | // |
| 171 | // Helper functions for constructing tuples while inferring the template |
| 172 | // argument types. |
| 173 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 174 | template <typename... Ts> |
| 175 | inline Tuple<Ts...> MakeTuple(const Ts&... arg) { |
| 176 | return Tuple<Ts...>(arg...); |
[email protected] | ae894519 | 2010-07-20 16:56:26 | [diff] [blame] | 177 | } |
| 178 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 179 | // The following set of helpers make what Boost refers to as "Tiers" - a tuple |
| 180 | // of references. |
| 181 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 182 | template <typename... Ts> |
| 183 | inline Tuple<Ts&...> MakeRefTuple(Ts&... arg) { |
| 184 | return Tuple<Ts&...>(arg...); |
[email protected] | ae894519 | 2010-07-20 16:56:26 | [diff] [blame] | 185 | } |
| 186 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 187 | // Dispatchers ---------------------------------------------------------------- |
| 188 | // |
| 189 | // Helper functions that call the given method on an object, with the unpacked |
| 190 | // tuple arguments. Notice that they all have the same number of arguments, |
| 191 | // so you need only write: |
| 192 | // DispatchToMethod(object, &Object::method, args); |
| 193 | // This is very useful for templated dispatchers, since they don't need to know |
| 194 | // what type |args| is. |
| 195 | |
| 196 | // Non-Static Dispatchers with no out params. |
| 197 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 198 | template <typename ObjT, typename Method, typename... Ts, size_t... Ns> |
tzik | 1ea87e3a | 2016-02-16 04:56:31 | [diff] [blame^] | 199 | inline void DispatchToMethodImpl(const ObjT& obj, |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 200 | Method method, |
| 201 | const Tuple<Ts...>& arg, |
| 202 | IndexSequence<Ns...>) { |
| 203 | (obj->*method)(base::internal::UnwrapTraits<Ts>::Unwrap(get<Ns>(arg))...); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 204 | } |
| 205 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 206 | template <typename ObjT, typename Method, typename... Ts> |
tzik | 1ea87e3a | 2016-02-16 04:56:31 | [diff] [blame^] | 207 | inline void DispatchToMethod(const ObjT& obj, |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 208 | Method method, |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 209 | const Tuple<Ts...>& arg) { |
| 210 | DispatchToMethodImpl(obj, method, arg, MakeIndexSequence<sizeof...(Ts)>()); |
[email protected] | fa685ff | 2011-02-17 19:47:13 | [diff] [blame] | 211 | } |
| 212 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 213 | // Static Dispatchers with no out params. |
| 214 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 215 | template <typename Function, typename... Ts, size_t... Ns> |
| 216 | inline void DispatchToFunctionImpl(Function function, |
| 217 | const Tuple<Ts...>& arg, |
| 218 | IndexSequence<Ns...>) { |
| 219 | (*function)(base::internal::UnwrapTraits<Ts>::Unwrap(get<Ns>(arg))...); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 220 | } |
| 221 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 222 | template <typename Function, typename... Ts> |
| 223 | inline void DispatchToFunction(Function function, const Tuple<Ts...>& arg) { |
| 224 | DispatchToFunctionImpl(function, arg, MakeIndexSequence<sizeof...(Ts)>()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 225 | } |
| 226 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 227 | // Dispatchers with out parameters. |
| 228 | |
| 229 | template <typename ObjT, |
| 230 | typename Method, |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 231 | typename... InTs, |
| 232 | typename... OutTs, |
| 233 | size_t... InNs, |
| 234 | size_t... OutNs> |
tzik | 1ea87e3a | 2016-02-16 04:56:31 | [diff] [blame^] | 235 | inline void DispatchToMethodImpl(const ObjT& obj, |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 236 | Method method, |
| 237 | const Tuple<InTs...>& in, |
| 238 | Tuple<OutTs...>* out, |
| 239 | IndexSequence<InNs...>, |
| 240 | IndexSequence<OutNs...>) { |
| 241 | (obj->*method)(base::internal::UnwrapTraits<InTs>::Unwrap(get<InNs>(in))..., |
| 242 | &get<OutNs>(*out)...); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 243 | } |
| 244 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 245 | template <typename ObjT, typename Method, typename... InTs, typename... OutTs> |
tzik | 1ea87e3a | 2016-02-16 04:56:31 | [diff] [blame^] | 246 | inline void DispatchToMethod(const ObjT& obj, |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 247 | Method method, |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 248 | const Tuple<InTs...>& in, |
| 249 | Tuple<OutTs...>* out) { |
| 250 | DispatchToMethodImpl(obj, method, in, out, |
| 251 | MakeIndexSequence<sizeof...(InTs)>(), |
| 252 | MakeIndexSequence<sizeof...(OutTs)>()); |
[email protected] | 8a2820a | 2008-10-09 21:58:05 | [diff] [blame] | 253 | } |
| 254 | |
brettw | d5ca2bc | 2015-05-29 22:15:47 | [diff] [blame] | 255 | } // namespace base |
| 256 | |
tfarina | a3116351 | 2015-05-13 22:10:15 | [diff] [blame] | 257 | #endif // BASE_TUPLE_H_ |