| 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 24 matching lines...) Expand all Loading... |
| 35 | 35 |
| 36 #if INSIDE_BLINK | 36 #if INSIDE_BLINK |
| 37 #include "platform/heap/Handle.h" | 37 #include "platform/heap/Handle.h" |
| 38 #include "wtf/PassRefPtr.h" | 38 #include "wtf/PassRefPtr.h" |
| 39 #include "wtf/TypeTraits.h" | 39 #include "wtf/TypeTraits.h" |
| 40 #endif | 40 #endif |
| 41 | 41 |
| 42 namespace blink { | 42 namespace blink { |
| 43 | 43 |
| 44 #if INSIDE_BLINK | 44 #if INSIDE_BLINK |
| 45 enum LifetimeManagementType { | |
| 46 RefCountedLifetime, | |
| 47 GarbageCollectedLifetime, | |
| 48 RefCountedGarbageCollectedLifetime | |
| 49 }; | |
| 50 | |
| 51 template<typename T> | 45 template<typename T> |
| 52 class LifetimeOf { | 46 class PtrStorageImpl { |
| 53 static const bool isGarbageCollected = WTF::IsSubclassOfTemplate<T, GarbageC
ollected>::value; | |
| 54 static const bool isRefCountedGarbageCollected = WTF::IsSubclassOfTemplate<T
, RefCountedGarbageCollected>::value; | |
| 55 public: | |
| 56 static const LifetimeManagementType value = | |
| 57 !isGarbageCollected ? RefCountedLifetime : | |
| 58 isRefCountedGarbageCollected ? RefCountedGarbageCollectedLifetime : Garb
ageCollectedLifetime; | |
| 59 }; | |
| 60 | |
| 61 template<typename T, LifetimeManagementType lifetime> | |
| 62 class PtrStorageImpl; | |
| 63 | |
| 64 template<typename T> | |
| 65 class PtrStorageImpl<T, RefCountedLifetime> { | |
| 66 public: | 47 public: |
| 67 typedef PassRefPtr<T> BlinkPtrType; | 48 typedef PassRefPtr<T> BlinkPtrType; |
| 68 | 49 |
| 69 void assign(const BlinkPtrType& val) | 50 void assign(const BlinkPtrType& val) |
| 70 { | 51 { |
| 71 release(); | 52 release(); |
| 72 m_ptr = val.leakRef(); | 53 m_ptr = val.leakRef(); |
| 73 } | 54 } |
| 74 | 55 |
| 75 void assign(const PtrStorageImpl& other) | 56 void assign(const PtrStorageImpl& other) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 86 { | 67 { |
| 87 WTF::derefIfNotNull(m_ptr); | 68 WTF::derefIfNotNull(m_ptr); |
| 88 m_ptr = 0; | 69 m_ptr = 0; |
| 89 } | 70 } |
| 90 | 71 |
| 91 private: | 72 private: |
| 92 T* m_ptr; | 73 T* m_ptr; |
| 93 }; | 74 }; |
| 94 | 75 |
| 95 template<typename T> | 76 template<typename T> |
| 96 class PtrStorageImpl<T, GarbageCollectedLifetime> { | 77 class PtrStorage : public PtrStorageImpl<T> { |
| 97 public: | |
| 98 void assign(const RawPtr<T>& val) | |
| 99 { | |
| 100 if (!val) { | |
| 101 release(); | |
| 102 return; | |
| 103 } | |
| 104 | |
| 105 if (!m_handle) | |
| 106 m_handle = new Persistent<T>(); | |
| 107 | |
| 108 (*m_handle) = val; | |
| 109 } | |
| 110 | |
| 111 void assign(T* ptr) { assign(RawPtr<T>(ptr)); } | |
| 112 template<typename U> void assign(const RawPtr<U>& val) { assign(RawPtr<T>(va
l)); } | |
| 113 | |
| 114 void assign(const PtrStorageImpl& other) { assign(other.get()); } | |
| 115 | |
| 116 T* get() const { return m_handle ? m_handle->get() : 0; } | |
| 117 | |
| 118 void release() | |
| 119 { | |
| 120 delete m_handle; | |
| 121 m_handle = 0; | |
| 122 } | |
| 123 | |
| 124 private: | |
| 125 Persistent<T>* m_handle; | |
| 126 }; | |
| 127 | |
| 128 template<typename T> | |
| 129 class PtrStorageImpl<T, RefCountedGarbageCollectedLifetime> : public PtrStorageI
mpl<T, GarbageCollectedLifetime> { | |
| 130 public: | |
| 131 void assign(const PassRefPtr<T>& val) { PtrStorageImpl<T, GarbageCollectedLi
fetime>::assign(val.get()); } | |
| 132 | |
| 133 void assign(const PtrStorageImpl& other) { PtrStorageImpl<T, GarbageCollecte
dLifetime>::assign(other.get()); } | |
| 134 }; | |
| 135 | |
| 136 template<typename T> | |
| 137 class PtrStorage : public PtrStorageImpl<T, LifetimeOf<T>::value> { | |
| 138 public: | 78 public: |
| 139 static PtrStorage& fromSlot(void** slot) | 79 static PtrStorage& fromSlot(void** slot) |
| 140 { | 80 { |
| 141 COMPILE_ASSERT(sizeof(PtrStorage) == sizeof(void*), PtrStorage_must_be_p
ointer_size); | 81 COMPILE_ASSERT(sizeof(PtrStorage) == sizeof(void*), PtrStorage_must_be_p
ointer_size); |
| 142 return *reinterpret_cast<PtrStorage*>(slot); | 82 return *reinterpret_cast<PtrStorage*>(slot); |
| 143 } | 83 } |
| 144 | 84 |
| 145 static const PtrStorage& fromSlot(void* const* slot) | 85 static const PtrStorage& fromSlot(void* const* slot) |
| 146 { | 86 { |
| 147 COMPILE_ASSERT(sizeof(PtrStorage) == sizeof(void*), PtrStorage_must_be_p
ointer_size); | 87 COMPILE_ASSERT(sizeof(PtrStorage) == sizeof(void*), PtrStorage_must_be_p
ointer_size); |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 // Disable the copy constructor; classes that contain a WebPrivatePtr | 201 // Disable the copy constructor; classes that contain a WebPrivatePtr |
| 262 // should implement their copy constructor using assign(). | 202 // should implement their copy constructor using assign(). |
| 263 WebPrivatePtr(const WebPrivatePtr<T>&); | 203 WebPrivatePtr(const WebPrivatePtr<T>&); |
| 264 | 204 |
| 265 void* m_storage; | 205 void* m_storage; |
| 266 }; | 206 }; |
| 267 | 207 |
| 268 } // namespace blink | 208 } // namespace blink |
| 269 | 209 |
| 270 #endif | 210 #endif |
| OLD | NEW |