blob: 88c3075f5f880ed55a45475e023d016359cd3120 [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};
brucedawson8b052562015-04-21 03:20:37 +090084template <> struct MakeIndexSequenceImpl<12> {
85 using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9,10,11>;
86};
87template <> struct MakeIndexSequenceImpl<13> {
88 using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9,10,11,12>;
89};
brucedawson864de222015-01-22 07:02:16 +090090
91#else // defined(WIN) && defined(_PREFAST_)
92
mdempsky0c1d6ed2014-12-10 12:10:59 +090093template <size_t... Ns>
94struct MakeIndexSequenceImpl<0, Ns...> {
95 using Type = IndexSequence<Ns...>;
96};
97
98template <size_t N, size_t... Ns>
mdempskyf705f9f2014-12-10 20:22:23 +090099struct MakeIndexSequenceImpl<N, Ns...>
100 : MakeIndexSequenceImpl<N - 1, N - 1, Ns...> {};
mdempsky0c1d6ed2014-12-10 12:10:59 +0900101
brucedawson864de222015-01-22 07:02:16 +0900102#endif // defined(WIN) && defined(_PREFAST_)
103
mdempsky0c1d6ed2014-12-10 12:10:59 +0900104template <size_t N>
105using MakeIndexSequence = typename MakeIndexSequenceImpl<N>::Type;
106
initial.commit3f4a7322008-07-27 06:49:38 +0900107// Traits ----------------------------------------------------------------------
108//
109// A simple traits class for tuple arguments.
110//
111// ValueType: the bare, nonref version of a type (same as the type for nonrefs).
112// RefType: the ref version of a type (same as the type for refs).
113// ParamType: what type to pass to functions (refs should not be constified).
114
115template <class P>
116struct TupleTraits {
117 typedef P ValueType;
118 typedef P& RefType;
119 typedef const P& ParamType;
120};
121
122template <class P>
123struct TupleTraits<P&> {
124 typedef P ValueType;
125 typedef P& RefType;
126 typedef P& ParamType;
127};
128
129// Tuple -----------------------------------------------------------------------
130//
131// This set of classes is useful for bundling 0 or more heterogeneous data types
132// into a single variable. The advantage of this is that it greatly simplifies
133// function objects that need to take an arbitrary number of parameters; see
134// RunnableMethod and IPC::MessageWithTuple.
135//
mdempskyf705f9f2014-12-10 20:22:23 +0900136// Tuple<> is supplied to act as a 'void' type. It can be used, for example,
deanm@google.comff4b7292008-08-21 19:58:08 +0900137// when dispatching to a function that accepts no arguments (see the
138// Dispatchers below).
mdempskyf705f9f2014-12-10 20:22:23 +0900139// Tuple<A> is rarely useful. One such use is when A is non-const ref that you
initial.commit3f4a7322008-07-27 06:49:38 +0900140// want filled by the dispatchee, and the tuple is merely a container for that
141// output (a "tier"). See MakeRefTuple and its usages.
142
mdempskyf705f9f2014-12-10 20:22:23 +0900143template <typename IxSeq, typename... Ts>
144struct TupleBaseImpl;
mdempsky0c1d6ed2014-12-10 12:10:59 +0900145template <typename... Ts>
mdempskyf705f9f2014-12-10 20:22:23 +0900146using TupleBase = TupleBaseImpl<MakeIndexSequence<sizeof...(Ts)>, Ts...>;
147template <size_t N, typename T>
148struct TupleLeaf;
initial.commit3f4a7322008-07-27 06:49:38 +0900149
mdempskyf705f9f2014-12-10 20:22:23 +0900150template <typename... Ts>
151struct Tuple : TupleBase<Ts...> {
152 Tuple() : TupleBase<Ts...>() {}
153 explicit Tuple(typename TupleTraits<Ts>::ParamType... args)
154 : TupleBase<Ts...>(args...) {}
155};
156
157// Avoids ambiguity between Tuple's two constructors.
mdempsky0c1d6ed2014-12-10 12:10:59 +0900158template <>
159struct Tuple<> {};
160
mdempskyf705f9f2014-12-10 20:22:23 +0900161template <size_t... Ns, typename... Ts>
162struct TupleBaseImpl<IndexSequence<Ns...>, Ts...> : TupleLeaf<Ns, Ts>... {
163 TupleBaseImpl() : TupleLeaf<Ns, Ts>()... {}
164 explicit TupleBaseImpl(typename TupleTraits<Ts>::ParamType... args)
165 : TupleLeaf<Ns, Ts>(args)... {}
initial.commit3f4a7322008-07-27 06:49:38 +0900166};
167
mdempskyf705f9f2014-12-10 20:22:23 +0900168template <size_t N, typename T>
169struct TupleLeaf {
mdempsky00c97232014-12-16 08:26:13 +0900170 TupleLeaf() {}
mdempskyf705f9f2014-12-10 20:22:23 +0900171 explicit TupleLeaf(typename TupleTraits<T>::ParamType x) : x(x) {}
initial.commit3f4a7322008-07-27 06:49:38 +0900172
mdempskyf705f9f2014-12-10 20:22:23 +0900173 T& get() { return x; }
174 const T& get() const { return x; }
initial.commit3f4a7322008-07-27 06:49:38 +0900175
mdempskyf705f9f2014-12-10 20:22:23 +0900176 T x;
initial.commit3f4a7322008-07-27 06:49:38 +0900177};
178
mdempsky0c1d6ed2014-12-10 12:10:59 +0900179// Tuple getters --------------------------------------------------------------
mdempskyf705f9f2014-12-10 20:22:23 +0900180//
181// Allows accessing an arbitrary tuple element by index.
182//
183// Example usage:
184// Tuple<int, double> t2;
185// get<0>(t2) = 42;
186// get<1>(t2) = 3.14;
mdempsky0c1d6ed2014-12-10 12:10:59 +0900187
mdempskyf705f9f2014-12-10 20:22:23 +0900188template <size_t I, typename T>
189T& get(TupleLeaf<I, T>& leaf) {
190 return leaf.get();
mdempsky0c1d6ed2014-12-10 12:10:59 +0900191}
192
mdempskyf705f9f2014-12-10 20:22:23 +0900193template <size_t I, typename T>
194const T& get(const TupleLeaf<I, T>& leaf) {
195 return leaf.get();
mdempsky0c1d6ed2014-12-10 12:10:59 +0900196}
197
erg@google.come6ffcb52010-08-18 03:38:24 +0900198// Tuple types ----------------------------------------------------------------
199//
200// Allows for selection of ValueTuple/RefTuple/ParamTuple without needing the
201// definitions of class types the tuple takes as parameters.
202
mdempsky0c1d6ed2014-12-10 12:10:59 +0900203template <typename T>
204struct TupleTypes;
erg@google.come6ffcb52010-08-18 03:38:24 +0900205
mdempsky0c1d6ed2014-12-10 12:10:59 +0900206template <typename... Ts>
207struct TupleTypes<Tuple<Ts...>> {
208 using ValueTuple = Tuple<typename TupleTraits<Ts>::ValueType...>;
209 using RefTuple = Tuple<typename TupleTraits<Ts>::RefType...>;
210 using ParamTuple = Tuple<typename TupleTraits<Ts>::ParamType...>;
erg@google.come6ffcb52010-08-18 03:38:24 +0900211};
212
initial.commit3f4a7322008-07-27 06:49:38 +0900213// Tuple creators -------------------------------------------------------------
214//
215// Helper functions for constructing tuples while inferring the template
216// argument types.
217
mdempsky0c1d6ed2014-12-10 12:10:59 +0900218template <typename... Ts>
219inline Tuple<Ts...> MakeTuple(const Ts&... arg) {
220 return Tuple<Ts...>(arg...);
phajdan.jr@chromium.org5a468fc2010-07-21 01:56:26 +0900221}
222
initial.commit3f4a7322008-07-27 06:49:38 +0900223// The following set of helpers make what Boost refers to as "Tiers" - a tuple
224// of references.
225
mdempsky0c1d6ed2014-12-10 12:10:59 +0900226template <typename... Ts>
227inline Tuple<Ts&...> MakeRefTuple(Ts&... arg) {
228 return Tuple<Ts&...>(arg...);
phajdan.jr@chromium.org5a468fc2010-07-21 01:56:26 +0900229}
230
initial.commit3f4a7322008-07-27 06:49:38 +0900231// Dispatchers ----------------------------------------------------------------
232//
233// Helper functions that call the given method on an object, with the unpacked
234// tuple arguments. Notice that they all have the same number of arguments,
235// so you need only write:
236// DispatchToMethod(object, &Object::method, args);
237// This is very useful for templated dispatchers, since they don't need to know
238// what type |args| is.
239
240// Non-Static Dispatchers with no out params.
241
mdempsky0c1d6ed2014-12-10 12:10:59 +0900242template <typename ObjT, typename Method, typename A>
initial.commit3f4a7322008-07-27 06:49:38 +0900243inline void DispatchToMethod(ObjT* obj, Method method, const A& arg) {
rsleevi@chromium.org274c7222013-06-27 21:48:02 +0900244 (obj->*method)(base::internal::UnwrapTraits<A>::Unwrap(arg));
initial.commit3f4a7322008-07-27 06:49:38 +0900245}
246
mdempsky0c1d6ed2014-12-10 12:10:59 +0900247template <typename ObjT, typename Method, typename... Ts, size_t... Ns>
248inline void DispatchToMethodImpl(ObjT* obj,
249 Method method,
250 const Tuple<Ts...>& arg,
251 IndexSequence<Ns...>) {
252 (obj->*method)(base::internal::UnwrapTraits<Ts>::Unwrap(get<Ns>(arg))...);
initial.commit3f4a7322008-07-27 06:49:38 +0900253}
254
mdempsky0c1d6ed2014-12-10 12:10:59 +0900255template <typename ObjT, typename Method, typename... Ts>
maruel@chromium.org8fe7adc2009-03-04 00:01:12 +0900256inline void DispatchToMethod(ObjT* obj,
257 Method method,
mdempsky0c1d6ed2014-12-10 12:10:59 +0900258 const Tuple<Ts...>& arg) {
259 DispatchToMethodImpl(obj, method, arg, MakeIndexSequence<sizeof...(Ts)>());
noelutz@google.comdd96ed82011-02-18 04:47:13 +0900260}
261
initial.commit3f4a7322008-07-27 06:49:38 +0900262// Static Dispatchers with no out params.
263
mdempsky0c1d6ed2014-12-10 12:10:59 +0900264template <typename Function, typename A>
265inline void DispatchToMethod(Function function, const A& arg) {
266 (*function)(base::internal::UnwrapTraits<A>::Unwrap(arg));
initial.commit3f4a7322008-07-27 06:49:38 +0900267}
268
mdempsky0c1d6ed2014-12-10 12:10:59 +0900269template <typename Function, typename... Ts, size_t... Ns>
270inline void DispatchToFunctionImpl(Function function,
271 const Tuple<Ts...>& arg,
272 IndexSequence<Ns...>) {
273 (*function)(base::internal::UnwrapTraits<Ts>::Unwrap(get<Ns>(arg))...);
initial.commit3f4a7322008-07-27 06:49:38 +0900274}
275
mdempsky0c1d6ed2014-12-10 12:10:59 +0900276template <typename Function, typename... Ts>
277inline void DispatchToFunction(Function function, const Tuple<Ts...>& arg) {
278 DispatchToFunctionImpl(function, arg, MakeIndexSequence<sizeof...(Ts)>());
initial.commit3f4a7322008-07-27 06:49:38 +0900279}
280
mdempsky0c1d6ed2014-12-10 12:10:59 +0900281// Dispatchers with out parameters.
282
283template <typename ObjT,
284 typename Method,
285 typename In,
286 typename... OutTs,
287 size_t... OutNs>
288inline void DispatchToMethodImpl(ObjT* obj,
289 Method method,
290 const In& in,
291 Tuple<OutTs...>* out,
292 IndexSequence<OutNs...>) {
293 (obj->*method)(base::internal::UnwrapTraits<In>::Unwrap(in),
294 &get<OutNs>(*out)...);
initial.commit3f4a7322008-07-27 06:49:38 +0900295}
296
mdempsky0c1d6ed2014-12-10 12:10:59 +0900297template <typename ObjT, typename Method, typename In, typename... OutTs>
maruel@chromium.org8fe7adc2009-03-04 00:01:12 +0900298inline void DispatchToMethod(ObjT* obj,
299 Method method,
mdempsky0c1d6ed2014-12-10 12:10:59 +0900300 const In& in,
301 Tuple<OutTs...>* out) {
302 DispatchToMethodImpl(obj, method, in, out,
303 MakeIndexSequence<sizeof...(OutTs)>());
initial.commit3f4a7322008-07-27 06:49:38 +0900304}
305
mdempsky0c1d6ed2014-12-10 12:10:59 +0900306template <typename ObjT,
307 typename Method,
308 typename... InTs,
309 typename... OutTs,
310 size_t... InNs,
311 size_t... OutNs>
312inline void DispatchToMethodImpl(ObjT* obj,
313 Method method,
314 const Tuple<InTs...>& in,
315 Tuple<OutTs...>* out,
316 IndexSequence<InNs...>,
317 IndexSequence<OutNs...>) {
318 (obj->*method)(base::internal::UnwrapTraits<InTs>::Unwrap(get<InNs>(in))...,
319 &get<OutNs>(*out)...);
initial.commit3f4a7322008-07-27 06:49:38 +0900320}
321
mdempsky0c1d6ed2014-12-10 12:10:59 +0900322template <typename ObjT, typename Method, typename... InTs, typename... OutTs>
maruel@chromium.org8fe7adc2009-03-04 00:01:12 +0900323inline void DispatchToMethod(ObjT* obj,
324 Method method,
mdempsky0c1d6ed2014-12-10 12:10:59 +0900325 const Tuple<InTs...>& in,
326 Tuple<OutTs...>* out) {
327 DispatchToMethodImpl(obj, method, in, out,
328 MakeIndexSequence<sizeof...(InTs)>(),
329 MakeIndexSequence<sizeof...(OutTs)>());
ojan@google.com38355092008-10-10 06:58:05 +0900330}
331
initial.commit3f4a7322008-07-27 06:49:38 +0900332#endif // BASE_TUPLE_H__