blob: c39df16869582f9426cec5a4162046d57675b180 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_SMART_POINTER_H_
29#define V8_SMART_POINTER_H_
30
31namespace v8 { namespace internal {
32
33
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000034// A 'scoped array pointer' that calls DeleteArray on its pointer when the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035// destructor is called.
36template<typename T>
37class SmartPointer {
38 public:
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000039
40 // Default constructor. Construct an empty scoped pointer.
41 inline SmartPointer() : p(NULL) {}
42
43
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044 // Construct a scoped pointer from a plain one.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000045 explicit inline SmartPointer(T* pointer) : p(pointer) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000046
47
48 // Copy constructor removes the pointer from the original to avoid double
49 // freeing.
50 inline SmartPointer(const SmartPointer<T>& rhs) : p(rhs.p) {
51 const_cast<SmartPointer<T>&>(rhs).p = NULL;
52 }
53
54
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000055 // When the destructor of the scoped pointer is executed the plain pointer
56 // is deleted using DeleteArray. This implies that you must allocate with
57 // NewArray.
58 inline ~SmartPointer() { if (p) DeleteArray(p); }
59
60
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000061 // You can get the underlying pointer out with the * operator.
62 inline T* operator*() { return p; }
63
64
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000065 // You can use [n] to index as if it was a plain pointer
66 inline T& operator[](size_t i) {
67 return p[i];
68 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000069
70 // We don't have implicit conversion to a T* since that hinders migration:
71 // You would not be able to change a method from returning a T* to
72 // returning an SmartPointer<T> and then get errors wherever it is used.
73
74
75 // If you want to take out the plain pointer and don't want it automatically
76 // deleted then call Detach(). Afterwards, the smart pointer is empty
77 // (NULL).
78 inline T* Detach() {
79 T* temp = p;
80 p = NULL;
81 return temp;
82 }
83
84
85 // Assignment requires an empty (NULL) SmartPointer as the receiver. Like
86 // the copy constructor it removes the pointer in the original to avoid
87 // double freeing.
88 inline SmartPointer& operator=(const SmartPointer<T>& rhs) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +000089 ASSERT(is_empty());
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000090 T* tmp = rhs.p; // swap to handle self-assignment
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000091 const_cast<SmartPointer<T>&>(rhs).p = NULL;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000092 p = tmp;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000093 return *this;
94 }
95
96
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +000097 inline bool is_empty() {
98 return p == NULL;
99 }
100
101
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000102 private:
103 T* p;
104};
105
106} } // namespace v8::internal
107
108#endif // V8_SMART_POINTER_H_