blob: 4408074335b3bd2c28b5ee5719a0b63ba262971c [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 {
mdempsky00c97232014-12-16 08:26:13 +0900118 TupleLeaf() {}
mdempskyf705f9f2014-12-10 20:22:23 +0900119 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
mdempsky0c1d6ed2014-12-10 12:10:59 +0900127// Tuple getters --------------------------------------------------------------
mdempskyf705f9f2014-12-10 20:22:23 +0900128//
129// Allows accessing an arbitrary tuple element by index.
130//
131// Example usage:
132// Tuple<int, double> t2;
133// get<0>(t2) = 42;
134// get<1>(t2) = 3.14;
mdempsky0c1d6ed2014-12-10 12:10:59 +0900135
mdempskyf705f9f2014-12-10 20:22:23 +0900136template <size_t I, typename T>
137T& get(TupleLeaf<I, T>& leaf) {
138 return leaf.get();
mdempsky0c1d6ed2014-12-10 12:10:59 +0900139}
140
mdempskyf705f9f2014-12-10 20:22:23 +0900141template <size_t I, typename T>
142const T& get(const TupleLeaf<I, T>& leaf) {
143 return leaf.get();
mdempsky0c1d6ed2014-12-10 12:10:59 +0900144}
145
erg@google.come6ffcb52010-08-18 03:38:24 +0900146// Tuple types ----------------------------------------------------------------
147//
148// Allows for selection of ValueTuple/RefTuple/ParamTuple without needing the
149// definitions of class types the tuple takes as parameters.
150
mdempsky0c1d6ed2014-12-10 12:10:59 +0900151template <typename T>
152struct TupleTypes;
erg@google.come6ffcb52010-08-18 03:38:24 +0900153
mdempsky0c1d6ed2014-12-10 12:10:59 +0900154template <typename... Ts>
155struct TupleTypes<Tuple<Ts...>> {
156 using ValueTuple = Tuple<typename TupleTraits<Ts>::ValueType...>;
157 using RefTuple = Tuple<typename TupleTraits<Ts>::RefType...>;
158 using ParamTuple = Tuple<typename TupleTraits<Ts>::ParamType...>;
erg@google.come6ffcb52010-08-18 03:38:24 +0900159};
160
initial.commit3f4a7322008-07-27 06:49:38 +0900161// Tuple creators -------------------------------------------------------------
162//
163// Helper functions for constructing tuples while inferring the template
164// argument types.
165
mdempsky0c1d6ed2014-12-10 12:10:59 +0900166template <typename... Ts>
167inline Tuple<Ts...> MakeTuple(const Ts&... arg) {
168 return Tuple<Ts...>(arg...);
phajdan.jr@chromium.org5a468fc2010-07-21 01:56:26 +0900169}
170
initial.commit3f4a7322008-07-27 06:49:38 +0900171// The following set of helpers make what Boost refers to as "Tiers" - a tuple
172// of references.
173
mdempsky0c1d6ed2014-12-10 12:10:59 +0900174template <typename... Ts>
175inline Tuple<Ts&...> MakeRefTuple(Ts&... arg) {
176 return Tuple<Ts&...>(arg...);
phajdan.jr@chromium.org5a468fc2010-07-21 01:56:26 +0900177}
178
initial.commit3f4a7322008-07-27 06:49:38 +0900179// Dispatchers ----------------------------------------------------------------
180//
181// Helper functions that call the given method on an object, with the unpacked
182// tuple arguments. Notice that they all have the same number of arguments,
183// so you need only write:
184// DispatchToMethod(object, &Object::method, args);
185// This is very useful for templated dispatchers, since they don't need to know
186// what type |args| is.
187
188// Non-Static Dispatchers with no out params.
189
mdempsky0c1d6ed2014-12-10 12:10:59 +0900190template <typename ObjT, typename Method, typename A>
initial.commit3f4a7322008-07-27 06:49:38 +0900191inline void DispatchToMethod(ObjT* obj, Method method, const A& arg) {
rsleevi@chromium.org274c7222013-06-27 21:48:02 +0900192 (obj->*method)(base::internal::UnwrapTraits<A>::Unwrap(arg));
initial.commit3f4a7322008-07-27 06:49:38 +0900193}
194
mdempsky0c1d6ed2014-12-10 12:10:59 +0900195template <typename ObjT, typename Method, typename... Ts, size_t... Ns>
196inline void DispatchToMethodImpl(ObjT* obj,
197 Method method,
198 const Tuple<Ts...>& arg,
199 IndexSequence<Ns...>) {
200 (obj->*method)(base::internal::UnwrapTraits<Ts>::Unwrap(get<Ns>(arg))...);
initial.commit3f4a7322008-07-27 06:49:38 +0900201}
202
mdempsky0c1d6ed2014-12-10 12:10:59 +0900203template <typename ObjT, typename Method, typename... Ts>
maruel@chromium.org8fe7adc2009-03-04 00:01:12 +0900204inline void DispatchToMethod(ObjT* obj,
205 Method method,
mdempsky0c1d6ed2014-12-10 12:10:59 +0900206 const Tuple<Ts...>& arg) {
207 DispatchToMethodImpl(obj, method, arg, MakeIndexSequence<sizeof...(Ts)>());
noelutz@google.comdd96ed82011-02-18 04:47:13 +0900208}
209
initial.commit3f4a7322008-07-27 06:49:38 +0900210// Static Dispatchers with no out params.
211
mdempsky0c1d6ed2014-12-10 12:10:59 +0900212template <typename Function, typename A>
213inline void DispatchToMethod(Function function, const A& arg) {
214 (*function)(base::internal::UnwrapTraits<A>::Unwrap(arg));
initial.commit3f4a7322008-07-27 06:49:38 +0900215}
216
mdempsky0c1d6ed2014-12-10 12:10:59 +0900217template <typename Function, typename... Ts, size_t... Ns>
218inline void DispatchToFunctionImpl(Function function,
219 const Tuple<Ts...>& arg,
220 IndexSequence<Ns...>) {
221 (*function)(base::internal::UnwrapTraits<Ts>::Unwrap(get<Ns>(arg))...);
initial.commit3f4a7322008-07-27 06:49:38 +0900222}
223
mdempsky0c1d6ed2014-12-10 12:10:59 +0900224template <typename Function, typename... Ts>
225inline void DispatchToFunction(Function function, const Tuple<Ts...>& arg) {
226 DispatchToFunctionImpl(function, arg, MakeIndexSequence<sizeof...(Ts)>());
initial.commit3f4a7322008-07-27 06:49:38 +0900227}
228
mdempsky0c1d6ed2014-12-10 12:10:59 +0900229// Dispatchers with out parameters.
230
231template <typename ObjT,
232 typename Method,
233 typename In,
234 typename... OutTs,
235 size_t... OutNs>
236inline void DispatchToMethodImpl(ObjT* obj,
237 Method method,
238 const In& in,
239 Tuple<OutTs...>* out,
240 IndexSequence<OutNs...>) {
241 (obj->*method)(base::internal::UnwrapTraits<In>::Unwrap(in),
242 &get<OutNs>(*out)...);
initial.commit3f4a7322008-07-27 06:49:38 +0900243}
244
mdempsky0c1d6ed2014-12-10 12:10:59 +0900245template <typename ObjT, typename Method, typename In, typename... OutTs>
maruel@chromium.org8fe7adc2009-03-04 00:01:12 +0900246inline void DispatchToMethod(ObjT* obj,
247 Method method,
mdempsky0c1d6ed2014-12-10 12:10:59 +0900248 const In& in,
249 Tuple<OutTs...>* out) {
250 DispatchToMethodImpl(obj, method, in, out,
251 MakeIndexSequence<sizeof...(OutTs)>());
initial.commit3f4a7322008-07-27 06:49:38 +0900252}
253
mdempsky0c1d6ed2014-12-10 12:10:59 +0900254template <typename ObjT,
255 typename Method,
256 typename... InTs,
257 typename... OutTs,
258 size_t... InNs,
259 size_t... OutNs>
260inline void DispatchToMethodImpl(ObjT* obj,
261 Method method,
262 const Tuple<InTs...>& in,
263 Tuple<OutTs...>* out,
264 IndexSequence<InNs...>,
265 IndexSequence<OutNs...>) {
266 (obj->*method)(base::internal::UnwrapTraits<InTs>::Unwrap(get<InNs>(in))...,
267 &get<OutNs>(*out)...);
initial.commit3f4a7322008-07-27 06:49:38 +0900268}
269
mdempsky0c1d6ed2014-12-10 12:10:59 +0900270template <typename ObjT, typename Method, typename... InTs, typename... OutTs>
maruel@chromium.org8fe7adc2009-03-04 00:01:12 +0900271inline void DispatchToMethod(ObjT* obj,
272 Method method,
mdempsky0c1d6ed2014-12-10 12:10:59 +0900273 const Tuple<InTs...>& in,
274 Tuple<OutTs...>* out) {
275 DispatchToMethodImpl(obj, method, in, out,
276 MakeIndexSequence<sizeof...(InTs)>(),
277 MakeIndexSequence<sizeof...(OutTs)>());
ojan@google.com38355092008-10-10 06:58:05 +0900278}
279
initial.commit3f4a7322008-07-27 06:49:38 +0900280#endif // BASE_TUPLE_H__