blob: 41c6a00191971e4eabfd2aa6dbf6d3308a6b0eca [file] [log] [blame]
oshima@chromium.orgcc654912011-09-28 09:33:59 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botf003cfe2008-08-24 09:55:55 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
mdempskyf705f9f2014-12-10 20:22:23 +09005// 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.
deanm@google.com96aac0a2008-08-25 22:42:07 +090010//
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.
mdempskyf705f9f2014-12-10 20:22:23 +090016// Tuple<int, const char*> tuple_a(1, "wee");
17// Tuple<int, const char*> tuple_b = MakeTuple(1, "wee");
deanm@google.com96aac0a2008-08-25 22:42:07 +090018//
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
initial.commit3f4a7322008-07-27 06:49:38 +090028#ifndef BASE_TUPLE_H__
29#define BASE_TUPLE_H__
30
rsleevi@chromium.org274c7222013-06-27 21:48:02 +090031#include "base/bind_helpers.h"
32
mdempsky0c1d6ed2014-12-10 12:10:59 +090033// Index sequences
34//
35// Minimal clone of the similarly-named C++14 functionality.
36
37template <size_t...>
38struct IndexSequence {};
39
40template <size_t... Ns>
41struct MakeIndexSequenceImpl;
42
brucedawson864de222015-01-22 07:02:16 +090043#if defined(_PREFAST_) && defined(OS_WIN)
44
45// Work around VC++ 2013 /analyze internal compiler error:
46// https://connect.microsoft.com/VisualStudio/feedback/details/1053626
47
48template <> struct MakeIndexSequenceImpl<0> {
49 using Type = IndexSequence<>;
50};
51template <> struct MakeIndexSequenceImpl<1> {
52 using Type = IndexSequence<0>;
53};
54template <> struct MakeIndexSequenceImpl<2> {
55 using Type = IndexSequence<0,1>;
56};
57template <> struct MakeIndexSequenceImpl<3> {
58 using Type = IndexSequence<0,1,2>;
59};
60template <> struct MakeIndexSequenceImpl<4> {
61 using Type = IndexSequence<0,1,2,3>;
62};
63template <> struct MakeIndexSequenceImpl<5> {
64 using Type = IndexSequence<0,1,2,3,4>;
65};
66template <> struct MakeIndexSequenceImpl<6> {
67 using Type = IndexSequence<0,1,2,3,4,5>;
68};
69template <> struct MakeIndexSequenceImpl<7> {
70 using Type = IndexSequence<0,1,2,3,4,5,6>;
71};
72template <> struct MakeIndexSequenceImpl<8> {
73 using Type = IndexSequence<0,1,2,3,4,5,6,7>;
74};
75template <> struct MakeIndexSequenceImpl<9> {
76 using Type = IndexSequence<0,1,2,3,4,5,6,7,8>;
77};
78template <> struct MakeIndexSequenceImpl<10> {
79 using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9>;
80};
81template <> struct MakeIndexSequenceImpl<11> {
82 using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9,10>;
83};
84
85#else // defined(WIN) && defined(_PREFAST_)
86
mdempsky0c1d6ed2014-12-10 12:10:59 +090087template <size_t... Ns>
88struct MakeIndexSequenceImpl<0, Ns...> {
89 using Type = IndexSequence<Ns...>;
90};
91
92template <size_t N, size_t... Ns>
mdempskyf705f9f2014-12-10 20:22:23 +090093struct MakeIndexSequenceImpl<N, Ns...>
94 : MakeIndexSequenceImpl<N - 1, N - 1, Ns...> {};
mdempsky0c1d6ed2014-12-10 12:10:59 +090095
brucedawson864de222015-01-22 07:02:16 +090096#endif // defined(WIN) && defined(_PREFAST_)
97
mdempsky0c1d6ed2014-12-10 12:10:59 +090098template <size_t N>
99using MakeIndexSequence = typename MakeIndexSequenceImpl<N>::Type;
100
initial.commit3f4a7322008-07-27 06:49:38 +0900101// Traits ----------------------------------------------------------------------
102//
103// A simple traits class for tuple arguments.
104//
105// ValueType: the bare, nonref version of a type (same as the type for nonrefs).
106// RefType: the ref version of a type (same as the type for refs).
107// ParamType: what type to pass to functions (refs should not be constified).
108
109template <class P>
110struct TupleTraits {
111 typedef P ValueType;
112 typedef P& RefType;
113 typedef const P& ParamType;
114};
115
116template <class P>
117struct TupleTraits<P&> {
118 typedef P ValueType;
119 typedef P& RefType;
120 typedef P& ParamType;
121};
122
123// Tuple -----------------------------------------------------------------------
124//
125// This set of classes is useful for bundling 0 or more heterogeneous data types
126// into a single variable. The advantage of this is that it greatly simplifies
127// function objects that need to take an arbitrary number of parameters; see
128// RunnableMethod and IPC::MessageWithTuple.
129//
mdempskyf705f9f2014-12-10 20:22:23 +0900130// Tuple<> is supplied to act as a 'void' type. It can be used, for example,
deanm@google.comff4b7292008-08-21 19:58:08 +0900131// when dispatching to a function that accepts no arguments (see the
132// Dispatchers below).
mdempskyf705f9f2014-12-10 20:22:23 +0900133// Tuple<A> is rarely useful. One such use is when A is non-const ref that you
initial.commit3f4a7322008-07-27 06:49:38 +0900134// want filled by the dispatchee, and the tuple is merely a container for that
135// output (a "tier"). See MakeRefTuple and its usages.
136
mdempskyf705f9f2014-12-10 20:22:23 +0900137template <typename IxSeq, typename... Ts>
138struct TupleBaseImpl;
mdempsky0c1d6ed2014-12-10 12:10:59 +0900139template <typename... Ts>
mdempskyf705f9f2014-12-10 20:22:23 +0900140using TupleBase = TupleBaseImpl<MakeIndexSequence<sizeof...(Ts)>, Ts...>;
141template <size_t N, typename T>
142struct TupleLeaf;
initial.commit3f4a7322008-07-27 06:49:38 +0900143
mdempskyf705f9f2014-12-10 20:22:23 +0900144template <typename... Ts>
145struct Tuple : TupleBase<Ts...> {
146 Tuple() : TupleBase<Ts...>() {}
147 explicit Tuple(typename TupleTraits<Ts>::ParamType... args)
148 : TupleBase<Ts...>(args...) {}
149};
150
151// Avoids ambiguity between Tuple's two constructors.
mdempsky0c1d6ed2014-12-10 12:10:59 +0900152template <>
153struct Tuple<> {};
154
mdempskyf705f9f2014-12-10 20:22:23 +0900155template <size_t... Ns, typename... Ts>
156struct TupleBaseImpl<IndexSequence<Ns...>, Ts...> : TupleLeaf<Ns, Ts>... {
157 TupleBaseImpl() : TupleLeaf<Ns, Ts>()... {}
158 explicit TupleBaseImpl(typename TupleTraits<Ts>::ParamType... args)
159 : TupleLeaf<Ns, Ts>(args)... {}
initial.commit3f4a7322008-07-27 06:49:38 +0900160};
161
mdempskyf705f9f2014-12-10 20:22:23 +0900162template <size_t N, typename T>
163struct TupleLeaf {
mdempsky00c97232014-12-16 08:26:13 +0900164 TupleLeaf() {}
mdempskyf705f9f2014-12-10 20:22:23 +0900165 explicit TupleLeaf(typename TupleTraits<T>::ParamType x) : x(x) {}
initial.commit3f4a7322008-07-27 06:49:38 +0900166
mdempskyf705f9f2014-12-10 20:22:23 +0900167 T& get() { return x; }
168 const T& get() const { return x; }
initial.commit3f4a7322008-07-27 06:49:38 +0900169
mdempskyf705f9f2014-12-10 20:22:23 +0900170 T x;
initial.commit3f4a7322008-07-27 06:49:38 +0900171};
172
mdempsky0c1d6ed2014-12-10 12:10:59 +0900173// Tuple getters --------------------------------------------------------------
mdempskyf705f9f2014-12-10 20:22:23 +0900174//
175// Allows accessing an arbitrary tuple element by index.
176//
177// Example usage:
178// Tuple<int, double> t2;
179// get<0>(t2) = 42;
180// get<1>(t2) = 3.14;
mdempsky0c1d6ed2014-12-10 12:10:59 +0900181
mdempskyf705f9f2014-12-10 20:22:23 +0900182template <size_t I, typename T>
183T& get(TupleLeaf<I, T>& leaf) {
184 return leaf.get();
mdempsky0c1d6ed2014-12-10 12:10:59 +0900185}
186
mdempskyf705f9f2014-12-10 20:22:23 +0900187template <size_t I, typename T>
188const T& get(const TupleLeaf<I, T>& leaf) {
189 return leaf.get();
mdempsky0c1d6ed2014-12-10 12:10:59 +0900190}
191
erg@google.come6ffcb52010-08-18 03:38:24 +0900192// Tuple types ----------------------------------------------------------------
193//
194// Allows for selection of ValueTuple/RefTuple/ParamTuple without needing the
195// definitions of class types the tuple takes as parameters.
196
mdempsky0c1d6ed2014-12-10 12:10:59 +0900197template <typename T>
198struct TupleTypes;
erg@google.come6ffcb52010-08-18 03:38:24 +0900199
mdempsky0c1d6ed2014-12-10 12:10:59 +0900200template <typename... Ts>
201struct TupleTypes<Tuple<Ts...>> {
202 using ValueTuple = Tuple<typename TupleTraits<Ts>::ValueType...>;
203 using RefTuple = Tuple<typename TupleTraits<Ts>::RefType...>;
204 using ParamTuple = Tuple<typename TupleTraits<Ts>::ParamType...>;
erg@google.come6ffcb52010-08-18 03:38:24 +0900205};
206
initial.commit3f4a7322008-07-27 06:49:38 +0900207// Tuple creators -------------------------------------------------------------
208//
209// Helper functions for constructing tuples while inferring the template
210// argument types.
211
mdempsky0c1d6ed2014-12-10 12:10:59 +0900212template <typename... Ts>
213inline Tuple<Ts...> MakeTuple(const Ts&... arg) {
214 return Tuple<Ts...>(arg...);
phajdan.jr@chromium.org5a468fc2010-07-21 01:56:26 +0900215}
216
initial.commit3f4a7322008-07-27 06:49:38 +0900217// The following set of helpers make what Boost refers to as "Tiers" - a tuple
218// of references.
219
mdempsky0c1d6ed2014-12-10 12:10:59 +0900220template <typename... Ts>
221inline Tuple<Ts&...> MakeRefTuple(Ts&... arg) {
222 return Tuple<Ts&...>(arg...);
phajdan.jr@chromium.org5a468fc2010-07-21 01:56:26 +0900223}
224
initial.commit3f4a7322008-07-27 06:49:38 +0900225// Dispatchers ----------------------------------------------------------------
226//
227// Helper functions that call the given method on an object, with the unpacked
228// tuple arguments. Notice that they all have the same number of arguments,
229// so you need only write:
230// DispatchToMethod(object, &Object::method, args);
231// This is very useful for templated dispatchers, since they don't need to know
232// what type |args| is.
233
234// Non-Static Dispatchers with no out params.
235
mdempsky0c1d6ed2014-12-10 12:10:59 +0900236template <typename ObjT, typename Method, typename A>
initial.commit3f4a7322008-07-27 06:49:38 +0900237inline void DispatchToMethod(ObjT* obj, Method method, const A& arg) {
rsleevi@chromium.org274c7222013-06-27 21:48:02 +0900238 (obj->*method)(base::internal::UnwrapTraits<A>::Unwrap(arg));
initial.commit3f4a7322008-07-27 06:49:38 +0900239}
240
mdempsky0c1d6ed2014-12-10 12:10:59 +0900241template <typename ObjT, typename Method, typename... Ts, size_t... Ns>
242inline void DispatchToMethodImpl(ObjT* obj,
243 Method method,
244 const Tuple<Ts...>& arg,
245 IndexSequence<Ns...>) {
246 (obj->*method)(base::internal::UnwrapTraits<Ts>::Unwrap(get<Ns>(arg))...);
initial.commit3f4a7322008-07-27 06:49:38 +0900247}
248
mdempsky0c1d6ed2014-12-10 12:10:59 +0900249template <typename ObjT, typename Method, typename... Ts>
maruel@chromium.org8fe7adc2009-03-04 00:01:12 +0900250inline void DispatchToMethod(ObjT* obj,
251 Method method,
mdempsky0c1d6ed2014-12-10 12:10:59 +0900252 const Tuple<Ts...>& arg) {
253 DispatchToMethodImpl(obj, method, arg, MakeIndexSequence<sizeof...(Ts)>());
noelutz@google.comdd96ed82011-02-18 04:47:13 +0900254}
255
initial.commit3f4a7322008-07-27 06:49:38 +0900256// Static Dispatchers with no out params.
257
mdempsky0c1d6ed2014-12-10 12:10:59 +0900258template <typename Function, typename A>
259inline void DispatchToMethod(Function function, const A& arg) {
260 (*function)(base::internal::UnwrapTraits<A>::Unwrap(arg));
initial.commit3f4a7322008-07-27 06:49:38 +0900261}
262
mdempsky0c1d6ed2014-12-10 12:10:59 +0900263template <typename Function, typename... Ts, size_t... Ns>
264inline void DispatchToFunctionImpl(Function function,
265 const Tuple<Ts...>& arg,
266 IndexSequence<Ns...>) {
267 (*function)(base::internal::UnwrapTraits<Ts>::Unwrap(get<Ns>(arg))...);
initial.commit3f4a7322008-07-27 06:49:38 +0900268}
269
mdempsky0c1d6ed2014-12-10 12:10:59 +0900270template <typename Function, typename... Ts>
271inline void DispatchToFunction(Function function, const Tuple<Ts...>& arg) {
272 DispatchToFunctionImpl(function, arg, MakeIndexSequence<sizeof...(Ts)>());
initial.commit3f4a7322008-07-27 06:49:38 +0900273}
274
mdempsky0c1d6ed2014-12-10 12:10:59 +0900275// Dispatchers with out parameters.
276
277template <typename ObjT,
278 typename Method,
279 typename In,
280 typename... OutTs,
281 size_t... OutNs>
282inline void DispatchToMethodImpl(ObjT* obj,
283 Method method,
284 const In& in,
285 Tuple<OutTs...>* out,
286 IndexSequence<OutNs...>) {
287 (obj->*method)(base::internal::UnwrapTraits<In>::Unwrap(in),
288 &get<OutNs>(*out)...);
initial.commit3f4a7322008-07-27 06:49:38 +0900289}
290
mdempsky0c1d6ed2014-12-10 12:10:59 +0900291template <typename ObjT, typename Method, typename In, typename... OutTs>
maruel@chromium.org8fe7adc2009-03-04 00:01:12 +0900292inline void DispatchToMethod(ObjT* obj,
293 Method method,
mdempsky0c1d6ed2014-12-10 12:10:59 +0900294 const In& in,
295 Tuple<OutTs...>* out) {
296 DispatchToMethodImpl(obj, method, in, out,
297 MakeIndexSequence<sizeof...(OutTs)>());
initial.commit3f4a7322008-07-27 06:49:38 +0900298}
299
mdempsky0c1d6ed2014-12-10 12:10:59 +0900300template <typename ObjT,
301 typename Method,
302 typename... InTs,
303 typename... OutTs,
304 size_t... InNs,
305 size_t... OutNs>
306inline void DispatchToMethodImpl(ObjT* obj,
307 Method method,
308 const Tuple<InTs...>& in,
309 Tuple<OutTs...>* out,
310 IndexSequence<InNs...>,
311 IndexSequence<OutNs...>) {
312 (obj->*method)(base::internal::UnwrapTraits<InTs>::Unwrap(get<InNs>(in))...,
313 &get<OutNs>(*out)...);
initial.commit3f4a7322008-07-27 06:49:38 +0900314}
315
mdempsky0c1d6ed2014-12-10 12:10:59 +0900316template <typename ObjT, typename Method, typename... InTs, typename... OutTs>
maruel@chromium.org8fe7adc2009-03-04 00:01:12 +0900317inline void DispatchToMethod(ObjT* obj,
318 Method method,
mdempsky0c1d6ed2014-12-10 12:10:59 +0900319 const Tuple<InTs...>& in,
320 Tuple<OutTs...>* out) {
321 DispatchToMethodImpl(obj, method, in, out,
322 MakeIndexSequence<sizeof...(InTs)>(),
323 MakeIndexSequence<sizeof...(OutTs)>());
ojan@google.com38355092008-10-10 06:58:05 +0900324}
325
initial.commit3f4a7322008-07-27 06:49:38 +0900326#endif // BASE_TUPLE_H__