]>
Commit | Line | Data |
---|---|---|
a6acecec RR |
1 | \section{\class{wxWeakRef<T>}}\label{wxweakref} |
2 | ||
9dd5ff5b RR |
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 | to goes out of scope (is destroyed), the weak reference is automatically | |
7 | reset to a NULL pointer. | |
8 | ||
9 | wxWeakref<T> can be used whenever one must keep a pointer to an object | |
10 | that does not directly own, and that may be destroyed before the object | |
11 | holding the reference. | |
12 | ||
13 | wxWeakref<T> is a small object and the mechanism behind it is fast | |
14 | ({\bf O(1)}). So the overall cost of using it is small. | |
15 | ||
16 | ||
17 | \wxheading{Example} | |
18 | ||
19 | \begin{verbatim} | |
20 | wxWindow *parent = /* Get parent window from somewhere */; | |
21 | wxWindow *wnd = new wxWindow( parent, wxID_ANY, "wxWindow" ); | |
22 | wxWeakRef<wxWindow> wr = wnd; | |
23 | wxWindowRef wr2 = wnd; // Same as above, but using a typedef | |
24 | // Do things with window | |
25 | wnd->Show( true ); | |
26 | // Weak ref is used like an ordinary pointer | |
27 | wr->Show( false ); | |
28 | wnd->Destroy(); | |
29 | // Now the weak ref has been reset, so we don't risk accessing | |
30 | // a dangling pointer: | |
31 | wxASSERT( wr==NULL ); | |
32 | \end{verbatim} | |
33 | ||
34 | wxWeakref<T> works for any objects that are derived from {\bf wxTrackableBase} | |
35 | or {\bf wxTrackable}. By default, wxEvtHandler and wxWindow derive from | |
36 | wxTrackableBase. However, 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: | |
a6acecec RR |
39 | |
40 | \begin{verbatim} | |
9dd5ff5b RR |
41 | class wxMyTrackableObject : public wxObject, public wxTrackable { |
42 | // ... other members here | |
43 | }; | |
44 | \end{verbatim} | |
45 | ||
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<>}. | |
51 | ||
52 | ||
53 | \wxheading{Predefined types} | |
a6acecec | 54 | |
9dd5ff5b RR |
55 | The following types of weak references are predefined: |
56 | ||
57 | \begin{verbatim} | |
58 | typedef wxWeakRef<wxObject> wxObjectRef; | |
59 | typedef wxWeakRef<wxEvtHandler> wxEvtHandlerRef; | |
60 | typedef wxWeakRef<wxWindow> wxWindowRef; | |
a6acecec RR |
61 | \end{verbatim} |
62 | ||
9dd5ff5b | 63 | |
a6acecec RR |
64 | \wxheading{Derived from} |
65 | ||
66 | wxTrackerNode | |
67 | ||
68 | \wxheading{Include files} | |
69 | ||
70 | <weakref.h> | |
71 | ||
72 | \wxheading{Data structures} | |
73 | ||
74 | \latexignore{\rtfignore{\wxheading{Members}}} | |
75 | ||
76 | ||
77 | \membersection{wxWeakRef<T>::wxWeakRef<T>}\label{wxweakrefwxweakref} | |
78 | ||
79 | \func{}{wxWeakRef<T>}{\param{T* }{pobj = NULL}} | |
80 | ||
9dd5ff5b RR |
81 | Constructor. The weak reference is initialized to {\it pobj}. |
82 | ||
a6acecec RR |
83 | |
84 | \membersection{wxWeakRef<T>::\destruct{wxWeakRef<T>}}\label{wxweakrefdtor} | |
85 | ||
86 | \func{}{\destruct{wxWeakRef<T>}}{\void} | |
87 | ||
88 | Destructor. | |
89 | ||
a6acecec | 90 | |
9dd5ff5b RR |
91 | \membersection{wxWeakRef<T>::get}\label{wxweakrefget} |
92 | ||
93 | \constfunc{T *}{get}{\void} | |
94 | ||
95 | Returns pointer to the tracked object or NULL. | |
96 | ||
97 | ||
98 | \membersection{wxWeakRef<T>::operator*}\label{wxweakrefoperatorreft} | |
99 | ||
100 | \constfunc{T \&}{operator*}{\void} | |
101 | ||
102 | Returns a reference to the tracked object. If the internal pointer is NULL | |
103 | this method will cause an assert in debug mode. | |
a6acecec | 104 | |
a6acecec RR |
105 | |
106 | \membersection{wxWeakRef<T>::operator->}\label{wxweakrefoperatorderef} | |
107 | ||
108 | \func{T*}{operator->}{\void} | |
109 | ||
9dd5ff5b RR |
110 | Smart pointer member access. Returns a pointer to the |
111 | tracked object. If the internal pointer is NULL this | |
112 | method will cause an assert in debug mode. | |
113 | ||
a6acecec RR |
114 | |
115 | \membersection{wxWeakRef<T>::operator=}\label{wxweakrefoperatorassign} | |
116 | ||
117 | \func{T* operator}{operator=}{\param{T* }{pobj}} | |
118 | ||
9dd5ff5b RR |
119 | Releases the currently tracked object and starts tracking {\it pobj}. |
120 | A weak reference may be reset by passing {\it NULL} as {\it pobj}. | |
a6acecec | 121 | |
a6acecec | 122 | |
9dd5ff5b | 123 | \membersection{wxWeakRef<T>::operator =}\label{wxweakrefoperatorassign2} |
a6acecec | 124 | |
9dd5ff5b | 125 | \func{T* operator}{operator =}{\param{wxWeakRef<T>\& }{wr}} |
a6acecec | 126 | |
9dd5ff5b RR |
127 | Release currently tracked object and start tracking the same object as |
128 | the wxWeakRef {\it wr}. | |
a6acecec | 129 | |
a6acecec RR |
130 | |
131 | \membersection{wxWeakRef<T>::OnObjectDestroy}\label{wxweakrefonobjectdestroy} | |
132 | ||
133 | \func{virtual void}{OnObjectDestroy}{\void} | |
134 | ||
135 | Called when the tracked object is destroyed. Be default sets | |
136 | internal pointer to NULL. | |
137 |