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 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 public: | 129 public: |
130 using Type = CrossThreadPersistent<T>; | 130 using Type = CrossThreadPersistent<T>; |
131 }; | 131 }; |
132 | 132 |
133 template <typename T> | 133 template <typename T> |
134 struct WebPrivatePtrPersistentStorageType<T, WebPrivatePtrDestructionCrossThread , WebPrivatePtrStrength::Weak> { | 134 struct WebPrivatePtrPersistentStorageType<T, WebPrivatePtrDestructionCrossThread , WebPrivatePtrStrength::Weak> { |
135 public: | 135 public: |
136 using Type = CrossThreadWeakPersistent<T>; | 136 using Type = CrossThreadWeakPersistent<T>; |
137 }; | 137 }; |
138 | 138 |
139 template<typename T, WebPrivatePtrDestruction crossThreadDestruction, WebPrivate PtrStrength strongOrWeak> | 139 template<typename T, WebPrivatePtrStrength strongOrWeak> |
140 class PtrStorageImpl<T, crossThreadDestruction, strongOrWeak, GarbageCollectedLi fetime> { | 140 class PtrStorageImpl<T, WebPrivatePtrDestructionCrossThread, strongOrWeak, Garba geCollectedLifetime> { |
141 public: | 141 public: |
142 void assign(T* val) | 142 void assign(T* val) |
143 { | 143 { |
144 if (!val) { | |
145 release(); | |
146 return; | |
147 } | |
148 | |
149 if (!m_handle) | |
150 m_handle = new (typename WebPrivatePtrPersistentStorageType<T, WebPr ivatePtrDestructionCrossThread, strongOrWeak>::Type)(); | |
151 | |
152 (*m_handle) = val; | |
153 } | |
154 | |
155 template<typename U> void assign(U* val) { assign(static_cast<T*>(val)); } | |
156 | |
157 void assign(const PtrStorageImpl& other) { assign(other.get()); } | |
158 | |
159 T* get() const { return m_handle ? m_handle->unsafeGet() : nullptr; } | |
sof
2016/06/02 07:00:12
You can use a trait (over WebPrivatePtrDestruction
| |
160 | |
161 void release() | |
162 { | |
163 delete m_handle; | |
164 m_handle = nullptr; | |
165 } | |
166 | |
167 private: | |
168 typename WebPrivatePtrPersistentStorageType<T, WebPrivatePtrDestructionCross Thread, strongOrWeak>::Type* m_handle; | |
169 }; | |
170 | |
171 template<typename T, WebPrivatePtrStrength strongOrWeak> | |
172 class PtrStorageImpl<T, WebPrivatePtrDestructionSameThread, strongOrWeak, Garbag eCollectedLifetime> { | |
173 public: | |
174 void assign(T* val) | |
175 { | |
144 if (!val) { | 176 if (!val) { |
145 release(); | 177 release(); |
146 return; | 178 return; |
147 } | 179 } |
148 | 180 |
149 if (!m_handle) | 181 if (!m_handle) |
150 m_handle = new (typename WebPrivatePtrPersistentStorageType<T, cross ThreadDestruction, strongOrWeak>::Type)(); | 182 m_handle = new (typename WebPrivatePtrPersistentStorageType<T, WebPr ivatePtrDestructionSameThread, strongOrWeak>::Type)(); |
151 | 183 |
152 (*m_handle) = val; | 184 (*m_handle) = val; |
153 } | 185 } |
154 | 186 |
155 template<typename U> void assign(U* val) { assign(static_cast<T*>(val)); } | 187 template<typename U> void assign(U* val) { assign(static_cast<T*>(val)); } |
156 | 188 |
157 void assign(const PtrStorageImpl& other) { assign(other.get()); } | 189 void assign(const PtrStorageImpl& other) { assign(other.get()); } |
158 | 190 |
159 T* get() const { return m_handle ? m_handle->get() : 0; } | 191 T* get() const { return m_handle ? m_handle->get() : 0; } |
160 | 192 |
161 void release() | 193 void release() |
162 { | 194 { |
163 delete m_handle; | 195 delete m_handle; |
164 m_handle = 0; | 196 m_handle = 0; |
165 } | 197 } |
166 | 198 |
167 private: | 199 private: |
168 typename WebPrivatePtrPersistentStorageType<T, crossThreadDestruction, stron gOrWeak>::Type* m_handle; | 200 typename WebPrivatePtrPersistentStorageType<T, WebPrivatePtrDestructionSameT hread, strongOrWeak>::Type* m_handle; |
169 }; | 201 }; |
170 | 202 |
171 template<typename T, WebPrivatePtrDestruction crossThreadDestruction, WebPrivate PtrStrength strongOrWeak> | 203 template<typename T, WebPrivatePtrDestruction crossThreadDestruction, WebPrivate PtrStrength strongOrWeak> |
172 class PtrStorage : public PtrStorageImpl<T, crossThreadDestruction, strongOrWeak , LifetimeOf<T>::value> { | 204 class PtrStorage : public PtrStorageImpl<T, crossThreadDestruction, strongOrWeak , LifetimeOf<T>::value> { |
173 public: | 205 public: |
174 static PtrStorage& fromSlot(void** slot) | 206 static PtrStorage& fromSlot(void** slot) |
175 { | 207 { |
176 static_assert(sizeof(PtrStorage) == sizeof(void*), "PtrStorage must be t he size of a pointer"); | 208 static_assert(sizeof(PtrStorage) == sizeof(void*), "PtrStorage must be t he size of a pointer"); |
177 return *reinterpret_cast<PtrStorage*>(slot); | 209 return *reinterpret_cast<PtrStorage*>(slot); |
178 } | 210 } |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
295 // Disable the copy constructor; classes that contain a WebPrivatePtr | 327 // Disable the copy constructor; classes that contain a WebPrivatePtr |
296 // should implement their copy constructor using assign(). | 328 // should implement their copy constructor using assign(). |
297 WebPrivatePtr(const WebPrivatePtr&); | 329 WebPrivatePtr(const WebPrivatePtr&); |
298 | 330 |
299 void* m_storage; | 331 void* m_storage; |
300 }; | 332 }; |
301 | 333 |
302 } // namespace blink | 334 } // namespace blink |
303 | 335 |
304 #endif // WebPrivatePtr_h | 336 #endif // WebPrivatePtr_h |
OLD | NEW |