// Copyright 2015 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef WebPassOwnPtr_h #define WebPassOwnPtr_h #include "public/platform/WebCommon.h" #include #if INSIDE_BLINK #include "wtf/PassOwnPtr.h" #else #include #endif namespace blink { // WebPassOwnPtr is used to pass a T pointer with ownership from chromium // side to blink side. T's definition must be shared among all users // (especially between chromium and blink). // TODO(yhirano): Migrate to scoped_ptr or std::unique_ptr once the repository // merge is done or C++11 std library is allowed. template class WebPassOwnPtr final { public: WebPassOwnPtr() : m_ptr(nullptr) {} WebPassOwnPtr(std::nullptr_t) : m_ptr(nullptr) {} // We need |const| to bind an rvalue. As a result, |m_ptr| needs to be // mutable because we manipulate it. template WebPassOwnPtr(const WebPassOwnPtr& o) { m_ptr = o.m_ptr; o.m_ptr = nullptr; } WebPassOwnPtr(const WebPassOwnPtr& o) { m_ptr = o.m_ptr; o.m_ptr = nullptr; } ~WebPassOwnPtr() { delete m_ptr; } WebPassOwnPtr& operator =(const WebPassOwnPtr&) = delete; #if INSIDE_BLINK PassOwnPtr release() { T* ptr = m_ptr; m_ptr = nullptr; return adoptPtr(ptr); } #else operator scoped_ptr() { T* ptr = m_ptr; m_ptr = nullptr; return scoped_ptr(ptr); } #endif // INSIDE_BLINK template friend class WebPassOwnPtr; template friend WebPassOwnPtr adoptWebPtr(U*); private: explicit WebPassOwnPtr(T* ptr) : m_ptr(ptr) {} // See the constructor comment to see why |mutable| is needed. mutable T* m_ptr; }; template WebPassOwnPtr adoptWebPtr(T* p) { return WebPassOwnPtr(p); } } // namespace blink #endif