Speculative fix for the white/stale tab issue on Windows.
[chromium-blink-merge.git] / base / memory / scoped_ptr.h
blobb1d6149c6c792203dac5ed7997e633c7eaea4a12
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[].
9 //
10 // Example usage (scoped_ptr):
11 // {
12 // scoped_ptr<Foo> foo(new Foo("wee"));
13 // } // foo goes out of scope, releasing the pointer with it.
15 // {
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):
30 // {
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.
34 // }
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
46 // }
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"));
51 // }
52 // scoped_ptr<Foo> PassThru(scoped_ptr<Foo> arg) {
53 // return arg.Pass();
54 // }
56 // {
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.
62 // }
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>();
82 // }
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.
94 #include <assert.h>
95 #include <stddef.h>
96 #include <stdlib.h>
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"
105 namespace base {
107 namespace subtle {
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>.
115 template <class T>
116 struct DefaultDeleter {
117 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
126 // misuses.
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) };
138 delete ptr;
142 // Specialization of DefaultDeleter for array types.
143 template <class T>
144 struct DefaultDeleter<T[]> {
145 inline void operator()(T* ptr) const {
146 enum { type_must_be_complete = sizeof(T) };
147 delete[] ptr;
150 private:
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
153 // type.
155 // References:
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))));
172 struct FreeDeleter {
173 inline void operator()(void* ptr) const {
174 free(ptr);
178 namespace internal {
180 template <typename T> struct IsNotRefCounted {
181 enum {
182 value = !base::is_convertible<T*, base::subtle::RefCountedBase*>::value &&
183 !base::is_convertible<T*, base::subtle::RefCountedThreadSafeBase*>::
184 value
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 {
192 public:
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
199 // scoped_ptr_impl.
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();
217 ~scoped_ptr_impl() {
218 if (data_.ptr != NULL) {
219 // Not using get_deleter() saves one function call in non-optimized
220 // builds.
221 static_cast<D&>(data_)(data_.ptr);
225 void reset(T* p) {
226 // This self-reset check is deprecated.
227 // this->reset(this->get()) currently works, but it is DEPRECATED, and
228 // will be removed once we verify that no one depends on it.
230 // TODO(ajwong): Change this behavior to match unique_ptr<>.
231 // http://crbug.com/162971
232 if (p != data_.ptr) {
233 if (data_.ptr != NULL) {
234 // Note that this can lead to undefined behavior and memory leaks
235 // in the unlikely but possible case that get_deleter()(get())
236 // indirectly deletes this. The fix is to reset ptr_ before deleting
237 // its old value, but first we need to clean up the code that relies
238 // on the current sequencing.
239 static_cast<D&>(data_)(data_.ptr);
241 data_.ptr = p;
245 T* get() const { return data_.ptr; }
247 D& get_deleter() { return data_; }
248 const D& get_deleter() const { return data_; }
250 void swap(scoped_ptr_impl& p2) {
251 // Standard swap idiom: 'using std::swap' ensures that std::swap is
252 // present in the overload set, but we call swap unqualified so that
253 // any more-specific overloads can be used, if available.
254 using std::swap;
255 swap(static_cast<D&>(data_), static_cast<D&>(p2.data_));
256 swap(data_.ptr, p2.data_.ptr);
259 T* release() {
260 T* old_ptr = data_.ptr;
261 data_.ptr = NULL;
262 return old_ptr;
265 private:
266 // Needed to allow type-converting constructor.
267 template <typename U, typename V> friend class scoped_ptr_impl;
269 // Use the empty base class optimization to allow us to have a D
270 // member, while avoiding any space overhead for it when D is an
271 // empty class. See e.g. http://www.cantrip.org/emptyopt.html for a good
272 // discussion of this technique.
273 struct Data : public D {
274 explicit Data(T* ptr_in) : ptr(ptr_in) {}
275 Data(T* ptr_in, const D& other) : D(other), ptr(ptr_in) {}
276 T* ptr;
279 Data data_;
281 DISALLOW_COPY_AND_ASSIGN(scoped_ptr_impl);
284 } // namespace internal
286 } // namespace base
288 // A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T>
289 // automatically deletes the pointer it holds (if any).
290 // That is, scoped_ptr<T> owns the T object that it points to.
291 // Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object.
292 // Also like T*, scoped_ptr<T> is thread-compatible, and once you
293 // dereference it, you get the thread safety guarantees of T.
295 // The size of scoped_ptr is small. On most compilers, when using the
296 // DefaultDeleter, sizeof(scoped_ptr<T>) == sizeof(T*). Custom deleters will
297 // increase the size proportional to whatever state they need to have. See
298 // comments inside scoped_ptr_impl<> for details.
300 // Current implementation targets having a strict subset of C++11's
301 // unique_ptr<> features. Known deficiencies include not supporting move-only
302 // deleteres, function pointers as deleters, and deleters with reference
303 // types.
304 template <class T, class D = base::DefaultDeleter<T> >
305 class scoped_ptr {
306 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue)
308 COMPILE_ASSERT(base::internal::IsNotRefCounted<T>::value,
309 T_is_refcounted_type_and_needs_scoped_refptr);
311 public:
312 // The element and deleter types.
313 typedef T element_type;
314 typedef D deleter_type;
316 // Constructor. Defaults to initializing with NULL.
317 scoped_ptr() : impl_(NULL) { }
319 // Constructor. Takes ownership of p.
320 explicit scoped_ptr(element_type* p) : impl_(p) { }
322 // Constructor. Allows initialization of a stateful deleter.
323 scoped_ptr(element_type* p, const D& d) : impl_(p, d) { }
325 // Constructor. Allows construction from a scoped_ptr rvalue for a
326 // convertible type and deleter.
328 // IMPLEMENTATION NOTE: C++11 unique_ptr<> keeps this constructor distinct
329 // from the normal move constructor. By C++11 20.7.1.2.1.21, this constructor
330 // has different post-conditions if D is a reference type. Since this
331 // implementation does not support deleters with reference type,
332 // we do not need a separate move constructor allowing us to avoid one
333 // use of SFINAE. You only need to care about this if you modify the
334 // implementation of scoped_ptr.
335 template <typename U, typename V>
336 scoped_ptr(scoped_ptr<U, V> other) : impl_(&other.impl_) {
337 COMPILE_ASSERT(!base::is_array<U>::value, U_cannot_be_an_array);
340 // Constructor. Move constructor for C++03 move emulation of this type.
341 scoped_ptr(RValue rvalue) : impl_(&rvalue.object->impl_) { }
343 // operator=. Allows assignment from a scoped_ptr rvalue for a convertible
344 // type and deleter.
346 // IMPLEMENTATION NOTE: C++11 unique_ptr<> keeps this operator= distinct from
347 // the normal move assignment operator. By C++11 20.7.1.2.3.4, this templated
348 // form has different requirements on for move-only Deleters. Since this
349 // implementation does not support move-only Deleters, we do not need a
350 // separate move assignment operator allowing us to avoid one use of SFINAE.
351 // You only need to care about this if you modify the implementation of
352 // scoped_ptr.
353 template <typename U, typename V>
354 scoped_ptr& operator=(scoped_ptr<U, V> rhs) {
355 COMPILE_ASSERT(!base::is_array<U>::value, U_cannot_be_an_array);
356 impl_.TakeState(&rhs.impl_);
357 return *this;
360 // Reset. Deletes the currently owned object, if any.
361 // Then takes ownership of a new object, if given.
362 void reset(element_type* p = NULL) { impl_.reset(p); }
364 // Accessors to get the owned object.
365 // operator* and operator-> will assert() if there is no current object.
366 element_type& operator*() const {
367 assert(impl_.get() != NULL);
368 return *impl_.get();
370 element_type* operator->() const {
371 assert(impl_.get() != NULL);
372 return impl_.get();
374 element_type* get() const { return impl_.get(); }
376 // Access to the deleter.
377 deleter_type& get_deleter() { return impl_.get_deleter(); }
378 const deleter_type& get_deleter() const { return impl_.get_deleter(); }
380 // Allow scoped_ptr<element_type> to be used in boolean expressions, but not
381 // implicitly convertible to a real bool (which is dangerous).
382 private:
383 typedef base::internal::scoped_ptr_impl<element_type, deleter_type>
384 scoped_ptr::*Testable;
386 public:
387 operator Testable() const { return impl_.get() ? &scoped_ptr::impl_ : NULL; }
389 // Comparison operators.
390 // These return whether two scoped_ptr refer to the same object, not just to
391 // two different but equal objects.
392 bool operator==(const element_type* p) const { return impl_.get() == p; }
393 bool operator!=(const element_type* p) const { return impl_.get() != p; }
395 // Swap two scoped pointers.
396 void swap(scoped_ptr& p2) {
397 impl_.swap(p2.impl_);
400 // Release a pointer.
401 // The return value is the current pointer held by this object.
402 // If this object holds a NULL pointer, the return value is NULL.
403 // After this operation, this object will hold a NULL pointer,
404 // and will not own the object any more.
405 element_type* release() WARN_UNUSED_RESULT {
406 return impl_.release();
409 // C++98 doesn't support functions templates with default parameters which
410 // makes it hard to write a PassAs() that understands converting the deleter
411 // while preserving simple calling semantics.
413 // Until there is a use case for PassAs() with custom deleters, just ignore
414 // the custom deleter.
415 template <typename PassAsType>
416 scoped_ptr<PassAsType> PassAs() {
417 return scoped_ptr<PassAsType>(Pass());
420 private:
421 // Needed to reach into |impl_| in the constructor.
422 template <typename U, typename V> friend class scoped_ptr;
423 base::internal::scoped_ptr_impl<element_type, deleter_type> impl_;
425 // Forbid comparison of scoped_ptr types. If U != T, it totally
426 // doesn't make sense, and if U == T, it still doesn't make sense
427 // because you should never have the same object owned by two different
428 // scoped_ptrs.
429 template <class U> bool operator==(scoped_ptr<U> const& p2) const;
430 template <class U> bool operator!=(scoped_ptr<U> const& p2) const;
433 template <class T, class D>
434 class scoped_ptr<T[], D> {
435 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr, RValue)
437 public:
438 // The element and deleter types.
439 typedef T element_type;
440 typedef D deleter_type;
442 // Constructor. Defaults to initializing with NULL.
443 scoped_ptr() : impl_(NULL) { }
445 // Constructor. Stores the given array. Note that the argument's type
446 // must exactly match T*. In particular:
447 // - it cannot be a pointer to a type derived from T, because it is
448 // inherently unsafe in the general case to access an array through a
449 // pointer whose dynamic type does not match its static type (eg., if
450 // T and the derived types had different sizes access would be
451 // incorrectly calculated). Deletion is also always undefined
452 // (C++98 [expr.delete]p3). If you're doing this, fix your code.
453 // - it cannot be NULL, because NULL is an integral expression, not a
454 // pointer to T. Use the no-argument version instead of explicitly
455 // passing NULL.
456 // - it cannot be const-qualified differently from T per unique_ptr spec
457 // (http://cplusplus.github.com/LWG/lwg-active.html#2118). Users wanting
458 // to work around this may use implicit_cast<const T*>().
459 // However, because of the first bullet in this comment, users MUST
460 // NOT use implicit_cast<Base*>() to upcast the static type of the array.
461 explicit scoped_ptr(element_type* array) : impl_(array) { }
463 // Constructor. Move constructor for C++03 move emulation of this type.
464 scoped_ptr(RValue rvalue) : impl_(&rvalue.object->impl_) { }
466 // operator=. Move operator= for C++03 move emulation of this type.
467 scoped_ptr& operator=(RValue rhs) {
468 impl_.TakeState(&rhs.object->impl_);
469 return *this;
472 // Reset. Deletes the currently owned array, if any.
473 // Then takes ownership of a new object, if given.
474 void reset(element_type* array = NULL) { impl_.reset(array); }
476 // Accessors to get the owned array.
477 element_type& operator[](size_t i) const {
478 assert(impl_.get() != NULL);
479 return impl_.get()[i];
481 element_type* get() const { return impl_.get(); }
483 // Access to the deleter.
484 deleter_type& get_deleter() { return impl_.get_deleter(); }
485 const deleter_type& get_deleter() const { return impl_.get_deleter(); }
487 // Allow scoped_ptr<element_type> to be used in boolean expressions, but not
488 // implicitly convertible to a real bool (which is dangerous).
489 private:
490 typedef base::internal::scoped_ptr_impl<element_type, deleter_type>
491 scoped_ptr::*Testable;
493 public:
494 operator Testable() const { return impl_.get() ? &scoped_ptr::impl_ : NULL; }
496 // Comparison operators.
497 // These return whether two scoped_ptr refer to the same object, not just to
498 // two different but equal objects.
499 bool operator==(element_type* array) const { return impl_.get() == array; }
500 bool operator!=(element_type* array) const { return impl_.get() != array; }
502 // Swap two scoped pointers.
503 void swap(scoped_ptr& p2) {
504 impl_.swap(p2.impl_);
507 // Release a pointer.
508 // The return value is the current pointer held by this object.
509 // If this object holds a NULL pointer, the return value is NULL.
510 // After this operation, this object will hold a NULL pointer,
511 // and will not own the object any more.
512 element_type* release() WARN_UNUSED_RESULT {
513 return impl_.release();
516 private:
517 // Force element_type to be a complete type.
518 enum { type_must_be_complete = sizeof(element_type) };
520 // Actually hold the data.
521 base::internal::scoped_ptr_impl<element_type, deleter_type> impl_;
523 // Disable initialization from any type other than element_type*, by
524 // providing a constructor that matches such an initialization, but is
525 // private and has no definition. This is disabled because it is not safe to
526 // call delete[] on an array whose static type does not match its dynamic
527 // type.
528 template <typename U> explicit scoped_ptr(U* array);
530 // Disable reset() from any type other than element_type*, for the same
531 // reasons as the constructor above.
532 template <typename U> void reset(U* array);
534 // Forbid comparison of scoped_ptr types. If U != T, it totally
535 // doesn't make sense, and if U == T, it still doesn't make sense
536 // because you should never have the same object owned by two different
537 // scoped_ptrs.
538 template <class U> bool operator==(scoped_ptr<U> const& p2) const;
539 template <class U> bool operator!=(scoped_ptr<U> const& p2) const;
542 // Free functions
543 template <class T, class D>
544 void swap(scoped_ptr<T, D>& p1, scoped_ptr<T, D>& p2) {
545 p1.swap(p2);
548 template <class T, class D>
549 bool operator==(T* p1, const scoped_ptr<T, D>& p2) {
550 return p1 == p2.get();
553 template <class T, class D>
554 bool operator!=(T* p1, const scoped_ptr<T, D>& p2) {
555 return p1 != p2.get();
558 // DEPRECATED: Use scoped_ptr<C[]> instead.
560 // scoped_array<C> is like scoped_ptr<C>, except that the caller must allocate
561 // with new [] and the destructor deletes objects with delete [].
563 // As with scoped_ptr<C>, a scoped_array<C> either points to an object
564 // or is NULL. A scoped_array<C> owns the object that it points to.
565 // scoped_array<T> is thread-compatible, and once you index into it,
566 // the returned objects have only the thread safety guarantees of T.
568 // Size: sizeof(scoped_array<C>) == sizeof(C*)
569 template <class C>
570 class scoped_array {
571 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_array, RValue)
573 public:
575 // The element type
576 typedef C element_type;
578 // Constructor. Defaults to initializing with NULL.
579 // There is no way to create an uninitialized scoped_array.
580 // The input parameter must be allocated with new [].
581 explicit scoped_array(C* p = NULL) : array_(p) { }
583 // Constructor. Move constructor for C++03 move emulation of this type.
584 scoped_array(RValue rvalue)
585 : array_(rvalue.object->release()) {
588 // Destructor. If there is a C object, delete it.
589 // We don't need to test ptr_ == NULL because C++ does that for us.
590 ~scoped_array() {
591 enum { type_must_be_complete = sizeof(C) };
592 delete[] array_;
595 // operator=. Move operator= for C++03 move emulation of this type.
596 scoped_array& operator=(RValue rhs) {
597 reset(rhs.object->release());
598 return *this;
601 // Reset. Deletes the current owned object, if any.
602 // Then takes ownership of a new object, if given.
603 // this->reset(this->get()) works.
604 void reset(C* p = NULL) {
605 if (p != array_) {
606 enum { type_must_be_complete = sizeof(C) };
607 delete[] array_;
608 array_ = p;
612 // Get one element of the current object.
613 // Will assert() if there is no current object, or index i is negative.
614 C& operator[](ptrdiff_t i) const {
615 assert(i >= 0);
616 assert(array_ != NULL);
617 return array_[i];
620 // Get a pointer to the zeroth element of the current object.
621 // If there is no current object, return NULL.
622 C* get() const {
623 return array_;
626 // Allow scoped_array<C> to be used in boolean expressions, but not
627 // implicitly convertible to a real bool (which is dangerous).
628 typedef C* scoped_array::*Testable;
629 operator Testable() const { return array_ ? &scoped_array::array_ : NULL; }
631 // Comparison operators.
632 // These return whether two scoped_array refer to the same object, not just to
633 // two different but equal objects.
634 bool operator==(C* p) const { return array_ == p; }
635 bool operator!=(C* p) const { return array_ != p; }
637 // Swap two scoped arrays.
638 void swap(scoped_array& p2) {
639 C* tmp = array_;
640 array_ = p2.array_;
641 p2.array_ = tmp;
644 // Release an array.
645 // The return value is the current pointer held by this object.
646 // If this object holds a NULL pointer, the return value is NULL.
647 // After this operation, this object will hold a NULL pointer,
648 // and will not own the object any more.
649 C* release() WARN_UNUSED_RESULT {
650 C* retVal = array_;
651 array_ = NULL;
652 return retVal;
655 private:
656 C* array_;
658 // Forbid comparison of different scoped_array types.
659 template <class C2> bool operator==(scoped_array<C2> const& p2) const;
660 template <class C2> bool operator!=(scoped_array<C2> const& p2) const;
663 // Free functions
664 template <class C>
665 void swap(scoped_array<C>& p1, scoped_array<C>& p2) {
666 p1.swap(p2);
669 template <class C>
670 bool operator==(C* p1, const scoped_array<C>& p2) {
671 return p1 == p2.get();
674 template <class C>
675 bool operator!=(C* p1, const scoped_array<C>& p2) {
676 return p1 != p2.get();
679 // DEPRECATED: Use scoped_ptr<C, base::FreeDeleter> instead.
681 // scoped_ptr_malloc<> is similar to scoped_ptr<>, but it accepts a
682 // second template argument, the functor used to free the object.
684 template<class C, class FreeProc = base::FreeDeleter>
685 class scoped_ptr_malloc {
686 MOVE_ONLY_TYPE_FOR_CPP_03(scoped_ptr_malloc, RValue)
688 public:
690 // The element type
691 typedef C element_type;
693 // Constructor. Defaults to initializing with NULL.
694 // There is no way to create an uninitialized scoped_ptr.
695 // The input parameter must be allocated with an allocator that matches the
696 // Free functor. For the default Free functor, this is malloc, calloc, or
697 // realloc.
698 explicit scoped_ptr_malloc(C* p = NULL): ptr_(p) {}
700 // Constructor. Move constructor for C++03 move emulation of this type.
701 scoped_ptr_malloc(RValue rvalue)
702 : ptr_(rvalue.object->release()) {
705 // Destructor. If there is a C object, call the Free functor.
706 ~scoped_ptr_malloc() {
707 reset();
710 // operator=. Move operator= for C++03 move emulation of this type.
711 scoped_ptr_malloc& operator=(RValue rhs) {
712 reset(rhs.object->release());
713 return *this;
716 // Reset. Calls the Free functor on the current owned object, if any.
717 // Then takes ownership of a new object, if given.
718 // this->reset(this->get()) works.
719 void reset(C* p = NULL) {
720 if (ptr_ != p) {
721 if (ptr_ != NULL) {
722 FreeProc free_proc;
723 free_proc(ptr_);
725 ptr_ = p;
729 // Get the current object.
730 // operator* and operator-> will cause an assert() failure if there is
731 // no current object.
732 C& operator*() const {
733 assert(ptr_ != NULL);
734 return *ptr_;
737 C* operator->() const {
738 assert(ptr_ != NULL);
739 return ptr_;
742 C* get() const {
743 return ptr_;
746 // Allow scoped_ptr_malloc<C> to be used in boolean expressions, but not
747 // implicitly convertible to a real bool (which is dangerous).
748 typedef C* scoped_ptr_malloc::*Testable;
749 operator Testable() const { return ptr_ ? &scoped_ptr_malloc::ptr_ : NULL; }
751 // Comparison operators.
752 // These return whether a scoped_ptr_malloc and a plain pointer refer
753 // to the same object, not just to two different but equal objects.
754 // For compatibility with the boost-derived implementation, these
755 // take non-const arguments.
756 bool operator==(C* p) const {
757 return ptr_ == p;
760 bool operator!=(C* p) const {
761 return ptr_ != p;
764 // Swap two scoped pointers.
765 void swap(scoped_ptr_malloc & b) {
766 C* tmp = b.ptr_;
767 b.ptr_ = ptr_;
768 ptr_ = tmp;
771 // Release a pointer.
772 // The return value is the current pointer held by this object.
773 // If this object holds a NULL pointer, the return value is NULL.
774 // After this operation, this object will hold a NULL pointer,
775 // and will not own the object any more.
776 C* release() WARN_UNUSED_RESULT {
777 C* tmp = ptr_;
778 ptr_ = NULL;
779 return tmp;
782 private:
783 C* ptr_;
785 // no reason to use these: each scoped_ptr_malloc should have its own object
786 template <class C2, class GP>
787 bool operator==(scoped_ptr_malloc<C2, GP> const& p) const;
788 template <class C2, class GP>
789 bool operator!=(scoped_ptr_malloc<C2, GP> const& p) const;
792 template<class C, class FP> inline
793 void swap(scoped_ptr_malloc<C, FP>& a, scoped_ptr_malloc<C, FP>& b) {
794 a.swap(b);
797 template<class C, class FP> inline
798 bool operator==(C* p, const scoped_ptr_malloc<C, FP>& b) {
799 return p == b.get();
802 template<class C, class FP> inline
803 bool operator!=(C* p, const scoped_ptr_malloc<C, FP>& b) {
804 return p != b.get();
807 // A function to convert T* into scoped_ptr<T>
808 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation
809 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg))
810 template <typename T>
811 scoped_ptr<T> make_scoped_ptr(T* ptr) {
812 return scoped_ptr<T>(ptr);
815 #endif // BASE_MEMORY_SCOPED_PTR_H_