OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "base/memory/weak_ptr.h" | 7 #include "base/memory/weak_ptr.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 } | 27 } |
28 DCHECK(result); // We synchronized on thread destruction above. | 28 DCHECK(result); // We synchronized on thread destruction above. |
29 return result; | 29 return result; |
30 } | 30 } |
31 private: | 31 private: |
32 static void CreateObject(T** result) { | 32 static void CreateObject(T** result) { |
33 *result = new T; | 33 *result = new T; |
34 } | 34 } |
35 }; | 35 }; |
36 | 36 |
37 struct Base {}; | 37 struct Base { std::string member; }; |
38 struct Derived : Base {}; | 38 struct Derived : Base {}; |
39 | 39 |
40 struct Producer : SupportsWeakPtr<Producer> {}; | 40 struct Producer : SupportsWeakPtr<Producer> {}; |
41 struct Consumer { WeakPtr<Producer> producer; }; | 41 struct Consumer { WeakPtr<Producer> producer; }; |
42 | 42 |
43 // Helper class to create and destroy weak pointer copies | 43 // Helper class to create and destroy weak pointer copies |
44 // and delete objects on a background thread. | 44 // and delete objects on a background thread. |
45 class BackgroundThread : public Thread { | 45 class BackgroundThread : public Thread { |
46 public: | 46 public: |
47 BackgroundThread() | 47 BackgroundThread() |
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 Consumer consumer; | 330 Consumer consumer; |
331 consumer.producer = producer.AsWeakPtr(); | 331 consumer.producer = producer.AsWeakPtr(); |
332 thread.CreateConsumerFromConsumer(&consumer_copy, &consumer); | 332 thread.CreateConsumerFromConsumer(&consumer_copy, &consumer); |
333 } | 333 } |
334 EXPECT_TRUE(consumer_copy->producer == NULL); | 334 EXPECT_TRUE(consumer_copy->producer == NULL); |
335 thread.DeleteConsumer(consumer_copy); | 335 thread.DeleteConsumer(consumer_copy); |
336 } | 336 } |
337 | 337 |
338 TEST(WeakPtrTest, Dereference) { | 338 TEST(WeakPtrTest, Dereference) { |
339 Base data; | 339 Base data; |
| 340 data.member = "123456"; |
340 WeakPtrFactory<Base> factory(&data); | 341 WeakPtrFactory<Base> factory(&data); |
341 WeakPtr<Base> ptr = factory.GetWeakPtr(); | 342 WeakPtr<Base> ptr = factory.GetWeakPtr(); |
342 EXPECT_EQ(&data, ptr.get()); | 343 EXPECT_EQ(&data, ptr.get()); |
343 EXPECT_EQ(&data, *ptr); | 344 EXPECT_EQ(data.member, (*ptr).member); |
| 345 EXPECT_EQ(data.member, ptr->member); |
344 } | 346 } |
345 | 347 |
346 } // namespace base | 348 } // namespace base |
OLD | NEW |