]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/cocoa/ObjcRef.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/cocoa/ObjcRef.h
3 // Purpose: wxObjcAutoRef template class
4 // Author: David Elliott
8 // Copyright: (c) 2004 David Elliott <dfe@cox.net>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 wxObjcAutoRefFromAlloc: construct a reference to an object that was
14 [NSObject -alloc]'ed and thus does not need a retain
15 wxObjcAutoRef: construct a reference to an object that was
16 either autoreleased or is retained by something else.
21 // We must do any calls to Objective-C from an Objective-C++ source file
22 class wxObjcAutoRefBase
25 static struct objc_object
* ObjcRetain(struct objc_object
*);
26 static struct objc_object
* ObjcRelease(struct objc_object
*);
29 // T should be a pointer like NSObject*
32 class wxObjcAutoRefFromAlloc
: wxObjcAutoRefBase
35 wxObjcAutoRefFromAlloc(T p
= 0)
37 // NOTE: this is from alloc. Do NOT retain
39 wxObjcAutoRefFromAlloc(const wxObjcAutoRefFromAlloc
& otherRef
)
40 : m_ptr(otherRef
.m_ptr
)
41 { ObjcRetain(m_ptr
); }
42 ~wxObjcAutoRefFromAlloc()
43 { ObjcRelease(m_ptr
); }
44 wxObjcAutoRefFromAlloc
& operator=(const wxObjcAutoRefFromAlloc
& otherRef
)
45 { ObjcRetain(otherRef
.m_ptr
);
47 m_ptr
= otherRef
.m_ptr
;
58 // The only thing wxObjcAutoRef has to do is retain an initial object
60 class wxObjcAutoRef
: public wxObjcAutoRefFromAlloc
<T
>
63 wxObjcAutoRef(T p
= 0)
64 : wxObjcAutoRefFromAlloc
<T
>(p
)
65 { ObjcRetain(m_ptr
); }
67 wxObjcAutoRef(const wxObjcAutoRef
& otherRef
)
68 : wxObjcAutoRefFromAlloc
<T
>(otherRef
)
70 wxObjcAutoRef(const wxObjcAutoRefFromAlloc
<T
>& otherRef
)
71 : wxObjcAutoRefFromAlloc
<T
>(otherRef
)
73 wxObjcAutoRef
& operator=(const wxObjcAutoRef
& otherRef
)
74 { return wxObjcAutoRefFromAlloc
<T
>::operator=(otherRef
); }