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