source: webkit/trunk/JavaScriptCore/wtf/RetainPtr.h@ 21064

Last change on this file since 21064 was 20500, checked in by beidson, 18 years ago

Rubberstamped by Anders and Maciej aand Geoff (oh my!)


Since CFTypeRef is really void*, a RetainPtr couldn't be used.
RefType was "void", which doesn't actually exist as a type.
Since RefType only existed for operator*(), and since that operator
doesn't make any sense for RetainPtr, I removed them!

  • kjs/nodes.cpp: Touch this to force a rebuild and (hopefully) help the compiler with dependencies
  • wtf/RetainPtr.h: Nuke RefType and operator*()
File size: 5.9 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 RetainPtr_h
24#define RetainPtr_h
25
26#include <algorithm>
27#include <CoreFoundation/CoreFoundation.h>
28
29namespace WTF {
30
31 template <typename T> struct RemovePointer {
32 typedef T type;
33 };
34
35 template <typename T> struct RemovePointer<T*> {
36 typedef T type;
37 };
38
39 // Unlike most most of our smart pointers, RetainPtr can take either the pointer type or the pointed-to type,
40 // so both RetainPtr<NSDictionary> and RetainPtr<CFDictionaryRef> will work.
41
42 enum AdoptCFTag { AdoptCF };
43 enum AdoptNSTag { AdoptNS };
44
45#ifdef __OBJC__
46 inline void adoptNSReference(id ptr)
47 {
48 if (ptr) {
49 CFRetain(ptr);
50 [ptr release];
51 }
52 }
53#endif
54
55 template <typename T> class RetainPtr
56 {
57 public:
58 typedef typename RemovePointer<T>::type ValueType;
59 typedef ValueType* PtrType;
60
61 RetainPtr() : m_ptr(0) {}
62 RetainPtr(PtrType ptr) : m_ptr(ptr) { if (ptr) CFRetain(ptr); }
63
64 RetainPtr(AdoptCFTag, PtrType ptr) : m_ptr(ptr) { }
65 RetainPtr(AdoptNSTag, PtrType ptr) : m_ptr(ptr) { adoptNSReference(ptr); }
66
67 RetainPtr(const RetainPtr& o) : m_ptr(o.m_ptr) { if (PtrType ptr = m_ptr) CFRetain(ptr); }
68
69 ~RetainPtr() { if (PtrType ptr = m_ptr) CFRelease(ptr); }
70
71 template <typename U> RetainPtr(const RetainPtr<U>& o) : m_ptr(o.get()) { if (PtrType ptr = m_ptr) CFRetain(ptr); }
72
73 PtrType get() const { return m_ptr; }
74
75 PtrType releaseRef() { PtrType tmp = m_ptr; m_ptr = 0; return tmp; }
76
77 PtrType operator->() const { return m_ptr; }
78
79 bool operator!() const { return !m_ptr; }
80
81 // This conversion operator allows implicit conversion to bool but not to other integer types.
82 typedef PtrType (RetainPtr::*UnspecifiedBoolType)() const;
83 operator UnspecifiedBoolType() const { return m_ptr ? &RetainPtr::get : 0; }
84
85 RetainPtr& operator=(const RetainPtr&);
86 template <typename U> RetainPtr& operator=(const RetainPtr<U>&);
87 RetainPtr& operator=(PtrType);
88 template <typename U> RetainPtr& operator=(U*);
89
90 void adoptCF(PtrType);
91 void adoptNS(PtrType);
92
93 void swap(RetainPtr&);
94
95 private:
96 PtrType m_ptr;
97 };
98
99 template <typename T> inline RetainPtr<T>& RetainPtr<T>::operator=(const RetainPtr<T>& o)
100 {
101 PtrType optr = o.get();
102 if (optr)
103 CFRetain(optr);
104 PtrType ptr = m_ptr;
105 m_ptr = optr;
106 if (ptr)
107 CFRelease(ptr);
108 return *this;
109 }
110
111 template <typename T> template <typename U> inline RetainPtr<T>& RetainPtr<T>::operator=(const RetainPtr<U>& o)
112 {
113 PtrType optr = o.get();
114 if (optr)
115 CFRetain(optr);
116 PtrType ptr = m_ptr;
117 m_ptr = optr;
118 if (ptr)
119 CFRelease(ptr);
120 return *this;
121 }
122
123 template <typename T> inline RetainPtr<T>& RetainPtr<T>::operator=(PtrType optr)
124 {
125 if (optr)
126 CFRetain(optr);
127 PtrType ptr = m_ptr;
128 m_ptr = optr;
129 if (ptr)
130 CFRelease(ptr);
131 return *this;
132 }
133
134 template <typename T> inline void RetainPtr<T>::adoptCF(PtrType optr)
135 {
136 PtrType ptr = m_ptr;
137 m_ptr = optr;
138 if (ptr)
139 CFRelease(ptr);
140 }
141
142 template <typename T> inline void RetainPtr<T>::adoptNS(PtrType optr)
143 {
144 adoptNSReference(optr);
145
146 PtrType ptr = m_ptr;
147 m_ptr = optr;
148 if (ptr)
149 CFRelease(ptr);
150 }
151
152 template <typename T> template <typename U> inline RetainPtr<T>& RetainPtr<T>::operator=(U* optr)
153 {
154 if (optr)
155 CFRetain(optr);
156 PtrType ptr = m_ptr;
157 m_ptr = optr;
158 if (ptr)
159 CFRelease(ptr);
160 return *this;
161 }
162
163 template <class T> inline void RetainPtr<T>::swap(RetainPtr<T>& o)
164 {
165 std::swap(m_ptr, o.m_ptr);
166 }
167
168 template <class T> inline void swap(RetainPtr<T>& a, RetainPtr<T>& b)
169 {
170 a.swap(b);
171 }
172
173 template <typename T, typename U> inline bool operator==(const RetainPtr<T>& a, const RetainPtr<U>& b)
174 {
175 return a.get() == b.get();
176 }
177
178 template <typename T, typename U> inline bool operator==(const RetainPtr<T>& a, U* b)
179 {
180 return a.get() == b;
181 }
182
183 template <typename T, typename U> inline bool operator==(T* a, const RetainPtr<U>& b)
184 {
185 return a == b.get();
186 }
187
188 template <typename T, typename U> inline bool operator!=(const RetainPtr<T>& a, const RetainPtr<U>& b)
189 {
190 return a.get() != b.get();
191 }
192
193 template <typename T, typename U> inline bool operator!=(const RetainPtr<T>& a, U* b)
194 {
195 return a.get() != b;
196 }
197
198 template <typename T, typename U> inline bool operator!=(T* a, const RetainPtr<U>& b)
199 {
200 return a != b.get();
201 }
202
203} // namespace WTF
204
205using WTF::AdoptCF;
206using WTF::AdoptNS;
207using WTF::RetainPtr;
208
209#endif // WTF_RetainPtr_h
Note: See TracBrowser for help on using the repository browser.