| 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 28 matching lines...) Expand all Loading... |
| 39 | 39 |
| 40 // RawPtr is a simple wrapper for a raw pointer that provides the | 40 // RawPtr is a simple wrapper for a raw pointer that provides the |
| 41 // interface (get, clear) of other pointer types such as RefPtr, | 41 // interface (get, clear) of other pointer types such as RefPtr, |
| 42 // Persistent and Member. This is used for the Blink garbage | 42 // Persistent and Member. This is used for the Blink garbage |
| 43 // collection work in order to be able to write shared code that will | 43 // collection work in order to be able to write shared code that will |
| 44 // use reference counting or garbage collection based on a | 44 // use reference counting or garbage collection based on a |
| 45 // compile-time flag. | 45 // compile-time flag. |
| 46 | 46 |
| 47 namespace WTF { | 47 namespace WTF { |
| 48 | 48 |
| 49 template<typename T> | 49 template <typename T> |
| 50 class RawPtr { | 50 class RawPtr { |
| 51 public: | 51 public: |
| 52 RawPtr() | 52 RawPtr() { |
| 53 { | |
| 54 #if ENABLE(ASSERT) | 53 #if ENABLE(ASSERT) |
| 55 m_ptr = reinterpret_cast<T*>(rawPtrZapValue); | 54 m_ptr = reinterpret_cast<T*>(rawPtrZapValue); |
| 56 #endif | 55 #endif |
| 57 } | 56 } |
| 58 RawPtr(std::nullptr_t) : m_ptr(0) { } | 57 RawPtr(std::nullptr_t) |
| 59 RawPtr(T* ptr) : m_ptr(ptr) { } | 58 : m_ptr(0) {} |
| 60 explicit RawPtr(T& reference) : m_ptr(&reference) { } | 59 RawPtr(T* ptr) |
| 61 RawPtr(const RawPtr& other) | 60 : m_ptr(ptr) {} |
| 62 : m_ptr(other.get()) | 61 explicit RawPtr(T& reference) |
| 63 { | 62 : m_ptr(&reference) {} |
| 64 } | 63 RawPtr(const RawPtr& other) |
| 64 : m_ptr(other.get()) { |
| 65 } |
| 65 | 66 |
| 66 template<typename U> | 67 template <typename U> |
| 67 RawPtr(const RawPtr<U>& other, EnsurePtrConvertibleArgDecl(U, T)) | 68 RawPtr(const RawPtr<U>& other, EnsurePtrConvertibleArgDecl(U, T)) |
| 68 : m_ptr(other.get()) | 69 : m_ptr(other.get()) { |
| 69 { | 70 } |
| 70 } | |
| 71 | 71 |
| 72 // Hash table deleted values, which are only constructed and never copied or
destroyed. | 72 // Hash table deleted values, which are only constructed and never copied or d
estroyed. |
| 73 RawPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { } | 73 RawPtr(HashTableDeletedValueType) |
| 74 bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue
(); } | 74 : m_ptr(hashTableDeletedValue()) {} |
| 75 bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue()
; } |
| 75 | 76 |
| 76 T* get() const { return m_ptr; } | 77 T* get() const { return m_ptr; } |
| 77 void clear() { m_ptr = 0; } | 78 void clear() { m_ptr = 0; } |
| 78 RawPtr<T> release() | 79 RawPtr<T> release() { |
| 79 { | 80 RawPtr<T> tmp = m_ptr; |
| 80 RawPtr<T> tmp = m_ptr; | 81 m_ptr = 0; |
| 81 m_ptr = 0; | 82 return tmp; |
| 82 return tmp; | 83 } |
| 83 } | 84 T* leakRef() { |
| 84 T* leakRef() | 85 T* ptr = m_ptr; |
| 85 { | 86 m_ptr = 0; |
| 86 T* ptr = m_ptr; | 87 return ptr; |
| 87 m_ptr = 0; | 88 } |
| 88 return ptr; | 89 T* leakPtr() { |
| 89 } | 90 T* ptr = m_ptr; |
| 90 T* leakPtr() | 91 m_ptr = 0; |
| 91 { | 92 return ptr; |
| 92 T* ptr = m_ptr; | 93 } |
| 93 m_ptr = 0; | |
| 94 return ptr; | |
| 95 } | |
| 96 | 94 |
| 97 template<typename U> | 95 template <typename U> |
| 98 RawPtr& operator=(U* ptr) | 96 RawPtr& operator=(U* ptr) { |
| 99 { | 97 m_ptr = ptr; |
| 100 m_ptr = ptr; | 98 return *this; |
| 101 return *this; | 99 } |
| 102 } | |
| 103 | 100 |
| 104 template<typename U> | 101 template <typename U> |
| 105 RawPtr& operator=(RawPtr<U> ptr) | 102 RawPtr& operator=(RawPtr<U> ptr) { |
| 106 { | 103 m_ptr = ptr.get(); |
| 107 m_ptr = ptr.get(); | 104 return *this; |
| 108 return *this; | 105 } |
| 109 } | |
| 110 | 106 |
| 111 RawPtr& operator=(std::nullptr_t) | 107 RawPtr& operator=(std::nullptr_t) { |
| 112 { | 108 m_ptr = 0; |
| 113 m_ptr = 0; | 109 return *this; |
| 114 return *this; | 110 } |
| 115 } | |
| 116 | 111 |
| 117 operator T*() const { return m_ptr; } | 112 operator T*() const { return m_ptr; } |
| 118 T& operator*() const { return *m_ptr; } | 113 T& operator*() const { return *m_ptr; } |
| 119 T* operator->() const { return m_ptr; } | 114 T* operator->() const { return m_ptr; } |
| 120 bool operator!() const { return !m_ptr; } | 115 bool operator!() const { return !m_ptr; } |
| 121 | 116 |
| 122 void swap(RawPtr& o) | 117 void swap(RawPtr& o) { |
| 123 { | 118 std::swap(m_ptr, o.m_ptr); |
| 124 std::swap(m_ptr, o.m_ptr); | 119 } |
| 125 } | |
| 126 | 120 |
| 127 static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); } | 121 static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); } |
| 128 | 122 |
| 129 private: | 123 private: |
| 130 static const uintptr_t rawPtrZapValue = 0x3a3a3a3a; | 124 static const uintptr_t rawPtrZapValue = 0x3a3a3a3a; |
| 131 T* m_ptr; | 125 T* m_ptr; |
| 132 }; | 126 }; |
| 133 | 127 |
| 134 template<typename T, typename U> inline RawPtr<T> static_pointer_cast(const RawP
tr<U>& p) | 128 template <typename T, typename U> |
| 135 { | 129 inline RawPtr<T> static_pointer_cast(const RawPtr<U>& p) { |
| 136 return RawPtr<T>(static_cast<T*>(p.get())); | 130 return RawPtr<T>(static_cast<T*>(p.get())); |
| 137 } | 131 } |
| 138 | 132 |
| 139 template<typename T> inline T* getPtr(const RawPtr<T>& p) | 133 template <typename T> |
| 140 { | 134 inline T* getPtr(const RawPtr<T>& p) { |
| 141 return p.get(); | 135 return p.get(); |
| 142 } | 136 } |
| 143 | 137 |
| 144 } // namespace WTF | 138 } // namespace WTF |
| 145 | 139 |
| 146 using WTF::RawPtr; | 140 using WTF::RawPtr; |
| 147 | 141 |
| 148 #endif | 142 #endif |
| OLD | NEW |