summaryrefslogtreecommitdiffstats
path: root/base/callback_internal.h
diff options
context:
space:
mode:
authorsail@chromium.org <sail@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-12-14 21:48:54 +0000
committersail@chromium.org <sail@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-12-14 21:48:54 +0000
commit115288648041898b6d50f7d2e17391e2d1972a76 (patch)
treeeee67d927a7ebb40e87848924a8f26c5c5cb99ca /base/callback_internal.h
parente91ac22d99252095839ca7c82c092972bd56445e (diff)
downloadchromium_src-115288648041898b6d50f7d2e17391e2d1972a76.zip
chromium_src-115288648041898b6d50f7d2e17391e2d1972a76.tar.gz
chromium_src-115288648041898b6d50f7d2e17391e2d1972a76.tar.bz2
Revert 114494 - Remove BindStateHolder and have Bind() return a Callback<> object directly.
This removes some complexity and also fixes a bug where if you call Bind() with the result of Bind(), the resulting Callback would only be valid during the first call. Ouch. BUG=none TEST=new unittests Review URL: http://codereview.chromium.org/8738001 TBR=ajwong@chromium.org Review URL: http://codereview.chromium.org/8914022 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@114495 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/callback_internal.h')
-rw-r--r--base/callback_internal.h30
1 files changed, 25 insertions, 5 deletions
diff --git a/base/callback_internal.h b/base/callback_internal.h
index 4bb8aa9..81c87c0 100644
--- a/base/callback_internal.h
+++ b/base/callback_internal.h
@@ -29,6 +29,29 @@ class BindStateBase : public RefCountedThreadSafe<BindStateBase> {
virtual ~BindStateBase() {}
};
+// This structure exists purely to pass the returned |bind_state_| from
+// Bind() to Callback while avoiding an extra AddRef/Release() pair.
+//
+// To do this, the constructor of Callback<> must take a const-ref. The
+// reference must be to a const object otherwise the compiler will emit a
+// warning about taking a reference to a temporary.
+//
+// Unfortunately, this means that the internal |bind_state_| field must
+// be made mutable.
+template <typename T>
+struct BindStateHolder {
+ explicit BindStateHolder(T* bind_state)
+ : bind_state_(bind_state) {
+ }
+
+ mutable scoped_refptr<BindStateBase> bind_state_;
+};
+
+template <typename T>
+BindStateHolder<T> MakeBindStateHolder(T* o) {
+ return BindStateHolder<T>(o);
+}
+
// Holds the Callback methods that don't require specialization to reduce
// template bloat.
class BASE_EXPORT CallbackBase {
@@ -49,11 +72,8 @@ class BASE_EXPORT CallbackBase {
// Returns true if this callback equals |other|. |other| may be null.
bool Equals(const CallbackBase& other) const;
- // Allow initializing of |bind_state_| via the constructor to avoid default
- // initialization of the scoped_refptr. We do not also initialize
- // |polymorphic_invoke_| here because doing a normal assignment in the
- // derived Callback templates makes for much nicer compiler errors.
- explicit CallbackBase(BindStateBase* bind_state);
+ CallbackBase(InvokeFuncStorage polymorphic_invoke,
+ scoped_refptr<BindStateBase>* bind_state);
// Force the destructor to be instantiated inside this translation unit so
// that our subclasses will not get inlined versions. Avoids more template