X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b4e0eabd78bfbd7434cf7b959f9449439e0d1dfe..e0062c047ecb5a7d4790b980777dfa70869f4834:/include/wx/mac/corefoundation/cfref.h diff --git a/include/wx/mac/corefoundation/cfref.h b/include/wx/mac/corefoundation/cfref.h index 29485f4325..7506791c0f 100644 --- a/include/wx/mac/corefoundation/cfref.h +++ b/include/wx/mac/corefoundation/cfref.h @@ -2,10 +2,10 @@ // Name: wx/mac/corefoundation/cfref.h // Purpose: wxCFRef template class // Author: David Elliott -// Modified by: +// Modified by: Stefan Csomor // Created: 2007/05/10 // RCS-ID: $Id$ -// Copyright: (c) 2007 David Elliott +// Copyright: (c) 2007 David Elliott , Stefan Csomor // Licence: wxWindows licence // Notes: See http://developer.apple.com/documentation/CoreFoundation/Conceptual/CFMemoryMgmt/index.html ///////////////////////////////////////////////////////////////////////////// @@ -19,6 +19,32 @@ #include +/*! @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 +inline void wxCFRelease(Type *r) +{ + if ( r != NULL ) + ::CFRelease((CFTypeRef)r); +} + +/*! @function wxCFRetain + @abstract A typesafe CFRetain variant that checks for NULL. +*/ +template +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 @@ -28,11 +54,6 @@ template class wxCFRef { - // Declare wxCFRef as a friend so that the conversion constructor can access m_ptr directly - template - friend class wxCFRef; - - public: /*! @method wxCFRef @abstract Creates a NULL reference @@ -65,11 +86,8 @@ public: 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 @@ -80,11 +98,8 @@ public: */ template wxCFRef(const wxCFRef& 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. @@ -102,10 +117,8 @@ public: */ 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; } @@ -120,11 +133,9 @@ public: template wxCFRef& operator=(const wxCFRef& 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; } @@ -160,8 +171,7 @@ public: */ void reset() { - if(m_ptr != NULL) - CFRelease(m_ptr); + wxCFRelease(m_ptr); m_ptr = NULL; } @@ -178,8 +188,7 @@ public: template void reset(otherType* p) { - if(m_ptr != NULL) - CFRelease(m_ptr); + wxCFRelease(m_ptr); m_ptr = p; // Automatic conversion should occur } protected: @@ -199,7 +208,7 @@ protected: template inline wxCFRef wxCFRefFromGet(Type *p) { - return wxCFRef( (p!=NULL) ? (Type*)CFRetain(p) : p ); + return wxCFRef(wxCFRetain(p)); } /*! @function static_cfref_cast @@ -217,7 +226,7 @@ inline wxCFRef static_cfref_cast(const wxCFRef &otherRef) template inline wxCFRef static_cfref_cast(const wxCFRef &otherRef) { - return wxCFRef(static_cast(CFRetain(otherRef.get()))); + return wxCFRef(static_cast(wxCFRetain(otherRef.get()))); } /*! @function CFRelease