summaryrefslogtreecommitdiffstats
path: root/base/callback_helpers.h
diff options
context:
space:
mode:
authorfischman@chromium.org <fischman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-24 20:37:27 +0000
committerfischman@chromium.org <fischman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-24 20:37:27 +0000
commit1192339cc77909d203a40ce01919f55fec39413f (patch)
treed89480c7fea632c896a70e41542785511830407b /base/callback_helpers.h
parent6e04d17161b8d53770979fb5a451da8174e255c6 (diff)
downloadchromium_src-1192339cc77909d203a40ce01919f55fec39413f.zip
chromium_src-1192339cc77909d203a40ce01919f55fec39413f.tar.gz
chromium_src-1192339cc77909d203a40ce01919f55fec39413f.tar.bz2
Make Callback::Reset() return a copy to support use-cases where Run() ends up modifying |*this|. Callers can use
cb.Reset().Run(args...); to avoid reentrancy-like bugs. Replace the special-purpose versions of ResetAndRunCB in the media/ codebase with this more-general facility. Review URL: http://codereview.chromium.org/9717021 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@128772 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/callback_helpers.h')
-rw-r--r--base/callback_helpers.h30
1 files changed, 30 insertions, 0 deletions
diff --git a/base/callback_helpers.h b/base/callback_helpers.h
new file mode 100644
index 0000000..52cb71b
--- /dev/null
+++ b/base/callback_helpers.h
@@ -0,0 +1,30 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// This defines helpful methods for dealing with Callbacks. Because Callbacks
+// are implemented using templates, with a class per callback signature, adding
+// methods to Callback<> itself is unattractive (lots of extra code gets
+// generated). Instead, consider adding methods here.
+//
+// ResetAndReturn(&cb) is like cb.Reset() but allows executing a callback (via a
+// copy) after the original callback is Reset(). This can be handy if Run()
+// reads/writes the variable holding the Callback.
+
+#ifndef BASE_CALLBACK_HELPERS_H_
+#define BASE_CALLBACK_HELPERS_H_
+
+#include "base/callback.h"
+
+namespace base {
+
+template <typename Sig>
+base::Callback<Sig> ResetAndReturn(base::Callback<Sig>* cb) {
+ base::Callback<Sig> ret(*cb);
+ cb->Reset();
+ return ret;
+}
+
+} // namespace base
+
+#endif // BASE_CALLBACK_HELPERS_H_