Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(27)

Side by Side Diff: base/callback_tuple.h

Issue 743853002: Bind: Use VariadicTemplates in bind_internal.h (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@05_combined
Patch Set: disable back nocompile tests Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« base/bind_internal.h ('K') | « base/bind_unittest.nc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 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 // Helper template classes for Bind implementation.
6
7 #ifndef BASE_CALLBACK_TUPLE_H_
8 #define BASE_CALLBACK_TUPLE_H_
9
10 namespace base {
11 namespace internal {
12
13 // Packs a list of types to hold them in a single type.
14 template <typename... Types>
15 struct TypeList {};
16
17 // Used for DropTypeListItem implementation.
18 template <size_t n, typename List>
19 struct DropTypeListItemImpl;
20
21 // Do not use enable_if and SFINAE here to avoid MSVC2013 compile failure.
22 template <size_t n, typename T, typename... List>
23 struct DropTypeListItemImpl<n, TypeList<T, List...>>
24 : DropTypeListItemImpl<n - 1, TypeList<List...>> {};
25
26 template <typename T, typename... List>
27 struct DropTypeListItemImpl<0, TypeList<T, List...>> {
28 typedef TypeList<T, List...> Type;
29 };
30
31 template <>
32 struct DropTypeListItemImpl<0, TypeList<>> {
33 typedef TypeList<> Type;
34 };
35
36 // A type-level function that drops |n| list item from given TypeList.
37 template <size_t n, typename List>
38 using DropTypeListItem = typename DropTypeListItemImpl<n, List>::Type;
39
40 // Used for ConcatTypeLists implementation.
41 template <typename List1, typename List2>
42 struct ConcatTypeListsImpl;
43
44 template <typename... Types1, typename... Types2>
45 struct ConcatTypeListsImpl<TypeList<Types1...>, TypeList<Types2...>> {
46 typedef TypeList<Types1..., Types2...> Type;
47 };
48
49 // A type-level function that concats two TypeLists.
50 template <typename List1, typename List2>
51 using ConcatTypeLists = typename ConcatTypeListsImpl<List1, List2>::Type;
52
53 template <size_t n, typename List>
54 struct NthTypeImpl;
55
56 template <size_t n, typename T, typename... Types>
57 struct NthTypeImpl<n, TypeList<T, Types...> >
58 : NthTypeImpl<n - 1, TypeList<Types...>> {
Nico 2014/12/08 19:58:08 :-/ (I know that there's no better way)
59 };
60
61 template <typename T, typename... Types>
62 struct NthTypeImpl<0, TypeList<T, Types...> > {
63 typedef T Type;
64 };
65
66 // A type-level function that extracts |n|th type from a TypeList.
67 template <size_t n, typename List>
68 using NthType = typename NthTypeImpl<n, List>::Type;
69
70 // Used for MakeFunctionType implementation.
71 template <typename R, typename ArgList>
72 struct MakeFunctionTypeImpl;
73
74 template <typename R, typename... Args>
75 struct MakeFunctionTypeImpl<R, TypeList<Args...>> {
76 typedef R(Type)(Args...);
77 };
78
79 // A type-level function that constructs a function type that has |R| as its
80 // return type and has TypeLists items as its arguments.
81 template <typename R, typename ArgList>
82 using MakeFunctionType = typename MakeFunctionTypeImpl<R, ArgList>::Type;
83
84 // Holds a list of indexes as a single type.
85 template <size_t... indexes>
86 struct IndexSequence {};
87
88 template <size_t n, size_t... sequence>
89 struct MakeIndexSequenceImpl
90 : MakeIndexSequenceImpl<n - 1, n - 1, sequence...> {};
91
92 template <size_t... sequence>
93 struct MakeIndexSequenceImpl<0, sequence...> {
94 typedef IndexSequence<sequence...> Type;
95 };
96
97 // A type-level function that makes IndexSequence<0, 1, .., n - 1>.
98 template <size_t n>
99 using MakeIndexSequence = typename MakeIndexSequenceImpl<n>::Type;
100
101 // Used for Tuple<> implementation. Holds |n|th item of a Tuple<>.
102 template <size_t n, typename T>
103 struct IndexedTupleItem {
104 explicit IndexedTupleItem(const T& value) : value_(value) {}
105 T value_;
106 };
107
108 //
109 // Implementation note: Unspecialized definition handles the empty case.
110 template <size_t n, typename... Types>
111 struct TupleBody {};
112
113 // Implementation note: Handles non-empty case of Tuple construction.
114 template <size_t n, typename T, typename... Types>
115 struct TupleBody<n, T, Types...>
116 : IndexedTupleItem<n, T>, TupleBody<n + 1, Types...>{
117 explicit TupleBody(const T& head, const Types&... tail)
118 : IndexedTupleItem<n, T>(head), TupleBody<n + 1, Types...>(tail...) {}
119 };
120
121 template <typename... Types>
122 using Tuple = TupleBody<0, Types...>;
123
124 // Extracts |i|th item from a tuple.
125 // Example:
126 // Tuple<int, std::string, void*> tuple(1, "foo", nullptr);
127 // std::string tuple_item = *GetTupleItem<1>(&tuple);
128 template <size_t i, typename T>
129 T* GetTupleItem(IndexedTupleItem<i, T>* item) {
130 return &item->value_;
131 }
132
133 template <size_t i, typename T>
134 const T* GetTupleItem(const IndexedTupleItem<i, T>* item) {
135 return &item->value_;
136 }
137
138 } // namespace internal
139 } // namespace base
140
141 #endif // BASE_CALLBACK_TUPLE_H_
OLDNEW
« base/bind_internal.h ('K') | « base/bind_unittest.nc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698