1
// -*- mode: c++; c-basic-offset: 4 -*-
3
* This file is part of the KDE libraries
4
* Copyright (C) 2005, 2006 Apple Computer, Inc.
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.
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.
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.
27
#include <CoreFoundation/CoreFoundation.h>
30
#import <Foundation/Foundation.h>
35
template <typename T> struct RemovePointer {
39
template <typename T> struct RemovePointer<T*> {
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.
46
enum AdoptCFTag { AdoptCF };
47
enum AdoptNSTag { AdoptNS };
50
inline void adoptNSReference(id ptr)
59
template <typename T> class RetainPtr
62
typedef typename RemovePointer<T>::type ValueType;
63
typedef ValueType* PtrType;
65
RetainPtr() : m_ptr(0) {}
66
RetainPtr(PtrType ptr) : m_ptr(ptr) { if (ptr) CFRetain(ptr); }
68
RetainPtr(AdoptCFTag, PtrType ptr) : m_ptr(ptr) { }
69
RetainPtr(AdoptNSTag, PtrType ptr) : m_ptr(ptr) { adoptNSReference(ptr); }
71
RetainPtr(const RetainPtr& o) : m_ptr(o.m_ptr) { if (PtrType ptr = m_ptr) CFRetain(ptr); }
73
~RetainPtr() { if (PtrType ptr = m_ptr) CFRelease(ptr); }
75
template <typename U> RetainPtr(const RetainPtr<U>& o) : m_ptr(o.get()) { if (PtrType ptr = m_ptr) CFRetain(ptr); }
77
PtrType get() const { return m_ptr; }
79
PtrType releaseRef() { PtrType tmp = m_ptr; m_ptr = 0; return tmp; }
81
PtrType operator->() const { return m_ptr; }
83
bool operator!() const { return !m_ptr; }
85
// This conversion operator allows implicit conversion to bool but not to other integer types.
86
typedef PtrType (RetainPtr::*UnspecifiedBoolType)() const;
87
operator UnspecifiedBoolType() const { return m_ptr ? &RetainPtr::get : 0; }
89
RetainPtr& operator=(const RetainPtr&);
90
template <typename U> RetainPtr& operator=(const RetainPtr<U>&);
91
RetainPtr& operator=(PtrType);
92
template <typename U> RetainPtr& operator=(U*);
94
void adoptCF(PtrType);
95
void adoptNS(PtrType);
97
void swap(RetainPtr&);
103
template <typename T> inline RetainPtr<T>& RetainPtr<T>::operator=(const RetainPtr<T>& o)
105
PtrType optr = o.get();
115
template <typename T> template <typename U> inline RetainPtr<T>& RetainPtr<T>::operator=(const RetainPtr<U>& o)
117
PtrType optr = o.get();
127
template <typename T> inline RetainPtr<T>& RetainPtr<T>::operator=(PtrType optr)
138
template <typename T> inline void RetainPtr<T>::adoptCF(PtrType optr)
146
template <typename T> inline void RetainPtr<T>::adoptNS(PtrType optr)
148
adoptNSReference(optr);
156
template <typename T> template <typename U> inline RetainPtr<T>& RetainPtr<T>::operator=(U* optr)
167
template <class T> inline void RetainPtr<T>::swap(RetainPtr<T>& o)
169
std::swap(m_ptr, o.m_ptr);
172
template <class T> inline void swap(RetainPtr<T>& a, RetainPtr<T>& b)
177
template <typename T, typename U> inline bool operator==(const RetainPtr<T>& a, const RetainPtr<U>& b)
179
return a.get() == b.get();
182
template <typename T, typename U> inline bool operator==(const RetainPtr<T>& a, U* b)
187
template <typename T, typename U> inline bool operator==(T* a, const RetainPtr<U>& b)
192
template <typename T, typename U> inline bool operator!=(const RetainPtr<T>& a, const RetainPtr<U>& b)
194
return a.get() != b.get();
197
template <typename T, typename U> inline bool operator!=(const RetainPtr<T>& a, U* b)
202
template <typename T, typename U> inline bool operator!=(T* a, const RetainPtr<U>& b)
211
using WTF::RetainPtr;
213
#endif // WTF_RetainPtr_h