]>
git.saurik.com Git - apple/javascriptcore.git/blob - wtf/RetainPtr.h
a66a12748a03b87ef6bb22807258dce2819c34f1
2 * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
25 #include <CoreFoundation/CoreFoundation.h>
28 #import <Foundation/Foundation.h>
33 template <typename T
> struct RemovePointer
{
37 template <typename T
> struct RemovePointer
<T
*> {
41 // Unlike most most of our smart pointers, RetainPtr can take either the pointer type or the pointed-to type,
42 // so both RetainPtr<NSDictionary> and RetainPtr<CFDictionaryRef> will work.
44 enum AdoptCFTag
{ AdoptCF
};
45 enum AdoptNSTag
{ AdoptNS
};
48 inline void adoptNSReference(id ptr
)
57 template <typename T
> class RetainPtr
{
59 typedef typename RemovePointer
<T
>::type ValueType
;
60 typedef ValueType
* PtrType
;
62 RetainPtr() : m_ptr(0) {}
63 RetainPtr(PtrType ptr
) : m_ptr(ptr
) { if (ptr
) CFRetain(ptr
); }
65 RetainPtr(AdoptCFTag
, PtrType ptr
) : m_ptr(ptr
) { }
66 RetainPtr(AdoptNSTag
, PtrType ptr
) : m_ptr(ptr
) { adoptNSReference(ptr
); }
68 RetainPtr(const RetainPtr
& o
) : m_ptr(o
.m_ptr
) { if (PtrType ptr
= m_ptr
) CFRetain(ptr
); }
70 ~RetainPtr() { if (PtrType ptr
= m_ptr
) CFRelease(ptr
); }
72 template <typename U
> RetainPtr(const RetainPtr
<U
>& o
) : m_ptr(o
.get()) { if (PtrType ptr
= m_ptr
) CFRetain(ptr
); }
74 PtrType
get() const { return m_ptr
; }
76 PtrType
releaseRef() { PtrType tmp
= m_ptr
; m_ptr
= 0; return tmp
; }
78 PtrType
operator->() const { return m_ptr
; }
80 bool operator!() const { return !m_ptr
; }
82 // This conversion operator allows implicit conversion to bool but not to other integer types.
83 typedef PtrType
RetainPtr::*UnspecifiedBoolType
;
84 operator UnspecifiedBoolType() const { return m_ptr
? &RetainPtr::m_ptr
: 0; }
86 RetainPtr
& operator=(const RetainPtr
&);
87 template <typename U
> RetainPtr
& operator=(const RetainPtr
<U
>&);
88 RetainPtr
& operator=(PtrType
);
89 template <typename U
> RetainPtr
& operator=(U
*);
91 void adoptCF(PtrType
);
92 void adoptNS(PtrType
);
94 void swap(RetainPtr
&);
100 template <typename T
> inline RetainPtr
<T
>& RetainPtr
<T
>::operator=(const RetainPtr
<T
>& o
)
102 PtrType optr
= o
.get();
112 template <typename T
> template <typename U
> inline RetainPtr
<T
>& RetainPtr
<T
>::operator=(const RetainPtr
<U
>& o
)
114 PtrType optr
= o
.get();
124 template <typename T
> inline RetainPtr
<T
>& RetainPtr
<T
>::operator=(PtrType optr
)
135 template <typename T
> inline void RetainPtr
<T
>::adoptCF(PtrType optr
)
143 template <typename T
> inline void RetainPtr
<T
>::adoptNS(PtrType optr
)
145 adoptNSReference(optr
);
153 template <typename T
> template <typename U
> inline RetainPtr
<T
>& RetainPtr
<T
>::operator=(U
* optr
)
164 template <class T
> inline void RetainPtr
<T
>::swap(RetainPtr
<T
>& o
)
166 std::swap(m_ptr
, o
.m_ptr
);
169 template <class T
> inline void swap(RetainPtr
<T
>& a
, RetainPtr
<T
>& b
)
174 template <typename T
, typename U
> inline bool operator==(const RetainPtr
<T
>& a
, const RetainPtr
<U
>& b
)
176 return a
.get() == b
.get();
179 template <typename T
, typename U
> inline bool operator==(const RetainPtr
<T
>& a
, U
* b
)
184 template <typename T
, typename U
> inline bool operator==(T
* a
, const RetainPtr
<U
>& b
)
189 template <typename T
, typename U
> inline bool operator!=(const RetainPtr
<T
>& a
, const RetainPtr
<U
>& b
)
191 return a
.get() != b
.get();
194 template <typename T
, typename U
> inline bool operator!=(const RetainPtr
<T
>& a
, U
* b
)
199 template <typename T
, typename U
> inline bool operator!=(T
* a
, const RetainPtr
<U
>& b
)
208 using WTF::RetainPtr
;
210 #endif // WTF_RetainPtr_h