summaryrefslogtreecommitdiffstats
path: root/base/bind_unittest.cc
diff options
context:
space:
mode:
authorsergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-12-09 02:25:47 +0000
committersergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-12-09 02:25:47 +0000
commit3e6d120bf18e6ba69373cc635e1e7dd46543ea33 (patch)
tree3353f324a4796ffc419f3468daf436866b0036fd /base/bind_unittest.cc
parentd17d2fcf157bd17b810c25328a8e0960c9d1d834 (diff)
downloadchromium_src-3e6d120bf18e6ba69373cc635e1e7dd46543ea33.zip
chromium_src-3e6d120bf18e6ba69373cc635e1e7dd46543ea33.tar.gz
chromium_src-3e6d120bf18e6ba69373cc635e1e7dd46543ea33.tar.bz2
Revert 113722 - Add Pass(), which implements move semantics, to scoped_ptr, scoped_array, and scoped_ptr_malloc.
This modification to the scopers implements the "moveable but not copyable" semantics that were introduced in C++11's unique_ptr<>. With this, is now possible to use scopers as an argument type or a return type. This signifies, in the type system, transfer of ownership into a function or out of a function respectively. Calling, or returning such a function MUST use the temporary resulting from the scoper's Pass() function. You CANNOT just pass the scoper by copy as there is still no copy constructor or assignment operator; trying to do so will yield a compilation error. This distinction makes it possible to avoid the implicit ownership transfer issues of auto_ptr, but still allow us to have compiler enforced ownership transfer. Also adds a Passed() helper that allows using a scoper with Bind(). BUG=96118 TEST=new unittests Review URL: http://codereview.chromium.org/8774032 TBR=ajwong@chromium.org Review URL: http://codereview.chromium.org/8890060 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@113738 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/bind_unittest.cc')
-rw-r--r--base/bind_unittest.cc67
1 files changed, 5 insertions, 62 deletions
diff --git a/base/bind_unittest.cc b/base/bind_unittest.cc
index d150362..654a277 100644
--- a/base/bind_unittest.cc
+++ b/base/bind_unittest.cc
@@ -5,9 +5,6 @@
#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"
@@ -105,7 +102,7 @@ class CopyCounter {
(*copies_)++;
}
- // Probing for copies from coercion.
+ // Probing for copies from coerscion.
CopyCounter(const DerivedCopyCounter& other)
: copies_(other.copies_),
assigns_(other.assigns_) {
@@ -152,11 +149,6 @@ 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) {
@@ -649,8 +641,8 @@ TEST_F(BindTest, Owned) {
// return the same value.
Callback<DeleteCounter*(void)> no_capture_cb =
Bind(&PolymorphicIdentity<DeleteCounter*>, Owned(counter));
- ASSERT_EQ(counter, no_capture_cb.Run());
- ASSERT_EQ(counter, no_capture_cb.Run());
+ EXPECT_EQ(counter, no_capture_cb.Run());
+ EXPECT_EQ(counter, no_capture_cb.Run());
EXPECT_EQ(0, deletes);
no_capture_cb.Reset(); // This should trigger a delete.
EXPECT_EQ(1, deletes);
@@ -665,60 +657,11 @@ TEST_F(BindTest, Owned) {
EXPECT_EQ(1, deletes);
}
-// Passed() wrapper support.
-// - Passed() can be constructed from a pointer to scoper.
-// - Passed() can be constructed from a scoper rvalue.
-// - Using Passed() gives Callback Ownership.
-// - Ownership is transferred from Callback to callee on the first Run().
-// - Callback supports unbound arguments.
-TEST_F(BindTest, ScopedPtr) {
- int deletes = 0;
-
- // Tests the Passed() function's support for pointers.
- scoped_ptr<DeleteCounter> ptr(new DeleteCounter(&deletes));
- Callback<scoped_ptr<DeleteCounter>(void)> unused_callback =
- Bind(&PassThru<scoped_ptr<DeleteCounter> >, Passed(&ptr));
- EXPECT_FALSE(ptr.get());
- EXPECT_EQ(0, deletes);
-
- // If we never invoke the Callback, it retains ownership and deletes.
- unused_callback.Reset();
- EXPECT_EQ(1, deletes);
-
- // Tests the Passed() function's support for rvalues.
- deletes = 0;
- DeleteCounter* counter = new DeleteCounter(&deletes);
- Callback<scoped_ptr<DeleteCounter>(void)> callback =
- Bind(&PassThru<scoped_ptr<DeleteCounter> >,
- Passed(scoped_ptr<DeleteCounter>(counter)));
- EXPECT_FALSE(ptr.get());
- EXPECT_EQ(0, deletes);
-
- // Check that ownership can be transferred back out.
- scoped_ptr<DeleteCounter> result = callback.Run();
- ASSERT_EQ(counter, result.get());
- EXPECT_EQ(0, deletes);
-
- // Resetting does not delete since ownership was transferred.
- callback.Reset();
- EXPECT_EQ(0, deletes);
-
- // Ensure that we actually did get ownership.
- result.reset();
- EXPECT_EQ(1, deletes);
-
- // Test unbound argument forwarding.
- Callback<scoped_ptr<DeleteCounter>(scoped_ptr<DeleteCounter>)> cb_unbound =
- Bind(&PassThru<scoped_ptr<DeleteCounter> >);
- ptr.reset(new DeleteCounter(&deletes));
- cb_unbound.Run(ptr.Pass());
-}
-
// Argument Copy-constructor usage for non-reference parameters.
// - Bound arguments are only copied once.
// - Forwarded arguments are only copied once.
-// - Forwarded arguments with coercions are only copied twice (once for the
-// coercion, and one for the final dispatch).
+// - Forwarded arguments with coerscions are only copied twice (once for the
+// coerscion, and one for the final dispatch).
TEST_F(BindTest, ArgumentCopies) {
int copies = 0;
int assigns = 0;