RefCountInterface: Make AddRef() and Release() const
This CL makes AddRef() and Release() const member methods and the refcount integer mutable. This is reasonable, because they only manage the lifetime of the object, and this is also how it's done in Chromium.
The purpose is to be able to capture a const pointer in a scoped_refptr, which is currenty impossible. The practial problem this CL solves is this:
void Foo::Bar() const {}
rtc::Callback0<void> Foo::MakeClosure() const {
return rtc::Bind(&Foo::Bar, this);
}
We currently capture |this| as const Foo*. With this CL, |this| will be captured as scoped_refptr<const Foo>.
A test is also added in bind_unittest to check this behaviour.
BUG=webrtc:5065
R=perkj@webrtc.org, tommi@webrtc.org
Review URL: https://codereview.webrtc.org/1403683004 .
Cr-Commit-Position: refs/heads/master@{#10253}
diff --git a/webrtc/base/bind_unittest.cc b/webrtc/base/bind_unittest.cc
index d38729d..fa47d27 100644
--- a/webrtc/base/bind_unittest.cc
+++ b/webrtc/base/bind_unittest.cc
@@ -108,6 +108,7 @@
EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(RefCountedObject<RefCountInterface>);
EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(RefCountedObject<B>);
EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(RefCountedObject<C>);
+EXPECT_IS_CAPTURED_AS_SCOPED_REFPTR(const RefCountedObject<RefCountInterface>);
TEST(BindTest, BindToMethod) {
MethodBindTester object = {0};
diff --git a/webrtc/base/refcount.h b/webrtc/base/refcount.h
index 49f3777..dbad2eb 100644
--- a/webrtc/base/refcount.h
+++ b/webrtc/base/refcount.h
@@ -20,8 +20,8 @@
// Reference count interface.
class RefCountInterface {
public:
- virtual int AddRef() = 0;
- virtual int Release() = 0;
+ virtual int AddRef() const = 0;
+ virtual int Release() const = 0;
protected:
virtual ~RefCountInterface() {}
};
@@ -95,11 +95,11 @@
: T(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11), ref_count_(0) {
}
- virtual int AddRef() {
+ int AddRef() const override {
return AtomicOps::Increment(&ref_count_);
}
- virtual int Release() {
+ int Release() const override {
int count = AtomicOps::Decrement(&ref_count_);
if (!count) {
delete this;
@@ -121,7 +121,7 @@
virtual ~RefCountedObject() {
}
- volatile int ref_count_;
+ mutable volatile int ref_count_;
};
} // namespace rtc