]>
Commit | Line | Data |
---|---|---|
fa8114de DE |
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 | /* | |
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. | |
17 | */ | |
18 | ||
19 | struct objc_object; | |
20 | ||
21 | // We must do any calls to Objective-C from an Objective-C++ source file | |
22 | class wxObjcAutoRefBase | |
23 | { | |
24 | protected: | |
25 | static struct objc_object* ObjcRetain(struct objc_object*); | |
26 | static struct objc_object* ObjcRelease(struct objc_object*); | |
27 | }; | |
28 | ||
29 | // T should be a pointer like NSObject* | |
30 | ||
31 | template <class T> | |
32 | class wxObjcAutoRefFromAlloc: wxObjcAutoRefBase | |
33 | { | |
34 | public: | |
35 | wxObjcAutoRefFromAlloc(T p = 0) | |
36 | : m_ptr(p) | |
37 | // NOTE: this is from alloc. Do NOT retain | |
38 | {} | |
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); | |
46 | ObjcRelease(m_ptr); | |
47 | m_ptr = otherRef.m_ptr; | |
48 | return *this; | |
49 | } | |
50 | operator T() const | |
51 | { return m_ptr; } | |
52 | T operator->() const | |
53 | { return m_ptr; } | |
54 | protected: | |
55 | T m_ptr; | |
56 | }; | |
57 | ||
58 | // The only thing wxObjcAutoRef has to do is retain an initial object | |
59 | template <class T> | |
60 | class wxObjcAutoRef: public wxObjcAutoRefFromAlloc<T> | |
61 | { | |
62 | public: | |
63 | wxObjcAutoRef(T p = 0) | |
64 | : wxObjcAutoRefFromAlloc<T>(p) | |
65 | { ObjcRetain(m_ptr); } | |
66 | ~wxObjcAutoRef() {} | |
67 | wxObjcAutoRef(const wxObjcAutoRef& otherRef) | |
68 | : wxObjcAutoRefFromAlloc<T>(otherRef) | |
69 | {} | |
70 | wxObjcAutoRef(const wxObjcAutoRefFromAlloc<T>& otherRef) | |
71 | : wxObjcAutoRefFromAlloc<T>(otherRef) | |
72 | {} | |
73 | wxObjcAutoRef& operator=(const wxObjcAutoRef& otherRef) | |
74 | { return wxObjcAutoRefFromAlloc<T>::operator=(otherRef); } | |
75 | }; | |
76 |