blob: 885413f7570e980dbc4c4a2caa4108b2594b8f1a [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
43template <size_t... Ns>
44struct MakeIndexSequenceImpl<0, Ns...> {
45 using Type = IndexSequence<Ns...>;
46};
47
48template <size_t N, size_t... Ns>
mdempskyf705f9f2014-12-10 20:22:23 +090049struct MakeIndexSequenceImpl<N, Ns...>
50 : MakeIndexSequenceImpl<N - 1, N - 1, Ns...> {};
mdempsky0c1d6ed2014-12-10 12:10:59 +090051
52template <size_t N>
53using MakeIndexSequence = typename MakeIndexSequenceImpl<N>::Type;
54
initial.commit3f4a7322008-07-27 06:49:38 +090055// Traits ----------------------------------------------------------------------
56//
57// A simple traits class for tuple arguments.
58//
59// ValueType: the bare, nonref version of a type (same as the type for nonrefs).
60// RefType: the ref version of a type (same as the type for refs).
61// ParamType: what type to pass to functions (refs should not be constified).
62
63template <class P>
64struct TupleTraits {
65 typedef P ValueType;
66 typedef P& RefType;
67 typedef const P& ParamType;
68};
69
70template <class P>
71struct TupleTraits<P&> {
72 typedef P ValueType;
73 typedef P& RefType;
74 typedef P& ParamType;
75};
76
77// Tuple -----------------------------------------------------------------------
78//
79// This set of classes is useful for bundling 0 or more heterogeneous data types
80// into a single variable. The advantage of this is that it greatly simplifies
81// function objects that need to take an arbitrary number of parameters; see
82// RunnableMethod and IPC::MessageWithTuple.
83//
mdempskyf705f9f2014-12-10 20:22:23 +090084// Tuple<> is supplied to act as a 'void' type. It can be used, for example,
deanm@google.comff4b7292008-08-21 19:58:08 +090085// when dispatching to a function that accepts no arguments (see the
86// Dispatchers below).
mdempskyf705f9f2014-12-10 20:22:23 +090087// Tuple<A> is rarely useful. One such use is when A is non-const ref that you
initial.commit3f4a7322008-07-27 06:49:38 +090088// want filled by the dispatchee, and the tuple is merely a container for that
89// output (a "tier"). See MakeRefTuple and its usages.
90
mdempskyf705f9f2014-12-10 20:22:23 +090091template <typename IxSeq, typename... Ts>
92struct TupleBaseImpl;
mdempsky0c1d6ed2014-12-10 12:10:59 +090093template <typename... Ts>
mdempskyf705f9f2014-12-10 20:22:23 +090094using TupleBase = TupleBaseImpl<MakeIndexSequence<sizeof...(Ts)>, Ts...>;
95template <size_t N, typename T>
96struct TupleLeaf;
initial.commit3f4a7322008-07-27 06:49:38 +090097
mdempskyf705f9f2014-12-10 20:22:23 +090098template <typename... Ts>
99struct Tuple : TupleBase<Ts...> {
100 Tuple() : TupleBase<Ts...>() {}
101 explicit Tuple(typename TupleTraits<Ts>::ParamType... args)
102 : TupleBase<Ts...>(args...) {}
103};
104
105// Avoids ambiguity between Tuple's two constructors.
mdempsky0c1d6ed2014-12-10 12:10:59 +0900106template <>
107struct Tuple<> {};
108
mdempskyf705f9f2014-12-10 20:22:23 +0900109template <size_t... Ns, typename... Ts>
110struct TupleBaseImpl<IndexSequence<Ns...>, Ts...> : TupleLeaf<Ns, Ts>... {
111 TupleBaseImpl() : TupleLeaf<Ns, Ts>()... {}
112 explicit TupleBaseImpl(typename TupleTraits<Ts>::ParamType... args)
113 : TupleLeaf<Ns, Ts>(args)... {}
initial.commit3f4a7322008-07-27 06:49:38 +0900114};
115
mdempskyf705f9f2014-12-10 20:22:23 +0900116template <size_t N, typename T>
117struct TupleLeaf {
118 TupleLeaf() : x() {}
119 explicit TupleLeaf(typename TupleTraits<T>::ParamType x) : x(x) {}
initial.commit3f4a7322008-07-27 06:49:38 +0900120
mdempskyf705f9f2014-12-10 20:22:23 +0900121 T& get() { return x; }
122 const T& get() const { return x; }
initial.commit3f4a7322008-07-27 06:49:38 +0900123
mdempskyf705f9f2014-12-10 20:22:23 +0900124 T x;
initial.commit3f4a7322008-07-27 06:49:38 +0900125};
126
mdempskyf705f9f2014-12-10 20:22:23 +0900127// For legacy compatibility, we name the first 8 tuple elements "a", "b", ...
128// TODO(mdempsky): Update users to use get<N>() (crbug.com/440675).
initial.commit3f4a7322008-07-27 06:49:38 +0900129
mdempskyf705f9f2014-12-10 20:22:23 +0900130#define DEFINE_TUPLE_LEAF(N, x) \
131 template <typename T> \
132 struct TupleLeaf<N, T> { \
133 TupleLeaf() : x() {} \
134 explicit TupleLeaf(typename TupleTraits<T>::ParamType x) : x(x) {} \
135 \
136 T& get() { return x; } \
137 const T& get() const { return x; } \
138 \
139 T x; \
140 }
initial.commit3f4a7322008-07-27 06:49:38 +0900141
mdempskyf705f9f2014-12-10 20:22:23 +0900142DEFINE_TUPLE_LEAF(0, a);
143DEFINE_TUPLE_LEAF(1, b);
144DEFINE_TUPLE_LEAF(2, c);
145DEFINE_TUPLE_LEAF(3, d);
146DEFINE_TUPLE_LEAF(4, e);
147DEFINE_TUPLE_LEAF(5, f);
148DEFINE_TUPLE_LEAF(6, g);
149DEFINE_TUPLE_LEAF(7, h);
initial.commit3f4a7322008-07-27 06:49:38 +0900150
mdempskyf705f9f2014-12-10 20:22:23 +0900151#undef DEFINE_TUPLE_LEAF
phajdan.jr@chromium.org5a468fc2010-07-21 01:56:26 +0900152
mdempsky0c1d6ed2014-12-10 12:10:59 +0900153// Deprecated compat aliases
mdempskyf705f9f2014-12-10 20:22:23 +0900154// TODO(mdempsky): Update users to just use Tuple instead (crbug.com/440675).
mdempsky0c1d6ed2014-12-10 12:10:59 +0900155
156using Tuple0 = Tuple<>;
157template <typename A>
158using Tuple1 = Tuple<A>;
159template <typename A, typename B>
160using Tuple2 = Tuple<A, B>;
161template <typename A, typename B, typename C>
162using Tuple3 = Tuple<A, B, C>;
163template <typename A, typename B, typename C, typename D>
164using Tuple4 = Tuple<A, B, C, D>;
165template <typename A, typename B, typename C, typename D, typename E>
166using Tuple5 = Tuple<A, B, C, D, E>;
167template <typename A,
168 typename B,
169 typename C,
170 typename D,
171 typename E,
172 typename F>
173using Tuple6 = Tuple<A, B, C, D, E, F>;
174template <typename A,
175 typename B,
176 typename C,
177 typename D,
178 typename E,
179 typename F,
180 typename G>
181using Tuple7 = Tuple<A, B, C, D, E, F, G>;
182template <typename A,
183 typename B,
184 typename C,
185 typename D,
186 typename E,
187 typename F,
188 typename G,
189 typename H>
190using Tuple8 = Tuple<A, B, C, D, E, F, G, H>;
191
mdempsky0c1d6ed2014-12-10 12:10:59 +0900192// Tuple getters --------------------------------------------------------------
mdempskyf705f9f2014-12-10 20:22:23 +0900193//
194// Allows accessing an arbitrary tuple element by index.
195//
196// Example usage:
197// Tuple<int, double> t2;
198// get<0>(t2) = 42;
199// get<1>(t2) = 3.14;
mdempsky0c1d6ed2014-12-10 12:10:59 +0900200
mdempskyf705f9f2014-12-10 20:22:23 +0900201template <size_t I, typename T>
202T& get(TupleLeaf<I, T>& leaf) {
203 return leaf.get();
mdempsky0c1d6ed2014-12-10 12:10:59 +0900204}
205
mdempskyf705f9f2014-12-10 20:22:23 +0900206template <size_t I, typename T>
207const T& get(const TupleLeaf<I, T>& leaf) {
208 return leaf.get();
mdempsky0c1d6ed2014-12-10 12:10:59 +0900209}
210
erg@google.come6ffcb52010-08-18 03:38:24 +0900211// Tuple types ----------------------------------------------------------------
212//
213// Allows for selection of ValueTuple/RefTuple/ParamTuple without needing the
214// definitions of class types the tuple takes as parameters.
215
mdempsky0c1d6ed2014-12-10 12:10:59 +0900216template <typename T>
217struct TupleTypes;
erg@google.come6ffcb52010-08-18 03:38:24 +0900218
mdempsky0c1d6ed2014-12-10 12:10:59 +0900219template <typename... Ts>
220struct TupleTypes<Tuple<Ts...>> {
221 using ValueTuple = Tuple<typename TupleTraits<Ts>::ValueType...>;
222 using RefTuple = Tuple<typename TupleTraits<Ts>::RefType...>;
223 using ParamTuple = Tuple<typename TupleTraits<Ts>::ParamType...>;
erg@google.come6ffcb52010-08-18 03:38:24 +0900224};
225
initial.commit3f4a7322008-07-27 06:49:38 +0900226// Tuple creators -------------------------------------------------------------
227//
228// Helper functions for constructing tuples while inferring the template
229// argument types.
230
mdempsky0c1d6ed2014-12-10 12:10:59 +0900231template <typename... Ts>
232inline Tuple<Ts...> MakeTuple(const Ts&... arg) {
233 return Tuple<Ts...>(arg...);
phajdan.jr@chromium.org5a468fc2010-07-21 01:56:26 +0900234}
235
initial.commit3f4a7322008-07-27 06:49:38 +0900236// The following set of helpers make what Boost refers to as "Tiers" - a tuple
237// of references.
238
mdempsky0c1d6ed2014-12-10 12:10:59 +0900239template <typename... Ts>
240inline Tuple<Ts&...> MakeRefTuple(Ts&... arg) {
241 return Tuple<Ts&...>(arg...);
phajdan.jr@chromium.org5a468fc2010-07-21 01:56:26 +0900242}
243
initial.commit3f4a7322008-07-27 06:49:38 +0900244// Dispatchers ----------------------------------------------------------------
245//
246// Helper functions that call the given method on an object, with the unpacked
247// tuple arguments. Notice that they all have the same number of arguments,
248// so you need only write:
249// DispatchToMethod(object, &Object::method, args);
250// This is very useful for templated dispatchers, since they don't need to know
251// what type |args| is.
252
253// Non-Static Dispatchers with no out params.
254
mdempsky0c1d6ed2014-12-10 12:10:59 +0900255template <typename ObjT, typename Method, typename A>
initial.commit3f4a7322008-07-27 06:49:38 +0900256inline void DispatchToMethod(ObjT* obj, Method method, const A& arg) {
rsleevi@chromium.org274c7222013-06-27 21:48:02 +0900257 (obj->*method)(base::internal::UnwrapTraits<A>::Unwrap(arg));
initial.commit3f4a7322008-07-27 06:49:38 +0900258}
259
mdempsky0c1d6ed2014-12-10 12:10:59 +0900260template <typename ObjT, typename Method, typename... Ts, size_t... Ns>
261inline void DispatchToMethodImpl(ObjT* obj,
262 Method method,
263 const Tuple<Ts...>& arg,
264 IndexSequence<Ns...>) {
265 (obj->*method)(base::internal::UnwrapTraits<Ts>::Unwrap(get<Ns>(arg))...);
initial.commit3f4a7322008-07-27 06:49:38 +0900266}
267
mdempsky0c1d6ed2014-12-10 12:10:59 +0900268template <typename ObjT, typename Method, typename... Ts>
maruel@chromium.org8fe7adc2009-03-04 00:01:12 +0900269inline void DispatchToMethod(ObjT* obj,
270 Method method,
mdempsky0c1d6ed2014-12-10 12:10:59 +0900271 const Tuple<Ts...>& arg) {
272 DispatchToMethodImpl(obj, method, arg, MakeIndexSequence<sizeof...(Ts)>());
noelutz@google.comdd96ed82011-02-18 04:47:13 +0900273}
274
initial.commit3f4a7322008-07-27 06:49:38 +0900275// Static Dispatchers with no out params.
276
mdempsky0c1d6ed2014-12-10 12:10:59 +0900277template <typename Function, typename A>
278inline void DispatchToMethod(Function function, const A& arg) {
279 (*function)(base::internal::UnwrapTraits<A>::Unwrap(arg));
initial.commit3f4a7322008-07-27 06:49:38 +0900280}
281
mdempsky0c1d6ed2014-12-10 12:10:59 +0900282template <typename Function, typename... Ts, size_t... Ns>
283inline void DispatchToFunctionImpl(Function function,
284 const Tuple<Ts...>& arg,
285 IndexSequence<Ns...>) {
286 (*function)(base::internal::UnwrapTraits<Ts>::Unwrap(get<Ns>(arg))...);
initial.commit3f4a7322008-07-27 06:49:38 +0900287}
288
mdempsky0c1d6ed2014-12-10 12:10:59 +0900289template <typename Function, typename... Ts>
290inline void DispatchToFunction(Function function, const Tuple<Ts...>& arg) {
291 DispatchToFunctionImpl(function, arg, MakeIndexSequence<sizeof...(Ts)>());
initial.commit3f4a7322008-07-27 06:49:38 +0900292}
293
mdempsky0c1d6ed2014-12-10 12:10:59 +0900294// Dispatchers with out parameters.
295
296template <typename ObjT,
297 typename Method,
298 typename In,
299 typename... OutTs,
300 size_t... OutNs>
301inline void DispatchToMethodImpl(ObjT* obj,
302 Method method,
303 const In& in,
304 Tuple<OutTs...>* out,
305 IndexSequence<OutNs...>) {
306 (obj->*method)(base::internal::UnwrapTraits<In>::Unwrap(in),
307 &get<OutNs>(*out)...);
initial.commit3f4a7322008-07-27 06:49:38 +0900308}
309
mdempsky0c1d6ed2014-12-10 12:10:59 +0900310template <typename ObjT, typename Method, typename In, typename... OutTs>
maruel@chromium.org8fe7adc2009-03-04 00:01:12 +0900311inline void DispatchToMethod(ObjT* obj,
312 Method method,
mdempsky0c1d6ed2014-12-10 12:10:59 +0900313 const In& in,
314 Tuple<OutTs...>* out) {
315 DispatchToMethodImpl(obj, method, in, out,
316 MakeIndexSequence<sizeof...(OutTs)>());
initial.commit3f4a7322008-07-27 06:49:38 +0900317}
318
mdempsky0c1d6ed2014-12-10 12:10:59 +0900319template <typename ObjT,
320 typename Method,
321 typename... InTs,
322 typename... OutTs,
323 size_t... InNs,
324 size_t... OutNs>
325inline void DispatchToMethodImpl(ObjT* obj,
326 Method method,
327 const Tuple<InTs...>& in,
328 Tuple<OutTs...>* out,
329 IndexSequence<InNs...>,
330 IndexSequence<OutNs...>) {
331 (obj->*method)(base::internal::UnwrapTraits<InTs>::Unwrap(get<InNs>(in))...,
332 &get<OutNs>(*out)...);
initial.commit3f4a7322008-07-27 06:49:38 +0900333}
334
mdempsky0c1d6ed2014-12-10 12:10:59 +0900335template <typename ObjT, typename Method, typename... InTs, typename... OutTs>
maruel@chromium.org8fe7adc2009-03-04 00:01:12 +0900336inline void DispatchToMethod(ObjT* obj,
337 Method method,
mdempsky0c1d6ed2014-12-10 12:10:59 +0900338 const Tuple<InTs...>& in,
339 Tuple<OutTs...>* out) {
340 DispatchToMethodImpl(obj, method, in, out,
341 MakeIndexSequence<sizeof...(InTs)>(),
342 MakeIndexSequence<sizeof...(OutTs)>());
ojan@google.com38355092008-10-10 06:58:05 +0900343}
344
initial.commit3f4a7322008-07-27 06:49:38 +0900345#endif // BASE_TUPLE_H__