blob: 885413f7570e980dbc4c4a2caa4108b2594b8f1a [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
initial.commitd7cae122008-07-26 21:49:3828#ifndef BASE_TUPLE_H__
29#define BASE_TUPLE_H__
30
[email protected]58ae6302013-06-27 12:48:0231#include "base/bind_helpers.h"
32
mdempsky6e7f6152014-12-10 03:10:5933// 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>
mdempsky06c626a2014-12-10 11:22:2349struct MakeIndexSequenceImpl<N, Ns...>
50 : MakeIndexSequenceImpl<N - 1, N - 1, Ns...> {};
mdempsky6e7f6152014-12-10 03:10:5951
52template <size_t N>
53using MakeIndexSequence = typename MakeIndexSequenceImpl<N>::Type;
54
initial.commitd7cae122008-07-26 21:49:3855// 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//
mdempsky06c626a2014-12-10 11:22:2384// Tuple<> is supplied to act as a 'void' type. It can be used, for example,
[email protected]458711982008-08-21 10:58:0885// when dispatching to a function that accepts no arguments (see the
86// Dispatchers below).
mdempsky06c626a2014-12-10 11:22:2387// Tuple<A> is rarely useful. One such use is when A is non-const ref that you
initial.commitd7cae122008-07-26 21:49:3888// want filled by the dispatchee, and the tuple is merely a container for that
89// output (a "tier"). See MakeRefTuple and its usages.
90
mdempsky06c626a2014-12-10 11:22:2391template <typename IxSeq, typename... Ts>
92struct TupleBaseImpl;
mdempsky6e7f6152014-12-10 03:10:5993template <typename... Ts>
mdempsky06c626a2014-12-10 11:22:2394using TupleBase = TupleBaseImpl<MakeIndexSequence<sizeof...(Ts)>, Ts...>;
95template <size_t N, typename T>
96struct TupleLeaf;
initial.commitd7cae122008-07-26 21:49:3897
mdempsky06c626a2014-12-10 11:22:2398template <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.
mdempsky6e7f6152014-12-10 03:10:59106template <>
107struct Tuple<> {};
108
mdempsky06c626a2014-12-10 11:22:23109template <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.commitd7cae122008-07-26 21:49:38114};
115
mdempsky06c626a2014-12-10 11:22:23116template <size_t N, typename T>
117struct TupleLeaf {
118 TupleLeaf() : x() {}
119 explicit TupleLeaf(typename TupleTraits<T>::ParamType x) : x(x) {}
initial.commitd7cae122008-07-26 21:49:38120
mdempsky06c626a2014-12-10 11:22:23121 T& get() { return x; }
122 const T& get() const { return x; }
initial.commitd7cae122008-07-26 21:49:38123
mdempsky06c626a2014-12-10 11:22:23124 T x;
initial.commitd7cae122008-07-26 21:49:38125};
126
mdempsky06c626a2014-12-10 11:22:23127// 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.commitd7cae122008-07-26 21:49:38129
mdempsky06c626a2014-12-10 11:22:23130#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.commitd7cae122008-07-26 21:49:38141
mdempsky06c626a2014-12-10 11:22:23142DEFINE_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.commitd7cae122008-07-26 21:49:38150
mdempsky06c626a2014-12-10 11:22:23151#undef DEFINE_TUPLE_LEAF
[email protected]ae8945192010-07-20 16:56:26152
mdempsky6e7f6152014-12-10 03:10:59153// Deprecated compat aliases
mdempsky06c626a2014-12-10 11:22:23154// TODO(mdempsky): Update users to just use Tuple instead (crbug.com/440675).
mdempsky6e7f6152014-12-10 03:10:59155
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
mdempsky6e7f6152014-12-10 03:10:59192// Tuple getters --------------------------------------------------------------
mdempsky06c626a2014-12-10 11:22:23193//
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;
mdempsky6e7f6152014-12-10 03:10:59200
mdempsky06c626a2014-12-10 11:22:23201template <size_t I, typename T>
202T& get(TupleLeaf<I, T>& leaf) {
203 return leaf.get();
mdempsky6e7f6152014-12-10 03:10:59204}
205
mdempsky06c626a2014-12-10 11:22:23206template <size_t I, typename T>
207const T& get(const TupleLeaf<I, T>& leaf) {
208 return leaf.get();
mdempsky6e7f6152014-12-10 03:10:59209}
210
[email protected]7a4de7a62010-08-17 18:38:24211// 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
mdempsky6e7f6152014-12-10 03:10:59216template <typename T>
217struct TupleTypes;
[email protected]7a4de7a62010-08-17 18:38:24218
mdempsky6e7f6152014-12-10 03:10:59219template <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...>;
[email protected]7a4de7a62010-08-17 18:38:24224};
225
initial.commitd7cae122008-07-26 21:49:38226// Tuple creators -------------------------------------------------------------
227//
228// Helper functions for constructing tuples while inferring the template
229// argument types.
230
mdempsky6e7f6152014-12-10 03:10:59231template <typename... Ts>
232inline Tuple<Ts...> MakeTuple(const Ts&... arg) {
233 return Tuple<Ts...>(arg...);
[email protected]ae8945192010-07-20 16:56:26234}
235
initial.commitd7cae122008-07-26 21:49:38236// The following set of helpers make what Boost refers to as "Tiers" - a tuple
237// of references.
238
mdempsky6e7f6152014-12-10 03:10:59239template <typename... Ts>
240inline Tuple<Ts&...> MakeRefTuple(Ts&... arg) {
241 return Tuple<Ts&...>(arg...);
[email protected]ae8945192010-07-20 16:56:26242}
243
initial.commitd7cae122008-07-26 21:49:38244// 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
mdempsky6e7f6152014-12-10 03:10:59255template <typename ObjT, typename Method, typename A>
initial.commitd7cae122008-07-26 21:49:38256inline void DispatchToMethod(ObjT* obj, Method method, const A& arg) {
[email protected]58ae6302013-06-27 12:48:02257 (obj->*method)(base::internal::UnwrapTraits<A>::Unwrap(arg));
initial.commitd7cae122008-07-26 21:49:38258}
259
mdempsky6e7f6152014-12-10 03:10:59260template <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.commitd7cae122008-07-26 21:49:38266}
267
mdempsky6e7f6152014-12-10 03:10:59268template <typename ObjT, typename Method, typename... Ts>
[email protected]52a261f2009-03-03 15:01:12269inline void DispatchToMethod(ObjT* obj,
270 Method method,
mdempsky6e7f6152014-12-10 03:10:59271 const Tuple<Ts...>& arg) {
272 DispatchToMethodImpl(obj, method, arg, MakeIndexSequence<sizeof...(Ts)>());
[email protected]fa685ff2011-02-17 19:47:13273}
274
initial.commitd7cae122008-07-26 21:49:38275// Static Dispatchers with no out params.
276
mdempsky6e7f6152014-12-10 03:10:59277template <typename Function, typename A>
278inline void DispatchToMethod(Function function, const A& arg) {
279 (*function)(base::internal::UnwrapTraits<A>::Unwrap(arg));
initial.commitd7cae122008-07-26 21:49:38280}
281
mdempsky6e7f6152014-12-10 03:10:59282template <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.commitd7cae122008-07-26 21:49:38287}
288
mdempsky6e7f6152014-12-10 03:10:59289template <typename Function, typename... Ts>
290inline void DispatchToFunction(Function function, const Tuple<Ts...>& arg) {
291 DispatchToFunctionImpl(function, arg, MakeIndexSequence<sizeof...(Ts)>());
initial.commitd7cae122008-07-26 21:49:38292}
293
mdempsky6e7f6152014-12-10 03:10:59294// 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.commitd7cae122008-07-26 21:49:38308}
309
mdempsky6e7f6152014-12-10 03:10:59310template <typename ObjT, typename Method, typename In, typename... OutTs>
[email protected]52a261f2009-03-03 15:01:12311inline void DispatchToMethod(ObjT* obj,
312 Method method,
mdempsky6e7f6152014-12-10 03:10:59313 const In& in,
314 Tuple<OutTs...>* out) {
315 DispatchToMethodImpl(obj, method, in, out,
316 MakeIndexSequence<sizeof...(OutTs)>());
initial.commitd7cae122008-07-26 21:49:38317}
318
mdempsky6e7f6152014-12-10 03:10:59319template <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.commitd7cae122008-07-26 21:49:38333}
334
mdempsky6e7f6152014-12-10 03:10:59335template <typename ObjT, typename Method, typename... InTs, typename... OutTs>
[email protected]52a261f2009-03-03 15:01:12336inline void DispatchToMethod(ObjT* obj,
337 Method method,
mdempsky6e7f6152014-12-10 03:10:59338 const Tuple<InTs...>& in,
339 Tuple<OutTs...>* out) {
340 DispatchToMethodImpl(obj, method, in, out,
341 MakeIndexSequence<sizeof...(InTs)>(),
342 MakeIndexSequence<sizeof...(OutTs)>());
[email protected]8a2820a2008-10-09 21:58:05343}
344
initial.commitd7cae122008-07-26 21:49:38345#endif // BASE_TUPLE_H__