OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef WebPassOwnPtr_h |
| 6 #define WebPassOwnPtr_h |
| 7 |
| 8 #include "public/platform/WebCommon.h" |
| 9 |
| 10 #if INSIDE_BLINK |
| 11 #include "wtf/PassOwnPtr.h" |
| 12 #endif |
| 13 |
| 14 namespace blink { |
| 15 |
| 16 // WebPassOwnPtr<T> is used to pass a T pointer with ownership from chromium |
| 17 // side to blink side. |
| 18 // WebPassOwnPtr<T> is destructible on chromium side only when it contains |
| 19 // nullptr. |
| 20 // TODO(yhirano): Migrate to scoped_ptr or std::unique_ptr once the repository |
| 21 // merge is done or C++11 std library is allowed. |
| 22 template <typename T> |
| 23 class WebPassOwnPtr final { |
| 24 public: |
| 25 WebPassOwnPtr() : m_ptr(nullptr) {} |
| 26 WebPassOwnPtr(decltype(nullptr)) : m_ptr(nullptr) {} |
| 27 // We need |const| to bind an rvalue. As a result, |m_ptr| needs to be |
| 28 // mutable because we manipulate it. |
| 29 template <typename U> |
| 30 WebPassOwnPtr(const WebPassOwnPtr<U>& o) |
| 31 { |
| 32 m_ptr = o.m_ptr; |
| 33 o.m_ptr = nullptr; |
| 34 } |
| 35 ~WebPassOwnPtr() |
| 36 { |
| 37 #if INSIDE_BLINK |
| 38 release(); |
| 39 #endif |
| 40 BLINK_ASSERT(!m_ptr); |
| 41 } |
| 42 WebPassOwnPtr& operator =(const WebPassOwnPtr&) = delete; |
| 43 |
| 44 #if INSIDE_BLINK |
| 45 PassOwnPtr<T> release() |
| 46 { |
| 47 T* ptr = m_ptr; |
| 48 m_ptr = nullptr; |
| 49 return adoptPtr(ptr); |
| 50 } |
| 51 #endif // INSIDE_BLINK |
| 52 |
| 53 template <typename U> friend class WebPassOwnPtr; |
| 54 template <typename U> friend WebPassOwnPtr<U> adoptWebPtr(U*); |
| 55 |
| 56 private: |
| 57 explicit WebPassOwnPtr(T* ptr) : m_ptr(ptr) {} |
| 58 |
| 59 // See the constructor comment to see why |mutable| is needed. |
| 60 mutable T* m_ptr; |
| 61 }; |
| 62 |
| 63 template <typename T> |
| 64 WebPassOwnPtr<T> adoptWebPtr(T* p) { return WebPassOwnPtr<T>(p); } |
| 65 |
| 66 } // namespace blink |
| 67 |
| 68 #endif |
OLD | NEW |