fischman@chromium.org | dc221a7 | 2012-03-25 05:37:27 +0900 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // This defines helpful methods for dealing with Callbacks. Because Callbacks |
| 6 | // are implemented using templates, with a class per callback signature, adding |
| 7 | // methods to Callback<> itself is unattractive (lots of extra code gets |
| 8 | // generated). Instead, consider adding methods here. |
| 9 | // |
| 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() |
| 12 | // reads/writes the variable holding the Callback. |
| 13 | |
| 14 | #ifndef BASE_CALLBACK_HELPERS_H_ |
| 15 | #define BASE_CALLBACK_HELPERS_H_ |
| 16 | |
| 17 | #include "base/callback.h" |
avi@chromium.org | b74bab8 | 2013-08-30 11:04:04 +0900 | [diff] [blame] | 18 | #include "base/compiler_specific.h" |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 19 | #include "base/macros.h" |
fischman@chromium.org | dc221a7 | 2012-03-25 05:37:27 +0900 | [diff] [blame] | 20 | |
| 21 | namespace base { |
| 22 | |
| 23 | template <typename Sig> |
| 24 | base::Callback<Sig> ResetAndReturn(base::Callback<Sig>* cb) { |
| 25 | base::Callback<Sig> ret(*cb); |
| 26 | cb->Reset(); |
| 27 | return ret; |
| 28 | } |
| 29 | |
sergeyu | f9cf281 | 2016-06-25 09:51:09 +0900 | [diff] [blame] | 30 | // ScopedClosureRunner is akin to std::unique_ptr<> for Closures. It ensures |
| 31 | // that the Closure is executed no matter how the current scope exits. |
avi@chromium.org | b74bab8 | 2013-08-30 11:04:04 +0900 | [diff] [blame] | 32 | class BASE_EXPORT ScopedClosureRunner { |
| 33 | public: |
| 34 | ScopedClosureRunner(); |
| 35 | explicit ScopedClosureRunner(const Closure& closure); |
| 36 | ~ScopedClosureRunner(); |
| 37 | |
sergeyu | f9cf281 | 2016-06-25 09:51:09 +0900 | [diff] [blame] | 38 | ScopedClosureRunner(ScopedClosureRunner&& other); |
| 39 | |
sergeyu | 93396d1 | 2016-07-08 09:34:27 +0900 | [diff] [blame] | 40 | // Releases the current closure if it's set and replaces it with the closure |
| 41 | // from |other|. |
sergeyu | f9cf281 | 2016-06-25 09:51:09 +0900 | [diff] [blame] | 42 | ScopedClosureRunner& operator=(ScopedClosureRunner&& other); |
| 43 | |
| 44 | // Calls the current closure and resets it, so it wont be called again. |
sergeyu | 93396d1 | 2016-07-08 09:34:27 +0900 | [diff] [blame] | 45 | void RunAndReset(); |
sergeyu | f9cf281 | 2016-06-25 09:51:09 +0900 | [diff] [blame] | 46 | |
sergeyu | 93396d1 | 2016-07-08 09:34:27 +0900 | [diff] [blame] | 47 | // Replaces closure with the new one releasing the old one without calling it. |
| 48 | void ReplaceClosure(const Closure& closure); |
sergeyu | f9cf281 | 2016-06-25 09:51:09 +0900 | [diff] [blame] | 49 | |
| 50 | // Releases the Closure without calling. |
avi@chromium.org | b74bab8 | 2013-08-30 11:04:04 +0900 | [diff] [blame] | 51 | Closure Release() WARN_UNUSED_RESULT; |
| 52 | |
| 53 | private: |
| 54 | Closure closure_; |
| 55 | |
| 56 | DISALLOW_COPY_AND_ASSIGN(ScopedClosureRunner); |
| 57 | }; |
| 58 | |
fischman@chromium.org | dc221a7 | 2012-03-25 05:37:27 +0900 | [diff] [blame] | 59 | } // namespace base |
| 60 | |
| 61 | #endif // BASE_CALLBACK_HELPERS_H_ |