1 \section{\class{wxWeakRef<T>
}}\label{wxweakref
}
3 wxWeakRef is a template class for weak references to wxWidgets objects,
4 such as
\helpref{wxEvtHandler
}{wxevthandler
},
\helpref{wxWindow
}{wxwindow
} and
5 \helpref{wxObject
}{wxobject
}. A weak reference behaves much like an ordinary
6 pointer, but when the object pointed is destroyed, the weak reference is
7 automatically reset to a NULL pointer.
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.
13 wxWeakref<T> is a small object and the mechanism behind it is fast
14 (
\textbf{O(
1)
}). So the overall cost of using it is small.
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
26 // Weak ref is used like an ordinary pointer
29 // Now the weak ref has been reset, so we don't risk accessing
30 // a dangling pointer:
34 wxWeakref<T> works for any objects that are derived from
35 \helpref{wxTrackableBase
}{wxtrackablebase
} or
\helpref{wxTrackable
}{wxtrackable
}.
36 By default, wxEvtHandler and wxWindow derive from wxTrackableBase. However,
37 wxObject does not, so types like
\helpref{wxFont
}{wxfont
} and
38 \helpref{wxColour
}{wxcolour
} are not trackable. The example below shows how to
39 create a wxObject derived class that is trackable:
42 class wxMyTrackableObject : public wxObject, public wxTrackable
{
43 // ... other members here
47 \textbf{Note:
} Custom trackable objects should derive from wxTrackable
48 if one wants to reference them from a
\texttt{wxWeakRef<wxObject>
}. The
49 difference between the two base classes is that wxTrackableBase
50 has no virtual member functions (no VTable), and thus cannot be detected
51 through
\texttt{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
}
74 \helpref{wxSharedPtr
}{wxsharedptr
},
\helpref{wxScopedPtr
}{wxscopedptrtemplate
}
76 \wxheading{Data structures
}
80 typedef T element_type
85 \latexignore{\rtfignore{\wxheading{Members
}}}
88 \membersection{wxWeakRef<T>::wxWeakRef<T>
}\label{wxweakrefwxweakref
}
90 \func{}{wxWeakRef<T>
}{\param{T*
}{pobj = NULL
}}
92 Constructor. The weak reference is initialized to
{\it pobj
}.
95 \membersection{wxWeakRef<T>::
\destruct{wxWeakRef<T>
}}\label{wxweakrefdtor
}
97 \func{}{\destruct{wxWeakRef<T>
}}{\void}
102 \membersection{wxWeakRef<T>::get
}\label{wxweakrefget
}
104 \constfunc{T *
}{get
}{\void}
106 Returns pointer to the tracked object or NULL.
108 \membersection{wxWeakRef<T>::operator unspecified
\_bool\_type}\label{wxweakrefoperatorbool
}
110 \constfunc{}{operator unspecified
\_bool\_type}{\void}
112 Conversion to a boolean expression (in a variant which is not
113 convertable to anything but a boolean expression). If this class
114 contains a valid pointer it will return
{\it true
}, if it contains
115 a NULL pointer it will return
{\it false
}.
118 \membersection{wxWeakRef<T>::operator*
}\label{wxweakrefoperatorreft
}
120 \constfunc{T \&
}{operator*
}{\void}
122 Returns a reference to the tracked object. If the internal pointer is NULL
123 this method will cause an assert in debug mode.
126 \membersection{wxWeakRef<T>::operator->
}\label{wxweakrefoperatorderef
}
128 \func{T*
}{operator->
}{\void}
130 Smart pointer member access. Returns a pointer to the
131 tracked object. If the internal pointer is NULL this
132 method will cause an assert in debug mode.
135 \membersection{wxWeakRef<T>::operator=
}\label{wxweakrefoperatorassign
}
137 \func{T* operator
}{operator=
}{\param{T*
}{pobj
}}
139 Releases the currently tracked object and starts tracking
{\it pobj
}.
140 A weak reference may be reset by passing
{\it NULL
} as
{\it pobj
}.
143 \membersection{wxWeakRef<T>::operator =
}\label{wxweakrefoperatorassign2
}
145 \func{T* operator
}{operator =
}{\param{wxWeakRef<T>\&
}{wr
}}
147 Release currently tracked object and start tracking the same object as
148 the wxWeakRef
{\it wr
}.
151 \membersection{wxWeakRef<T>::OnObjectDestroy
}\label{wxweakrefonobjectdestroy
}
153 \func{virtual void
}{OnObjectDestroy
}{\void}
155 Called when the tracked object is destroyed. Be default sets
156 internal pointer to NULL.