]>
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 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_COCOA_OBJCREF_H__
13 #define _WX_COCOA_OBJCREF_H__
15 wxObjcAutoRefFromAlloc: construct a reference to an object that was
16 [NSObject -alloc]'ed and thus does not need a retain
17 wxObjcAutoRef: construct a reference to an object that was
18 either autoreleased or is retained by something else.
23 // We must do any calls to Objective-C from an Objective-C++ source file
24 class wxObjcAutoRefBase
27 static struct objc_object
* ObjcRetain(struct objc_object
*);
28 static void ObjcRelease(struct objc_object
*);
31 // T should be a pointer like NSObject*
34 class wxObjcAutoRefFromAlloc
: wxObjcAutoRefBase
37 wxObjcAutoRefFromAlloc(T p
= 0)
39 // NOTE: this is from alloc. Do NOT retain
41 wxObjcAutoRefFromAlloc(const wxObjcAutoRefFromAlloc
& otherRef
)
42 : m_ptr(otherRef
.m_ptr
)
43 { ObjcRetain(m_ptr
); }
44 ~wxObjcAutoRefFromAlloc()
45 { ObjcRelease(m_ptr
); }
46 wxObjcAutoRefFromAlloc
& operator=(const wxObjcAutoRefFromAlloc
& otherRef
)
47 { ObjcRetain(otherRef
.m_ptr
);
49 m_ptr
= otherRef
.m_ptr
;
60 // The only thing wxObjcAutoRef has to do is retain an initial object
62 class wxObjcAutoRef
: public wxObjcAutoRefFromAlloc
<T
>
65 wxObjcAutoRef(T p
= 0)
66 : wxObjcAutoRefFromAlloc
<T
>(p
)
67 { ObjcRetain(m_ptr
); }
69 wxObjcAutoRef(const wxObjcAutoRef
& otherRef
)
70 : wxObjcAutoRefFromAlloc
<T
>(otherRef
)
72 wxObjcAutoRef(const wxObjcAutoRefFromAlloc
<T
>& otherRef
)
73 : wxObjcAutoRefFromAlloc
<T
>(otherRef
)
75 wxObjcAutoRef
& operator=(const wxObjcAutoRef
& otherRef
)
76 { return wxObjcAutoRefFromAlloc
<T
>::operator=(otherRef
); }
79 #endif //ndef _WX_COCOA_OBJCREF_H__