henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 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 | |
deadbeef | 08187d4 | 2017-02-25 11:21:18 -0800 | [diff] [blame] | 11 | #include <type_traits> |
| 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "rtc_base/bind.h" |
| 14 | #include "rtc_base/gunit.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/refcount.h" |
Niels Möller | 84255bb | 2017-10-06 13:43:23 +0200 | [diff] [blame] | 17 | #include "rtc_base/refcountedobject.h" |
Magnus Jedvert | d3de9c5 | 2015-08-20 16:03:52 +0200 | [diff] [blame] | 18 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 19 | namespace rtc { |
| 20 | |
| 21 | namespace { |
| 22 | |
Magnus Jedvert | b274547 | 2015-08-25 17:56:22 +0200 | [diff] [blame] | 23 | struct LifeTimeCheck; |
| 24 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 25 | struct MethodBindTester { |
| 26 | void NullaryVoid() { ++call_count; } |
| 27 | int NullaryInt() { ++call_count; return 1; } |
| 28 | int NullaryConst() const { ++call_count; return 2; } |
| 29 | void UnaryVoid(int dummy) { ++call_count; } |
| 30 | template <class T> T Identity(T value) { ++call_count; return value; } |
noahric | 5d9b92b | 2015-10-24 11:14:46 -0700 | [diff] [blame] | 31 | int UnaryByPointer(int* value) const { |
| 32 | ++call_count; |
| 33 | return ++(*value); |
| 34 | } |
| 35 | int UnaryByRef(const int& value) const { |
| 36 | ++call_count; |
| 37 | return ++const_cast<int&>(value); |
| 38 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 39 | int Multiply(int a, int b) const { ++call_count; return a * b; } |
Magnus Jedvert | b274547 | 2015-08-25 17:56:22 +0200 | [diff] [blame] | 40 | void RefArgument(const scoped_refptr<LifeTimeCheck>& object) { |
| 41 | EXPECT_TRUE(object.get() != nullptr); |
| 42 | } |
| 43 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 44 | mutable int call_count; |
| 45 | }; |
| 46 | |
Magnus Jedvert | d3de9c5 | 2015-08-20 16:03:52 +0200 | [diff] [blame] | 47 | struct A { int dummy; }; |
| 48 | struct B: public RefCountInterface { int dummy; }; |
| 49 | struct C: public A, B {}; |
| 50 | struct D { |
| 51 | int AddRef(); |
| 52 | }; |
| 53 | struct E: public D { |
| 54 | int Release(); |
| 55 | }; |
| 56 | struct F { |
| 57 | void AddRef(); |
| 58 | void Release(); |
| 59 | }; |
| 60 | |
Magnus Jedvert | b274547 | 2015-08-25 17:56:22 +0200 | [diff] [blame] | 61 | struct LifeTimeCheck { |
| 62 | LifeTimeCheck() : ref_count_(0) {} |
| 63 | void AddRef() { ++ref_count_; } |
| 64 | void Release() { --ref_count_; } |
Magnus Jedvert | d3de9c5 | 2015-08-20 16:03:52 +0200 | [diff] [blame] | 65 | void NullaryVoid() {} |
Magnus Jedvert | b274547 | 2015-08-25 17:56:22 +0200 | [diff] [blame] | 66 | int ref_count_; |
Magnus Jedvert | d3de9c5 | 2015-08-20 16:03:52 +0200 | [diff] [blame] | 67 | }; |
| 68 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 69 | int Return42() { return 42; } |
| 70 | int Negate(int a) { return -a; } |
| 71 | int Multiply(int a, int b) { return a * b; } |
| 72 | |
| 73 | } // namespace |
| 74 | |
Magnus Jedvert | d3de9c5 | 2015-08-20 16:03:52 +0200 | [diff] [blame] | 75 | // Try to catch any problem with scoped_refptr type deduction in rtc::Bind at |
| 76 | // compile time. |
| 77 | #define EXPECT_IS_CAPTURED_AS_PTR(T) \ |
| 78 | static_assert(is_same<detail::PointerType<T>::type, T*>::value, \ |
| 79 | "PointerType") |
| 80 | #define EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(T) \ |
| 81 | static_assert( \ |
| 82 | is_same<detail::PointerType<T>::type, scoped_refptr<T>>::value, \ |
| 83 | "PointerType") |
| 84 | |
| 85 | EXPECT_IS_CAPTURED_AS_PTR(void); |
| 86 | EXPECT_IS_CAPTURED_AS_PTR(int); |
| 87 | EXPECT_IS_CAPTURED_AS_PTR(double); |
| 88 | EXPECT_IS_CAPTURED_AS_PTR(A); |
| 89 | EXPECT_IS_CAPTURED_AS_PTR(D); |
| 90 | EXPECT_IS_CAPTURED_AS_PTR(RefCountInterface*); |
deadbeef | ccaaffb | 2017-02-25 12:56:20 -0800 | [diff] [blame] | 91 | EXPECT_IS_CAPTURED_AS_PTR( |
| 92 | decltype(Unretained<RefCountedObject<RefCountInterface>>)); |
Magnus Jedvert | d3de9c5 | 2015-08-20 16:03:52 +0200 | [diff] [blame] | 93 | |
| 94 | EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(RefCountInterface); |
| 95 | EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(B); |
| 96 | EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(C); |
| 97 | EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(E); |
| 98 | EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(F); |
| 99 | EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(RefCountedObject<RefCountInterface>); |
| 100 | EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(RefCountedObject<B>); |
| 101 | EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(RefCountedObject<C>); |
Magnus Jedvert | 1b40a9a | 2015-10-12 15:50:43 +0200 | [diff] [blame] | 102 | EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(const RefCountedObject<RefCountInterface>); |
Magnus Jedvert | d3de9c5 | 2015-08-20 16:03:52 +0200 | [diff] [blame] | 103 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 104 | TEST(BindTest, BindToMethod) { |
| 105 | MethodBindTester object = {0}; |
| 106 | EXPECT_EQ(0, object.call_count); |
| 107 | Bind(&MethodBindTester::NullaryVoid, &object)(); |
| 108 | EXPECT_EQ(1, object.call_count); |
| 109 | EXPECT_EQ(1, Bind(&MethodBindTester::NullaryInt, &object)()); |
| 110 | EXPECT_EQ(2, object.call_count); |
| 111 | EXPECT_EQ(2, Bind(&MethodBindTester::NullaryConst, |
| 112 | static_cast<const MethodBindTester*>(&object))()); |
| 113 | EXPECT_EQ(3, object.call_count); |
| 114 | Bind(&MethodBindTester::UnaryVoid, &object, 5)(); |
| 115 | EXPECT_EQ(4, object.call_count); |
| 116 | EXPECT_EQ(100, Bind(&MethodBindTester::Identity<int>, &object, 100)()); |
| 117 | EXPECT_EQ(5, object.call_count); |
| 118 | const std::string string_value("test string"); |
| 119 | EXPECT_EQ(string_value, Bind(&MethodBindTester::Identity<std::string>, |
| 120 | &object, string_value)()); |
| 121 | EXPECT_EQ(6, object.call_count); |
| 122 | int value = 11; |
noahric | 5d9b92b | 2015-10-24 11:14:46 -0700 | [diff] [blame] | 123 | // Bind binds by value, even if the method signature is by reference, so |
| 124 | // "reference" binds require pointers. |
| 125 | EXPECT_EQ(12, Bind(&MethodBindTester::UnaryByPointer, &object, &value)()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 126 | EXPECT_EQ(12, value); |
| 127 | EXPECT_EQ(7, object.call_count); |
noahric | 5d9b92b | 2015-10-24 11:14:46 -0700 | [diff] [blame] | 128 | // It's possible to bind to a function that takes a const reference, though |
| 129 | // the capture will be a copy. See UnaryByRef hackery above where it removes |
| 130 | // the const to make sure the underlying storage is, in fact, a copy. |
| 131 | EXPECT_EQ(13, Bind(&MethodBindTester::UnaryByRef, &object, value)()); |
| 132 | // But the original value is unmodified. |
| 133 | EXPECT_EQ(12, value); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 134 | EXPECT_EQ(8, object.call_count); |
noahric | 5d9b92b | 2015-10-24 11:14:46 -0700 | [diff] [blame] | 135 | EXPECT_EQ(56, Bind(&MethodBindTester::Multiply, &object, 7, 8)()); |
| 136 | EXPECT_EQ(9, object.call_count); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | TEST(BindTest, BindToFunction) { |
| 140 | EXPECT_EQ(42, Bind(&Return42)()); |
| 141 | EXPECT_EQ(3, Bind(&Negate, -3)()); |
| 142 | EXPECT_EQ(56, Bind(&Multiply, 8, 7)()); |
| 143 | } |
| 144 | |
Magnus Jedvert | d3de9c5 | 2015-08-20 16:03:52 +0200 | [diff] [blame] | 145 | // Test Bind where method object implements RefCountInterface and is passed as a |
| 146 | // pointer. |
| 147 | TEST(BindTest, CapturePointerAsScopedRefPtr) { |
Magnus Jedvert | b274547 | 2015-08-25 17:56:22 +0200 | [diff] [blame] | 148 | LifeTimeCheck object; |
| 149 | EXPECT_EQ(object.ref_count_, 0); |
| 150 | scoped_refptr<LifeTimeCheck> scoped_object(&object); |
| 151 | EXPECT_EQ(object.ref_count_, 1); |
Magnus Jedvert | d3de9c5 | 2015-08-20 16:03:52 +0200 | [diff] [blame] | 152 | { |
Magnus Jedvert | b274547 | 2015-08-25 17:56:22 +0200 | [diff] [blame] | 153 | auto functor = Bind(&LifeTimeCheck::NullaryVoid, &object); |
| 154 | EXPECT_EQ(object.ref_count_, 2); |
| 155 | scoped_object = nullptr; |
| 156 | EXPECT_EQ(object.ref_count_, 1); |
Magnus Jedvert | d3de9c5 | 2015-08-20 16:03:52 +0200 | [diff] [blame] | 157 | } |
Magnus Jedvert | b274547 | 2015-08-25 17:56:22 +0200 | [diff] [blame] | 158 | EXPECT_EQ(object.ref_count_, 0); |
Magnus Jedvert | d3de9c5 | 2015-08-20 16:03:52 +0200 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // Test Bind where method object implements RefCountInterface and is passed as a |
| 162 | // scoped_refptr<>. |
| 163 | TEST(BindTest, CaptureScopedRefPtrAsScopedRefPtr) { |
Magnus Jedvert | b274547 | 2015-08-25 17:56:22 +0200 | [diff] [blame] | 164 | LifeTimeCheck object; |
| 165 | EXPECT_EQ(object.ref_count_, 0); |
| 166 | scoped_refptr<LifeTimeCheck> scoped_object(&object); |
| 167 | EXPECT_EQ(object.ref_count_, 1); |
Magnus Jedvert | d3de9c5 | 2015-08-20 16:03:52 +0200 | [diff] [blame] | 168 | { |
Magnus Jedvert | b274547 | 2015-08-25 17:56:22 +0200 | [diff] [blame] | 169 | auto functor = Bind(&LifeTimeCheck::NullaryVoid, scoped_object); |
| 170 | EXPECT_EQ(object.ref_count_, 2); |
| 171 | scoped_object = nullptr; |
| 172 | EXPECT_EQ(object.ref_count_, 1); |
Magnus Jedvert | d3de9c5 | 2015-08-20 16:03:52 +0200 | [diff] [blame] | 173 | } |
Magnus Jedvert | b274547 | 2015-08-25 17:56:22 +0200 | [diff] [blame] | 174 | EXPECT_EQ(object.ref_count_, 0); |
Magnus Jedvert | d3de9c5 | 2015-08-20 16:03:52 +0200 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // Test Bind where method object is captured as scoped_refptr<> and the functor |
| 178 | // dies while there are references left. |
| 179 | TEST(BindTest, FunctorReleasesObjectOnDestruction) { |
Magnus Jedvert | b274547 | 2015-08-25 17:56:22 +0200 | [diff] [blame] | 180 | LifeTimeCheck object; |
| 181 | EXPECT_EQ(object.ref_count_, 0); |
| 182 | scoped_refptr<LifeTimeCheck> scoped_object(&object); |
| 183 | EXPECT_EQ(object.ref_count_, 1); |
| 184 | Bind(&LifeTimeCheck::NullaryVoid, &object)(); |
| 185 | EXPECT_EQ(object.ref_count_, 1); |
| 186 | scoped_object = nullptr; |
| 187 | EXPECT_EQ(object.ref_count_, 0); |
| 188 | } |
| 189 | |
| 190 | // Test Bind with scoped_refptr<> argument. |
| 191 | TEST(BindTest, ScopedRefPointerArgument) { |
| 192 | LifeTimeCheck object; |
| 193 | EXPECT_EQ(object.ref_count_, 0); |
| 194 | scoped_refptr<LifeTimeCheck> scoped_object(&object); |
| 195 | EXPECT_EQ(object.ref_count_, 1); |
| 196 | { |
| 197 | MethodBindTester bind_tester; |
| 198 | auto functor = |
| 199 | Bind(&MethodBindTester::RefArgument, &bind_tester, scoped_object); |
| 200 | EXPECT_EQ(object.ref_count_, 2); |
| 201 | } |
| 202 | EXPECT_EQ(object.ref_count_, 1); |
| 203 | scoped_object = nullptr; |
| 204 | EXPECT_EQ(object.ref_count_, 0); |
| 205 | } |
| 206 | |
| 207 | namespace { |
| 208 | |
| 209 | const int* Ref(const int& a) { return &a; } |
| 210 | |
| 211 | } // anonymous namespace |
| 212 | |
noahric | 5d9b92b | 2015-10-24 11:14:46 -0700 | [diff] [blame] | 213 | // Test Bind with non-scoped_refptr<> reference argument, which should be |
| 214 | // modified to a non-reference capture. |
Magnus Jedvert | b274547 | 2015-08-25 17:56:22 +0200 | [diff] [blame] | 215 | TEST(BindTest, RefArgument) { |
| 216 | const int x = 42; |
noahric | 5d9b92b | 2015-10-24 11:14:46 -0700 | [diff] [blame] | 217 | EXPECT_EQ(&x, Ref(x)); |
| 218 | // Bind() should make a copy of |x|, i.e. the pointers should be different. |
Magnus Jedvert | b274547 | 2015-08-25 17:56:22 +0200 | [diff] [blame] | 219 | auto functor = Bind(&Ref, x); |
noahric | 5d9b92b | 2015-10-24 11:14:46 -0700 | [diff] [blame] | 220 | EXPECT_NE(&x, functor()); |
Magnus Jedvert | d3de9c5 | 2015-08-20 16:03:52 +0200 | [diff] [blame] | 221 | } |
| 222 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 223 | } // namespace rtc |