blob: cbdcd764c45e3f6291d292406f38aba397359e1c [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2011 The Chromium Authors
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>
Peter Kasting134ef9af2024-12-28 02:30:0929
tzik9ca302192016-02-11 10:24:4530#include <tuple>
Jeremy Roman84956fa2017-08-16 15:55:2031#include <utility>
avi9b6f42932015-12-26 22:15:1432
avi9b6f42932015-12-26 22:15:1433#include "build/build_config.h"
[email protected]58ae6302013-06-27 12:48:0234
brettwd5ca2bc2015-05-29 22:15:4735namespace base {
36
initial.commitd7cae122008-07-26 21:49:3837// Dispatchers ----------------------------------------------------------------
38//
39// Helper functions that call the given method on an object, with the unpacked
40// tuple arguments. Notice that they all have the same number of arguments,
41// so you need only write:
42// DispatchToMethod(object, &Object::method, args);
43// This is very useful for templated dispatchers, since they don't need to know
44// what type |args| is.
45
46// Non-Static Dispatchers with no out params.
47
tzik2387a8252016-08-25 13:57:1448template <typename ObjT, typename Method, typename Tuple, size_t... Ns>
tzik1ea87e3a2016-02-16 04:56:3149inline void DispatchToMethodImpl(const ObjT& obj,
mdempsky6e7f6152014-12-10 03:10:5950 Method method,
tzik2387a8252016-08-25 13:57:1451 Tuple&& args,
Jeremy Roman84956fa2017-08-16 15:55:2052 std::index_sequence<Ns...>) {
tzikf7c47572017-04-05 21:45:0353 (obj->*method)(std::get<Ns>(std::forward<Tuple>(args))...);
initial.commitd7cae122008-07-26 21:49:3854}
55
tzik2387a8252016-08-25 13:57:1456template <typename ObjT, typename Method, typename Tuple>
Peter Kasting134ef9af2024-12-28 02:30:0957inline void DispatchToMethod(const ObjT& obj, Method method, Tuple&& args) {
Andrew Rayskiy6f021362023-10-19 19:55:0658 constexpr size_t size = std::tuple_size_v<std::decay_t<Tuple>>;
tzik2387a8252016-08-25 13:57:1459 DispatchToMethodImpl(obj, method, std::forward<Tuple>(args),
tzikf32dc9712017-10-07 00:37:1260 std::make_index_sequence<size>());
[email protected]fa685ff2011-02-17 19:47:1361}
62
initial.commitd7cae122008-07-26 21:49:3863// Static Dispatchers with no out params.
64
tzik2387a8252016-08-25 13:57:1465template <typename Function, typename Tuple, size_t... Ns>
mdempsky6e7f6152014-12-10 03:10:5966inline void DispatchToFunctionImpl(Function function,
tzik2387a8252016-08-25 13:57:1467 Tuple&& args,
Jeremy Roman84956fa2017-08-16 15:55:2068 std::index_sequence<Ns...>) {
tzikf7c47572017-04-05 21:45:0369 (*function)(std::get<Ns>(std::forward<Tuple>(args))...);
initial.commitd7cae122008-07-26 21:49:3870}
71
tzik2387a8252016-08-25 13:57:1472template <typename Function, typename Tuple>
73inline void DispatchToFunction(Function function, Tuple&& args) {
Andrew Rayskiy6f021362023-10-19 19:55:0674 constexpr size_t size = std::tuple_size_v<std::decay_t<Tuple>>;
tzik2387a8252016-08-25 13:57:1475 DispatchToFunctionImpl(function, std::forward<Tuple>(args),
tzikf32dc9712017-10-07 00:37:1276 std::make_index_sequence<size>());
initial.commitd7cae122008-07-26 21:49:3877}
78
mdempsky6e7f6152014-12-10 03:10:5979// Dispatchers with out parameters.
80
81template <typename ObjT,
82 typename Method,
tzik2387a8252016-08-25 13:57:1483 typename InTuple,
84 typename OutTuple,
mdempsky6e7f6152014-12-10 03:10:5985 size_t... InNs,
86 size_t... OutNs>
tzik1ea87e3a2016-02-16 04:56:3187inline void DispatchToMethodImpl(const ObjT& obj,
mdempsky6e7f6152014-12-10 03:10:5988 Method method,
tzik2387a8252016-08-25 13:57:1489 InTuple&& in,
90 OutTuple* out,
Jeremy Roman84956fa2017-08-16 15:55:2091 std::index_sequence<InNs...>,
92 std::index_sequence<OutNs...>) {
tzikf7c47572017-04-05 21:45:0393 (obj->*method)(std::get<InNs>(std::forward<InTuple>(in))...,
tzik1068f1be2016-06-03 07:25:2094 &std::get<OutNs>(*out)...);
initial.commitd7cae122008-07-26 21:49:3895}
96
tzik2387a8252016-08-25 13:57:1497template <typename ObjT, typename Method, typename InTuple, typename OutTuple>
tzik1ea87e3a2016-02-16 04:56:3198inline void DispatchToMethod(const ObjT& obj,
[email protected]52a261f2009-03-03 15:01:1299 Method method,
tzik2387a8252016-08-25 13:57:14100 InTuple&& in,
101 OutTuple* out) {
Andrew Rayskiy6f021362023-10-19 19:55:06102 constexpr size_t in_size = std::tuple_size_v<std::decay_t<InTuple>>;
103 constexpr size_t out_size = std::tuple_size_v<OutTuple>;
tzik2387a8252016-08-25 13:57:14104 DispatchToMethodImpl(obj, method, std::forward<InTuple>(in), out,
tzikf32dc9712017-10-07 00:37:12105 std::make_index_sequence<in_size>(),
106 std::make_index_sequence<out_size>());
[email protected]8a2820a2008-10-09 21:58:05107}
108
brettwd5ca2bc2015-05-29 22:15:47109} // namespace base
110
tfarinaa31163512015-05-13 22:10:15111#endif // BASE_TUPLE_H_