OLD | NEW |
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 Loading... |
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 SelectType::Type if |condition| is true. Otherwise returns |
| 85 // |Else|. |
| 86 template <bool condition, typename Then, typename Else> |
| 87 struct SelectType { |
| 88 typedef Then Type; |
| 89 }; |
| 90 |
| 91 template <typename Then, typename Else> |
| 92 struct SelectType<false, Then, Else> { |
| 93 typedef Else Type; |
| 94 }; |
| 95 |
| 96 template <typename> |
| 97 struct CallbackParamTraitsForMoveOnlyType; |
| 98 |
| 99 template <typename> |
| 100 struct CallbackParamTraitsForNonMoveOnlyType; |
| 101 |
| 102 // TODO(tzik): Use a default parameter once MSVS supports variadic templates |
| 103 // with default values. |
| 104 // http://connect.microsoft.com/VisualStudio/feedbackdetail/view/957801/compilat
ion-error-with-variadic-templates |
| 105 // |
84 // This is a typetraits object that's used to take an argument type, and | 106 // This is a typetraits object that's used to take an argument type, and |
85 // extract a suitable type for storing and forwarding arguments. | 107 // extract a suitable type for storing and forwarding arguments. |
86 // | 108 // |
87 // In particular, it strips off references, and converts arrays to | 109 // In particular, it strips off references, and converts arrays to |
88 // pointers for storage; and it avoids accidentally trying to create a | 110 // pointers for storage; and it avoids accidentally trying to create a |
89 // "reference of a reference" if the argument is a reference type. | 111 // "reference of a reference" if the argument is a reference type. |
90 // | 112 // |
91 // This array type becomes an issue for storage because we are passing bound | 113 // 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 | 114 // 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 | 115 // array type in the initializer list which C++ does not allow. This will |
94 // break passing of C-string literals. | 116 // break passing of C-string literals. |
95 template <typename T, bool is_move_only = IsMoveOnlyType<T>::value> | 117 template <typename T> |
96 struct CallbackParamTraits { | 118 struct CallbackParamTraits |
| 119 : SelectType<IsMoveOnlyType<T>::value, |
| 120 CallbackParamTraitsForMoveOnlyType<T>, |
| 121 CallbackParamTraitsForNonMoveOnlyType<T> >::Type { |
| 122 }; |
| 123 |
| 124 template <typename T> |
| 125 struct CallbackParamTraitsForNonMoveOnlyType { |
97 typedef const T& ForwardType; | 126 typedef const T& ForwardType; |
98 typedef T StorageType; | 127 typedef T StorageType; |
99 }; | 128 }; |
100 | 129 |
101 // The Storage should almost be impossible to trigger unless someone manually | 130 // The Storage should almost be impossible to trigger unless someone manually |
102 // specifies type of the bind parameters. However, in case they do, | 131 // specifies type of the bind parameters. However, in case they do, |
103 // this will guard against us accidentally storing a reference parameter. | 132 // this will guard against us accidentally storing a reference parameter. |
104 // | 133 // |
105 // The ForwardType should only be used for unbound arguments. | 134 // The ForwardType should only be used for unbound arguments. |
106 template <typename T> | 135 template <typename T> |
107 struct CallbackParamTraits<T&, false> { | 136 struct CallbackParamTraitsForNonMoveOnlyType<T&> { |
108 typedef T& ForwardType; | 137 typedef T& ForwardType; |
109 typedef T StorageType; | 138 typedef T StorageType; |
110 }; | 139 }; |
111 | 140 |
112 // Note that for array types, we implicitly add a const in the conversion. This | 141 // 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 | 142 // 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 | 143 // 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 | 144 // T[n]" does not seem to match correctly, so we are stuck with this |
116 // restriction. | 145 // restriction. |
117 template <typename T, size_t n> | 146 template <typename T, size_t n> |
118 struct CallbackParamTraits<T[n], false> { | 147 struct CallbackParamTraitsForNonMoveOnlyType<T[n]> { |
119 typedef const T* ForwardType; | 148 typedef const T* ForwardType; |
120 typedef const T* StorageType; | 149 typedef const T* StorageType; |
121 }; | 150 }; |
122 | 151 |
123 // See comment for CallbackParamTraits<T[n]>. | 152 // See comment for CallbackParamTraits<T[n]>. |
124 template <typename T> | 153 template <typename T> |
125 struct CallbackParamTraits<T[], false> { | 154 struct CallbackParamTraitsForNonMoveOnlyType<T[]> { |
126 typedef const T* ForwardType; | 155 typedef const T* ForwardType; |
127 typedef const T* StorageType; | 156 typedef const T* StorageType; |
128 }; | 157 }; |
129 | 158 |
130 // Parameter traits for movable-but-not-copyable scopers. | 159 // Parameter traits for movable-but-not-copyable scopers. |
131 // | 160 // |
132 // Callback<>/Bind() understands movable-but-not-copyable semantics where | 161 // Callback<>/Bind() understands movable-but-not-copyable semantics where |
133 // the type cannot be copied but can still have its state destructively | 162 // 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 | 163 // 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 | 164 // helper function. When used with Bind(), this signifies transferal of the |
136 // object's state to the target function. | 165 // object's state to the target function. |
137 // | 166 // |
138 // For these types, the ForwardType must not be a const reference, or a | 167 // For these types, the ForwardType must not be a const reference, or a |
139 // reference. A const reference is inappropriate, and would break const | 168 // reference. A const reference is inappropriate, and would break const |
140 // correctness, because we are implementing a destructive move. A non-const | 169 // correctness, because we are implementing a destructive move. A non-const |
141 // reference cannot be used with temporaries which means the result of a | 170 // 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(). | 171 // function or a cast would not be usable with Callback<> or Bind(). |
143 template <typename T> | 172 template <typename T> |
144 struct CallbackParamTraits<T, true> { | 173 struct CallbackParamTraitsForMoveOnlyType { |
145 typedef T ForwardType; | 174 typedef T ForwardType; |
146 typedef T StorageType; | 175 typedef T StorageType; |
147 }; | 176 }; |
148 | 177 |
149 // CallbackForward() is a very limited simulation of C++11's std::forward() | 178 // 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 | 179 // 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 | 180 // types. It is needed because forwarding a movable-but-not-copyable |
152 // argument to another function requires us to invoke the proper move | 181 // 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 | 182 // operator to create a rvalue version of the type. The supported types are |
154 // whitelisted below as overloads of the CallbackForward() function. The | 183 // whitelisted below as overloads of the CallbackForward() function. The |
(...skipping 14 matching lines...) Expand all Loading... |
169 | 198 |
170 template <typename T> | 199 template <typename T> |
171 typename enable_if<IsMoveOnlyType<T>::value, T>::type CallbackForward(T& t) { | 200 typename enable_if<IsMoveOnlyType<T>::value, T>::type CallbackForward(T& t) { |
172 return t.Pass(); | 201 return t.Pass(); |
173 } | 202 } |
174 | 203 |
175 } // namespace internal | 204 } // namespace internal |
176 } // namespace base | 205 } // namespace base |
177 | 206 |
178 #endif // BASE_CALLBACK_INTERNAL_H_ | 207 #endif // BASE_CALLBACK_INTERNAL_H_ |
OLD | NEW |