Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2403)

Unified Diff: base/bind_unittest.cc

Issue 8774032: Add Pass(), which implements move semantics, to scoped_ptr, scoped_array, and scoped_ptr_malloc. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: small fixes. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/bind_unittest.cc
diff --git a/base/bind_unittest.cc b/base/bind_unittest.cc
index 654a2775a2f23efc2f266d8db8286e20fed15b0a..e736d242e777f1ffe36c3b02b468380bdc0430ea 100644
--- a/base/bind_unittest.cc
+++ b/base/bind_unittest.cc
@@ -5,6 +5,9 @@
#include "base/bind.h"
#include "base/callback.h"
+#include "base/memory/ref_counted.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/memory/weak_ptr.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -149,6 +152,11 @@ class DeleteCounter {
int* deletes_;
};
+template <typename T>
+T PassThru(T scoper) {
+ return scoper.Pass();
+}
+
// Some test functions that we can Bind to.
template <typename T>
T PolymorphicIdentity(T t) {
@@ -641,8 +649,8 @@ TEST_F(BindTest, Owned) {
// return the same value.
Callback<DeleteCounter*(void)> no_capture_cb =
Bind(&PolymorphicIdentity<DeleteCounter*>, Owned(counter));
- EXPECT_EQ(counter, no_capture_cb.Run());
- EXPECT_EQ(counter, no_capture_cb.Run());
+ ASSERT_EQ(counter, no_capture_cb.Run());
+ ASSERT_EQ(counter, no_capture_cb.Run());
EXPECT_EQ(0, deletes);
no_capture_cb.Reset(); // This should trigger a delete.
EXPECT_EQ(1, deletes);
@@ -657,6 +665,41 @@ TEST_F(BindTest, Owned) {
EXPECT_EQ(1, deletes);
}
+// PassScoped() wrapper support.
+// - Using PassScoped() gives Callback Ownership.
+// - Ownership is transfered from Callback to callee on the first Run().
+TEST_F(BindTest, ScopedPtr) {
+ int deletes = 0;
+
+ // If we never invoke the Callback, it returns ownership and deletes.
+ scoped_ptr<DeleteCounter> ptr(new DeleteCounter(&deletes));
+ Callback<scoped_ptr<DeleteCounter>(void)> unused_callback =
+ Bind(&PassThru<scoped_ptr<DeleteCounter> >, PassScoped(&ptr));
+ EXPECT_EQ(0, deletes);
+ unused_callback.Reset();
+ EXPECT_EQ(1, deletes);
+
+ // Check that ownership can be transfered back out.
+ deletes = 0;
+ DeleteCounter* counter = new DeleteCounter(&deletes);
+ ptr.reset(counter);
+ Callback<scoped_ptr<DeleteCounter>(void)> callback =
+ Bind(&PassThru<scoped_ptr<DeleteCounter> >, PassScoped(&ptr));
+ EXPECT_EQ(0, deletes);
+
+ scoped_ptr<DeleteCounter> result = callback.Run();
+ ASSERT_EQ(counter, result.get());
+ EXPECT_EQ(0, deletes);
+
+ // Resetting does not delete since ownership was transfered.
+ callback.Reset();
+ EXPECT_EQ(0, deletes);
+
+ // Ensure that we actually did get ownership.
+ result.reset();
+ EXPECT_EQ(1, deletes);
+}
+
// Argument Copy-constructor usage for non-reference parameters.
// - Bound arguments are only copied once.
// - Forwarded arguments are only copied once.
« no previous file with comments | « base/bind_internal.h.pump ('k') | base/callback_internal.h » ('j') | base/memory/scoped_ptr.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698