OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/memory/ref_counted.h" | 5 #include "base/memory/ref_counted.h" |
6 #include "testing/gtest/include/gtest/gtest.h" | 6 #include "testing/gtest/include/gtest/gtest.h" |
7 | 7 |
8 namespace { | 8 namespace { |
9 | 9 |
10 class SelfAssign : public base::RefCounted<SelfAssign> { | 10 class SelfAssign : public base::RefCounted<SelfAssign> { |
11 friend class base::RefCounted<SelfAssign>; | 11 friend class base::RefCounted<SelfAssign>; |
12 | 12 |
13 ~SelfAssign() {} | 13 ~SelfAssign() {} |
14 }; | 14 }; |
15 | 15 |
16 class CheckDerivedMemberAccess : public scoped_refptr<SelfAssign> { | 16 class CheckDerivedMemberAccess : public scoped_refptr<SelfAssign> { |
17 public: | 17 public: |
18 CheckDerivedMemberAccess() { | 18 CheckDerivedMemberAccess() { |
19 // This shouldn't compile if we don't have access to the member variable. | 19 // This shouldn't compile if we don't have access to the member variable. |
20 SelfAssign** pptr = &ptr_; | 20 SelfAssign** pptr = &ptr_; |
21 EXPECT_EQ(*pptr, ptr_); | 21 EXPECT_EQ(*pptr, ptr_); |
22 } | 22 } |
23 }; | 23 }; |
24 | 24 |
| 25 class ScopedRefPtrToSelf : public base::RefCounted<ScopedRefPtrToSelf> { |
| 26 public: |
| 27 ScopedRefPtrToSelf() |
| 28 : ALLOW_THIS_IN_INITIALIZER_LIST(self_ptr_(this)) { |
| 29 } |
| 30 ~ScopedRefPtrToSelf() { was_destroyed_ = true; } |
| 31 |
| 32 static bool was_destroyed() { return was_destroyed_; } |
| 33 |
| 34 void SelfDestruct() { self_ptr_ = NULL; } |
| 35 |
| 36 private: |
| 37 friend class base::RefCounted<ScopedRefPtrToSelf>; |
| 38 |
| 39 static bool was_destroyed_; |
| 40 |
| 41 scoped_refptr<ScopedRefPtrToSelf> self_ptr_; |
| 42 }; |
| 43 |
| 44 bool ScopedRefPtrToSelf::was_destroyed_ = false; |
| 45 |
25 } // end namespace | 46 } // end namespace |
26 | 47 |
27 TEST(RefCountedUnitTest, TestSelfAssignment) { | 48 TEST(RefCountedUnitTest, TestSelfAssignment) { |
28 SelfAssign* p = new SelfAssign; | 49 SelfAssign* p = new SelfAssign; |
29 scoped_refptr<SelfAssign> var(p); | 50 scoped_refptr<SelfAssign> var(p); |
30 var = var; | 51 var = var; |
31 EXPECT_EQ(var.get(), p); | 52 EXPECT_EQ(var.get(), p); |
32 } | 53 } |
33 | 54 |
34 TEST(RefCountedUnitTest, ScopedRefPtrMemberAccess) { | 55 TEST(RefCountedUnitTest, ScopedRefPtrMemberAccess) { |
35 CheckDerivedMemberAccess check; | 56 CheckDerivedMemberAccess check; |
36 } | 57 } |
| 58 |
| 59 TEST(RefCountedUnitTest, ScopedRefPtrToSelf) { |
| 60 ScopedRefPtrToSelf* check = new ScopedRefPtrToSelf(); |
| 61 EXPECT_FALSE(ScopedRefPtrToSelf::was_destroyed()); |
| 62 check->SelfDestruct(); |
| 63 EXPECT_TRUE(ScopedRefPtrToSelf::was_destroyed()); |
| 64 } |
OLD | NEW |