annotate trunk/src/Support/linked_ptr.h @ 706:f8e90b5d85fd tip

Delete CARFAC code from this repository. It has been moved to https://github.com/google/carfac Please email me with your github username to get access. I've also created a new mailing list to discuss CARFAC development: https://groups.google.com/forum/#!forum/carfac-dev
author ronw@google.com
date Thu, 18 Jul 2013 20:56:51 +0000
parents ec3de2d9e641
children
rev   line source
tomwalters@406 1 // Copyright (c) 2003 Google Inc.
tomwalters@406 2 //
tomwalters@406 3 // Permission is hereby granted, free of charge, to any person or organization
tomwalters@406 4 // obtaining a copy of the software and accompanying documentation covered by
tomwalters@406 5 // this license (the "Software") to use, reproduce, display, distribute,
tomwalters@406 6 // execute, and transmit the Software, and to prepare derivative works of the
tomwalters@406 7 // Software, and to permit third-parties to whom the Software is furnished to
tomwalters@406 8 // do so, all subject to the following:
tomwalters@406 9 //
tomwalters@406 10 // The copyright notices in the Software and this entire statement, including
tomwalters@406 11 // the above license grant, this restriction and the following disclaimer,
tomwalters@406 12 // must be included in all copies of the Software, in whole or in part, and
tomwalters@406 13 // all derivative works of the Software, unless such copies or derivative
tomwalters@406 14 // works are solely in the form of machine-executable object code generated by
tomwalters@406 15 // a source language processor.
tomwalters@406 16 //
tomwalters@406 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
tomwalters@406 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
tomwalters@406 19 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
tomwalters@406 20 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
tomwalters@406 21 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
tomwalters@406 22 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
tomwalters@406 23 // DEALINGS IN THE SOFTWARE.
tomwalters@406 24 //
tomwalters@406 25 // linked_ptr.h
tomwalters@406 26 // Author: Dan Egnor
tomwalters@406 27 //
tomwalters@406 28 // A "smart" pointer type with reference tracking. Every pointer to a
tomwalters@406 29 // particular object is kept on a circular linked list. When the last pointer
tomwalters@406 30 // to an object is destroyed or reassigned, the object is deleted.
tomwalters@406 31 //
tomwalters@406 32 // Used properly, this deletes the object when the last reference goes away.
tomwalters@406 33 // There are several caveats:
tomwalters@406 34 // - Like all reference counting schemes, cycles lead to leaks.
tomwalters@406 35 // - Each smart pointer is actually two pointers (8 bytes instead of 4).
tomwalters@406 36 // - Every time a pointer is assigned, the entire list of pointers to that
tomwalters@406 37 // object is traversed. This class is therefore NOT SUITABLE when there
tomwalters@406 38 // will often be more than two or three pointers to a particular object.
tomwalters@406 39 // - References are only tracked as long as linked_ptr<> objects are copied.
tomwalters@406 40 // If a linked_ptr<> is converted to a raw pointer and back, BAD THINGS
tomwalters@406 41 // will happen (double deletion).
tomwalters@406 42 //
tomwalters@406 43 // A good use of this class is storing object references in STL containers.
tomwalters@406 44 // You can safely put linked_ptr<> in a vector<>.
tomwalters@406 45 // Other uses may not be as good.
tomwalters@406 46 //
tomwalters@406 47 // Note: If you use an incomplete type with linked_ptr<>, the class
tomwalters@406 48 // *containing* linked_ptr<> must have a constructor and destructor (even
tomwalters@406 49 // if they do nothing!).
tomwalters@406 50 //
tomwalters@406 51 // Bill Gibbons suggested we use something like this. Yonat Sharon has
tomwalters@406 52 // a different (less useful IMHO) implementation at ootips.org.
tomwalters@406 53 //
tomwalters@406 54 // Thread Safety:
tomwalters@406 55 // A linked_ptr is NOT thread safe. Copying a linked_ptr object is
tomwalters@406 56 // effectively a read-write operation.
tomwalters@406 57
tomwalters@406 58 #ifndef UTIL_GTL_LINKED_PTR_H__
tomwalters@406 59 #define UTIL_GTL_LINKED_PTR_H__
tomwalters@406 60
tomwalters@406 61 #include <assert.h>
tomwalters@406 62
tomwalters@406 63 // This is used internally by all instances of linked_ptr<>. It needs to be
tomwalters@406 64 // a non-template class because different types of linked_ptr<> can refer to
tomwalters@406 65 // the same object (linked_ptr<Superclass>(obj) vs linked_ptr<Subclass>(obj)).
tomwalters@406 66 // So, it needs to be possible for different types of linked_ptr to participate
tomwalters@406 67 // in the same circular linked list, so we need a single class type here.
tomwalters@406 68 //
tomwalters@406 69 // DO NOT USE THIS CLASS DIRECTLY YOURSELF. Use linked_ptr<T>.
tomwalters@406 70 class linked_ptr_internal {
tomwalters@406 71 public:
tomwalters@406 72 // Create a new circle that includes only this instance.
tomwalters@406 73 void join_new() {
tomwalters@406 74 next_ = this;
tomwalters@406 75 }
tomwalters@406 76
tomwalters@406 77 // Join an existing circle.
tomwalters@406 78 void join(linked_ptr_internal const* ptr) {
tomwalters@406 79 linked_ptr_internal const* p = ptr;
tomwalters@406 80 while (p->next_ != ptr) p = p->next_;
tomwalters@406 81 p->next_ = this;
tomwalters@406 82 next_ = ptr;
tomwalters@406 83 }
tomwalters@406 84
tomwalters@406 85 // Leave whatever circle we're part of. Returns true iff we were the
tomwalters@406 86 // last member of the circle. Once this is done, you can join() another.
tomwalters@406 87 bool depart() {
tomwalters@406 88 if (next_ == this) return true;
tomwalters@406 89 linked_ptr_internal const* p = next_;
tomwalters@406 90 while (p->next_ != this) p = p->next_;
tomwalters@406 91 p->next_ = next_;
tomwalters@406 92 return false;
tomwalters@406 93 }
tomwalters@406 94
tomwalters@406 95 private:
tomwalters@406 96 mutable linked_ptr_internal const* next_;
tomwalters@406 97 };
tomwalters@406 98
tomwalters@406 99 template <typename T>
tomwalters@406 100 class linked_ptr {
tomwalters@406 101 public:
tomwalters@406 102 typedef T element_type;
tomwalters@406 103
tomwalters@406 104 // Take over ownership of a raw pointer. This should happen as soon as
tomwalters@406 105 // possible after the object is created.
tomwalters@406 106 explicit linked_ptr(T* ptr = NULL) { capture(ptr); }
tomwalters@406 107 ~linked_ptr() { depart(); }
tomwalters@406 108
tomwalters@406 109 // Copy an existing linked_ptr<>, adding ourselves to the list of references.
tomwalters@406 110 template <typename U> linked_ptr(linked_ptr<U> const& ptr) { copy(&ptr); }
tomwalters@406 111 linked_ptr(linked_ptr const& ptr) { assert(&ptr != this); copy(&ptr); }
tomwalters@406 112
tomwalters@406 113 // Assignment releases the old value and acquires the new.
tomwalters@406 114 template <typename U> linked_ptr& operator=(linked_ptr<U> const& ptr) {
tomwalters@406 115 depart();
tomwalters@406 116 copy(&ptr);
tomwalters@406 117 return *this;
tomwalters@406 118 }
tomwalters@406 119
tomwalters@406 120 linked_ptr& operator=(linked_ptr const& ptr) {
tomwalters@406 121 if (&ptr != this) {
tomwalters@406 122 depart();
tomwalters@406 123 copy(&ptr);
tomwalters@406 124 }
tomwalters@406 125 return *this;
tomwalters@406 126 }
tomwalters@406 127
tomwalters@406 128 // Smart pointer members.
tomwalters@406 129 void reset(T* ptr = NULL) { depart(); capture(ptr); }
tomwalters@406 130 T* get() const { return value_; }
tomwalters@406 131 T* operator->() const { return value_; }
tomwalters@406 132 T& operator*() const { return *value_; }
tomwalters@406 133 // Release ownership of the pointed object and returns it.
tomwalters@406 134 // Sole ownership by this linked_ptr object is required.
tomwalters@406 135 T* release() {
tomwalters@406 136 // !! GOOGLE Gears specific modification !!
tomwalters@406 137 // GCC emits a warning for not using last in a opt build.
tomwalters@406 138 // Warnings are treated as errors.
tomwalters@406 139 #ifdef DEBUG
tomwalters@406 140 bool last = link_.depart();
tomwalters@406 141 assert(last);
tomwalters@406 142 #else
tomwalters@406 143 link_.depart();
tomwalters@406 144 #endif
tomwalters@406 145 T* v = value_;
tomwalters@406 146 value_ = NULL;
tomwalters@406 147 return v;
tomwalters@406 148 }
tomwalters@406 149
tomwalters@406 150 bool operator==(T* p) const { return value_ == p; }
tomwalters@406 151 bool operator!=(T* p) const { return value_ != p; }
tomwalters@406 152 template <typename U>
tomwalters@406 153 bool operator==(linked_ptr<U> const& ptr) const {
tomwalters@406 154 return value_ == ptr.get();
tomwalters@406 155 }
tomwalters@406 156 template <typename U>
tomwalters@406 157 bool operator!=(linked_ptr<U> const& ptr) const {
tomwalters@406 158 return value_ != ptr.get();
tomwalters@406 159 }
tomwalters@406 160
tomwalters@406 161 private:
tomwalters@406 162 template <typename U>
tomwalters@406 163 friend class linked_ptr;
tomwalters@406 164
tomwalters@406 165 T* value_;
tomwalters@406 166 linked_ptr_internal link_;
tomwalters@406 167
tomwalters@406 168 void depart() {
tomwalters@406 169 if (link_.depart()) delete value_;
tomwalters@406 170 }
tomwalters@406 171
tomwalters@406 172 void capture(T* ptr) {
tomwalters@406 173 value_ = ptr;
tomwalters@406 174 link_.join_new();
tomwalters@406 175 }
tomwalters@406 176
tomwalters@406 177 template <typename U> void copy(linked_ptr<U> const* ptr) {
tomwalters@406 178 value_ = ptr->get();
tomwalters@406 179 if (value_)
tomwalters@406 180 link_.join(&ptr->link_);
tomwalters@406 181 else
tomwalters@406 182 link_.join_new();
tomwalters@406 183 }
tomwalters@406 184 };
tomwalters@406 185
tomwalters@406 186 template<typename T> inline
tomwalters@406 187 bool operator==(T* ptr, const linked_ptr<T>& x) {
tomwalters@406 188 return ptr == x.get();
tomwalters@406 189 }
tomwalters@406 190
tomwalters@406 191 template<typename T> inline
tomwalters@406 192 bool operator!=(T* ptr, const linked_ptr<T>& x) {
tomwalters@406 193 return ptr != x.get();
tomwalters@406 194 }
tomwalters@406 195
tomwalters@406 196 // A function to convert T* into linked_ptr<T>
tomwalters@406 197 // Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation
tomwalters@406 198 // for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg))
tomwalters@406 199 template <typename T>
tomwalters@406 200 linked_ptr<T> make_linked_ptr(T* ptr) {
tomwalters@406 201 return linked_ptr<T>(ptr);
tomwalters@406 202 }
tomwalters@406 203
tomwalters@406 204 #endif // UTIL_GTL_LINKED_PTR_H__