[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 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 31 | #include "build/build_config.h" |
[email protected] | 58ae630 | 2013-06-27 12:48:02 | [diff] [blame] | 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 | |
| 45 | template <size_t... Ns> |
| 46 | struct MakeIndexSequenceImpl<0, Ns...> { |
| 47 | using Type = IndexSequence<Ns...>; |
| 48 | }; |
| 49 | |
| 50 | template <size_t N, size_t... Ns> |
mdempsky | 06c626a | 2014-12-10 11:22:23 | [diff] [blame] | 51 | struct MakeIndexSequenceImpl<N, Ns...> |
| 52 | : MakeIndexSequenceImpl<N - 1, N - 1, Ns...> {}; |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 53 | |
| 54 | template <size_t N> |
| 55 | using MakeIndexSequence = typename MakeIndexSequenceImpl<N>::Type; |
| 56 | |
tzik | 2387a825 | 2016-08-25 13:57:14 | [diff] [blame] | 57 | template <typename T> |
| 58 | using MakeIndexSequenceForTuple = |
| 59 | MakeIndexSequence<std::tuple_size<typename std::decay<T>::type>::value>; |
| 60 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 61 | // Dispatchers ---------------------------------------------------------------- |
| 62 | // |
| 63 | // Helper functions that call the given method on an object, with the unpacked |
| 64 | // tuple arguments. Notice that they all have the same number of arguments, |
| 65 | // so you need only write: |
| 66 | // DispatchToMethod(object, &Object::method, args); |
| 67 | // This is very useful for templated dispatchers, since they don't need to know |
| 68 | // what type |args| is. |
| 69 | |
| 70 | // Non-Static Dispatchers with no out params. |
| 71 | |
tzik | 2387a825 | 2016-08-25 13:57:14 | [diff] [blame] | 72 | template <typename ObjT, typename Method, typename Tuple, size_t... Ns> |
tzik | 1ea87e3a | 2016-02-16 04:56:31 | [diff] [blame] | 73 | inline void DispatchToMethodImpl(const ObjT& obj, |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 74 | Method method, |
tzik | 2387a825 | 2016-08-25 13:57:14 | [diff] [blame] | 75 | Tuple&& args, |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 76 | IndexSequence<Ns...>) { |
tzik | f7c4757 | 2017-04-05 21:45:03 | [diff] [blame] | 77 | (obj->*method)(std::get<Ns>(std::forward<Tuple>(args))...); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 78 | } |
| 79 | |
tzik | 2387a825 | 2016-08-25 13:57:14 | [diff] [blame] | 80 | template <typename ObjT, typename Method, typename Tuple> |
tzik | 1ea87e3a | 2016-02-16 04:56:31 | [diff] [blame] | 81 | inline void DispatchToMethod(const ObjT& obj, |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 82 | Method method, |
tzik | 2387a825 | 2016-08-25 13:57:14 | [diff] [blame] | 83 | Tuple&& args) { |
| 84 | DispatchToMethodImpl(obj, method, std::forward<Tuple>(args), |
| 85 | MakeIndexSequenceForTuple<Tuple>()); |
[email protected] | fa685ff | 2011-02-17 19:47:13 | [diff] [blame] | 86 | } |
| 87 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 88 | // Static Dispatchers with no out params. |
| 89 | |
tzik | 2387a825 | 2016-08-25 13:57:14 | [diff] [blame] | 90 | template <typename Function, typename Tuple, size_t... Ns> |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 91 | inline void DispatchToFunctionImpl(Function function, |
tzik | 2387a825 | 2016-08-25 13:57:14 | [diff] [blame] | 92 | Tuple&& args, |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 93 | IndexSequence<Ns...>) { |
tzik | f7c4757 | 2017-04-05 21:45:03 | [diff] [blame] | 94 | (*function)(std::get<Ns>(std::forward<Tuple>(args))...); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 95 | } |
| 96 | |
tzik | 2387a825 | 2016-08-25 13:57:14 | [diff] [blame] | 97 | template <typename Function, typename Tuple> |
| 98 | inline void DispatchToFunction(Function function, Tuple&& args) { |
| 99 | DispatchToFunctionImpl(function, std::forward<Tuple>(args), |
| 100 | MakeIndexSequenceForTuple<Tuple>()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 101 | } |
| 102 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 103 | // Dispatchers with out parameters. |
| 104 | |
| 105 | template <typename ObjT, |
| 106 | typename Method, |
tzik | 2387a825 | 2016-08-25 13:57:14 | [diff] [blame] | 107 | typename InTuple, |
| 108 | typename OutTuple, |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 109 | size_t... InNs, |
| 110 | size_t... OutNs> |
tzik | 1ea87e3a | 2016-02-16 04:56:31 | [diff] [blame] | 111 | inline void DispatchToMethodImpl(const ObjT& obj, |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 112 | Method method, |
tzik | 2387a825 | 2016-08-25 13:57:14 | [diff] [blame] | 113 | InTuple&& in, |
| 114 | OutTuple* out, |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 115 | IndexSequence<InNs...>, |
| 116 | IndexSequence<OutNs...>) { |
tzik | f7c4757 | 2017-04-05 21:45:03 | [diff] [blame] | 117 | (obj->*method)(std::get<InNs>(std::forward<InTuple>(in))..., |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame] | 118 | &std::get<OutNs>(*out)...); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 119 | } |
| 120 | |
tzik | 2387a825 | 2016-08-25 13:57:14 | [diff] [blame] | 121 | template <typename ObjT, typename Method, typename InTuple, typename OutTuple> |
tzik | 1ea87e3a | 2016-02-16 04:56:31 | [diff] [blame] | 122 | inline void DispatchToMethod(const ObjT& obj, |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 123 | Method method, |
tzik | 2387a825 | 2016-08-25 13:57:14 | [diff] [blame] | 124 | InTuple&& in, |
| 125 | OutTuple* out) { |
| 126 | DispatchToMethodImpl(obj, method, std::forward<InTuple>(in), out, |
| 127 | MakeIndexSequenceForTuple<InTuple>(), |
| 128 | MakeIndexSequenceForTuple<OutTuple>()); |
[email protected] | 8a2820a | 2008-10-09 21:58:05 | [diff] [blame] | 129 | } |
| 130 | |
brettw | d5ca2bc | 2015-05-29 22:15:47 | [diff] [blame] | 131 | } // namespace base |
| 132 | |
tfarina | a3116351 | 2015-05-13 22:10:15 | [diff] [blame] | 133 | #endif // BASE_TUPLE_H_ |