Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / wtf / PassOwnPtr.h
1 /*
2  * Copyright (C) 2009, 2010 Apple Inc. All rights reserved.
3  * Copyright (C) 2013 Intel Corporation. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #ifndef WTF_PassOwnPtr_h
28 #define WTF_PassOwnPtr_h
29
30 #include "wtf/NullPtr.h"
31 #include "wtf/OwnPtrCommon.h"
32
33 namespace WTF {
34
35     template<typename T> class OwnPtr;
36     template<typename T> class PassOwnPtr;
37     template<typename T> PassOwnPtr<T> adoptPtr(T*);
38     template<typename T> PassOwnPtr<T[]> adoptArrayPtr(T*);
39
40     template<typename T> class PassOwnPtr {
41     public:
42         typedef typename RemoveExtent<T>::Type ValueType;
43         typedef ValueType* PtrType;
44
45         PassOwnPtr() : m_ptr(0) { }
46         PassOwnPtr(std::nullptr_t) : m_ptr(0) { }
47
48         // It somewhat breaks the type system to allow transfer of ownership out of
49         // a const PassOwnPtr. However, it makes it much easier to work with PassOwnPtr
50         // temporaries, and we don't have a need to use real const PassOwnPtrs anyway.
51         PassOwnPtr(const PassOwnPtr& o) : m_ptr(o.leakPtr()) { }
52         template<typename U> PassOwnPtr(const PassOwnPtr<U>&, EnsurePtrConvertibleArgDecl(U, T));
53
54         ~PassOwnPtr() { OwnedPtrDeleter<T>::deletePtr(m_ptr); }
55
56         PtrType get() const { return m_ptr; }
57
58         PtrType leakPtr() const WARN_UNUSED_RETURN;
59
60         ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; }
61         PtrType operator->() const { ASSERT(m_ptr); return m_ptr; }
62
63         bool operator!() const { return !m_ptr; }
64
65         // This conversion operator allows implicit conversion to bool but not to other integer types.
66         typedef PtrType PassOwnPtr::*UnspecifiedBoolType;
67         operator UnspecifiedBoolType() const { return m_ptr ? &PassOwnPtr::m_ptr : 0; }
68
69         template<typename U> friend PassOwnPtr<U> adoptPtr(U*);
70         template<typename U> friend PassOwnPtr<U[]> adoptArrayPtr(U*);
71         template<typename U> friend class OwnPtr;
72
73     private:
74         explicit PassOwnPtr(PtrType ptr) : m_ptr(ptr) { }
75
76         PassOwnPtr& operator=(const PassOwnPtr&) { COMPILE_ASSERT(!sizeof(T*), PassOwnPtr_should_never_be_assigned_to); return *this; }
77
78         // We should never have two OwnPtrs for the same underlying object (otherwise we'll get
79         // double-destruction), so these equality operators should never be needed.
80         template<typename U> bool operator==(const PassOwnPtr<U>&) const { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; }
81         template<typename U> bool operator!=(const PassOwnPtr<U>&) const { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; }
82         template<typename U> bool operator==(const OwnPtr<U>&) const { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; }
83         template<typename U> bool operator!=(const OwnPtr<U>&) const { COMPILE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; }
84
85         mutable PtrType m_ptr;
86     };
87
88     template<typename T> template<typename U> inline PassOwnPtr<T>::PassOwnPtr(const PassOwnPtr<U>& o, EnsurePtrConvertibleArgDefn(U, T))
89         : m_ptr(o.leakPtr())
90     {
91         COMPILE_ASSERT(!IsArray<T>::value, Pointers_to_array_must_never_be_converted);
92     }
93
94     template<typename T> inline typename PassOwnPtr<T>::PtrType PassOwnPtr<T>::leakPtr() const
95     {
96         PtrType ptr = m_ptr;
97         m_ptr = 0;
98         return ptr;
99     }
100
101     template<typename T, typename U> inline bool operator==(const PassOwnPtr<T>& a, U* b)
102     {
103         return a.get() == b;
104     }
105
106     template<typename T, typename U> inline bool operator==(T* a, const PassOwnPtr<U>& b)
107     {
108         return a == b.get();
109     }
110
111     template<typename T, typename U> inline bool operator!=(const PassOwnPtr<T>& a, U* b)
112     {
113         return a.get() != b;
114     }
115
116     template<typename T, typename U> inline bool operator!=(T* a, const PassOwnPtr<U>& b)
117     {
118         return a != b.get();
119     }
120
121     template<typename T> inline PassOwnPtr<T> adoptPtr(T* ptr)
122     {
123         return PassOwnPtr<T>(ptr);
124     }
125
126     template<typename T> inline PassOwnPtr<T[]> adoptArrayPtr(T* ptr)
127     {
128         return PassOwnPtr<T[]>(ptr);
129     }
130
131     template<typename T, typename U> inline PassOwnPtr<T> static_pointer_cast(const PassOwnPtr<U>& p)
132     {
133         COMPILE_ASSERT(!IsArray<T>::value, Pointers_to_array_must_never_be_converted);
134         return adoptPtr(static_cast<T*>(p.leakPtr()));
135     }
136
137     template<typename T> inline T* getPtr(const PassOwnPtr<T>& p)
138     {
139         return p.get();
140     }
141
142 } // namespace WTF
143
144 using WTF::PassOwnPtr;
145 using WTF::adoptPtr;
146 using WTF::adoptArrayPtr;
147 using WTF::static_pointer_cast;
148
149 #endif // WTF_PassOwnPtr_h