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
3 // found in the LICENSE file.
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
7 // end of a scope. There are two main classes you will use, which correspond
8 // to the operators new/delete and new[]/delete[].
10 // Example usage (scoped_ptr):
12 // scoped_ptr<Foo> foo(new Foo("wee"));
13 // } // foo goes out of scope, releasing the pointer with it.
16 // scoped_ptr<Foo> foo; // No pointer managed.
17 // foo.reset(new Foo("wee")); // Now a pointer is managed.
18 // foo.reset(new Foo("wee2")); // Foo("wee") was destroyed.
19 // foo.reset(new Foo("wee3")); // Foo("wee2") was destroyed.
20 // foo->Method(); // Foo::Method() called.
21 // foo.get()->Method(); // Foo::Method() called.
22 // SomeFunc(foo.release()); // SomeFunc takes ownership, foo no longer
23 // // manages a pointer.
24 // foo.reset(new Foo("wee4")); // foo manages a pointer again.
25 // foo.reset(); // Foo("wee4") destroyed, foo no longer
26 // // manages a pointer.
27 // } // foo wasn't managing a pointer, so nothing was destroyed.
29 // Example usage (scoped_array):
31 // scoped_array<Foo> foo(new Foo[100]);
32 // foo.get()->Method(); // Foo::Method on the 0th element.
33 // foo[10].Method(); // Foo::Method on the 10th element.
36 // These scopers also implement part of the functionality of C++11 unique_ptr
37 // in that they are "movable but not copyable." You can use the scopers in
38 // the parameter and return types of functions to signify ownership transfer
39 // in to and out of a function. When calling a function that has a scoper
40 // as the argument type, it must be called with the result of an analogous
41 // scoper's Pass() function or another function that generates a temporary;
42 // passing by copy will NOT work. Here is an example using scoped_ptr:
44 // void TakesOwnership(scoped_ptr<Foo> arg) {
45 // // Do something with arg
47 // scoped_ptr<Foo> CreateFoo() {
48 // // No need for calling Pass() because we are constructing a temporary
49 // // for the return value.
50 // return scoped_ptr<Foo>(new Foo("new"));
52 // scoped_ptr<Foo> PassThru(scoped_ptr<Foo> arg) {
57 // scoped_ptr<Foo> ptr(new Foo("yay")); // ptr manages Foo("yay").
58 // TakesOwnership(ptr.Pass()); // ptr no longer owns Foo("yay").
59 // scoped_ptr<Foo> ptr2 = CreateFoo(); // ptr2 owns the return Foo.
60 // scoped_ptr<Foo> ptr3 = // ptr3 now owns what was in ptr2.
61 // PassThru(ptr2.Pass()); // ptr2 is correspondingly NULL.
64 // Notice that if you do not call Pass() when returning from PassThru(), or
65 // when invoking TakesOwnership(), the code will not compile because scopers
66 // are not copyable; they only implement move semantics which require calling
67 // the Pass() function to signify a destructive transfer of state. CreateFoo()
68 // is different though because we are constructing a temporary on the return
69 // line and thus can avoid needing to call Pass().
71 // Pass() properly handles upcast in assignment, i.e. you can assign
72 // scoped_ptr<Child> to scoped_ptr<Parent>:
74 // scoped_ptr<Foo> foo(new Foo());
75 // scoped_ptr<FooParent> parent = foo.Pass();
77 // PassAs<>() should be used to upcast return value in return statement:
79 // scoped_ptr<Foo> CreateFoo() {
80 // scoped_ptr<FooChild> result(new FooChild());
81 // return result.PassAs<Foo>();
84 // Note that PassAs<>() is implemented only for scoped_ptr, but not for
85 // scoped_array. This is because casting array pointers may not be safe.
87 #ifndef BASE_MEMORY_SCOPED_PTR_H_
88 #define BASE_MEMORY_SCOPED_PTR_H_
90 // This is an implementation designed to match the anticipated future TR2
91 // implementation of the scoped_ptr class, and its closely-related brethren,
92 // scoped_array, scoped_ptr_malloc.
98 #include <algorithm> // For std::swap().
100 #include "base/basictypes.h"
101 #include "base/compiler_specific.h"
102 #include "base/move.h"
103 #include "base/template_util.h"
108 class RefCountedBase
;
109 class RefCountedThreadSafeBase
;
110 } // namespace subtle
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>.
116 struct DefaultDeleter
{
118 template <typename U
> DefaultDeleter(const DefaultDeleter
<U
>& other
) {
119 // IMPLEMENTATION NOTE: C++11 20.7.1.1.2p2 only provides this constructor
120 // if U* is implicitly convertible to T* and U is not an array type.
122 // Correct implementation should use SFINAE to disable this
123 // constructor. However, since there are no other 1-argument constructors,
124 // using a COMPILE_ASSERT() based on is_convertible<> and requiring
125 // complete types is simpler and will cause compile failures for equivalent
128 // Note, the is_convertible<U*, T*> check also ensures that U is not an
129 // array. T is guaranteed to be a non-array, so any U* where U is an array
130 // cannot convert to T*.
131 enum { T_must_be_complete
= sizeof(T
) };
132 enum { U_must_be_complete
= sizeof(U
) };
133 COMPILE_ASSERT((base::is_convertible
<U
*, T
*>::value
),
134 U_ptr_must_implicitly_convert_to_T_ptr
);
136 inline void operator()(T
* ptr
) const {
137 enum { type_must_be_complete
= sizeof(T
) };
142 // Specialization of DefaultDeleter for array types.
144 struct DefaultDeleter
<T
[]> {
145 inline void operator()(T
* ptr
) const {
146 enum { type_must_be_complete
= sizeof(T
) };
151 // Disable this operator for any U != T because it is undefined to execute
152 // an array delete when the static type of the array mismatches the dynamic
156 // C++98 [expr.delete]p3
157 // http://cplusplus.github.com/LWG/lwg-defects.html#938
158 template <typename U
> void operator()(U
* array
) const;
161 template <class T
, int n
>
162 struct DefaultDeleter
<T
[n
]> {
163 // Never allow someone to declare something like scoped_ptr<int[10]>.
164 COMPILE_ASSERT(sizeof(T
) == -1, do_not_use_array_with_size_as_type
);
167 // Function object which invokes 'free' on its parameter, which must be
168 // a pointer. Can be used to store malloc-allocated pointers in scoped_ptr:
170 // scoped_ptr<int, base::FreeDeleter> foo_ptr(
171 // static_cast<int*>(malloc(sizeof(int))));
173 inline void operator()(void* ptr
) const {
180 template <typename T
> struct IsNotRefCounted
{
182 value
= !base::is_convertible
<T
*, base::subtle::RefCountedBase
*>::value
&&
183 !base::is_convertible
<T
*, base::subtle::RefCountedThreadSafeBase
*>::
188 // Minimal implementation of the core logic of scoped_ptr, suitable for
189 // reuse in both scoped_ptr and its specializations.
190 template <class T
, class D
>
191 class scoped_ptr_impl
{
193 explicit scoped_ptr_impl(T
* p
) : data_(p
) { }
195 // Initializer for deleters that have data parameters.
196 scoped_ptr_impl(T
* p
, const D
& d
) : data_(p
, d
) {}
198 // Templated constructor that destructively takes the value from another
200 template <typename U
, typename V
>
201 scoped_ptr_impl(scoped_ptr_impl
<U
, V
>* other
)
202 : data_(other
->release(), other
->get_deleter()) {
203 // We do not support move-only deleters. We could modify our move
204 // emulation to have base::subtle::move() and base::subtle::forward()
205 // functions that are imperfect emulations of their C++11 equivalents,
206 // but until there's a requirement, just assume deleters are copyable.
209 template <typename U
, typename V
>
210 void TakeState(scoped_ptr_impl
<U
, V
>* other
) {
211 // See comment in templated constructor above regarding lack of support
212 // for move-only deleters.
213 reset(other
->release());
214 get_deleter() = other
->get_deleter();
218 if (data_
.ptr
!= NULL
) {
219 // Not using get_deleter() saves one function call in non-optimized
221 static_cast<D
&>(data_
)(data_
.ptr
);
226 // This is a self-reset, which is no longer allowed: http://crbug.com/162971
227 if (p
!= NULL
&& p
== data_
.ptr
)
230 // Note that running data_.ptr = p can lead to undefined behavior if
231 // get_deleter()(get()) deletes this. In order to pevent this, reset()
232 // should update the stored pointer before deleting its old value.
234 // However, changing reset() to use that behavior may cause current code to
235 // break in unexpected ways. If the destruction of the owned object
236 // dereferences the scoped_ptr when it is destroyed by a call to reset(),
237 // then it will incorrectly dispatch calls to |p| rather than the original
238 // value of |data_.ptr|.
240 // During the transition period, set the stored pointer to NULL while
241 // deleting the object. Eventually, this safety check will be removed to
242 // prevent the scenario initially described from occuring and
243 // http://crbug.com/176091 can be closed.
247 static_cast<D
&>(data_
)(old
);
251 T
* get() const { return data_
.ptr
; }
253 D
& get_deleter() { return data_
; }
254 const D
& get_deleter() const { return data_
; }
256 void swap(scoped_ptr_impl
& p2
) {
257 // Standard swap idiom: 'using std::swap' ensures that std::swap is
258 // present in the overload set, but we call swap unqualified so that
259 // any more-specific overloads can be used, if available.
261 swap(static_cast<D
&>(data_
), static_cast<D
&>(p2
.data_
));
262 swap(data_
.ptr
, p2
.data_
.ptr
);
266 T
* old_ptr
= data_
.ptr
;
272 // Needed to allow type-converting constructor.
273 template <typename U
, typename V
> friend class scoped_ptr_impl
;
275 // Use the empty base class optimization to allow us to have a D
276 // member, while avoiding any space overhead for it when D is an
277 // empty class. See e.g. http://www.cantrip.org/emptyopt.html for a good
278 // discussion of this technique.
279 struct Data
: public D
{
280 explicit Data(T
* ptr_in
) : ptr(ptr_in
) {}
281 Data(T
* ptr_in
, const D
& other
) : D(other
), ptr(ptr_in
) {}
287 DISALLOW_COPY_AND_ASSIGN(scoped_ptr_impl
);
290 } // namespace internal
294 // A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T>
295 // automatically deletes the pointer it holds (if any).
296 // That is, scoped_ptr<T> owns the T object that it points to.
297 // Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object.
298 // Also like T*, scoped_ptr<T> is thread-compatible, and once you
299 // dereference it, you get the thread safety guarantees of T.
301 // The size of scoped_ptr is small. On most compilers, when using the
302 // DefaultDeleter, sizeof(scoped_ptr<T>) == sizeof(T*). Custom deleters will
303 // increase the size proportional to whatever state they need to have. See
304 // comments inside scoped_ptr_impl<> for details.
306 // Current implementation targets having a strict subset of C++11's
307 // unique_ptr<> features. Known deficiencies include not supporting move-only
308 // deleteres, function pointers as deleters, and deleters with reference
310 template <class T
, class D
= base::DefaultDeleter
<T
> >
312 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr
, RValue
)
314 COMPILE_ASSERT(base::internal::IsNotRefCounted
<T
>::value
,
315 T_is_refcounted_type_and_needs_scoped_refptr
);
318 // The element and deleter types.
319 typedef T element_type
;
320 typedef D deleter_type
;
322 // Constructor. Defaults to initializing with NULL.
323 scoped_ptr() : impl_(NULL
) { }
325 // Constructor. Takes ownership of p.
326 explicit scoped_ptr(element_type
* p
) : impl_(p
) { }
328 // Constructor. Allows initialization of a stateful deleter.
329 scoped_ptr(element_type
* p
, const D
& d
) : impl_(p
, d
) { }
331 // Constructor. Allows construction from a scoped_ptr rvalue for a
332 // convertible type and deleter.
334 // IMPLEMENTATION NOTE: C++11 unique_ptr<> keeps this constructor distinct
335 // from the normal move constructor. By C++11 20.7.1.2.1.21, this constructor
336 // has different post-conditions if D is a reference type. Since this
337 // implementation does not support deleters with reference type,
338 // we do not need a separate move constructor allowing us to avoid one
339 // use of SFINAE. You only need to care about this if you modify the
340 // implementation of scoped_ptr.
341 template <typename U
, typename V
>
342 scoped_ptr(scoped_ptr
<U
, V
> other
) : impl_(&other
.impl_
) {
343 COMPILE_ASSERT(!base::is_array
<U
>::value
, U_cannot_be_an_array
);
346 // Constructor. Move constructor for C++03 move emulation of this type.
347 scoped_ptr(RValue rvalue
) : impl_(&rvalue
.object
->impl_
) { }
349 // operator=. Allows assignment from a scoped_ptr rvalue for a convertible
352 // IMPLEMENTATION NOTE: C++11 unique_ptr<> keeps this operator= distinct from
353 // the normal move assignment operator. By C++11 20.7.1.2.3.4, this templated
354 // form has different requirements on for move-only Deleters. Since this
355 // implementation does not support move-only Deleters, we do not need a
356 // separate move assignment operator allowing us to avoid one use of SFINAE.
357 // You only need to care about this if you modify the implementation of
359 template <typename U
, typename V
>
360 scoped_ptr
& operator=(scoped_ptr
<U
, V
> rhs
) {
361 COMPILE_ASSERT(!base::is_array
<U
>::value
, U_cannot_be_an_array
);
362 impl_
.TakeState(&rhs
.impl_
);
366 // Reset. Deletes the currently owned object, if any.
367 // Then takes ownership of a new object, if given.
368 void reset(element_type
* p
= NULL
) { impl_
.reset(p
); }
370 // Accessors to get the owned object.
371 // operator* and operator-> will assert() if there is no current object.
372 element_type
& operator*() const {
373 assert(impl_
.get() != NULL
);
376 element_type
* operator->() const {
377 assert(impl_
.get() != NULL
);
380 element_type
* get() const { return impl_
.get(); }
382 // Access to the deleter.
383 deleter_type
& get_deleter() { return impl_
.get_deleter(); }
384 const deleter_type
& get_deleter() const { return impl_
.get_deleter(); }
386 // Allow scoped_ptr<element_type> to be used in boolean expressions, but not
387 // implicitly convertible to a real bool (which is dangerous).
389 typedef base::internal::scoped_ptr_impl
<element_type
, deleter_type
>
390 scoped_ptr::*Testable
;
393 operator Testable() const { return impl_
.get() ? &scoped_ptr::impl_
: NULL
; }
395 // Comparison operators.
396 // These return whether two scoped_ptr refer to the same object, not just to
397 // two different but equal objects.
398 bool operator==(const element_type
* p
) const { return impl_
.get() == p
; }
399 bool operator!=(const element_type
* p
) const { return impl_
.get() != p
; }
401 // Swap two scoped pointers.
402 void swap(scoped_ptr
& p2
) {
403 impl_
.swap(p2
.impl_
);
406 // Release a pointer.
407 // The return value is the current pointer held by this object.
408 // If this object holds a NULL pointer, the return value is NULL.
409 // After this operation, this object will hold a NULL pointer,
410 // and will not own the object any more.
411 element_type
* release() WARN_UNUSED_RESULT
{
412 return impl_
.release();
415 // C++98 doesn't support functions templates with default parameters which
416 // makes it hard to write a PassAs() that understands converting the deleter
417 // while preserving simple calling semantics.
419 // Until there is a use case for PassAs() with custom deleters, just ignore
420 // the custom deleter.
421 template <typename PassAsType
>
422 scoped_ptr
<PassAsType
> PassAs() {
423 return scoped_ptr
<PassAsType
>(Pass());
427 // Needed to reach into |impl_| in the constructor.
428 template <typename U
, typename V
> friend class scoped_ptr
;
429 base::internal::scoped_ptr_impl
<element_type
, deleter_type
> impl_
;
431 // Forbid comparison of scoped_ptr types. If U != T, it totally
432 // doesn't make sense, and if U == T, it still doesn't make sense
433 // because you should never have the same object owned by two different
435 template <class U
> bool operator==(scoped_ptr
<U
> const& p2
) const;
436 template <class U
> bool operator!=(scoped_ptr
<U
> const& p2
) const;
439 template <class T
, class D
>
440 class scoped_ptr
<T
[], D
> {
441 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr
, RValue
)
444 // The element and deleter types.
445 typedef T element_type
;
446 typedef D deleter_type
;
448 // Constructor. Defaults to initializing with NULL.
449 scoped_ptr() : impl_(NULL
) { }
451 // Constructor. Stores the given array. Note that the argument's type
452 // must exactly match T*. In particular:
453 // - it cannot be a pointer to a type derived from T, because it is
454 // inherently unsafe in the general case to access an array through a
455 // pointer whose dynamic type does not match its static type (eg., if
456 // T and the derived types had different sizes access would be
457 // incorrectly calculated). Deletion is also always undefined
458 // (C++98 [expr.delete]p3). If you're doing this, fix your code.
459 // - it cannot be NULL, because NULL is an integral expression, not a
460 // pointer to T. Use the no-argument version instead of explicitly
462 // - it cannot be const-qualified differently from T per unique_ptr spec
463 // (http://cplusplus.github.com/LWG/lwg-active.html#2118). Users wanting
464 // to work around this may use implicit_cast<const T*>().
465 // However, because of the first bullet in this comment, users MUST
466 // NOT use implicit_cast<Base*>() to upcast the static type of the array.
467 explicit scoped_ptr(element_type
* array
) : impl_(array
) { }
469 // Constructor. Move constructor for C++03 move emulation of this type.
470 scoped_ptr(RValue rvalue
) : impl_(&rvalue
.object
->impl_
) { }
472 // operator=. Move operator= for C++03 move emulation of this type.
473 scoped_ptr
& operator=(RValue rhs
) {
474 impl_
.TakeState(&rhs
.object
->impl_
);
478 // Reset. Deletes the currently owned array, if any.
479 // Then takes ownership of a new object, if given.
480 void reset(element_type
* array
= NULL
) { impl_
.reset(array
); }
482 // Accessors to get the owned array.
483 element_type
& operator[](size_t i
) const {
484 assert(impl_
.get() != NULL
);
485 return impl_
.get()[i
];
487 element_type
* get() const { return impl_
.get(); }
489 // Access to the deleter.
490 deleter_type
& get_deleter() { return impl_
.get_deleter(); }
491 const deleter_type
& get_deleter() const { return impl_
.get_deleter(); }
493 // Allow scoped_ptr<element_type> to be used in boolean expressions, but not
494 // implicitly convertible to a real bool (which is dangerous).
496 typedef base::internal::scoped_ptr_impl
<element_type
, deleter_type
>
497 scoped_ptr::*Testable
;
500 operator Testable() const { return impl_
.get() ? &scoped_ptr::impl_
: NULL
; }
502 // Comparison operators.
503 // These return whether two scoped_ptr refer to the same object, not just to
504 // two different but equal objects.
505 bool operator==(element_type
* array
) const { return impl_
.get() == array
; }
506 bool operator!=(element_type
* array
) const { return impl_
.get() != array
; }
508 // Swap two scoped pointers.
509 void swap(scoped_ptr
& p2
) {
510 impl_
.swap(p2
.impl_
);
513 // Release a pointer.
514 // The return value is the current pointer held by this object.
515 // If this object holds a NULL pointer, the return value is NULL.
516 // After this operation, this object will hold a NULL pointer,
517 // and will not own the object any more.
518 element_type
* release() WARN_UNUSED_RESULT
{
519 return impl_
.release();
523 // Force element_type to be a complete type.
524 enum { type_must_be_complete
= sizeof(element_type
) };
526 // Actually hold the data.
527 base::internal::scoped_ptr_impl
<element_type
, deleter_type
> impl_
;
529 // Disable initialization from any type other than element_type*, by
530 // providing a constructor that matches such an initialization, but is
531 // private and has no definition. This is disabled because it is not safe to
532 // call delete[] on an array whose static type does not match its dynamic
534 template <typename U
> explicit scoped_ptr(U
* array
);
535 explicit scoped_ptr(int disallow_construction_from_null
);
537 // Disable reset() from any type other than element_type*, for the same
538 // reasons as the constructor above.
539 template <typename U
> void reset(U
* array
);
540 void reset(int disallow_reset_from_null
);
542 // Forbid comparison of scoped_ptr types. If U != T, it totally
543 // doesn't make sense, and if U == T, it still doesn't make sense
544 // because you should never have the same object owned by two different
546 template <class U
> bool operator==(scoped_ptr
<U
> const& p2
) const;
547 template <class U
> bool operator!=(scoped_ptr
<U
> const& p2
) const;
551 template <class T
, class D
>
552 void swap(scoped_ptr
<T
, D
>& p1
, scoped_ptr
<T
, D
>& p2
) {
556 template <class T
, class D
>
557 bool operator==(T
* p1
, const scoped_ptr
<T
, D
>& p2
) {
558 return p1
== p2
.get();
561 template <class T
, class D
>
562 bool operator!=(T
* p1
, const scoped_ptr
<T
, D
>& p2
) {
563 return p1
!= p2
.get();
566 // DEPRECATED: Use scoped_ptr<C[]> instead.
568 // scoped_array<C> is like scoped_ptr<C>, except that the caller must allocate
569 // with new [] and the destructor deletes objects with delete [].
571 // As with scoped_ptr<C>, a scoped_array<C> either points to an object
572 // or is NULL. A scoped_array<C> owns the object that it points to.
573 // scoped_array<T> is thread-compatible, and once you index into it,
574 // the returned objects have only the thread safety guarantees of T.
576 // Size: sizeof(scoped_array<C>) == sizeof(C*)
579 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_array
, RValue
)
584 typedef C element_type
;
586 // Constructor. Defaults to initializing with NULL.
587 // There is no way to create an uninitialized scoped_array.
588 // The input parameter must be allocated with new [].
589 explicit scoped_array(C
* p
= NULL
) : array_(p
) { }
591 // Constructor. Move constructor for C++03 move emulation of this type.
592 scoped_array(RValue rvalue
)
593 : array_(rvalue
.object
->release()) {
596 // Destructor. If there is a C object, delete it.
597 // We don't need to test ptr_ == NULL because C++ does that for us.
599 enum { type_must_be_complete
= sizeof(C
) };
603 // operator=. Move operator= for C++03 move emulation of this type.
604 scoped_array
& operator=(RValue rhs
) {
605 reset(rhs
.object
->release());
609 // Reset. Deletes the current owned object, if any.
610 void reset(C
* p
= NULL
) {
611 // This is a self-reset, which is no longer allowed: http://crbug.com/162971
612 if (p
!= NULL
&& p
== array_
)
622 // Get one element of the current object.
623 // Will assert() if there is no current object, or index i is negative.
624 C
& operator[](ptrdiff_t i
) const {
626 assert(array_
!= NULL
);
630 // Get a pointer to the zeroth element of the current object.
631 // If there is no current object, return NULL.
636 // Allow scoped_array<C> to be used in boolean expressions, but not
637 // implicitly convertible to a real bool (which is dangerous).
638 typedef C
* scoped_array::*Testable
;
639 operator Testable() const { return array_
? &scoped_array::array_
: NULL
; }
641 // Comparison operators.
642 // These return whether two scoped_array refer to the same object, not just to
643 // two different but equal objects.
644 bool operator==(C
* p
) const { return array_
== p
; }
645 bool operator!=(C
* p
) const { return array_
!= p
; }
647 // Swap two scoped arrays.
648 void swap(scoped_array
& p2
) {
655 // The return value is the current pointer held by this object.
656 // If this object holds a NULL pointer, the return value is NULL.
657 // After this operation, this object will hold a NULL pointer,
658 // and will not own the object any more.
659 C
* release() WARN_UNUSED_RESULT
{
668 // Disable initialization from any type other than C*, by providing a
669 // constructor that matches such an initialization, but is private and has no
670 // definition. This is disabled because it is not safe to call delete[] on an
671 // array whose static type does not match its dynamic type.
672 template <typename C2
> explicit scoped_array(C2
* array
);
673 explicit scoped_array(int disallow_construction_from_null
);
675 // Disable reset() from any type other than C*, for the same reasons as the
676 // constructor above.
677 template <typename C2
> void reset(C2
* array
);
678 void reset(int disallow_reset_from_null
);
680 // Forbid comparison of different scoped_array types.
681 template <class C2
> bool operator==(scoped_array
<C2
> const& p2
) const;
682 template <class C2
> bool operator!=(scoped_array
<C2
> const& p2
) const;
687 void swap(scoped_array
<C
>& p1
, scoped_array
<C
>& p2
) {
692 bool operator==(C
* p1
, const scoped_array
<C
>& p2
) {
693 return p1
== p2
.get();
697 bool operator!=(C
* p1
, const scoped_array
<C
>& p2
) {
698 return p1
!= p2
.get();
701 // DEPRECATED: Use scoped_ptr<C, base::FreeDeleter> instead.
703 // scoped_ptr_malloc<> is similar to scoped_ptr<>, but it accepts a
704 // second template argument, the functor used to free the object.
706 template<class C
, class FreeProc
= base::FreeDeleter
>
707 class scoped_ptr_malloc
{
708 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr_malloc
, RValue
)
713 typedef C element_type
;
715 // Constructor. Defaults to initializing with NULL.
716 // There is no way to create an uninitialized scoped_ptr.
717 // The input parameter must be allocated with an allocator that matches the
718 // Free functor. For the default Free functor, this is malloc, calloc, or
720 explicit scoped_ptr_malloc(C
* p
= NULL
): ptr_(p
) {}
722 // Constructor. Move constructor for C++03 move emulation of this type.
723 scoped_ptr_malloc(RValue rvalue
)
724 : ptr_(rvalue
.object
->release()) {
727 // Destructor. If there is a C object, call the Free functor.
728 ~scoped_ptr_malloc() {
732 // operator=. Move operator= for C++03 move emulation of this type.
733 scoped_ptr_malloc
& operator=(RValue rhs
) {
734 reset(rhs
.object
->release());
738 // Reset. Calls the Free functor on the current owned object, if any.
739 // Then takes ownership of a new object, if given.
740 // this->reset(this->get()) works.
741 void reset(C
* p
= NULL
) {
751 // Get the current object.
752 // operator* and operator-> will cause an assert() failure if there is
753 // no current object.
754 C
& operator*() const {
755 assert(ptr_
!= NULL
);
759 C
* operator->() const {
760 assert(ptr_
!= NULL
);
768 // Allow scoped_ptr_malloc<C> to be used in boolean expressions, but not
769 // implicitly convertible to a real bool (which is dangerous).
770 typedef C
* scoped_ptr_malloc::*Testable
;
771 operator Testable() const { return ptr_
? &scoped_ptr_malloc::ptr_
: NULL
; }
773 // Comparison operators.
774 // These return whether a scoped_ptr_malloc and a plain pointer refer
775 // to the same object, not just to two different but equal objects.
776 // For compatibility with the boost-derived implementation, these
777 // take non-const arguments.
778 bool operator==(C
* p
) const {
782 bool operator!=(C
* p
) const {
786 // Swap two scoped pointers.
787 void swap(scoped_ptr_malloc
& b
) {
793 // Release a pointer.
794 // The return value is the current pointer held by this object.
795 // If this object holds a NULL pointer, the return value is NULL.
796 // After this operation, this object will hold a NULL pointer,
797 // and will not own the object any more.
798 C
* release() WARN_UNUSED_RESULT
{
807 // no reason to use these: each scoped_ptr_malloc should have its own object
808 template <class C2
, class GP
>
809 bool operator==(scoped_ptr_malloc
<C2
, GP
> const& p
) const;
810 template <class C2
, class GP
>
811 bool operator!=(scoped_ptr_malloc
<C2
, GP
> const& p
) const;
814 template<class C
, class FP
> inline
815 void swap(scoped_ptr_malloc
<C
, FP
>& a
, scoped_ptr_malloc
<C
, FP
>& b
) {
819 template<class C
, class FP
> inline
820 bool operator==(C
* p
, const scoped_ptr_malloc
<C
, FP
>& b
) {
824 template<class C
, class FP
> inline
825 bool operator!=(C
* p
, const scoped_ptr_malloc
<C
, FP
>& b
) {
829 // A function to convert T* into scoped_ptr<T>
830 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation
831 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg))
832 template <typename T
>
833 scoped_ptr
<T
> make_scoped_ptr(T
* ptr
) {
834 return scoped_ptr
<T
>(ptr
);
837 #endif // BASE_MEMORY_SCOPED_PTR_H_