| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_BASE_SMART_POINTERS_H_ | 5 #ifndef V8_BASE_SMART_POINTERS_H_ |
| 6 #define V8_BASE_SMART_POINTERS_H_ | 6 #define V8_BASE_SMART_POINTERS_H_ |
| 7 | 7 |
| 8 #include "src/base/logging.h" | 8 #include "src/base/logging.h" |
| 9 | 9 |
| 10 namespace v8 { | 10 namespace v8 { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 // is deleted using DeleteArray. This implies that you must allocate with | 76 // is deleted using DeleteArray. This implies that you must allocate with |
| 77 // NewArray. | 77 // NewArray. |
| 78 ~SmartPointerBase() { | 78 ~SmartPointerBase() { |
| 79 if (p_) Deallocator::Delete(p_); | 79 if (p_) Deallocator::Delete(p_); |
| 80 } | 80 } |
| 81 | 81 |
| 82 private: | 82 private: |
| 83 T* p_; | 83 T* p_; |
| 84 }; | 84 }; |
| 85 | 85 |
| 86 // A 'scoped array pointer' that calls DeleteArray on its pointer when the | |
| 87 // destructor is called. | |
| 88 | |
| 89 template <typename T> | |
| 90 struct ArrayDeallocator { | |
| 91 static void Delete(T* array) { delete[] array; } | |
| 92 }; | |
| 93 | |
| 94 | |
| 95 template <typename T> | |
| 96 class SmartArrayPointer : public SmartPointerBase<ArrayDeallocator<T>, T> { | |
| 97 public: | |
| 98 SmartArrayPointer() {} | |
| 99 explicit SmartArrayPointer(T* ptr) | |
| 100 : SmartPointerBase<ArrayDeallocator<T>, T>(ptr) {} | |
| 101 SmartArrayPointer(const SmartArrayPointer<T>& rhs) | |
| 102 : SmartPointerBase<ArrayDeallocator<T>, T>(rhs) {} | |
| 103 }; | |
| 104 | |
| 105 | |
| 106 template <typename T> | 86 template <typename T> |
| 107 struct ObjectDeallocator { | 87 struct ObjectDeallocator { |
| 108 static void Delete(T* object) { delete object; } | 88 static void Delete(T* object) { delete object; } |
| 109 }; | 89 }; |
| 110 | 90 |
| 111 template <typename T> | 91 template <typename T> |
| 112 class SmartPointer : public SmartPointerBase<ObjectDeallocator<T>, T> { | 92 class SmartPointer : public SmartPointerBase<ObjectDeallocator<T>, T> { |
| 113 public: | 93 public: |
| 114 SmartPointer() {} | 94 SmartPointer() {} |
| 115 explicit SmartPointer(T* ptr) | 95 explicit SmartPointer(T* ptr) |
| 116 : SmartPointerBase<ObjectDeallocator<T>, T>(ptr) {} | 96 : SmartPointerBase<ObjectDeallocator<T>, T>(ptr) {} |
| 117 SmartPointer(const SmartPointer<T>& rhs) | 97 SmartPointer(const SmartPointer<T>& rhs) |
| 118 : SmartPointerBase<ObjectDeallocator<T>, T>(rhs) {} | 98 : SmartPointerBase<ObjectDeallocator<T>, T>(rhs) {} |
| 119 }; | 99 }; |
| 120 | 100 |
| 121 } // namespace base | 101 } // namespace base |
| 122 } // namespace v8 | 102 } // namespace v8 |
| 123 | 103 |
| 124 #endif // V8_SMART_POINTERS_H_ | 104 #endif // V8_SMART_POINTERS_H_ |
| OLD | NEW |