]> git.saurik.com Git - wxWidgets.git/blame - include/wx/cocoa/ObjcRef.h
include wrapwin.h instead of windows.h directly (fixes CE compilation problems)
[wxWidgets.git] / include / wx / cocoa / ObjcRef.h
CommitLineData
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/*
13wxObjcAutoRefFromAlloc: construct a reference to an object that was
14[NSObject -alloc]'ed and thus does not need a retain
15wxObjcAutoRef: construct a reference to an object that was
16either autoreleased or is retained by something else.
17*/
18
19struct objc_object;
20
21// We must do any calls to Objective-C from an Objective-C++ source file
22class wxObjcAutoRefBase
23{
24protected:
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
31template <class T>
32class wxObjcAutoRefFromAlloc: wxObjcAutoRefBase
33{
34public:
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; }
54protected:
55 T m_ptr;
56};
57
58// The only thing wxObjcAutoRef has to do is retain an initial object
59template <class T>
60class wxObjcAutoRef: public wxObjcAutoRefFromAlloc<T>
61{
62public:
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