summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-07 00:25:29 +0000
committerajwong@chromium.org <ajwong@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-07 00:25:29 +0000
commit7a15d117f34715773a07e821d6d259cc6e726ef9 (patch)
tree04a63437171eacb6c1b09669d205e8c1a60ee128 /base
parente210c08d24f9ab65cac1f97127af1fbf6d8574f8 (diff)
downloadchromium_src-7a15d117f34715773a07e821d6d259cc6e726ef9.zip
chromium_src-7a15d117f34715773a07e821d6d259cc6e726ef9.tar.gz
chromium_src-7a15d117f34715773a07e821d6d259cc6e726ef9.tar.bz2
Support scoped_refptr<> as the object for a method call in base::Bind(). Reduce useless copies.
Patch up an API hole where you can't do base::Bind(&Foo::method, ptr), if ptr is a scoped_refptr. Also modify Unwrap() to remove a couple of unecessary copies for scoped_refptrs, and WeakPtrs<>. BUG=none TEST=none Review URL: http://codereview.chromium.org/8171013 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@104399 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/bind_helpers.h13
-rw-r--r--base/bind_unittest.cc10
2 files changed, 20 insertions, 3 deletions
diff --git a/base/bind_helpers.h b/base/bind_helpers.h
index 7eb5716..07f11f9 100644
--- a/base/bind_helpers.h
+++ b/base/bind_helpers.h
@@ -227,6 +227,11 @@ const T& Unwrap(ConstRefWrapper<T> const_ref) {
return const_ref.get();
}
+template <typename T>
+T* Unwrap(const scoped_refptr<T>& o) { return o.get(); }
+
+template <typename T>
+const WeakPtr<T>& Unwrap(const WeakPtr<T>& o) { return o; }
// Utility for handling different refcounting semantics in the Bind()
// function.
@@ -257,6 +262,14 @@ struct MaybeRefcount<base::true_type, T*> {
static void Release(T* o) { o->Release(); }
};
+// No need to additionally AddRef() and Release() since we are storing a
+// scoped_refptr<> inside the storage object already.
+template <typename T>
+struct MaybeRefcount<base::true_type, scoped_refptr<T> > {
+ static void AddRef(const scoped_refptr<T>& o) {}
+ static void Release(const scoped_refptr<T>& o) {}
+};
+
template <typename T>
struct MaybeRefcount<base::true_type, const T*> {
static void AddRef(const T* o) { o->AddRef(); }
diff --git a/base/bind_unittest.cc b/base/bind_unittest.cc
index 648096d..acaf562 100644
--- a/base/bind_unittest.cc
+++ b/base/bind_unittest.cc
@@ -284,6 +284,7 @@ TEST_F(BindTest, CallbackBindMore) {
// - Normal function.
// - Normal function bound with non-refcounted first argument.
// - Method bound to non-const object.
+// - Method bound to scoped_refptr.
// - Const method bound to non-const object.
// - Const method bound to const object.
// - Derived classes can be used with pointers to non-virtual base functions.
@@ -291,9 +292,9 @@ TEST_F(BindTest, CallbackBindMore) {
// preserve virtual dispatch).
TEST_F(BindTest, FunctionTypeSupport) {
EXPECT_CALL(static_func_mock_, VoidMethod0());
- EXPECT_CALL(has_ref_, AddRef()).Times(3);
- EXPECT_CALL(has_ref_, Release()).Times(3);
- EXPECT_CALL(has_ref_, VoidMethod0());
+ EXPECT_CALL(has_ref_, AddRef()).Times(5);
+ EXPECT_CALL(has_ref_, Release()).Times(5);
+ EXPECT_CALL(has_ref_, VoidMethod0()).Times(2);
EXPECT_CALL(has_ref_, VoidConstMethod0()).Times(2);
Closure normal_cb = Bind(&VoidFunc0);
@@ -303,11 +304,14 @@ TEST_F(BindTest, FunctionTypeSupport) {
EXPECT_EQ(&no_ref_, normal_non_refcounted_cb.Run());
Closure method_cb = Bind(&HasRef::VoidMethod0, &has_ref_);
+ Closure method_refptr_cb = Bind(&HasRef::VoidMethod0,
+ make_scoped_refptr(&has_ref_));
Closure const_method_nonconst_obj_cb = Bind(&HasRef::VoidConstMethod0,
&has_ref_);
Closure const_method_const_obj_cb = Bind(&HasRef::VoidConstMethod0,
const_has_ref_ptr_);
method_cb.Run();
+ method_refptr_cb.Run();
const_method_nonconst_obj_cb.Run();
const_method_const_obj_cb.Run();