]>
Commit | Line | Data |
---|---|---|
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 | // Copyright: (c) 2007 Arne Steinarson | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #ifndef _WX_TRACKER_H_ | |
11 | #define _WX_TRACKER_H_ | |
12 | ||
13 | #include "wx/defs.h" | |
14 | ||
15 | class wxEventConnectionRef; | |
16 | ||
17 | // This class represents an object tracker and is stored in a linked list | |
18 | // in the tracked object. It is only used in one of its derived forms. | |
19 | class WXDLLIMPEXP_BASE wxTrackerNode | |
20 | { | |
21 | public: | |
22 | wxTrackerNode() : m_nxt(NULL) { } | |
23 | virtual ~wxTrackerNode() { } | |
24 | ||
25 | virtual void OnObjectDestroy() = 0; | |
26 | ||
27 | virtual wxEventConnectionRef *ToEventConnection() { return NULL; } | |
28 | ||
29 | private: | |
30 | wxTrackerNode *m_nxt; | |
31 | ||
32 | friend class wxTrackable; // For list access | |
33 | friend class wxEvtHandler; // For list access | |
34 | }; | |
35 | ||
36 | // Add-on base class for a trackable object. | |
37 | class WXDLLIMPEXP_BASE wxTrackable | |
38 | { | |
39 | public: | |
40 | void AddNode(wxTrackerNode *prn) | |
41 | { | |
42 | prn->m_nxt = m_first; | |
43 | m_first = prn; | |
44 | } | |
45 | ||
46 | void RemoveNode(wxTrackerNode *prn) | |
47 | { | |
48 | for ( wxTrackerNode **pprn = &m_first; *pprn; pprn = &(*pprn)->m_nxt ) | |
49 | { | |
50 | if ( *pprn == prn ) | |
51 | { | |
52 | *pprn = prn->m_nxt; | |
53 | return; | |
54 | } | |
55 | } | |
56 | ||
57 | wxFAIL_MSG( "removing invalid tracker node" ); | |
58 | } | |
59 | ||
60 | wxTrackerNode *GetFirst() const { return m_first; } | |
61 | ||
62 | protected: | |
63 | // this class is only supposed to be used as a base class but never be | |
64 | // created nor destroyed directly so all ctors and dtor are protected | |
65 | ||
66 | wxTrackable() : m_first(NULL) { } | |
67 | ||
68 | // copy ctor and assignment operator intentionally do not copy m_first: the | |
69 | // objects which track the original trackable shouldn't track the new copy | |
70 | wxTrackable(const wxTrackable& WXUNUSED(other)) : m_first(NULL) { } | |
71 | wxTrackable& operator=(const wxTrackable& WXUNUSED(other)) { return *this; } | |
72 | ||
73 | // dtor is not virtual: this class is not supposed to be used | |
74 | // polymorphically and adding a virtual table to it would add unwanted | |
75 | // overhead | |
76 | ~wxTrackable() | |
77 | { | |
78 | // Notify all registered refs | |
79 | while ( m_first ) | |
80 | { | |
81 | wxTrackerNode * const first = m_first; | |
82 | m_first = first->m_nxt; | |
83 | first->OnObjectDestroy(); | |
84 | } | |
85 | } | |
86 | ||
87 | wxTrackerNode *m_first; | |
88 | }; | |
89 | ||
90 | #endif // _WX_TRACKER_H_ | |
91 |