/src/wxwidgets/include/wx/tracker.h
Line | Count | Source (jump to first uncovered line) |
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 | 0 | wxTrackerNode() : m_nxt(nullptr) { } |
23 | 0 | virtual ~wxTrackerNode() = default; |
24 | | |
25 | | virtual void OnObjectDestroy() = 0; |
26 | | |
27 | 0 | virtual wxEventConnectionRef *ToEventConnection() { return nullptr; } |
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 | 0 | { |
42 | 0 | prn->m_nxt = m_first; |
43 | 0 | m_first = prn; |
44 | 0 | } |
45 | | |
46 | | void RemoveNode(wxTrackerNode *prn) |
47 | 0 | { |
48 | 0 | for ( wxTrackerNode **pprn = &m_first; *pprn; pprn = &(*pprn)->m_nxt ) |
49 | 0 | { |
50 | 0 | if ( *pprn == prn ) |
51 | 0 | { |
52 | 0 | *pprn = prn->m_nxt; |
53 | 0 | return; |
54 | 0 | } |
55 | 0 | } |
56 | | |
57 | 0 | wxFAIL_MSG( "removing invalid tracker node" ); |
58 | 0 | } |
59 | | |
60 | 0 | 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 | 0 | wxTrackable() : m_first(nullptr) { } |
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 | 0 | wxTrackable(const wxTrackable& WXUNUSED(other)) : m_first(nullptr) { } |
71 | 0 | 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 | 0 | { |
78 | | // Notify all registered refs |
79 | 0 | while ( m_first ) |
80 | 0 | { |
81 | 0 | wxTrackerNode * const first = m_first; |
82 | 0 | m_first = first->m_nxt; |
83 | 0 | first->OnObjectDestroy(); |
84 | 0 | } |
85 | 0 | } |
86 | | |
87 | | wxTrackerNode *m_first; |
88 | | }; |
89 | | |
90 | | #endif // _WX_TRACKER_H_ |
91 | | |