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 <alogrithm> // 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 C> | |
116 struct DefaultDeleter { | |
gromer
2012/10/18 20:45:20
I named this in camel-case because it's intended t
| |
117 DefaultDeleter() {} | |
118 template <typename U> DefaultDeleter(const DefaultDeleter<U>& other) { | |
119 // All default single-object deleters can trivially convert to one another. | |
120 } | |
121 inline void operator()(C* ptr) const { | |
122 enum { type_must_be_complete = sizeof(C) }; | |
123 delete ptr; | |
124 } | |
125 }; | |
126 | |
127 // Specialization of DefaultDeleter for array types. | |
128 template <class C> | |
129 struct DefaultDeleter<C[]> { | |
130 inline void operator()(C* ptr) const { | |
131 enum { type_must_be_complete = sizeof(C) }; | |
132 delete[] ptr; | |
133 } | |
134 | |
135 private: | |
136 // Disable this operator for any U != C 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 specialization. | |
165 template <class Element, class Deleter> | |
166 class scoped_ptr_impl { | |
167 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr_impl, RValue) | |
168 | |
169 public: | |
170 explicit scoped_ptr_impl(Element* p) : data_(p) { } | |
171 | |
172 template <typename U, typename V> | |
173 scoped_ptr_impl(scoped_ptr_impl<U, V> other) : data_(NULL) { | |
174 // We do not support move-only deleters. We could modify our move | |
175 // emulation to have a base::subtle::move() function that is an imperfect | |
176 // emulation of C++11 std::move(). But until there's a requirement, this | |
177 // is simpler. | |
178 reset(other.release()); | |
179 get_deleter() = other.get_deleter(); | |
180 } | |
181 | |
182 template <typename U, typename V> | |
183 const scoped_ptr_impl& operator=(scoped_ptr_impl<U, V> rhs) { | |
184 // See comment in move type-coverting constructor above regarding lack of | |
185 // support for move-only deleters. | |
186 reset(rhs.release()); | |
187 get_deleter() = rhs.get_deleter(); | |
188 return *this; | |
189 } | |
190 | |
191 scoped_ptr_impl(RValue rvalue) : data_(NULL) { | |
192 swap(*rvalue.object); | |
193 } | |
194 | |
195 ~scoped_ptr_impl() { | |
196 if (data_.ptr != NULL) { | |
197 get_deleter()(data_.ptr); | |
198 } | |
199 } | |
200 | |
201 void reset(Element* p) { | |
202 // This self-reset check is deprecated. | |
203 // this->reset(this->get()) currently works, but it is DEPRECATED, and | |
204 // will be removed once we verify that no one depends on it. | |
205 // | |
206 // TODO(ajwong): File bug for the deprecation and ordering issue below. | |
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 get_deleter()(data_.ptr); | |
215 } | |
216 data_.ptr = p; | |
217 } | |
218 } | |
219 | |
220 Element* get() const { return data_.ptr; } | |
221 | |
222 Deleter& get_deleter() { return data_; } | |
223 const Deleter& 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<Deleter&>(data_), static_cast<Deleter&>(p2.data_)); | |
231 swap(data_.ptr, p2.data_.ptr); | |
232 } | |
233 | |
234 Element* release() { | |
235 Element* retVal = data_.ptr; | |
236 data_.ptr = NULL; | |
237 return retVal; | |
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 Deleter | |
245 // member, while avoiding any space overhead for it when Deleter 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 Deleter { | |
249 explicit Data(Element* ptr_in) : ptr(ptr_in) {} | |
250 Element* ptr; | |
251 }; | |
252 | |
253 Data data_; | |
254 }; | |
255 | |
120 } // namespace internal | 256 } // namespace internal |
257 | |
121 } // namespace base | 258 } // namespace base |
122 | 259 |
123 // A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T> | 260 // 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). | 261 // automatically deletes the pointer it holds (if any). |
125 // That is, scoped_ptr<T> owns the T object that it points to. | 262 // 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. | 263 // 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 | 264 // Also like T*, scoped_ptr<T> is thread-compatible, and once you |
128 // dereference it, you get the thread safety guarantees of T. | 265 // dereference it, you get the thread safety guarantees of T. |
129 // | 266 // |
130 // The size of a scoped_ptr is small: | 267 // The size of a scoped_ptr is small: |
131 // sizeof(scoped_ptr<C>) == sizeof(C*) | 268 // sizeof(scoped_ptr<C>) == sizeof(C*) |
132 template <class C> | 269 template <class Element, class Deleter = base::DefaultDeleter<Element> > |
133 class scoped_ptr { | 270 class scoped_ptr { |
134 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue) | 271 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue) |
135 | 272 |
136 COMPILE_ASSERT(base::internal::IsNotRefCounted<C>::value, | 273 COMPILE_ASSERT(base::internal::IsNotRefCounted<Element>::value, |
137 C_is_refcounted_type_and_needs_scoped_refptr); | 274 Element_is_refcounted_type_and_needs_scoped_refptr); |
138 | 275 |
139 public: | 276 public: |
140 | 277 // The element and deleter types. |
141 // The element type | 278 typedef Element element_type; |
142 typedef C element_type; | 279 typedef Deleter deleter_type; |
143 | 280 |
144 // Constructor. Defaults to initializing with NULL. | 281 // Constructor. Defaults to initializing with NULL. |
145 // There is no way to create an uninitialized scoped_ptr. | 282 scoped_ptr() : impl_(NULL) { } |
146 // The input parameter must be allocated with new. | 283 |
147 explicit scoped_ptr(C* p = NULL) : ptr_(p) { } | 284 // Constructor. Takes ownership of p. |
285 explicit scoped_ptr(element_type* p) : impl_(p) { } | |
148 | 286 |
149 // Constructor. Allows construction from a scoped_ptr rvalue for a | 287 // Constructor. Allows construction from a scoped_ptr rvalue for a |
150 // convertible type. | 288 // convertible type and deleter. |
151 template <typename U> | 289 template <typename U, typename V> |
152 scoped_ptr(scoped_ptr<U> other) : ptr_(other.release()) { } | 290 scoped_ptr(scoped_ptr<U, V> other) : impl_(other.impl_.Pass()) { } |
153 | 291 |
154 // Constructor. Move constructor for C++03 move emulation of this type. | 292 // Constructor. Move constructor for C++03 move emulation of this type. |
155 scoped_ptr(RValue rvalue) | 293 scoped_ptr(RValue rvalue) : impl_(rvalue.object->release()) { } |
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 | 294 |
166 // operator=. Allows assignment from a scoped_ptr rvalue for a convertible | 295 // operator=. Allows assignment from a scoped_ptr rvalue for a convertible |
167 // type. | 296 // type and deleter. |
168 template <typename U> | 297 template <typename U, typename V> |
169 scoped_ptr& operator=(scoped_ptr<U> rhs) { | 298 scoped_ptr& operator=(scoped_ptr<U, V> rhs) { |
170 reset(rhs.release()); | 299 impl_ = rhs.impl_.Pass(); |
171 return *this; | 300 return *this; |
172 } | 301 } |
173 | 302 |
174 // operator=. Move operator= for C++03 move emulation of this type. | 303 // operator=. Move operator= for C++03 move emulation of this type. |
175 scoped_ptr& operator=(RValue rhs) { | 304 scoped_ptr& operator=(RValue rhs) { |
176 swap(*rhs->object); | 305 swap(*rhs->object); |
177 return *this; | 306 return *this; |
178 } | 307 } |
179 | 308 |
180 // Reset. Deletes the current owned object, if any. | 309 // Reset. Deletes the current owned object, if any. |
310 void reset() { impl_.reset(NULL); } | |
311 | |
312 // Reset. Deletes the currently owned object, if any. | |
181 // Then takes ownership of a new object, if given. | 313 // Then takes ownership of a new object, if given. |
182 // this->reset(this->get()) works. | 314 void reset(element_type* p) { 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 | 315 |
191 // Accessors to get the owned object. | 316 // Accessors to get the owned object. |
192 // operator* and operator-> will assert() if there is no current object. | 317 // operator* and operator-> will assert() if there is no current object. |
193 C& operator*() const { | 318 element_type& operator*() const { |
194 assert(ptr_ != NULL); | 319 assert(impl_.get() != NULL); |
195 return *ptr_; | 320 return *impl_.get(); |
196 } | 321 } |
197 C* operator->() const { | 322 element_type* operator->() const { |
198 assert(ptr_ != NULL); | 323 assert(impl_.get() != NULL); |
199 return ptr_; | 324 return impl_.get(); |
200 } | 325 } |
201 C* get() const { return ptr_; } | 326 element_type* get() const { return impl_.get(); } |
202 | 327 |
203 // Allow scoped_ptr<C> to be used in boolean expressions, but not | 328 // Access to the deleter. |
329 deleter_type& get_deleter() { return impl_.get_deleter(); } | |
330 const deleter_type& get_deleter() const { return impl_.get_deleter(); } | |
331 | |
332 // Allow scoped_ptr<element_type> to be used in boolean expressions, but not | |
204 // implicitly convertible to a real bool (which is dangerous). | 333 // implicitly convertible to a real bool (which is dangerous). |
205 typedef C* scoped_ptr::*Testable; | 334 private: |
206 operator Testable() const { return ptr_ ? &scoped_ptr::ptr_ : NULL; } | 335 typedef base::internal::scoped_ptr_impl<element_type, deleter_type> |
336 scoped_ptr::*Testable; | |
337 | |
338 public: | |
339 operator Testable() const { return impl_.get() ? &scoped_ptr::impl_ : NULL; } | |
207 | 340 |
208 // Comparison operators. | 341 // Comparison operators. |
209 // These return whether two scoped_ptr refer to the same object, not just to | 342 // These return whether two scoped_ptr refer to the same object, not just to |
210 // two different but equal objects. | 343 // two different but equal objects. |
211 bool operator==(C* p) const { return ptr_ == p; } | 344 bool operator==(element_type* p) const { return impl_.get() == p; } |
212 bool operator!=(C* p) const { return ptr_ != p; } | 345 bool operator!=(element_type* p) const { return impl_.get() != p; } |
213 | 346 |
214 // Swap two scoped pointers. | 347 // Swap two scoped pointers. |
215 void swap(scoped_ptr& p2) { | 348 void swap(scoped_ptr& p2) { |
216 C* tmp = ptr_; | 349 impl_.swap(p2.impl_); |
217 ptr_ = p2.ptr_; | |
218 p2.ptr_ = tmp; | |
219 } | 350 } |
220 | 351 |
221 // Release a pointer. | 352 // Release a pointer. |
222 // The return value is the current pointer held by this object. | 353 // The return value is the current pointer held by this object. |
223 // If this object holds a NULL pointer, the return value is NULL. | 354 // If this object holds a NULL pointer, the return value is NULL. |
224 // After this operation, this object will hold a NULL pointer, | 355 // After this operation, this object will hold a NULL pointer, |
225 // and will not own the object any more. | 356 // and will not own the object any more. |
226 C* release() WARN_UNUSED_RESULT { | 357 element_type* release() WARN_UNUSED_RESULT { |
227 C* retVal = ptr_; | 358 return impl_.release(); |
228 ptr_ = NULL; | |
229 return retVal; | |
230 } | 359 } |
231 | 360 |
361 // C++98 doesn't support functions templates with default parameters which | |
362 // makes it hard to write a PassAs() that understands converting the deleter | |
363 // while preserving simple calling semantics. | |
364 // | |
365 // Since there isn't a use case yet for PassAs() with custom deleters, we | |
366 // just ignore the custom deleter for now. | |
232 template <typename PassAsType> | 367 template <typename PassAsType> |
233 scoped_ptr<PassAsType> PassAs() { | 368 scoped_ptr<PassAsType> PassAs() { |
234 return scoped_ptr<PassAsType>(release()); | 369 return scoped_ptr<PassAsType>(Pass()); |
235 } | 370 } |
236 | 371 |
237 private: | 372 private: |
238 C* ptr_; | 373 // Needed to reach into |impl_| in the constructor. |
374 template <typename U, typename V> friend class scoped_ptr; | |
375 base::internal::scoped_ptr_impl<element_type, deleter_type> impl_; | |
239 | 376 |
240 // Forbid comparison of scoped_ptr types. If C2 != C, it totally doesn't | 377 // Forbid comparison of scoped_ptr types. If U != Element, it totally |
241 // make sense, and if C2 == C, it still doesn't make sense because you should | 378 // doesn't make sense, and if U == Element, it still doesn't make sense |
242 // never have the same object owned by two different scoped_ptrs. | 379 // because you should never have the same object owned by two different |
243 template <class C2> bool operator==(scoped_ptr<C2> const& p2) const; | 380 // scoped_ptrs. |
244 template <class C2> bool operator!=(scoped_ptr<C2> const& p2) const; | 381 template <class U> bool operator==(scoped_ptr<U> const& p2) const; |
382 template <class U> bool operator!=(scoped_ptr<U> const& p2) const; | |
383 }; | |
245 | 384 |
385 template <class Element, class Deleter> | |
386 class scoped_ptr<Element[], Deleter> { | |
387 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue) | |
388 | |
389 COMPILE_ASSERT(base::internal::IsNotRefCounted<Element>::value, | |
390 Element_is_refcounted_type_and_needs_scoped_refptr); | |
391 | |
392 public: | |
393 // The element and deleter types. | |
394 typedef Element element_type; | |
395 typedef Deleter deleter_type; | |
396 | |
397 // Constructor. Defaults to initializing with NULL. | |
398 // There is no way to create an uninitialized scoped_ptr. | |
399 scoped_ptr() : impl_(NULL) { } | |
400 | |
401 // Constructor. Stores the given array. Note that the argument's type | |
402 // must exactly match Element*. In particular: | |
403 // - it cannot be a pointer to a type derived from Element, because it is | |
404 // inherently unsafe to access an array through a pointer whose | |
405 // dynamic type does not match its static type. If you're doing this, | |
406 // fix your code. | |
407 // - it cannot be NULL, because NULL is an integral expression, not a | |
408 // pointer to Element. Use the no-argument version instead of explicitly | |
409 // passing NULL. | |
410 // - it cannot be const-qualified differently from Element. You can work | |
411 // around this using implicit_cast (from base/casts.h): | |
412 // | |
413 // int* i; | |
414 // scoped_ptr<const int[]> arr(implicit_cast<const int[]>(i)); | |
415 // | |
416 // TODO(ajwong): Find citations for the above. Also see if we want to keep | |
417 // the implicit_cast<> comment. | |
418 explicit scoped_ptr(element_type* array) : impl_(array) { } | |
419 | |
420 // Constructor. Move constructor for C++03 move emulation of this type. | |
421 scoped_ptr(RValue rvalue) : impl_(rvalue.object->release()) { } | |
422 | |
423 // operator=. Move operator= for C++03 move emulation of this type. | |
424 scoped_ptr& operator=(RValue rhs) { | |
425 swap(*rhs->object); | |
426 return *this; | |
427 } | |
428 | |
429 // Reset. Deletes the currently owned array, if any. | |
430 void reset() { impl_.reset(NULL); } | |
431 | |
432 // Reset. Deletes the currently owned array, if any. | |
433 // Then takes ownership of a new object, if given. | |
434 void reset(element_type* array) { impl_.reset(array); } | |
435 | |
436 // Accessors to get the owned array. | |
437 // operator* and operator-> will assert() if there is no current array. | |
438 element_type& operator[](size_t i) const { | |
439 assert(impl_.get() != NULL); | |
440 return impl_.get()[i]; | |
441 } | |
442 element_type* get() const { return impl_.get(); } | |
443 | |
444 // Access to the deleter. | |
445 deleter_type& get_deleter() { return impl_.get_deleter(); } | |
446 const deleter_type& get_deleter() const { return impl_.get_deleter(); } | |
447 | |
448 // Allow scoped_ptr<element_type> to be used in boolean expressions, but not | |
449 // implicitly convertible to a real bool (which is dangerous). | |
450 private: | |
451 typedef base::internal::scoped_ptr_impl<element_type, deleter_type> | |
452 scoped_ptr::*Testable; | |
453 | |
454 public: | |
455 operator Testable() const { return impl_.get() ? &scoped_ptr::impl_ : NULL; } | |
456 | |
457 // Comparison operators. | |
458 // These return whether two scoped_ptr refer to the same object, not just to | |
459 // two different but equal objects. | |
460 bool operator==(element_type* array) const { return impl_.get() == array; } | |
461 bool operator!=(element_type* array) const { return impl_.get() != array; } | |
462 | |
463 // Swap two scoped pointers. | |
464 void swap(scoped_ptr& p2) { | |
465 impl_.swap(p2.impl_); | |
466 } | |
467 | |
468 // Release a pointer. | |
469 // The return value is the current pointer held by this object. | |
470 // If this object holds a NULL pointer, the return value is NULL. | |
471 // After this operation, this object will hold a NULL pointer, | |
472 // and will not own the object any more. | |
473 element_type* release() WARN_UNUSED_RESULT { | |
474 return impl_.release(); | |
475 } | |
476 | |
477 private: | |
478 // Force element_type to be a complete type. | |
479 enum { type_must_be_complete = sizeof(element_type) }; | |
480 | |
481 // Actually hold the data. | |
482 base::internal::scoped_ptr_impl<element_type, deleter_type> impl_; | |
483 | |
484 // Disable initialization from any type other than element_type*, by | |
485 // providing a constructor that matches such an initialization, but is | |
486 // private and has no definition. This is disabled because it is not safe to | |
487 // call delete[] on an array whose static type does not match its dynamic | |
488 // type. | |
489 template <typename T> | |
490 explicit scoped_ptr(T* array); | |
491 | |
492 // Disable reset() from any type other than element_type*, for the same | |
493 // reasons as the constructor above. | |
494 template <typename T> | |
495 void reset(T* array); | |
496 | |
497 // Forbid comparison of scoped_ptr types. If U != Element, it totally | |
498 // doesn't make sense, and if U == Element, it still doesn't make sense | |
499 // because you should never have the same object owned by two different | |
500 // scoped_ptrs. | |
501 template <class U> bool operator==(scoped_ptr<U> const& p2) const; | |
502 template <class U> bool operator!=(scoped_ptr<U> const& p2) const; | |
246 }; | 503 }; |
247 | 504 |
248 // Free functions | 505 // Free functions |
249 template <class C> | 506 template <class C, class D> |
250 void swap(scoped_ptr<C>& p1, scoped_ptr<C>& p2) { | 507 void swap(scoped_ptr<C, D>& p1, scoped_ptr<C, D>& p2) { |
251 p1.swap(p2); | 508 p1.swap(p2); |
252 } | 509 } |
253 | 510 |
254 template <class C> | 511 template <class C, class D> |
255 bool operator==(C* p1, const scoped_ptr<C>& p2) { | 512 bool operator==(C* p1, const scoped_ptr<C, D>& p2) { |
256 return p1 == p2.get(); | 513 return p1 == p2.get(); |
257 } | 514 } |
258 | 515 |
259 template <class C> | 516 template <class C, class D> |
260 bool operator!=(C* p1, const scoped_ptr<C>& p2) { | 517 bool operator!=(C* p1, const scoped_ptr<C, D>& p2) { |
261 return p1 != p2.get(); | 518 return p1 != p2.get(); |
262 } | 519 } |
263 | 520 |
264 // scoped_array<C> is like scoped_ptr<C>, except that the caller must allocate | 521 // scoped_array<C> is like scoped_ptr<C>, except that the caller must allocate |
265 // with new [] and the destructor deletes objects with delete []. | 522 // with new [] and the destructor deletes objects with delete []. |
266 // | 523 // |
267 // As with scoped_ptr<C>, a scoped_array<C> either points to an object | 524 // 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. | 525 // 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, | 526 // scoped_array<T> is thread-compatible, and once you index into it, |
270 // the returned objects have only the thread safety guarantees of T. | 527 // the returned objects have only the thread safety guarantees of T. |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
515 | 772 |
516 // A function to convert T* into scoped_ptr<T> | 773 // A function to convert T* into scoped_ptr<T> |
517 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation | 774 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation |
518 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) | 775 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) |
519 template <typename T> | 776 template <typename T> |
520 scoped_ptr<T> make_scoped_ptr(T* ptr) { | 777 scoped_ptr<T> make_scoped_ptr(T* ptr) { |
521 return scoped_ptr<T>(ptr); | 778 return scoped_ptr<T>(ptr); |
522 } | 779 } |
523 | 780 |
524 #endif // BASE_MEMORY_SCOPED_PTR_H_ | 781 #endif // BASE_MEMORY_SCOPED_PTR_H_ |
OLD | NEW |