1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // 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
12 // // These two methods of creating a Tuple are identical.
13 // std::tuple<int, const char*> tuple_a(1, "wee");
14 // std::tuple<int, const char*> tuple_b = std::make_tuple(1, "wee");
16 // void SomeFunc(int a, const char* b) { }
17 // DispatchToFunction(&SomeFunc, tuple_a); // SomeFunc(1, "wee")
18 // DispatchToFunction(
19 // &SomeFunc, std::make_tuple(10, "foo")); // SomeFunc(10, "foo")
21 // struct { void SomeMeth(int a, int b, int c) { } } foo;
22 // DispatchToMethod(&foo, &Foo::SomeMeth, std::make_tuple(1, 2, 3));
23 // // foo->SomeMeth(1, 2, 3);
32 #include "build/build_config.h"
36 // Dispatchers ----------------------------------------------------------------
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.
45 // Non-Static Dispatchers with no out params.
47 template <typename ObjT, typename Method, typename Tuple, size_t... Ns>
48 inline void DispatchToMethodImpl(const ObjT& obj,
51 std::index_sequence<Ns...>) {
52 (obj->*method)(std::get<Ns>(std::forward<Tuple>(args))...);
55 template <typename ObjT, typename Method, typename Tuple>
56 inline void DispatchToMethod(const ObjT& obj,
59 constexpr size_t size = std::tuple_size<std::decay_t<Tuple>>::value;
60 DispatchToMethodImpl(obj, method, std::forward<Tuple>(args),
61 std::make_index_sequence<size>());
64 // Static Dispatchers with no out params.
66 template <typename Function, typename Tuple, size_t... Ns>
67 inline void DispatchToFunctionImpl(Function function,
69 std::index_sequence<Ns...>) {
70 (*function)(std::get<Ns>(std::forward<Tuple>(args))...);
73 template <typename Function, typename Tuple>
74 inline void DispatchToFunction(Function function, Tuple&& args) {
75 constexpr size_t size = std::tuple_size<std::decay_t<Tuple>>::value;
76 DispatchToFunctionImpl(function, std::forward<Tuple>(args),
77 std::make_index_sequence<size>());
80 // Dispatchers with out parameters.
82 template <typename ObjT,
88 inline void DispatchToMethodImpl(const ObjT& obj,
92 std::index_sequence<InNs...>,
93 std::index_sequence<OutNs...>) {
94 (obj->*method)(std::get<InNs>(std::forward<InTuple>(in))...,
95 &std::get<OutNs>(*out)...);
98 template <typename ObjT, typename Method, typename InTuple, typename OutTuple>
99 inline void DispatchToMethod(const ObjT& obj,
103 constexpr size_t in_size = std::tuple_size<std::decay_t<InTuple>>::value;
104 constexpr size_t out_size = std::tuple_size<OutTuple>::value;
105 DispatchToMethodImpl(obj, method, std::forward<InTuple>(in), out,
106 std::make_index_sequence<in_size>(),
107 std::make_index_sequence<out_size>());
112 #endif // BASE_TUPLE_H_