[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 | |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame^] | 5 | // Use std::tuple as tuple type. This file contains helper functions for |
| 6 | // working with std::tuples. |
| 7 | // The functions DispatchToMethod and DispatchToFunction take a function pointer |
| 8 | // or instance and method pointer, and unpack a tuple into arguments to the |
| 9 | // call. |
[email protected] | 302bdc13 | 2008-08-25 13:42:07 | [diff] [blame] | 10 | // |
| 11 | // Example usage: |
| 12 | // // These two methods of creating a Tuple are identical. |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame^] | 13 | // std::tuple<int, const char*> tuple_a(1, "wee"); |
| 14 | // std::tuple<int, const char*> tuple_b = std::make_tuple(1, "wee"); |
[email protected] | 302bdc13 | 2008-08-25 13:42:07 | [diff] [blame] | 15 | // |
| 16 | // void SomeFunc(int a, const char* b) { } |
| 17 | // DispatchToFunction(&SomeFunc, tuple_a); // SomeFunc(1, "wee") |
| 18 | // DispatchToFunction( |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame^] | 19 | // &SomeFunc, std::make_tuple(10, "foo")); // SomeFunc(10, "foo") |
[email protected] | 302bdc13 | 2008-08-25 13:42:07 | [diff] [blame] | 20 | // |
| 21 | // struct { void SomeMeth(int a, int b, int c) { } } foo; |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame^] | 22 | // DispatchToMethod(&foo, &Foo::SomeMeth, std::make_tuple(1, 2, 3)); |
[email protected] | 302bdc13 | 2008-08-25 13:42:07 | [diff] [blame] | 23 | // // foo->SomeMeth(1, 2, 3); |
| 24 | |
tfarina | a3116351 | 2015-05-13 22:10:15 | [diff] [blame] | 25 | #ifndef BASE_TUPLE_H_ |
| 26 | #define BASE_TUPLE_H_ |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 27 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 28 | #include <stddef.h> |
tzik | 9ca30219 | 2016-02-11 10:24:45 | [diff] [blame] | 29 | #include <tuple> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 30 | |
[email protected] | 58ae630 | 2013-06-27 12:48:02 | [diff] [blame] | 31 | #include "base/bind_helpers.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 32 | #include "build/build_config.h" |
[email protected] | 58ae630 | 2013-06-27 12:48:02 | [diff] [blame] | 33 | |
brettw | d5ca2bc | 2015-05-29 22:15:47 | [diff] [blame] | 34 | namespace base { |
| 35 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 36 | // Index sequences |
| 37 | // |
| 38 | // Minimal clone of the similarly-named C++14 functionality. |
| 39 | |
| 40 | template <size_t...> |
| 41 | struct IndexSequence {}; |
| 42 | |
| 43 | template <size_t... Ns> |
| 44 | struct MakeIndexSequenceImpl; |
| 45 | |
brucedawson | b132ccd | 2015-01-21 22:02:16 | [diff] [blame] | 46 | #if defined(_PREFAST_) && defined(OS_WIN) |
| 47 | |
| 48 | // Work around VC++ 2013 /analyze internal compiler error: |
| 49 | // https://connect.microsoft.com/VisualStudio/feedback/details/1053626 |
| 50 | |
| 51 | template <> struct MakeIndexSequenceImpl<0> { |
| 52 | using Type = IndexSequence<>; |
| 53 | }; |
| 54 | template <> struct MakeIndexSequenceImpl<1> { |
| 55 | using Type = IndexSequence<0>; |
| 56 | }; |
| 57 | template <> struct MakeIndexSequenceImpl<2> { |
| 58 | using Type = IndexSequence<0,1>; |
| 59 | }; |
| 60 | template <> struct MakeIndexSequenceImpl<3> { |
| 61 | using Type = IndexSequence<0,1,2>; |
| 62 | }; |
| 63 | template <> struct MakeIndexSequenceImpl<4> { |
| 64 | using Type = IndexSequence<0,1,2,3>; |
| 65 | }; |
| 66 | template <> struct MakeIndexSequenceImpl<5> { |
| 67 | using Type = IndexSequence<0,1,2,3,4>; |
| 68 | }; |
| 69 | template <> struct MakeIndexSequenceImpl<6> { |
| 70 | using Type = IndexSequence<0,1,2,3,4,5>; |
| 71 | }; |
| 72 | template <> struct MakeIndexSequenceImpl<7> { |
| 73 | using Type = IndexSequence<0,1,2,3,4,5,6>; |
| 74 | }; |
| 75 | template <> struct MakeIndexSequenceImpl<8> { |
| 76 | using Type = IndexSequence<0,1,2,3,4,5,6,7>; |
| 77 | }; |
| 78 | template <> struct MakeIndexSequenceImpl<9> { |
| 79 | using Type = IndexSequence<0,1,2,3,4,5,6,7,8>; |
| 80 | }; |
| 81 | template <> struct MakeIndexSequenceImpl<10> { |
| 82 | using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9>; |
| 83 | }; |
| 84 | template <> struct MakeIndexSequenceImpl<11> { |
| 85 | using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9,10>; |
| 86 | }; |
brucedawson | 35709e17 | 2015-04-20 18:20:37 | [diff] [blame] | 87 | template <> struct MakeIndexSequenceImpl<12> { |
| 88 | using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9,10,11>; |
| 89 | }; |
| 90 | template <> struct MakeIndexSequenceImpl<13> { |
| 91 | using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9,10,11,12>; |
| 92 | }; |
brucedawson | b132ccd | 2015-01-21 22:02:16 | [diff] [blame] | 93 | |
rjkroege | b1f8959 | 2016-04-16 03:34:36 | [diff] [blame] | 94 | #else // defined(OS_WIN) && defined(_PREFAST_) |
brucedawson | b132ccd | 2015-01-21 22:02:16 | [diff] [blame] | 95 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 96 | template <size_t... Ns> |
| 97 | struct MakeIndexSequenceImpl<0, Ns...> { |
| 98 | using Type = IndexSequence<Ns...>; |
| 99 | }; |
| 100 | |
| 101 | template <size_t N, size_t... Ns> |
mdempsky | 06c626a | 2014-12-10 11:22:23 | [diff] [blame] | 102 | struct MakeIndexSequenceImpl<N, Ns...> |
| 103 | : MakeIndexSequenceImpl<N - 1, N - 1, Ns...> {}; |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 104 | |
rjkroege | b1f8959 | 2016-04-16 03:34:36 | [diff] [blame] | 105 | #endif // defined(OS_WIN) && defined(_PREFAST_) |
brucedawson | b132ccd | 2015-01-21 22:02:16 | [diff] [blame] | 106 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 107 | template <size_t N> |
| 108 | using MakeIndexSequence = typename MakeIndexSequenceImpl<N>::Type; |
| 109 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 110 | // Dispatchers ---------------------------------------------------------------- |
| 111 | // |
| 112 | // Helper functions that call the given method on an object, with the unpacked |
| 113 | // tuple arguments. Notice that they all have the same number of arguments, |
| 114 | // so you need only write: |
| 115 | // DispatchToMethod(object, &Object::method, args); |
| 116 | // This is very useful for templated dispatchers, since they don't need to know |
| 117 | // what type |args| is. |
| 118 | |
| 119 | // Non-Static Dispatchers with no out params. |
| 120 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 121 | template <typename ObjT, typename Method, typename... Ts, size_t... Ns> |
tzik | 1ea87e3a | 2016-02-16 04:56:31 | [diff] [blame] | 122 | inline void DispatchToMethodImpl(const ObjT& obj, |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 123 | Method method, |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame^] | 124 | const std::tuple<Ts...>& arg, |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 125 | IndexSequence<Ns...>) { |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame^] | 126 | (obj->*method)(internal::Unwrap(std::get<Ns>(arg))...); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 127 | } |
| 128 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 129 | template <typename ObjT, typename Method, typename... Ts> |
tzik | 1ea87e3a | 2016-02-16 04:56:31 | [diff] [blame] | 130 | inline void DispatchToMethod(const ObjT& obj, |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 131 | Method method, |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame^] | 132 | const std::tuple<Ts...>& arg) { |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 133 | DispatchToMethodImpl(obj, method, arg, MakeIndexSequence<sizeof...(Ts)>()); |
[email protected] | fa685ff | 2011-02-17 19:47:13 | [diff] [blame] | 134 | } |
| 135 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 136 | // Static Dispatchers with no out params. |
| 137 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 138 | template <typename Function, typename... Ts, size_t... Ns> |
| 139 | inline void DispatchToFunctionImpl(Function function, |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame^] | 140 | const std::tuple<Ts...>& arg, |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 141 | IndexSequence<Ns...>) { |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame^] | 142 | (*function)(internal::Unwrap(std::get<Ns>(arg))...); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 143 | } |
| 144 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 145 | template <typename Function, typename... Ts> |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame^] | 146 | inline void DispatchToFunction(Function function, |
| 147 | const std::tuple<Ts...>& arg) { |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 148 | DispatchToFunctionImpl(function, arg, MakeIndexSequence<sizeof...(Ts)>()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 149 | } |
| 150 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 151 | // Dispatchers with out parameters. |
| 152 | |
| 153 | template <typename ObjT, |
| 154 | typename Method, |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 155 | typename... InTs, |
| 156 | typename... OutTs, |
| 157 | size_t... InNs, |
| 158 | size_t... OutNs> |
tzik | 1ea87e3a | 2016-02-16 04:56:31 | [diff] [blame] | 159 | inline void DispatchToMethodImpl(const ObjT& obj, |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 160 | Method method, |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame^] | 161 | const std::tuple<InTs...>& in, |
| 162 | std::tuple<OutTs...>* out, |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 163 | IndexSequence<InNs...>, |
| 164 | IndexSequence<OutNs...>) { |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame^] | 165 | (obj->*method)(internal::Unwrap(std::get<InNs>(in))..., |
| 166 | &std::get<OutNs>(*out)...); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 167 | } |
| 168 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 169 | template <typename ObjT, typename Method, typename... InTs, typename... OutTs> |
tzik | 1ea87e3a | 2016-02-16 04:56:31 | [diff] [blame] | 170 | inline void DispatchToMethod(const ObjT& obj, |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 171 | Method method, |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame^] | 172 | const std::tuple<InTs...>& in, |
| 173 | std::tuple<OutTs...>* out) { |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 174 | DispatchToMethodImpl(obj, method, in, out, |
| 175 | MakeIndexSequence<sizeof...(InTs)>(), |
| 176 | MakeIndexSequence<sizeof...(OutTs)>()); |
[email protected] | 8a2820a | 2008-10-09 21:58:05 | [diff] [blame] | 177 | } |
| 178 | |
brettw | d5ca2bc | 2015-05-29 22:15:47 | [diff] [blame] | 179 | } // namespace base |
| 180 | |
tfarina | a3116351 | 2015-05-13 22:10:15 | [diff] [blame] | 181 | #endif // BASE_TUPLE_H_ |