comparison src/Support/linked_ptr.h @ 123:79d5cbc09484

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