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

Side by Side Diff: base/memory/ref_counted.h

Issue 623633002: scoped_refptr: support opaque pointed-to classes via extern template (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix header guard Created 6 years, 2 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
« no previous file with comments | « base/base.gyp ('k') | base/memory/ref_counted_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_MEMORY_REF_COUNTED_H_ 5 #ifndef BASE_MEMORY_REF_COUNTED_H_
6 #define BASE_MEMORY_REF_COUNTED_H_ 6 #define BASE_MEMORY_REF_COUNTED_H_
7 7
8 #include <cassert> 8 #include <cassert>
9 #include <iosfwd> 9 #include <iosfwd>
10 10
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 template <class T> 269 template <class T>
270 class scoped_refptr { 270 class scoped_refptr {
271 public: 271 public:
272 typedef T element_type; 272 typedef T element_type;
273 273
274 scoped_refptr() : ptr_(NULL) { 274 scoped_refptr() : ptr_(NULL) {
275 } 275 }
276 276
277 scoped_refptr(T* p) : ptr_(p) { 277 scoped_refptr(T* p) : ptr_(p) {
278 if (ptr_) 278 if (ptr_)
279 ptr_->AddRef(); 279 AddRef(ptr_);
280 } 280 }
281 281
282 scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) { 282 scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) {
283 if (ptr_) 283 if (ptr_)
284 ptr_->AddRef(); 284 AddRef(ptr_);
285 } 285 }
286 286
287 template <typename U> 287 template <typename U>
288 scoped_refptr(const scoped_refptr<U>& r) : ptr_(r.get()) { 288 scoped_refptr(const scoped_refptr<U>& r) : ptr_(r.get()) {
289 if (ptr_) 289 if (ptr_)
290 ptr_->AddRef(); 290 AddRef(ptr_);
291 } 291 }
292 292
293 ~scoped_refptr() { 293 ~scoped_refptr() {
294 if (ptr_) 294 if (ptr_)
295 ptr_->Release(); 295 Release(ptr_);
296 } 296 }
297 297
298 T* get() const { return ptr_; } 298 T* get() const { return ptr_; }
299 299
300 #if !defined(DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR) 300 #if !defined(DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR)
301 // Allow scoped_refptr<C> to be used in boolean expression 301 // Allow scoped_refptr<C> to be used in boolean expression
302 // and comparison operations. 302 // and comparison operations.
303 operator T*() const { return ptr_; } 303 operator T*() const { return ptr_; }
304 #endif 304 #endif
305 305
306 T& operator*() const { 306 T& operator*() const {
307 assert(ptr_ != NULL); 307 assert(ptr_ != NULL);
308 return *ptr_; 308 return *ptr_;
309 } 309 }
310 310
311 T* operator->() const { 311 T* operator->() const {
312 assert(ptr_ != NULL); 312 assert(ptr_ != NULL);
313 return ptr_; 313 return ptr_;
314 } 314 }
315 315
316 scoped_refptr<T>& operator=(T* p) { 316 scoped_refptr<T>& operator=(T* p) {
317 // AddRef first so that self assignment should work 317 // AddRef first so that self assignment should work
318 if (p) 318 if (p)
319 p->AddRef(); 319 AddRef(p);
320 T* old_ptr = ptr_; 320 T* old_ptr = ptr_;
321 ptr_ = p; 321 ptr_ = p;
322 if (old_ptr) 322 if (old_ptr)
323 old_ptr->Release(); 323 Release(old_ptr);
324 return *this; 324 return *this;
325 } 325 }
326 326
327 scoped_refptr<T>& operator=(const scoped_refptr<T>& r) { 327 scoped_refptr<T>& operator=(const scoped_refptr<T>& r) {
328 return *this = r.ptr_; 328 return *this = r.ptr_;
329 } 329 }
330 330
331 template <typename U> 331 template <typename U>
332 scoped_refptr<T>& operator=(const scoped_refptr<U>& r) { 332 scoped_refptr<T>& operator=(const scoped_refptr<U>& r) {
333 return *this = r.get(); 333 return *this = r.get();
(...skipping 21 matching lines...) Expand all
355 } 355 }
356 356
357 template <typename U> 357 template <typename U>
358 bool operator<(const scoped_refptr<U>& rhs) const { 358 bool operator<(const scoped_refptr<U>& rhs) const {
359 return ptr_ < rhs.get(); 359 return ptr_ < rhs.get();
360 } 360 }
361 #endif 361 #endif
362 362
363 protected: 363 protected:
364 T* ptr_; 364 T* ptr_;
365
366 private:
367 // Non-inline helpers to allow:
368 // class Opaque;
369 // extern template class scoped_refptr<Opaque>;
370 // Otherwise the compiler will complain that Opaque is an incomplete type.
371 static void AddRef(T* ptr);
372 static void Release(T* ptr);
365 }; 373 };
366 374
375 template <typename T>
376 void scoped_refptr<T>::AddRef(T* ptr) {
377 ptr->AddRef();
378 }
379
380 template <typename T>
381 void scoped_refptr<T>::Release(T* ptr) {
382 ptr->Release();
383 }
384
367 // Handy utility for creating a scoped_refptr<T> out of a T* explicitly without 385 // Handy utility for creating a scoped_refptr<T> out of a T* explicitly without
368 // having to retype all the template arguments 386 // having to retype all the template arguments
369 template <typename T> 387 template <typename T>
370 scoped_refptr<T> make_scoped_refptr(T* t) { 388 scoped_refptr<T> make_scoped_refptr(T* t) {
371 return scoped_refptr<T>(t); 389 return scoped_refptr<T>(t);
372 } 390 }
373 391
374 #if defined(DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR) 392 #if defined(DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR)
375 // Temporary operator overloads to facilitate the transition... 393 // Temporary operator overloads to facilitate the transition...
376 template <typename T, typename U> 394 template <typename T, typename U>
(...skipping 16 matching lines...) Expand all
393 return !operator==(lhs, rhs); 411 return !operator==(lhs, rhs);
394 } 412 }
395 413
396 template <typename T> 414 template <typename T>
397 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { 415 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) {
398 return out << p.get(); 416 return out << p.get();
399 } 417 }
400 #endif // defined(DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR) 418 #endif // defined(DISABLE_SCOPED_REFPTR_CONVERSION_OPERATOR)
401 419
402 #endif // BASE_MEMORY_REF_COUNTED_H_ 420 #endif // BASE_MEMORY_REF_COUNTED_H_
OLDNEW
« no previous file with comments | « base/base.gyp ('k') | base/memory/ref_counted_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698