OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Use std::tuple as tuple type. This file contains helper functions for | 5 // Use std::tuple as tuple type. This file contains helper functions for |
6 // working with std::tuples. | 6 // working with std::tuples. |
7 // The functions DispatchToMethod and DispatchToFunction take a function pointer | 7 // The functions DispatchToMethod and DispatchToFunction take a function pointer |
8 // or instance and method pointer, and unpack a tuple into arguments to the | 8 // or instance and method pointer, and unpack a tuple into arguments to the |
9 // call. | 9 // call. |
10 // | 10 // |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 struct MakeIndexSequenceImpl<0, Ns...> { | 97 struct MakeIndexSequenceImpl<0, Ns...> { |
98 using Type = IndexSequence<Ns...>; | 98 using Type = IndexSequence<Ns...>; |
99 }; | 99 }; |
100 | 100 |
101 template <size_t N, size_t... Ns> | 101 template <size_t N, size_t... Ns> |
102 struct MakeIndexSequenceImpl<N, Ns...> | 102 struct MakeIndexSequenceImpl<N, Ns...> |
103 : MakeIndexSequenceImpl<N - 1, N - 1, Ns...> {}; | 103 : MakeIndexSequenceImpl<N - 1, N - 1, Ns...> {}; |
104 | 104 |
105 #endif // defined(OS_WIN) && defined(_PREFAST_) | 105 #endif // defined(OS_WIN) && defined(_PREFAST_) |
106 | 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 |
107 template <size_t N> | 121 template <size_t N> |
108 using MakeIndexSequence = typename MakeIndexSequenceImpl<N>::Type; | 122 using MakeIndexSequence = typename MakeIndexSequenceImpl<N>::Type; |
109 | 123 |
110 // Dispatchers ---------------------------------------------------------------- | 124 // Dispatchers ---------------------------------------------------------------- |
111 // | 125 // |
112 // Helper functions that call the given method on an object, with the unpacked | 126 // Helper functions that call the given method on an object, with the unpacked |
113 // tuple arguments. Notice that they all have the same number of arguments, | 127 // tuple arguments. Notice that they all have the same number of arguments, |
114 // so you need only write: | 128 // so you need only write: |
115 // DispatchToMethod(object, &Object::method, args); | 129 // DispatchToMethod(object, &Object::method, args); |
116 // This is very useful for templated dispatchers, since they don't need to know | 130 // This is very useful for templated dispatchers, since they don't need to know |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 const std::tuple<InTs...>& in, | 186 const std::tuple<InTs...>& in, |
173 std::tuple<OutTs...>* out) { | 187 std::tuple<OutTs...>* out) { |
174 DispatchToMethodImpl(obj, method, in, out, | 188 DispatchToMethodImpl(obj, method, in, out, |
175 MakeIndexSequence<sizeof...(InTs)>(), | 189 MakeIndexSequence<sizeof...(InTs)>(), |
176 MakeIndexSequence<sizeof...(OutTs)>()); | 190 MakeIndexSequence<sizeof...(OutTs)>()); |
177 } | 191 } |
178 | 192 |
179 } // namespace base | 193 } // namespace base |
180 | 194 |
181 #endif // BASE_TUPLE_H_ | 195 #endif // BASE_TUPLE_H_ |
OLD | NEW |