Roll src/third_party/skia d094908:ac8d8b8
[chromium-blink-merge.git] / base / tuple.h
blob4408074335b3bd2c28b5ee5719a0b63ba262971c
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 // Tuple getters --------------------------------------------------------------
129 // Allows accessing an arbitrary tuple element by index.
131 // Example usage:
132 // Tuple<int, double> t2;
133 // get<0>(t2) = 42;
134 // get<1>(t2) = 3.14;
136 template <size_t I, typename T>
137 T& get(TupleLeaf<I, T>& leaf) {
138 return leaf.get();
141 template <size_t I, typename T>
142 const T& get(const TupleLeaf<I, T>& leaf) {
143 return leaf.get();
146 // Tuple types ----------------------------------------------------------------
148 // Allows for selection of ValueTuple/RefTuple/ParamTuple without needing the
149 // definitions of class types the tuple takes as parameters.
151 template <typename T>
152 struct TupleTypes;
154 template <typename... Ts>
155 struct 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...>;
161 // Tuple creators -------------------------------------------------------------
163 // Helper functions for constructing tuples while inferring the template
164 // argument types.
166 template <typename... Ts>
167 inline Tuple<Ts...> MakeTuple(const Ts&... arg) {
168 return Tuple<Ts...>(arg...);
171 // The following set of helpers make what Boost refers to as "Tiers" - a tuple
172 // of references.
174 template <typename... Ts>
175 inline Tuple<Ts&...> MakeRefTuple(Ts&... arg) {
176 return Tuple<Ts&...>(arg...);
179 // Dispatchers ----------------------------------------------------------------
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.
188 // Non-Static Dispatchers with no out params.
190 template <typename ObjT, typename Method, typename A>
191 inline void DispatchToMethod(ObjT* obj, Method method, const A& arg) {
192 (obj->*method)(base::internal::UnwrapTraits<A>::Unwrap(arg));
195 template <typename ObjT, typename Method, typename... Ts, size_t... Ns>
196 inline 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))...);
203 template <typename ObjT, typename Method, typename... Ts>
204 inline void DispatchToMethod(ObjT* obj,
205 Method method,
206 const Tuple<Ts...>& arg) {
207 DispatchToMethodImpl(obj, method, arg, MakeIndexSequence<sizeof...(Ts)>());
210 // Static Dispatchers with no out params.
212 template <typename Function, typename A>
213 inline void DispatchToMethod(Function function, const A& arg) {
214 (*function)(base::internal::UnwrapTraits<A>::Unwrap(arg));
217 template <typename Function, typename... Ts, size_t... Ns>
218 inline void DispatchToFunctionImpl(Function function,
219 const Tuple<Ts...>& arg,
220 IndexSequence<Ns...>) {
221 (*function)(base::internal::UnwrapTraits<Ts>::Unwrap(get<Ns>(arg))...);
224 template <typename Function, typename... Ts>
225 inline void DispatchToFunction(Function function, const Tuple<Ts...>& arg) {
226 DispatchToFunctionImpl(function, arg, MakeIndexSequence<sizeof...(Ts)>());
229 // Dispatchers with out parameters.
231 template <typename ObjT,
232 typename Method,
233 typename In,
234 typename... OutTs,
235 size_t... OutNs>
236 inline 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)...);
245 template <typename ObjT, typename Method, typename In, typename... OutTs>
246 inline void DispatchToMethod(ObjT* obj,
247 Method method,
248 const In& in,
249 Tuple<OutTs...>* out) {
250 DispatchToMethodImpl(obj, method, in, out,
251 MakeIndexSequence<sizeof...(OutTs)>());
254 template <typename ObjT,
255 typename Method,
256 typename... InTs,
257 typename... OutTs,
258 size_t... InNs,
259 size_t... OutNs>
260 inline 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)...);
270 template <typename ObjT, typename Method, typename... InTs, typename... OutTs>
271 inline void DispatchToMethod(ObjT* obj,
272 Method method,
273 const Tuple<InTs...>& in,
274 Tuple<OutTs...>* out) {
275 DispatchToMethodImpl(obj, method, in, out,
276 MakeIndexSequence<sizeof...(InTs)>(),
277 MakeIndexSequence<sizeof...(OutTs)>());
280 #endif // BASE_TUPLE_H__