blob: 049b505dd62d772a1bc411a5e934f342a06bf50c [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>
Jeremy Roman84956fa2017-08-16 15:55:2030#include <utility>
avi9b6f42932015-12-26 22:15:1431
avi9b6f42932015-12-26 22:15:1432#include "build/build_config.h"
[email protected]58ae6302013-06-27 12:48:0233
brettwd5ca2bc2015-05-29 22:15:4734namespace base {
35
tzik2387a8252016-08-25 13:57:1436template <typename T>
Jeremy Roman84956fa2017-08-16 15:55:2037using MakeIndexSequenceForTuple = std::make_index_sequence<
38 std::tuple_size<typename std::decay<T>::type>::value>;
tzik2387a8252016-08-25 13:57:1439
initial.commitd7cae122008-07-26 21:49:3840// Dispatchers ----------------------------------------------------------------
41//
42// Helper functions that call the given method on an object, with the unpacked
43// tuple arguments. Notice that they all have the same number of arguments,
44// so you need only write:
45// DispatchToMethod(object, &Object::method, args);
46// This is very useful for templated dispatchers, since they don't need to know
47// what type |args| is.
48
49// Non-Static Dispatchers with no out params.
50
tzik2387a8252016-08-25 13:57:1451template <typename ObjT, typename Method, typename Tuple, size_t... Ns>
tzik1ea87e3a2016-02-16 04:56:3152inline void DispatchToMethodImpl(const ObjT& obj,
mdempsky6e7f6152014-12-10 03:10:5953 Method method,
tzik2387a8252016-08-25 13:57:1454 Tuple&& args,
Jeremy Roman84956fa2017-08-16 15:55:2055 std::index_sequence<Ns...>) {
tzikf7c47572017-04-05 21:45:0356 (obj->*method)(std::get<Ns>(std::forward<Tuple>(args))...);
initial.commitd7cae122008-07-26 21:49:3857}
58
tzik2387a8252016-08-25 13:57:1459template <typename ObjT, typename Method, typename Tuple>
tzik1ea87e3a2016-02-16 04:56:3160inline void DispatchToMethod(const ObjT& obj,
[email protected]52a261f2009-03-03 15:01:1261 Method method,
tzik2387a8252016-08-25 13:57:1462 Tuple&& args) {
63 DispatchToMethodImpl(obj, method, std::forward<Tuple>(args),
64 MakeIndexSequenceForTuple<Tuple>());
[email protected]fa685ff2011-02-17 19:47:1365}
66
initial.commitd7cae122008-07-26 21:49:3867// Static Dispatchers with no out params.
68
tzik2387a8252016-08-25 13:57:1469template <typename Function, typename Tuple, size_t... Ns>
mdempsky6e7f6152014-12-10 03:10:5970inline void DispatchToFunctionImpl(Function function,
tzik2387a8252016-08-25 13:57:1471 Tuple&& args,
Jeremy Roman84956fa2017-08-16 15:55:2072 std::index_sequence<Ns...>) {
tzikf7c47572017-04-05 21:45:0373 (*function)(std::get<Ns>(std::forward<Tuple>(args))...);
initial.commitd7cae122008-07-26 21:49:3874}
75
tzik2387a8252016-08-25 13:57:1476template <typename Function, typename Tuple>
77inline void DispatchToFunction(Function function, Tuple&& args) {
78 DispatchToFunctionImpl(function, std::forward<Tuple>(args),
79 MakeIndexSequenceForTuple<Tuple>());
initial.commitd7cae122008-07-26 21:49:3880}
81
mdempsky6e7f6152014-12-10 03:10:5982// Dispatchers with out parameters.
83
84template <typename ObjT,
85 typename Method,
tzik2387a8252016-08-25 13:57:1486 typename InTuple,
87 typename OutTuple,
mdempsky6e7f6152014-12-10 03:10:5988 size_t... InNs,
89 size_t... OutNs>
tzik1ea87e3a2016-02-16 04:56:3190inline void DispatchToMethodImpl(const ObjT& obj,
mdempsky6e7f6152014-12-10 03:10:5991 Method method,
tzik2387a8252016-08-25 13:57:1492 InTuple&& in,
93 OutTuple* out,
Jeremy Roman84956fa2017-08-16 15:55:2094 std::index_sequence<InNs...>,
95 std::index_sequence<OutNs...>) {
tzikf7c47572017-04-05 21:45:0396 (obj->*method)(std::get<InNs>(std::forward<InTuple>(in))...,
tzik1068f1be2016-06-03 07:25:2097 &std::get<OutNs>(*out)...);
initial.commitd7cae122008-07-26 21:49:3898}
99
tzik2387a8252016-08-25 13:57:14100template <typename ObjT, typename Method, typename InTuple, typename OutTuple>
tzik1ea87e3a2016-02-16 04:56:31101inline void DispatchToMethod(const ObjT& obj,
[email protected]52a261f2009-03-03 15:01:12102 Method method,
tzik2387a8252016-08-25 13:57:14103 InTuple&& in,
104 OutTuple* out) {
105 DispatchToMethodImpl(obj, method, std::forward<InTuple>(in), out,
106 MakeIndexSequenceForTuple<InTuple>(),
107 MakeIndexSequenceForTuple<OutTuple>());
[email protected]8a2820a2008-10-09 21:58:05108}
109
brettwd5ca2bc2015-05-29 22:15:47110} // namespace base
111
tfarinaa31163512015-05-13 22:10:15112#endif // BASE_TUPLE_H_