tracker.h 2.5 KB

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