1 \section{\class{wxWeakRef<T>
}}\label{wxweakref
}
3 {\bf wxWeakRef
} is a template class for weak references to wxWidgets objects,
4 such as
{\bf wxEvtHandler
},
{\bf wxWindow
} and
{\bf wxObject
}. A weak
5 reference behaves much like an ordinary pointer, but when the object pointed
6 is destroyed, the weak reference is automatically reset to a NULL pointer.
8 wxWeakref<T> can be used whenever one must keep a pointer to an object
9 that does not directly own, and that may be destroyed before the object
10 holding the reference.
12 wxWeakref<T> is a small object and the mechanism behind it is fast
13 (
{\bf O(
1)
}). So the overall cost of using it is small.
19 wxWindow *parent = /* Get parent window from somewhere */;
20 wxWindow *wnd = new wxWindow( parent, wxID_ANY, "wxWindow" );
21 wxWeakRef<wxWindow> wr = wnd;
22 wxWindowRef wr2 = wnd; // Same as above, but using a typedef
23 // Do things with window
25 // Weak ref is used like an ordinary pointer
28 // Now the weak ref has been reset, so we don't risk accessing
29 // a dangling pointer:
33 wxWeakref<T> works for any objects that are derived from
34 \helpref{wxTrackableBase
}{wxtrackablebase
} or
\helpref{wxTrackable
}{wxtrackable
}.
35 By default, wxEvtHandler and wxWindow derive from wxTrackableBase. However,
36 wxObject does not, so types like
{\bf wxFont
} and
37 {\bf wxColour
} are not trackable. The example below shows how to create a
38 wxObject derived class that is trackable:
41 class wxMyTrackableObject : public wxObject, public wxTrackable
{
42 // ... other members here
46 {\bf Note:
} Custom trackable objects should derive from wxTrackable
47 if one wants to reference them from a
{\bf wxWeakRef<wxObject>
}. The
48 difference between the two base classes is that wxTrackableBase
49 has no virtual member functions (no VTable), and thus cannot be detected
50 through
{\bf dynamic_cast<>
}.
53 \wxheading{Predefined types
}
55 The following types of weak references are predefined:
58 typedef wxWeakRef<wxObject> wxObjectRef;
59 typedef wxWeakRef<wxEvtHandler> wxEvtHandlerRef;
60 typedef wxWeakRef<wxWindow> wxWindowRef;
64 \wxheading{Derived from
}
68 \wxheading{Include files
}
72 \wxheading{Data structures
}
76 typedef T element_type
81 \latexignore{\rtfignore{\wxheading{Members
}}}
84 \membersection{wxWeakRef<T>::wxWeakRef<T>
}\label{wxweakrefwxweakref
}
86 \func{}{wxWeakRef<T>
}{\param{T*
}{pobj = NULL
}}
88 Constructor. The weak reference is initialized to
{\it pobj
}.
91 \membersection{wxWeakRef<T>::
\destruct{wxWeakRef<T>
}}\label{wxweakrefdtor
}
93 \func{}{\destruct{wxWeakRef<T>
}}{\void}
98 \membersection{wxWeakRef<T>::get
}\label{wxweakrefget
}
100 \constfunc{T *
}{get
}{\void}
102 Returns pointer to the tracked object or NULL.
105 \membersection{wxWeakRef<T>::operator*
}\label{wxweakrefoperatorreft
}
107 \constfunc{T \&
}{operator*
}{\void}
109 Returns a reference to the tracked object. If the internal pointer is NULL
110 this method will cause an assert in debug mode.
113 \membersection{wxWeakRef<T>::operator->
}\label{wxweakrefoperatorderef
}
115 \func{T*
}{operator->
}{\void}
117 Smart pointer member access. Returns a pointer to the
118 tracked object. If the internal pointer is NULL this
119 method will cause an assert in debug mode.
122 \membersection{wxWeakRef<T>::operator=
}\label{wxweakrefoperatorassign
}
124 \func{T* operator
}{operator=
}{\param{T*
}{pobj
}}
126 Releases the currently tracked object and starts tracking
{\it pobj
}.
127 A weak reference may be reset by passing
{\it NULL
} as
{\it pobj
}.
130 \membersection{wxWeakRef<T>::operator =
}\label{wxweakrefoperatorassign2
}
132 \func{T* operator
}{operator =
}{\param{wxWeakRef<T>\&
}{wr
}}
134 Release currently tracked object and start tracking the same object as
135 the wxWeakRef
{\it wr
}.
138 \membersection{wxWeakRef<T>::OnObjectDestroy
}\label{wxweakrefonobjectdestroy
}
140 \func{virtual void
}{OnObjectDestroy
}{\void}
142 Called when the tracked object is destroyed. Be default sets
143 internal pointer to NULL.