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