blob: d07d38c6031726f28308ed14648b6308e3805cb0 [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
mdempsky06c626a2014-12-10 11:22:235// 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]302bdc132008-08-25 13:42:0710//
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.
mdempsky06c626a2014-12-10 11:22:2316// Tuple<int, const char*> tuple_a(1, "wee");
17// Tuple<int, const char*> tuple_b = MakeTuple(1, "wee");
[email protected]302bdc132008-08-25 13:42:0718//
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
tfarinaa31163512015-05-13 22:10:1528#ifndef BASE_TUPLE_H_
29#define BASE_TUPLE_H_
initial.commitd7cae122008-07-26 21:49:3830
avi9b6f42932015-12-26 22:15:1431#include <stddef.h>
tzik9ca302192016-02-11 10:24:4532#include <tuple>
avi9b6f42932015-12-26 22:15:1433
[email protected]58ae6302013-06-27 12:48:0234#include "base/bind_helpers.h"
avi9b6f42932015-12-26 22:15:1435#include "build/build_config.h"
[email protected]58ae6302013-06-27 12:48:0236
brettwd5ca2bc2015-05-29 22:15:4737namespace base {
38
mdempsky6e7f6152014-12-10 03:10:5939// Index sequences
40//
41// Minimal clone of the similarly-named C++14 functionality.
42
43template <size_t...>
44struct IndexSequence {};
45
46template <size_t... Ns>
47struct MakeIndexSequenceImpl;
48
brucedawsonb132ccd2015-01-21 22:02:1649#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
54template <> struct MakeIndexSequenceImpl<0> {
55 using Type = IndexSequence<>;
56};
57template <> struct MakeIndexSequenceImpl<1> {
58 using Type = IndexSequence<0>;
59};
60template <> struct MakeIndexSequenceImpl<2> {
61 using Type = IndexSequence<0,1>;
62};
63template <> struct MakeIndexSequenceImpl<3> {
64 using Type = IndexSequence<0,1,2>;
65};
66template <> struct MakeIndexSequenceImpl<4> {
67 using Type = IndexSequence<0,1,2,3>;
68};
69template <> struct MakeIndexSequenceImpl<5> {
70 using Type = IndexSequence<0,1,2,3,4>;
71};
72template <> struct MakeIndexSequenceImpl<6> {
73 using Type = IndexSequence<0,1,2,3,4,5>;
74};
75template <> struct MakeIndexSequenceImpl<7> {
76 using Type = IndexSequence<0,1,2,3,4,5,6>;
77};
78template <> struct MakeIndexSequenceImpl<8> {
79 using Type = IndexSequence<0,1,2,3,4,5,6,7>;
80};
81template <> struct MakeIndexSequenceImpl<9> {
82 using Type = IndexSequence<0,1,2,3,4,5,6,7,8>;
83};
84template <> struct MakeIndexSequenceImpl<10> {
85 using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9>;
86};
87template <> struct MakeIndexSequenceImpl<11> {
88 using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9,10>;
89};
brucedawson35709e172015-04-20 18:20:3790template <> struct MakeIndexSequenceImpl<12> {
91 using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9,10,11>;
92};
93template <> struct MakeIndexSequenceImpl<13> {
94 using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9,10,11,12>;
95};
brucedawsonb132ccd2015-01-21 22:02:1696
97#else // defined(WIN) && defined(_PREFAST_)
98
mdempsky6e7f6152014-12-10 03:10:5999template <size_t... Ns>
100struct MakeIndexSequenceImpl<0, Ns...> {
101 using Type = IndexSequence<Ns...>;
102};
103
104template <size_t N, size_t... Ns>
mdempsky06c626a2014-12-10 11:22:23105struct MakeIndexSequenceImpl<N, Ns...>
106 : MakeIndexSequenceImpl<N - 1, N - 1, Ns...> {};
mdempsky6e7f6152014-12-10 03:10:59107
brucedawsonb132ccd2015-01-21 22:02:16108#endif // defined(WIN) && defined(_PREFAST_)
109
mdempsky6e7f6152014-12-10 03:10:59110template <size_t N>
111using MakeIndexSequence = typename MakeIndexSequenceImpl<N>::Type;
112
initial.commitd7cae122008-07-26 21:49:38113// 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
121template <class P>
122struct TupleTraits {
123 typedef P ValueType;
124 typedef P& RefType;
125 typedef const P& ParamType;
126};
127
128template <class P>
129struct 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//
mdempsky06c626a2014-12-10 11:22:23142// Tuple<> is supplied to act as a 'void' type. It can be used, for example,
[email protected]458711982008-08-21 10:58:08143// when dispatching to a function that accepts no arguments (see the
144// Dispatchers below).
mdempsky06c626a2014-12-10 11:22:23145// Tuple<A> is rarely useful. One such use is when A is non-const ref that you
initial.commitd7cae122008-07-26 21:49:38146// want filled by the dispatchee, and the tuple is merely a container for that
147// output (a "tier"). See MakeRefTuple and its usages.
148
mdempsky6e7f6152014-12-10 03:10:59149template <typename... Ts>
tzik9ca302192016-02-11 10:24:45150using Tuple = std::tuple<Ts...>;
initial.commitd7cae122008-07-26 21:49:38151
tzik9ca302192016-02-11 10:24:45152using std::get;
mdempsky6e7f6152014-12-10 03:10:59153
[email protected]7a4de7a62010-08-17 18:38:24154// 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
mdempsky6e7f6152014-12-10 03:10:59159template <typename T>
160struct TupleTypes;
[email protected]7a4de7a62010-08-17 18:38:24161
mdempsky6e7f6152014-12-10 03:10:59162template <typename... Ts>
163struct 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]7a4de7a62010-08-17 18:38:24167};
168
initial.commitd7cae122008-07-26 21:49:38169// Tuple creators -------------------------------------------------------------
170//
171// Helper functions for constructing tuples while inferring the template
172// argument types.
173
mdempsky6e7f6152014-12-10 03:10:59174template <typename... Ts>
175inline Tuple<Ts...> MakeTuple(const Ts&... arg) {
176 return Tuple<Ts...>(arg...);
[email protected]ae8945192010-07-20 16:56:26177}
178
initial.commitd7cae122008-07-26 21:49:38179// The following set of helpers make what Boost refers to as "Tiers" - a tuple
180// of references.
181
mdempsky6e7f6152014-12-10 03:10:59182template <typename... Ts>
183inline Tuple<Ts&...> MakeRefTuple(Ts&... arg) {
184 return Tuple<Ts&...>(arg...);
[email protected]ae8945192010-07-20 16:56:26185}
186
initial.commitd7cae122008-07-26 21:49:38187// 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
mdempsky6e7f6152014-12-10 03:10:59198template <typename ObjT, typename Method, typename... Ts, size_t... Ns>
tzik1ea87e3a2016-02-16 04:56:31199inline void DispatchToMethodImpl(const ObjT& obj,
mdempsky6e7f6152014-12-10 03:10:59200 Method method,
201 const Tuple<Ts...>& arg,
202 IndexSequence<Ns...>) {
203 (obj->*method)(base::internal::UnwrapTraits<Ts>::Unwrap(get<Ns>(arg))...);
initial.commitd7cae122008-07-26 21:49:38204}
205
mdempsky6e7f6152014-12-10 03:10:59206template <typename ObjT, typename Method, typename... Ts>
tzik1ea87e3a2016-02-16 04:56:31207inline void DispatchToMethod(const ObjT& obj,
[email protected]52a261f2009-03-03 15:01:12208 Method method,
mdempsky6e7f6152014-12-10 03:10:59209 const Tuple<Ts...>& arg) {
210 DispatchToMethodImpl(obj, method, arg, MakeIndexSequence<sizeof...(Ts)>());
[email protected]fa685ff2011-02-17 19:47:13211}
212
initial.commitd7cae122008-07-26 21:49:38213// Static Dispatchers with no out params.
214
mdempsky6e7f6152014-12-10 03:10:59215template <typename Function, typename... Ts, size_t... Ns>
216inline void DispatchToFunctionImpl(Function function,
217 const Tuple<Ts...>& arg,
218 IndexSequence<Ns...>) {
219 (*function)(base::internal::UnwrapTraits<Ts>::Unwrap(get<Ns>(arg))...);
initial.commitd7cae122008-07-26 21:49:38220}
221
mdempsky6e7f6152014-12-10 03:10:59222template <typename Function, typename... Ts>
223inline void DispatchToFunction(Function function, const Tuple<Ts...>& arg) {
224 DispatchToFunctionImpl(function, arg, MakeIndexSequence<sizeof...(Ts)>());
initial.commitd7cae122008-07-26 21:49:38225}
226
mdempsky6e7f6152014-12-10 03:10:59227// Dispatchers with out parameters.
228
229template <typename ObjT,
230 typename Method,
mdempsky6e7f6152014-12-10 03:10:59231 typename... InTs,
232 typename... OutTs,
233 size_t... InNs,
234 size_t... OutNs>
tzik1ea87e3a2016-02-16 04:56:31235inline void DispatchToMethodImpl(const ObjT& obj,
mdempsky6e7f6152014-12-10 03:10:59236 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.commitd7cae122008-07-26 21:49:38243}
244
mdempsky6e7f6152014-12-10 03:10:59245template <typename ObjT, typename Method, typename... InTs, typename... OutTs>
tzik1ea87e3a2016-02-16 04:56:31246inline void DispatchToMethod(const ObjT& obj,
[email protected]52a261f2009-03-03 15:01:12247 Method method,
mdempsky6e7f6152014-12-10 03:10:59248 const Tuple<InTs...>& in,
249 Tuple<OutTs...>* out) {
250 DispatchToMethodImpl(obj, method, in, out,
251 MakeIndexSequence<sizeof...(InTs)>(),
252 MakeIndexSequence<sizeof...(OutTs)>());
[email protected]8a2820a2008-10-09 21:58:05253}
254
brettwd5ca2bc2015-05-29 22:15:47255} // namespace base
256
tfarinaa31163512015-05-13 22:10:15257#endif // BASE_TUPLE_H_