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