tomwalters@123: // Copyright (c) 2003 Google Inc. tomwalters@123: // tomwalters@123: // Permission is hereby granted, free of charge, to any person or organization tomwalters@123: // obtaining a copy of the software and accompanying documentation covered by tomwalters@123: // this license (the "Software") to use, reproduce, display, distribute, tomwalters@123: // execute, and transmit the Software, and to prepare derivative works of the tomwalters@123: // Software, and to permit third-parties to whom the Software is furnished to tomwalters@123: // do so, all subject to the following: tomwalters@123: // tomwalters@123: // The copyright notices in the Software and this entire statement, including tomwalters@123: // the above license grant, this restriction and the following disclaimer, tomwalters@123: // must be included in all copies of the Software, in whole or in part, and tomwalters@123: // all derivative works of the Software, unless such copies or derivative tomwalters@123: // works are solely in the form of machine-executable object code generated by tomwalters@123: // a source language processor. tomwalters@123: // tomwalters@123: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR tomwalters@123: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, tomwalters@123: // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT tomwalters@123: // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE tomwalters@123: // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, tomwalters@123: // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER tomwalters@123: // DEALINGS IN THE SOFTWARE. tomwalters@123: // tomwalters@123: // linked_ptr.h tomwalters@123: // Author: Dan Egnor tomwalters@123: // tomwalters@123: // A "smart" pointer type with reference tracking. Every pointer to a tomwalters@123: // particular object is kept on a circular linked list. When the last pointer tomwalters@123: // to an object is destroyed or reassigned, the object is deleted. tomwalters@123: // tomwalters@123: // Used properly, this deletes the object when the last reference goes away. tomwalters@123: // There are several caveats: tomwalters@123: // - Like all reference counting schemes, cycles lead to leaks. tomwalters@123: // - Each smart pointer is actually two pointers (8 bytes instead of 4). tomwalters@123: // - Every time a pointer is assigned, the entire list of pointers to that tomwalters@123: // object is traversed. This class is therefore NOT SUITABLE when there tomwalters@123: // will often be more than two or three pointers to a particular object. tomwalters@123: // - References are only tracked as long as linked_ptr<> objects are copied. tomwalters@123: // If a linked_ptr<> is converted to a raw pointer and back, BAD THINGS tomwalters@123: // will happen (double deletion). tomwalters@123: // tomwalters@123: // A good use of this class is storing object references in STL containers. tomwalters@123: // You can safely put linked_ptr<> in a vector<>. tomwalters@123: // Other uses may not be as good. tomwalters@123: // tomwalters@123: // Note: If you use an incomplete type with linked_ptr<>, the class tomwalters@123: // *containing* linked_ptr<> must have a constructor and destructor (even tomwalters@123: // if they do nothing!). tomwalters@123: // tomwalters@123: // Bill Gibbons suggested we use something like this. Yonat Sharon has tomwalters@123: // a different (less useful IMHO) implementation at ootips.org. tomwalters@123: // tomwalters@123: // Thread Safety: tomwalters@123: // A linked_ptr is NOT thread safe. Copying a linked_ptr object is tomwalters@123: // effectively a read-write operation. tomwalters@123: tomwalters@123: #ifndef UTIL_GTL_LINKED_PTR_H__ tomwalters@123: #define UTIL_GTL_LINKED_PTR_H__ tomwalters@123: tomwalters@123: #include tomwalters@123: tomwalters@123: // This is used internally by all instances of linked_ptr<>. It needs to be tomwalters@123: // a non-template class because different types of linked_ptr<> can refer to tomwalters@123: // the same object (linked_ptr(obj) vs linked_ptr(obj)). tomwalters@123: // So, it needs to be possible for different types of linked_ptr to participate tomwalters@123: // in the same circular linked list, so we need a single class type here. tomwalters@123: // tomwalters@123: // DO NOT USE THIS CLASS DIRECTLY YOURSELF. Use linked_ptr. tomwalters@123: class linked_ptr_internal { tomwalters@123: public: tomwalters@123: // Create a new circle that includes only this instance. tomwalters@123: void join_new() { tomwalters@123: next_ = this; tomwalters@123: } tomwalters@123: tomwalters@123: // Join an existing circle. tomwalters@123: void join(linked_ptr_internal const* ptr) { tomwalters@123: linked_ptr_internal const* p = ptr; tomwalters@123: while (p->next_ != ptr) p = p->next_; tomwalters@123: p->next_ = this; tomwalters@123: next_ = ptr; tomwalters@123: } tomwalters@123: tomwalters@123: // Leave whatever circle we're part of. Returns true iff we were the tomwalters@123: // last member of the circle. Once this is done, you can join() another. tomwalters@123: bool depart() { tomwalters@123: if (next_ == this) return true; tomwalters@123: linked_ptr_internal const* p = next_; tomwalters@123: while (p->next_ != this) p = p->next_; tomwalters@123: p->next_ = next_; tomwalters@123: return false; tomwalters@123: } tomwalters@123: tomwalters@123: private: tomwalters@123: mutable linked_ptr_internal const* next_; tomwalters@123: }; tomwalters@123: tomwalters@123: template tomwalters@123: class linked_ptr { tomwalters@123: public: tomwalters@123: typedef T element_type; tomwalters@123: tomwalters@123: // Take over ownership of a raw pointer. This should happen as soon as tomwalters@123: // possible after the object is created. tomwalters@123: explicit linked_ptr(T* ptr = NULL) { capture(ptr); } tomwalters@123: ~linked_ptr() { depart(); } tomwalters@123: tomwalters@123: // Copy an existing linked_ptr<>, adding ourselves to the list of references. tomwalters@123: template linked_ptr(linked_ptr const& ptr) { copy(&ptr); } tomwalters@123: linked_ptr(linked_ptr const& ptr) { assert(&ptr != this); copy(&ptr); } tomwalters@123: tomwalters@123: // Assignment releases the old value and acquires the new. tomwalters@123: template linked_ptr& operator=(linked_ptr const& ptr) { tomwalters@123: depart(); tomwalters@123: copy(&ptr); tomwalters@123: return *this; tomwalters@123: } tomwalters@123: tomwalters@123: linked_ptr& operator=(linked_ptr const& ptr) { tomwalters@123: if (&ptr != this) { tomwalters@123: depart(); tomwalters@123: copy(&ptr); tomwalters@123: } tomwalters@123: return *this; tomwalters@123: } tomwalters@123: tomwalters@123: // Smart pointer members. tomwalters@123: void reset(T* ptr = NULL) { depart(); capture(ptr); } tomwalters@123: T* get() const { return value_; } tomwalters@123: T* operator->() const { return value_; } tomwalters@123: T& operator*() const { return *value_; } tomwalters@123: // Release ownership of the pointed object and returns it. tomwalters@123: // Sole ownership by this linked_ptr object is required. tomwalters@123: T* release() { tomwalters@123: // !! GOOGLE Gears specific modification !! tomwalters@123: // GCC emits a warning for not using last in a opt build. tomwalters@123: // Warnings are treated as errors. tomwalters@123: #ifdef DEBUG tomwalters@123: bool last = link_.depart(); tomwalters@123: assert(last); tomwalters@123: #else tomwalters@123: link_.depart(); tomwalters@123: #endif tomwalters@123: T* v = value_; tomwalters@123: value_ = NULL; tomwalters@123: return v; tomwalters@123: } tomwalters@123: tomwalters@123: bool operator==(T* p) const { return value_ == p; } tomwalters@123: bool operator!=(T* p) const { return value_ != p; } tomwalters@123: template tomwalters@123: bool operator==(linked_ptr const& ptr) const { tomwalters@123: return value_ == ptr.get(); tomwalters@123: } tomwalters@123: template tomwalters@123: bool operator!=(linked_ptr const& ptr) const { tomwalters@123: return value_ != ptr.get(); tomwalters@123: } tomwalters@123: tomwalters@123: private: tomwalters@123: template tomwalters@123: friend class linked_ptr; tomwalters@123: tomwalters@123: T* value_; tomwalters@123: linked_ptr_internal link_; tomwalters@123: tomwalters@123: void depart() { tomwalters@123: if (link_.depart()) delete value_; tomwalters@123: } tomwalters@123: tomwalters@123: void capture(T* ptr) { tomwalters@123: value_ = ptr; tomwalters@123: link_.join_new(); tomwalters@123: } tomwalters@123: tomwalters@123: template void copy(linked_ptr const* ptr) { tomwalters@123: value_ = ptr->get(); tomwalters@123: if (value_) tomwalters@123: link_.join(&ptr->link_); tomwalters@123: else tomwalters@123: link_.join_new(); tomwalters@123: } tomwalters@123: }; tomwalters@123: tomwalters@123: template inline tomwalters@123: bool operator==(T* ptr, const linked_ptr& x) { tomwalters@123: return ptr == x.get(); tomwalters@123: } tomwalters@123: tomwalters@123: template inline tomwalters@123: bool operator!=(T* ptr, const linked_ptr& x) { tomwalters@123: return ptr != x.get(); tomwalters@123: } tomwalters@123: tomwalters@123: // A function to convert T* into linked_ptr tomwalters@123: // Doing e.g. make_linked_ptr(new FooBarBaz(arg)) is a shorter notation tomwalters@123: // for linked_ptr >(new FooBarBaz(arg)) tomwalters@123: template tomwalters@123: linked_ptr make_linked_ptr(T* ptr) { tomwalters@123: return linked_ptr(ptr); tomwalters@123: } tomwalters@123: tomwalters@123: #endif // UTIL_GTL_LINKED_PTR_H__