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 |
|
---|
32 | namespace 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 |
|
---|
207 | using WTF::AdoptCF;
|
---|
208 | using WTF::AdoptNS;
|
---|
209 | using WTF::RetainPtr;
|
---|
210 |
|
---|
211 | #endif // WTF_RetainPtr_h
|
---|