perkj | 0489e49 | 2016-10-20 07:24:01 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 11 | #include "rtc_base/ref_counted_object.h" |
| 12 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 13 | #include <memory> |
perkj | 0489e49 | 2016-10-20 07:24:01 | [diff] [blame] | 14 | #include <string> |
Danil Chapovalov | 8df643b | 2021-01-22 15:11:10 | [diff] [blame] | 15 | #include <type_traits> |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 16 | #include <utility> |
perkj | 0489e49 | 2016-10-20 07:24:01 | [diff] [blame] | 17 | |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 18 | #include "absl/strings/string_view.h" |
Niels Möller | 105711e | 2022-06-14 13:48:26 | [diff] [blame] | 19 | #include "api/make_ref_counted.h" |
Mirko Bonadei | d970807 | 2019-01-25 19:26:48 | [diff] [blame] | 20 | #include "api/scoped_refptr.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 21 | #include "rtc_base/ref_count.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 22 | #include "test/gtest.h" |
perkj | 0489e49 | 2016-10-20 07:24:01 | [diff] [blame] | 23 | |
| 24 | namespace rtc { |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | class A { |
| 29 | public: |
| 30 | A() {} |
| 31 | |
Byoungchan Lee | 14af762 | 2022-01-11 20:24:58 | [diff] [blame] | 32 | A(const A&) = delete; |
| 33 | A& operator=(const A&) = delete; |
perkj | 0489e49 | 2016-10-20 07:24:01 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | class RefClass : public RefCountInterface { |
| 37 | public: |
| 38 | RefClass() {} |
| 39 | |
| 40 | protected: |
ehmaldonado | da8dcfb | 2017-01-04 15:11:23 | [diff] [blame] | 41 | ~RefClass() override {} |
perkj | 0489e49 | 2016-10-20 07:24:01 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | class RefClassWithRvalue : public RefCountInterface { |
| 45 | public: |
| 46 | explicit RefClassWithRvalue(std::unique_ptr<A> a) : a_(std::move(a)) {} |
| 47 | |
| 48 | protected: |
ehmaldonado | da8dcfb | 2017-01-04 15:11:23 | [diff] [blame] | 49 | ~RefClassWithRvalue() override {} |
perkj | 0489e49 | 2016-10-20 07:24:01 | [diff] [blame] | 50 | |
| 51 | public: |
| 52 | std::unique_ptr<A> a_; |
| 53 | }; |
| 54 | |
| 55 | class RefClassWithMixedValues : public RefCountInterface { |
| 56 | public: |
Ali Tofigh | 7fa9057 | 2022-03-17 14:47:49 | [diff] [blame] | 57 | RefClassWithMixedValues(std::unique_ptr<A> a, int b, absl::string_view c) |
perkj | 0489e49 | 2016-10-20 07:24:01 | [diff] [blame] | 58 | : a_(std::move(a)), b_(b), c_(c) {} |
| 59 | |
| 60 | protected: |
ehmaldonado | da8dcfb | 2017-01-04 15:11:23 | [diff] [blame] | 61 | ~RefClassWithMixedValues() override {} |
perkj | 0489e49 | 2016-10-20 07:24:01 | [diff] [blame] | 62 | |
| 63 | public: |
| 64 | std::unique_ptr<A> a_; |
| 65 | int b_; |
| 66 | std::string c_; |
| 67 | }; |
| 68 | |
Tomas Gunnarsson | d784200 | 2021-04-22 15:41:33 | [diff] [blame] | 69 | class Foo { |
| 70 | public: |
| 71 | Foo() {} |
| 72 | Foo(int i, int j) : foo_(i + j) {} |
| 73 | int foo_ = 0; |
| 74 | }; |
| 75 | |
| 76 | class FooItf : public RefCountInterface { |
| 77 | public: |
| 78 | FooItf() {} |
| 79 | FooItf(int i, int j) : foo_(i + j) {} |
| 80 | int foo_ = 0; |
| 81 | }; |
| 82 | |
perkj | 0489e49 | 2016-10-20 07:24:01 | [diff] [blame] | 83 | } // namespace |
| 84 | |
Niels Möller | 6f72f56 | 2017-10-19 11:15:17 | [diff] [blame] | 85 | TEST(RefCountedObject, HasOneRef) { |
perkj | 0489e49 | 2016-10-20 07:24:01 | [diff] [blame] | 86 | scoped_refptr<RefCountedObject<RefClass>> aref( |
| 87 | new RefCountedObject<RefClass>()); |
| 88 | EXPECT_TRUE(aref->HasOneRef()); |
Niels Möller | 6f72f56 | 2017-10-19 11:15:17 | [diff] [blame] | 89 | aref->AddRef(); |
| 90 | EXPECT_FALSE(aref->HasOneRef()); |
| 91 | EXPECT_EQ(aref->Release(), RefCountReleaseStatus::kOtherRefsRemained); |
| 92 | EXPECT_TRUE(aref->HasOneRef()); |
perkj | 0489e49 | 2016-10-20 07:24:01 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | TEST(RefCountedObject, SupportRValuesInCtor) { |
| 96 | std::unique_ptr<A> a(new A()); |
| 97 | scoped_refptr<RefClassWithRvalue> ref( |
| 98 | new RefCountedObject<RefClassWithRvalue>(std::move(a))); |
| 99 | EXPECT_TRUE(ref->a_.get() != nullptr); |
| 100 | EXPECT_TRUE(a.get() == nullptr); |
| 101 | } |
| 102 | |
| 103 | TEST(RefCountedObject, SupportMixedTypesInCtor) { |
| 104 | std::unique_ptr<A> a(new A()); |
| 105 | int b = 9; |
| 106 | std::string c = "hello"; |
| 107 | scoped_refptr<RefClassWithMixedValues> ref( |
| 108 | new RefCountedObject<RefClassWithMixedValues>(std::move(a), b, c)); |
| 109 | EXPECT_TRUE(ref->a_.get() != nullptr); |
| 110 | EXPECT_TRUE(a.get() == nullptr); |
| 111 | EXPECT_EQ(b, ref->b_); |
| 112 | EXPECT_EQ(c, ref->c_); |
| 113 | } |
| 114 | |
Danil Chapovalov | 8df643b | 2021-01-22 15:11:10 | [diff] [blame] | 115 | TEST(FinalRefCountedObject, CanWrapIntoScopedRefptr) { |
| 116 | using WrappedTyped = FinalRefCountedObject<A>; |
| 117 | static_assert(!std::is_polymorphic<WrappedTyped>::value, ""); |
| 118 | scoped_refptr<WrappedTyped> ref(new WrappedTyped()); |
| 119 | EXPECT_TRUE(ref.get()); |
| 120 | EXPECT_TRUE(ref->HasOneRef()); |
| 121 | // Test reference counter is updated on some simple operations. |
| 122 | scoped_refptr<WrappedTyped> ref2 = ref; |
| 123 | EXPECT_FALSE(ref->HasOneRef()); |
| 124 | EXPECT_FALSE(ref2->HasOneRef()); |
| 125 | |
| 126 | ref = nullptr; |
| 127 | EXPECT_TRUE(ref2->HasOneRef()); |
| 128 | } |
| 129 | |
Danil Chapovalov | 80b7628 | 2021-04-26 14:32:27 | [diff] [blame] | 130 | TEST(FinalRefCountedObject, CanCreateFromMovedType) { |
| 131 | class MoveOnly { |
| 132 | public: |
| 133 | MoveOnly(int a) : a_(a) {} |
| 134 | MoveOnly(MoveOnly&&) = default; |
| 135 | |
| 136 | int a() { return a_; } |
| 137 | |
| 138 | private: |
| 139 | int a_; |
| 140 | }; |
| 141 | MoveOnly foo(5); |
| 142 | auto ref = make_ref_counted<MoveOnly>(std::move(foo)); |
| 143 | EXPECT_EQ(ref->a(), 5); |
| 144 | } |
| 145 | |
Tomas Gunnarsson | d784200 | 2021-04-22 15:41:33 | [diff] [blame] | 146 | // This test is mostly a compile-time test for scoped_refptr compatibility. |
| 147 | TEST(RefCounted, SmartPointers) { |
| 148 | // Sanity compile-time tests. FooItf is virtual, Foo is not, FooItf inherits |
| 149 | // from RefCountInterface, Foo does not. |
| 150 | static_assert(std::is_base_of<RefCountInterface, FooItf>::value, ""); |
| 151 | static_assert(!std::is_base_of<RefCountInterface, Foo>::value, ""); |
| 152 | static_assert(std::is_polymorphic<FooItf>::value, ""); |
| 153 | static_assert(!std::is_polymorphic<Foo>::value, ""); |
| 154 | |
Tomas Gunnarsson | d784200 | 2021-04-22 15:41:33 | [diff] [blame] | 155 | { |
| 156 | // Test with FooItf, a class that inherits from RefCountInterface. |
| 157 | // Check that we get a valid FooItf reference counted object. |
| 158 | auto p = make_ref_counted<FooItf>(2, 3); |
| 159 | EXPECT_NE(p.get(), nullptr); |
| 160 | EXPECT_EQ(p->foo_, 5); // the FooItf ctor just stores 2+3 in foo_. |
| 161 | |
Niels Möller | bed8507 | 2022-06-14 10:30:16 | [diff] [blame] | 162 | // Declaring what should result in the same type as `p` is of. |
| 163 | scoped_refptr<FooItf> p2 = p; |
Tomas Gunnarsson | d784200 | 2021-04-22 15:41:33 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | { |
| 167 | // Same for `Foo` |
| 168 | auto p = make_ref_counted<Foo>(2, 3); |
| 169 | EXPECT_NE(p.get(), nullptr); |
| 170 | EXPECT_EQ(p->foo_, 5); |
Niels Möller | bed8507 | 2022-06-14 10:30:16 | [diff] [blame] | 171 | scoped_refptr<FinalRefCountedObject<Foo>> p2 = p; |
Tomas Gunnarsson | d784200 | 2021-04-22 15:41:33 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
perkj | 0489e49 | 2016-10-20 07:24:01 | [diff] [blame] | 175 | } // namespace rtc |