1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // 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.
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.
15 // // These two methods of creating a Tuple are identical.
16 // Tuple<int, const char*> tuple_a(1, "wee");
17 // Tuple<int, const char*> tuple_b = MakeTuple(1, "wee");
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")
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);
28 #ifndef BASE_TUPLE_H__
29 #define BASE_TUPLE_H__
31 #include "base/bind_helpers.h"
35 // Minimal clone of the similarly-named C++14 functionality.
38 struct IndexSequence
{};
40 template <size_t... Ns
>
41 struct MakeIndexSequenceImpl
;
43 #if defined(_PREFAST_) && defined(OS_WIN)
45 // Work around VC++ 2013 /analyze internal compiler error:
46 // https://connect.microsoft.com/VisualStudio/feedback/details/1053626
48 template <> struct MakeIndexSequenceImpl
<0> {
49 using Type
= IndexSequence
<>;
51 template <> struct MakeIndexSequenceImpl
<1> {
52 using Type
= IndexSequence
<0>;
54 template <> struct MakeIndexSequenceImpl
<2> {
55 using Type
= IndexSequence
<0,1>;
57 template <> struct MakeIndexSequenceImpl
<3> {
58 using Type
= IndexSequence
<0,1,2>;
60 template <> struct MakeIndexSequenceImpl
<4> {
61 using Type
= IndexSequence
<0,1,2,3>;
63 template <> struct MakeIndexSequenceImpl
<5> {
64 using Type
= IndexSequence
<0,1,2,3,4>;
66 template <> struct MakeIndexSequenceImpl
<6> {
67 using Type
= IndexSequence
<0,1,2,3,4,5>;
69 template <> struct MakeIndexSequenceImpl
<7> {
70 using Type
= IndexSequence
<0,1,2,3,4,5,6>;
72 template <> struct MakeIndexSequenceImpl
<8> {
73 using Type
= IndexSequence
<0,1,2,3,4,5,6,7>;
75 template <> struct MakeIndexSequenceImpl
<9> {
76 using Type
= IndexSequence
<0,1,2,3,4,5,6,7,8>;
78 template <> struct MakeIndexSequenceImpl
<10> {
79 using Type
= IndexSequence
<0,1,2,3,4,5,6,7,8,9>;
81 template <> struct MakeIndexSequenceImpl
<11> {
82 using Type
= IndexSequence
<0,1,2,3,4,5,6,7,8,9,10>;
84 template <> struct MakeIndexSequenceImpl
<12> {
85 using Type
= IndexSequence
<0,1,2,3,4,5,6,7,8,9,10,11>;
87 template <> struct MakeIndexSequenceImpl
<13> {
88 using Type
= IndexSequence
<0,1,2,3,4,5,6,7,8,9,10,11,12>;
91 #else // defined(WIN) && defined(_PREFAST_)
93 template <size_t... Ns
>
94 struct MakeIndexSequenceImpl
<0, Ns
...> {
95 using Type
= IndexSequence
<Ns
...>;
98 template <size_t N
, size_t... Ns
>
99 struct MakeIndexSequenceImpl
<N
, Ns
...>
100 : MakeIndexSequenceImpl
<N
- 1, N
- 1, Ns
...> {};
102 #endif // defined(WIN) && defined(_PREFAST_)
105 using MakeIndexSequence
= typename MakeIndexSequenceImpl
<N
>::Type
;
107 // Traits ----------------------------------------------------------------------
109 // A simple traits class for tuple arguments.
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).
119 typedef const P
& ParamType
;
123 struct TupleTraits
<P
&> {
126 typedef P
& ParamType
;
129 // Tuple -----------------------------------------------------------------------
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.
136 // Tuple<> is supplied to act as a 'void' type. It can be used, for example,
137 // when dispatching to a function that accepts no arguments (see the
138 // Dispatchers below).
139 // Tuple<A> is rarely useful. One such use is when A is non-const ref that you
140 // want filled by the dispatchee, and the tuple is merely a container for that
141 // output (a "tier"). See MakeRefTuple and its usages.
143 template <typename IxSeq
, typename
... Ts
>
144 struct TupleBaseImpl
;
145 template <typename
... Ts
>
146 using TupleBase
= TupleBaseImpl
<MakeIndexSequence
<sizeof...(Ts
)>, Ts
...>;
147 template <size_t N
, typename T
>
150 template <typename
... Ts
>
151 struct Tuple
: TupleBase
<Ts
...> {
152 Tuple() : TupleBase
<Ts
...>() {}
153 explicit Tuple(typename TupleTraits
<Ts
>::ParamType
... args
)
154 : TupleBase
<Ts
...>(args
...) {}
157 // Avoids ambiguity between Tuple's two constructors.
161 template <size_t... Ns
, typename
... Ts
>
162 struct 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
)... {}
168 template <size_t N
, typename T
>
171 explicit TupleLeaf(typename TupleTraits
<T
>::ParamType x
) : x(x
) {}
173 T
& get() { return x
; }
174 const T
& get() const { return x
; }
179 // Tuple getters --------------------------------------------------------------
181 // Allows accessing an arbitrary tuple element by index.
184 // Tuple<int, double> t2;
186 // get<1>(t2) = 3.14;
188 template <size_t I
, typename T
>
189 T
& get(TupleLeaf
<I
, T
>& leaf
) {
193 template <size_t I
, typename T
>
194 const T
& get(const TupleLeaf
<I
, T
>& leaf
) {
198 // Tuple types ----------------------------------------------------------------
200 // Allows for selection of ValueTuple/RefTuple/ParamTuple without needing the
201 // definitions of class types the tuple takes as parameters.
203 template <typename T
>
206 template <typename
... Ts
>
207 struct 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
...>;
213 // Tuple creators -------------------------------------------------------------
215 // Helper functions for constructing tuples while inferring the template
218 template <typename
... Ts
>
219 inline Tuple
<Ts
...> MakeTuple(const Ts
&... arg
) {
220 return Tuple
<Ts
...>(arg
...);
223 // The following set of helpers make what Boost refers to as "Tiers" - a tuple
226 template <typename
... Ts
>
227 inline Tuple
<Ts
&...> MakeRefTuple(Ts
&... arg
) {
228 return Tuple
<Ts
&...>(arg
...);
231 // Dispatchers ----------------------------------------------------------------
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.
240 // Non-Static Dispatchers with no out params.
242 template <typename ObjT
, typename Method
, typename A
>
243 inline void DispatchToMethod(ObjT
* obj
, Method method
, const A
& arg
) {
244 (obj
->*method
)(base::internal::UnwrapTraits
<A
>::Unwrap(arg
));
247 template <typename ObjT
, typename Method
, typename
... Ts
, size_t... Ns
>
248 inline void DispatchToMethodImpl(ObjT
* obj
,
250 const Tuple
<Ts
...>& arg
,
251 IndexSequence
<Ns
...>) {
252 (obj
->*method
)(base::internal::UnwrapTraits
<Ts
>::Unwrap(get
<Ns
>(arg
))...);
255 template <typename ObjT
, typename Method
, typename
... Ts
>
256 inline void DispatchToMethod(ObjT
* obj
,
258 const Tuple
<Ts
...>& arg
) {
259 DispatchToMethodImpl(obj
, method
, arg
, MakeIndexSequence
<sizeof...(Ts
)>());
262 // Static Dispatchers with no out params.
264 template <typename Function
, typename A
>
265 inline void DispatchToMethod(Function function
, const A
& arg
) {
266 (*function
)(base::internal::UnwrapTraits
<A
>::Unwrap(arg
));
269 template <typename Function
, typename
... Ts
, size_t... Ns
>
270 inline void DispatchToFunctionImpl(Function function
,
271 const Tuple
<Ts
...>& arg
,
272 IndexSequence
<Ns
...>) {
273 (*function
)(base::internal::UnwrapTraits
<Ts
>::Unwrap(get
<Ns
>(arg
))...);
276 template <typename Function
, typename
... Ts
>
277 inline void DispatchToFunction(Function function
, const Tuple
<Ts
...>& arg
) {
278 DispatchToFunctionImpl(function
, arg
, MakeIndexSequence
<sizeof...(Ts
)>());
281 // Dispatchers with out parameters.
283 template <typename ObjT
,
288 inline void DispatchToMethodImpl(ObjT
* obj
,
291 Tuple
<OutTs
...>* out
,
292 IndexSequence
<OutNs
...>) {
293 (obj
->*method
)(base::internal::UnwrapTraits
<In
>::Unwrap(in
),
294 &get
<OutNs
>(*out
)...);
297 template <typename ObjT
, typename Method
, typename In
, typename
... OutTs
>
298 inline void DispatchToMethod(ObjT
* obj
,
301 Tuple
<OutTs
...>* out
) {
302 DispatchToMethodImpl(obj
, method
, in
, out
,
303 MakeIndexSequence
<sizeof...(OutTs
)>());
306 template <typename ObjT
,
312 inline void DispatchToMethodImpl(ObjT
* obj
,
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
)...);
322 template <typename ObjT
, typename Method
, typename
... InTs
, typename
... OutTs
>
323 inline void DispatchToMethod(ObjT
* obj
,
325 const Tuple
<InTs
...>& in
,
326 Tuple
<OutTs
...>* out
) {
327 DispatchToMethodImpl(obj
, method
, in
, out
,
328 MakeIndexSequence
<sizeof...(InTs
)>(),
329 MakeIndexSequence
<sizeof...(OutTs
)>());
332 #endif // BASE_TUPLE_H__