1ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Use of this source code is governed by a BSD-style license that can be
3ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// found in the LICENSE file.
4ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
5ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Scopers help you manage ownership of a pointer, helping you easily manage the
6ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// a pointer within a scope, and automatically destroying the pointer at the
7ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// end of a scope.  There are two main classes you will use, which correspond
8ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// to the operators new/delete and new[]/delete[].
9ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//
10ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Example usage (scoped_ptr):
11ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//   {
12ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//     scoped_ptr<Foo> foo(new Foo("wee"));
13ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//   }  // foo goes out of scope, releasing the pointer with it.
14ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//
15ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//   {
16ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//     scoped_ptr<Foo> foo;          // No pointer managed.
17ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//     foo.reset(new Foo("wee"));    // Now a pointer is managed.
18ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//     foo.reset(new Foo("wee2"));   // Foo("wee") was destroyed.
19ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//     foo.reset(new Foo("wee3"));   // Foo("wee2") was destroyed.
20ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//     foo->Method();                // Foo::Method() called.
21ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//     foo.get()->Method();          // Foo::Method() called.
22ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//     SomeFunc(foo.release());      // SomeFunc takes ownership, foo no longer
23ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//                                   // manages a pointer.
24ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//     foo.reset(new Foo("wee4"));   // foo manages a pointer again.
25ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//     foo.reset();                  // Foo("wee4") destroyed, foo no longer
26ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//                                   // manages a pointer.
27ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//   }  // foo wasn't managing a pointer, so nothing was destroyed.
28ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//
29ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Example usage (scoped_array):
30ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//   {
31ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//     scoped_array<Foo> foo(new Foo[100]);
32ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//     foo.get()->Method();  // Foo::Method on the 0th element.
33ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//     foo[10].Method();     // Foo::Method on the 10th element.
34ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//   }
35ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
36ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#ifndef BASE_MEMORY_SCOPED_PTR_H_
37ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#define BASE_MEMORY_SCOPED_PTR_H_
38ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#pragma once
39ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
40ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// This is an implementation designed to match the anticipated future TR2
41ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// implementation of the scoped_ptr class, and its closely-related brethren,
42ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// scoped_array, scoped_ptr_malloc.
43ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
44ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include <assert.h>
45ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include <stddef.h>
46ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include <stdlib.h>
47ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
48ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/compiler_specific.h"
49ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
50ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T>
51ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// automatically deletes the pointer it holds (if any).
52ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// That is, scoped_ptr<T> owns the T object that it points to.
53ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object.
54ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Also like T*, scoped_ptr<T> is thread-compatible, and once you
55ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// dereference it, you get the threadsafety guarantees of T.
56ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//
57ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// The size of a scoped_ptr is small:
58ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// sizeof(scoped_ptr<C>) == sizeof(C*)
59ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsentemplate <class C>
60ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenclass scoped_ptr {
61ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen public:
62ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
63ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // The element type
64ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  typedef C element_type;
65ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
66ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Constructor.  Defaults to initializing with NULL.
67ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // There is no way to create an uninitialized scoped_ptr.
68ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // The input parameter must be allocated with new.
69ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  explicit scoped_ptr(C* p = NULL) : ptr_(p) { }
70ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
71ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Destructor.  If there is a C object, delete it.
72ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // We don't need to test ptr_ == NULL because C++ does that for us.
73ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  ~scoped_ptr() {
74ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    enum { type_must_be_complete = sizeof(C) };
75ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    delete ptr_;
76ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  }
77ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
78ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Reset.  Deletes the current owned object, if any.
79ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Then takes ownership of a new object, if given.
80ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // this->reset(this->get()) works.
81ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  void reset(C* p = NULL) {
82ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    if (p != ptr_) {
83ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      enum { type_must_be_complete = sizeof(C) };
84ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      delete ptr_;
85ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      ptr_ = p;
86ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    }
87ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  }
88ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
89ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Accessors to get the owned object.
90ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // operator* and operator-> will assert() if there is no current object.
91ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  C& operator*() const {
92ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    assert(ptr_ != NULL);
93ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    return *ptr_;
94ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  }
95ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  C* operator->() const  {
96ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    assert(ptr_ != NULL);
97ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    return ptr_;
98ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  }
99ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  C* get() const { return ptr_; }
100ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
101ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Comparison operators.
102ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // These return whether two scoped_ptr refer to the same object, not just to
103ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // two different but equal objects.
104ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  bool operator==(C* p) const { return ptr_ == p; }
105ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  bool operator!=(C* p) const { return ptr_ != p; }
106ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
107ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Swap two scoped pointers.
108ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  void swap(scoped_ptr& p2) {
109ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    C* tmp = ptr_;
110ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    ptr_ = p2.ptr_;
111ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    p2.ptr_ = tmp;
112ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  }
113ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
114ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Release a pointer.
115ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // The return value is the current pointer held by this object.
116ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // If this object holds a NULL pointer, the return value is NULL.
117ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // After this operation, this object will hold a NULL pointer,
118ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // and will not own the object any more.
119ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  C* release() WARN_UNUSED_RESULT {
120ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    C* retVal = ptr_;
121ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    ptr_ = NULL;
122ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    return retVal;
123ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  }
124ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
125ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen private:
126ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  C* ptr_;
127ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
128ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Forbid comparison of scoped_ptr types.  If C2 != C, it totally doesn't
129ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // make sense, and if C2 == C, it still doesn't make sense because you should
130ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // never have the same object owned by two different scoped_ptrs.
131ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  template <class C2> bool operator==(scoped_ptr<C2> const& p2) const;
132ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  template <class C2> bool operator!=(scoped_ptr<C2> const& p2) const;
133ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
134ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Disallow evil constructors
135ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  scoped_ptr(const scoped_ptr&);
136ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  void operator=(const scoped_ptr&);
137ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen};
138ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
139ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Free functions
140ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsentemplate <class C>
141ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenvoid swap(scoped_ptr<C>& p1, scoped_ptr<C>& p2) {
142ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  p1.swap(p2);
143ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen}
144ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
145ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsentemplate <class C>
146ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenbool operator==(C* p1, const scoped_ptr<C>& p2) {
147ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  return p1 == p2.get();
148ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen}
149ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
150ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsentemplate <class C>
151ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenbool operator!=(C* p1, const scoped_ptr<C>& p2) {
152ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  return p1 != p2.get();
153ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen}
154ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
155ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// scoped_array<C> is like scoped_ptr<C>, except that the caller must allocate
156ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// with new [] and the destructor deletes objects with delete [].
157ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//
158ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// As with scoped_ptr<C>, a scoped_array<C> either points to an object
159ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// or is NULL.  A scoped_array<C> owns the object that it points to.
160ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// scoped_array<T> is thread-compatible, and once you index into it,
161ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// the returned objects have only the threadsafety guarantees of T.
162ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen//
163ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Size: sizeof(scoped_array<C>) == sizeof(C*)
164ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsentemplate <class C>
165ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenclass scoped_array {
166ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen public:
167ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
168ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // The element type
169ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  typedef C element_type;
170ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
171ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Constructor.  Defaults to intializing with NULL.
172ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // There is no way to create an uninitialized scoped_array.
173ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // The input parameter must be allocated with new [].
174ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  explicit scoped_array(C* p = NULL) : array_(p) { }
175ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
176ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Destructor.  If there is a C object, delete it.
177ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // We don't need to test ptr_ == NULL because C++ does that for us.
178ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  ~scoped_array() {
179ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    enum { type_must_be_complete = sizeof(C) };
180ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    delete[] array_;
181ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  }
182ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
183ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Reset.  Deletes the current owned object, if any.
184ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Then takes ownership of a new object, if given.
185ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // this->reset(this->get()) works.
186ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  void reset(C* p = NULL) {
187ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    if (p != array_) {
188ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      enum { type_must_be_complete = sizeof(C) };
189ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      delete[] array_;
190ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      array_ = p;
191ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    }
192ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  }
193ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
194ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Get one element of the current object.
195ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Will assert() if there is no current object, or index i is negative.
196ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  C& operator[](ptrdiff_t i) const {
197ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    assert(i >= 0);
198ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    assert(array_ != NULL);
199ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    return array_[i];
200ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  }
201ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
202ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Get a pointer to the zeroth element of the current object.
203ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // If there is no current object, return NULL.
204ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  C* get() const {
205ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    return array_;
206ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  }
207ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
208ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Comparison operators.
209ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // These return whether two scoped_array refer to the same object, not just to
210ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // two different but equal objects.
211ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  bool operator==(C* p) const { return array_ == p; }
212ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  bool operator!=(C* p) const { return array_ != p; }
213ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
214ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Swap two scoped arrays.
215ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  void swap(scoped_array& p2) {
216ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    C* tmp = array_;
217ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    array_ = p2.array_;
218ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    p2.array_ = tmp;
219ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  }
220ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
221ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Release an array.
222ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // The return value is the current pointer held by this object.
223ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // If this object holds a NULL pointer, the return value is NULL.
224ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // After this operation, this object will hold a NULL pointer,
225ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // and will not own the object any more.
226ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  C* release() WARN_UNUSED_RESULT {
227ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    C* retVal = array_;
228ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    array_ = NULL;
229ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    return retVal;
230ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  }
231ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
232ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen private:
233ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  C* array_;
234ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
235ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Forbid comparison of different scoped_array types.
236ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  template <class C2> bool operator==(scoped_array<C2> const& p2) const;
237ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  template <class C2> bool operator!=(scoped_array<C2> const& p2) const;
238ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
239ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Disallow evil constructors
240ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  scoped_array(const scoped_array&);
241ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  void operator=(const scoped_array&);
242ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen};
243ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
244ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Free functions
245ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsentemplate <class C>
246ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenvoid swap(scoped_array<C>& p1, scoped_array<C>& p2) {
247ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  p1.swap(p2);
248ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen}
249ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
250ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsentemplate <class C>
251ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenbool operator==(C* p1, const scoped_array<C>& p2) {
252ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  return p1 == p2.get();
253ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen}
254ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
255ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsentemplate <class C>
256ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenbool operator!=(C* p1, const scoped_array<C>& p2) {
257ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  return p1 != p2.get();
258ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen}
259ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
260ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// This class wraps the c library function free() in a class that can be
261ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// passed as a template argument to scoped_ptr_malloc below.
262ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenclass ScopedPtrMallocFree {
263ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen public:
264ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  inline void operator()(void* x) const {
265ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    free(x);
266ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  }
267ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen};
268ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
269ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// scoped_ptr_malloc<> is similar to scoped_ptr<>, but it accepts a
270ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// second template argument, the functor used to free the object.
271ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
272ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsentemplate<class C, class FreeProc = ScopedPtrMallocFree>
273ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenclass scoped_ptr_malloc {
274ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen public:
275ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
276ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // The element type
277ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  typedef C element_type;
278ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
279ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Constructor.  Defaults to initializing with NULL.
280ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // There is no way to create an uninitialized scoped_ptr.
281ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // The input parameter must be allocated with an allocator that matches the
282ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Free functor.  For the default Free functor, this is malloc, calloc, or
283ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // realloc.
284ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  explicit scoped_ptr_malloc(C* p = NULL): ptr_(p) {}
285ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
286ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Destructor.  If there is a C object, call the Free functor.
287ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  ~scoped_ptr_malloc() {
288ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    free_(ptr_);
289ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  }
290ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
291ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Reset.  Calls the Free functor on the current owned object, if any.
292ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Then takes ownership of a new object, if given.
293ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // this->reset(this->get()) works.
294ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  void reset(C* p = NULL) {
295ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    if (ptr_ != p) {
296ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      free_(ptr_);
297ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      ptr_ = p;
298ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    }
299ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  }
300ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
301ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Get the current object.
302ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // operator* and operator-> will cause an assert() failure if there is
303ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // no current object.
304ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  C& operator*() const {
305ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    assert(ptr_ != NULL);
306ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    return *ptr_;
307ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  }
308ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
309ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  C* operator->() const {
310ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    assert(ptr_ != NULL);
311ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    return ptr_;
312ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  }
313ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
314ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  C* get() const {
315ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    return ptr_;
316ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  }
317ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
318ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Comparison operators.
319ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // These return whether a scoped_ptr_malloc and a plain pointer refer
320ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // to the same object, not just to two different but equal objects.
321ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // For compatibility with the boost-derived implementation, these
322ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // take non-const arguments.
323ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  bool operator==(C* p) const {
324ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    return ptr_ == p;
325ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  }
326ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
327ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  bool operator!=(C* p) const {
328ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    return ptr_ != p;
329ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  }
330ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
331ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Swap two scoped pointers.
332ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  void swap(scoped_ptr_malloc & b) {
333ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    C* tmp = b.ptr_;
334ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    b.ptr_ = ptr_;
335ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    ptr_ = tmp;
336ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  }
337ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
338ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Release a pointer.
339ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // The return value is the current pointer held by this object.
340ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // If this object holds a NULL pointer, the return value is NULL.
341ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // After this operation, this object will hold a NULL pointer,
342ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // and will not own the object any more.
343ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  C* release() WARN_UNUSED_RESULT {
344ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    C* tmp = ptr_;
345ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    ptr_ = NULL;
346ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    return tmp;
347ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  }
348ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
349ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen private:
350ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  C* ptr_;
351ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
352ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // no reason to use these: each scoped_ptr_malloc should have its own object
353ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  template <class C2, class GP>
354ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  bool operator==(scoped_ptr_malloc<C2, GP> const& p) const;
355ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  template <class C2, class GP>
356ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  bool operator!=(scoped_ptr_malloc<C2, GP> const& p) const;
357ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
358ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  static FreeProc const free_;
359ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
360ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  // Disallow evil constructors
361ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  scoped_ptr_malloc(const scoped_ptr_malloc&);
362ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  void operator=(const scoped_ptr_malloc&);
363ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen};
364ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
365ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsentemplate<class C, class FP>
366ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenFP const scoped_ptr_malloc<C, FP>::free_ = FP();
367ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
368ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsentemplate<class C, class FP> inline
369ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenvoid swap(scoped_ptr_malloc<C, FP>& a, scoped_ptr_malloc<C, FP>& b) {
370ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  a.swap(b);
371ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen}
372ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
373ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsentemplate<class C, class FP> inline
374ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenbool operator==(C* p, const scoped_ptr_malloc<C, FP>& b) {
375ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  return p == b.get();
376ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen}
377ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
378ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsentemplate<class C, class FP> inline
379ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenbool operator!=(C* p, const scoped_ptr_malloc<C, FP>& b) {
380ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  return p != b.get();
381ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen}
382ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
383ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#endif  // BASE_MEMORY_SCOPED_PTR_H_
384