// Name: wx/mac/corefoundation/cfref.h
// Purpose: wxCFRef template class
// Author: David Elliott <dfe@cox.net>
-// Modified by:
+// Modified by: Stefan Csomor
// Created: 2007/05/10
// RCS-ID: $Id$
-// Copyright: (c) 2007 David Elliott <dfe@cox.net>
+// Copyright: (c) 2007 David Elliott <dfe@cox.net>, Stefan Csomor
// Licence: wxWindows licence
// Notes: See http://developer.apple.com/documentation/CoreFoundation/Conceptual/CFMemoryMgmt/index.html
/////////////////////////////////////////////////////////////////////////////
#include <CoreFoundation/CFBase.h>
+/*! @function wxCFRelease
+ @abstract A CFRelease variant that checks for NULL before releasing.
+ @discussion The parameter is template not for type safety but to ensure the argument
+ is a raw pointer and not a ref holder of any type.
+*/
+template <class Type>
+inline void wxCFRelease(Type *r)
+{
+ if ( r != NULL )
+ ::CFRelease((CFTypeRef)r);
+}
+
+/*! @function wxCFRetain
+ @abstract A typesafe CFRetain variant that checks for NULL.
+*/
+template <class Type>
+inline Type* wxCFRetain(Type *r)
+{
+ // NOTE(DE): Setting r to the result of CFRetain improves efficiency on both x86 and PPC
+ // Casting r to CFTypeRef ensures we are calling the real C version defined in CFBase.h
+ // and not any possibly templated/overloaded CFRetain.
+ if ( r != NULL )
+ r = (Type*)::CFRetain((CFTypeRef)r);
+ return r;
+}
+
/*! @class wxCFRef
@templatefield refType The CF reference type (e.g. CFStringRef, CFRunLoopRef, etc.)
It should already be a pointer. This is different from
template <class refType>
class wxCFRef
{
- // Declare wxCFRef<otherRefType> as a friend so that the conversion constructor can access m_ptr directly
- template <class otherRefType>
- friend class wxCFRef;
-
-
public:
/*! @method wxCFRef
@abstract Creates a NULL reference
the object will be explicitly retained by this new ref.
*/
wxCFRef(const wxCFRef& otherRef)
- : m_ptr(otherRef.m_ptr)
- {
- if(m_ptr != NULL)
- CFRetain(m_ptr);
- }
+ : m_ptr(wxCFRetain(otherRef.m_ptr))
+ {}
/*! @method wxCFRef
@abstract Copies a ref holder where its type can be converted to ours
*/
template <class otherRefType>
wxCFRef(const wxCFRef<otherRefType>& otherRef)
- : m_ptr(otherRef.m_ptr) // Implicit conversion from otherRefType to refType should occur
- {
- if(m_ptr != NULL)
- CFRetain(m_ptr);
- }
+ : m_ptr(wxCFRetain(otherRef.get())) // Implicit conversion from otherRefType to refType should occur
+ {}
/*! @method ~wxCFRef
@abstract Releases (potentially shared) ownership of the ref.
*/
wxCFRef& operator=(const wxCFRef& otherRef)
{
- if(otherRef.m_ptr != NULL)
- CFRetain(otherRef.m_ptr);
- if(m_ptr != NULL)
- CFRelease(m_ptr);
+ wxCFRetain(otherRef.m_ptr);
+ wxCFRelease(m_ptr);
m_ptr = otherRef.m_ptr;
return *this;
}
template <class otherRefType>
wxCFRef& operator=(const wxCFRef<otherRefType>& otherRef)
{
- if(otherRef.m_ptr != NULL)
- CFRetain(otherRef.m_ptr);
- if(m_ptr != NULL)
- CFRelease(m_ptr);
- m_ptr = otherRef.m_ptr; // Implicit conversion from otherRefType to refType should occur
+ wxCFRetain(otherRef.get());
+ wxCFRelease(m_ptr);
+ m_ptr = otherRef.get(); // Implicit conversion from otherRefType to refType should occur
return *this;
}
*/
void reset()
{
- if(m_ptr != NULL)
- CFRelease(m_ptr);
+ wxCFRelease(m_ptr);
m_ptr = NULL;
}
template <class otherType>
void reset(otherType* p)
{
- if(m_ptr != NULL)
- CFRelease(m_ptr);
+ wxCFRelease(m_ptr);
m_ptr = p; // Automatic conversion should occur
}
protected:
template <typename Type>
inline wxCFRef<Type*> wxCFRefFromGet(Type *p)
{
- return wxCFRef<Type*>( (p!=NULL) ? (Type*)CFRetain(p) : p );
+ return wxCFRef<Type*>(wxCFRetain(p));
}
/*! @function static_cfref_cast
template <class refType, class otherRefType>
inline wxCFRef<refType> static_cfref_cast(const wxCFRef<otherRefType> &otherRef)
{
- return wxCFRef<refType>(static_cast<refType>(CFRetain(otherRef.get())));
+ return wxCFRef<refType>(static_cast<refType>(wxCFRetain(otherRef.get())));
}
/*! @function CFRelease