]> git.saurik.com Git - wxWidgets.git/blame - include/wx/cocoa/ObjcRef.h
wxMSWUniv build fix.
[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>
65571936 9// Licence: wxWindows licence
fa8114de
DE
10/////////////////////////////////////////////////////////////////////////////
11
256af34f
DE
12#ifndef _WX_COCOA_OBJCREF_H__
13#define _WX_COCOA_OBJCREF_H__
fa8114de
DE
14/*
15wxObjcAutoRefFromAlloc: construct a reference to an object that was
16[NSObject -alloc]'ed and thus does not need a retain
17wxObjcAutoRef: construct a reference to an object that was
18either autoreleased or is retained by something else.
19*/
20
21struct objc_object;
22
23// We must do any calls to Objective-C from an Objective-C++ source file
24class wxObjcAutoRefBase
25{
26protected:
27 static struct objc_object* ObjcRetain(struct objc_object*);
e76d459d 28 static void ObjcRelease(struct objc_object*);
fa8114de
DE
29};
30
31// T should be a pointer like NSObject*
32
33template <class T>
34class wxObjcAutoRefFromAlloc: wxObjcAutoRefBase
35{
36public:
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; }
56protected:
57 T m_ptr;
58};
59
60// The only thing wxObjcAutoRef has to do is retain an initial object
61template <class T>
62class wxObjcAutoRef: public wxObjcAutoRefFromAlloc<T>
63{
64public:
65 wxObjcAutoRef(T p = 0)
66 : wxObjcAutoRefFromAlloc<T>(p)
4508c2b0 67 { ObjcRetain(wxObjcAutoRefFromAlloc<T>::m_ptr); }
fa8114de
DE
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
256af34f 79#endif //ndef _WX_COCOA_OBJCREF_H__