]> git.saurik.com Git - wxWidgets.git/blame - interface/wx/weakref.h
Restore wxFile docs
[wxWidgets.git] / interface / wx / weakref.h
CommitLineData
23324ae1
FM
1/////////////////////////////////////////////////////////////////////////////
2// Name: weakref.h
e54c96f1 3// Purpose: interface of wxWeakRefDynamic<T>
23324ae1
FM
4// Author: wxWidgets team
5// RCS-ID: $Id$
6// Licence: wxWindows license
7/////////////////////////////////////////////////////////////////////////////
8
9/**
7c913512 10
73473b3e
RR
11 wxWeakRefDynamic<T> is a template class for weak references that is used in
12 the same way as wxWeakRef<T>. The only difference is that wxWeakRefDynamic
7c913512
FM
13 defaults to using @c dynamic_cast for establishing the object
14 reference (while wxWeakRef defaults to @c static_cast).
15
23324ae1
FM
16 So, wxWeakRef will detect a type mismatch during compile time and will
17 have a little better run-time performance. The role of wxWeakRefDynamic
7c913512
FM
18 is to handle objects which derived type one does not know.
19
73473b3e 20 @note wxWeakRef<T> selects an implementation based on the static type
23324ae1 21 of T. If T does not have wxTrackable statically, it defaults to to a mixed-
7c913512
FM
22 mode operation, where it uses @c dynamic_cast as the last measure (if
23 available from the compiler and enabled when building wxWidgets).
24
73473b3e 25 For general cases, wxWeakRef<T> is the better choice.
7c913512 26
73473b3e 27 For API documentation, see: wxWeakRef<T>
7c913512 28
23324ae1
FM
29 @library{wxcore}
30 @category{FIXME}
31*/
5cb947fd 32template<typename T>
7c913512 33class wxWeakRefDynamic<T>
23324ae1
FM
34{
35public:
7c913512 36
23324ae1
FM
37};
38
39
e54c96f1 40
23324ae1 41/**
7c913512
FM
42
43 wxWeakRef is a template class for weak references to wxWidgets objects,
44 such as wxEvtHandler, wxWindow and
23324ae1
FM
45 wxObject. A weak reference behaves much like an ordinary
46 pointer, but when the object pointed is destroyed, the weak reference is
7c913512
FM
47 automatically reset to a @NULL pointer.
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
e84fd6a1
RR
56 Example
57
58 @code
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
63 wnd->Show( true );
64 // Weak ref is used like an ordinary pointer
65 wr->Show( false );
66 wnd->Destroy();
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
72 wxWeakRef<T> works for any objects that are derived from wxTrackable. By default,
73 wxEvtHandler and wxWindow derive from wxTrackable. However, wxObject does not, so
74 types like wxFont and wxColour are not trackable. The example below shows how to
75 create a wxObject derived class that is trackable:
76
77 @code
78 class wxMyTrackableObject : public wxObject, public wxTrackable
79 {
80 // ... other members here
81 };
82 @endcode
83
84 The following types of weak references are predefined:
85
86 @code
87 typedef wxWeakRef<wxEvtHandler> wxEvtHandlerRef;
88 typedef wxWeakRef<wxWindow> wxWindowRef;
89 @endcode
90
91
23324ae1
FM
92 @library{wxbase}
93 @category{FIXME}
7c913512 94
73473b3e 95 @see wxSharedPtr<T>, wxScopedPtr<T>
23324ae1 96*/
5cb947fd 97template<typename T>
7c913512 98class wxWeakRef<T>
23324ae1
FM
99{
100public:
101 /**
102 Constructor. The weak reference is initialized to @e pobj.
103 */
5cb947fd 104 wxWeakRef(T* pobj = NULL);
23324ae1 105
6dd21c54
RR
106 /**
107 Copy constructor.
108 */
109 wxWeakRef(const wxWeakRef<T>& wr);
110
23324ae1
FM
111 /**
112 Destructor.
113 */
5cb947fd 114 ~wxWeakRef();
23324ae1
FM
115
116 /**
117 Called when the tracked object is destroyed. Be default sets
14bdedbc
RR
118 internal pointer to @NULL. You need to call this method if
119 you override it.
23324ae1
FM
120 */
121 virtual void OnObjectDestroy();
122
123 /**
124 Release currently tracked object and rests object reference.
125 */
126 void Release();
127
128 /**
129 Returns pointer to the tracked object or @NULL.
130 */
328f5751 131 T* get() const;
23324ae1
FM
132
133 /**
134 Release currently tracked object and start tracking the same object as
135 the wxWeakRef @e wr.
136 */
137 T* operator =(wxWeakRef<T>& wr);
138
139 /**
140 Implicit conversion to T*. Returns pointer to the tracked
141 object or @NULL.
142 */
328f5751 143 T* operator*() const;
23324ae1
FM
144
145 /**
146 Returns a reference to the tracked object. If the internal pointer is @NULL
147 this method will cause an assert in debug mode.
148 */
328f5751 149 T operator*() const;
23324ae1
FM
150
151 /**
152 Smart pointer member access. Returns a pointer to the
153 tracked object. If the internal pointer is @NULL this
154 method will cause an assert in debug mode.
155 */
156 T* operator-();
157
158 /**
159 Releases the currently tracked object and starts tracking @e pobj.
160 A weak reference may be reset by passing @e @NULL as @e pobj.
161 */
162 T* operator=(T* pobj);
163};
e54c96f1 164