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

Unified Diff: webrtc/base/scoped_ref_ptr.h

Issue 2084893002: Implement scoped_const_refptr template. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 6 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « webrtc/base/bind_unittest.cc ('k') | webrtc/common_video/include/video_frame_buffer.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/base/scoped_ref_ptr.h
diff --git a/webrtc/base/scoped_ref_ptr.h b/webrtc/base/scoped_ref_ptr.h
index ceee445216a3ce6af300c7830f19f528049e4c2c..c628561ca1e1f584b1132b63937f72bc4064afb3 100644
--- a/webrtc/base/scoped_ref_ptr.h
+++ b/webrtc/base/scoped_ref_ptr.h
@@ -68,54 +68,59 @@
namespace rtc {
template <class T>
-class scoped_refptr {
+class scoped_const_refptr {
public:
- scoped_refptr() : ptr_(NULL) {
- }
+ scoped_const_refptr() : ptr_(NULL) {}
- scoped_refptr(T* p) : ptr_(p) {
+ scoped_const_refptr(T* p) : ptr_(p) {
if (ptr_)
ptr_->AddRef();
}
- scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) {
+ scoped_const_refptr(const scoped_const_refptr<T>& r) : ptr_(r.ptr_) {
if (ptr_)
ptr_->AddRef();
}
template <typename U>
- scoped_refptr(const scoped_refptr<U>& r) : ptr_(r.get()) {
+ scoped_const_refptr(const scoped_const_refptr<U>& r) : ptr_(r.get()) {
if (ptr_)
ptr_->AddRef();
}
// Move constructors.
- scoped_refptr(scoped_refptr<T>&& r) : ptr_(r.release()) {}
+ scoped_const_refptr(scoped_const_refptr<T>&& r) : ptr_(r.release()) {}
template <typename U>
- scoped_refptr(scoped_refptr<U>&& r) : ptr_(r.release()) {}
+ scoped_const_refptr(scoped_const_refptr<U>&& r) : ptr_(r.release()) {}
- ~scoped_refptr() {
+ ~scoped_const_refptr() {
if (ptr_)
ptr_->Release();
}
- T* get() const { return ptr_; }
- operator T*() const { return ptr_; }
- T* operator->() const { return ptr_; }
+ const T* get() const { return ptr_; }
+#if 0
+ // TODO(nisse): If enabled, we get errors like "conversion from
+ // 'const rtc::scoped_refptr<webrtc::VideoFrameBuffer>' to 'bool' is
+ // ambiguous" because it collides with the * operator in the
+ // subclass.
+ operator const T*() const { return ptr_; }
+#endif
+ const T* operator->() const { return ptr_; }
// Release a pointer.
// The return value is the current pointer held by this object.
// If this object holds a NULL pointer, the return value is NULL.
// After this operation, this object will hold a NULL pointer,
// and will not own the object any more.
- T* release() {
- T* retVal = ptr_;
+ const T* release() {
+ const T* retVal = ptr_;
ptr_ = NULL;
return retVal;
}
- scoped_refptr<T>& operator=(T* p) {
+ scoped_const_refptr<T>& operator=(T* p) {
// AddRef first so that self assignment should work
if (p)
p->AddRef();
@@ -125,27 +130,80 @@ class scoped_refptr {
return *this;
}
- scoped_refptr<T>& operator=(const scoped_refptr<T>& r) {
+ scoped_const_refptr<T>& operator=(const scoped_const_refptr<T>& r) {
return *this = r.ptr_;
}
template <typename U>
- scoped_refptr<T>& operator=(const scoped_refptr<U>& r) {
+ scoped_const_refptr<T>& operator=(const scoped_const_refptr<U>& r) {
return *this = r.get();
}
- void swap(T** pp) {
- T* p = ptr_;
+ void swap(const T** pp) {
+ const T* p = ptr_;
ptr_ = *pp;
*pp = p;
}
- void swap(scoped_refptr<T>& r) {
+ void swap(scoped_const_refptr<T>& r) {
swap(&r.ptr_);
}
protected:
- T* ptr_;
+ const T* ptr_;
+};
+
+template <class T>
+class scoped_refptr : public scoped_const_refptr<T> {
+ public:
+ scoped_refptr() : scoped_const_refptr<T>() {}
+ scoped_refptr(T* p) : scoped_const_refptr<T>(p) {}
+
+ scoped_refptr(const scoped_refptr<T>& r) : scoped_refptr<T>(r.get()) {}
+
+ template <typename U>
+ scoped_refptr(const scoped_refptr<U>& r) : scoped_refptr<T>(r.get()) {}
+
+ T* get() const { return const_cast<T*>(scoped_const_refptr<T>::ptr_); }
+ operator T*() const { return get(); }
+ T* operator->() const { return get(); }
+
+ T* release() {
+ T* retVal = get();
+ scoped_const_refptr<T>::ptr_ = NULL;
+ return retVal;
+ }
+
+ scoped_refptr<T>& operator=(T* p) {
+ // AddRef first so that self assignment should work
+ if (p)
+ p->AddRef();
+ if (scoped_const_refptr<T>::ptr_)
+ scoped_const_refptr<T>::ptr_->Release();
+ set(p);
+ return *this;
+ }
+
+ scoped_refptr<T>& operator=(const scoped_refptr<T>& r) {
+ return *this = r.get();
+ }
+ template <typename U>
+ scoped_refptr<T>& operator=(const scoped_refptr<U>& r) {
+ return *this = r.get();
+ }
+ void swap(T** pp) {
+ const T* p = scoped_const_refptr<T>::ptr_;
+ set(*pp);
+ *pp = const_cast<T*>(p);
+ }
+
+ void swap(scoped_refptr<T>& r) {
+ swap(const_cast<T**>(&r.ptr_));
+ }
+ private:
+ void set(T* p) {
+ scoped_const_refptr<T>::ptr_ = const_cast<T*>(p);
+ }
};
} // namespace rtc
« no previous file with comments | « webrtc/base/bind_unittest.cc ('k') | webrtc/common_video/include/video_frame_buffer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698