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

Side by Side Diff: base/callback.h.pump

Issue 6507029: Callback API Change: is_null, Reset, and Equals (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: grammar Created 9 years, 10 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 | Annotate | Revision Log
OLDNEW
1 $$ This is a pump file for generating file templates. Pump is a python 1 $$ This is a pump file for generating file templates. Pump is a python
2 $$ script that is part of the Google Test suite of utilities. Description 2 $$ script that is part of the Google Test suite of utilities. Description
3 $$ can be found here: 3 $$ can be found here:
4 $$ 4 $$
5 $$ http://code.google.com/p/googletest/wiki/PumpManual 5 $$ http://code.google.com/p/googletest/wiki/PumpManual
6 $$ 6 $$
7 7
8 $var MAX_ARITY = 6 8 $var MAX_ARITY = 6
9 9
10 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 10 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 // - Invoking the return of Bind. Bind(&foo).Run() does not work; 209 // - Invoking the return of Bind. Bind(&foo).Run() does not work;
210 // - Binding arrays to functions that take a non-const pointer. 210 // - Binding arrays to functions that take a non-const pointer.
211 // Example: 211 // Example:
212 // void Foo(const char* ptr); 212 // void Foo(const char* ptr);
213 // void Bar(char* ptr); 213 // void Bar(char* ptr);
214 // Bind(&Foo, "test"); 214 // Bind(&Foo, "test");
215 // Bind(&Bar, "test"); // This fails because ptr is not const. 215 // Bind(&Bar, "test"); // This fails because ptr is not const.
216 216
217 namespace base { 217 namespace base {
218 218
219 namespace internal {
220
221 // Holds the methods that don't require specialization to reduce template bloat.
222 class CallbackBase {
223 protected:
224 // In C++, it is safe to cast function pointers to function pointers of
225 // another type. It is not okay to use void*. We create a InvokeFuncStorage
226 // that that can store our function pointer, and then cast it back to
227 // the original type on usage.
228 typedef void(*InvokeFuncStorage)(void);
229
230 public:
231 explicit CallbackBase(InvokeFuncStorage polymorphic_invoke)
232 : polymorphic_invoke_(polymorphic_invoke) {
233 }
234
235 // Returns true if Callback is empty (doesn't refer to anything).
236 bool is_empty() const {
237 return invoker_storage_.get() == NULL;
238 }
239
240 // Returns the Callback into an empty state.
241 void reset() {
242 invoker_storage_ = NULL;
243 polymorphic_invoke_ = NULL;
244 }
245
246 bool Equals(const CallbackBase& other) const {
247 return invoker_storage_.get() == other.invoker_storage_.get() &&
248 polymorphic_invoke_ == other.polymorphic_invoke_;
249 }
250
251 protected:
252 scoped_refptr<InvokerStorageBase> invoker_storage_;
253 InvokeFuncStorage polymorphic_invoke_;
254 };
255
256 } // namespace internal
257
258
219 // First, we forward declare the Callback class template. This informs the 259 // First, we forward declare the Callback class template. This informs the
220 // compiler that the template only has 1 type parameter which is the function 260 // compiler that the template only has 1 type parameter which is the function
221 // signature that the Callback is representing. 261 // signature that the Callback is representing.
222 // 262 //
223 // After this, create template specializations for 0-$(MAX_ARITY) parameters. No te that 263 // After this, create template specializations for 0-$(MAX_ARITY) parameters. No te that
224 // even though the template typelist grows, the specialization still 264 // even though the template typelist grows, the specialization still
225 // only has one type: the function signature. 265 // only has one type: the function signature.
226 template <typename Sig> 266 template <typename Sig>
227 class Callback; 267 class Callback;
228 268
229
230 $range ARITY 0..MAX_ARITY 269 $range ARITY 0..MAX_ARITY
231 $for ARITY [[ 270 $for ARITY [[
232 $range ARG 1..ARITY 271 $range ARG 1..ARITY
233 272
234 $if ARITY == 0 [[ 273 $if ARITY == 0 [[
235 template <typename R> 274 template <typename R>
236 class Callback<R(void)> { 275 class Callback<R(void)> : public internal::CallbackBase {
237 ]] $else [[ 276 ]] $else [[
238 template <typename R, $for ARG , [[typename A$(ARG)]]> 277 template <typename R, $for ARG , [[typename A$(ARG)]]>
239 class Callback<R($for ARG , [[A$(ARG)]])> { 278 class Callback<R($for ARG , [[A$(ARG)]])> : public internal::CallbackBase {
240 ]] 279 ]]
241 280
242 public: 281 public:
243 typedef R(*PolymorphicInvoke)(internal::InvokerStorageBase*[[]] 282 typedef R(*PolymorphicInvoke)(internal::InvokerStorageBase*[[]]
244 $if ARITY != 0 [[, ]] 283 $if ARITY != 0 [[, ]]
245 $for ARG , 284 $for ARG ,
246 [[const A$(ARG)&]]); 285 [[const A$(ARG)&]]);
247 286
248 Callback() : polymorphic_invoke_(NULL) { } 287 Callback() : CallbackBase(NULL) { }
249 288
250 // We pass InvokerStorageHolder by const ref to avoid incurring an 289 // We pass InvokerStorageHolder by const ref to avoid incurring an
251 // unnecessary AddRef/Unref pair even though we will modify the object. 290 // unnecessary AddRef/Unref pair even though we will modify the object.
252 // We cannot use a normal reference because the compiler will warn 291 // We cannot use a normal reference because the compiler will warn
253 // since this is often used on a return value, which is a temporary. 292 // since this is often used on a return value, which is a temporary.
254 // 293 //
255 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT 294 // Note that this constructor CANNOT be explicit, and that Bind() CANNOT
256 // return the exact Callback<> type. See base/bind.h for details. 295 // return the exact Callback<> type. See base/bind.h for details.
257 template <typename T> 296 template <typename T>
258 Callback(const internal::InvokerStorageHolder<T>& invoker_holder) 297 Callback(const internal::InvokerStorageHolder<T>& invoker_holder)
259 : polymorphic_invoke_(&T::FunctionTraits::DoInvoke) { 298 : CallbackBase(
299 reinterpret_cast<InvokeFuncStorage>(&T::FunctionTraits::DoInvoke)) {
260 invoker_storage_.swap(invoker_holder.invoker_storage_); 300 invoker_storage_.swap(invoker_holder.invoker_storage_);
261 } 301 }
262 302
263 303
264 $if ARITY == 0 [[
265 R Run(void) const {
266 ]] $else [[
267 R Run($for ARG , 304 R Run($for ARG ,
268 [[const A$(ARG)& a$(ARG)]]) const { 305 [[const A$(ARG)& a$(ARG)]]) const {
269 ]] 306 PolymorphicInvoke f =
307 reinterpret_cast<PolymorphicInvoke>(polymorphic_invoke_);
270 308
271 return polymorphic_invoke_(invoker_storage_.get()[[]] 309 return f(invoker_storage_.get()[[]]
272 $if ARITY != 0 [[, ]] 310 $if ARITY != 0 [[, ]]
273 $for ARG , 311 $for ARG ,
274 [[a$(ARG)]]); 312 [[a$(ARG)]]);
275 } 313 }
276
277 private:
278 scoped_refptr<internal::InvokerStorageBase> invoker_storage_;
279 PolymorphicInvoke polymorphic_invoke_;
280 }; 314 };
281 315
282 316
283 ]] $$ for ARITY 317 ]] $$ for ARITY
284 318
285 // Syntactic sugar to make Callbacks<void(void)> easier to declare since it 319 // Syntactic sugar to make Callbacks<void(void)> easier to declare since it
286 // will be used in a lot of APIs with delayed execution. 320 // will be used in a lot of APIs with delayed execution.
287 typedef Callback<void(void)> Closure; 321 typedef Callback<void(void)> Closure;
288 322
289 } // namespace base 323 } // namespace base
290 324
291 #endif // BASE_CALLBACK_H 325 #endif // BASE_CALLBACK_H
OLDNEW
« base/callback.h ('K') | « base/callback.h ('k') | base/callback_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698