1// Copyright (c) 2006-2009 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.
4
5#ifndef BASE_SCOPED_PTR_H__
6#define BASE_SCOPED_PTR_H__
7
8//  This is an implementation designed to match the anticipated future TR2
9//  implementation of the scoped_ptr class, and its closely-related brethren,
10//  scoped_array, scoped_ptr_malloc, and make_scoped_ptr.
11//
12//  See http://wiki/Main/ScopedPointerInterface for the spec that drove this
13//  file.
14
15#include <assert.h>
16#include <stdlib.h>
17#include <cstddef>
18
19#ifdef OS_EMBEDDED_QNX
20// NOTE(akirmse):
21// The C++ standard says that <stdlib.h> declares both ::foo and std::foo
22// But this isn't done in QNX version 6.3.2 200709062316.
23using std::free;
24using std::malloc;
25using std::realloc;
26#endif
27
28template <class C> class scoped_ptr;
29template <class C, class Free> class scoped_ptr_malloc;
30template <class C> class scoped_array;
31
32template <class C>
33scoped_ptr<C> make_scoped_ptr(C *);
34
35// A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T>
36// automatically deletes the pointer it holds (if any).
37// That is, scoped_ptr<T> owns the T object that it points to.
38// Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object.
39// Also like T*, scoped_ptr<T> is thread-compatible, and once you
40// dereference it, you get the threadsafety guarantees of T.
41//
42// The size of a scoped_ptr is small:
43// sizeof(scoped_ptr<C>) == sizeof(C*)
44template <class C>
45class scoped_ptr {
46 public:
47
48  // The element type
49  typedef C element_type;
50
51  // Constructor.  Defaults to intializing with NULL.
52  // There is no way to create an uninitialized scoped_ptr.
53  // The input parameter must be allocated with new.
54  explicit scoped_ptr(C* p = NULL) : ptr_(p) { }
55
56  // Destructor.  If there is a C object, delete it.
57  // We don't need to test ptr_ == NULL because C++ does that for us.
58  ~scoped_ptr() {
59    enum { type_must_be_complete = sizeof(C) };
60    delete ptr_;
61  }
62
63  // Reset.  Deletes the current owned object, if any.
64  // Then takes ownership of a new object, if given.
65  // this->reset(this->get()) works.
66  void reset(C* p = NULL) {
67    if (p != ptr_) {
68      enum { type_must_be_complete = sizeof(C) };
69      delete ptr_;
70      ptr_ = p;
71    }
72  }
73
74  // Accessors to get the owned object.
75  // operator* and operator-> will assert() if there is no current object.
76  C& operator*() const {
77    assert(ptr_ != NULL);
78    return *ptr_;
79  }
80  C* operator->() const  {
81    assert(ptr_ != NULL);
82    return ptr_;
83  }
84  C* get() const { return ptr_; }
85
86  // Comparison operators.
87  // These return whether a scoped_ptr and a raw pointer refer to
88  // the same object, not just to two different but equal objects.
89  bool operator==(const C* p) const { return ptr_ == p; }
90  bool operator!=(const C* p) const { return ptr_ != p; }
91
92  // Swap two scoped pointers.
93  void swap(scoped_ptr& p2) {
94    C* tmp = ptr_;
95    ptr_ = p2.ptr_;
96    p2.ptr_ = tmp;
97  }
98
99  // Release a pointer.
100  // The return value is the current pointer held by this object.
101  // If this object holds a NULL pointer, the return value is NULL.
102  // After this operation, this object will hold a NULL pointer,
103  // and will not own the object any more.
104  C* release() {
105    C* retVal = ptr_;
106    ptr_ = NULL;
107    return retVal;
108  }
109
110 private:
111  C* ptr_;
112
113  // google3 friend class that can access copy ctor (although if it actually
114  // calls a copy ctor, there will be a problem) see below
115  friend scoped_ptr<C> make_scoped_ptr<C>(C *p);
116
117  // Forbid comparison of scoped_ptr types.  If C2 != C, it totally doesn't
118  // make sense, and if C2 == C, it still doesn't make sense because you should
119  // never have the same object owned by two different scoped_ptrs.
120  template <class C2> bool operator==(scoped_ptr<C2> const& p2) const;
121  template <class C2> bool operator!=(scoped_ptr<C2> const& p2) const;
122
123  // Disallow evil constructors
124  scoped_ptr(const scoped_ptr&);
125  void operator=(const scoped_ptr&);
126};
127
128// Free functions
129template <class C>
130inline void swap(scoped_ptr<C>& p1, scoped_ptr<C>& p2) {
131  p1.swap(p2);
132}
133
134template <class C>
135inline bool operator==(const C* p1, const scoped_ptr<C>& p2) {
136  return p1 == p2.get();
137}
138
139template <class C>
140inline bool operator==(const C* p1, const scoped_ptr<const C>& p2) {
141  return p1 == p2.get();
142}
143
144template <class C>
145inline bool operator!=(const C* p1, const scoped_ptr<C>& p2) {
146  return p1 != p2.get();
147}
148
149template <class C>
150inline bool operator!=(const C* p1, const scoped_ptr<const C>& p2) {
151  return p1 != p2.get();
152}
153
154template <class C>
155scoped_ptr<C> make_scoped_ptr(C *p) {
156  // This does nothing but to return a scoped_ptr of the type that the passed
157  // pointer is of.  (This eliminates the need to specify the name of T when
158  // making a scoped_ptr that is used anonymously/temporarily.)  From an
159  // access control point of view, we construct an unnamed scoped_ptr here
160  // which we return and thus copy-construct.  Hence, we need to have access
161  // to scoped_ptr::scoped_ptr(scoped_ptr const &).  However, it is guaranteed
162  // that we never actually call the copy constructor, which is a good thing
163  // as we would call the temporary's object destructor (and thus delete p)
164  // if we actually did copy some object, here.
165  return scoped_ptr<C>(p);
166}
167
168// scoped_array<C> is like scoped_ptr<C>, except that the caller must allocate
169// with new [] and the destructor deletes objects with delete [].
170//
171// As with scoped_ptr<C>, a scoped_array<C> either points to an object
172// or is NULL.  A scoped_array<C> owns the object that it points to.
173// scoped_array<T> is thread-compatible, and once you index into it,
174// the returned objects have only the threadsafety guarantees of T.
175//
176// Size: sizeof(scoped_array<C>) == sizeof(C*)
177template <class C>
178class scoped_array {
179 public:
180
181  // The element type
182  typedef C element_type;
183
184  // Constructor.  Defaults to intializing with NULL.
185  // There is no way to create an uninitialized scoped_array.
186  // The input parameter must be allocated with new [].
187  explicit scoped_array(C* p = NULL) : array_(p) { }
188
189  // Destructor.  If there is a C object, delete it.
190  // We don't need to test ptr_ == NULL because C++ does that for us.
191  ~scoped_array() {
192    enum { type_must_be_complete = sizeof(C) };
193    delete[] array_;
194  }
195
196  // Reset.  Deletes the current owned object, if any.
197  // Then takes ownership of a new object, if given.
198  // this->reset(this->get()) works.
199  void reset(C* p = NULL) {
200    if (p != array_) {
201      enum { type_must_be_complete = sizeof(C) };
202      delete[] array_;
203      array_ = p;
204    }
205  }
206
207  // Get one element of the current object.
208  // Will assert() if there is no current object, or index i is negative.
209  C& operator[](std::ptrdiff_t i) const {
210    assert(i >= 0);
211    assert(array_ != NULL);
212    return array_[i];
213  }
214
215  // Get a pointer to the zeroth element of the current object.
216  // If there is no current object, return NULL.
217  C* get() const {
218    return array_;
219  }
220
221  // Comparison operators.
222  // These return whether a scoped_array and a raw pointer refer to
223  // the same array, not just to two different but equal arrays.
224  bool operator==(const C* p) const { return array_ == p; }
225  bool operator!=(const C* p) const { return array_ != p; }
226
227  // Swap two scoped arrays.
228  void swap(scoped_array& p2) {
229    C* tmp = array_;
230    array_ = p2.array_;
231    p2.array_ = tmp;
232  }
233
234  // Release an array.
235  // The return value is the current pointer held by this object.
236  // If this object holds a NULL pointer, the return value is NULL.
237  // After this operation, this object will hold a NULL pointer,
238  // and will not own the object any more.
239  C* release() {
240    C* retVal = array_;
241    array_ = NULL;
242    return retVal;
243  }
244
245 private:
246  C* array_;
247
248  // Forbid comparison of different scoped_array types.
249  template <class C2> bool operator==(scoped_array<C2> const& p2) const;
250  template <class C2> bool operator!=(scoped_array<C2> const& p2) const;
251
252  // Disallow evil constructors
253  scoped_array(const scoped_array&);
254  void operator=(const scoped_array&);
255};
256
257// Free functions
258template <class C>
259inline void swap(scoped_array<C>& p1, scoped_array<C>& p2) {
260  p1.swap(p2);
261}
262
263template <class C>
264inline bool operator==(const C* p1, const scoped_array<C>& p2) {
265  return p1 == p2.get();
266}
267
268template <class C>
269inline bool operator==(const C* p1, const scoped_array<const C>& p2) {
270  return p1 == p2.get();
271}
272
273template <class C>
274inline bool operator!=(const C* p1, const scoped_array<C>& p2) {
275  return p1 != p2.get();
276}
277
278template <class C>
279inline bool operator!=(const C* p1, const scoped_array<const C>& p2) {
280  return p1 != p2.get();
281}
282
283// This class wraps the c library function free() in a class that can be
284// passed as a template argument to scoped_ptr_malloc below.
285class ScopedPtrMallocFree {
286 public:
287  inline void operator()(void* x) const {
288    free(x);
289  }
290};
291
292// scoped_ptr_malloc<> is similar to scoped_ptr<>, but it accepts a
293// second template argument, the functor used to free the object.
294
295template<class C, class FreeProc = ScopedPtrMallocFree>
296class scoped_ptr_malloc {
297 public:
298
299  // The element type
300  typedef C element_type;
301
302  // Construction with no arguments sets ptr_ to NULL.
303  // There is no way to create an uninitialized scoped_ptr.
304  // The input parameter must be allocated with an allocator that matches the
305  // Free functor.  For the default Free functor, this is malloc, calloc, or
306  // realloc.
307  explicit scoped_ptr_malloc(): ptr_(NULL) { }
308
309  // Construct with a C*, and provides an error with a D*.
310  template<class must_be_C>
311  explicit scoped_ptr_malloc(must_be_C* p): ptr_(p) { }
312
313  // Construct with a void*, such as you get from malloc.
314  explicit scoped_ptr_malloc(void *p): ptr_(static_cast<C*>(p)) { }
315
316  // Destructor.  If there is a C object, call the Free functor.
317  ~scoped_ptr_malloc() {
318    free_(ptr_);
319  }
320
321  // Reset.  Calls the Free functor on the current owned object, if any.
322  // Then takes ownership of a new object, if given.
323  // this->reset(this->get()) works.
324  void reset(C* p = NULL) {
325    if (ptr_ != p) {
326      free_(ptr_);
327      ptr_ = p;
328    }
329  }
330
331  // Reallocates the existing pointer, and returns 'true' if
332  // the reallcation is succesfull.  If the reallocation failed, then
333  // the pointer remains in its previous state.
334  //
335  // Note: this calls realloc() directly, even if an alternate 'free'
336  // functor is provided in the template instantiation.
337  bool try_realloc(size_t new_size) {
338    C* new_ptr = static_cast<C*>(realloc(ptr_, new_size));
339    if (new_ptr == NULL) {
340      return false;
341    }
342    ptr_ = new_ptr;
343    return true;
344  }
345
346  // Get the current object.
347  // operator* and operator-> will cause an assert() failure if there is
348  // no current object.
349  C& operator*() const {
350    assert(ptr_ != NULL);
351    return *ptr_;
352  }
353
354  C* operator->() const {
355    assert(ptr_ != NULL);
356    return ptr_;
357  }
358
359  C* get() const {
360    return ptr_;
361  }
362
363  // Comparison operators.
364  // These return whether a scoped_ptr_malloc and a plain pointer refer
365  // to the same object, not just to two different but equal objects.
366  // For compatibility with the boost-derived implementation, these
367  // take non-const arguments.
368  bool operator==(C* p) const {
369    return ptr_ == p;
370  }
371
372  bool operator!=(C* p) const {
373    return ptr_ != p;
374  }
375
376  // Swap two scoped pointers.
377  void swap(scoped_ptr_malloc & b) {
378    C* tmp = b.ptr_;
379    b.ptr_ = ptr_;
380    ptr_ = tmp;
381  }
382
383  // Release a pointer.
384  // The return value is the current pointer held by this object.
385  // If this object holds a NULL pointer, the return value is NULL.
386  // After this operation, this object will hold a NULL pointer,
387  // and will not own the object any more.
388  C* release() {
389    C* tmp = ptr_;
390    ptr_ = NULL;
391    return tmp;
392  }
393
394 private:
395  C* ptr_;
396
397  // no reason to use these: each scoped_ptr_malloc should have its own object
398  template <class C2, class GP>
399  bool operator==(scoped_ptr_malloc<C2, GP> const& p) const;
400  template <class C2, class GP>
401  bool operator!=(scoped_ptr_malloc<C2, GP> const& p) const;
402
403  static FreeProc const free_;
404
405  // Disallow evil constructors
406  scoped_ptr_malloc(const scoped_ptr_malloc&);
407  void operator=(const scoped_ptr_malloc&);
408};
409
410template<class C, class FP>
411FP const scoped_ptr_malloc<C, FP>::free_ = FP();
412
413template<class C, class FP> inline
414void swap(scoped_ptr_malloc<C, FP>& a, scoped_ptr_malloc<C, FP>& b) {
415  a.swap(b);
416}
417
418template<class C, class FP> inline
419bool operator==(C* p, const scoped_ptr_malloc<C, FP>& b) {
420  return p == b.get();
421}
422
423template<class C, class FP> inline
424bool operator!=(C* p, const scoped_ptr_malloc<C, FP>& b) {
425  return p != b.get();
426}
427
428#endif  // BASE_SCOPED_PTR_H__
429