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 Signature, | 23 template <typename Signature, |
24 internal::CopyMode copy_mode, | 24 internal::CopyMode copy_mode, |
25 internal::RepeatMode repeat_mode> | 25 internal::RepeatMode repeat_mode> |
26 base::Callback<Signature, copy_mode, repeat_mode> ResetAndReturn( | 26 base::Callback<Signature, copy_mode, repeat_mode> ResetAndReturn( |
27 base::Callback<Signature, copy_mode, repeat_mode>* cb) { | 27 base::Callback<Signature, copy_mode, repeat_mode>* cb) { |
28 base::Callback<Signature, copy_mode, repeat_mode> ret(*cb); | 28 base::Callback<Signature, copy_mode, repeat_mode> ret(std::move(*cb)); |
29 cb->Reset(); | 29 DCHECK(!*cb); |
30 return ret; | 30 return ret; |
31 } | 31 } |
32 | 32 |
33 // ScopedClosureRunner is akin to std::unique_ptr<> for Closures. It ensures | 33 // ScopedClosureRunner is akin to std::unique_ptr<> for Closures. It ensures |
34 // that the Closure is executed no matter how the current scope exits. | 34 // that the Closure is executed no matter how the current scope exits. |
35 class BASE_EXPORT ScopedClosureRunner { | 35 class BASE_EXPORT ScopedClosureRunner { |
36 public: | 36 public: |
37 ScopedClosureRunner(); | 37 ScopedClosureRunner(); |
38 explicit ScopedClosureRunner(const Closure& closure); | 38 explicit ScopedClosureRunner(const Closure& closure); |
39 ~ScopedClosureRunner(); | 39 ~ScopedClosureRunner(); |
(...skipping 15 matching lines...) Expand all Loading... |
55 | 55 |
56 private: | 56 private: |
57 Closure closure_; | 57 Closure closure_; |
58 | 58 |
59 DISALLOW_COPY_AND_ASSIGN(ScopedClosureRunner); | 59 DISALLOW_COPY_AND_ASSIGN(ScopedClosureRunner); |
60 }; | 60 }; |
61 | 61 |
62 } // namespace base | 62 } // namespace base |
63 | 63 |
64 #endif // BASE_CALLBACK_HELPERS_H_ | 64 #endif // BASE_CALLBACK_HELPERS_H_ |
OLD | NEW |