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.
29 #include <CoreFoundation/CoreFoundation.h>
33 #import <Foundation/Foundation.h>
38 template <typename T> struct RemovePointer {
42 template <typename T> struct RemovePointer<T*> {
46 // Unlike most most of our smart pointers, RetainPtr can take either the pointer type or the pointed-to type,
47 // so both RetainPtr<NSDictionary> and RetainPtr<CFDictionaryRef> will work.
49 enum AdoptCFTag { AdoptCF };
50 enum AdoptNSTag { AdoptNS };
53 inline void adoptNSReference(id ptr)
62 template <typename T> class RetainPtr {
64 typedef typename RemovePointer<T>::type ValueType;
65 typedef ValueType* PtrType;
67 RetainPtr() : m_ptr(0) {}
68 RetainPtr(PtrType ptr) : m_ptr(ptr) { if (ptr) CFRetain(ptr); }
70 RetainPtr(AdoptCFTag, PtrType ptr) : m_ptr(ptr) { }
71 RetainPtr(AdoptNSTag, PtrType ptr) : m_ptr(ptr) { adoptNSReference(ptr); }
73 RetainPtr(const RetainPtr& o) : m_ptr(o.m_ptr) { if (PtrType ptr = m_ptr) CFRetain(ptr); }
75 ~RetainPtr() { if (PtrType ptr = m_ptr) CFRelease(ptr); }
77 template <typename U> RetainPtr(const RetainPtr<U>& o) : m_ptr(o.get()) { if (PtrType ptr = m_ptr) CFRetain(ptr); }
79 PtrType get() const { return m_ptr; }
81 PtrType releaseRef() { PtrType tmp = m_ptr; m_ptr = 0; return tmp; }
83 PtrType operator->() const { return m_ptr; }
85 bool operator!() const { return !m_ptr; }
87 // This conversion operator allows implicit conversion to bool but not to other integer types.
88 typedef PtrType RetainPtr::*UnspecifiedBoolType;
89 operator UnspecifiedBoolType() const { return m_ptr ? &RetainPtr::m_ptr : 0; }
91 RetainPtr& operator=(const RetainPtr&);
92 template <typename U> RetainPtr& operator=(const RetainPtr<U>&);
93 RetainPtr& operator=(PtrType);
94 template <typename U> RetainPtr& operator=(U*);
96 void adoptCF(PtrType);
97 void adoptNS(PtrType);
99 void swap(RetainPtr&);
105 template <typename T> inline RetainPtr<T>& RetainPtr<T>::operator=(const RetainPtr<T>& o)
107 PtrType optr = o.get();
117 template <typename T> template <typename U> inline RetainPtr<T>& RetainPtr<T>::operator=(const RetainPtr<U>& o)
119 PtrType optr = o.get();
129 template <typename T> inline RetainPtr<T>& RetainPtr<T>::operator=(PtrType optr)
140 template <typename T> inline void RetainPtr<T>::adoptCF(PtrType optr)
148 template <typename T> inline void RetainPtr<T>::adoptNS(PtrType optr)
150 adoptNSReference(optr);
158 template <typename T> template <typename U> inline RetainPtr<T>& RetainPtr<T>::operator=(U* optr)
169 template <class T> inline void RetainPtr<T>::swap(RetainPtr<T>& o)
171 std::swap(m_ptr, o.m_ptr);
174 template <class T> inline void swap(RetainPtr<T>& a, RetainPtr<T>& b)
179 template <typename T, typename U> inline bool operator==(const RetainPtr<T>& a, const RetainPtr<U>& b)
181 return a.get() == b.get();
184 template <typename T, typename U> inline bool operator==(const RetainPtr<T>& a, U* b)
189 template <typename T, typename U> inline bool operator==(T* a, const RetainPtr<U>& b)
194 template <typename T, typename U> inline bool operator!=(const RetainPtr<T>& a, const RetainPtr<U>& b)
196 return a.get() != b.get();
199 template <typename T, typename U> inline bool operator!=(const RetainPtr<T>& a, U* b)
204 template <typename T, typename U> inline bool operator!=(T* a, const RetainPtr<U>& b)
213 using WTF::RetainPtr;
215 #endif // WTF_RetainPtr_h