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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 linked_ptr_internal const* p = next_; | 62 linked_ptr_internal const* p = next_; |
63 while (p->next_ != this) p = p->next_; | 63 while (p->next_ != this) p = p->next_; |
64 p->next_ = next_; | 64 p->next_ = next_; |
65 return false; | 65 return false; |
66 } | 66 } |
67 | 67 |
68 private: | 68 private: |
69 mutable linked_ptr_internal const* next_; | 69 mutable linked_ptr_internal const* next_; |
70 }; | 70 }; |
71 | 71 |
72 // TODO(http://crbug.com/556939): DEPRECATED: Use scoped_ptr instead (now that | 72 // TODO(http://crbug.com/556939): DEPRECATED: Use unique_ptr instead (now that |
73 // we have support for moveable types inside STL containers). | 73 // we have support for moveable types inside STL containers). |
74 template <typename T> | 74 template <typename T> |
75 class linked_ptr { | 75 class linked_ptr { |
76 public: | 76 public: |
77 typedef T element_type; | 77 typedef T element_type; |
78 | 78 |
79 // Take over ownership of a raw pointer. This should happen as soon as | 79 // Take over ownership of a raw pointer. This should happen as soon as |
80 // possible after the object is created. | 80 // possible after the object is created. |
81 explicit linked_ptr(T* ptr = NULL) { capture(ptr); } | 81 explicit linked_ptr(T* ptr = NULL) { capture(ptr); } |
82 ~linked_ptr() { depart(); } | 82 ~linked_ptr() { depart(); } |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 | 170 |
171 // A function to convert T* into linked_ptr<T> | 171 // A function to convert T* into linked_ptr<T> |
172 // Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation | 172 // Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation |
173 // for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) | 173 // for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) |
174 template <typename T> | 174 template <typename T> |
175 linked_ptr<T> make_linked_ptr(T* ptr) { | 175 linked_ptr<T> make_linked_ptr(T* ptr) { |
176 return linked_ptr<T>(ptr); | 176 return linked_ptr<T>(ptr); |
177 } | 177 } |
178 | 178 |
179 #endif // BASE_MEMORY_LINKED_PTR_H_ | 179 #endif // BASE_MEMORY_LINKED_PTR_H_ |
OLD | NEW |