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 | 88 linked_ptr(linked_ptr const& ptr) { DCHECK_NE(&ptr, this); copy(&ptr); } |
89 linked_ptr(linked_ptr const& ptr) { | |
90 DCHECK_NE(&ptr, this); | |
91 copy(&ptr); | |
92 } | |
93 | 89 |
94 // Assignment releases the old value and acquires the new. | 90 // Assignment releases the old value and acquires the new. |
95 template <typename U> linked_ptr& operator=(linked_ptr<U> const& ptr) { | 91 template <typename U> linked_ptr& operator=(linked_ptr<U> const& ptr) { |
96 depart(); | 92 depart(); |
97 copy(&ptr); | 93 copy(&ptr); |
98 return *this; | 94 return *this; |
99 } | 95 } |
100 | 96 |
101 linked_ptr& operator=(linked_ptr const& ptr) { | 97 linked_ptr& operator=(linked_ptr const& ptr) { |
102 if (&ptr != this) { | 98 if (&ptr != this) { |
103 depart(); | 99 depart(); |
104 copy(&ptr); | 100 copy(&ptr); |
105 } | 101 } |
106 return *this; | 102 return *this; |
107 } | 103 } |
108 | 104 |
109 // Smart pointer members. | 105 // Smart pointer members. |
110 void reset(T* ptr = NULL) { | 106 void reset(T* ptr = NULL) { depart(); capture(ptr); } |
111 depart(); | |
112 capture(ptr); | |
113 } | |
114 T* get() const { return value_; } | 107 T* get() const { return value_; } |
115 T* operator->() const { return value_; } | 108 T* operator->() const { return value_; } |
116 T& operator*() const { return *value_; } | 109 T& operator*() const { return *value_; } |
117 // Release ownership of the pointed object and returns it. | 110 // Release ownership of the pointed object and returns it. |
118 // Sole ownership by this linked_ptr object is required. | 111 // Sole ownership by this linked_ptr object is required. |
119 T* release() { | 112 T* release() { |
120 bool last = link_.depart(); | 113 bool last = link_.depart(); |
121 CHECK(last); | 114 CHECK(last); |
122 T* v = value_; | 115 T* v = value_; |
123 value_ = NULL; | 116 value_ = NULL; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 | 165 |
173 // A function to convert T* into linked_ptr<T> | 166 // A function to convert T* into linked_ptr<T> |
174 // Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation | 167 // Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation |
175 // for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) | 168 // for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) |
176 template <typename T> | 169 template <typename T> |
177 linked_ptr<T> make_linked_ptr(T* ptr) { | 170 linked_ptr<T> make_linked_ptr(T* ptr) { |
178 return linked_ptr<T>(ptr); | 171 return linked_ptr<T>(ptr); |
179 } | 172 } |
180 | 173 |
181 #endif // BASE_LINKED_PTR_H_ | 174 #endif // BASE_LINKED_PTR_H_ |
OLD | NEW |