diff options
author | ckehoe <ckehoe@chromium.org> | 2014-11-14 22:00:52 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-11-15 06:01:08 +0000 |
commit | 79411ab5fef47880a9a31f463c09b348aa74df00 (patch) | |
tree | 929c0035399fd22de0e152832c8be6c35585a6ee /base/cancelable_callback.h | |
parent | c35f68e712ff1bb4563268c3c53e3719b2251ce9 (diff) | |
download | chromium_src-79411ab5fef47880a9a31f463c09b348aa74df00.zip chromium_src-79411ab5fef47880a9a31f463c09b348aa74df00.tar.gz chromium_src-79411ab5fef47880a9a31f463c09b348aa74df00.tar.bz2 |
Fixing CancelableCallback to use variadic templates. Previously, 2-argument callbacks wouldn't compile.
Review URL: https://codereview.chromium.org/694193003
Cr-Commit-Position: refs/heads/master@{#304360}
Diffstat (limited to 'base/cancelable_callback.h')
-rw-r--r-- | base/cancelable_callback.h | 163 |
1 files changed, 13 insertions, 150 deletions
diff --git a/base/cancelable_callback.h b/base/cancelable_callback.h index 159100f..91eb046 100644 --- a/base/cancelable_callback.h +++ b/base/cancelable_callback.h @@ -55,13 +55,13 @@ namespace base { template <typename Sig> class CancelableCallback; -template <> -class CancelableCallback<void(void)> { +template <typename... A> +class CancelableCallback<void(A...)> { public: CancelableCallback() : weak_factory_(this) {} // |callback| must not be null. - explicit CancelableCallback(const base::Callback<void(void)>& callback) + explicit CancelableCallback(const base::Callback<void(A...)>& callback) : weak_factory_(this), callback_(callback) { DCHECK(!callback.is_null()); @@ -84,7 +84,7 @@ class CancelableCallback<void(void)> { // Sets |callback| as the closure that may be cancelled. |callback| may not // be null. Outstanding and any previously wrapped callbacks are cancelled. - void Reset(const base::Callback<void(void)>& callback) { + void Reset(const base::Callback<void(A...)>& callback) { DCHECK(!callback.is_null()); // Outstanding tasks (e.g., posted to a message loop) must not be called. @@ -97,173 +97,36 @@ class CancelableCallback<void(void)> { } // Returns a callback that can be disabled by calling Cancel(). - const base::Callback<void(void)>& callback() const { + const base::Callback<void(A...)>& callback() const { return forwarder_; } private: - void Forward() { - callback_.Run(); + void Forward(A... args) const { + callback_.Run(args...); } // Helper method to bind |forwarder_| using a weak pointer from // |weak_factory_|. void InitializeForwarder() { - forwarder_ = base::Bind(&CancelableCallback<void(void)>::Forward, + forwarder_ = base::Bind(&CancelableCallback<void(A...)>::Forward, weak_factory_.GetWeakPtr()); } // Used to ensure Forward() is not run when this object is destroyed. - base::WeakPtrFactory<CancelableCallback<void(void)> > weak_factory_; + // TODO(ckehoe): This should be the last class member. + // Move it there when crbug.com/433583 is fixed. + base::WeakPtrFactory<CancelableCallback<void(A...)> > weak_factory_; // The wrapper closure. - base::Callback<void(void)> forwarder_; + base::Callback<void(A...)> forwarder_; // The stored closure that may be cancelled. - base::Callback<void(void)> callback_; + base::Callback<void(A...)> callback_; DISALLOW_COPY_AND_ASSIGN(CancelableCallback); }; -template <typename A1> -class CancelableCallback<void(A1)> { - public: - CancelableCallback() : weak_factory_(this) {} - - // |callback| must not be null. - explicit CancelableCallback(const base::Callback<void(A1)>& callback) - : weak_factory_(this), - callback_(callback) { - DCHECK(!callback.is_null()); - InitializeForwarder(); - } - - ~CancelableCallback() {} - - // Cancels and drops the reference to the wrapped callback. - void Cancel() { - weak_factory_.InvalidateWeakPtrs(); - forwarder_.Reset(); - callback_.Reset(); - } - - // Returns true if the wrapped callback has been cancelled. - bool IsCancelled() const { - return callback_.is_null(); - } - - // Sets |callback| as the closure that may be cancelled. |callback| may not - // be null. Outstanding and any previously wrapped callbacks are cancelled. - void Reset(const base::Callback<void(A1)>& callback) { - DCHECK(!callback.is_null()); - - // Outstanding tasks (e.g., posted to a message loop) must not be called. - Cancel(); - - // |forwarder_| is no longer valid after Cancel(), so re-bind. - InitializeForwarder(); - - callback_ = callback; - } - - // Returns a callback that can be disabled by calling Cancel(). - const base::Callback<void(A1)>& callback() const { - return forwarder_; - } - - private: - void Forward(A1 a1) const { - callback_.Run(a1); - } - - // Helper method to bind |forwarder_| using a weak pointer from - // |weak_factory_|. - void InitializeForwarder() { - forwarder_ = base::Bind(&CancelableCallback<void(A1)>::Forward, - weak_factory_.GetWeakPtr()); - } - - // Used to ensure Forward() is not run when this object is destroyed. - base::WeakPtrFactory<CancelableCallback<void(A1)> > weak_factory_; - - // The wrapper closure. - base::Callback<void(A1)> forwarder_; - - // The stored closure that may be cancelled. - base::Callback<void(A1)> callback_; - - DISALLOW_COPY_AND_ASSIGN(CancelableCallback); -}; - -template <typename A1, typename A2> -class CancelableCallback<void(A1, A2)> { - public: - CancelableCallback() : weak_factory_(this) {} - - // |callback| must not be null. - explicit CancelableCallback(const base::Callback<void(A1, A2)>& callback) - : weak_factory_(this), - callback_(callback) { - DCHECK(!callback.is_null()); - InitializeForwarder(); - } - - ~CancelableCallback() {} - - // Cancels and drops the reference to the wrapped callback. - void Cancel() { - weak_factory_.InvalidateWeakPtrs(); - forwarder_.Reset(); - callback_.Reset(); - } - - // Returns true if the wrapped callback has been cancelled. - bool IsCancelled() const { - return callback_.is_null(); - } - - // Sets |callback| as the closure that may be cancelled. |callback| may not - // be null. Outstanding and any previously wrapped callbacks are cancelled. - void Reset(const base::Callback<void(A1, A2)>& callback) { - DCHECK(!callback.is_null()); - - // Outstanding tasks (e.g., posted to a message loop) must not be called. - Cancel(); - - // |forwarder_| is no longer valid after Cancel(), so re-bind. - InitializeForwarder(); - - callback_ = callback; - } - - // Returns a callback that can be disabled by calling Cancel(). - const base::Callback<void(A1, A2)>& callback() const { - return forwarder_; - } - - private: - void Forward(A1 a1, A2 a2) const { - callback_.Run(a1, a2); - } - - // Helper method to bind |forwarder_| using a weak pointer from - // |weak_factory_|. - void InitializeForwarder() { - forwarder_ = base::Bind(&CancelableCallback<void(A1, A2)>::Forward, - weak_factory_.GetWeakPtr()); - } - - // The wrapper closure. - base::Callback<void(A1, A2)> forwarder_; - - // The stored closure that may be cancelled. - base::Callback<void(A1, A2)> callback_; - - // Used to ensure Forward() is not run when this object is destroyed. - base::WeakPtrFactory<CancelableCallback<void(A1, A2)> > weak_factory_; - DISALLOW_COPY_AND_ASSIGN(CancelableCallback); -}; - typedef CancelableCallback<void(void)> CancelableClosure; } // namespace base |