From fa8114de44a45d992e45912818307d8b1a5004ba Mon Sep 17 00:00:00 2001 From: David Elliott Date: Fri, 2 Apr 2004 17:43:59 +0000 Subject: [PATCH] Added wxObjcAutoRef* classes for automatic retain/release of Objective-C objects git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@26580 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/cocoa/ObjcRef.h | 76 ++++++++++++++++++++++++++++++++++++++ src/cocoa/ObjcRef.mm | 25 +++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 include/wx/cocoa/ObjcRef.h create mode 100644 src/cocoa/ObjcRef.mm diff --git a/include/wx/cocoa/ObjcRef.h b/include/wx/cocoa/ObjcRef.h new file mode 100644 index 0000000000..2af1c2598a --- /dev/null +++ b/include/wx/cocoa/ObjcRef.h @@ -0,0 +1,76 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: wx/cocoa/ObjcRef.h +// Purpose: wxObjcAutoRef template class +// Author: David Elliott +// Modified by: +// Created: 2004/03/28 +// RCS-ID: $Id$ +// Copyright: (c) 2004 David Elliott +// Licence: wxWindows licence +///////////////////////////////////////////////////////////////////////////// + +/* +wxObjcAutoRefFromAlloc: construct a reference to an object that was +[NSObject -alloc]'ed and thus does not need a retain +wxObjcAutoRef: construct a reference to an object that was +either autoreleased or is retained by something else. +*/ + +struct objc_object; + +// We must do any calls to Objective-C from an Objective-C++ source file +class wxObjcAutoRefBase +{ +protected: + static struct objc_object* ObjcRetain(struct objc_object*); + static struct objc_object* ObjcRelease(struct objc_object*); +}; + +// T should be a pointer like NSObject* + +template +class wxObjcAutoRefFromAlloc: wxObjcAutoRefBase +{ +public: + wxObjcAutoRefFromAlloc(T p = 0) + : m_ptr(p) + // NOTE: this is from alloc. Do NOT retain + {} + wxObjcAutoRefFromAlloc(const wxObjcAutoRefFromAlloc& otherRef) + : m_ptr(otherRef.m_ptr) + { ObjcRetain(m_ptr); } + ~wxObjcAutoRefFromAlloc() + { ObjcRelease(m_ptr); } + wxObjcAutoRefFromAlloc& operator=(const wxObjcAutoRefFromAlloc& otherRef) + { ObjcRetain(otherRef.m_ptr); + ObjcRelease(m_ptr); + m_ptr = otherRef.m_ptr; + return *this; + } + operator T() const + { return m_ptr; } + T operator->() const + { return m_ptr; } +protected: + T m_ptr; +}; + +// The only thing wxObjcAutoRef has to do is retain an initial object +template +class wxObjcAutoRef: public wxObjcAutoRefFromAlloc +{ +public: + wxObjcAutoRef(T p = 0) + : wxObjcAutoRefFromAlloc(p) + { ObjcRetain(m_ptr); } + ~wxObjcAutoRef() {} + wxObjcAutoRef(const wxObjcAutoRef& otherRef) + : wxObjcAutoRefFromAlloc(otherRef) + {} + wxObjcAutoRef(const wxObjcAutoRefFromAlloc& otherRef) + : wxObjcAutoRefFromAlloc(otherRef) + {} + wxObjcAutoRef& operator=(const wxObjcAutoRef& otherRef) + { return wxObjcAutoRefFromAlloc::operator=(otherRef); } +}; + diff --git a/src/cocoa/ObjcRef.mm b/src/cocoa/ObjcRef.mm new file mode 100644 index 0000000000..ace3567500 --- /dev/null +++ b/src/cocoa/ObjcRef.mm @@ -0,0 +1,25 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: cocoa/ObjcRef.mm +// Purpose: wxObjcAutoRefBase implementation +// Author: David Elliott +// Modified by: +// Created: 2004/03/28 +// RCS-ID: $Id$ +// Copyright: (c) 2004 David Elliott +// Licence: wxWindows licence +///////////////////////////////////////////////////////////////////////////// + +#include "wx/cocoa/ObjcRef.h" + +#include + +/*static*/ struct objc_object* wxObjcAutoRefBase::ObjcRetain(struct objc_object* obj) +{ + return [obj retain]; +} + +/*static*/ struct objc_object* wxObjcAutoRefBase::ObjcRelease(struct objc_object* obj) +{ + return [obj release]; +} + -- 2.45.2