Address NewApi Android lint warnings in src/content.
[chromium-blink-merge.git] / base / tuple.h
blob41c6a00191971e4eabfd2aa6dbf6d3308a6b0eca
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.
14 // Example usage:
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"
33 // Index sequences
35 // Minimal clone of the similarly-named C++14 functionality.
37 template <size_t...>
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>;
85 #else // defined(WIN) && defined(_PREFAST_)
87 template <size_t... Ns>
88 struct MakeIndexSequenceImpl<0, Ns...> {
89 using Type = IndexSequence<Ns...>;
92 template <size_t N, size_t... Ns>
93 struct MakeIndexSequenceImpl<N, Ns...>
94 : MakeIndexSequenceImpl<N - 1, N - 1, Ns...> {};
96 #endif // defined(WIN) && defined(_PREFAST_)
98 template <size_t N>
99 using MakeIndexSequence = typename MakeIndexSequenceImpl<N>::Type;
101 // Traits ----------------------------------------------------------------------
103 // A simple traits class for tuple arguments.
105 // ValueType: the bare, nonref version of a type (same as the type for nonrefs).
106 // RefType: the ref version of a type (same as the type for refs).
107 // ParamType: what type to pass to functions (refs should not be constified).
109 template <class P>
110 struct TupleTraits {
111 typedef P ValueType;
112 typedef P& RefType;
113 typedef const P& ParamType;
116 template <class P>
117 struct TupleTraits<P&> {
118 typedef P ValueType;
119 typedef P& RefType;
120 typedef P& ParamType;
123 // Tuple -----------------------------------------------------------------------
125 // This set of classes is useful for bundling 0 or more heterogeneous data types
126 // into a single variable. The advantage of this is that it greatly simplifies
127 // function objects that need to take an arbitrary number of parameters; see
128 // RunnableMethod and IPC::MessageWithTuple.
130 // Tuple<> is supplied to act as a 'void' type. It can be used, for example,
131 // when dispatching to a function that accepts no arguments (see the
132 // Dispatchers below).
133 // Tuple<A> is rarely useful. One such use is when A is non-const ref that you
134 // want filled by the dispatchee, and the tuple is merely a container for that
135 // output (a "tier"). See MakeRefTuple and its usages.
137 template <typename IxSeq, typename... Ts>
138 struct TupleBaseImpl;
139 template <typename... Ts>
140 using TupleBase = TupleBaseImpl<MakeIndexSequence<sizeof...(Ts)>, Ts...>;
141 template <size_t N, typename T>
142 struct TupleLeaf;
144 template <typename... Ts>
145 struct Tuple : TupleBase<Ts...> {
146 Tuple() : TupleBase<Ts...>() {}
147 explicit Tuple(typename TupleTraits<Ts>::ParamType... args)
148 : TupleBase<Ts...>(args...) {}
151 // Avoids ambiguity between Tuple's two constructors.
152 template <>
153 struct Tuple<> {};
155 template <size_t... Ns, typename... Ts>
156 struct TupleBaseImpl<IndexSequence<Ns...>, Ts...> : TupleLeaf<Ns, Ts>... {
157 TupleBaseImpl() : TupleLeaf<Ns, Ts>()... {}
158 explicit TupleBaseImpl(typename TupleTraits<Ts>::ParamType... args)
159 : TupleLeaf<Ns, Ts>(args)... {}
162 template <size_t N, typename T>
163 struct TupleLeaf {
164 TupleLeaf() {}
165 explicit TupleLeaf(typename TupleTraits<T>::ParamType x) : x(x) {}
167 T& get() { return x; }
168 const T& get() const { return x; }
170 T x;
173 // Tuple getters --------------------------------------------------------------
175 // Allows accessing an arbitrary tuple element by index.
177 // Example usage:
178 // Tuple<int, double> t2;
179 // get<0>(t2) = 42;
180 // get<1>(t2) = 3.14;
182 template <size_t I, typename T>
183 T& get(TupleLeaf<I, T>& leaf) {
184 return leaf.get();
187 template <size_t I, typename T>
188 const T& get(const TupleLeaf<I, T>& leaf) {
189 return leaf.get();
192 // Tuple types ----------------------------------------------------------------
194 // Allows for selection of ValueTuple/RefTuple/ParamTuple without needing the
195 // definitions of class types the tuple takes as parameters.
197 template <typename T>
198 struct TupleTypes;
200 template <typename... Ts>
201 struct TupleTypes<Tuple<Ts...>> {
202 using ValueTuple = Tuple<typename TupleTraits<Ts>::ValueType...>;
203 using RefTuple = Tuple<typename TupleTraits<Ts>::RefType...>;
204 using ParamTuple = Tuple<typename TupleTraits<Ts>::ParamType...>;
207 // Tuple creators -------------------------------------------------------------
209 // Helper functions for constructing tuples while inferring the template
210 // argument types.
212 template <typename... Ts>
213 inline Tuple<Ts...> MakeTuple(const Ts&... arg) {
214 return Tuple<Ts...>(arg...);
217 // The following set of helpers make what Boost refers to as "Tiers" - a tuple
218 // of references.
220 template <typename... Ts>
221 inline Tuple<Ts&...> MakeRefTuple(Ts&... arg) {
222 return Tuple<Ts&...>(arg...);
225 // Dispatchers ----------------------------------------------------------------
227 // Helper functions that call the given method on an object, with the unpacked
228 // tuple arguments. Notice that they all have the same number of arguments,
229 // so you need only write:
230 // DispatchToMethod(object, &Object::method, args);
231 // This is very useful for templated dispatchers, since they don't need to know
232 // what type |args| is.
234 // Non-Static Dispatchers with no out params.
236 template <typename ObjT, typename Method, typename A>
237 inline void DispatchToMethod(ObjT* obj, Method method, const A& arg) {
238 (obj->*method)(base::internal::UnwrapTraits<A>::Unwrap(arg));
241 template <typename ObjT, typename Method, typename... Ts, size_t... Ns>
242 inline void DispatchToMethodImpl(ObjT* obj,
243 Method method,
244 const Tuple<Ts...>& arg,
245 IndexSequence<Ns...>) {
246 (obj->*method)(base::internal::UnwrapTraits<Ts>::Unwrap(get<Ns>(arg))...);
249 template <typename ObjT, typename Method, typename... Ts>
250 inline void DispatchToMethod(ObjT* obj,
251 Method method,
252 const Tuple<Ts...>& arg) {
253 DispatchToMethodImpl(obj, method, arg, MakeIndexSequence<sizeof...(Ts)>());
256 // Static Dispatchers with no out params.
258 template <typename Function, typename A>
259 inline void DispatchToMethod(Function function, const A& arg) {
260 (*function)(base::internal::UnwrapTraits<A>::Unwrap(arg));
263 template <typename Function, typename... Ts, size_t... Ns>
264 inline void DispatchToFunctionImpl(Function function,
265 const Tuple<Ts...>& arg,
266 IndexSequence<Ns...>) {
267 (*function)(base::internal::UnwrapTraits<Ts>::Unwrap(get<Ns>(arg))...);
270 template <typename Function, typename... Ts>
271 inline void DispatchToFunction(Function function, const Tuple<Ts...>& arg) {
272 DispatchToFunctionImpl(function, arg, MakeIndexSequence<sizeof...(Ts)>());
275 // Dispatchers with out parameters.
277 template <typename ObjT,
278 typename Method,
279 typename In,
280 typename... OutTs,
281 size_t... OutNs>
282 inline void DispatchToMethodImpl(ObjT* obj,
283 Method method,
284 const In& in,
285 Tuple<OutTs...>* out,
286 IndexSequence<OutNs...>) {
287 (obj->*method)(base::internal::UnwrapTraits<In>::Unwrap(in),
288 &get<OutNs>(*out)...);
291 template <typename ObjT, typename Method, typename In, typename... OutTs>
292 inline void DispatchToMethod(ObjT* obj,
293 Method method,
294 const In& in,
295 Tuple<OutTs...>* out) {
296 DispatchToMethodImpl(obj, method, in, out,
297 MakeIndexSequence<sizeof...(OutTs)>());
300 template <typename ObjT,
301 typename Method,
302 typename... InTs,
303 typename... OutTs,
304 size_t... InNs,
305 size_t... OutNs>
306 inline void DispatchToMethodImpl(ObjT* obj,
307 Method method,
308 const Tuple<InTs...>& in,
309 Tuple<OutTs...>* out,
310 IndexSequence<InNs...>,
311 IndexSequence<OutNs...>) {
312 (obj->*method)(base::internal::UnwrapTraits<InTs>::Unwrap(get<InNs>(in))...,
313 &get<OutNs>(*out)...);
316 template <typename ObjT, typename Method, typename... InTs, typename... OutTs>
317 inline void DispatchToMethod(ObjT* obj,
318 Method method,
319 const Tuple<InTs...>& in,
320 Tuple<OutTs...>* out) {
321 DispatchToMethodImpl(obj, method, in, out,
322 MakeIndexSequence<sizeof...(InTs)>(),
323 MakeIndexSequence<sizeof...(OutTs)>());
326 #endif // BASE_TUPLE_H__