OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 public: | 78 public: |
79 typedef T element_type; | 79 typedef T element_type; |
80 | 80 |
81 // Take over ownership of a raw pointer. This should happen as soon as | 81 // Take over ownership of a raw pointer. This should happen as soon as |
82 // possible after the object is created. | 82 // possible after the object is created. |
83 explicit linked_ptr(T* ptr = NULL) { capture(ptr); } | 83 explicit linked_ptr(T* ptr = NULL) { capture(ptr); } |
84 ~linked_ptr() { depart(); } | 84 ~linked_ptr() { depart(); } |
85 | 85 |
86 // Copy an existing linked_ptr<>, adding ourselves to the list of references. | 86 // Copy an existing linked_ptr<>, adding ourselves to the list of references. |
87 template <typename U> linked_ptr(linked_ptr<U> const& ptr) { copy(&ptr); } | 87 template <typename U> linked_ptr(linked_ptr<U> const& ptr) { copy(&ptr); } |
88 linked_ptr(linked_ptr const& ptr) { DCHECK_NE(&ptr, this); copy(&ptr); } | 88 |
| 89 linked_ptr(linked_ptr const& ptr) { |
| 90 DCHECK_NE(&ptr, this); |
| 91 copy(&ptr); |
| 92 } |
89 | 93 |
90 // Assignment releases the old value and acquires the new. | 94 // Assignment releases the old value and acquires the new. |
91 template <typename U> linked_ptr& operator=(linked_ptr<U> const& ptr) { | 95 template <typename U> linked_ptr& operator=(linked_ptr<U> const& ptr) { |
92 depart(); | 96 depart(); |
93 copy(&ptr); | 97 copy(&ptr); |
94 return *this; | 98 return *this; |
95 } | 99 } |
96 | 100 |
97 linked_ptr& operator=(linked_ptr const& ptr) { | 101 linked_ptr& operator=(linked_ptr const& ptr) { |
98 if (&ptr != this) { | 102 if (&ptr != this) { |
99 depart(); | 103 depart(); |
100 copy(&ptr); | 104 copy(&ptr); |
101 } | 105 } |
102 return *this; | 106 return *this; |
103 } | 107 } |
104 | 108 |
105 // Smart pointer members. | 109 // Smart pointer members. |
106 void reset(T* ptr = NULL) { depart(); capture(ptr); } | 110 void reset(T* ptr = NULL) { |
| 111 depart(); |
| 112 capture(ptr); |
| 113 } |
107 T* get() const { return value_; } | 114 T* get() const { return value_; } |
108 T* operator->() const { return value_; } | 115 T* operator->() const { return value_; } |
109 T& operator*() const { return *value_; } | 116 T& operator*() const { return *value_; } |
110 // Release ownership of the pointed object and returns it. | 117 // Release ownership of the pointed object and returns it. |
111 // Sole ownership by this linked_ptr object is required. | 118 // Sole ownership by this linked_ptr object is required. |
112 T* release() { | 119 T* release() { |
113 bool last = link_.depart(); | 120 bool last = link_.depart(); |
114 CHECK(last); | 121 CHECK(last); |
115 T* v = value_; | 122 T* v = value_; |
116 value_ = NULL; | 123 value_ = NULL; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 | 172 |
166 // A function to convert T* into linked_ptr<T> | 173 // A function to convert T* into linked_ptr<T> |
167 // Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation | 174 // Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation |
168 // for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) | 175 // for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) |
169 template <typename T> | 176 template <typename T> |
170 linked_ptr<T> make_linked_ptr(T* ptr) { | 177 linked_ptr<T> make_linked_ptr(T* ptr) { |
171 return linked_ptr<T>(ptr); | 178 return linked_ptr<T>(ptr); |
172 } | 179 } |
173 | 180 |
174 #endif // BASE_LINKED_PTR_H_ | 181 #endif // BASE_LINKED_PTR_H_ |
OLD | NEW |