blob: 58681d515a8b954d145d16a84876d12e5e2eb705 [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
initial.commitd7cae122008-07-26 21:49:3836// Dispatchers ----------------------------------------------------------------
37//
38// Helper functions that call the given method on an object, with the unpacked
39// tuple arguments. Notice that they all have the same number of arguments,
40// so you need only write:
41// DispatchToMethod(object, &Object::method, args);
42// This is very useful for templated dispatchers, since they don't need to know
43// what type |args| is.
44
45// Non-Static Dispatchers with no out params.
46
tzik2387a8252016-08-25 13:57:1447template <typename ObjT, typename Method, typename Tuple, size_t... Ns>
tzik1ea87e3a2016-02-16 04:56:3148inline void DispatchToMethodImpl(const ObjT& obj,
mdempsky6e7f6152014-12-10 03:10:5949 Method method,
tzik2387a8252016-08-25 13:57:1450 Tuple&& args,
Jeremy Roman84956fa2017-08-16 15:55:2051 std::index_sequence<Ns...>) {
tzikf7c47572017-04-05 21:45:0352 (obj->*method)(std::get<Ns>(std::forward<Tuple>(args))...);
initial.commitd7cae122008-07-26 21:49:3853}
54
tzik2387a8252016-08-25 13:57:1455template <typename ObjT, typename Method, typename Tuple>
tzik1ea87e3a2016-02-16 04:56:3156inline void DispatchToMethod(const ObjT& obj,
[email protected]52a261f2009-03-03 15:01:1257 Method method,
tzik2387a8252016-08-25 13:57:1458 Tuple&& args) {
tzikf32dc9712017-10-07 00:37:1259 constexpr size_t size = std::tuple_size<std::decay_t<Tuple>>::value;
tzik2387a8252016-08-25 13:57:1460 DispatchToMethodImpl(obj, method, std::forward<Tuple>(args),
tzikf32dc9712017-10-07 00:37:1261 std::make_index_sequence<size>());
[email protected]fa685ff2011-02-17 19:47:1362}
63
initial.commitd7cae122008-07-26 21:49:3864// Static Dispatchers with no out params.
65
tzik2387a8252016-08-25 13:57:1466template <typename Function, typename Tuple, size_t... Ns>
mdempsky6e7f6152014-12-10 03:10:5967inline void DispatchToFunctionImpl(Function function,
tzik2387a8252016-08-25 13:57:1468 Tuple&& args,
Jeremy Roman84956fa2017-08-16 15:55:2069 std::index_sequence<Ns...>) {
tzikf7c47572017-04-05 21:45:0370 (*function)(std::get<Ns>(std::forward<Tuple>(args))...);
initial.commitd7cae122008-07-26 21:49:3871}
72
tzik2387a8252016-08-25 13:57:1473template <typename Function, typename Tuple>
74inline void DispatchToFunction(Function function, Tuple&& args) {
tzikf32dc9712017-10-07 00:37:1275 constexpr size_t size = std::tuple_size<std::decay_t<Tuple>>::value;
tzik2387a8252016-08-25 13:57:1476 DispatchToFunctionImpl(function, std::forward<Tuple>(args),
tzikf32dc9712017-10-07 00:37:1277 std::make_index_sequence<size>());
initial.commitd7cae122008-07-26 21:49:3878}
79
mdempsky6e7f6152014-12-10 03:10:5980// Dispatchers with out parameters.
81
82template <typename ObjT,
83 typename Method,
tzik2387a8252016-08-25 13:57:1484 typename InTuple,
85 typename OutTuple,
mdempsky6e7f6152014-12-10 03:10:5986 size_t... InNs,
87 size_t... OutNs>
tzik1ea87e3a2016-02-16 04:56:3188inline void DispatchToMethodImpl(const ObjT& obj,
mdempsky6e7f6152014-12-10 03:10:5989 Method method,
tzik2387a8252016-08-25 13:57:1490 InTuple&& in,
91 OutTuple* out,
Jeremy Roman84956fa2017-08-16 15:55:2092 std::index_sequence<InNs...>,
93 std::index_sequence<OutNs...>) {
tzikf7c47572017-04-05 21:45:0394 (obj->*method)(std::get<InNs>(std::forward<InTuple>(in))...,
tzik1068f1be2016-06-03 07:25:2095 &std::get<OutNs>(*out)...);
initial.commitd7cae122008-07-26 21:49:3896}
97
tzik2387a8252016-08-25 13:57:1498template <typename ObjT, typename Method, typename InTuple, typename OutTuple>
tzik1ea87e3a2016-02-16 04:56:3199inline void DispatchToMethod(const ObjT& obj,
[email protected]52a261f2009-03-03 15:01:12100 Method method,
tzik2387a8252016-08-25 13:57:14101 InTuple&& in,
102 OutTuple* out) {
tzikf32dc9712017-10-07 00:37:12103 constexpr size_t in_size = std::tuple_size<std::decay_t<InTuple>>::value;
104 constexpr size_t out_size = std::tuple_size<OutTuple>::value;
tzik2387a8252016-08-25 13:57:14105 DispatchToMethodImpl(obj, method, std::forward<InTuple>(in), out,
tzikf32dc9712017-10-07 00:37:12106 std::make_index_sequence<in_size>(),
107 std::make_index_sequence<out_size>());
[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_