source: webkit/trunk/JavaScriptCore/wtf/RefPtr.h@ 27885

Last change on this file since 27885 was 27208, checked in by darin, 18 years ago
  • fix "broken everything" from the storage leak fix
  • wtf/RefPtr.h: (WTF::RefPtr::RefPtr): Added a PlacementNewAdopt constructor.
  • kjs/ustring.h: (KJS::UString::UString): Pass PlacementNewAdopt along to RefPtr.
  • Property svn:eol-style set to native
File size: 5.6 KB
Line 
1// -*- mode: c++; c-basic-offset: 4 -*-
2/*
3 * Copyright (C) 2005, 2006, 2007 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#ifndef WTF_RefPtr_h
23#define WTF_RefPtr_h
24
25#include <algorithm>
26#include "AlwaysInline.h"
27
28namespace WTF {
29
30 enum PlacementNewAdoptType { PlacementNewAdopt };
31
32 template <typename T> class PassRefPtr;
33
34 template <typename T> class RefPtr {
35 public:
36 RefPtr() : m_ptr(0) {}
37 RefPtr(T *ptr) : m_ptr(ptr) { if (ptr) ptr->ref(); }
38 RefPtr(const RefPtr& o) : m_ptr(o.m_ptr) { if (T *ptr = m_ptr) ptr->ref(); }
39 // see comment in PassRefPtr.h for why this takes const reference
40 template <typename U> RefPtr(const PassRefPtr<U>&);
41
42 // Special constructor for cases where we overwrite an object in place.
43 RefPtr(PlacementNewAdoptType) { }
44
45 ~RefPtr() { if (T *ptr = m_ptr) ptr->deref(); }
46
47 template <typename U> RefPtr(const RefPtr<U>& o) : m_ptr(o.get()) { if (T *ptr = m_ptr) ptr->ref(); }
48
49 T *get() const { return m_ptr; }
50
51 PassRefPtr<T> release() { PassRefPtr<T> tmp = adoptRef(m_ptr); m_ptr = 0; return tmp; }
52
53 T& operator*() const { return *m_ptr; }
54 ALWAYS_INLINE T *operator->() const { return m_ptr; }
55
56 bool operator!() const { return !m_ptr; }
57
58 // This conversion operator allows implicit conversion to bool but not to other integer types.
59 typedef T * (RefPtr::*UnspecifiedBoolType)() const;
60 operator UnspecifiedBoolType() const { return m_ptr ? &RefPtr::get : 0; }
61
62 RefPtr& operator=(const RefPtr&);
63 RefPtr& operator=(T *);
64 RefPtr& operator=(const PassRefPtr<T>&);
65 template <typename U> RefPtr& operator=(const RefPtr<U>&);
66 template <typename U> RefPtr& operator=(const PassRefPtr<U>&);
67
68 void swap(RefPtr&);
69
70 private:
71 T* m_ptr;
72 };
73
74 template <typename T> template <typename U> inline RefPtr<T>::RefPtr(const PassRefPtr<U>& o)
75 : m_ptr(o.releaseRef())
76 {
77 }
78
79 template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr<T>& o)
80 {
81 T* optr = o.get();
82 if (optr)
83 optr->ref();
84 T* ptr = m_ptr;
85 m_ptr = optr;
86 if (ptr)
87 ptr->deref();
88 return *this;
89 }
90
91 template <typename T> template <typename U> inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr<U>& o)
92 {
93 T* optr = o.get();
94 if (optr)
95 optr->ref();
96 T* ptr = m_ptr;
97 m_ptr = optr;
98 if (ptr)
99 ptr->deref();
100 return *this;
101 }
102
103 template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(T* optr)
104 {
105 if (optr)
106 optr->ref();
107 T* ptr = m_ptr;
108 m_ptr = optr;
109 if (ptr)
110 ptr->deref();
111 return *this;
112 }
113
114 template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<T>& o)
115 {
116 T* ptr = m_ptr;
117 m_ptr = o.releaseRef();
118 if (ptr)
119 ptr->deref();
120 return *this;
121 }
122
123 template <typename T> template <typename U> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<U>& o)
124 {
125 T* ptr = m_ptr;
126 m_ptr = o.releaseRef();
127 if (ptr)
128 ptr->deref();
129 return *this;
130 }
131
132 template <class T> inline void RefPtr<T>::swap(RefPtr<T>& o)
133 {
134 std::swap(m_ptr, o.m_ptr);
135 }
136
137 template <class T> inline void swap(RefPtr<T>& a, RefPtr<T>& b)
138 {
139 a.swap(b);
140 }
141
142 template <typename T, typename U> inline bool operator==(const RefPtr<T>& a, const RefPtr<U>& b)
143 {
144 return a.get() == b.get();
145 }
146
147 template <typename T, typename U> inline bool operator==(const RefPtr<T>& a, U* b)
148 {
149 return a.get() == b;
150 }
151
152 template <typename T, typename U> inline bool operator==(T* a, const RefPtr<U>& b)
153 {
154 return a == b.get();
155 }
156
157 template <typename T, typename U> inline bool operator!=(const RefPtr<T>& a, const RefPtr<U>& b)
158 {
159 return a.get() != b.get();
160 }
161
162 template <typename T, typename U> inline bool operator!=(const RefPtr<T>& a, U* b)
163 {
164 return a.get() != b;
165 }
166
167 template <typename T, typename U> inline bool operator!=(T* a, const RefPtr<U>& b)
168 {
169 return a != b.get();
170 }
171
172 template <typename T, typename U> inline RefPtr<T> static_pointer_cast(const RefPtr<U>& p)
173 {
174 return RefPtr<T>(static_cast<T *>(p.get()));
175 }
176
177 template <typename T, typename U> inline RefPtr<T> const_pointer_cast(const RefPtr<U>& p)
178 {
179 return RefPtr<T>(const_cast<T *>(p.get()));
180 }
181
182 template <typename T> inline T* getPtr(const RefPtr<T>& p)
183 {
184 return p.get();
185 }
186
187} // namespace WTF
188
189using WTF::RefPtr;
190using WTF::static_pointer_cast;
191using WTF::const_pointer_cast;
192
193#endif // WTF_RefPtr_h
Note: See TracBrowser for help on using the repository browser.