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 26 matching lines...) Expand all Loading... |
37 struct Base { std::string member; }; | 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() : Thread("owner_thread") {} |
48 : Thread("owner_thread") { | |
49 } | |
50 | 48 |
51 ~BackgroundThread() { | 49 virtual ~BackgroundThread() { |
52 Stop(); | 50 Stop(); |
53 } | 51 } |
54 | 52 |
55 void CreateConsumerFromProducer(Consumer** consumer, Producer* producer) { | 53 void CreateConsumerFromProducer(Consumer** consumer, Producer* producer) { |
56 WaitableEvent completion(true, false); | 54 WaitableEvent completion(true, false); |
57 message_loop()->PostTask( | 55 message_loop()->PostTask( |
58 FROM_HERE, | 56 FROM_HERE, |
59 base::Bind(&BackgroundThread::DoCreateFromProducer, consumer, producer, | 57 base::Bind(&BackgroundThread::DoCreateFromProducer, consumer, producer, |
60 &completion)); | 58 &completion)); |
61 completion.Wait(); | 59 completion.Wait(); |
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 Base data; | 337 Base data; |
340 data.member = "123456"; | 338 data.member = "123456"; |
341 WeakPtrFactory<Base> factory(&data); | 339 WeakPtrFactory<Base> factory(&data); |
342 WeakPtr<Base> ptr = factory.GetWeakPtr(); | 340 WeakPtr<Base> ptr = factory.GetWeakPtr(); |
343 EXPECT_EQ(&data, ptr.get()); | 341 EXPECT_EQ(&data, ptr.get()); |
344 EXPECT_EQ(data.member, (*ptr).member); | 342 EXPECT_EQ(data.member, (*ptr).member); |
345 EXPECT_EQ(data.member, ptr->member); | 343 EXPECT_EQ(data.member, ptr->member); |
346 } | 344 } |
347 | 345 |
348 } // namespace base | 346 } // namespace base |
OLD | NEW |