improvements to wxWeakRef and related classes
[wxWidgets.git] / include / wx / tracker.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/tracker.h
3 // Purpose: Support class for object lifetime tracking (wxWeakRef<T>)
4 // Author: Arne Steinarson
5 // Created: 28 Dec 07
6 // RCS-ID: $Id$
7 // Copyright: (c) 2007 Arne Steinarson
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_TRACKER_H_
12 #define _WX_TRACKER_H_
13
14 #include "wx/defs.h"
15
16 class wxEventConnectionRef;
17
18 // This class represents an object tracker and is stored in a linked list
19 // in the tracked object. It is only used in one of its derived forms.
20 class WXDLLIMPEXP_BASE wxTrackerNode
21 {
22 public:
23 wxTrackerNode() : m_nxt(NULL) { }
24 virtual ~wxTrackerNode() { }
25
26 virtual void OnObjectDestroy() = 0;
27
28 virtual wxEventConnectionRef *ToEventConnection() { return NULL; }
29
30 private:
31 wxTrackerNode *m_nxt;
32
33 friend class wxTrackable; // For list access
34 friend class wxEvtHandler; // For list access
35 };
36
37 // Add-on base class for a trackable object.
38 class wxTrackable
39 {
40 public:
41 wxTrackable() : m_first(NULL) { }
42
43 ~wxTrackable()
44 {
45 // Notify all registered refs
46 while ( m_first )
47 {
48 wxTrackerNode * const first = m_first;
49 m_first = first->m_nxt;
50 first->OnObjectDestroy();
51 }
52 }
53
54 void AddNode(wxTrackerNode *prn)
55 {
56 prn->m_nxt = m_first;
57 m_first = prn;
58 }
59
60 void RemoveNode(wxTrackerNode *prn)
61 {
62 for ( wxTrackerNode **pprn = &m_first; *pprn; pprn = &(*pprn)->m_nxt )
63 {
64 if ( *pprn == prn )
65 {
66 *pprn = prn->m_nxt;
67 return;
68 }
69 }
70
71 wxFAIL_MSG( "removing invalid tracker node" );
72 }
73
74 wxTrackerNode *GetFirst() const { return m_first; }
75
76 protected:
77 wxTrackerNode *m_first;
78
79 DECLARE_NO_COPY_CLASS(wxTrackable)
80 };
81
82 #endif // _WX_TRACKER_H_
83