[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> |
Jeremy Roman | 84956fa | 2017-08-16 15:55:20 | [diff] [blame] | 30 | #include <utility> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 31 | |
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 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 36 | // Dispatchers ---------------------------------------------------------------- |
| 37 | // |
| 38 | // Helper functions that call the given method on an object, with the unpacked |
| 39 | // tuple arguments. Notice that they all have the same number of arguments, |
| 40 | // so you need only write: |
| 41 | // DispatchToMethod(object, &Object::method, args); |
| 42 | // This is very useful for templated dispatchers, since they don't need to know |
| 43 | // what type |args| is. |
| 44 | |
| 45 | // Non-Static Dispatchers with no out params. |
| 46 | |
tzik | 2387a825 | 2016-08-25 13:57:14 | [diff] [blame] | 47 | template <typename ObjT, typename Method, typename Tuple, size_t... Ns> |
tzik | 1ea87e3a | 2016-02-16 04:56:31 | [diff] [blame] | 48 | inline void DispatchToMethodImpl(const ObjT& obj, |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 49 | Method method, |
tzik | 2387a825 | 2016-08-25 13:57:14 | [diff] [blame] | 50 | Tuple&& args, |
Jeremy Roman | 84956fa | 2017-08-16 15:55:20 | [diff] [blame] | 51 | std::index_sequence<Ns...>) { |
tzik | f7c4757 | 2017-04-05 21:45:03 | [diff] [blame] | 52 | (obj->*method)(std::get<Ns>(std::forward<Tuple>(args))...); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 53 | } |
| 54 | |
tzik | 2387a825 | 2016-08-25 13:57:14 | [diff] [blame] | 55 | template <typename ObjT, typename Method, typename Tuple> |
tzik | 1ea87e3a | 2016-02-16 04:56:31 | [diff] [blame] | 56 | inline void DispatchToMethod(const ObjT& obj, |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 57 | Method method, |
tzik | 2387a825 | 2016-08-25 13:57:14 | [diff] [blame] | 58 | Tuple&& args) { |
tzik | f32dc971 | 2017-10-07 00:37:12 | [diff] [blame] | 59 | constexpr size_t size = std::tuple_size<std::decay_t<Tuple>>::value; |
tzik | 2387a825 | 2016-08-25 13:57:14 | [diff] [blame] | 60 | DispatchToMethodImpl(obj, method, std::forward<Tuple>(args), |
tzik | f32dc971 | 2017-10-07 00:37:12 | [diff] [blame] | 61 | std::make_index_sequence<size>()); |
[email protected] | fa685ff | 2011-02-17 19:47:13 | [diff] [blame] | 62 | } |
| 63 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 64 | // Static Dispatchers with no out params. |
| 65 | |
tzik | 2387a825 | 2016-08-25 13:57:14 | [diff] [blame] | 66 | template <typename Function, typename Tuple, size_t... Ns> |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 67 | inline void DispatchToFunctionImpl(Function function, |
tzik | 2387a825 | 2016-08-25 13:57:14 | [diff] [blame] | 68 | Tuple&& args, |
Jeremy Roman | 84956fa | 2017-08-16 15:55:20 | [diff] [blame] | 69 | std::index_sequence<Ns...>) { |
tzik | f7c4757 | 2017-04-05 21:45:03 | [diff] [blame] | 70 | (*function)(std::get<Ns>(std::forward<Tuple>(args))...); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 71 | } |
| 72 | |
tzik | 2387a825 | 2016-08-25 13:57:14 | [diff] [blame] | 73 | template <typename Function, typename Tuple> |
| 74 | inline void DispatchToFunction(Function function, Tuple&& args) { |
tzik | f32dc971 | 2017-10-07 00:37:12 | [diff] [blame] | 75 | constexpr size_t size = std::tuple_size<std::decay_t<Tuple>>::value; |
tzik | 2387a825 | 2016-08-25 13:57:14 | [diff] [blame] | 76 | DispatchToFunctionImpl(function, std::forward<Tuple>(args), |
tzik | f32dc971 | 2017-10-07 00:37:12 | [diff] [blame] | 77 | std::make_index_sequence<size>()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 78 | } |
| 79 | |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 80 | // Dispatchers with out parameters. |
| 81 | |
| 82 | template <typename ObjT, |
| 83 | typename Method, |
tzik | 2387a825 | 2016-08-25 13:57:14 | [diff] [blame] | 84 | typename InTuple, |
| 85 | typename OutTuple, |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 86 | size_t... InNs, |
| 87 | size_t... OutNs> |
tzik | 1ea87e3a | 2016-02-16 04:56:31 | [diff] [blame] | 88 | inline void DispatchToMethodImpl(const ObjT& obj, |
mdempsky | 6e7f615 | 2014-12-10 03:10:59 | [diff] [blame] | 89 | Method method, |
tzik | 2387a825 | 2016-08-25 13:57:14 | [diff] [blame] | 90 | InTuple&& in, |
| 91 | OutTuple* out, |
Jeremy Roman | 84956fa | 2017-08-16 15:55:20 | [diff] [blame] | 92 | std::index_sequence<InNs...>, |
| 93 | std::index_sequence<OutNs...>) { |
tzik | f7c4757 | 2017-04-05 21:45:03 | [diff] [blame] | 94 | (obj->*method)(std::get<InNs>(std::forward<InTuple>(in))..., |
tzik | 1068f1be | 2016-06-03 07:25:20 | [diff] [blame] | 95 | &std::get<OutNs>(*out)...); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 96 | } |
| 97 | |
tzik | 2387a825 | 2016-08-25 13:57:14 | [diff] [blame] | 98 | template <typename ObjT, typename Method, typename InTuple, typename OutTuple> |
tzik | 1ea87e3a | 2016-02-16 04:56:31 | [diff] [blame] | 99 | inline void DispatchToMethod(const ObjT& obj, |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 100 | Method method, |
tzik | 2387a825 | 2016-08-25 13:57:14 | [diff] [blame] | 101 | InTuple&& in, |
| 102 | OutTuple* out) { |
tzik | f32dc971 | 2017-10-07 00:37:12 | [diff] [blame] | 103 | constexpr size_t in_size = std::tuple_size<std::decay_t<InTuple>>::value; |
| 104 | constexpr size_t out_size = std::tuple_size<OutTuple>::value; |
tzik | 2387a825 | 2016-08-25 13:57:14 | [diff] [blame] | 105 | DispatchToMethodImpl(obj, method, std::forward<InTuple>(in), out, |
tzik | f32dc971 | 2017-10-07 00:37:12 | [diff] [blame] | 106 | std::make_index_sequence<in_size>(), |
| 107 | std::make_index_sequence<out_size>()); |
[email protected] | 8a2820a | 2008-10-09 21:58:05 | [diff] [blame] | 108 | } |
| 109 | |
brettw | d5ca2bc | 2015-05-29 22:15:47 | [diff] [blame] | 110 | } // namespace base |
| 111 | |
tfarina | a3116351 | 2015-05-13 22:10:15 | [diff] [blame] | 112 | #endif // BASE_TUPLE_H_ |