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 {
61 typedef typename RemovePointer<T>::type ValueType;
62 typedef ValueType* PtrType;
64 RetainPtr() : m_ptr(0) {}
65 RetainPtr(PtrType ptr) : m_ptr(ptr) { if (ptr) CFRetain(ptr); }
67 RetainPtr(AdoptCFTag, PtrType ptr) : m_ptr(ptr) { }
68 RetainPtr(AdoptNSTag, PtrType ptr) : m_ptr(ptr) { adoptNSReference(ptr); }
70 RetainPtr(const RetainPtr& o) : m_ptr(o.m_ptr) { if (PtrType ptr = m_ptr) CFRetain(ptr); }
72 ~RetainPtr() { if (PtrType ptr = m_ptr) CFRelease(ptr); }
74 template <typename U> RetainPtr(const RetainPtr<U>& o) : m_ptr(o.get()) { if (PtrType ptr = m_ptr) CFRetain(ptr); }
76 PtrType get() const { return m_ptr; }
78 PtrType releaseRef() { PtrType tmp = m_ptr; m_ptr = 0; return tmp; }
80 PtrType operator->() const { return m_ptr; }
82 bool operator!() const { return !m_ptr; }
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; }
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*);
93 void adoptCF(PtrType);
94 void adoptNS(PtrType);
96 void swap(RetainPtr&);
102 template <typename T> inline RetainPtr<T>& RetainPtr<T>::operator=(const RetainPtr<T>& o)
104 PtrType optr = o.get();
114 template <typename T> template <typename U> inline RetainPtr<T>& RetainPtr<T>::operator=(const RetainPtr<U>& o)
116 PtrType optr = o.get();
126 template <typename T> inline RetainPtr<T>& RetainPtr<T>::operator=(PtrType optr)
137 template <typename T> inline void RetainPtr<T>::adoptCF(PtrType optr)
145 template <typename T> inline void RetainPtr<T>::adoptNS(PtrType optr)
147 adoptNSReference(optr);
155 template <typename T> template <typename U> inline RetainPtr<T>& RetainPtr<T>::operator=(U* optr)
166 template <class T> inline void RetainPtr<T>::swap(RetainPtr<T>& o)
168 std::swap(m_ptr, o.m_ptr);
171 template <class T> inline void swap(RetainPtr<T>& a, RetainPtr<T>& b)
176 template <typename T, typename U> inline bool operator==(const RetainPtr<T>& a, const RetainPtr<U>& b)
178 return a.get() == b.get();
181 template <typename T, typename U> inline bool operator==(const RetainPtr<T>& a, U* b)
186 template <typename T, typename U> inline bool operator==(T* a, const RetainPtr<U>& b)
191 template <typename T, typename U> inline bool operator!=(const RetainPtr<T>& a, const RetainPtr<U>& b)
193 return a.get() != b.get();
196 template <typename T, typename U> inline bool operator!=(const RetainPtr<T>& a, U* b)
201 template <typename T, typename U> inline bool operator!=(T* a, const RetainPtr<U>& b)
210 using WTF::RetainPtr;
212 #endif // WTF_RetainPtr_h