1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 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 9 // call. 10 // 11 // Example usage: 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"); 15 // 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") 20 // 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); 24 25 #ifndef BASE_TUPLE_H_ 26 #define BASE_TUPLE_H_ 27 28 #include <stddef.h> 29 #include <tuple> 30 31 #include "base/bind_helpers.h" 32 #include "build/build_config.h" 33 34 namespace base { 35 36 // Index sequences 37 // 38 // Minimal clone of the similarly-named C++14 functionality. 39 40 template <size_t...> 41 struct IndexSequence {}; 42 43 template <size_t... Ns> 44 struct MakeIndexSequenceImpl; 45 46 #if defined(_PREFAST_) && defined(OS_WIN) 47 48 // Work around VC++ 2013 /analyze internal compiler error: 49 // https://connect.microsoft.com/VisualStudio/feedback/details/1053626 50 51 template <> struct MakeIndexSequenceImpl<0> { 52 using Type = IndexSequence<>; 53 }; 54 template <> struct MakeIndexSequenceImpl<1> { 55 using Type = IndexSequence<0>; 56 }; 57 template <> struct MakeIndexSequenceImpl<2> { 58 using Type = IndexSequence<0,1>; 59 }; 60 template <> struct MakeIndexSequenceImpl<3> { 61 using Type = IndexSequence<0,1,2>; 62 }; 63 template <> struct MakeIndexSequenceImpl<4> { 64 using Type = IndexSequence<0,1,2,3>; 65 }; 66 template <> struct MakeIndexSequenceImpl<5> { 67 using Type = IndexSequence<0,1,2,3,4>; 68 }; 69 template <> struct MakeIndexSequenceImpl<6> { 70 using Type = IndexSequence<0,1,2,3,4,5>; 71 }; 72 template <> struct MakeIndexSequenceImpl<7> { 73 using Type = IndexSequence<0,1,2,3,4,5,6>; 74 }; 75 template <> struct MakeIndexSequenceImpl<8> { 76 using Type = IndexSequence<0,1,2,3,4,5,6,7>; 77 }; 78 template <> struct MakeIndexSequenceImpl<9> { 79 using Type = IndexSequence<0,1,2,3,4,5,6,7,8>; 80 }; 81 template <> struct MakeIndexSequenceImpl<10> { 82 using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9>; 83 }; 84 template <> struct MakeIndexSequenceImpl<11> { 85 using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9,10>; 86 }; 87 template <> struct MakeIndexSequenceImpl<12> { 88 using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9,10,11>; 89 }; 90 template <> struct MakeIndexSequenceImpl<13> { 91 using Type = IndexSequence<0,1,2,3,4,5,6,7,8,9,10,11,12>; 92 }; 93 94 #else // defined(OS_WIN) && defined(_PREFAST_) 95 96 template <size_t... Ns> 97 struct MakeIndexSequenceImpl<0, Ns...> { 98 using Type = IndexSequence<Ns...>; 99 }; 100 101 template <size_t N, size_t... Ns> 102 struct MakeIndexSequenceImpl<N, Ns...> 103 : MakeIndexSequenceImpl<N - 1, N - 1, Ns...> {}; 104 105 #endif // defined(OS_WIN) && defined(_PREFAST_) 106 107 // std::get() in <=libstdc++-4.6 returns an lvalue-reference for 108 // rvalue-reference of a tuple, where an rvalue-reference is expected. 109 template <size_t I, typename... Ts> 110 typename std::tuple_element<I, std::tuple<Ts...>>::type&& get( 111 std::tuple<Ts...>&& t) { 112 using ElemType = typename std::tuple_element<I, std::tuple<Ts...>>::type; 113 return std::forward<ElemType>(std::get<I>(t)); 114 } 115 116 template <size_t I, typename T> 117 auto get(T& t) -> decltype(std::get<I>(t)) { 118 return std::get<I>(t); 119 } 120 121 template <size_t N> 122 using MakeIndexSequence = typename MakeIndexSequenceImpl<N>::Type; 123 124 // Dispatchers ---------------------------------------------------------------- 125 // 126 // Helper functions that call the given method on an object, with the unpacked 127 // tuple arguments. Notice that they all have the same number of arguments, 128 // so you need only write: 129 // DispatchToMethod(object, &Object::method, args); 130 // This is very useful for templated dispatchers, since they don't need to know 131 // what type |args| is. 132 133 // Non-Static Dispatchers with no out params. 134 135 template <typename ObjT, typename Method, typename... Ts, size_t... Ns> 136 inline void DispatchToMethodImpl(const ObjT& obj, 137 Method method, 138 const std::tuple<Ts...>& arg, 139 IndexSequence<Ns...>) { 140 (obj->*method)(internal::Unwrap(std::get<Ns>(arg))...); 141 } 142 143 template <typename ObjT, typename Method, typename... Ts> 144 inline void DispatchToMethod(const ObjT& obj, 145 Method method, 146 const std::tuple<Ts...>& arg) { 147 DispatchToMethodImpl(obj, method, arg, MakeIndexSequence<sizeof...(Ts)>()); 148 } 149 150 // Static Dispatchers with no out params. 151 152 template <typename Function, typename... Ts, size_t... Ns> 153 inline void DispatchToFunctionImpl(Function function, 154 const std::tuple<Ts...>& arg, 155 IndexSequence<Ns...>) { 156 (*function)(internal::Unwrap(std::get<Ns>(arg))...); 157 } 158 159 template <typename Function, typename... Ts> 160 inline void DispatchToFunction(Function function, 161 const std::tuple<Ts...>& arg) { 162 DispatchToFunctionImpl(function, arg, MakeIndexSequence<sizeof...(Ts)>()); 163 } 164 165 // Dispatchers with out parameters. 166 167 template <typename ObjT, 168 typename Method, 169 typename... InTs, 170 typename... OutTs, 171 size_t... InNs, 172 size_t... OutNs> 173 inline void DispatchToMethodImpl(const ObjT& obj, 174 Method method, 175 const std::tuple<InTs...>& in, 176 std::tuple<OutTs...>* out, 177 IndexSequence<InNs...>, 178 IndexSequence<OutNs...>) { 179 (obj->*method)(internal::Unwrap(std::get<InNs>(in))..., 180 &std::get<OutNs>(*out)...); 181 } 182 183 template <typename ObjT, typename Method, typename... InTs, typename... OutTs> 184 inline void DispatchToMethod(const ObjT& obj, 185 Method method, 186 const std::tuple<InTs...>& in, 187 std::tuple<OutTs...>* out) { 188 DispatchToMethodImpl(obj, method, in, out, 189 MakeIndexSequence<sizeof...(InTs)>(), 190 MakeIndexSequence<sizeof...(OutTs)>()); 191 } 192 193 } // namespace base 194 195 #endif // BASE_TUPLE_H_ 196