OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // A "smart" pointer type with reference tracking. Every pointer to a | 5 // A "smart" pointer type with reference tracking. Every pointer to a |
6 // particular object is kept on a circular linked list. When the last pointer | 6 // particular object is kept on a circular linked list. When the last pointer |
7 // to an object is destroyed or reassigned, the object is deleted. | 7 // to an object is destroyed or reassigned, the object is deleted. |
8 // | 8 // |
9 // Used properly, this deletes the object when the last reference goes away. | 9 // Used properly, this deletes the object when the last reference goes away. |
10 // There are several caveats: | 10 // There are several caveats: |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 bool operator==(const T* p) const { return value_ == p; } | 127 bool operator==(const T* p) const { return value_ == p; } |
128 bool operator!=(const T* p) const { return value_ != p; } | 128 bool operator!=(const T* p) const { return value_ != p; } |
129 template <typename U> | 129 template <typename U> |
130 bool operator==(linked_ptr<U> const& ptr) const { | 130 bool operator==(linked_ptr<U> const& ptr) const { |
131 return value_ == ptr.get(); | 131 return value_ == ptr.get(); |
132 } | 132 } |
133 template <typename U> | 133 template <typename U> |
134 bool operator!=(linked_ptr<U> const& ptr) const { | 134 bool operator!=(linked_ptr<U> const& ptr) const { |
135 return value_ != ptr.get(); | 135 return value_ != ptr.get(); |
136 } | 136 } |
| 137 // To allow use as a key in map or set. |
| 138 bool operator<(linked_ptr<T> const& ptr) const { return value_ < ptr.get(); } |
137 | 139 |
138 private: | 140 private: |
139 template <typename U> | 141 template <typename U> |
140 friend class linked_ptr; | 142 friend class linked_ptr; |
141 | 143 |
142 T* value_; | 144 T* value_; |
143 linked_ptr_internal link_; | 145 linked_ptr_internal link_; |
144 | 146 |
145 void depart() { | 147 void depart() { |
146 if (link_.depart()) delete value_; | 148 if (link_.depart()) delete value_; |
(...skipping 25 matching lines...) Expand all Loading... |
172 | 174 |
173 // A function to convert T* into linked_ptr<T> | 175 // A function to convert T* into linked_ptr<T> |
174 // Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation | 176 // Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation |
175 // for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) | 177 // for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) |
176 template <typename T> | 178 template <typename T> |
177 linked_ptr<T> make_linked_ptr(T* ptr) { | 179 linked_ptr<T> make_linked_ptr(T* ptr) { |
178 return linked_ptr<T>(ptr); | 180 return linked_ptr<T>(ptr); |
179 } | 181 } |
180 | 182 |
181 #endif // BASE_MEMORY_LINKED_PTR_H_ | 183 #endif // BASE_MEMORY_LINKED_PTR_H_ |
OLD | NEW |