]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/tracker.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Support class for object lifetime tracking (wxWeakRef<T>)
4 // Author: Arne Steinarson
7 // Copyright: (c) 2007 Arne Steinarson
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_TRACKER_H_
12 #define _WX_TRACKER_H_
14 // This structure represents an object tracker and is stored in a linked list
15 // in the tracked object. It is only used in one of its derived forms.
18 wxTrackerNode() : m_next(NULL
) { }
19 virtual ~wxTrackerNode() { }
21 virtual void OnObjectDestroy() = 0;
23 // This is to tell the difference between different tracker node types.
24 // It's a replacement of dynamic_cast<> for this class since dynamic_cast
25 // may be disabled (and we don't have wxDynamicCast since wxTrackerNode
26 // is not derived wxObject).
27 enum wxTrackerNodeType
{ WeakRef
, EventConnectionRef
};
29 virtual wxTrackerNodeType
GetType() = 0;
32 wxTrackerNode
*m_next
;
34 friend class wxTrackableBase
; // For list access
35 friend class wxEvtHandler
; // For list access
39 // Add-on base class for a trackable object.
40 struct wxTrackableBase
42 wxTrackableBase() : m_first(NULL
) { }
46 // Notify all registered refs
48 // OnObjectDestroy has to remove the item from the link chain
51 wxTrackerNode
*first
= m_first
;
52 first
->OnObjectDestroy();
57 void AddNode(wxTrackerNode
*node
)
59 node
->m_next
= m_first
;
63 void RemoveNode(wxTrackerNode
*node
)
65 for ( wxTrackerNode
**pn
= &m_first
; *pn
; pn
= &(*pn
)->m_next
)
74 wxFAIL_MSG( "node should be present" );
77 wxTrackerNode
*GetFirst() { return m_first
; }
80 wxTrackerNode
*m_first
;
83 // The difference to wxTrackableBase is that this class adds
84 // a VTable to enable dynamic_cast query for wxTrackable.
85 struct wxTrackable
: wxTrackableBase
87 virtual ~wxTrackable() { }
91 // Helper to decide if an object has a base class or not
92 // (strictly speaking, this test succeeds if a type is convertible
93 // to another type in some way.)
94 template <class T
, class B
>
97 static char Match(B
*pb
);
98 static int Match(...);
100 enum { value
= sizeof(Match((T
*)NULL
)) == sizeof(char) };
103 // A structure to cast to wxTrackableBase, using either static_cast<> or
105 template <class T
, bool is_static
>
106 struct wxTrackableCaster
;
109 struct wxTrackableCaster
<T
, true>
111 static wxTrackableBase
* Cast(T
* pt
)
113 return static_cast<wxTrackableBase
*>(pt
);
117 #ifdef HAVE_DYNAMIC_CAST
120 struct wxTrackableCaster
<T
, false>
122 static wxTrackableBase
*Cast(T
* pt
)
124 return dynamic_cast<wxTrackableBase
*>(pt
);
128 #else // !HAVE_DYNAMIC_CAST
131 struct wxTrackableCaster
<T
, false>
133 static wxTrackableBase
*Cast(T
* pt
) { return NULL
; }
136 #endif // HAVE_DYNAMIC_CAST/!HAVE_DYNAMIC_CAST
138 #endif // _WX_TRACKER_H_