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 | |
71 // We don't have implicit conversion to a T* since that hinders migration: | 66 // We don't have implicit conversion to a T* since that hinders migration: |
72 // You would not be able to change a method from returning a T* to | 67 // You would not be able to change a method from returning a T* to |
73 // returning an SmartArrayPointer<T> and then get errors wherever it is used. | 68 // returning an SmartArrayPointer<T> and then get errors wherever it is used. |
74 | 69 |
75 | 70 |
76 // If you want to take out the plain pointer and don't want it automatically | 71 // If you want to take out the plain pointer and don't want it automatically |
77 // deleted then call Detach(). Afterwards, the smart pointer is empty | 72 // deleted then call Detach(). Afterwards, the smart pointer is empty |
78 // (NULL). | 73 // (NULL). |
79 inline T* Detach() { | 74 inline T* Detach() { |
80 T* temp = p_; | 75 T* temp = p_; |
81 p_ = NULL; | 76 p_ = NULL; |
82 return temp; | 77 return temp; |
83 } | 78 } |
84 | 79 |
85 inline void Reset(T* new_value) { | |
86 if (p_) Deallocator::Delete(p_); | |
87 p_ = new_value; | |
88 } | |
89 | |
90 // Assignment requires an empty (NULL) SmartArrayPointer as the receiver. Like | 80 // Assignment requires an empty (NULL) SmartArrayPointer as the receiver. Like |
91 // the copy constructor it removes the pointer in the original to avoid | 81 // the copy constructor it removes the pointer in the original to avoid |
92 // double freeing. | 82 // double freeing. |
93 inline SmartPointerBase<Deallocator, T>& operator=( | 83 inline SmartPointerBase<Deallocator, T>& operator=( |
94 const SmartPointerBase<Deallocator, T>& rhs) { | 84 const SmartPointerBase<Deallocator, T>& rhs) { |
95 ASSERT(is_empty()); | 85 ASSERT(is_empty()); |
96 T* tmp = rhs.p_; // swap to handle self-assignment | 86 T* tmp = rhs.p_; // swap to handle self-assignment |
97 const_cast<SmartPointerBase<Deallocator, T>&>(rhs).p_ = NULL; | 87 const_cast<SmartPointerBase<Deallocator, T>&>(rhs).p_ = NULL; |
98 p_ = tmp; | 88 p_ = tmp; |
99 return *this; | 89 return *this; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 inline SmartPointer() { } | 130 inline SmartPointer() { } |
141 explicit inline SmartPointer(T* ptr) | 131 explicit inline SmartPointer(T* ptr) |
142 : SmartPointerBase<ObjectDeallocator<T>, T>(ptr) { } | 132 : SmartPointerBase<ObjectDeallocator<T>, T>(ptr) { } |
143 inline SmartPointer(const SmartPointer<T>& rhs) | 133 inline SmartPointer(const SmartPointer<T>& rhs) |
144 : SmartPointerBase<ObjectDeallocator<T>, T>(rhs) { } | 134 : SmartPointerBase<ObjectDeallocator<T>, T>(rhs) { } |
145 }; | 135 }; |
146 | 136 |
147 } } // namespace v8::internal | 137 } } // namespace v8::internal |
148 | 138 |
149 #endif // V8_SMART_POINTERS_H_ | 139 #endif // V8_SMART_POINTERS_H_ |
OLD | NEW |