blob: 62344a1233916317b5ff96659fc5370daa5c4260 [file] [log] [blame]
[email protected]6b9b771c2011-09-28 00:33:591// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
tzik1068f1be2016-06-03 07:25:205// 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]302bdc132008-08-25 13:42:0710//
11// Example usage:
12// // These two methods of creating a Tuple are identical.
tzik1068f1be2016-06-03 07:25:2013// std::tuple<int, const char*> tuple_a(1, "wee");
14// std::tuple<int, const char*> tuple_b = std::make_tuple(1, "wee");
[email protected]302bdc132008-08-25 13:42:0715//
16// void SomeFunc(int a, const char* b) { }
17// DispatchToFunction(&SomeFunc, tuple_a); // SomeFunc(1, "wee")
18// DispatchToFunction(
tzik1068f1be2016-06-03 07:25:2019// &SomeFunc, std::make_tuple(10, "foo")); // SomeFunc(10, "foo")
[email protected]302bdc132008-08-25 13:42:0720//
21// struct { void SomeMeth(int a, int b, int c) { } } foo;
tzik1068f1be2016-06-03 07:25:2022// DispatchToMethod(&foo, &Foo::SomeMeth, std::make_tuple(1, 2, 3));
[email protected]302bdc132008-08-25 13:42:0723// // foo->SomeMeth(1, 2, 3);
24
tfarinaa31163512015-05-13 22:10:1525#ifndef BASE_TUPLE_H_
26#define BASE_TUPLE_H_
initial.commitd7cae122008-07-26 21:49:3827
avi9b6f42932015-12-26 22:15:1428#include <stddef.h>
tzik9ca302192016-02-11 10:24:4529#include <tuple>
avi9b6f42932015-12-26 22:15:1430
[email protected]58ae6302013-06-27 12:48:0231#include "base/bind_helpers.h"
avi9b6f42932015-12-26 22:15:1432#include "build/build_config.h"
[email protected]58ae6302013-06-27 12:48:0233
brettwd5ca2bc2015-05-29 22:15:4734namespace base {
35
mdempsky6e7f6152014-12-10 03:10:5936// Index sequences
37//
38// Minimal clone of the similarly-named C++14 functionality.
39
40template <size_t...>
41struct IndexSequence {};
42
43template <size_t... Ns>
44struct MakeIndexSequenceImpl;
45
brucedawsonb132ccd2015-01-21 22:02:1646#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
51template <> struct MakeIndexSequenceImpl<0> {
52 using Type = IndexSequence<>;
53};
54template <> struct MakeIndexSequenceImpl<1> {
55 using Type = IndexSequence<0>;
56};
57template <> struct MakeIndexSequenceImpl<2> {
58 using Type = IndexSequence<0,1>;
59};
60template <> struct MakeIndexSequenceImpl<3> {
61 using Type = IndexSequence<0,1,2>;
62};
63template <> struct MakeIndexSequenceImpl<4> {
64 using Type = IndexSequence<0,1,2,3>;
65};
66template <> struct MakeIndexSequenceImpl<5> {
67 using Type = IndexSequence<0,1,2,3,4>;
68};
69template <> struct MakeIndexSequenceImpl<6> {
70 using Type = IndexSequence<0,1,2,3,4,5>;
71};
72template <> struct MakeIndexSequenceImpl<7> {
73 using Type = IndexSequence<0,1,2,3,4,5,6>;
74};
75template <> struct MakeIndexSequenceImpl<8> {
76 using Type = IndexSequence<0,1,2,3,4,5,6,7>;
77};
78template <> struct MakeIndexSequenceImpl<9> {
79 using Type = IndexSequence<0,1,2,3,4,5,6,7,8>;
80};
81template <> struct MakeIndexSequenceImpl<10> {
82 using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9>;
83};
84template <> struct MakeIndexSequenceImpl<11> {
85 using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9,10>;
86};
brucedawson35709e172015-04-20 18:20:3787template <> struct MakeIndexSequenceImpl<12> {
88 using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9,10,11>;
89};
90template <> struct MakeIndexSequenceImpl<13> {
91 using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9,10,11,12>;
92};
brucedawsonb132ccd2015-01-21 22:02:1693
rjkroegeb1f89592016-04-16 03:34:3694#else // defined(OS_WIN) && defined(_PREFAST_)
brucedawsonb132ccd2015-01-21 22:02:1695
mdempsky6e7f6152014-12-10 03:10:5996template <size_t... Ns>
97struct MakeIndexSequenceImpl<0, Ns...> {
98 using Type = IndexSequence<Ns...>;
99};
100
101template <size_t N, size_t... Ns>
mdempsky06c626a2014-12-10 11:22:23102struct MakeIndexSequenceImpl<N, Ns...>
103 : MakeIndexSequenceImpl<N - 1, N - 1, Ns...> {};
mdempsky6e7f6152014-12-10 03:10:59104
rjkroegeb1f89592016-04-16 03:34:36105#endif // defined(OS_WIN) && defined(_PREFAST_)
brucedawsonb132ccd2015-01-21 22:02:16106
mdempsky6e7f6152014-12-10 03:10:59107template <size_t N>
108using MakeIndexSequence = typename MakeIndexSequenceImpl<N>::Type;
109
initial.commitd7cae122008-07-26 21:49:38110// 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
mdempsky6e7f6152014-12-10 03:10:59121template <typename ObjT, typename Method, typename... Ts, size_t... Ns>
tzik1ea87e3a2016-02-16 04:56:31122inline void DispatchToMethodImpl(const ObjT& obj,
mdempsky6e7f6152014-12-10 03:10:59123 Method method,
tzik1068f1be2016-06-03 07:25:20124 const std::tuple<Ts...>& arg,
mdempsky6e7f6152014-12-10 03:10:59125 IndexSequence<Ns...>) {
tzik1068f1be2016-06-03 07:25:20126 (obj->*method)(internal::Unwrap(std::get<Ns>(arg))...);
initial.commitd7cae122008-07-26 21:49:38127}
128
mdempsky6e7f6152014-12-10 03:10:59129template <typename ObjT, typename Method, typename... Ts>
tzik1ea87e3a2016-02-16 04:56:31130inline void DispatchToMethod(const ObjT& obj,
[email protected]52a261f2009-03-03 15:01:12131 Method method,
tzik1068f1be2016-06-03 07:25:20132 const std::tuple<Ts...>& arg) {
mdempsky6e7f6152014-12-10 03:10:59133 DispatchToMethodImpl(obj, method, arg, MakeIndexSequence<sizeof...(Ts)>());
[email protected]fa685ff2011-02-17 19:47:13134}
135
initial.commitd7cae122008-07-26 21:49:38136// Static Dispatchers with no out params.
137
mdempsky6e7f6152014-12-10 03:10:59138template <typename Function, typename... Ts, size_t... Ns>
139inline void DispatchToFunctionImpl(Function function,
tzik1068f1be2016-06-03 07:25:20140 const std::tuple<Ts...>& arg,
mdempsky6e7f6152014-12-10 03:10:59141 IndexSequence<Ns...>) {
tzik1068f1be2016-06-03 07:25:20142 (*function)(internal::Unwrap(std::get<Ns>(arg))...);
initial.commitd7cae122008-07-26 21:49:38143}
144
mdempsky6e7f6152014-12-10 03:10:59145template <typename Function, typename... Ts>
tzik1068f1be2016-06-03 07:25:20146inline void DispatchToFunction(Function function,
147 const std::tuple<Ts...>& arg) {
mdempsky6e7f6152014-12-10 03:10:59148 DispatchToFunctionImpl(function, arg, MakeIndexSequence<sizeof...(Ts)>());
initial.commitd7cae122008-07-26 21:49:38149}
150
mdempsky6e7f6152014-12-10 03:10:59151// Dispatchers with out parameters.
152
153template <typename ObjT,
154 typename Method,
mdempsky6e7f6152014-12-10 03:10:59155 typename... InTs,
156 typename... OutTs,
157 size_t... InNs,
158 size_t... OutNs>
tzik1ea87e3a2016-02-16 04:56:31159inline void DispatchToMethodImpl(const ObjT& obj,
mdempsky6e7f6152014-12-10 03:10:59160 Method method,
tzik1068f1be2016-06-03 07:25:20161 const std::tuple<InTs...>& in,
162 std::tuple<OutTs...>* out,
mdempsky6e7f6152014-12-10 03:10:59163 IndexSequence<InNs...>,
164 IndexSequence<OutNs...>) {
tzik1068f1be2016-06-03 07:25:20165 (obj->*method)(internal::Unwrap(std::get<InNs>(in))...,
166 &std::get<OutNs>(*out)...);
initial.commitd7cae122008-07-26 21:49:38167}
168
mdempsky6e7f6152014-12-10 03:10:59169template <typename ObjT, typename Method, typename... InTs, typename... OutTs>
tzik1ea87e3a2016-02-16 04:56:31170inline void DispatchToMethod(const ObjT& obj,
[email protected]52a261f2009-03-03 15:01:12171 Method method,
tzik1068f1be2016-06-03 07:25:20172 const std::tuple<InTs...>& in,
173 std::tuple<OutTs...>* out) {
mdempsky6e7f6152014-12-10 03:10:59174 DispatchToMethodImpl(obj, method, in, out,
175 MakeIndexSequence<sizeof...(InTs)>(),
176 MakeIndexSequence<sizeof...(OutTs)>());
[email protected]8a2820a2008-10-09 21:58:05177}
178
brettwd5ca2bc2015-05-29 22:15:47179} // namespace base
180
tfarinaa31163512015-05-13 22:10:15181#endif // BASE_TUPLE_H_