OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 // When the destructor of the scoped pointer is executed the plain pointer | 51 // When the destructor of the scoped pointer is executed the plain pointer |
52 // is deleted using DeleteArray. This implies that you must allocate with | 52 // is deleted using DeleteArray. This implies that you must allocate with |
53 // NewArray. | 53 // NewArray. |
54 inline ~SmartPointerBase() { if (p_) Deallocator::Delete(p_); } | 54 inline ~SmartPointerBase() { if (p_) Deallocator::Delete(p_); } |
55 | 55 |
56 inline T* operator->() const { return p_; } | 56 inline T* operator->() const { return p_; } |
57 | 57 |
58 // You can get the underlying pointer out with the * operator. | 58 // You can get the underlying pointer out with the * operator. |
59 inline T* operator*() { return p_; } | 59 inline T* operator*() { return p_; } |
60 | 60 |
61 // You can use [n] to index as if it was a plain pointer | 61 // You can use [n] to index as if it was a plain pointer. |
62 inline T& operator[](size_t i) { | 62 inline T& operator[](size_t i) { |
63 return p_[i]; | 63 return p_[i]; |
64 } | 64 } |
65 | 65 |
| 66 // You can use [n] to index as if it was a plain pointer. |
| 67 const inline T& operator[](size_t i) const { |
| 68 return p_[i]; |
| 69 } |
| 70 |
66 // We don't have implicit conversion to a T* since that hinders migration: | 71 // We don't have implicit conversion to a T* since that hinders migration: |
67 // You would not be able to change a method from returning a T* to | 72 // You would not be able to change a method from returning a T* to |
68 // returning an SmartArrayPointer<T> and then get errors wherever it is used. | 73 // returning an SmartArrayPointer<T> and then get errors wherever it is used. |
69 | 74 |
70 | 75 |
71 // If you want to take out the plain pointer and don't want it automatically | 76 // If you want to take out the plain pointer and don't want it automatically |
72 // deleted then call Detach(). Afterwards, the smart pointer is empty | 77 // deleted then call Detach(). Afterwards, the smart pointer is empty |
73 // (NULL). | 78 // (NULL). |
74 inline T* Detach() { | 79 inline T* Detach() { |
75 T* temp = p_; | 80 T* temp = p_; |
76 p_ = NULL; | 81 p_ = NULL; |
77 return temp; | 82 return temp; |
78 } | 83 } |
79 | 84 |
| 85 inline void Reset(T* new_value) { |
| 86 if (p_) Deallocator::Delete(p_); |
| 87 p_ = new_value; |
| 88 } |
| 89 |
80 // Assignment requires an empty (NULL) SmartArrayPointer as the receiver. Like | 90 // Assignment requires an empty (NULL) SmartArrayPointer as the receiver. Like |
81 // the copy constructor it removes the pointer in the original to avoid | 91 // the copy constructor it removes the pointer in the original to avoid |
82 // double freeing. | 92 // double freeing. |
83 inline SmartPointerBase<Deallocator, T>& operator=( | 93 inline SmartPointerBase<Deallocator, T>& operator=( |
84 const SmartPointerBase<Deallocator, T>& rhs) { | 94 const SmartPointerBase<Deallocator, T>& rhs) { |
85 ASSERT(is_empty()); | 95 ASSERT(is_empty()); |
86 T* tmp = rhs.p_; // swap to handle self-assignment | 96 T* tmp = rhs.p_; // swap to handle self-assignment |
87 const_cast<SmartPointerBase<Deallocator, T>&>(rhs).p_ = NULL; | 97 const_cast<SmartPointerBase<Deallocator, T>&>(rhs).p_ = NULL; |
88 p_ = tmp; | 98 p_ = tmp; |
89 return *this; | 99 return *this; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 inline SmartPointer() { } | 140 inline SmartPointer() { } |
131 explicit inline SmartPointer(T* ptr) | 141 explicit inline SmartPointer(T* ptr) |
132 : SmartPointerBase<ObjectDeallocator<T>, T>(ptr) { } | 142 : SmartPointerBase<ObjectDeallocator<T>, T>(ptr) { } |
133 inline SmartPointer(const SmartPointer<T>& rhs) | 143 inline SmartPointer(const SmartPointer<T>& rhs) |
134 : SmartPointerBase<ObjectDeallocator<T>, T>(rhs) { } | 144 : SmartPointerBase<ObjectDeallocator<T>, T>(rhs) { } |
135 }; | 145 }; |
136 | 146 |
137 } } // namespace v8::internal | 147 } } // namespace v8::internal |
138 | 148 |
139 #endif // V8_SMART_POINTERS_H_ | 149 #endif // V8_SMART_POINTERS_H_ |
OLD | NEW |