Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / public / platform / WebPassOwnPtr.h
blobfc57aa2bd1397940e2243b68ae36e61bffb96a42
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.
5 #ifndef WebPassOwnPtr_h
6 #define WebPassOwnPtr_h
8 #include "public/platform/WebCommon.h"
10 #if INSIDE_BLINK
11 #include "wtf/PassOwnPtr.h"
12 #endif
14 namespace blink {
16 // WebPassOwnPtr<T> is used to pass a T pointer with ownership from chromium
17 // side to blink side. T's definition must be shared among all users
18 // (especially between chromium and blink).
19 // TODO(yhirano): Migrate to scoped_ptr or std::unique_ptr once the repository
20 // merge is done or C++11 std library is allowed.
21 template <typename T>
22 class WebPassOwnPtr final {
23 public:
24 WebPassOwnPtr() : m_ptr(nullptr) {}
25 WebPassOwnPtr(decltype(nullptr)) : m_ptr(nullptr) {}
26 // We need |const| to bind an rvalue. As a result, |m_ptr| needs to be
27 // mutable because we manipulate it.
28 template <typename U>
29 WebPassOwnPtr(const WebPassOwnPtr<U>& o)
31 m_ptr = o.m_ptr;
32 o.m_ptr = nullptr;
34 WebPassOwnPtr(const WebPassOwnPtr& o)
36 m_ptr = o.m_ptr;
37 o.m_ptr = nullptr;
39 ~WebPassOwnPtr()
41 delete m_ptr;
43 WebPassOwnPtr& operator =(const WebPassOwnPtr&) = delete;
45 #if INSIDE_BLINK
46 PassOwnPtr<T> release()
48 T* ptr = m_ptr;
49 m_ptr = nullptr;
50 return adoptPtr(ptr);
52 #endif // INSIDE_BLINK
54 template <typename U> friend class WebPassOwnPtr;
55 template <typename U> friend WebPassOwnPtr<U> adoptWebPtr(U*);
57 private:
58 explicit WebPassOwnPtr(T* ptr) : m_ptr(ptr) {}
60 // See the constructor comment to see why |mutable| is needed.
61 mutable T* m_ptr;
64 template <typename T>
65 WebPassOwnPtr<T> adoptWebPtr(T* p) { return WebPassOwnPtr<T>(p); }
67 } // namespace blink
69 #endif