You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
7.3 KiB

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