blob: e13afaf31fc5b3ffa5f229c7351e8c641499dd50 [file] [log] [blame]
[email protected]6b9b771c2011-09-28 00:33:591// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
tzik1068f1be2016-06-03 07:25:205// Use std::tuple as tuple type. This file contains helper functions for
6// working with std::tuples.
7// The functions DispatchToMethod and DispatchToFunction take a function pointer
8// or instance and method pointer, and unpack a tuple into arguments to the
9// call.
[email protected]302bdc132008-08-25 13:42:0710//
11// Example usage:
12// // These two methods of creating a Tuple are identical.
tzik1068f1be2016-06-03 07:25:2013// std::tuple<int, const char*> tuple_a(1, "wee");
14// std::tuple<int, const char*> tuple_b = std::make_tuple(1, "wee");
[email protected]302bdc132008-08-25 13:42:0715//
16// void SomeFunc(int a, const char* b) { }
17// DispatchToFunction(&SomeFunc, tuple_a); // SomeFunc(1, "wee")
18// DispatchToFunction(
tzik1068f1be2016-06-03 07:25:2019// &SomeFunc, std::make_tuple(10, "foo")); // SomeFunc(10, "foo")
[email protected]302bdc132008-08-25 13:42:0720//
21// struct { void SomeMeth(int a, int b, int c) { } } foo;
tzik1068f1be2016-06-03 07:25:2022// DispatchToMethod(&foo, &Foo::SomeMeth, std::make_tuple(1, 2, 3));
[email protected]302bdc132008-08-25 13:42:0723// // foo->SomeMeth(1, 2, 3);
24
tfarinaa31163512015-05-13 22:10:1525#ifndef BASE_TUPLE_H_
26#define BASE_TUPLE_H_
initial.commitd7cae122008-07-26 21:49:3827
avi9b6f42932015-12-26 22:15:1428#include <stddef.h>
tzik9ca302192016-02-11 10:24:4529#include <tuple>
avi9b6f42932015-12-26 22:15:1430
avi9b6f42932015-12-26 22:15:1431#include "build/build_config.h"
[email protected]58ae6302013-06-27 12:48:0232
brettwd5ca2bc2015-05-29 22:15:4733namespace base {
34
mdempsky6e7f6152014-12-10 03:10:5935// Index sequences
36//
37// Minimal clone of the similarly-named C++14 functionality.
38
39template <size_t...>
40struct IndexSequence {};
41
42template <size_t... Ns>
43struct MakeIndexSequenceImpl;
44
45template <size_t... Ns>
46struct MakeIndexSequenceImpl<0, Ns...> {
47 using Type = IndexSequence<Ns...>;
48};
49
50template <size_t N, size_t... Ns>
mdempsky06c626a2014-12-10 11:22:2351struct MakeIndexSequenceImpl<N, Ns...>
52 : MakeIndexSequenceImpl<N - 1, N - 1, Ns...> {};
mdempsky6e7f6152014-12-10 03:10:5953
54template <size_t N>
55using MakeIndexSequence = typename MakeIndexSequenceImpl<N>::Type;
56
tzik2387a8252016-08-25 13:57:1457template <typename T>
58using MakeIndexSequenceForTuple =
59 MakeIndexSequence<std::tuple_size<typename std::decay<T>::type>::value>;
60
initial.commitd7cae122008-07-26 21:49:3861// Dispatchers ----------------------------------------------------------------
62//
63// Helper functions that call the given method on an object, with the unpacked
64// tuple arguments. Notice that they all have the same number of arguments,
65// so you need only write:
66// DispatchToMethod(object, &Object::method, args);
67// This is very useful for templated dispatchers, since they don't need to know
68// what type |args| is.
69
70// Non-Static Dispatchers with no out params.
71
tzik2387a8252016-08-25 13:57:1472template <typename ObjT, typename Method, typename Tuple, size_t... Ns>
tzik1ea87e3a2016-02-16 04:56:3173inline void DispatchToMethodImpl(const ObjT& obj,
mdempsky6e7f6152014-12-10 03:10:5974 Method method,
tzik2387a8252016-08-25 13:57:1475 Tuple&& args,
mdempsky6e7f6152014-12-10 03:10:5976 IndexSequence<Ns...>) {
tzikf7c47572017-04-05 21:45:0377 (obj->*method)(std::get<Ns>(std::forward<Tuple>(args))...);
initial.commitd7cae122008-07-26 21:49:3878}
79
tzik2387a8252016-08-25 13:57:1480template <typename ObjT, typename Method, typename Tuple>
tzik1ea87e3a2016-02-16 04:56:3181inline void DispatchToMethod(const ObjT& obj,
[email protected]52a261f2009-03-03 15:01:1282 Method method,
tzik2387a8252016-08-25 13:57:1483 Tuple&& args) {
84 DispatchToMethodImpl(obj, method, std::forward<Tuple>(args),
85 MakeIndexSequenceForTuple<Tuple>());
[email protected]fa685ff2011-02-17 19:47:1386}
87
initial.commitd7cae122008-07-26 21:49:3888// Static Dispatchers with no out params.
89
tzik2387a8252016-08-25 13:57:1490template <typename Function, typename Tuple, size_t... Ns>
mdempsky6e7f6152014-12-10 03:10:5991inline void DispatchToFunctionImpl(Function function,
tzik2387a8252016-08-25 13:57:1492 Tuple&& args,
mdempsky6e7f6152014-12-10 03:10:5993 IndexSequence<Ns...>) {
tzikf7c47572017-04-05 21:45:0394 (*function)(std::get<Ns>(std::forward<Tuple>(args))...);
initial.commitd7cae122008-07-26 21:49:3895}
96
tzik2387a8252016-08-25 13:57:1497template <typename Function, typename Tuple>
98inline void DispatchToFunction(Function function, Tuple&& args) {
99 DispatchToFunctionImpl(function, std::forward<Tuple>(args),
100 MakeIndexSequenceForTuple<Tuple>());
initial.commitd7cae122008-07-26 21:49:38101}
102
mdempsky6e7f6152014-12-10 03:10:59103// Dispatchers with out parameters.
104
105template <typename ObjT,
106 typename Method,
tzik2387a8252016-08-25 13:57:14107 typename InTuple,
108 typename OutTuple,
mdempsky6e7f6152014-12-10 03:10:59109 size_t... InNs,
110 size_t... OutNs>
tzik1ea87e3a2016-02-16 04:56:31111inline void DispatchToMethodImpl(const ObjT& obj,
mdempsky6e7f6152014-12-10 03:10:59112 Method method,
tzik2387a8252016-08-25 13:57:14113 InTuple&& in,
114 OutTuple* out,
mdempsky6e7f6152014-12-10 03:10:59115 IndexSequence<InNs...>,
116 IndexSequence<OutNs...>) {
tzikf7c47572017-04-05 21:45:03117 (obj->*method)(std::get<InNs>(std::forward<InTuple>(in))...,
tzik1068f1be2016-06-03 07:25:20118 &std::get<OutNs>(*out)...);
initial.commitd7cae122008-07-26 21:49:38119}
120
tzik2387a8252016-08-25 13:57:14121template <typename ObjT, typename Method, typename InTuple, typename OutTuple>
tzik1ea87e3a2016-02-16 04:56:31122inline void DispatchToMethod(const ObjT& obj,
[email protected]52a261f2009-03-03 15:01:12123 Method method,
tzik2387a8252016-08-25 13:57:14124 InTuple&& in,
125 OutTuple* out) {
126 DispatchToMethodImpl(obj, method, std::forward<InTuple>(in), out,
127 MakeIndexSequenceForTuple<InTuple>(),
128 MakeIndexSequenceForTuple<OutTuple>());
[email protected]8a2820a2008-10-09 21:58:05129}
130
brettwd5ca2bc2015-05-29 22:15:47131} // namespace base
132
tfarinaa31163512015-05-13 22:10:15133#endif // BASE_TUPLE_H_