]> git.saurik.com Git - wxWidgets.git/blame - interface/wx/weakref.h
Move code removing "-psn_xxx" command line arguments to common code.
[wxWidgets.git] / interface / wx / weakref.h
CommitLineData
23324ae1
FM
1/////////////////////////////////////////////////////////////////////////////
2// Name: weakref.h
e25cd775 3// Purpose: interface of wxWeakRefDynamic<T>, wxWeakRef<T>
23324ae1 4// Author: wxWidgets team
526954c5 5// Licence: wxWindows licence
23324ae1
FM
6/////////////////////////////////////////////////////////////////////////////
7
e25cd775 8
23324ae1 9/**
73473b3e
RR
10 wxWeakRefDynamic<T> is a template class for weak references that is used in
11 the same way as wxWeakRef<T>. The only difference is that wxWeakRefDynamic
e25cd775
FM
12 defaults to using @c dynamic_cast for establishing the object reference
13 (while wxWeakRef defaults to @c static_cast).
7c913512 14
23324ae1
FM
15 So, wxWeakRef will detect a type mismatch during compile time and will
16 have a little better run-time performance. The role of wxWeakRefDynamic
7c913512
FM
17 is to handle objects which derived type one does not know.
18
e25cd775 19 @note wxWeakRef<T> selects an implementation based on the static type of T.
4c51a665 20 If T does not have wxTrackable statically, it defaults to a mixed-
e25cd775
FM
21 mode operation, where it uses @c dynamic_cast as the last measure
22 (if available from the compiler and enabled when building wxWidgets).
7c913512 23
73473b3e 24 For general cases, wxWeakRef<T> is the better choice.
7c913512 25
e25cd775 26 For API documentation, see: wxWeakRef<T>.
7c913512 27
6c107bd2 28 @tparam T
f045c7f5 29 The type to which the smart pointer points to.
6c107bd2 30
e25cd775 31 @nolibrary
d9a09273 32 @category{smartpointers}
23324ae1 33*/
5cb947fd 34template<typename T>
7c913512 35class wxWeakRefDynamic<T>
23324ae1
FM
36{
37public:
7c913512 38
23324ae1
FM
39};
40
41
e54c96f1 42
23324ae1 43/**
e25cd775
FM
44 wxWeakRef<T> is a template class for weak references to wxWidgets objects,
45 such as wxEvtHandler, wxWindow and wxObject.
46 A weak reference behaves much like an ordinary pointer, but when the object
47 pointed is destroyed, the weak reference is automatically reset to a @NULL pointer.
7c913512 48
73473b3e 49 wxWeakRef<T> can be used whenever one must keep a pointer to an object
23324ae1 50 that one does not directly own, and that may be destroyed before the object
7c913512
FM
51 holding the reference.
52
73473b3e 53 wxWeakRef<T> is a small object and the mechanism behind it is fast
7c913512
FM
54 (@b O(1)). So the overall cost of using it is small.
55
e25cd775
FM
56 Example:
57
e84fd6a1
RR
58 @code
59 wxWindow *wnd = new wxWindow( parent, wxID_ANY, "wxWindow" );
e25cd775 60 wxWeakRef<wxWindow> wr = wnd;
e84fd6a1
RR
61 wxWindowRef wr2 = wnd; // Same as above, but using a typedef
62 // Do things with window
63 wnd->Show( true );
e25cd775 64 // Weak ref is used like an ordinary pointer
e84fd6a1 65 wr->Show( false );
e25cd775 66 wnd->Destroy();
e84fd6a1
RR
67 // Now the weak ref has been reset, so we don't risk accessing
68 // a dangling pointer:
69 wxASSERT( wr==NULL );
70 @endcode
71
e25cd775
FM
72 wxWeakRef<T> works for any objects that are derived from wxTrackable.
73 By default, wxEvtHandler and wxWindow derive from wxTrackable.
74 However, wxObject does not, so types like wxFont and wxColour are not
75 trackable. The example below shows how to create a wxObject derived class
76 that is trackable:
e84fd6a1
RR
77
78 @code
79 class wxMyTrackableObject : public wxObject, public wxTrackable
e25cd775
FM
80 {
81 // ... other members here
82 };
e84fd6a1
RR
83 @endcode
84
85 The following types of weak references are predefined:
86
87 @code
88 typedef wxWeakRef<wxEvtHandler> wxEvtHandlerRef;
89 typedef wxWeakRef<wxWindow> wxWindowRef;
90 @endcode
91
6c107bd2 92 @tparam T
f045c7f5 93 The type to which the smart pointer points to.
6c107bd2 94
e25cd775 95 @nolibrary
d9a09273 96 @category{smartpointers}
7c913512 97
73473b3e 98 @see wxSharedPtr<T>, wxScopedPtr<T>
23324ae1 99*/
5cb947fd 100template<typename T>
e25cd775 101class wxWeakRef<T> : public wxTrackerNode
23324ae1
FM
102{
103public:
fc4e23d7
VZ
104 /// Type of the element stored by this reference.
105 typedef T element_type;
106
23324ae1
FM
107 /**
108 Constructor. The weak reference is initialized to @e pobj.
109 */
5cb947fd 110 wxWeakRef(T* pobj = NULL);
23324ae1 111
6dd21c54
RR
112 /**
113 Copy constructor.
114 */
115 wxWeakRef(const wxWeakRef<T>& wr);
e25cd775 116
23324ae1
FM
117 /**
118 Destructor.
119 */
fc4e23d7 120 virtual ~wxWeakRef();
23324ae1
FM
121
122 /**
123 Called when the tracked object is destroyed. Be default sets
e25cd775
FM
124 internal pointer to @NULL.
125 You need to call this method if you override it.
23324ae1
FM
126 */
127 virtual void OnObjectDestroy();
128
129 /**
130 Release currently tracked object and rests object reference.
131 */
132 void Release();
133
134 /**
135 Returns pointer to the tracked object or @NULL.
136 */
328f5751 137 T* get() const;
23324ae1
FM
138
139 /**
140 Release currently tracked object and start tracking the same object as
141 the wxWeakRef @e wr.
142 */
143 T* operator =(wxWeakRef<T>& wr);
144
145 /**
e25cd775
FM
146 Implicit conversion to T*.
147 Returns pointer to the tracked object or @NULL.
23324ae1 148 */
328f5751 149 T* operator*() const;
23324ae1
FM
150
151 /**
e25cd775
FM
152 Returns a reference to the tracked object.
153 If the internal pointer is @NULL this method will cause an assert in debug mode.
23324ae1 154 */
e25cd775 155 T& operator*() const;
23324ae1
FM
156
157 /**
37494cb3 158 Smart pointer member access. Returns a pointer to the tracked object.
e25cd775 159 If the internal pointer is @NULL this method will cause an assert in debug mode.
23324ae1 160 */
37494cb3 161 T* operator->();
23324ae1
FM
162
163 /**
164 Releases the currently tracked object and starts tracking @e pobj.
165 A weak reference may be reset by passing @e @NULL as @e pobj.
166 */
167 T* operator=(T* pobj);
168};
e54c96f1 169