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

Side by Side Diff: base/callback_internal.h

Issue 610423003: [Base] Use variadic template in base/callback.h (wave 1) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: workaround for win toolchain Created 6 years, 2 months 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
« no previous file with comments | « base/callback.h.pump ('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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 // This file contains utility functions and classes that help the 5 // This file contains utility functions and classes that help the
6 // implementation, and management of the Callback objects. 6 // implementation, and management of the Callback objects.
7 7
8 #ifndef BASE_CALLBACK_INTERNAL_H_ 8 #ifndef BASE_CALLBACK_INTERNAL_H_
9 #define BASE_CALLBACK_INTERNAL_H_ 9 #define BASE_CALLBACK_INTERNAL_H_
10 10
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 template <typename U> 74 template <typename U>
75 static YesType Test(const typename U::MoveOnlyTypeForCPP03*); 75 static YesType Test(const typename U::MoveOnlyTypeForCPP03*);
76 76
77 template <typename U> 77 template <typename U>
78 static NoType Test(...); 78 static NoType Test(...);
79 79
80 static const bool value = sizeof(Test<T>(0)) == sizeof(YesType) && 80 static const bool value = sizeof(Test<T>(0)) == sizeof(YesType) &&
81 !is_const<T>::value; 81 !is_const<T>::value;
82 }; 82 };
83 83
84 // Returns Then as |type| if condition is true. Otherwise returns Else.
Aaron Boodman 2014/09/30 21:05:37 The pipes are usually used around param names in c
tzik 2014/10/01 01:22:40 Done.
85 template <bool condition, typename Then, typename Else>
86 struct SelectType {
87 typedef Then type;
88 };
89
90 template <typename Then, typename Else>
91 struct SelectType<false, Then, Else> {
92 typedef Else type;
93 };
94
95 template <typename>
96 struct CallbackParamTraitsForMoveOnlyType;
97
98 template <typename>
99 struct CallbackParamTraitsForNonMoveOnlyType;
Nico 2014/09/30 11:41:04 Should there be a "TODO: Use a default parameter o
tzik 2014/09/30 14:43:10 Done.
100
84 // This is a typetraits object that's used to take an argument type, and 101 // This is a typetraits object that's used to take an argument type, and
85 // extract a suitable type for storing and forwarding arguments. 102 // extract a suitable type for storing and forwarding arguments.
86 // 103 //
87 // In particular, it strips off references, and converts arrays to 104 // In particular, it strips off references, and converts arrays to
88 // pointers for storage; and it avoids accidentally trying to create a 105 // pointers for storage; and it avoids accidentally trying to create a
89 // "reference of a reference" if the argument is a reference type. 106 // "reference of a reference" if the argument is a reference type.
90 // 107 //
91 // This array type becomes an issue for storage because we are passing bound 108 // This array type becomes an issue for storage because we are passing bound
92 // parameters by const reference. In this case, we end up passing an actual 109 // parameters by const reference. In this case, we end up passing an actual
93 // array type in the initializer list which C++ does not allow. This will 110 // array type in the initializer list which C++ does not allow. This will
94 // break passing of C-string literals. 111 // break passing of C-string literals.
95 template <typename T, bool is_move_only = IsMoveOnlyType<T>::value> 112 template <typename T>
96 struct CallbackParamTraits { 113 struct CallbackParamTraits
114 : SelectType<IsMoveOnlyType<T>::value,
115 CallbackParamTraitsForMoveOnlyType<T>,
116 CallbackParamTraitsForNonMoveOnlyType<T> >::type {
117 };
118
119 template <typename T>
120 struct CallbackParamTraitsForNonMoveOnlyType {
97 typedef const T& ForwardType; 121 typedef const T& ForwardType;
98 typedef T StorageType; 122 typedef T StorageType;
99 }; 123 };
100 124
101 // The Storage should almost be impossible to trigger unless someone manually 125 // The Storage should almost be impossible to trigger unless someone manually
102 // specifies type of the bind parameters. However, in case they do, 126 // specifies type of the bind parameters. However, in case they do,
103 // this will guard against us accidentally storing a reference parameter. 127 // this will guard against us accidentally storing a reference parameter.
104 // 128 //
105 // The ForwardType should only be used for unbound arguments. 129 // The ForwardType should only be used for unbound arguments.
106 template <typename T> 130 template <typename T>
107 struct CallbackParamTraits<T&, false> { 131 struct CallbackParamTraitsForNonMoveOnlyType<T&> {
108 typedef T& ForwardType; 132 typedef T& ForwardType;
109 typedef T StorageType; 133 typedef T StorageType;
110 }; 134 };
111 135
112 // Note that for array types, we implicitly add a const in the conversion. This 136 // Note that for array types, we implicitly add a const in the conversion. This
113 // means that it is not possible to bind array arguments to functions that take 137 // means that it is not possible to bind array arguments to functions that take
114 // a non-const pointer. Trying to specialize the template based on a "const 138 // a non-const pointer. Trying to specialize the template based on a "const
115 // T[n]" does not seem to match correctly, so we are stuck with this 139 // T[n]" does not seem to match correctly, so we are stuck with this
116 // restriction. 140 // restriction.
117 template <typename T, size_t n> 141 template <typename T, size_t n>
118 struct CallbackParamTraits<T[n], false> { 142 struct CallbackParamTraitsForNonMoveOnlyType<T[n]> {
119 typedef const T* ForwardType; 143 typedef const T* ForwardType;
120 typedef const T* StorageType; 144 typedef const T* StorageType;
121 }; 145 };
122 146
123 // See comment for CallbackParamTraits<T[n]>. 147 // See comment for CallbackParamTraits<T[n]>.
124 template <typename T> 148 template <typename T>
125 struct CallbackParamTraits<T[], false> { 149 struct CallbackParamTraitsForNonMoveOnlyType<T[]> {
126 typedef const T* ForwardType; 150 typedef const T* ForwardType;
127 typedef const T* StorageType; 151 typedef const T* StorageType;
128 }; 152 };
129 153
130 // Parameter traits for movable-but-not-copyable scopers. 154 // Parameter traits for movable-but-not-copyable scopers.
131 // 155 //
132 // Callback<>/Bind() understands movable-but-not-copyable semantics where 156 // Callback<>/Bind() understands movable-but-not-copyable semantics where
133 // the type cannot be copied but can still have its state destructively 157 // the type cannot be copied but can still have its state destructively
134 // transferred (aka. moved) to another instance of the same type by calling a 158 // transferred (aka. moved) to another instance of the same type by calling a
135 // helper function. When used with Bind(), this signifies transferal of the 159 // helper function. When used with Bind(), this signifies transferal of the
136 // object's state to the target function. 160 // object's state to the target function.
137 // 161 //
138 // For these types, the ForwardType must not be a const reference, or a 162 // For these types, the ForwardType must not be a const reference, or a
139 // reference. A const reference is inappropriate, and would break const 163 // reference. A const reference is inappropriate, and would break const
140 // correctness, because we are implementing a destructive move. A non-const 164 // correctness, because we are implementing a destructive move. A non-const
141 // reference cannot be used with temporaries which means the result of a 165 // reference cannot be used with temporaries which means the result of a
142 // function or a cast would not be usable with Callback<> or Bind(). 166 // function or a cast would not be usable with Callback<> or Bind().
143 template <typename T> 167 template <typename T>
144 struct CallbackParamTraits<T, true> { 168 struct CallbackParamTraitsForMoveOnlyType {
145 typedef T ForwardType; 169 typedef T ForwardType;
146 typedef T StorageType; 170 typedef T StorageType;
147 }; 171 };
148 172
149 // CallbackForward() is a very limited simulation of C++11's std::forward() 173 // CallbackForward() is a very limited simulation of C++11's std::forward()
150 // used by the Callback/Bind system for a set of movable-but-not-copyable 174 // used by the Callback/Bind system for a set of movable-but-not-copyable
151 // types. It is needed because forwarding a movable-but-not-copyable 175 // types. It is needed because forwarding a movable-but-not-copyable
152 // argument to another function requires us to invoke the proper move 176 // argument to another function requires us to invoke the proper move
153 // operator to create a rvalue version of the type. The supported types are 177 // operator to create a rvalue version of the type. The supported types are
154 // whitelisted below as overloads of the CallbackForward() function. The 178 // whitelisted below as overloads of the CallbackForward() function. The
(...skipping 14 matching lines...) Expand all
169 193
170 template <typename T> 194 template <typename T>
171 typename enable_if<IsMoveOnlyType<T>::value, T>::type CallbackForward(T& t) { 195 typename enable_if<IsMoveOnlyType<T>::value, T>::type CallbackForward(T& t) {
172 return t.Pass(); 196 return t.Pass();
173 } 197 }
174 198
175 } // namespace internal 199 } // namespace internal
176 } // namespace base 200 } // namespace base
177 201
178 #endif // BASE_CALLBACK_INTERNAL_H_ 202 #endif // BASE_CALLBACK_INTERNAL_H_
OLDNEW
« no previous file with comments | « base/callback.h.pump ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698