Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(416)

Side by Side Diff: webrtc/base/bind_unittest.cc

Issue 1300523004: rtc::Bind: Capture method objects as scoped_refptr if they are ref counted (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: landable patch Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« webrtc/base/bind.h ('K') | « webrtc/base/bind.h.pump ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/base/bind.h" 11 #include "webrtc/base/bind.h"
12 #include "webrtc/base/gunit.h" 12 #include "webrtc/base/gunit.h"
13 13
14 namespace rtc { 14 namespace rtc {
15 15
16 namespace { 16 namespace {
17 17
18 struct MethodBindTester { 18 struct MethodBindTester {
19 void NullaryVoid() { ++call_count; } 19 void NullaryVoid() { ++call_count; }
20 int NullaryInt() { ++call_count; return 1; } 20 int NullaryInt() { ++call_count; return 1; }
21 int NullaryConst() const { ++call_count; return 2; } 21 int NullaryConst() const { ++call_count; return 2; }
22 void UnaryVoid(int dummy) { ++call_count; } 22 void UnaryVoid(int dummy) { ++call_count; }
23 template <class T> T Identity(T value) { ++call_count; return value; } 23 template <class T> T Identity(T value) { ++call_count; return value; }
24 int UnaryByRef(int& value) const { ++call_count; return ++value; } // NOLINT 24 int UnaryByRef(int& value) const { ++call_count; return ++value; } // NOLINT
25 int Multiply(int a, int b) const { ++call_count; return a * b; } 25 int Multiply(int a, int b) const { ++call_count; return a * b; }
26 mutable int call_count; 26 mutable int call_count;
27 }; 27 };
28 28
29 struct A { int dummy; };
30 struct B: public RefCountInterface { int dummy; };
31 struct C: public A, B {};
32
33 class LifeTimeCheck : public RefCountInterface {
34 public:
35 LifeTimeCheck(bool* hasDied) : hasDied_(hasDied), isOkToDie(false) {}
36 ~LifeTimeCheck() {
37 EXPECT_TRUE(isOkToDie);
38 *hasDied_ = true;
39 }
40 void PrepareToDie() { isOkToDie = true; }
41 void NullaryVoid() {}
42
43 private:
44 bool* const hasDied_;
45 bool isOkToDie;
46 };
47
29 int Return42() { return 42; } 48 int Return42() { return 42; }
30 int Negate(int a) { return -a; } 49 int Negate(int a) { return -a; }
31 int Multiply(int a, int b) { return a * b; } 50 int Multiply(int a, int b) { return a * b; }
32 51
33 } // namespace 52 } // namespace
34 53
54 // Try to catch any problem with scoped_refptr type deduction in rtc::Bind at
55 // compile time.
56 #define EXPECT_IS_CAPTURED_AS_PTR(T) \
57 static_assert(is_same<detail::PointerType<T>::type, T*>::value, \
58 "PointerType")
59 #define EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(T) \
60 static_assert( \
61 is_same<detail::PointerType<T>::type, scoped_refptr<T>>::value, \
62 "PointerType")
63
64 EXPECT_IS_CAPTURED_AS_PTR(void);
65 EXPECT_IS_CAPTURED_AS_PTR(int);
66 EXPECT_IS_CAPTURED_AS_PTR(double);
67 EXPECT_IS_CAPTURED_AS_PTR(A);
68 EXPECT_IS_CAPTURED_AS_PTR(RefCountInterface*);
69
70 EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(RefCountInterface);
71 EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(B);
72 EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(C);
73 EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(RefCountedObject<RefCountInterface>);
74 EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(RefCountedObject<B>);
75 EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(RefCountedObject<C>);
76
35 TEST(BindTest, BindToMethod) { 77 TEST(BindTest, BindToMethod) {
36 MethodBindTester object = {0}; 78 MethodBindTester object = {0};
37 EXPECT_EQ(0, object.call_count); 79 EXPECT_EQ(0, object.call_count);
38 Bind(&MethodBindTester::NullaryVoid, &object)(); 80 Bind(&MethodBindTester::NullaryVoid, &object)();
39 EXPECT_EQ(1, object.call_count); 81 EXPECT_EQ(1, object.call_count);
40 EXPECT_EQ(1, Bind(&MethodBindTester::NullaryInt, &object)()); 82 EXPECT_EQ(1, Bind(&MethodBindTester::NullaryInt, &object)());
41 EXPECT_EQ(2, object.call_count); 83 EXPECT_EQ(2, object.call_count);
42 EXPECT_EQ(2, Bind(&MethodBindTester::NullaryConst, 84 EXPECT_EQ(2, Bind(&MethodBindTester::NullaryConst,
43 static_cast<const MethodBindTester*>(&object))()); 85 static_cast<const MethodBindTester*>(&object))());
44 EXPECT_EQ(3, object.call_count); 86 EXPECT_EQ(3, object.call_count);
(...skipping 12 matching lines...) Expand all
57 EXPECT_EQ(56, Bind(&MethodBindTester::Multiply, &object, 7, 8)()); 99 EXPECT_EQ(56, Bind(&MethodBindTester::Multiply, &object, 7, 8)());
58 EXPECT_EQ(8, object.call_count); 100 EXPECT_EQ(8, object.call_count);
59 } 101 }
60 102
61 TEST(BindTest, BindToFunction) { 103 TEST(BindTest, BindToFunction) {
62 EXPECT_EQ(42, Bind(&Return42)()); 104 EXPECT_EQ(42, Bind(&Return42)());
63 EXPECT_EQ(3, Bind(&Negate, -3)()); 105 EXPECT_EQ(3, Bind(&Negate, -3)());
64 EXPECT_EQ(56, Bind(&Multiply, 8, 7)()); 106 EXPECT_EQ(56, Bind(&Multiply, 8, 7)());
65 } 107 }
66 108
109 // Test Bind where method object implements RefCountInterface and is passed as a
110 // pointer.
111 TEST(BindTest, CapturePointerAsScopedRefPtr) {
112 bool objectHasDied = false;
113 scoped_refptr<LifeTimeCheck> object =
114 new RefCountedObject<LifeTimeCheck>(&objectHasDied);
115 {
116 MethodFunctor0<LifeTimeCheck, void (LifeTimeCheck::*)(), void> functor =
117 Bind(&LifeTimeCheck::PrepareToDie, object.get());
118 object = nullptr;
119 EXPECT_FALSE(objectHasDied);
120 // Run prepare to die via functor.
121 functor();
122 }
123 EXPECT_TRUE(objectHasDied);
124 }
125
126 // Test Bind where method object implements RefCountInterface and is passed as a
127 // scoped_refptr<>.
128 TEST(BindTest, CaptureScopedRefPtrAsScopedRefPtr) {
129 bool objectHasDied = false;
130 scoped_refptr<LifeTimeCheck> object =
131 new RefCountedObject<LifeTimeCheck>(&objectHasDied);
132 {
133 MethodFunctor0<LifeTimeCheck, void (LifeTimeCheck::*)(), void> functor =
134 Bind(&LifeTimeCheck::PrepareToDie, object);
135 object = nullptr;
136 EXPECT_FALSE(objectHasDied);
137 // Run prepare to die via functor.
138 functor();
139 }
140 EXPECT_TRUE(objectHasDied);
141 }
142
143 // Test Bind where method object is captured as scoped_refptr<> and the functor
144 // dies while there are references left.
145 TEST(BindTest, FunctorReleasesObjectOnDestruction) {
146 bool objectHasDied = false;
147 scoped_refptr<LifeTimeCheck> object =
148 new RefCountedObject<LifeTimeCheck>(&objectHasDied);
149 Bind(&LifeTimeCheck::NullaryVoid, object.get())();
150 EXPECT_FALSE(objectHasDied);
151 object->PrepareToDie();
152 object = nullptr;
153 EXPECT_TRUE(objectHasDied);
154 }
155
67 } // namespace rtc 156 } // namespace rtc
OLDNEW
« webrtc/base/bind.h ('K') | « webrtc/base/bind.h.pump ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698