OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Scopers help you manage ownership of a pointer, helping you easily manage the | 5 // Scopers help you manage ownership of a pointer, helping you easily manage the |
6 // a pointer within a scope, and automatically destroying the pointer at the | 6 // a pointer within a scope, and automatically destroying the pointer at the |
7 // end of a scope. There are two main classes you will use, which correspond | 7 // end of a scope. There are two main classes you will use, which correspond |
8 // to the operators new/delete and new[]/delete[]. | 8 // to the operators new/delete and new[]/delete[]. |
9 // | 9 // |
10 // Example usage (scoped_ptr): | 10 // Example usage (scoped_ptr): |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
88 #define BASE_MEMORY_SCOPED_PTR_H_ | 88 #define BASE_MEMORY_SCOPED_PTR_H_ |
89 | 89 |
90 // This is an implementation designed to match the anticipated future TR2 | 90 // This is an implementation designed to match the anticipated future TR2 |
91 // implementation of the scoped_ptr class, and its closely-related brethren, | 91 // implementation of the scoped_ptr class, and its closely-related brethren, |
92 // scoped_array, scoped_ptr_malloc. | 92 // scoped_array, scoped_ptr_malloc. |
93 | 93 |
94 #include <assert.h> | 94 #include <assert.h> |
95 #include <stddef.h> | 95 #include <stddef.h> |
96 #include <stdlib.h> | 96 #include <stdlib.h> |
97 | 97 |
98 #include <algorithm> // For std::swap(). | |
99 | |
98 #include "base/basictypes.h" | 100 #include "base/basictypes.h" |
99 #include "base/compiler_specific.h" | 101 #include "base/compiler_specific.h" |
100 #include "base/move.h" | 102 #include "base/move.h" |
101 #include "base/template_util.h" | 103 #include "base/template_util.h" |
102 | 104 |
103 namespace base { | 105 namespace base { |
104 | 106 |
105 namespace subtle { | 107 namespace subtle { |
106 class RefCountedBase; | 108 class RefCountedBase; |
107 class RefCountedThreadSafeBase; | 109 class RefCountedThreadSafeBase; |
108 } // namespace subtle | 110 } // namespace subtle |
109 | 111 |
112 // Function object which deletes its parameter, which must be a pointer. | |
113 // If C is an array type, invokes 'delete[]' on the parameter; otherwise, | |
114 // invokes 'delete'. The default deleter for scoped_ptr<T>. | |
115 template <class T> | |
116 struct DefaultDeleter { | |
117 DefaultDeleter() {} | |
118 template <typename U> DefaultDeleter(const DefaultDeleter<U>& other) { | |
119 // All default single-object deleters can trivially convert to one another. | |
Ryan Sleevi
2012/11/29 01:42:26
20.7.1.1.2 2 has
"This constructor shall not part
gromer
2012/12/04 19:41:18
Oh, I see, this is an issue only because of the Ch
Ryan Sleevi
2012/12/04 20:09:51
gromer: I'm not sure I follow your remark. The spe
gromer
2012/12/04 20:39:27
I'm afraid I don't follow you either; I was mostly
awong
2012/12/12 02:17:03
Reading through this, I think adding a COMPILE_ASS
gromer
2012/12/12 17:29:57
LG.
| |
120 } | |
121 inline void operator()(T* ptr) const { | |
122 enum { type_must_be_complete = sizeof(T) }; | |
123 delete ptr; | |
124 } | |
125 }; | |
126 | |
127 // Specialization of DefaultDeleter for array types. | |
128 template <class T> | |
129 struct DefaultDeleter<T[]> { | |
Ryan Sleevi
2012/11/29 01:42:26
You're missing the constructors here - in particul
gromer
2012/12/04 19:41:18
"Missing" relative to what? std::default_delete<T[
Ryan Sleevi
2012/12/04 20:09:51
Sorry, ignore this. It's a disconnect between libc
| |
130 inline void operator()(T* ptr) const { | |
131 enum { type_must_be_complete = sizeof(T) }; | |
132 delete[] ptr; | |
133 } | |
134 | |
135 private: | |
136 // Disable this operator for any U != T because it is unsafe to execute | |
137 // an array delete when the static type of the array mismatches the dynamic | |
138 // type. | |
139 template <typename U> void operator()(U* array) const; | |
140 }; | |
141 | |
142 // Function object which invokes 'free' on its parameter, which must be | |
143 // a pointer. Can be used to store malloc-allocated pointers in scoped_ptr: | |
144 // | |
145 // scoped_ptr<int, base::FreeDeleter> foo_ptr( | |
146 // static_cast<int*>(malloc(sizeof(int)))); | |
147 struct FreeDeleter { | |
148 inline void operator()(void* ptr) const { | |
149 free(ptr); | |
150 } | |
151 }; | |
152 | |
110 namespace internal { | 153 namespace internal { |
111 | 154 |
112 template <typename T> struct IsNotRefCounted { | 155 template <typename T> struct IsNotRefCounted { |
113 enum { | 156 enum { |
114 value = !base::is_convertible<T*, base::subtle::RefCountedBase*>::value && | 157 value = !base::is_convertible<T*, base::subtle::RefCountedBase*>::value && |
115 !base::is_convertible<T*, base::subtle::RefCountedThreadSafeBase*>:: | 158 !base::is_convertible<T*, base::subtle::RefCountedThreadSafeBase*>:: |
116 value | 159 value |
117 }; | 160 }; |
118 }; | 161 }; |
119 | 162 |
163 // Minimal implementation of the core logic of scoped_ptr, suitable for | |
164 // reuse in both scoped_ptr and its specializations. | |
165 template <class T, class D> | |
166 class scoped_ptr_impl { | |
167 public: | |
168 explicit scoped_ptr_impl(T* p) : data_(p) { } | |
169 | |
170 // Initializer for deleters that have data parameters. | |
171 scoped_ptr_impl(T* p, const D& d) : data_(p, d) {} | |
172 | |
173 // Templated constructor that destructively takes the value from another | |
174 // scoped_ptr_impl. | |
175 template <typename U, typename V> | |
176 scoped_ptr_impl(scoped_ptr_impl<U, V>* other) | |
Jeffrey Yasskin
2012/11/28 22:06:42
Could you DISALLOW_COPY_AND_ASSIGN so there's no w
awong
2012/11/29 01:15:13
Done.
| |
177 : data_(other->release(), other->get_deleter()) { | |
178 // We do not support move-only deleters. We could modify our move | |
179 // emulation to have base::subtle::move() and base::subtle::forward() | |
180 // functions that are imperfect emulations of their C++11 equivalents, | |
181 // but until there's a requirement, just assume deleters are copyable. | |
182 } | |
183 | |
184 template <typename U, typename V> | |
185 void TakeState(scoped_ptr_impl<U, V>* other) { | |
186 // See comment in templated constructor above regarding lack of support | |
187 // for move-only deleters. | |
188 reset(other->release()); | |
189 get_deleter() = other->get_deleter(); | |
190 } | |
191 | |
192 ~scoped_ptr_impl() { | |
193 if (data_.ptr != NULL) { | |
194 // Not using get_deleter() saves one function call in non-optimized | |
195 // builds. | |
196 static_cast<D&>(data_)(data_.ptr); | |
197 } | |
198 } | |
199 | |
200 void reset(T* p) { | |
201 // This self-reset check is deprecated. | |
202 // this->reset(this->get()) currently works, but it is DEPRECATED, and | |
203 // will be removed once we verify that no one depends on it. | |
204 // | |
205 // TODO(ajwong): Change this behavior to match unique_ptr<>. | |
206 // http://crbug.com/162971 | |
207 if (p != data_.ptr) { | |
208 if (data_.ptr != NULL) { | |
209 // Note that this can lead to undefined behavior and memory leaks | |
210 // in the unlikely but possible case that get_deleter()(get()) | |
211 // indirectly deletes this. The fix is to reset ptr_ before deleting | |
212 // its old value, but first we need to clean up the code that relies | |
213 // on the current sequencing. | |
214 static_cast<D&>(data_)(data_.ptr); | |
215 } | |
216 data_.ptr = p; | |
217 } | |
218 } | |
219 | |
220 T* get() const { return data_.ptr; } | |
221 | |
222 D& get_deleter() { return data_; } | |
Ryan Sleevi
2012/11/29 01:42:26
Shouldn't you remove and then re-add the reference
awong
2012/12/12 02:17:03
If I'm correct, right now we would just fail if D
Ryan Sleevi
2012/12/12 03:37:46
Yup.
Being able to take pointer types "would be n
| |
223 const D& get_deleter() const { return data_; } | |
224 | |
225 void swap(scoped_ptr_impl& p2) { | |
226 // Standard swap idiom: 'using std::swap' ensures that std::swap is | |
227 // present in the overload set, but we call swap unqualified so that | |
228 // any more-specific overloads can be used, if available. | |
229 using std::swap; | |
230 swap(static_cast<D&>(data_), static_cast<D&>(p2.data_)); | |
231 swap(data_.ptr, p2.data_.ptr); | |
232 } | |
233 | |
234 T* release() { | |
235 T* old_ptr = data_.ptr; | |
236 data_.ptr = NULL; | |
237 return old_ptr; | |
238 } | |
239 | |
240 private: | |
241 // Needed to allow type-converting constructor. | |
242 template <typename U, typename V> friend class scoped_ptr_impl; | |
243 | |
244 // Use the empty base class optimization to allow us to have a D | |
245 // member, while avoiding any space overhead for it when D is an | |
246 // empty class. See e.g. http://www.cantrip.org/emptyopt.html for a good | |
247 // discussion of this technique. | |
248 struct Data : public D { | |
249 explicit Data(T* ptr_in) : ptr(ptr_in) {} | |
250 Data(T* ptr_in, const D& other) : D(other), ptr(ptr_in) {} | |
251 T* ptr; | |
252 }; | |
253 | |
254 Data data_; | |
255 }; | |
256 | |
120 } // namespace internal | 257 } // namespace internal |
258 | |
121 } // namespace base | 259 } // namespace base |
122 | 260 |
123 // A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T> | 261 // A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T> |
124 // automatically deletes the pointer it holds (if any). | 262 // automatically deletes the pointer it holds (if any). |
125 // That is, scoped_ptr<T> owns the T object that it points to. | 263 // That is, scoped_ptr<T> owns the T object that it points to. |
126 // Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object. | 264 // Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object. |
127 // Also like T*, scoped_ptr<T> is thread-compatible, and once you | 265 // Also like T*, scoped_ptr<T> is thread-compatible, and once you |
128 // dereference it, you get the thread safety guarantees of T. | 266 // dereference it, you get the thread safety guarantees of T. |
129 // | 267 // |
130 // The size of a scoped_ptr is small: | 268 // The size of a scoped_ptr is small: |
131 // sizeof(scoped_ptr<C>) == sizeof(C*) | 269 // sizeof(scoped_ptr<C>) == sizeof(C*) |
132 template <class C> | 270 template <class T, class D = base::DefaultDeleter<T> > |
Ryan Sleevi
2012/11/29 01:42:26
20.7.1.2.1 requires that D be a function object ty
awong
2012/12/12 02:17:03
Right now, I think what happens is we don't suppor
Ryan Sleevi
2012/12/12 03:37:46
You could use a helper type to add a "deleter_type
awong
2012/12/12 21:00:14
Yeah, I think that would work. However, if I start
| |
133 class scoped_ptr { | 271 class scoped_ptr { |
134 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue) | 272 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue) |
135 | 273 |
136 COMPILE_ASSERT(base::internal::IsNotRefCounted<C>::value, | 274 COMPILE_ASSERT(base::internal::IsNotRefCounted<T>::value, |
137 C_is_refcounted_type_and_needs_scoped_refptr); | 275 T_is_refcounted_type_and_needs_scoped_refptr); |
138 | 276 |
139 public: | 277 public: |
140 | 278 // The element and deleter types. |
Ryan Sleevi
2012/11/29 01:42:26
20.7.1.2.3 defines the ::pointer type, as accordin
awong
2012/12/12 02:17:03
Good to know. Going to skip this one as well becau
| |
141 // The element type | 279 typedef T element_type; |
142 typedef C element_type; | 280 typedef D deleter_type; |
143 | 281 |
144 // Constructor. Defaults to initializing with NULL. | 282 // Constructor. Defaults to initializing with NULL. |
145 // There is no way to create an uninitialized scoped_ptr. | 283 scoped_ptr() : impl_(NULL) { } |
146 // The input parameter must be allocated with new. | 284 |
147 explicit scoped_ptr(C* p = NULL) : ptr_(p) { } | 285 // Constructor. Takes ownership of p. |
286 explicit scoped_ptr(element_type* p) : impl_(p) { } | |
287 | |
288 // Constructor. Allows initialization of a stateful deleter. | |
289 scoped_ptr(element_type* p, const D& d) : impl_(p, d) { } | |
148 | 290 |
149 // Constructor. Allows construction from a scoped_ptr rvalue for a | 291 // Constructor. Allows construction from a scoped_ptr rvalue for a |
150 // convertible type. | 292 // convertible type and deleter. |
151 template <typename U> | 293 template <typename U, typename V> |
152 scoped_ptr(scoped_ptr<U> other) : ptr_(other.release()) { } | 294 scoped_ptr(scoped_ptr<U, V> other) : impl_(&other.impl_) { } |
Ryan Sleevi
2012/11/29 01:42:26
Note 20.7.1.2.1.19, which discards the move constr
gromer
2012/12/04 19:41:18
As noted, the simulated move semantics are outside
awong
2012/12/12 02:17:03
Ick...nice catch.
Reading through 20.7.1.2.1.{15.
Ryan Sleevi
2012/12/12 03:37:46
Sounds right to me.
gromer
2012/12/12 17:29:57
LG.
| |
153 | 295 |
154 // Constructor. Move constructor for C++03 move emulation of this type. | 296 // Constructor. Move constructor for C++03 move emulation of this type. |
155 scoped_ptr(RValue rvalue) | 297 scoped_ptr(RValue rvalue) : impl_(&rvalue.object->impl_) { } |
156 : ptr_(rvalue.object->release()) { | |
157 } | |
158 | |
159 // Destructor. If there is a C object, delete it. | |
160 // We don't need to test ptr_ == NULL because C++ does that for us. | |
161 ~scoped_ptr() { | |
162 enum { type_must_be_complete = sizeof(C) }; | |
163 delete ptr_; | |
164 } | |
165 | 298 |
166 // operator=. Allows assignment from a scoped_ptr rvalue for a convertible | 299 // operator=. Allows assignment from a scoped_ptr rvalue for a convertible |
167 // type. | 300 // type and deleter. |
168 template <typename U> | 301 template <typename U, typename V> |
169 scoped_ptr& operator=(scoped_ptr<U> rhs) { | 302 scoped_ptr& operator=(scoped_ptr<U, V> rhs) { |
Ryan Sleevi
2012/11/29 01:42:26
same concerns here raised on 294
This is called o
awong
2012/12/12 02:17:03
See previous.
| |
170 reset(rhs.release()); | 303 impl_.TakeState(&rhs.impl_); |
171 return *this; | 304 return *this; |
172 } | 305 } |
173 | 306 |
174 // operator=. Move operator= for C++03 move emulation of this type. | 307 // Reset. Deletes the currently owned object, if any. |
175 scoped_ptr& operator=(RValue rhs) { | |
176 swap(*rhs->object); | |
177 return *this; | |
178 } | |
179 | |
180 // Reset. Deletes the current owned object, if any. | |
181 // Then takes ownership of a new object, if given. | 308 // Then takes ownership of a new object, if given. |
182 // this->reset(this->get()) works. | 309 void reset(element_type* p = NULL) { impl_.reset(p); } |
183 void reset(C* p = NULL) { | |
184 if (p != ptr_) { | |
185 enum { type_must_be_complete = sizeof(C) }; | |
186 delete ptr_; | |
187 ptr_ = p; | |
188 } | |
189 } | |
190 | 310 |
191 // Accessors to get the owned object. | 311 // Accessors to get the owned object. |
192 // operator* and operator-> will assert() if there is no current object. | 312 // operator* and operator-> will assert() if there is no current object. |
193 C& operator*() const { | 313 element_type& operator*() const { |
194 assert(ptr_ != NULL); | 314 assert(impl_.get() != NULL); |
195 return *ptr_; | 315 return *impl_.get(); |
196 } | 316 } |
197 C* operator->() const { | 317 element_type* operator->() const { |
198 assert(ptr_ != NULL); | 318 assert(impl_.get() != NULL); |
199 return ptr_; | 319 return impl_.get(); |
200 } | 320 } |
201 C* get() const { return ptr_; } | 321 element_type* get() const { return impl_.get(); } |
202 | 322 |
203 // Allow scoped_ptr<C> to be used in boolean expressions, but not | 323 // Access to the deleter. |
324 deleter_type& get_deleter() { return impl_.get_deleter(); } | |
325 const deleter_type& get_deleter() const { return impl_.get_deleter(); } | |
326 | |
327 // Allow scoped_ptr<element_type> to be used in boolean expressions, but not | |
204 // implicitly convertible to a real bool (which is dangerous). | 328 // implicitly convertible to a real bool (which is dangerous). |
205 typedef C* scoped_ptr::*Testable; | 329 private: |
206 operator Testable() const { return ptr_ ? &scoped_ptr::ptr_ : NULL; } | 330 typedef base::internal::scoped_ptr_impl<element_type, deleter_type> |
331 scoped_ptr::*Testable; | |
332 | |
333 public: | |
334 operator Testable() const { return impl_.get() ? &scoped_ptr::impl_ : NULL; } | |
207 | 335 |
208 // Comparison operators. | 336 // Comparison operators. |
209 // These return whether two scoped_ptr refer to the same object, not just to | 337 // These return whether two scoped_ptr refer to the same object, not just to |
210 // two different but equal objects. | 338 // two different but equal objects. |
211 bool operator==(C* p) const { return ptr_ == p; } | 339 bool operator==(element_type* p) const { return impl_.get() == p; } |
212 bool operator!=(C* p) const { return ptr_ != p; } | 340 bool operator!=(element_type* p) const { return impl_.get() != p; } |
213 | 341 |
214 // Swap two scoped pointers. | 342 // Swap two scoped pointers. |
215 void swap(scoped_ptr& p2) { | 343 void swap(scoped_ptr& p2) { |
216 C* tmp = ptr_; | 344 impl_.swap(p2.impl_); |
217 ptr_ = p2.ptr_; | |
218 p2.ptr_ = tmp; | |
219 } | 345 } |
220 | 346 |
221 // Release a pointer. | 347 // Release a pointer. |
222 // The return value is the current pointer held by this object. | 348 // The return value is the current pointer held by this object. |
223 // If this object holds a NULL pointer, the return value is NULL. | 349 // If this object holds a NULL pointer, the return value is NULL. |
224 // After this operation, this object will hold a NULL pointer, | 350 // After this operation, this object will hold a NULL pointer, |
225 // and will not own the object any more. | 351 // and will not own the object any more. |
226 C* release() WARN_UNUSED_RESULT { | 352 element_type* release() WARN_UNUSED_RESULT { |
227 C* retVal = ptr_; | 353 return impl_.release(); |
228 ptr_ = NULL; | |
229 return retVal; | |
230 } | 354 } |
231 | 355 |
356 // C++98 doesn't support functions templates with default parameters which | |
357 // makes it hard to write a PassAs() that understands converting the deleter | |
358 // while preserving simple calling semantics. | |
359 // | |
360 // Until there is a use case for PassAs() with custom deleters, just ignore | |
361 // the custom deleter. | |
232 template <typename PassAsType> | 362 template <typename PassAsType> |
233 scoped_ptr<PassAsType> PassAs() { | 363 scoped_ptr<PassAsType> PassAs() { |
234 return scoped_ptr<PassAsType>(release()); | 364 return scoped_ptr<PassAsType>(Pass()); |
235 } | 365 } |
236 | 366 |
237 private: | 367 private: |
238 C* ptr_; | 368 // Needed to reach into |impl_| in the constructor. |
369 template <typename U, typename V> friend class scoped_ptr; | |
370 base::internal::scoped_ptr_impl<element_type, deleter_type> impl_; | |
239 | 371 |
240 // Forbid comparison of scoped_ptr types. If C2 != C, it totally doesn't | 372 // Forbid comparison of scoped_ptr types. If U != T, it totally |
241 // make sense, and if C2 == C, it still doesn't make sense because you should | 373 // doesn't make sense, and if U == T, it still doesn't make sense |
242 // never have the same object owned by two different scoped_ptrs. | 374 // because you should never have the same object owned by two different |
243 template <class C2> bool operator==(scoped_ptr<C2> const& p2) const; | 375 // scoped_ptrs. |
244 template <class C2> bool operator!=(scoped_ptr<C2> const& p2) const; | 376 template <class U> bool operator==(scoped_ptr<U> const& p2) const; |
377 template <class U> bool operator!=(scoped_ptr<U> const& p2) const; | |
378 }; | |
245 | 379 |
380 template <class T, class D> | |
381 class scoped_ptr<T[], D> { | |
382 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue) | |
383 | |
384 public: | |
385 // The element and deleter types. | |
386 typedef T element_type; | |
387 typedef D deleter_type; | |
388 | |
389 // Constructor. Defaults to initializing with NULL. | |
390 scoped_ptr() : impl_(NULL) { } | |
391 | |
392 // Constructor. Stores the given array. Note that the argument's type | |
393 // must exactly match T*. In particular: | |
394 // - it cannot be a pointer to a type derived from T, because it is | |
395 // inherently unsafe to access an array through a pointer whose | |
396 // dynamic type does not match its static type. If you're doing this, | |
397 // fix your code. | |
398 // http://cplusplus.github.com/LWG/lwg-defects.html#938 | |
399 // - it cannot be NULL, because NULL is an integral expression, not a | |
400 // pointer to T. Use the no-argument version instead of explicitly | |
401 // passing NULL. | |
402 // - it cannot be const-qualified differently from T per unique_ptr spec. | |
Jeffrey Yasskin
2012/11/28 22:06:42
Perhaps mention that users should implicit_cast<co
awong
2012/11/29 01:15:13
Done and also beefed up the first bullet.
| |
403 // http://cplusplus.github.com/LWG/lwg-active.html#2118 | |
404 explicit scoped_ptr(element_type* array) : impl_(array) { } | |
405 | |
406 // Constructor. Move constructor for C++03 move emulation of this type. | |
407 scoped_ptr(RValue rvalue) : impl_(&rvalue.object->impl_) { } | |
408 | |
409 // operator=. Move operator= for C++03 move emulation of this type. | |
410 scoped_ptr& operator=(RValue rhs) { | |
411 impl_.TakeState(&rhs.object->impl_); | |
412 return *this; | |
413 } | |
414 | |
415 // Reset. Deletes the currently owned array, if any. | |
416 // Then takes ownership of a new object, if given. | |
417 void reset(element_type* array = NULL) { impl_.reset(array); } | |
418 | |
419 // Accessors to get the owned array. | |
420 element_type& operator[](size_t i) const { | |
421 assert(impl_.get() != NULL); | |
Ryan Sleevi
2012/11/29 01:42:26
I didn't think we "liked" using assert() - or at l
awong
2012/12/12 02:17:03
I waffled on this one, but am leaving it as assert
| |
422 return impl_.get()[i]; | |
423 } | |
424 element_type* get() const { return impl_.get(); } | |
425 | |
426 // Access to the deleter. | |
427 deleter_type& get_deleter() { return impl_.get_deleter(); } | |
428 const deleter_type& get_deleter() const { return impl_.get_deleter(); } | |
429 | |
430 // Allow scoped_ptr<element_type> to be used in boolean expressions, but not | |
431 // implicitly convertible to a real bool (which is dangerous). | |
432 private: | |
433 typedef base::internal::scoped_ptr_impl<element_type, deleter_type> | |
434 scoped_ptr::*Testable; | |
435 | |
436 public: | |
437 operator Testable() const { return impl_.get() ? &scoped_ptr::impl_ : NULL; } | |
438 | |
439 // Comparison operators. | |
440 // These return whether two scoped_ptr refer to the same object, not just to | |
441 // two different but equal objects. | |
442 bool operator==(element_type* array) const { return impl_.get() == array; } | |
443 bool operator!=(element_type* array) const { return impl_.get() != array; } | |
444 | |
445 // Swap two scoped pointers. | |
446 void swap(scoped_ptr& p2) { | |
447 impl_.swap(p2.impl_); | |
448 } | |
449 | |
450 // Release a pointer. | |
451 // The return value is the current pointer held by this object. | |
452 // If this object holds a NULL pointer, the return value is NULL. | |
453 // After this operation, this object will hold a NULL pointer, | |
454 // and will not own the object any more. | |
455 element_type* release() WARN_UNUSED_RESULT { | |
456 return impl_.release(); | |
457 } | |
458 | |
459 private: | |
460 // Force element_type to be a complete type. | |
461 enum { type_must_be_complete = sizeof(element_type) }; | |
462 | |
463 // Actually hold the data. | |
464 base::internal::scoped_ptr_impl<element_type, deleter_type> impl_; | |
465 | |
466 // Disable initialization from any type other than element_type*, by | |
467 // providing a constructor that matches such an initialization, but is | |
468 // private and has no definition. This is disabled because it is not safe to | |
469 // call delete[] on an array whose static type does not match its dynamic | |
470 // type. | |
471 template <typename U> | |
472 explicit scoped_ptr(U* array); | |
473 | |
474 // Disable reset() from any type other than element_type*, for the same | |
475 // reasons as the constructor above. | |
476 template <typename U> | |
477 void reset(U* array); | |
478 | |
479 // Forbid comparison of scoped_ptr types. If U != T, it totally | |
480 // doesn't make sense, and if U == T, it still doesn't make sense | |
481 // because you should never have the same object owned by two different | |
482 // scoped_ptrs. | |
483 template <class U> bool operator==(scoped_ptr<U> const& p2) const; | |
484 template <class U> bool operator!=(scoped_ptr<U> const& p2) const; | |
246 }; | 485 }; |
247 | 486 |
248 // Free functions | 487 // Free functions |
249 template <class C> | 488 template <class T, class D> |
250 void swap(scoped_ptr<C>& p1, scoped_ptr<C>& p2) { | 489 void swap(scoped_ptr<T, D>& p1, scoped_ptr<T, D>& p2) { |
251 p1.swap(p2); | 490 p1.swap(p2); |
252 } | 491 } |
253 | 492 |
254 template <class C> | 493 template <class T, class D> |
255 bool operator==(C* p1, const scoped_ptr<C>& p2) { | 494 bool operator==(T* p1, const scoped_ptr<T, D>& p2) { |
256 return p1 == p2.get(); | 495 return p1 == p2.get(); |
257 } | 496 } |
258 | 497 |
259 template <class C> | 498 template <class T, class D> |
260 bool operator!=(C* p1, const scoped_ptr<C>& p2) { | 499 bool operator!=(T* p1, const scoped_ptr<T, D>& p2) { |
261 return p1 != p2.get(); | 500 return p1 != p2.get(); |
262 } | 501 } |
263 | 502 |
503 // DEPRECATED: Use scoped_ptr<C[]> instead. | |
504 // | |
264 // scoped_array<C> is like scoped_ptr<C>, except that the caller must allocate | 505 // scoped_array<C> is like scoped_ptr<C>, except that the caller must allocate |
265 // with new [] and the destructor deletes objects with delete []. | 506 // with new [] and the destructor deletes objects with delete []. |
266 // | 507 // |
267 // As with scoped_ptr<C>, a scoped_array<C> either points to an object | 508 // As with scoped_ptr<C>, a scoped_array<C> either points to an object |
268 // or is NULL. A scoped_array<C> owns the object that it points to. | 509 // or is NULL. A scoped_array<C> owns the object that it points to. |
269 // scoped_array<T> is thread-compatible, and once you index into it, | 510 // scoped_array<T> is thread-compatible, and once you index into it, |
270 // the returned objects have only the thread safety guarantees of T. | 511 // the returned objects have only the thread safety guarantees of T. |
271 // | 512 // |
272 // Size: sizeof(scoped_array<C>) == sizeof(C*) | 513 // Size: sizeof(scoped_array<C>) == sizeof(C*) |
273 template <class C> | 514 template <class C> |
(...skipping 17 matching lines...) Expand all Loading... | |
291 | 532 |
292 // Destructor. If there is a C object, delete it. | 533 // Destructor. If there is a C object, delete it. |
293 // We don't need to test ptr_ == NULL because C++ does that for us. | 534 // We don't need to test ptr_ == NULL because C++ does that for us. |
294 ~scoped_array() { | 535 ~scoped_array() { |
295 enum { type_must_be_complete = sizeof(C) }; | 536 enum { type_must_be_complete = sizeof(C) }; |
296 delete[] array_; | 537 delete[] array_; |
297 } | 538 } |
298 | 539 |
299 // operator=. Move operator= for C++03 move emulation of this type. | 540 // operator=. Move operator= for C++03 move emulation of this type. |
300 scoped_array& operator=(RValue rhs) { | 541 scoped_array& operator=(RValue rhs) { |
301 swap(*rhs.object); | 542 reset(rhs.object->release()); |
302 return *this; | 543 return *this; |
303 } | 544 } |
304 | 545 |
305 // Reset. Deletes the current owned object, if any. | 546 // Reset. Deletes the current owned object, if any. |
306 // Then takes ownership of a new object, if given. | 547 // Then takes ownership of a new object, if given. |
307 // this->reset(this->get()) works. | 548 // this->reset(this->get()) works. |
308 void reset(C* p = NULL) { | 549 void reset(C* p = NULL) { |
309 if (p != array_) { | 550 if (p != array_) { |
310 enum { type_must_be_complete = sizeof(C) }; | 551 enum { type_must_be_complete = sizeof(C) }; |
311 delete[] array_; | 552 delete[] array_; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
373 template <class C> | 614 template <class C> |
374 bool operator==(C* p1, const scoped_array<C>& p2) { | 615 bool operator==(C* p1, const scoped_array<C>& p2) { |
375 return p1 == p2.get(); | 616 return p1 == p2.get(); |
376 } | 617 } |
377 | 618 |
378 template <class C> | 619 template <class C> |
379 bool operator!=(C* p1, const scoped_array<C>& p2) { | 620 bool operator!=(C* p1, const scoped_array<C>& p2) { |
380 return p1 != p2.get(); | 621 return p1 != p2.get(); |
381 } | 622 } |
382 | 623 |
383 // This class wraps the c library function free() in a class that can be | 624 // DEPRECATED: Use scoped_ptr<C, base::FreeDeleter> instead. |
384 // passed as a template argument to scoped_ptr_malloc below. | 625 // |
385 class ScopedPtrMallocFree { | |
386 public: | |
387 inline void operator()(void* x) const { | |
388 free(x); | |
389 } | |
390 }; | |
391 | |
392 // scoped_ptr_malloc<> is similar to scoped_ptr<>, but it accepts a | 626 // scoped_ptr_malloc<> is similar to scoped_ptr<>, but it accepts a |
393 // second template argument, the functor used to free the object. | 627 // second template argument, the functor used to free the object. |
394 | 628 |
395 template<class C, class FreeProc = ScopedPtrMallocFree> | 629 template<class C, class FreeProc = base::FreeDeleter> |
396 class scoped_ptr_malloc { | 630 class scoped_ptr_malloc { |
397 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr_malloc, RValue) | 631 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr_malloc, RValue) |
398 | 632 |
399 public: | 633 public: |
400 | 634 |
401 // The element type | 635 // The element type |
402 typedef C element_type; | 636 typedef C element_type; |
403 | 637 |
404 // Constructor. Defaults to initializing with NULL. | 638 // Constructor. Defaults to initializing with NULL. |
405 // There is no way to create an uninitialized scoped_ptr. | 639 // There is no way to create an uninitialized scoped_ptr. |
406 // The input parameter must be allocated with an allocator that matches the | 640 // The input parameter must be allocated with an allocator that matches the |
407 // Free functor. For the default Free functor, this is malloc, calloc, or | 641 // Free functor. For the default Free functor, this is malloc, calloc, or |
408 // realloc. | 642 // realloc. |
409 explicit scoped_ptr_malloc(C* p = NULL): ptr_(p) {} | 643 explicit scoped_ptr_malloc(C* p = NULL): ptr_(p) {} |
410 | 644 |
411 // Constructor. Move constructor for C++03 move emulation of this type. | 645 // Constructor. Move constructor for C++03 move emulation of this type. |
412 scoped_ptr_malloc(RValue rvalue) | 646 scoped_ptr_malloc(RValue rvalue) |
413 : ptr_(rvalue.object->release()) { | 647 : ptr_(rvalue.object->release()) { |
414 } | 648 } |
415 | 649 |
416 // Destructor. If there is a C object, call the Free functor. | 650 // Destructor. If there is a C object, call the Free functor. |
417 ~scoped_ptr_malloc() { | 651 ~scoped_ptr_malloc() { |
418 reset(); | 652 reset(); |
419 } | 653 } |
420 | 654 |
421 // operator=. Move operator= for C++03 move emulation of this type. | 655 // operator=. Move operator= for C++03 move emulation of this type. |
422 scoped_ptr_malloc& operator=(RValue rhs) { | 656 scoped_ptr_malloc& operator=(RValue rhs) { |
423 swap(*rhs.object); | 657 reset(rhs.object->release()); |
424 return *this; | 658 return *this; |
425 } | 659 } |
426 | 660 |
427 // Reset. Calls the Free functor on the current owned object, if any. | 661 // Reset. Calls the Free functor on the current owned object, if any. |
428 // Then takes ownership of a new object, if given. | 662 // Then takes ownership of a new object, if given. |
429 // this->reset(this->get()) works. | 663 // this->reset(this->get()) works. |
430 void reset(C* p = NULL) { | 664 void reset(C* p = NULL) { |
431 if (ptr_ != p) { | 665 if (ptr_ != p) { |
432 FreeProc free_proc; | 666 if (ptr_ != NULL) { |
433 free_proc(ptr_); | 667 FreeProc free_proc; |
668 free_proc(ptr_); | |
669 } | |
434 ptr_ = p; | 670 ptr_ = p; |
435 } | 671 } |
436 } | 672 } |
437 | 673 |
438 // Get the current object. | 674 // Get the current object. |
439 // operator* and operator-> will cause an assert() failure if there is | 675 // operator* and operator-> will cause an assert() failure if there is |
440 // no current object. | 676 // no current object. |
441 C& operator*() const { | 677 C& operator*() const { |
442 assert(ptr_ != NULL); | 678 assert(ptr_ != NULL); |
443 return *ptr_; | 679 return *ptr_; |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
515 | 751 |
516 // A function to convert T* into scoped_ptr<T> | 752 // A function to convert T* into scoped_ptr<T> |
517 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation | 753 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation |
518 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) | 754 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) |
519 template <typename T> | 755 template <typename T> |
520 scoped_ptr<T> make_scoped_ptr(T* ptr) { | 756 scoped_ptr<T> make_scoped_ptr(T* ptr) { |
521 return scoped_ptr<T>(ptr); | 757 return scoped_ptr<T>(ptr); |
522 } | 758 } |
523 | 759 |
524 #endif // BASE_MEMORY_SCOPED_PTR_H_ | 760 #endif // BASE_MEMORY_SCOPED_PTR_H_ |
OLD | NEW |