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: // A "smart" pointer type with reference tracking. Every pointer to a michael@0: // particular object is kept on a circular linked list. When the last pointer michael@0: // to an object is destroyed or reassigned, the object is deleted. michael@0: // michael@0: // Used properly, this deletes the object when the last reference goes away. michael@0: // There are several caveats: michael@0: // - Like all reference counting schemes, cycles lead to leaks. michael@0: // - Each smart pointer is actually two pointers (8 bytes instead of 4). michael@0: // - Every time a pointer is released, the entire list of pointers to that michael@0: // object is traversed. This class is therefore NOT SUITABLE when there michael@0: // will often be more than two or three pointers to a particular object. michael@0: // - References are only tracked as long as linked_ptr<> objects are copied. michael@0: // If a linked_ptr<> is converted to a raw pointer and back, BAD THINGS michael@0: // will happen (double deletion). michael@0: // michael@0: // A good use of this class is storing object references in STL containers. michael@0: // You can safely put linked_ptr<> in a vector<>. michael@0: // Other uses may not be as good. michael@0: // michael@0: // Note: If you use an incomplete type with linked_ptr<>, the class michael@0: // *containing* linked_ptr<> must have a constructor and destructor (even michael@0: // if they do nothing!). michael@0: // michael@0: // Thread Safety: michael@0: // A linked_ptr is NOT thread safe. Copying a linked_ptr object is michael@0: // effectively a read-write operation. michael@0: // michael@0: // Alternative: to linked_ptr is shared_ptr, which michael@0: // - is also two pointers in size (8 bytes for 32 bit addresses) michael@0: // - is thread safe for copying and deletion michael@0: // - supports weak_ptrs michael@0: michael@0: #ifndef BASE_LINKED_PTR_H_ michael@0: #define BASE_LINKED_PTR_H_ michael@0: michael@0: #include "base/logging.h" // for CHECK macros michael@0: michael@0: // This is used internally by all instances of linked_ptr<>. It needs to be michael@0: // a non-template class because different types of linked_ptr<> can refer to michael@0: // the same object (linked_ptr(obj) vs linked_ptr(obj)). michael@0: // So, it needs to be possible for different types of linked_ptr to participate michael@0: // in the same circular linked list, so we need a single class type here. michael@0: // michael@0: // DO NOT USE THIS CLASS DIRECTLY YOURSELF. Use linked_ptr. michael@0: class linked_ptr_internal { michael@0: public: michael@0: // Create a new circle that includes only this instance. michael@0: void join_new() { michael@0: next_ = this; michael@0: } michael@0: michael@0: // Join an existing circle. michael@0: void join(linked_ptr_internal const* ptr) { michael@0: next_ = ptr->next_; michael@0: ptr->next_ = this; michael@0: } michael@0: michael@0: // Leave whatever circle we're part of. Returns true iff we were the michael@0: // last member of the circle. Once this is done, you can join() another. michael@0: bool depart() { michael@0: if (next_ == this) return true; michael@0: linked_ptr_internal const* p = next_; michael@0: while (p->next_ != this) p = p->next_; michael@0: p->next_ = next_; michael@0: return false; michael@0: } michael@0: michael@0: private: michael@0: mutable linked_ptr_internal const* next_; michael@0: }; michael@0: michael@0: template michael@0: class linked_ptr { michael@0: public: michael@0: typedef T element_type; michael@0: michael@0: // Take over ownership of a raw pointer. This should happen as soon as michael@0: // possible after the object is created. michael@0: explicit linked_ptr(T* ptr = NULL) { capture(ptr); } michael@0: ~linked_ptr() { depart(); } michael@0: michael@0: // Copy an existing linked_ptr<>, adding ourselves to the list of references. michael@0: template linked_ptr(linked_ptr const& ptr) { copy(&ptr); } michael@0: linked_ptr(linked_ptr const& ptr) { DCHECK_NE(&ptr, this); copy(&ptr); } michael@0: michael@0: // Assignment releases the old value and acquires the new. michael@0: template linked_ptr& operator=(linked_ptr const& ptr) { michael@0: depart(); michael@0: copy(&ptr); michael@0: return *this; michael@0: } michael@0: michael@0: linked_ptr& operator=(linked_ptr const& ptr) { michael@0: if (&ptr != this) { michael@0: depart(); michael@0: copy(&ptr); michael@0: } michael@0: return *this; michael@0: } michael@0: michael@0: // Smart pointer members. michael@0: void reset(T* ptr = NULL) { depart(); capture(ptr); } michael@0: T* get() const { return value_; } michael@0: T* operator->() const { return value_; } michael@0: T& operator*() const { return *value_; } michael@0: // Release ownership of the pointed object and returns it. michael@0: // Sole ownership by this linked_ptr object is required. michael@0: T* release() { michael@0: bool last = link_.depart(); michael@0: CHECK(last); michael@0: T* v = value_; michael@0: value_ = NULL; michael@0: return v; michael@0: } michael@0: michael@0: bool operator==(const T* p) const { return value_ == p; } michael@0: bool operator!=(const T* p) const { return value_ != p; } michael@0: template michael@0: bool operator==(linked_ptr const& ptr) const { michael@0: return value_ == ptr.get(); michael@0: } michael@0: template michael@0: bool operator!=(linked_ptr const& ptr) const { michael@0: return value_ != ptr.get(); michael@0: } michael@0: michael@0: private: michael@0: template michael@0: friend class linked_ptr; michael@0: michael@0: T* value_; michael@0: linked_ptr_internal link_; michael@0: michael@0: void depart() { michael@0: if (link_.depart()) delete value_; michael@0: } michael@0: michael@0: void capture(T* ptr) { michael@0: value_ = ptr; michael@0: link_.join_new(); michael@0: } michael@0: michael@0: template void copy(linked_ptr const* ptr) { michael@0: value_ = ptr->get(); michael@0: if (value_) michael@0: link_.join(&ptr->link_); michael@0: else michael@0: link_.join_new(); michael@0: } michael@0: }; michael@0: michael@0: template inline michael@0: bool operator==(T* ptr, const linked_ptr& x) { michael@0: return ptr == x.get(); michael@0: } michael@0: michael@0: template inline michael@0: bool operator!=(T* ptr, const linked_ptr& x) { michael@0: return ptr != x.get(); michael@0: } michael@0: michael@0: // A function to convert T* into linked_ptr michael@0: // Doing e.g. make_linked_ptr(new FooBarBaz(arg)) is a shorter notation michael@0: // for linked_ptr >(new FooBarBaz(arg)) michael@0: template michael@0: linked_ptr make_linked_ptr(T* ptr) { michael@0: return linked_ptr(ptr); michael@0: } michael@0: michael@0: #endif // BASE_LINKED_PTR_H_