summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorwillchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-23 00:08:42 +0000
committerwillchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-23 00:08:42 +0000
commita13f122545304dc92550ddf4cf5bed6053d41f4b (patch)
treedb832f9bf947b405d5a2922522302ba1198f096e /base
parentd53cbc05cbb75d40e8a088a9eb641c3d3ff6674c (diff)
downloadchromium_src-a13f122545304dc92550ddf4cf5bed6053d41f4b.zip
chromium_src-a13f122545304dc92550ddf4cf5bed6053d41f4b.tar.gz
chromium_src-a13f122545304dc92550ddf4cf5bed6053d41f4b.tar.bz2
Fix ScopedVector<T> passing via base::Bind()
The original implementation neglected to define the necessary CallbackParamTraits and CallbackForward implementations for ScopedVector. Fix scoped_ptr_malloc and scoped_array while I'm at it. Awesome. BUG=NONE TEST=scoped_vector_unittest.cc Review URL: http://codereview.chromium.org/9827003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@128350 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/callback_internal.h20
-rw-r--r--base/memory/scoped_vector_unittest.cc42
2 files changed, 58 insertions, 4 deletions
diff --git a/base/callback_internal.h b/base/callback_internal.h
index 21599bf..244504b 100644
--- a/base/callback_internal.h
+++ b/base/callback_internal.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// 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.
@@ -15,6 +15,9 @@
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
+template <typename T>
+class ScopedVector;
+
namespace base {
namespace internal {
@@ -146,6 +149,12 @@ struct CallbackParamTraits<scoped_ptr_malloc<T> > {
typedef scoped_ptr_malloc<T> StorageType;
};
+template <typename T>
+struct CallbackParamTraits<ScopedVector<T> > {
+ typedef ScopedVector<T> ForwardType;
+ typedef ScopedVector<T> StorageType;
+};
+
// CallbackForward() is a very limited simulation of C++11's std::forward()
// used by the Callback/Bind system for a set of movable-but-not-copyable
// types. It is needed because forwarding a movable-but-not-copyable
@@ -164,10 +173,15 @@ template <typename T>
scoped_ptr<T> CallbackForward(scoped_ptr<T>& p) { return p.Pass(); }
template <typename T>
-scoped_ptr<T> CallbackForward(scoped_array<T>& p) { return p.Pass(); }
+scoped_array<T> CallbackForward(scoped_array<T>& p) { return p.Pass(); }
+
+template <typename T>
+scoped_ptr_malloc<T> CallbackForward(scoped_ptr_malloc<T>& p) {
+ return p.Pass();
+}
template <typename T>
-scoped_ptr<T> CallbackForward(scoped_ptr_malloc<T>& p) { return p.Pass(); }
+ScopedVector<T> CallbackForward(ScopedVector<T>& p) { return p.Pass(); }
} // namespace internal
} // namespace base
diff --git a/base/memory/scoped_vector_unittest.cc b/base/memory/scoped_vector_unittest.cc
index cda1e4b..e10cb83 100644
--- a/base/memory/scoped_vector_unittest.cc
+++ b/base/memory/scoped_vector_unittest.cc
@@ -2,8 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h"
+
+#include "base/bind.h"
+#include "base/callback.h"
+#include "base/memory/scoped_ptr.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
@@ -161,6 +164,43 @@ TEST(ScopedVectorTest, MoveAssign) {
EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state());
}
+class DeleteCounter {
+ public:
+ explicit DeleteCounter(int* deletes)
+ : deletes_(deletes) {
+ }
+
+ ~DeleteCounter() {
+ (*deletes_)++;
+ }
+
+ void VoidMethod0() {}
+
+ private:
+ int* const deletes_;
+
+ DISALLOW_COPY_AND_ASSIGN(DeleteCounter);
+};
+
+template <typename T>
+ScopedVector<T> PassThru(ScopedVector<T> scoper) {
+ return scoper.Pass();
+}
+
+TEST(ScopedVectorTest, Passed) {
+ int deletes = 0;
+ ScopedVector<DeleteCounter> deleter_vector;
+ deleter_vector.push_back(new DeleteCounter(&deletes));
+ EXPECT_EQ(0, deletes);
+ base::Callback<ScopedVector<DeleteCounter>(void)> callback =
+ base::Bind(&PassThru<DeleteCounter>, base::Passed(&deleter_vector));
+ EXPECT_EQ(0, deletes);
+ ScopedVector<DeleteCounter> result = callback.Run();
+ EXPECT_EQ(0, deletes);
+ result.reset();
+ EXPECT_EQ(1, deletes);
+};
+
TEST(ScopedVectorTest, InsertRange) {
LifeCycleWatcher watchers[5];