| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2014 Google Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions are | |
| 6 * met: | |
| 7 * | |
| 8 * * Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * * Redistributions in binary form must reproduce the above | |
| 11 * copyright notice, this list of conditions and the following disclaimer | |
| 12 * in the documentation and/or other materials provided with the | |
| 13 * distribution. | |
| 14 * * Neither the name of Google Inc. nor the names of its | |
| 15 * contributors may be used to endorse or promote products derived from | |
| 16 * this software without specific prior written permission. | |
| 17 * | |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 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. | |
| 29 */ | |
| 30 | |
| 31 #ifndef WTF_RawPtr_h | |
| 32 #define WTF_RawPtr_h | |
| 33 | |
| 34 #include "wtf/Allocator.h" | |
| 35 #include "wtf/HashTableDeletedValueType.h" | |
| 36 #include "wtf/TypeTraits.h" | |
| 37 #include <algorithm> | |
| 38 #include <stdint.h> | |
| 39 #include <utility> | |
| 40 | |
| 41 // RawPtr is a simple wrapper for a raw pointer that provides the | |
| 42 // interface (get, clear) of other pointer types such as RefPtr, | |
| 43 // Persistent and Member. This is used for the Blink garbage | |
| 44 // collection work in order to be able to write shared code that will | |
| 45 // use reference counting or garbage collection based on a | |
| 46 // compile-time flag. | |
| 47 | |
| 48 namespace WTF { | |
| 49 | |
| 50 template<typename T> | |
| 51 class RawPtr { | |
| 52 USING_FAST_MALLOC(RawPtr); | |
| 53 public: | |
| 54 RawPtr() | |
| 55 { | |
| 56 #if ENABLE(ASSERT) | |
| 57 m_ptr = reinterpret_cast<T*>(rawPtrZapValue); | |
| 58 #endif | |
| 59 } | |
| 60 RawPtr(std::nullptr_t) : m_ptr(0) { } | |
| 61 RawPtr(T* ptr) : m_ptr(ptr) { } | |
| 62 explicit RawPtr(T& reference) : m_ptr(&reference) { } | |
| 63 RawPtr(const RawPtr& other) | |
| 64 : m_ptr(other.get()) | |
| 65 { | |
| 66 } | |
| 67 | |
| 68 template<typename U> | |
| 69 RawPtr(const RawPtr<U>& other, EnsurePtrConvertibleArgDecl(U, T)) | |
| 70 : m_ptr(other.get()) | |
| 71 { | |
| 72 } | |
| 73 | |
| 74 // Hash table deleted values, which are only constructed and never copied or
destroyed. | |
| 75 RawPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { } | |
| 76 bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue
(); } | |
| 77 | |
| 78 T* get() const { return m_ptr; } | |
| 79 void clear() { m_ptr = 0; } | |
| 80 RawPtr<T> release() | |
| 81 { | |
| 82 RawPtr<T> tmp = m_ptr; | |
| 83 m_ptr = 0; | |
| 84 return tmp; | |
| 85 } | |
| 86 T* leakRef() | |
| 87 { | |
| 88 T* ptr = m_ptr; | |
| 89 m_ptr = 0; | |
| 90 return ptr; | |
| 91 } | |
| 92 T* leakPtr() | |
| 93 { | |
| 94 T* ptr = m_ptr; | |
| 95 m_ptr = 0; | |
| 96 return ptr; | |
| 97 } | |
| 98 | |
| 99 template<typename U> | |
| 100 RawPtr& operator=(U* ptr) | |
| 101 { | |
| 102 m_ptr = ptr; | |
| 103 return *this; | |
| 104 } | |
| 105 | |
| 106 template<typename U> | |
| 107 RawPtr& operator=(RawPtr<U> ptr) | |
| 108 { | |
| 109 m_ptr = ptr.get(); | |
| 110 return *this; | |
| 111 } | |
| 112 | |
| 113 RawPtr& operator=(std::nullptr_t) | |
| 114 { | |
| 115 m_ptr = 0; | |
| 116 return *this; | |
| 117 } | |
| 118 | |
| 119 operator T*() const { return m_ptr; } | |
| 120 T& operator*() const { return *m_ptr; } | |
| 121 T* operator->() const { return m_ptr; } | |
| 122 bool operator!() const { return !m_ptr; } | |
| 123 | |
| 124 void swap(RawPtr& o) | |
| 125 { | |
| 126 std::swap(m_ptr, o.m_ptr); | |
| 127 } | |
| 128 | |
| 129 static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); } | |
| 130 | |
| 131 private: | |
| 132 static const uintptr_t rawPtrZapValue = 0x3a3a3a3a; | |
| 133 T* m_ptr; | |
| 134 }; | |
| 135 | |
| 136 template<typename T, typename U> inline RawPtr<T> static_pointer_cast(const RawP
tr<U>& p) | |
| 137 { | |
| 138 return RawPtr<T>(static_cast<T*>(p.get())); | |
| 139 } | |
| 140 | |
| 141 template<typename T> inline T* getPtr(const RawPtr<T>& p) | |
| 142 { | |
| 143 return p.get(); | |
| 144 } | |
| 145 | |
| 146 } // namespace WTF | |
| 147 | |
| 148 using WTF::RawPtr; | |
| 149 | |
| 150 #endif | |
| OLD | NEW |