source: webkit/trunk/JavaScriptCore/wtf/PassRefPtr.h@ 27698

Last change on this file since 27698 was 27103, checked in by mjs, 18 years ago

Reviewed by Oliver.


  • encourage GCC a little harder to inline a few hot functions for 1.5% improvement on SunSpider.
  • kjs/value.h: (KJS::JSValue::getUInt32): (KJS::JSValue::getTruncatedInt32): (KJS::JSValue::toNumber):
  • wtf/PassRefPtr.h: (WTF::PassRefPtr::~PassRefPtr):
  • wtf/RefPtr.h: (WTF::RefPtr::operator->):
  • Property svn:eol-style set to native
File size: 6.1 KB
Line 
1// -*- mode: c++; c-basic-offset: 4 -*-
2/*
3 * This file is part of the KDE libraries
4 * Copyright (C) 2005, 2006 Apple Computer, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#ifndef WTF_PassRefPtr_h
24#define WTF_PassRefPtr_h
25
26#include "AlwaysInline.h"
27
28namespace WTF {
29
30 template<typename T> class RefPtr;
31 template<typename T> class PassRefPtr;
32 template <typename T> PassRefPtr<T> adoptRef(T*);
33
34 template<typename T>
35 class PassRefPtr
36 {
37 public:
38 PassRefPtr() : m_ptr(0) {}
39 PassRefPtr(T* ptr) : m_ptr(ptr) { if (ptr) ptr->ref(); }
40 // It somewhat breaks the type system to allow transfer of ownership out of
41 // a const PassRefPtr. However, it makes it much easier to work with PassRefPtr
42 // temporaries, and we don't really have a need to use real const PassRefPtrs
43 // anyway.
44 PassRefPtr(const PassRefPtr& o) : m_ptr(o.releaseRef()) {}
45 template <typename U> PassRefPtr(const PassRefPtr<U>& o) : m_ptr(o.releaseRef()) { }
46
47 ALWAYS_INLINE ~PassRefPtr() { if (T* ptr = m_ptr) ptr->deref(); }
48
49 template <class U>
50 PassRefPtr(const RefPtr<U>& o) : m_ptr(o.get()) { if (T* ptr = m_ptr) ptr->ref(); }
51
52 T* get() const { return m_ptr; }
53
54 T* releaseRef() const { T* tmp = m_ptr; m_ptr = 0; return tmp; }
55
56 T& operator*() const { return *m_ptr; }
57 T* operator->() const { return m_ptr; }
58
59 bool operator!() const { return !m_ptr; }
60
61 // This conversion operator allows implicit conversion to bool but not to other integer types.
62 typedef T* (PassRefPtr::*UnspecifiedBoolType)() const;
63 operator UnspecifiedBoolType() const { return m_ptr ? &PassRefPtr::get : 0; }
64
65 PassRefPtr& operator=(T*);
66 PassRefPtr& operator=(const PassRefPtr&);
67 template <typename U> PassRefPtr& operator=(const PassRefPtr<U>&);
68 template <typename U> PassRefPtr& operator=(const RefPtr<U>&);
69
70 friend PassRefPtr adoptRef<T>(T*);
71 private:
72 // adopting constructor
73 PassRefPtr(T* ptr, bool) : m_ptr(ptr) {}
74 mutable T* m_ptr;
75 };
76
77 template <typename T> template <typename U> inline PassRefPtr<T>& PassRefPtr<T>::operator=(const RefPtr<U>& o)
78 {
79 T* optr = o.get();
80 if (optr)
81 optr->ref();
82 T* ptr = m_ptr;
83 m_ptr = optr;
84 if (ptr)
85 ptr->deref();
86 return *this;
87 }
88
89 template <typename T> inline PassRefPtr<T>& PassRefPtr<T>::operator=(T* optr)
90 {
91 if (optr)
92 optr->ref();
93 T* ptr = m_ptr;
94 m_ptr = optr;
95 if (ptr)
96 ptr->deref();
97 return *this;
98 }
99
100 template <typename T> inline PassRefPtr<T>& PassRefPtr<T>::operator=(const PassRefPtr<T>& ref)
101 {
102 T* ptr = m_ptr;
103 m_ptr = ref.releaseRef();
104 if (ptr)
105 ptr->deref();
106 return *this;
107 }
108
109 template <typename T> template <typename U> inline PassRefPtr<T>& PassRefPtr<T>::operator=(const PassRefPtr<U>& ref)
110 {
111 T* ptr = m_ptr;
112 m_ptr = ref.releaseRef();
113 if (ptr)
114 ptr->deref();
115 return *this;
116 }
117
118 template <typename T, typename U> inline bool operator==(const PassRefPtr<T>& a, const PassRefPtr<U>& b)
119 {
120 return a.get() == b.get();
121 }
122
123 template <typename T, typename U> inline bool operator==(const PassRefPtr<T>& a, const RefPtr<U>& b)
124 {
125 return a.get() == b.get();
126 }
127
128 template <typename T, typename U> inline bool operator==(const RefPtr<T>& a, const PassRefPtr<U>& b)
129 {
130 return a.get() == b.get();
131 }
132
133 template <typename T, typename U> inline bool operator==(const PassRefPtr<T>& a, U* b)
134 {
135 return a.get() == b;
136 }
137
138 template <typename T, typename U> inline bool operator==(T* a, const PassRefPtr<U>& b)
139 {
140 return a == b.get();
141 }
142
143 template <typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a, const PassRefPtr<U>& b)
144 {
145 return a.get() != b.get();
146 }
147
148 template <typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a, const RefPtr<U>& b)
149 {
150 return a.get() != b.get();
151 }
152
153 template <typename T, typename U> inline bool operator!=(const RefPtr<T>& a, const PassRefPtr<U>& b)
154 {
155 return a.get() != b.get();
156 }
157
158 template <typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a, U* b)
159 {
160 return a.get() != b;
161 }
162
163 template <typename T, typename U> inline bool operator!=(T* a, const PassRefPtr<U>& b)
164 {
165 return a != b.get();
166 }
167
168 template <typename T> inline PassRefPtr<T> adoptRef(T* p)
169 {
170 return PassRefPtr<T>(p, true);
171 }
172
173 template <typename T, typename U> inline PassRefPtr<T> static_pointer_cast(const PassRefPtr<U>& p)
174 {
175 return adoptRef(static_cast<T*>(p.releaseRef()));
176 }
177
178 template <typename T, typename U> inline PassRefPtr<T> const_pointer_cast(const PassRefPtr<U>& p)
179 {
180 return adoptRef(const_cast<T*>(p.releaseRef()));
181 }
182
183 template <typename T> inline T* getPtr(const PassRefPtr<T>& p)
184 {
185 return p.get();
186 }
187
188} // namespace WTF
189
190using WTF::PassRefPtr;
191using WTF::adoptRef;
192using WTF::static_pointer_cast;
193using WTF::const_pointer_cast;
194
195#endif // WTF_PassRefPtr_h
Note: See TracBrowser for help on using the repository browser.