]>
git.saurik.com Git - apple/javascriptcore.git/blob - wtf/RetainPtr.h
   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. 
  24 #include "TypeTraits.h" 
  26 #include <CoreFoundation/CoreFoundation.h> 
  29 #import <Foundation/Foundation.h> 
  34     // Unlike most most of our smart pointers, RetainPtr can take either the pointer type or the pointed-to type, 
  35     // so both RetainPtr<NSDictionary> and RetainPtr<CFDictionaryRef> will work. 
  37     enum AdoptCFTag 
{ AdoptCF 
}; 
  38     enum AdoptNSTag 
{ AdoptNS 
}; 
  41     inline void adoptNSReference(id ptr
) 
  50     template <typename T
> class RetainPtr 
{ 
  52         typedef typename RemovePointer
<T
>::Type ValueType
; 
  53         typedef ValueType
* PtrType
; 
  55         RetainPtr() : m_ptr(0) {} 
  56         RetainPtr(PtrType ptr
) : m_ptr(ptr
) { if (ptr
) CFRetain(ptr
); } 
  58         RetainPtr(AdoptCFTag
, PtrType ptr
) : m_ptr(ptr
) { } 
  59         RetainPtr(AdoptNSTag
, PtrType ptr
) : m_ptr(ptr
) { adoptNSReference(ptr
); } 
  61         RetainPtr(const RetainPtr
& o
) : m_ptr(o
.m_ptr
) { if (PtrType ptr 
= m_ptr
) CFRetain(ptr
); } 
  63         ~RetainPtr() { if (PtrType ptr 
= m_ptr
) CFRelease(ptr
); } 
  65         template <typename U
> RetainPtr(const RetainPtr
<U
>& o
) : m_ptr(o
.get()) { if (PtrType ptr 
= m_ptr
) CFRetain(ptr
); } 
  67         PtrType 
get() const { return m_ptr
; } 
  69         PtrType 
releaseRef() { PtrType tmp 
= m_ptr
; m_ptr 
= 0; return tmp
; } 
  71         PtrType 
operator->() const { return m_ptr
; } 
  73         bool operator!() const { return !m_ptr
; } 
  75         // This conversion operator allows implicit conversion to bool but not to other integer types. 
  76         typedef PtrType 
RetainPtr::*UnspecifiedBoolType
; 
  77         operator UnspecifiedBoolType() const { return m_ptr 
? &RetainPtr::m_ptr 
: 0; } 
  79         RetainPtr
& operator=(const RetainPtr
&); 
  80         template <typename U
> RetainPtr
& operator=(const RetainPtr
<U
>&); 
  81         RetainPtr
& operator=(PtrType
); 
  82         template <typename U
> RetainPtr
& operator=(U
*); 
  84         void adoptCF(PtrType
); 
  85         void adoptNS(PtrType
); 
  87         void swap(RetainPtr
&); 
  93     template <typename T
> inline RetainPtr
<T
>& RetainPtr
<T
>::operator=(const RetainPtr
<T
>& o
) 
  95         PtrType optr 
= o
.get(); 
 105     template <typename T
> template <typename U
> inline RetainPtr
<T
>& RetainPtr
<T
>::operator=(const RetainPtr
<U
>& o
) 
 107         PtrType optr 
= o
.get(); 
 117     template <typename T
> inline RetainPtr
<T
>& RetainPtr
<T
>::operator=(PtrType optr
) 
 128     template <typename T
> inline void RetainPtr
<T
>::adoptCF(PtrType optr
) 
 136     template <typename T
> inline void RetainPtr
<T
>::adoptNS(PtrType optr
) 
 138         adoptNSReference(optr
); 
 146     template <typename T
> template <typename U
> inline RetainPtr
<T
>& RetainPtr
<T
>::operator=(U
* optr
) 
 157     template <class T
> inline void RetainPtr
<T
>::swap(RetainPtr
<T
>& o
) 
 159         std::swap(m_ptr
, o
.m_ptr
); 
 162     template <class T
> inline void swap(RetainPtr
<T
>& a
, RetainPtr
<T
>& b
) 
 167     template <typename T
, typename U
> inline bool operator==(const RetainPtr
<T
>& a
, const RetainPtr
<U
>& b
) 
 169         return a
.get() == b
.get();  
 172     template <typename T
, typename U
> inline bool operator==(const RetainPtr
<T
>& a
, U
* b
) 
 177     template <typename T
, typename U
> inline bool operator==(T
* a
, const RetainPtr
<U
>& b
)  
 182     template <typename T
, typename U
> inline bool operator!=(const RetainPtr
<T
>& a
, const RetainPtr
<U
>& b
) 
 184         return a
.get() != b
.get();  
 187     template <typename T
, typename U
> inline bool operator!=(const RetainPtr
<T
>& a
, U
* b
) 
 192     template <typename T
, typename U
> inline bool operator!=(T
* a
, const RetainPtr
<U
>& b
) 
 201 using WTF::RetainPtr
; 
 203 #endif // WTF_RetainPtr_h