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