| 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 18 matching lines...) Expand all Loading... |
| 29 #define V8_SMART_POINTERS_H_ | 29 #define V8_SMART_POINTERS_H_ |
| 30 | 30 |
| 31 namespace v8 { | 31 namespace v8 { |
| 32 namespace internal { | 32 namespace internal { |
| 33 | 33 |
| 34 | 34 |
| 35 template<typename Deallocator, typename T> | 35 template<typename Deallocator, typename T> |
| 36 class SmartPointerBase { | 36 class SmartPointerBase { |
| 37 public: | 37 public: |
| 38 // Default constructor. Constructs an empty scoped pointer. | 38 // Default constructor. Constructs an empty scoped pointer. |
| 39 inline SmartPointerBase() : p_(NULL) {} | 39 SmartPointerBase() : p_(NULL) {} |
| 40 | 40 |
| 41 // Constructs a scoped pointer from a plain one. | 41 // Constructs a scoped pointer from a plain one. |
| 42 explicit inline SmartPointerBase(T* ptr) : p_(ptr) {} | 42 explicit SmartPointerBase(T* ptr) : p_(ptr) {} |
| 43 | 43 |
| 44 // Copy constructor removes the pointer from the original to avoid double | 44 // Copy constructor removes the pointer from the original to avoid double |
| 45 // freeing. | 45 // freeing. |
| 46 inline SmartPointerBase(const SmartPointerBase<Deallocator, T>& rhs) | 46 SmartPointerBase(const SmartPointerBase<Deallocator, T>& rhs) |
| 47 : p_(rhs.p_) { | 47 : p_(rhs.p_) { |
| 48 const_cast<SmartPointerBase<Deallocator, T>&>(rhs).p_ = NULL; | 48 const_cast<SmartPointerBase<Deallocator, T>&>(rhs).p_ = NULL; |
| 49 } | 49 } |
| 50 | 50 |
| 51 // When the destructor of the scoped pointer is executed the plain pointer | 51 T* operator->() const { return p_; } |
| 52 // is deleted using DeleteArray. This implies that you must allocate with | |
| 53 // NewArray. | |
| 54 inline ~SmartPointerBase() { if (p_) Deallocator::Delete(p_); } | |
| 55 | 52 |
| 56 inline T* operator->() const { return p_; } | 53 T& operator*() const { return *p_; } |
| 57 | 54 |
| 58 // You can get the underlying pointer out with the * operator. | 55 T* get() const { return p_; } |
| 59 inline T* operator*() { return p_; } | |
| 60 | 56 |
| 61 // You can use [n] to index as if it was a plain pointer. | 57 // You can use [n] to index as if it was a plain pointer. |
| 62 inline T& operator[](size_t i) { | 58 T& operator[](size_t i) { |
| 63 return p_[i]; | 59 return p_[i]; |
| 64 } | 60 } |
| 65 | 61 |
| 66 // You can use [n] to index as if it was a plain pointer. | 62 // You can use [n] to index as if it was a plain pointer. |
| 67 const inline T& operator[](size_t i) const { | 63 const T& operator[](size_t i) const { |
| 68 return p_[i]; | 64 return p_[i]; |
| 69 } | 65 } |
| 70 | 66 |
| 71 // We don't have implicit conversion to a T* since that hinders migration: | 67 // 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 | 68 // 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. | 69 // returning an SmartArrayPointer<T> and then get errors wherever it is used. |
| 74 | 70 |
| 75 | 71 |
| 76 // If you want to take out the plain pointer and don't want it automatically | 72 // 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 | 73 // deleted then call Detach(). Afterwards, the smart pointer is empty |
| 78 // (NULL). | 74 // (NULL). |
| 79 inline T* Detach() { | 75 T* Detach() { |
| 80 T* temp = p_; | 76 T* temp = p_; |
| 81 p_ = NULL; | 77 p_ = NULL; |
| 82 return temp; | 78 return temp; |
| 83 } | 79 } |
| 84 | 80 |
| 85 inline void Reset(T* new_value) { | 81 void Reset(T* new_value) { |
| 82 ASSERT(p_ == NULL || p_ != new_value); |
| 86 if (p_) Deallocator::Delete(p_); | 83 if (p_) Deallocator::Delete(p_); |
| 87 p_ = new_value; | 84 p_ = new_value; |
| 88 } | 85 } |
| 89 | 86 |
| 90 // Assignment requires an empty (NULL) SmartArrayPointer as the receiver. Like | 87 // Assignment requires an empty (NULL) SmartArrayPointer as the receiver. Like |
| 91 // the copy constructor it removes the pointer in the original to avoid | 88 // the copy constructor it removes the pointer in the original to avoid |
| 92 // double freeing. | 89 // double freeing. |
| 93 inline SmartPointerBase<Deallocator, T>& operator=( | 90 SmartPointerBase<Deallocator, T>& operator=( |
| 94 const SmartPointerBase<Deallocator, T>& rhs) { | 91 const SmartPointerBase<Deallocator, T>& rhs) { |
| 95 ASSERT(is_empty()); | 92 ASSERT(is_empty()); |
| 96 T* tmp = rhs.p_; // swap to handle self-assignment | 93 T* tmp = rhs.p_; // swap to handle self-assignment |
| 97 const_cast<SmartPointerBase<Deallocator, T>&>(rhs).p_ = NULL; | 94 const_cast<SmartPointerBase<Deallocator, T>&>(rhs).p_ = NULL; |
| 98 p_ = tmp; | 95 p_ = tmp; |
| 99 return *this; | 96 return *this; |
| 100 } | 97 } |
| 101 | 98 |
| 102 inline bool is_empty() { return p_ == NULL; } | 99 bool is_empty() const { return p_ == NULL; } |
| 100 |
| 101 protected: |
| 102 // When the destructor of the scoped pointer is executed the plain pointer |
| 103 // is deleted using DeleteArray. This implies that you must allocate with |
| 104 // NewArray. |
| 105 ~SmartPointerBase() { if (p_) Deallocator::Delete(p_); } |
| 103 | 106 |
| 104 private: | 107 private: |
| 105 T* p_; | 108 T* p_; |
| 106 }; | 109 }; |
| 107 | 110 |
| 108 // A 'scoped array pointer' that calls DeleteArray on its pointer when the | 111 // A 'scoped array pointer' that calls DeleteArray on its pointer when the |
| 109 // destructor is called. | 112 // destructor is called. |
| 110 | 113 |
| 111 template<typename T> | 114 template<typename T> |
| 112 struct ArrayDeallocator { | 115 struct ArrayDeallocator { |
| 113 static void Delete(T* array) { | 116 static void Delete(T* array) { |
| 114 DeleteArray(array); | 117 DeleteArray(array); |
| 115 } | 118 } |
| 116 }; | 119 }; |
| 117 | 120 |
| 118 | 121 |
| 119 template<typename T> | 122 template<typename T> |
| 120 class SmartArrayPointer: public SmartPointerBase<ArrayDeallocator<T>, T> { | 123 class SmartArrayPointer: public SmartPointerBase<ArrayDeallocator<T>, T> { |
| 121 public: | 124 public: |
| 122 inline SmartArrayPointer() { } | 125 SmartArrayPointer() { } |
| 123 explicit inline SmartArrayPointer(T* ptr) | 126 explicit SmartArrayPointer(T* ptr) |
| 124 : SmartPointerBase<ArrayDeallocator<T>, T>(ptr) { } | 127 : SmartPointerBase<ArrayDeallocator<T>, T>(ptr) { } |
| 125 inline SmartArrayPointer(const SmartArrayPointer<T>& rhs) | 128 SmartArrayPointer(const SmartArrayPointer<T>& rhs) |
| 126 : SmartPointerBase<ArrayDeallocator<T>, T>(rhs) { } | 129 : SmartPointerBase<ArrayDeallocator<T>, T>(rhs) { } |
| 127 }; | 130 }; |
| 128 | 131 |
| 129 | 132 |
| 130 template<typename T> | 133 template<typename T> |
| 131 struct ObjectDeallocator { | 134 struct ObjectDeallocator { |
| 132 static void Delete(T* object) { | 135 static void Delete(T* object) { |
| 133 delete object; | 136 delete object; |
| 134 } | 137 } |
| 135 }; | 138 }; |
| 136 | 139 |
| 137 | 140 |
| 138 template<typename T> | 141 template<typename T> |
| 139 class SmartPointer: public SmartPointerBase<ObjectDeallocator<T>, T> { | 142 class SmartPointer: public SmartPointerBase<ObjectDeallocator<T>, T> { |
| 140 public: | 143 public: |
| 141 inline SmartPointer() { } | 144 SmartPointer() { } |
| 142 explicit inline SmartPointer(T* ptr) | 145 explicit SmartPointer(T* ptr) |
| 143 : SmartPointerBase<ObjectDeallocator<T>, T>(ptr) { } | 146 : SmartPointerBase<ObjectDeallocator<T>, T>(ptr) { } |
| 144 inline SmartPointer(const SmartPointer<T>& rhs) | 147 SmartPointer(const SmartPointer<T>& rhs) |
| 145 : SmartPointerBase<ObjectDeallocator<T>, T>(rhs) { } | 148 : SmartPointerBase<ObjectDeallocator<T>, T>(rhs) { } |
| 146 }; | 149 }; |
| 147 | 150 |
| 148 } } // namespace v8::internal | 151 } } // namespace v8::internal |
| 149 | 152 |
| 150 #endif // V8_SMART_POINTERS_H_ | 153 #endif // V8_SMART_POINTERS_H_ |
| OLD | NEW |