| 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 #include <cstddef> | |
| 10 | |
| 11 #if INSIDE_BLINK | |
| 12 #include "wtf/PassOwnPtr.h" | |
| 13 #else | |
| 14 #include <base/memory/scoped_ptr.h> | |
| 15 #endif | |
| 16 | |
| 17 namespace blink { | |
| 18 | |
| 19 // WebPassOwnPtr<T> is used to pass a T pointer with ownership from chromium | |
| 20 // side to blink side. T's definition must be shared among all users | |
| 21 // (especially between chromium and blink). | |
| 22 // TODO(yhirano): Migrate to scoped_ptr or std::unique_ptr once the repository | |
| 23 // merge is done or C++11 std library is allowed. | |
| 24 template <typename T> | |
| 25 class WebPassOwnPtr final { | |
| 26 public: | |
| 27 WebPassOwnPtr() : m_ptr(nullptr) {} | |
| 28 WebPassOwnPtr(std::nullptr_t) : m_ptr(nullptr) {} | |
| 29 // We need |const| to bind an rvalue. As a result, |m_ptr| needs to be | |
| 30 // mutable because we manipulate it. | |
| 31 template <typename U> | |
| 32 WebPassOwnPtr(const WebPassOwnPtr<U>& o) | |
| 33 { | |
| 34 m_ptr = o.m_ptr; | |
| 35 o.m_ptr = nullptr; | |
| 36 } | |
| 37 WebPassOwnPtr(const WebPassOwnPtr& o) | |
| 38 { | |
| 39 m_ptr = o.m_ptr; | |
| 40 o.m_ptr = nullptr; | |
| 41 } | |
| 42 ~WebPassOwnPtr() | |
| 43 { | |
| 44 delete m_ptr; | |
| 45 } | |
| 46 WebPassOwnPtr& operator =(const WebPassOwnPtr&) = delete; | |
| 47 | |
| 48 #if INSIDE_BLINK | |
| 49 PassOwnPtr<T> release() | |
| 50 { | |
| 51 T* ptr = m_ptr; | |
| 52 m_ptr = nullptr; | |
| 53 return adoptPtr(ptr); | |
| 54 } | |
| 55 #else | |
| 56 operator scoped_ptr<T>() | |
| 57 { | |
| 58 T* ptr = m_ptr; | |
| 59 m_ptr = nullptr; | |
| 60 return scoped_ptr<T>(ptr); | |
| 61 } | |
| 62 #endif // INSIDE_BLINK | |
| 63 | |
| 64 template <typename U> friend class WebPassOwnPtr; | |
| 65 template <typename U> friend WebPassOwnPtr<U> adoptWebPtr(U*); | |
| 66 | |
| 67 private: | |
| 68 explicit WebPassOwnPtr(T* ptr) : m_ptr(ptr) {} | |
| 69 | |
| 70 // See the constructor comment to see why |mutable| is needed. | |
| 71 mutable T* m_ptr; | |
| 72 }; | |
| 73 | |
| 74 template <typename T> | |
| 75 WebPassOwnPtr<T> adoptWebPtr(T* p) { return WebPassOwnPtr<T>(p); } | |
| 76 | |
| 77 } // namespace blink | |
| 78 | |
| 79 #endif | |
| OLD | NEW |