diff options
author | darin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-01 20:50:17 +0000 |
---|---|---|
committer | darin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-01 20:50:17 +0000 |
commit | 3125d6467fc6d03e81776fca4228e92dd107e93a (patch) | |
tree | 7a5b154e921fe88291701bc3d67ec577ed0f2c4f /base/weak_ptr_unittest.cc | |
parent | 40e62b147d083b8e9bb5b42fdb065dd5f654f523 (diff) | |
download | chromium_src-3125d6467fc6d03e81776fca4228e92dd107e93a.zip chromium_src-3125d6467fc6d03e81776fca4228e92dd107e93a.tar.gz chromium_src-3125d6467fc6d03e81776fca4228e92dd107e93a.tar.bz2 |
Add a WeakPtr<T> class.
This allows a class to hand out weak pointers to itself that will be nulled out
automatically when the class instance is destroyed.
I have provided two ways for a class to implement weak pointers. It can either
subclass SupportsWeakPtr (like subclassing RefCounted) or it can be composed of
WeakPtrFactory (like having a ScopedRunnableMethodFactory member).
Eventually, I'd like to make it possible to pass a WeakPtr<T> as the first
parameter to NewRunnableMethod. This will make ScopedRunnableMethodFactory
obsolete and should help cleanup some code.
One feature that makes the implementation here a bit more complicated is that
it is possible to pass a WeakPtr<U> to a method that takes a WeakPtr<T>
provided U "is a" T. This proved useful in RenderView, which can then give
out weak references to both itself as well as to an interface it implements.
This informed the design of WeakPtr, causing it to have a T* ptr_ member
instead of stashing that pointer within the ref counted WeakReference object.
R=brettw
BUG=none
TEST=weak_ptr_unittest.cc
Review URL: http://codereview.chromium.org/183026
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@25087 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/weak_ptr_unittest.cc')
-rw-r--r-- | base/weak_ptr_unittest.cc | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/base/weak_ptr_unittest.cc b/base/weak_ptr_unittest.cc new file mode 100644 index 0000000..80c277b --- /dev/null +++ b/base/weak_ptr_unittest.cc @@ -0,0 +1,122 @@ +// Copyright (c) 2009 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. + +#include "testing/gtest/include/gtest/gtest.h" +#include "base/thread.h" +#include "base/weak_ptr.h" + +namespace base { +namespace { + +template <class T> +class OffThreadObjectCreator { + public: + static T* NewObject() { + T* result; + { + Thread creator_thread("creator_thread"); + creator_thread.Start(); + creator_thread.message_loop()->PostTask(FROM_HERE, + NewRunnableFunction(OffThreadObjectCreator::CreateObject, &result)); + } + DCHECK(result); // We synchronized on thread destruction above. + return result; + } + private: + static void CreateObject(T** result) { + *result = new T; + } +}; + +struct Base {}; +struct Derived : Base {}; + +struct Producer : SupportsWeakPtr<Producer> {}; +struct Consumer { WeakPtr<Producer> producer; }; + +} // namespace + +TEST(WeakPtrTest, Basic) { + int data; + WeakPtrFactory<int> factory(&data); + WeakPtr<int> ptr = factory.GetWeakPtr(); + EXPECT_EQ(&data, ptr.get()); +} + +TEST(WeakPtrTest, Comparison) { + int data; + WeakPtrFactory<int> factory(&data); + WeakPtr<int> ptr = factory.GetWeakPtr(); + WeakPtr<int> ptr2 = ptr; + EXPECT_TRUE(ptr == ptr2); +} + +TEST(WeakPtrTest, OutOfScope) { + WeakPtr<int> ptr; + EXPECT_EQ(NULL, ptr.get()); + { + int data; + WeakPtrFactory<int> factory(&data); + ptr = factory.GetWeakPtr(); + } + EXPECT_EQ(NULL, ptr.get()); +} + +TEST(WeakPtrTest, Multiple) { + WeakPtr<int> a, b; + { + int data; + WeakPtrFactory<int> factory(&data); + a = factory.GetWeakPtr(); + b = factory.GetWeakPtr(); + EXPECT_EQ(&data, a.get()); + EXPECT_EQ(&data, b.get()); + } + EXPECT_EQ(NULL, a.get()); + EXPECT_EQ(NULL, b.get()); +} + +TEST(WeakPtrTest, UpCast) { + Derived data; + WeakPtrFactory<Derived> factory(&data); + WeakPtr<Base> ptr = factory.GetWeakPtr(); + ptr = factory.GetWeakPtr(); + EXPECT_EQ(ptr.get(), &data); +} + +TEST(WeakPtrTest, SupportsWeakPtr) { + Producer f; + WeakPtr<Producer> ptr = f.AsWeakPtr(); + EXPECT_EQ(&f, ptr.get()); +} + +TEST(WeakPtrTest, InvalidateWeakPtrs) { + int data; + WeakPtrFactory<int> factory(&data); + WeakPtr<int> ptr = factory.GetWeakPtr(); + EXPECT_EQ(&data, ptr.get()); + factory.InvalidateWeakPtrs(); + EXPECT_EQ(NULL, ptr.get()); +} + +TEST(WeakPtrTest, SingleThreaded1) { + // Test that it is OK to create a class that supports weak references on one + // thread, but use it on another. This tests that we do not trip runtime + // checks that ensure that a weak reference is not used by multiple threads. + scoped_ptr<Producer> producer(OffThreadObjectCreator<Producer>::NewObject()); + WeakPtr<Producer> weak_producer = producer->AsWeakPtr(); + EXPECT_EQ(producer.get(), weak_producer.get()); +} + +TEST(WeakPtrTest, SingleThreaded2) { + // Test that it is OK to create a class that has a WeakPtr member on one + // thread, but use it on another. This tests that we do not trip runtime + // checks that ensure that a weak reference is not used by multiple threads. + scoped_ptr<Consumer> consumer(OffThreadObjectCreator<Consumer>::NewObject()); + Producer producer; + consumer->producer = producer.AsWeakPtr(); + EXPECT_EQ(&producer, consumer->producer.get()); +} + +} // namespace base |