1/*
2 *  Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2013 Apple Inc. All rights reserved.
3 *
4 *  This library is free software; you can redistribute it and/or
5 *  modify it under the terms of the GNU Library General Public
6 *  License as published by the Free Software Foundation; either
7 *  version 2 of the License, or (at your option) any later version.
8 *
9 *  This library is distributed in the hope that it will be useful,
10 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 *  Library General Public License for more details.
13 *
14 *  You should have received a copy of the GNU Library General Public License
15 *  along with this library; see the file COPYING.LIB.  If not, write to
16 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 *  Boston, MA 02110-1301, USA.
18 *
19 */
20
21// RefPtr and PassRefPtr are documented at http://webkit.org/coding/RefPtr.html
22
23#ifndef WTF_RefPtr_h
24#define WTF_RefPtr_h
25
26#include "FastMalloc.h"
27#include "PassRefPtr.h"
28#include <algorithm>
29#include <utility>
30
31namespace WTF {
32
33    enum HashTableDeletedValueType { HashTableDeletedValue };
34
35    template<typename T> class RefPtr {
36        WTF_MAKE_FAST_ALLOCATED;
37    public:
38        ALWAYS_INLINE RefPtr() : m_ptr(nullptr) { }
39        ALWAYS_INLINE RefPtr(T* ptr) : m_ptr(ptr) { refIfNotNull(ptr); }
40        ALWAYS_INLINE RefPtr(const RefPtr& o) : m_ptr(o.m_ptr) { refIfNotNull(m_ptr); }
41        template<typename U> RefPtr(const RefPtr<U>& o) : m_ptr(o.get()) { refIfNotNull(m_ptr); }
42
43        ALWAYS_INLINE RefPtr(RefPtr&& o) : m_ptr(o.release().leakRef()) { }
44        template<typename U> RefPtr(RefPtr<U>&& o) : m_ptr(o.release().leakRef()) { }
45
46        // See comments in PassRefPtr.h for an explanation of why this takes a const reference.
47        template<typename U> RefPtr(const PassRefPtr<U>&);
48
49        template<typename U> RefPtr(PassRef<U>&&);
50
51        // Hash table deleted values, which are only constructed and never copied or destroyed.
52        RefPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { }
53        bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue(); }
54
55        ALWAYS_INLINE ~RefPtr() { derefIfNotNull(m_ptr); }
56
57        T* get() const { return m_ptr; }
58
59        void clear();
60        PassRefPtr<T> release() { PassRefPtr<T> tmp = adoptRef(m_ptr); m_ptr = nullptr; return tmp; }
61        PassRef<T> releaseNonNull() { ASSERT(m_ptr); PassRef<T> tmp = adoptRef(*m_ptr); m_ptr = nullptr; return tmp; }
62
63        T& operator*() const { return *m_ptr; }
64        ALWAYS_INLINE T* operator->() const { return m_ptr; }
65
66        bool operator!() const { return !m_ptr; }
67
68        // This conversion operator allows implicit conversion to bool but not to other integer types.
69        typedef T* (RefPtr::*UnspecifiedBoolType);
70        operator UnspecifiedBoolType() const { return m_ptr ? &RefPtr::m_ptr : nullptr; }
71
72        RefPtr& operator=(const RefPtr&);
73        RefPtr& operator=(T*);
74        RefPtr& operator=(const PassRefPtr<T>&);
75        template<typename U> RefPtr& operator=(const RefPtr<U>&);
76        template<typename U> RefPtr& operator=(const PassRefPtr<U>&);
77        RefPtr& operator=(RefPtr&&);
78        template<typename U> RefPtr& operator=(RefPtr<U>&&);
79        template<typename U> RefPtr& operator=(PassRef<U>);
80
81        void swap(RefPtr&);
82
83        static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); }
84
85    private:
86        T* m_ptr;
87    };
88
89    template<typename T> template<typename U> inline RefPtr<T>::RefPtr(const PassRefPtr<U>& o)
90        : m_ptr(o.leakRef())
91    {
92    }
93
94    template<typename T> template<typename U> inline RefPtr<T>::RefPtr(PassRef<U>&& reference)
95        : m_ptr(&reference.leakRef())
96    {
97    }
98
99    template<typename T> inline void RefPtr<T>::clear()
100    {
101        T* ptr = m_ptr;
102        m_ptr = nullptr;
103        derefIfNotNull(ptr);
104    }
105
106    template<typename T> inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr& o)
107    {
108        RefPtr ptr = o;
109        swap(ptr);
110        return *this;
111    }
112
113    template<typename T> template<typename U> inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr<U>& o)
114    {
115        RefPtr ptr = o;
116        swap(ptr);
117        return *this;
118    }
119
120    template<typename T> inline RefPtr<T>& RefPtr<T>::operator=(T* optr)
121    {
122        RefPtr ptr = optr;
123        swap(ptr);
124        return *this;
125    }
126
127    template<typename T> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<T>& o)
128    {
129        RefPtr ptr = o;
130        swap(ptr);
131        return *this;
132    }
133
134    template<typename T> template<typename U> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<U>& o)
135    {
136        RefPtr ptr = o;
137        swap(ptr);
138        return *this;
139    }
140
141    template<typename T> inline RefPtr<T>& RefPtr<T>::operator=(RefPtr&& o)
142    {
143        RefPtr ptr = WTF::move(o);
144        swap(ptr);
145        return *this;
146    }
147
148    template<typename T> template<typename U> inline RefPtr<T>& RefPtr<T>::operator=(RefPtr<U>&& o)
149    {
150        RefPtr ptr = WTF::move(o);
151        swap(ptr);
152        return *this;
153    }
154
155    template<typename T> template<typename U> inline RefPtr<T>& RefPtr<T>::operator=(PassRef<U> reference)
156    {
157        RefPtr ptr = WTF::move(reference);
158        swap(ptr);
159        return *this;
160    }
161
162    template<class T> inline void RefPtr<T>::swap(RefPtr& o)
163    {
164        std::swap(m_ptr, o.m_ptr);
165    }
166
167    template<class T> inline void swap(RefPtr<T>& a, RefPtr<T>& b)
168    {
169        a.swap(b);
170    }
171
172    template<typename T, typename U> inline bool operator==(const RefPtr<T>& a, const RefPtr<U>& b)
173    {
174        return a.get() == b.get();
175    }
176
177    template<typename T, typename U> inline bool operator==(const RefPtr<T>& a, U* b)
178    {
179        return a.get() == b;
180    }
181
182    template<typename T, typename U> inline bool operator==(T* a, const RefPtr<U>& b)
183    {
184        return a == b.get();
185    }
186
187    template<typename T, typename U> inline bool operator!=(const RefPtr<T>& a, const RefPtr<U>& b)
188    {
189        return a.get() != b.get();
190    }
191
192    template<typename T, typename U> inline bool operator!=(const RefPtr<T>& a, U* b)
193    {
194        return a.get() != b;
195    }
196
197    template<typename T, typename U> inline bool operator!=(T* a, const RefPtr<U>& b)
198    {
199        return a != b.get();
200    }
201
202    template<typename T, typename U> inline RefPtr<T> static_pointer_cast(const RefPtr<U>& p)
203    {
204        return RefPtr<T>(static_cast<T*>(p.get()));
205    }
206
207    template<typename T> inline T* getPtr(const RefPtr<T>& p)
208    {
209        return p.get();
210    }
211
212} // namespace WTF
213
214using WTF::RefPtr;
215using WTF::static_pointer_cast;
216
217#endif // WTF_RefPtr_h
218