1/*
2 *  Copyright (C) 2005, 2006, 2007, 2008 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#ifndef WTF_RefPtr_h
22#define WTF_RefPtr_h
23
24#include <algorithm>
25#include "AlwaysInline.h"
26#include "FastAllocBase.h"
27#include "PassRefPtr.h"
28
29namespace WTF {
30
31    enum PlacementNewAdoptType { PlacementNewAdopt };
32
33    template <typename T> class PassRefPtr;
34    template <typename T> class NonNullPassRefPtr;
35
36    enum HashTableDeletedValueType { HashTableDeletedValue };
37
38    template <typename T> class RefPtr : public FastAllocBase {
39    public:
40        RefPtr() : m_ptr(0) { }
41        RefPtr(T* ptr) : m_ptr(ptr) { refIfNotNull(ptr); }
42        RefPtr(const RefPtr& o) : m_ptr(o.m_ptr) { T* ptr = m_ptr; refIfNotNull(ptr); }
43        // see comment in PassRefPtr.h for why this takes const reference
44        template <typename U> RefPtr(const PassRefPtr<U>&);
45        template <typename U> RefPtr(const NonNullPassRefPtr<U>&);
46
47        // Special constructor for cases where we overwrite an object in place.
48        RefPtr(PlacementNewAdoptType) { }
49
50        // Hash table deleted values, which are only constructed and never copied or destroyed.
51        RefPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { }
52        bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue(); }
53
54        ~RefPtr() { derefIfNotNull(m_ptr); }
55
56        template <typename U> RefPtr(const RefPtr<U>& o) : m_ptr(o.get()) { T* ptr = m_ptr; refIfNotNull(ptr); }
57
58        T* get() const { return m_ptr; }
59
60        void clear() { derefIfNotNull(m_ptr); m_ptr = 0; }
61        PassRefPtr<T> release() { PassRefPtr<T> tmp = adoptRef(m_ptr); m_ptr = 0; 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 : 0; }
71
72        RefPtr& operator=(const RefPtr&);
73        RefPtr& operator=(T*);
74        RefPtr& operator=(const PassRefPtr<T>&);
75        RefPtr& operator=(const NonNullPassRefPtr<T>&);
76        template <typename U> RefPtr& operator=(const RefPtr<U>&);
77        template <typename U> RefPtr& operator=(const PassRefPtr<U>&);
78        template <typename U> RefPtr& operator=(const NonNullPassRefPtr<U>&);
79
80        void swap(RefPtr&);
81
82    private:
83        static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); }
84
85        T* m_ptr;
86    };
87
88    template <typename T> template <typename U> inline RefPtr<T>::RefPtr(const PassRefPtr<U>& o)
89        : m_ptr(o.releaseRef())
90    {
91    }
92
93    template <typename T> template <typename U> inline RefPtr<T>::RefPtr(const NonNullPassRefPtr<U>& o)
94        : m_ptr(o.releaseRef())
95    {
96    }
97
98    template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr<T>& o)
99    {
100        T* optr = o.get();
101        refIfNotNull(optr);
102        T* ptr = m_ptr;
103        m_ptr = optr;
104        derefIfNotNull(ptr);
105        return *this;
106    }
107
108    template <typename T> template <typename U> inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr<U>& o)
109    {
110        T* optr = o.get();
111        refIfNotNull(optr);
112        T* ptr = m_ptr;
113        m_ptr = optr;
114        derefIfNotNull(ptr);
115        return *this;
116    }
117
118    template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(T* optr)
119    {
120        refIfNotNull(optr);
121        T* ptr = m_ptr;
122        m_ptr = optr;
123        derefIfNotNull(ptr);
124        return *this;
125    }
126
127    template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<T>& o)
128    {
129        T* ptr = m_ptr;
130        m_ptr = o.releaseRef();
131        derefIfNotNull(ptr);
132        return *this;
133    }
134
135    template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(const NonNullPassRefPtr<T>& o)
136    {
137        T* ptr = m_ptr;
138        m_ptr = o.releaseRef();
139        derefIfNotNull(ptr);
140        return *this;
141    }
142
143    template <typename T> template <typename U> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<U>& o)
144    {
145        T* ptr = m_ptr;
146        m_ptr = o.releaseRef();
147        derefIfNotNull(ptr);
148        return *this;
149    }
150
151    template <typename T> template <typename U> inline RefPtr<T>& RefPtr<T>::operator=(const NonNullPassRefPtr<U>& o)
152    {
153        T* ptr = m_ptr;
154        m_ptr = o.releaseRef();
155        derefIfNotNull(ptr);
156        return *this;
157    }
158
159    template <class T> inline void RefPtr<T>::swap(RefPtr<T>& o)
160    {
161        std::swap(m_ptr, o.m_ptr);
162    }
163
164    template <class T> inline void swap(RefPtr<T>& a, RefPtr<T>& b)
165    {
166        a.swap(b);
167    }
168
169    template <typename T, typename U> inline bool operator==(const RefPtr<T>& a, const RefPtr<U>& b)
170    {
171        return a.get() == b.get();
172    }
173
174    template <typename T, typename U> inline bool operator==(const RefPtr<T>& a, U* b)
175    {
176        return a.get() == b;
177    }
178
179    template <typename T, typename U> inline bool operator==(T* a, const RefPtr<U>& b)
180    {
181        return a == b.get();
182    }
183
184    template <typename T, typename U> inline bool operator!=(const RefPtr<T>& a, const RefPtr<U>& b)
185    {
186        return a.get() != b.get();
187    }
188
189    template <typename T, typename U> inline bool operator!=(const RefPtr<T>& a, U* b)
190    {
191        return a.get() != b;
192    }
193
194    template <typename T, typename U> inline bool operator!=(T* a, const RefPtr<U>& b)
195    {
196        return a != b.get();
197    }
198
199    template <typename T, typename U> inline RefPtr<T> static_pointer_cast(const RefPtr<U>& p)
200    {
201        return RefPtr<T>(static_cast<T*>(p.get()));
202    }
203
204    template <typename T, typename U> inline RefPtr<T> const_pointer_cast(const RefPtr<U>& p)
205    {
206        return RefPtr<T>(const_cast<T*>(p.get()));
207    }
208
209    template <typename T> inline T* getPtr(const RefPtr<T>& p)
210    {
211        return p.get();
212    }
213
214} // namespace WTF
215
216using WTF::RefPtr;
217using WTF::static_pointer_cast;
218using WTF::const_pointer_cast;
219
220#endif // WTF_RefPtr_h
221