OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2011 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 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 // | 61 // |
62 | 62 |
63 #ifndef WEBRTC_BASE_SCOPED_REF_PTR_H_ | 63 #ifndef WEBRTC_BASE_SCOPED_REF_PTR_H_ |
64 #define WEBRTC_BASE_SCOPED_REF_PTR_H_ | 64 #define WEBRTC_BASE_SCOPED_REF_PTR_H_ |
65 | 65 |
66 #include <stddef.h> | 66 #include <stddef.h> |
67 | 67 |
68 namespace rtc { | 68 namespace rtc { |
69 | 69 |
70 template <class T> | 70 template <class T> |
71 class scoped_refptr { | 71 class scoped_const_refptr { |
72 public: | 72 public: |
73 scoped_refptr() : ptr_(NULL) { | 73 scoped_const_refptr() : ptr_(NULL) {} |
74 } | |
75 | 74 |
76 scoped_refptr(T* p) : ptr_(p) { | 75 scoped_const_refptr(T* p) : ptr_(p) { |
77 if (ptr_) | 76 if (ptr_) |
78 ptr_->AddRef(); | 77 ptr_->AddRef(); |
79 } | 78 } |
80 | 79 |
81 scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) { | 80 scoped_const_refptr(const scoped_const_refptr<T>& r) : ptr_(r.ptr_) { |
82 if (ptr_) | 81 if (ptr_) |
83 ptr_->AddRef(); | 82 ptr_->AddRef(); |
84 } | 83 } |
85 | 84 |
86 template <typename U> | 85 template <typename U> |
87 scoped_refptr(const scoped_refptr<U>& r) : ptr_(r.get()) { | 86 scoped_const_refptr(const scoped_const_refptr<U>& r) : ptr_(r.get()) { |
88 if (ptr_) | 87 if (ptr_) |
89 ptr_->AddRef(); | 88 ptr_->AddRef(); |
90 } | 89 } |
91 | 90 |
92 // Move constructors. | 91 // Move constructors. |
93 scoped_refptr(scoped_refptr<T>&& r) : ptr_(r.release()) {} | 92 scoped_const_refptr(scoped_const_refptr<T>&& r) : ptr_(r.release()) {} |
94 | 93 |
95 template <typename U> | 94 template <typename U> |
96 scoped_refptr(scoped_refptr<U>&& r) : ptr_(r.release()) {} | 95 scoped_const_refptr(scoped_const_refptr<U>&& r) : ptr_(r.release()) {} |
97 | 96 |
98 ~scoped_refptr() { | 97 ~scoped_const_refptr() { |
99 if (ptr_) | 98 if (ptr_) |
100 ptr_->Release(); | 99 ptr_->Release(); |
101 } | 100 } |
102 | 101 |
103 T* get() const { return ptr_; } | 102 const T* get() const { return ptr_; } |
104 operator T*() const { return ptr_; } | 103 #if 0 |
105 T* operator->() const { return ptr_; } | 104 // TODO(nisse): If enabled, we get errors like "conversion from |
| 105 // 'const rtc::scoped_refptr<webrtc::VideoFrameBuffer>' to 'bool' is |
| 106 // ambiguous" because it collides with the * operator in the |
| 107 // subclass. |
| 108 operator const T*() const { return ptr_; } |
| 109 #endif |
| 110 const T* operator->() const { return ptr_; } |
106 | 111 |
107 // Release a pointer. | 112 // Release a pointer. |
108 // The return value is the current pointer held by this object. | 113 // The return value is the current pointer held by this object. |
109 // If this object holds a NULL pointer, the return value is NULL. | 114 // If this object holds a NULL pointer, the return value is NULL. |
110 // After this operation, this object will hold a NULL pointer, | 115 // After this operation, this object will hold a NULL pointer, |
111 // and will not own the object any more. | 116 // and will not own the object any more. |
112 T* release() { | 117 const T* release() { |
113 T* retVal = ptr_; | 118 const T* retVal = ptr_; |
114 ptr_ = NULL; | 119 ptr_ = NULL; |
115 return retVal; | 120 return retVal; |
116 } | 121 } |
117 | 122 |
118 scoped_refptr<T>& operator=(T* p) { | 123 scoped_const_refptr<T>& operator=(T* p) { |
119 // AddRef first so that self assignment should work | 124 // AddRef first so that self assignment should work |
120 if (p) | 125 if (p) |
121 p->AddRef(); | 126 p->AddRef(); |
122 if (ptr_ ) | 127 if (ptr_ ) |
123 ptr_ ->Release(); | 128 ptr_ ->Release(); |
124 ptr_ = p; | 129 ptr_ = p; |
125 return *this; | 130 return *this; |
126 } | 131 } |
127 | 132 |
| 133 scoped_const_refptr<T>& operator=(const scoped_const_refptr<T>& r) { |
| 134 return *this = r.ptr_; |
| 135 } |
| 136 |
| 137 template <typename U> |
| 138 scoped_const_refptr<T>& operator=(const scoped_const_refptr<U>& r) { |
| 139 return *this = r.get(); |
| 140 } |
| 141 |
| 142 void swap(const T** pp) { |
| 143 const T* p = ptr_; |
| 144 ptr_ = *pp; |
| 145 *pp = p; |
| 146 } |
| 147 |
| 148 void swap(scoped_const_refptr<T>& r) { |
| 149 swap(&r.ptr_); |
| 150 } |
| 151 |
| 152 protected: |
| 153 const T* ptr_; |
| 154 }; |
| 155 |
| 156 template <class T> |
| 157 class scoped_refptr : public scoped_const_refptr<T> { |
| 158 public: |
| 159 scoped_refptr() : scoped_const_refptr<T>() {} |
| 160 scoped_refptr(T* p) : scoped_const_refptr<T>(p) {} |
| 161 |
| 162 scoped_refptr(const scoped_refptr<T>& r) : scoped_refptr<T>(r.get()) {} |
| 163 |
| 164 template <typename U> |
| 165 scoped_refptr(const scoped_refptr<U>& r) : scoped_refptr<T>(r.get()) {} |
| 166 |
| 167 T* get() const { return const_cast<T*>(scoped_const_refptr<T>::ptr_); } |
| 168 operator T*() const { return get(); } |
| 169 T* operator->() const { return get(); } |
| 170 |
| 171 T* release() { |
| 172 T* retVal = get(); |
| 173 scoped_const_refptr<T>::ptr_ = NULL; |
| 174 return retVal; |
| 175 } |
| 176 |
| 177 scoped_refptr<T>& operator=(T* p) { |
| 178 // AddRef first so that self assignment should work |
| 179 if (p) |
| 180 p->AddRef(); |
| 181 if (scoped_const_refptr<T>::ptr_) |
| 182 scoped_const_refptr<T>::ptr_->Release(); |
| 183 set(p); |
| 184 return *this; |
| 185 } |
| 186 |
128 scoped_refptr<T>& operator=(const scoped_refptr<T>& r) { | 187 scoped_refptr<T>& operator=(const scoped_refptr<T>& r) { |
129 return *this = r.ptr_; | 188 return *this = r.get(); |
130 } | 189 } |
131 | |
132 template <typename U> | 190 template <typename U> |
133 scoped_refptr<T>& operator=(const scoped_refptr<U>& r) { | 191 scoped_refptr<T>& operator=(const scoped_refptr<U>& r) { |
134 return *this = r.get(); | 192 return *this = r.get(); |
135 } | 193 } |
136 | |
137 void swap(T** pp) { | 194 void swap(T** pp) { |
138 T* p = ptr_; | 195 const T* p = scoped_const_refptr<T>::ptr_; |
139 ptr_ = *pp; | 196 set(*pp); |
140 *pp = p; | 197 *pp = const_cast<T*>(p); |
141 } | 198 } |
142 | 199 |
143 void swap(scoped_refptr<T>& r) { | 200 void swap(scoped_refptr<T>& r) { |
144 swap(&r.ptr_); | 201 swap(const_cast<T**>(&r.ptr_)); |
145 } | 202 } |
146 | 203 private: |
147 protected: | 204 void set(T* p) { |
148 T* ptr_; | 205 scoped_const_refptr<T>::ptr_ = const_cast<T*>(p); |
| 206 } |
149 }; | 207 }; |
150 | 208 |
151 } // namespace rtc | 209 } // namespace rtc |
152 | 210 |
153 #endif // WEBRTC_BASE_SCOPED_REF_PTR_H_ | 211 #endif // WEBRTC_BASE_SCOPED_REF_PTR_H_ |
OLD | NEW |