| OLD | NEW | 
|---|
| 1 /* | 1 /* | 
| 2  * Copyright (C) 2010 Google Inc. All rights reserved. | 2  * Copyright (C) 2010 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 16 matching lines...) Expand all  Loading... | 
| 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 WebPrivatePtr_h | 31 #ifndef WebPrivatePtr_h | 
| 32 #define WebPrivatePtr_h | 32 #define WebPrivatePtr_h | 
| 33 | 33 | 
| 34 #include "WebCommon.h" | 34 #include "WebCommon.h" | 
| 35 | 35 | 
| 36 #if INSIDE_BLINK | 36 #if INSIDE_BLINK | 
|  | 37 #include "heap/Handle.h" | 
| 37 #include "wtf/PassRefPtr.h" | 38 #include "wtf/PassRefPtr.h" | 
| 38 #endif | 39 #endif | 
| 39 | 40 | 
| 40 namespace blink { | 41 namespace blink { | 
| 41 | 42 | 
|  | 43 #if INSIDE_BLINK | 
|  | 44 template<typename T, bool IsGarbageCollected> | 
|  | 45 class PtrStorageImpl; | 
|  | 46 | 
|  | 47 template<typename T> | 
|  | 48 class PtrStorageImpl<T, false> { | 
|  | 49 public: | 
|  | 50     typedef PassRefPtr<T> BlinkPtrType; | 
|  | 51 | 
|  | 52     void assign(const BlinkPtrType& val) | 
|  | 53     { | 
|  | 54         release(); | 
|  | 55         m_ptr = val.leakRef(); | 
|  | 56     } | 
|  | 57 | 
|  | 58     void assign(const PtrStorageImpl& other) | 
|  | 59     { | 
|  | 60         release(); | 
|  | 61         T* val = other.get(); | 
|  | 62         if (val) | 
|  | 63             val->ref(); | 
|  | 64         m_ptr = val; | 
|  | 65     } | 
|  | 66 | 
|  | 67     T* get() const { return m_ptr; } | 
|  | 68 | 
|  | 69     void release() | 
|  | 70     { | 
|  | 71         if (m_ptr) | 
|  | 72             m_ptr->deref(); | 
|  | 73         m_ptr = 0; | 
|  | 74     } | 
|  | 75 | 
|  | 76 private: | 
|  | 77     T* m_ptr; | 
|  | 78 }; | 
|  | 79 | 
|  | 80 template<typename T> | 
|  | 81 class PtrStorageImpl<T, true> { | 
|  | 82     typedef WebCore::Persistent<T> HandleType; | 
|  | 83 public: | 
|  | 84     typedef RawPtr<T> BlinkPtrType; | 
|  | 85 | 
|  | 86     void assign(const BlinkPtrType& val) | 
|  | 87     { | 
|  | 88         if (!val) { | 
|  | 89             release(); | 
|  | 90             return; | 
|  | 91         } | 
|  | 92 | 
|  | 93         if (!m_handle) | 
|  | 94             m_handle = new HandleType(); | 
|  | 95 | 
|  | 96         (*m_handle) = val; | 
|  | 97     } | 
|  | 98 | 
|  | 99     void assign(const PtrStorageImpl& other) { assign(other.get()); } | 
|  | 100 | 
|  | 101     T* get() const { return m_handle ? m_handle->get() : 0; } | 
|  | 102 | 
|  | 103     void release() | 
|  | 104     { | 
|  | 105         delete m_handle; | 
|  | 106         m_handle = 0; | 
|  | 107     } | 
|  | 108 | 
|  | 109 private: | 
|  | 110     HandleType* m_handle; | 
|  | 111 }; | 
|  | 112 | 
|  | 113 template<typename T> | 
|  | 114 class PtrStorage : public PtrStorageImpl<T, WTF::IsSubclassOfTemplate<T, WebCore
     ::GarbageCollected>::value> { | 
|  | 115 public: | 
|  | 116     static PtrStorage& fromSlot(void** slot) | 
|  | 117     { | 
|  | 118         COMPILE_ASSERT(sizeof(PtrStorage) == sizeof(void*), PtrStorage_must_be_p
     ointer_size); | 
|  | 119         return *reinterpret_cast<PtrStorage*>(slot); | 
|  | 120     } | 
|  | 121 | 
|  | 122     static const PtrStorage& fromSlot(void* const* slot) | 
|  | 123     { | 
|  | 124         COMPILE_ASSERT(sizeof(PtrStorage) == sizeof(void*), PtrStorage_must_be_p
     ointer_size); | 
|  | 125         return *reinterpret_cast<const PtrStorage*>(slot); | 
|  | 126     } | 
|  | 127 | 
|  | 128 private: | 
|  | 129     PtrStorage(); | 
|  | 130     PtrStorage(const PtrStorage&); | 
|  | 131 }; | 
|  | 132 #endif | 
|  | 133 | 
| 42 // This class is an implementation detail of the Blink API. It exists to help | 134 // This class is an implementation detail of the Blink API. It exists to help | 
| 43 // simplify the implementation of Blink interfaces that merely wrap a reference | 135 // simplify the implementation of Blink interfaces that merely wrap a reference | 
| 44 // counted WebCore class. | 136 // counted WebCore class. | 
| 45 // | 137 // | 
| 46 // A typical implementation of a class which uses WebPrivatePtr might look like | 138 // A typical implementation of a class which uses WebPrivatePtr might look like | 
| 47 // this: | 139 // this: | 
| 48 //    class WebFoo { | 140 //    class WebFoo { | 
| 49 //    public: | 141 //    public: | 
| 50 //        BLINK_EXPORT ~WebFoo(); | 142 //        BLINK_EXPORT ~WebFoo(); | 
| 51 //        WebFoo() { } | 143 //        WebFoo() { } | 
| (...skipping 19 matching lines...) Expand all  Loading... | 
| 71 //        WebPrivatePtr<WebCore::Foo> m_private; | 163 //        WebPrivatePtr<WebCore::Foo> m_private; | 
| 72 //    }; | 164 //    }; | 
| 73 // | 165 // | 
| 74 //    // WebFoo.cpp | 166 //    // WebFoo.cpp | 
| 75 //    WebFoo::~WebFoo() { m_private.reset(); } | 167 //    WebFoo::~WebFoo() { m_private.reset(); } | 
| 76 //    void WebFoo::assign(const WebFoo& other) { ... } | 168 //    void WebFoo::assign(const WebFoo& other) { ... } | 
| 77 // | 169 // | 
| 78 template <typename T> | 170 template <typename T> | 
| 79 class WebPrivatePtr { | 171 class WebPrivatePtr { | 
| 80 public: | 172 public: | 
| 81     WebPrivatePtr() : m_ptr(0) { } | 173     WebPrivatePtr() : m_storage(0) { } | 
| 82     ~WebPrivatePtr() | 174     ~WebPrivatePtr() | 
| 83     { | 175     { | 
| 84         // We don't destruct the object pointed by m_ptr here because we don't | 176         // We don't destruct the object pointed by m_ptr here because we don't | 
| 85         // want to expose destructors of core classes to embedders. We should | 177         // want to expose destructors of core classes to embedders. We should | 
| 86         // call reset() manually in destructors of classes with WebPrivatePtr | 178         // call reset() manually in destructors of classes with WebPrivatePtr | 
| 87         // members. | 179         // members. | 
| 88         BLINK_ASSERT(!m_ptr); | 180         BLINK_ASSERT(!m_storage); | 
| 89     } | 181     } | 
| 90 | 182 | 
| 91     bool isNull() const { return !m_ptr; } | 183     bool isNull() const { return !m_storage; } | 
| 92 | 184 | 
| 93 #if INSIDE_BLINK | 185 #if INSIDE_BLINK | 
| 94     WebPrivatePtr(const PassRefPtr<T>& prp) | 186     template<typename U> | 
| 95         : m_ptr(prp.leakRef()) | 187     WebPrivatePtr(const U& ptr) // Can't use PtrStorage<T>::BlinkPtrType because
      it will cause early instantiation. | 
|  | 188         : m_storage(0) | 
| 96     { | 189     { | 
|  | 190         storage().assign(ptr); | 
| 97     } | 191     } | 
| 98 | 192 | 
| 99     void reset() | 193     void reset() { storage().release(); } | 
| 100     { |  | 
| 101         assign(0); |  | 
| 102     } |  | 
| 103 | 194 | 
| 104     WebPrivatePtr<T>& operator=(const WebPrivatePtr<T>& other) | 195     WebPrivatePtr<T>& operator=(const WebPrivatePtr<T>& other) | 
| 105     { | 196     { | 
| 106         T* p = other.m_ptr; | 197         storage().assign(other.storage()); | 
| 107         if (p) |  | 
| 108             p->ref(); |  | 
| 109         assign(p); |  | 
| 110         return *this; | 198         return *this; | 
| 111     } | 199     } | 
| 112 | 200 | 
| 113     WebPrivatePtr<T>& operator=(const PassRefPtr<T>& prp) | 201     template<typename U> | 
|  | 202     WebPrivatePtr<T>& operator=(const U& ptr) // Can't use PtrStorage<T>::BlinkP
     trType because it will cause early instantiation. | 
| 114     { | 203     { | 
| 115         assign(prp.leakRef()); | 204         storage().assign(ptr); | 
| 116         return *this; | 205         return *this; | 
| 117     } | 206     } | 
| 118 | 207 | 
| 119     T* get() const | 208     T* get() const { return storage().get(); } | 
| 120     { |  | 
| 121         return m_ptr; |  | 
| 122     } |  | 
| 123 | 209 | 
| 124     T* operator->() const | 210     T* operator->() const | 
| 125     { | 211     { | 
| 126         ASSERT(m_ptr); | 212         ASSERT(m_storage); | 
| 127         return m_ptr; | 213         return get(); | 
| 128     } | 214     } | 
| 129 #endif | 215 #endif | 
| 130 | 216 | 
| 131 private: | 217 private: | 
| 132 #if INSIDE_BLINK | 218 #if INSIDE_BLINK | 
| 133     void assign(T* p) | 219     PtrStorage<T>& storage() { return PtrStorage<T>::fromSlot(&m_storage); } | 
| 134     { | 220     const PtrStorage<T>& storage() const { return PtrStorage<T>::fromSlot(&m_sto
     rage); } | 
| 135         // p is already ref'd for us by the caller | 221 #endif | 
| 136         if (m_ptr) | 222 | 
| 137             m_ptr->deref(); | 223 #if !INSIDE_BLINK | 
| 138         m_ptr = p; |  | 
| 139     } |  | 
| 140 #else |  | 
| 141     // Disable the assignment operator; we define it above for when | 224     // Disable the assignment operator; we define it above for when | 
| 142     // INSIDE_BLINK is set, but we need to make sure that it is not | 225     // INSIDE_BLINK is set, but we need to make sure that it is not | 
| 143     // used outside there; the compiler-provided version won't handle reference | 226     // used outside there; the compiler-provided version won't handle reference | 
| 144     // counting properly. | 227     // counting properly. | 
| 145     WebPrivatePtr<T>& operator=(const WebPrivatePtr<T>& other); | 228     WebPrivatePtr<T>& operator=(const WebPrivatePtr<T>& other); | 
| 146 #endif | 229 #endif | 
| 147     // Disable the copy constructor; classes that contain a WebPrivatePtr | 230     // Disable the copy constructor; classes that contain a WebPrivatePtr | 
| 148     // should implement their copy constructor using assign(). | 231     // should implement their copy constructor using assign(). | 
| 149     WebPrivatePtr(const WebPrivatePtr<T>&); | 232     WebPrivatePtr(const WebPrivatePtr<T>&); | 
| 150 | 233 | 
| 151     T* m_ptr; | 234     void* m_storage; | 
| 152 }; | 235 }; | 
| 153 | 236 | 
| 154 } // namespace blink | 237 } // namespace blink | 
| 155 | 238 | 
| 156 #endif | 239 #endif | 
| OLD | NEW | 
|---|