]> git.saurik.com Git - wxWidgets.git/blob - include/wx/cocoa/ObjcRef.h
use correct test
[wxWidgets.git] / include / wx / cocoa / ObjcRef.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/cocoa/ObjcRef.h
3 // Purpose: wxObjcAutoRef template class
4 // Author: David Elliott
5 // Modified by:
6 // Created: 2004/03/28
7 // RCS-ID: $Id$
8 // Copyright: (c) 2004 David Elliott <dfe@cox.net>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_COCOA_OBJCREF_H__
13 #define _WX_COCOA_OBJCREF_H__
14 /*
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.
19 */
20
21 struct objc_object;
22
23 // We must do any calls to Objective-C from an Objective-C++ source file
24 class wxObjcAutoRefBase
25 {
26 protected:
27 static struct objc_object* ObjcRetain(struct objc_object*);
28 static void ObjcRelease(struct objc_object*);
29 };
30
31 // T should be a pointer like NSObject*
32
33 template <class T>
34 class wxObjcAutoRefFromAlloc: wxObjcAutoRefBase
35 {
36 public:
37 wxObjcAutoRefFromAlloc(T p = 0)
38 : m_ptr(p)
39 // NOTE: this is from alloc. Do NOT retain
40 {}
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);
48 ObjcRelease(m_ptr);
49 m_ptr = otherRef.m_ptr;
50 return *this;
51 }
52 operator T() const
53 { return m_ptr; }
54 T operator->() const
55 { return m_ptr; }
56 protected:
57 T m_ptr;
58 };
59
60 // The only thing wxObjcAutoRef has to do is retain an initial object
61 template <class T>
62 class wxObjcAutoRef: public wxObjcAutoRefFromAlloc<T>
63 {
64 public:
65 wxObjcAutoRef(T p = 0)
66 : wxObjcAutoRefFromAlloc<T>(p)
67 { ObjcRetain(wxObjcAutoRefFromAlloc<T>::m_ptr); }
68 ~wxObjcAutoRef() {}
69 wxObjcAutoRef(const wxObjcAutoRef& otherRef)
70 : wxObjcAutoRefFromAlloc<T>(otherRef)
71 {}
72 wxObjcAutoRef(const wxObjcAutoRefFromAlloc<T>& otherRef)
73 : wxObjcAutoRefFromAlloc<T>(otherRef)
74 {}
75 wxObjcAutoRef& operator=(const wxObjcAutoRef& otherRef)
76 { return wxObjcAutoRefFromAlloc<T>::operator=(otherRef); }
77 };
78
79 #endif //ndef _WX_COCOA_OBJCREF_H__