OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2014 Google Inc. All rights reserved. | 2 * Copyright (C) 2014 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 14 matching lines...) Expand all Loading... |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #ifndef WTF_RawPtr_h | 31 #ifndef WTF_RawPtr_h |
32 #define WTF_RawPtr_h | 32 #define WTF_RawPtr_h |
33 | 33 |
34 #include <algorithm> | 34 #include <algorithm> |
| 35 #include "wtf/HashTableDeletedValueType.h" |
35 | 36 |
36 // Ptr is a simple wrapper for a raw pointer that provides the | 37 // Ptr is a simple wrapper for a raw pointer that provides the |
37 // interface (get, clear) of other pointer types such as RefPtr, | 38 // interface (get, clear) of other pointer types such as RefPtr, |
38 // Persistent and Member. This is used for the Blink garbage | 39 // Persistent and Member. This is used for the Blink garbage |
39 // collection work in order to be able to write shared code that will | 40 // collection work in order to be able to write shared code that will |
40 // use reference counting or garbage collection based on a | 41 // use reference counting or garbage collection based on a |
41 // compile-time flag. | 42 // compile-time flag. |
42 | 43 |
43 namespace WTF { | 44 namespace WTF { |
44 | 45 |
45 template<typename T> | 46 template<typename T> |
46 class RawPtr { | 47 class RawPtr { |
47 WTF_DISALLOW_CONSTRUCTION_FROM_ZERO(RawPtr); | 48 WTF_DISALLOW_CONSTRUCTION_FROM_ZERO(RawPtr); |
48 WTF_DISALLOW_ZERO_ASSIGNMENT(RawPtr); | 49 WTF_DISALLOW_ZERO_ASSIGNMENT(RawPtr); |
49 public: | 50 public: |
50 RawPtr() : m_ptr(0) { } | 51 RawPtr() : m_ptr(0) { } |
51 RawPtr(std::nullptr_t) : m_ptr(0) { } | 52 RawPtr(std::nullptr_t) : m_ptr(0) { } |
52 RawPtr(T* ptr) : m_ptr(ptr) { } | 53 RawPtr(T* ptr) : m_ptr(ptr) { } |
53 RawPtr(const RawPtr& other) | 54 RawPtr(const RawPtr& other) |
54 : m_ptr(other.get()) | 55 : m_ptr(other.get()) |
55 { | 56 { |
56 } | 57 } |
57 | 58 |
58 template<typename U> | 59 template<typename U> |
59 RawPtr(const RawPtr<U>& other) | 60 RawPtr(const RawPtr<U>& other) |
60 : m_ptr(other.get()) | 61 : m_ptr(other.get()) |
61 { | 62 { |
62 } | 63 } |
63 | 64 |
| 65 // Hash table deleted values, which are only constructed and never copied or
destroyed. |
| 66 RawPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { } |
| 67 bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue
(); } |
| 68 |
64 T* get() const { return m_ptr; } | 69 T* get() const { return m_ptr; } |
65 void clear() { m_ptr = 0; } | 70 void clear() { m_ptr = 0; } |
66 // FIXME: oilpan: Remove release and leakRef once we remove RefPtrWillBeRawP
tr. | 71 // FIXME: oilpan: Remove release and leakRef once we remove RefPtrWillBeRawP
tr. |
67 RawPtr<T> release() | 72 RawPtr<T> release() |
68 { | 73 { |
69 RawPtr<T> tmp = m_ptr; | 74 RawPtr<T> tmp = m_ptr; |
70 m_ptr = 0; | 75 m_ptr = 0; |
71 return tmp; | 76 return tmp; |
72 } | 77 } |
73 T* leakRef() | 78 T* leakRef() |
(...skipping 26 matching lines...) Expand all Loading... |
100 operator T*() const { return m_ptr; } | 105 operator T*() const { return m_ptr; } |
101 T& operator*() const { return *m_ptr; } | 106 T& operator*() const { return *m_ptr; } |
102 T* operator->() const { return m_ptr; } | 107 T* operator->() const { return m_ptr; } |
103 bool operator!() const { return !m_ptr; } | 108 bool operator!() const { return !m_ptr; } |
104 | 109 |
105 void swap(RawPtr& o) | 110 void swap(RawPtr& o) |
106 { | 111 { |
107 std::swap(m_ptr, o.m_ptr); | 112 std::swap(m_ptr, o.m_ptr); |
108 } | 113 } |
109 | 114 |
| 115 static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); } |
110 | 116 |
111 private: | 117 private: |
112 T* m_ptr; | 118 T* m_ptr; |
113 }; | 119 }; |
114 | 120 |
115 template<typename T, typename U> inline RawPtr<T> static_pointer_cast(const RawP
tr<U>& p) | 121 template<typename T, typename U> inline RawPtr<T> static_pointer_cast(const RawP
tr<U>& p) |
116 { | 122 { |
117 return RawPtr<T>(static_cast<T*>(p.get())); | 123 return RawPtr<T>(static_cast<T*>(p.get())); |
118 } | 124 } |
119 | 125 |
120 template<typename T> inline T* getPtr(const RawPtr<T>& p) | 126 template<typename T> inline T* getPtr(const RawPtr<T>& p) |
121 { | 127 { |
122 return p.get(); | 128 return p.get(); |
123 } | 129 } |
124 | 130 |
125 } // namespace WTF | 131 } // namespace WTF |
126 | 132 |
127 using WTF::RawPtr; | 133 using WTF::RawPtr; |
128 | 134 |
129 #endif | 135 #endif |
OLD | NEW |