OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef WebScopedPtr_h |
| 6 #define WebScopedPtr_h |
| 7 |
| 8 #include <stddef.h> |
| 9 |
| 10 namespace WebTestRunner { |
| 11 |
| 12 template<typename Deallocator, typename T> |
| 13 class WebScopedPtrBase { |
| 14 public: |
| 15 // Default constructor. Constructs an empty scoped pointer. |
| 16 inline WebScopedPtrBase() : m_ptr(0) { } |
| 17 |
| 18 // Constructs a scoped pointer from a plain one. |
| 19 explicit inline WebScopedPtrBase(T* ptr) : m_ptr(ptr) { } |
| 20 |
| 21 // Copy constructor removes the pointer from the original to avoid double |
| 22 // freeing. |
| 23 inline WebScopedPtrBase(const WebScopedPtrBase<Deallocator, T>& rhs) |
| 24 : m_ptr(rhs.m_ptr) |
| 25 { |
| 26 const_cast<WebScopedPtrBase<Deallocator, T>&>(rhs).m_ptr = 0; |
| 27 } |
| 28 |
| 29 // When the destructor of the scoped pointer is executed the plain pointer |
| 30 // is deleted using DeleteArray. This implies that you must allocate with |
| 31 // NewArray. |
| 32 inline ~WebScopedPtrBase() |
| 33 { |
| 34 if (m_ptr) |
| 35 Deallocator::Delete(m_ptr); |
| 36 } |
| 37 |
| 38 inline T* operator->() const { return m_ptr; } |
| 39 |
| 40 // You can get the underlying pointer out with the * operator. |
| 41 inline T* operator*() { return m_ptr; } |
| 42 |
| 43 // You can use [n] to index as if it was a plain pointer. |
| 44 inline T& operator[](size_t i) |
| 45 { |
| 46 return m_ptr[i]; |
| 47 } |
| 48 |
| 49 // You can use [n] to index as if it was a plain pointer. |
| 50 const inline T& operator[](size_t i) const |
| 51 { |
| 52 return m_ptr[i]; |
| 53 } |
| 54 |
| 55 // We don't have implicit conversion to a T* since that hinders migration: |
| 56 // You would not be able to change a method from returning a T* to |
| 57 // returning an WebScopedArrayPtr<T> and then get errors wherever it is used
. |
| 58 inline T* get() const { return m_ptr; } |
| 59 |
| 60 inline void reset(T* newValue = 0) |
| 61 { |
| 62 if (m_ptr) |
| 63 Deallocator::Delete(m_ptr); |
| 64 m_ptr = newValue; |
| 65 } |
| 66 |
| 67 // Assignment requires an empty (0) WebScopedArrayPtr as the receiver. Like |
| 68 // the copy constructor it removes the pointer in the original to avoid |
| 69 // double freeing. |
| 70 inline WebScopedPtrBase<Deallocator, T>& operator=(const WebScopedPtrBase<De
allocator, T>& rhs) |
| 71 { |
| 72 reset(rhs.m_ptr); |
| 73 const_cast<WebScopedPtrBase<Deallocator, T>&>(rhs).m_ptr = 0; |
| 74 return *this; |
| 75 } |
| 76 |
| 77 inline bool isEmpty() { return !m_ptr; } |
| 78 |
| 79 private: |
| 80 T* m_ptr; |
| 81 }; |
| 82 |
| 83 // A 'scoped array pointer' that calls DeleteArray on its pointer when the |
| 84 // destructor is called. |
| 85 template<typename T> |
| 86 struct ArrayDeallocator { |
| 87 static void Delete(T* array) |
| 88 { |
| 89 DeleteArray(array); |
| 90 } |
| 91 }; |
| 92 |
| 93 template<typename T> |
| 94 class WebScopedArrayPtr: public WebScopedPtrBase<ArrayDeallocator<T>, T> { |
| 95 public: |
| 96 inline WebScopedArrayPtr() { } |
| 97 explicit inline WebScopedArrayPtr(T* ptr) |
| 98 : WebScopedPtrBase<ArrayDeallocator<T>, T>(ptr) { } |
| 99 inline WebScopedArrayPtr(const WebScopedArrayPtr<T>& rhs) |
| 100 : WebScopedPtrBase<ArrayDeallocator<T>, T>(rhs) { } |
| 101 }; |
| 102 |
| 103 template<typename T> |
| 104 struct ObjectDeallocator { |
| 105 static void Delete(T* object) |
| 106 { |
| 107 delete object; |
| 108 } |
| 109 }; |
| 110 |
| 111 template<typename T> |
| 112 class WebScopedPtr: public WebScopedPtrBase<ObjectDeallocator<T>, T> { |
| 113 public: |
| 114 inline WebScopedPtr() { } |
| 115 explicit inline WebScopedPtr(T* ptr) |
| 116 : WebScopedPtrBase<ObjectDeallocator<T>, T>(ptr) { } |
| 117 inline WebScopedPtr(const WebScopedPtr<T>& rhs) |
| 118 : WebScopedPtrBase<ObjectDeallocator<T>, T>(rhs) { } |
| 119 }; |
| 120 |
| 121 } // namespace WebTestRunner |
| 122 |
| 123 #endif // WebScopedPtr_h |
OLD | NEW |