Add checks to ShowPageActionPopup
[chromium-blink-merge.git] / base / tuple.h
bloba0d22454abde023b5fe298d52ac78127c6cd2d2b
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 template <size_t... Ns>
44 struct MakeIndexSequenceImpl<0, Ns...> {
45 using Type = IndexSequence<Ns...>;
48 template <size_t N, size_t... Ns>
49 struct MakeIndexSequenceImpl<N, Ns...>
50 : MakeIndexSequenceImpl<N - 1, N - 1, Ns...> {};
52 template <size_t N>
53 using MakeIndexSequence = typename MakeIndexSequenceImpl<N>::Type;
55 // Traits ----------------------------------------------------------------------
57 // A simple traits class for tuple arguments.
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).
63 template <class P>
64 struct TupleTraits {
65 typedef P ValueType;
66 typedef P& RefType;
67 typedef const P& ParamType;
70 template <class P>
71 struct TupleTraits<P&> {
72 typedef P ValueType;
73 typedef P& RefType;
74 typedef P& ParamType;
77 // Tuple -----------------------------------------------------------------------
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.
84 // Tuple<> is supplied to act as a 'void' type. It can be used, for example,
85 // when dispatching to a function that accepts no arguments (see the
86 // Dispatchers below).
87 // Tuple<A> is rarely useful. One such use is when A is non-const ref that you
88 // want filled by the dispatchee, and the tuple is merely a container for that
89 // output (a "tier"). See MakeRefTuple and its usages.
91 template <typename IxSeq, typename... Ts>
92 struct TupleBaseImpl;
93 template <typename... Ts>
94 using TupleBase = TupleBaseImpl<MakeIndexSequence<sizeof...(Ts)>, Ts...>;
95 template <size_t N, typename T>
96 struct TupleLeaf;
98 template <typename... Ts>
99 struct Tuple : TupleBase<Ts...> {
100 Tuple() : TupleBase<Ts...>() {}
101 explicit Tuple(typename TupleTraits<Ts>::ParamType... args)
102 : TupleBase<Ts...>(args...) {}
105 // Avoids ambiguity between Tuple's two constructors.
106 template <>
107 struct Tuple<> {};
109 template <size_t... Ns, typename... Ts>
110 struct 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)... {}
116 template <size_t N, typename T>
117 struct TupleLeaf {
118 TupleLeaf() {}
119 explicit TupleLeaf(typename TupleTraits<T>::ParamType x) : x(x) {}
121 T& get() { return x; }
122 const T& get() const { return x; }
124 T x;
127 // For legacy compatibility, we name the first 8 tuple elements "a", "b", ...
128 // TODO(mdempsky): Update users to use get<N>() (crbug.com/440675).
130 #define DEFINE_TUPLE_LEAF(N, x) \
131 template <typename T> \
132 struct TupleLeaf<N, T> { \
133 TupleLeaf() {} \
134 explicit TupleLeaf(typename TupleTraits<T>::ParamType x) : x(x) {} \
136 T& get() { return x; } \
137 const T& get() const { return x; } \
139 T x; \
142 DEFINE_TUPLE_LEAF(0, a);
143 DEFINE_TUPLE_LEAF(1, b);
144 DEFINE_TUPLE_LEAF(2, c);
145 DEFINE_TUPLE_LEAF(3, d);
146 DEFINE_TUPLE_LEAF(4, e);
147 DEFINE_TUPLE_LEAF(5, f);
148 DEFINE_TUPLE_LEAF(6, g);
149 DEFINE_TUPLE_LEAF(7, h);
151 #undef DEFINE_TUPLE_LEAF
153 // Deprecated compat aliases
154 // TODO(mdempsky): Update users to just use Tuple instead (crbug.com/440675).
156 using Tuple0 = Tuple<>;
157 template <typename A>
158 using Tuple1 = Tuple<A>;
159 template <typename A, typename B>
160 using Tuple2 = Tuple<A, B>;
161 template <typename A, typename B, typename C>
162 using Tuple3 = Tuple<A, B, C>;
163 template <typename A, typename B, typename C, typename D>
164 using Tuple4 = Tuple<A, B, C, D>;
165 template <typename A, typename B, typename C, typename D, typename E>
166 using Tuple5 = Tuple<A, B, C, D, E>;
167 template <typename A,
168 typename B,
169 typename C,
170 typename D,
171 typename E,
172 typename F>
173 using Tuple6 = Tuple<A, B, C, D, E, F>;
174 template <typename A,
175 typename B,
176 typename C,
177 typename D,
178 typename E,
179 typename F,
180 typename G>
181 using Tuple7 = Tuple<A, B, C, D, E, F, G>;
182 template <typename A,
183 typename B,
184 typename C,
185 typename D,
186 typename E,
187 typename F,
188 typename G,
189 typename H>
190 using Tuple8 = Tuple<A, B, C, D, E, F, G, H>;
192 // Tuple getters --------------------------------------------------------------
194 // Allows accessing an arbitrary tuple element by index.
196 // Example usage:
197 // Tuple<int, double> t2;
198 // get<0>(t2) = 42;
199 // get<1>(t2) = 3.14;
201 template <size_t I, typename T>
202 T& get(TupleLeaf<I, T>& leaf) {
203 return leaf.get();
206 template <size_t I, typename T>
207 const T& get(const TupleLeaf<I, T>& leaf) {
208 return leaf.get();
211 // Tuple types ----------------------------------------------------------------
213 // Allows for selection of ValueTuple/RefTuple/ParamTuple without needing the
214 // definitions of class types the tuple takes as parameters.
216 template <typename T>
217 struct TupleTypes;
219 template <typename... Ts>
220 struct 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...>;
226 // Tuple creators -------------------------------------------------------------
228 // Helper functions for constructing tuples while inferring the template
229 // argument types.
231 template <typename... Ts>
232 inline Tuple<Ts...> MakeTuple(const Ts&... arg) {
233 return Tuple<Ts...>(arg...);
236 // The following set of helpers make what Boost refers to as "Tiers" - a tuple
237 // of references.
239 template <typename... Ts>
240 inline Tuple<Ts&...> MakeRefTuple(Ts&... arg) {
241 return Tuple<Ts&...>(arg...);
244 // Dispatchers ----------------------------------------------------------------
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.
253 // Non-Static Dispatchers with no out params.
255 template <typename ObjT, typename Method, typename A>
256 inline void DispatchToMethod(ObjT* obj, Method method, const A& arg) {
257 (obj->*method)(base::internal::UnwrapTraits<A>::Unwrap(arg));
260 template <typename ObjT, typename Method, typename... Ts, size_t... Ns>
261 inline 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))...);
268 template <typename ObjT, typename Method, typename... Ts>
269 inline void DispatchToMethod(ObjT* obj,
270 Method method,
271 const Tuple<Ts...>& arg) {
272 DispatchToMethodImpl(obj, method, arg, MakeIndexSequence<sizeof...(Ts)>());
275 // Static Dispatchers with no out params.
277 template <typename Function, typename A>
278 inline void DispatchToMethod(Function function, const A& arg) {
279 (*function)(base::internal::UnwrapTraits<A>::Unwrap(arg));
282 template <typename Function, typename... Ts, size_t... Ns>
283 inline void DispatchToFunctionImpl(Function function,
284 const Tuple<Ts...>& arg,
285 IndexSequence<Ns...>) {
286 (*function)(base::internal::UnwrapTraits<Ts>::Unwrap(get<Ns>(arg))...);
289 template <typename Function, typename... Ts>
290 inline void DispatchToFunction(Function function, const Tuple<Ts...>& arg) {
291 DispatchToFunctionImpl(function, arg, MakeIndexSequence<sizeof...(Ts)>());
294 // Dispatchers with out parameters.
296 template <typename ObjT,
297 typename Method,
298 typename In,
299 typename... OutTs,
300 size_t... OutNs>
301 inline 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)...);
310 template <typename ObjT, typename Method, typename In, typename... OutTs>
311 inline void DispatchToMethod(ObjT* obj,
312 Method method,
313 const In& in,
314 Tuple<OutTs...>* out) {
315 DispatchToMethodImpl(obj, method, in, out,
316 MakeIndexSequence<sizeof...(OutTs)>());
319 template <typename ObjT,
320 typename Method,
321 typename... InTs,
322 typename... OutTs,
323 size_t... InNs,
324 size_t... OutNs>
325 inline 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)...);
335 template <typename ObjT, typename Method, typename... InTs, typename... OutTs>
336 inline void DispatchToMethod(ObjT* obj,
337 Method method,
338 const Tuple<InTs...>& in,
339 Tuple<OutTs...>* out) {
340 DispatchToMethodImpl(obj, method, in, out,
341 MakeIndexSequence<sizeof...(InTs)>(),
342 MakeIndexSequence<sizeof...(OutTs)>());
345 #endif // BASE_TUPLE_H__