]> git.saurik.com Git - wxWidgets.git/blob - include/wx/tracker.h
avoiding crashes on osx during app shutdown
[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 WXDLLIMPEXP_BASE wxTrackable
39 {
40 public:
41 void AddNode(wxTrackerNode *prn)
42 {
43 prn->m_nxt = m_first;
44 m_first = prn;
45 }
46
47 void RemoveNode(wxTrackerNode *prn)
48 {
49 for ( wxTrackerNode **pprn = &m_first; *pprn; pprn = &(*pprn)->m_nxt )
50 {
51 if ( *pprn == prn )
52 {
53 *pprn = prn->m_nxt;
54 return;
55 }
56 }
57
58 wxFAIL_MSG( "removing invalid tracker node" );
59 }
60
61 wxTrackerNode *GetFirst() const { return m_first; }
62
63 protected:
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
66
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;
89 };
90
91 #endif // _WX_TRACKER_H_
92