]>
Commit | Line | Data |
---|---|---|
db7035e4 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/tracker.h | |
3 | // Purpose: Support class for object lifetime tracking (wxWeakRef<T>) | |
4 | // Author: Arne Steinarson | |
cc6ceca7 | 5 | // Created: 28 Dec 07 |
db7035e4 VZ |
6 | // Copyright: (c) 2007 Arne Steinarson |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #ifndef _WX_TRACKER_H_ | |
11 | #define _WX_TRACKER_H_ | |
12 | ||
cc6ceca7 | 13 | #include "wx/defs.h" |
7d23dd28 | 14 | |
cc6ceca7 VZ |
15 | class wxEventConnectionRef; |
16 | ||
17 | // This class represents an object tracker and is stored in a linked list | |
db7035e4 | 18 | // in the tracked object. It is only used in one of its derived forms. |
cc6ceca7 VZ |
19 | class WXDLLIMPEXP_BASE wxTrackerNode |
20 | { | |
21 | public: | |
22 | wxTrackerNode() : m_nxt(NULL) { } | |
db7035e4 | 23 | virtual ~wxTrackerNode() { } |
cc6ceca7 VZ |
24 | |
25 | virtual void OnObjectDestroy() = 0; | |
26 | ||
27 | virtual wxEventConnectionRef *ToEventConnection() { return NULL; } | |
28 | ||
29 | private: | |
7d23dd28 | 30 | wxTrackerNode *m_nxt; |
cc6ceca7 VZ |
31 | |
32 | friend class wxTrackable; // For list access | |
db7035e4 VZ |
33 | friend class wxEvtHandler; // For list access |
34 | }; | |
35 | ||
db7035e4 | 36 | // Add-on base class for a trackable object. |
ccc998cc | 37 | class WXDLLIMPEXP_BASE wxTrackable |
cc6ceca7 VZ |
38 | { |
39 | public: | |
cc6ceca7 | 40 | void AddNode(wxTrackerNode *prn) |
db7035e4 | 41 | { |
7d23dd28 RR |
42 | prn->m_nxt = m_first; |
43 | m_first = prn; | |
db7035e4 | 44 | } |
cc6ceca7 VZ |
45 | |
46 | void RemoveNode(wxTrackerNode *prn) | |
db7035e4 | 47 | { |
cc6ceca7 | 48 | for ( wxTrackerNode **pprn = &m_first; *pprn; pprn = &(*pprn)->m_nxt ) |
db7035e4 | 49 | { |
cc6ceca7 | 50 | if ( *pprn == prn ) |
db7035e4 | 51 | { |
7d23dd28 | 52 | *pprn = prn->m_nxt; |
db7035e4 VZ |
53 | return; |
54 | } | |
55 | } | |
db7035e4 | 56 | |
cc6ceca7 VZ |
57 | wxFAIL_MSG( "removing invalid tracker node" ); |
58 | } | |
db7035e4 | 59 | |
cc6ceca7 | 60 | wxTrackerNode *GetFirst() const { return m_first; } |
7d23dd28 | 61 | |
cc6ceca7 | 62 | protected: |
dd10e614 VZ |
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 | |
db7035e4 | 65 | |
dd10e614 VZ |
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; | |
db7035e4 VZ |
88 | }; |
89 | ||
db7035e4 VZ |
90 | #endif // _WX_TRACKER_H_ |
91 |