OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 defines a set of argument wrappers and related factory methods that | 5 // This defines a set of argument wrappers and related factory methods that |
6 // can be used specify the refcounting and reference semantics of arguments | 6 // can be used specify the refcounting and reference semantics of arguments |
7 // that are bound by the Bind() function in base/bind.h. | 7 // that are bound by the Bind() function in base/bind.h. |
8 // | 8 // |
9 // It also defines a set of simple functions and utilities that people want | 9 // It also defines a set of simple functions and utilities that people want |
10 // when using Callback<> and Bind(). | 10 // when using Callback<> and Bind(). |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 // // Assign to a Callback with a void return type. | 114 // // Assign to a Callback with a void return type. |
115 // Callback<void(int)> cb = Bind(IgnoreResult(&DoSomething)); | 115 // Callback<void(int)> cb = Bind(IgnoreResult(&DoSomething)); |
116 // cb->Run(1); // Prints "1". | 116 // cb->Run(1); // Prints "1". |
117 // | 117 // |
118 // // Prints "1" on |ml|. | 118 // // Prints "1" on |ml|. |
119 // ml->PostTask(FROM_HERE, Bind(IgnoreResult(&DoSomething), 1); | 119 // ml->PostTask(FROM_HERE, Bind(IgnoreResult(&DoSomething), 1); |
120 // | 120 // |
121 // | 121 // |
122 // EXAMPLE OF Passed(): | 122 // EXAMPLE OF Passed(): |
123 // | 123 // |
124 // void TakesOwnership(scoped_ptr<Foo> arg) { } | 124 // void TakesOwnership(std::unique_ptr<Foo> arg) { } |
125 // scoped_ptr<Foo> CreateFoo() { return scoped_ptr<Foo>(new Foo()); } | 125 // std::unique_ptr<Foo> CreateFoo() { return std::unique_ptr<Foo>(new Foo()); |
| 126 // } |
126 // | 127 // |
127 // scoped_ptr<Foo> f(new Foo()); | 128 // std::unique_ptr<Foo> f(new Foo()); |
128 // | 129 // |
129 // // |cb| is given ownership of Foo(). |f| is now NULL. | 130 // // |cb| is given ownership of Foo(). |f| is now NULL. |
130 // // You can use std::move(f) in place of &f, but it's more verbose. | 131 // // You can use std::move(f) in place of &f, but it's more verbose. |
131 // Closure cb = Bind(&TakesOwnership, Passed(&f)); | 132 // Closure cb = Bind(&TakesOwnership, Passed(&f)); |
132 // | 133 // |
133 // // Run was never called so |cb| still owns Foo() and deletes | 134 // // Run was never called so |cb| still owns Foo() and deletes |
134 // // it on Reset(). | 135 // // it on Reset(). |
135 // cb.Reset(); | 136 // cb.Reset(); |
136 // | 137 // |
137 // // |cb| is given a new Foo created by CreateFoo(). | 138 // // |cb| is given a new Foo created by CreateFoo(). |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 | 347 |
347 template <typename T> | 348 template <typename T> |
348 struct IgnoreResultHelper<Callback<T> > { | 349 struct IgnoreResultHelper<Callback<T> > { |
349 explicit IgnoreResultHelper(const Callback<T>& functor) : functor_(functor) {} | 350 explicit IgnoreResultHelper(const Callback<T>& functor) : functor_(functor) {} |
350 | 351 |
351 const Callback<T>& functor_; | 352 const Callback<T>& functor_; |
352 }; | 353 }; |
353 | 354 |
354 // An alternate implementation is to avoid the destructive copy, and instead | 355 // An alternate implementation is to avoid the destructive copy, and instead |
355 // specialize ParamTraits<> for OwnedWrapper<> to change the StorageType to | 356 // specialize ParamTraits<> for OwnedWrapper<> to change the StorageType to |
356 // a class that is essentially a scoped_ptr<>. | 357 // a class that is essentially a std::unique_ptr<>. |
357 // | 358 // |
358 // The current implementation has the benefit though of leaving ParamTraits<> | 359 // The current implementation has the benefit though of leaving ParamTraits<> |
359 // fully in callback_internal.h as well as avoiding type conversions during | 360 // fully in callback_internal.h as well as avoiding type conversions during |
360 // storage. | 361 // storage. |
361 template <typename T> | 362 template <typename T> |
362 class OwnedWrapper { | 363 class OwnedWrapper { |
363 public: | 364 public: |
364 explicit OwnedWrapper(T* o) : ptr_(o) {} | 365 explicit OwnedWrapper(T* o) : ptr_(o) {} |
365 ~OwnedWrapper() { delete ptr_; } | 366 ~OwnedWrapper() { delete ptr_; } |
366 T* get() const { return ptr_; } | 367 T* get() const { return ptr_; } |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
622 BASE_EXPORT void DoNothing(); | 623 BASE_EXPORT void DoNothing(); |
623 | 624 |
624 template<typename T> | 625 template<typename T> |
625 void DeletePointer(T* obj) { | 626 void DeletePointer(T* obj) { |
626 delete obj; | 627 delete obj; |
627 } | 628 } |
628 | 629 |
629 } // namespace base | 630 } // namespace base |
630 | 631 |
631 #endif // BASE_BIND_HELPERS_H_ | 632 #endif // BASE_BIND_HELPERS_H_ |
OLD | NEW |