michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef BASE_REF_COUNTED_H_ michael@0: #define BASE_REF_COUNTED_H_ michael@0: michael@0: #include "base/atomic_ref_count.h" michael@0: #include "base/thread_collision_warner.h" michael@0: michael@0: namespace base { michael@0: michael@0: namespace subtle { michael@0: michael@0: class RefCountedBase { michael@0: protected: michael@0: RefCountedBase(); michael@0: ~RefCountedBase(); michael@0: michael@0: void AddRef(); michael@0: michael@0: // Returns true if the object should self-delete. michael@0: bool Release(); michael@0: michael@0: private: michael@0: int ref_count_; michael@0: #ifndef NDEBUG michael@0: bool in_dtor_; michael@0: #endif michael@0: michael@0: DFAKE_MUTEX(add_release_) michael@0: michael@0: DISALLOW_COPY_AND_ASSIGN(RefCountedBase); michael@0: }; michael@0: michael@0: class RefCountedThreadSafeBase { michael@0: protected: michael@0: RefCountedThreadSafeBase(); michael@0: ~RefCountedThreadSafeBase(); michael@0: michael@0: void AddRef(); michael@0: michael@0: // Returns true if the object should self-delete. michael@0: bool Release(); michael@0: michael@0: private: michael@0: AtomicRefCount ref_count_; michael@0: #ifndef NDEBUG michael@0: bool in_dtor_; michael@0: #endif michael@0: michael@0: DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafeBase); michael@0: }; michael@0: michael@0: michael@0: michael@0: } // namespace subtle michael@0: michael@0: // michael@0: // A base class for reference counted classes. Otherwise, known as a cheap michael@0: // knock-off of WebKit's RefCounted class. To use this guy just extend your michael@0: // class from it like so: michael@0: // michael@0: // class MyFoo : public base::RefCounted { michael@0: // ... michael@0: // }; michael@0: // michael@0: template michael@0: class RefCounted : public subtle::RefCountedBase { michael@0: public: michael@0: RefCounted() { } michael@0: ~RefCounted() { } michael@0: michael@0: void AddRef() { michael@0: subtle::RefCountedBase::AddRef(); michael@0: } michael@0: michael@0: void Release() { michael@0: if (subtle::RefCountedBase::Release()) { michael@0: delete static_cast(this); michael@0: } michael@0: } michael@0: michael@0: private: michael@0: DISALLOW_COPY_AND_ASSIGN(RefCounted); michael@0: }; michael@0: michael@0: // michael@0: // A thread-safe variant of RefCounted michael@0: // michael@0: // class MyFoo : public base::RefCountedThreadSafe { michael@0: // ... michael@0: // }; michael@0: // michael@0: template michael@0: class RefCountedThreadSafe : public subtle::RefCountedThreadSafeBase { michael@0: public: michael@0: RefCountedThreadSafe() { } michael@0: ~RefCountedThreadSafe() { } michael@0: michael@0: void AddRef() { michael@0: subtle::RefCountedThreadSafeBase::AddRef(); michael@0: } michael@0: michael@0: void Release() { michael@0: if (subtle::RefCountedThreadSafeBase::Release()) { michael@0: delete static_cast(this); michael@0: } michael@0: } michael@0: michael@0: private: michael@0: DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafe); michael@0: }; michael@0: michael@0: // michael@0: // A wrapper for some piece of data so we can place other things in michael@0: // scoped_refptrs<>. michael@0: // michael@0: template michael@0: class RefCountedData : public base::RefCounted< base::RefCountedData > { michael@0: public: michael@0: RefCountedData() : data() {} michael@0: RefCountedData(const T& in_value) : data(in_value) {} michael@0: michael@0: T data; michael@0: }; michael@0: michael@0: } // namespace base michael@0: michael@0: // michael@0: // A smart pointer class for reference counted objects. Use this class instead michael@0: // of calling AddRef and Release manually on a reference counted object to michael@0: // avoid common memory leaks caused by forgetting to Release an object michael@0: // reference. Sample usage: michael@0: // michael@0: // class MyFoo : public RefCounted { michael@0: // ... michael@0: // }; michael@0: // michael@0: // void some_function() { michael@0: // scoped_refptr foo = new MyFoo(); michael@0: // foo->Method(param); michael@0: // // |foo| is released when this function returns michael@0: // } michael@0: // michael@0: // void some_other_function() { michael@0: // scoped_refptr foo = new MyFoo(); michael@0: // ... michael@0: // foo = NULL; // explicitly releases |foo| michael@0: // ... michael@0: // if (foo) michael@0: // foo->Method(param); michael@0: // } michael@0: // michael@0: // The above examples show how scoped_refptr acts like a pointer to T. michael@0: // Given two scoped_refptr classes, it is also possible to exchange michael@0: // references between the two objects, like so: michael@0: // michael@0: // { michael@0: // scoped_refptr a = new MyFoo(); michael@0: // scoped_refptr b; michael@0: // michael@0: // b.swap(a); michael@0: // // now, |b| references the MyFoo object, and |a| references NULL. michael@0: // } michael@0: // michael@0: // To make both |a| and |b| in the above example reference the same MyFoo michael@0: // object, simply use the assignment operator: michael@0: // michael@0: // { michael@0: // scoped_refptr a = new MyFoo(); michael@0: // scoped_refptr b; michael@0: // michael@0: // b = a; michael@0: // // now, |a| and |b| each own a reference to the same MyFoo object. michael@0: // } michael@0: // michael@0: template michael@0: class scoped_refptr { michael@0: public: michael@0: scoped_refptr() : ptr_(NULL) { michael@0: } michael@0: michael@0: scoped_refptr(T* p) : ptr_(p) { michael@0: if (ptr_) michael@0: ptr_->AddRef(); michael@0: } michael@0: michael@0: scoped_refptr(const scoped_refptr& r) : ptr_(r.ptr_) { michael@0: if (ptr_) michael@0: ptr_->AddRef(); michael@0: } michael@0: michael@0: ~scoped_refptr() { michael@0: if (ptr_) michael@0: ptr_->Release(); michael@0: } michael@0: michael@0: T* get() const { return ptr_; } michael@0: operator T*() const { return ptr_; } michael@0: T* operator->() const { return ptr_; } michael@0: michael@0: scoped_refptr& operator=(T* p) { michael@0: // AddRef first so that self assignment should work michael@0: if (p) michael@0: p->AddRef(); michael@0: if (ptr_ ) michael@0: ptr_ ->Release(); michael@0: ptr_ = p; michael@0: return *this; michael@0: } michael@0: michael@0: scoped_refptr& operator=(const scoped_refptr& r) { michael@0: return *this = r.ptr_; michael@0: } michael@0: michael@0: void swap(T** pp) { michael@0: T* p = ptr_; michael@0: ptr_ = *pp; michael@0: *pp = p; michael@0: } michael@0: michael@0: void swap(scoped_refptr& r) { michael@0: swap(&r.ptr_); michael@0: } michael@0: michael@0: protected: michael@0: T* ptr_; michael@0: }; michael@0: michael@0: #endif // BASE_REF_COUNTED_H_