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

Last change on this file since 38205 was 35900, checked in by [email protected], 17 years ago

2008-08-23 Cameron Zwarich <[email protected]>

Rubber-stamped by Mark Rowe.

Remove modelines.

JavaScriptCore:

  • API/APICast.h:
  • API/JSBase.cpp:
  • API/JSCallbackConstructor.cpp:
  • API/JSCallbackConstructor.h:
  • API/JSCallbackFunction.cpp:
  • API/JSCallbackFunction.h:
  • API/JSCallbackObject.cpp:
  • API/JSCallbackObject.h:
  • API/JSCallbackObjectFunctions.h:
  • API/JSClassRef.cpp:
  • API/JSContextRef.cpp:
  • API/JSObjectRef.cpp:
  • API/JSProfilerPrivate.cpp:
  • API/JSStringRef.cpp:
  • API/JSStringRefBSTR.cpp:
  • API/JSStringRefCF.cpp:
  • API/JSValueRef.cpp:
  • API/tests/JSNode.c:
  • API/tests/JSNode.h:
  • API/tests/JSNodeList.c:
  • API/tests/JSNodeList.h:
  • API/tests/Node.c:
  • API/tests/Node.h:
  • API/tests/NodeList.c:
  • API/tests/NodeList.h:
  • API/tests/minidom.c:
  • API/tests/minidom.js:
  • API/tests/testapi.c:
  • API/tests/testapi.js:
  • JavaScriptCore.pro:
  • kjs/FunctionConstructor.h:
  • kjs/FunctionPrototype.h:
  • kjs/JSArray.h:
  • kjs/JSString.h:
  • kjs/JSWrapperObject.cpp:
  • kjs/NumberConstructor.h:
  • kjs/NumberObject.h:
  • kjs/NumberPrototype.h:
  • kjs/lexer.h:
  • kjs/lookup.h:
  • wtf/Assertions.cpp:
  • wtf/Assertions.h:
  • wtf/HashCountedSet.h:
  • wtf/HashFunctions.h:
  • wtf/HashIterators.h:
  • wtf/HashMap.h:
  • wtf/HashSet.h:
  • wtf/HashTable.h:
  • wtf/HashTraits.h:
  • wtf/ListHashSet.h:
  • wtf/ListRefPtr.h:
  • wtf/Noncopyable.h:
  • wtf/OwnArrayPtr.h:
  • wtf/OwnPtr.h:
  • wtf/PassRefPtr.h:
  • wtf/Platform.h:
  • wtf/RefPtr.h:
  • wtf/RefPtrHashMap.h:
  • wtf/RetainPtr.h:
  • wtf/UnusedParam.h:
  • wtf/Vector.h:
  • wtf/VectorTraits.h:
  • wtf/unicode/Unicode.h:
  • wtf/unicode/icu/UnicodeIcu.h:

WebCore:

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