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 defines helpful methods for dealing with Callbacks. Because Callbacks | 5 // This defines helpful methods for dealing with Callbacks. Because Callbacks |
6 // are implemented using templates, with a class per callback signature, adding | 6 // are implemented using templates, with a class per callback signature, adding |
7 // methods to Callback<> itself is unattractive (lots of extra code gets | 7 // methods to Callback<> itself is unattractive (lots of extra code gets |
8 // generated). Instead, consider adding methods here. | 8 // generated). Instead, consider adding methods here. |
9 // | 9 // |
10 // ResetAndReturn(&cb) is like cb.Reset() but allows executing a callback (via a | 10 // ResetAndReturn(&cb) is like cb.Reset() but allows executing a callback (via a |
11 // copy) after the original callback is Reset(). This can be handy if Run() | 11 // copy) after the original callback is Reset(). This can be handy if Run() |
12 // reads/writes the variable holding the Callback. | 12 // reads/writes the variable holding the Callback. |
13 | 13 |
14 #ifndef BASE_CALLBACK_HELPERS_H_ | 14 #ifndef BASE_CALLBACK_HELPERS_H_ |
15 #define BASE_CALLBACK_HELPERS_H_ | 15 #define BASE_CALLBACK_HELPERS_H_ |
16 | 16 |
17 #include "base/callback.h" | 17 #include "base/callback.h" |
18 #include "base/compiler_specific.h" | 18 #include "base/compiler_specific.h" |
19 #include "base/macros.h" | 19 #include "base/macros.h" |
20 | 20 |
21 namespace base { | 21 namespace base { |
22 | 22 |
23 template <typename Sig> | 23 template <typename Sig> |
24 base::Callback<Sig> ResetAndReturn(base::Callback<Sig>* cb) { | 24 base::Callback<Sig> ResetAndReturn(base::Callback<Sig>* cb) { |
25 base::Callback<Sig> ret(*cb); | 25 base::Callback<Sig> ret(*cb); |
26 cb->Reset(); | 26 cb->Reset(); |
27 return ret; | 27 return ret; |
28 } | 28 } |
29 | 29 |
30 // ScopedClosureRunner is akin to scoped_ptr for Closures. It ensures that the | 30 // ScopedClosureRunner is akin to std::unique_ptr<> for Closures. It ensures |
31 // Closure is executed and deleted no matter how the current scope exits. | 31 // that theClosure is executed no matter how the current scope exits. |
danakj
2016/06/23 20:19:28
the Closure
Sergey Ulanov
2016/06/23 23:30:32
Done.
| |
32 class BASE_EXPORT ScopedClosureRunner { | 32 class BASE_EXPORT ScopedClosureRunner { |
33 public: | 33 public: |
34 ScopedClosureRunner(); | 34 ScopedClosureRunner(); |
35 ScopedClosureRunner(ScopedClosureRunner&& other); | |
danakj
2016/06/23 20:19:28
Any reason you're not defining operator= along wit
Sergey Ulanov
2016/06/23 23:30:32
One small concern I have with operator= is that wh
danakj
2016/06/24 20:06:30
I think it makes sense that it does what reset doe
Sergey Ulanov
2016/06/24 23:44:01
Done.
BTW, As far as I can tell there is only one
| |
35 explicit ScopedClosureRunner(const Closure& closure); | 36 explicit ScopedClosureRunner(const Closure& closure); |
36 ~ScopedClosureRunner(); | 37 ~ScopedClosureRunner(); |
37 | 38 |
38 void Reset(); | 39 void Reset(); |
39 void Reset(const Closure& closure); | 40 void Reset(const Closure& closure); |
40 Closure Release() WARN_UNUSED_RESULT; | 41 Closure Release() WARN_UNUSED_RESULT; |
41 | 42 |
42 private: | 43 private: |
43 Closure closure_; | 44 Closure closure_; |
44 | 45 |
45 DISALLOW_COPY_AND_ASSIGN(ScopedClosureRunner); | 46 DISALLOW_COPY_AND_ASSIGN(ScopedClosureRunner); |
46 }; | 47 }; |
47 | 48 |
48 } // namespace base | 49 } // namespace base |
49 | 50 |
50 #endif // BASE_CALLBACK_HELPERS_H_ | 51 #endif // BASE_CALLBACK_HELPERS_H_ |
OLD | NEW |