]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/weakref.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxWeakRefDynamic<T>, wxWeakRef<T>
4 // Author: wxWidgets team
5 // Licence: wxWindows licence
6 /////////////////////////////////////////////////////////////////////////////
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
12 defaults to using @c dynamic_cast for establishing the object reference
13 (while wxWeakRef defaults to @c static_cast).
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
17 is to handle objects which derived type one does not know.
19 @note wxWeakRef<T> selects an implementation based on the static type of T.
20 If T does not have wxTrackable statically, it defaults to a mixed-
21 mode operation, where it uses @c dynamic_cast as the last measure
22 (if available from the compiler and enabled when building wxWidgets).
24 For general cases, wxWeakRef<T> is the better choice.
26 For API documentation, see: wxWeakRef<T>.
29 The type to which the smart pointer points to.
32 @category{smartpointers}
35 class wxWeakRefDynamic
<T
>
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.
49 wxWeakRef<T> can be used whenever one must keep a pointer to an object
50 that one does not directly own, and that may be destroyed before the object
51 holding the reference.
53 wxWeakRef<T> is a small object and the mechanism behind it is fast
54 (@b O(1)). So the overall cost of using it is small.
59 wxWindow *wnd = new wxWindow( parent, wxID_ANY, "wxWindow" );
60 wxWeakRef<wxWindow> wr = wnd;
61 wxWindowRef wr2 = wnd; // Same as above, but using a typedef
62 // Do things with window
64 // Weak ref is used like an ordinary pointer
67 // Now the weak ref has been reset, so we don't risk accessing
68 // a dangling pointer:
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
79 class wxMyTrackableObject : public wxObject, public wxTrackable
81 // ... other members here
85 The following types of weak references are predefined:
88 typedef wxWeakRef<wxEvtHandler> wxEvtHandlerRef;
89 typedef wxWeakRef<wxWindow> wxWindowRef;
93 The type to which the smart pointer points to.
96 @category{smartpointers}
98 @see wxSharedPtr<T>, wxScopedPtr<T>
101 class wxWeakRef
<T
> : public wxTrackerNode
104 /// Type of the element stored by this reference.
105 typedef T element_type
;
108 Constructor. The weak reference is initialized to @e pobj.
110 wxWeakRef(T
* pobj
= NULL
);
115 wxWeakRef(const wxWeakRef
<T
>& wr
);
120 virtual ~wxWeakRef();
123 Called when the tracked object is destroyed. Be default sets
124 internal pointer to @NULL.
125 You need to call this method if you override it.
127 virtual void OnObjectDestroy();
130 Release currently tracked object and rests object reference.
135 Returns pointer to the tracked object or @NULL.
140 Release currently tracked object and start tracking the same object as
143 T
* operator =(wxWeakRef
<T
>& wr
);
146 Implicit conversion to T*.
147 Returns pointer to the tracked object or @NULL.
149 T
* operator*() const;
152 Returns a reference to the tracked object.
153 If the internal pointer is @NULL this method will cause an assert in debug mode.
155 T
& operator*() const;
158 Smart pointer member access. Returns a pointer to the tracked object.
159 If the internal pointer is @NULL this method will cause an assert in debug mode.
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.
167 T
* operator=(T
* pobj
);