2008-01-06 13:01:28 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: wx/tracker.h
|
|
|
|
// Purpose: Support class for object lifetime tracking (wxWeakRef<T>)
|
|
|
|
// Author: Arne Steinarson
|
2008-01-12 20:12:13 -05:00
|
|
|
// Created: 28 Dec 07
|
2008-01-08 04:58:44 -05:00
|
|
|
// RCS-ID: $Id$
|
2008-01-06 13:01:28 -05:00
|
|
|
// Copyright: (c) 2007 Arne Steinarson
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef _WX_TRACKER_H_
|
|
|
|
#define _WX_TRACKER_H_
|
|
|
|
|
2008-01-12 20:12:13 -05:00
|
|
|
#include "wx/defs.h"
|
2008-01-08 04:58:44 -05:00
|
|
|
|
2008-01-12 20:12:13 -05:00
|
|
|
class wxEventConnectionRef;
|
|
|
|
|
|
|
|
// This class represents an object tracker and is stored in a linked list
|
2008-01-06 13:01:28 -05:00
|
|
|
// in the tracked object. It is only used in one of its derived forms.
|
2008-01-12 20:12:13 -05:00
|
|
|
class WXDLLIMPEXP_BASE wxTrackerNode
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
wxTrackerNode() : m_nxt(NULL) { }
|
2008-01-06 13:01:28 -05:00
|
|
|
virtual ~wxTrackerNode() { }
|
2008-01-12 20:12:13 -05:00
|
|
|
|
|
|
|
virtual void OnObjectDestroy() = 0;
|
|
|
|
|
|
|
|
virtual wxEventConnectionRef *ToEventConnection() { return NULL; }
|
|
|
|
|
|
|
|
private:
|
2008-01-08 04:58:44 -05:00
|
|
|
wxTrackerNode *m_nxt;
|
2008-01-12 20:12:13 -05:00
|
|
|
|
|
|
|
friend class wxTrackable; // For list access
|
2008-01-06 13:01:28 -05:00
|
|
|
friend class wxEvtHandler; // For list access
|
|
|
|
};
|
|
|
|
|
|
|
|
// Add-on base class for a trackable object.
|
2008-01-29 12:41:10 -05:00
|
|
|
class WXDLLIMPEXP_BASE wxTrackable
|
2008-01-12 20:12:13 -05:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
void AddNode(wxTrackerNode *prn)
|
2008-01-06 13:01:28 -05:00
|
|
|
{
|
2008-01-08 04:58:44 -05:00
|
|
|
prn->m_nxt = m_first;
|
|
|
|
m_first = prn;
|
2008-01-06 13:01:28 -05:00
|
|
|
}
|
2008-01-12 20:12:13 -05:00
|
|
|
|
|
|
|
void RemoveNode(wxTrackerNode *prn)
|
2008-01-06 13:01:28 -05:00
|
|
|
{
|
2008-01-12 20:12:13 -05:00
|
|
|
for ( wxTrackerNode **pprn = &m_first; *pprn; pprn = &(*pprn)->m_nxt )
|
2008-01-06 13:01:28 -05:00
|
|
|
{
|
2008-01-12 20:12:13 -05:00
|
|
|
if ( *pprn == prn )
|
2008-01-06 13:01:28 -05:00
|
|
|
{
|
2008-01-08 04:58:44 -05:00
|
|
|
*pprn = prn->m_nxt;
|
2008-01-06 13:01:28 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-12 20:12:13 -05:00
|
|
|
wxFAIL_MSG( "removing invalid tracker node" );
|
|
|
|
}
|
2008-01-06 13:01:28 -05:00
|
|
|
|
2008-01-12 20:12:13 -05:00
|
|
|
wxTrackerNode *GetFirst() const { return m_first; }
|
2008-01-08 04:58:44 -05:00
|
|
|
|
2008-01-12 20:12:13 -05:00
|
|
|
protected:
|
2008-01-15 21:04:00 -05:00
|
|
|
// this class is only supposed to be used as a base class but never be
|
|
|
|
// created nor destroyed directly so all ctors and dtor are protected
|
2008-01-06 13:01:28 -05:00
|
|
|
|
2008-01-15 21:04:00 -05:00
|
|
|
wxTrackable() : m_first(NULL) { }
|
|
|
|
|
|
|
|
// copy ctor and assignment operator intentionally do not copy m_first: the
|
|
|
|
// objects which track the original trackable shouldn't track the new copy
|
|
|
|
wxTrackable(const wxTrackable& WXUNUSED(other)) : m_first(NULL) { }
|
|
|
|
wxTrackable& operator=(const wxTrackable& WXUNUSED(other)) { return *this; }
|
|
|
|
|
|
|
|
// dtor is not virtual: this class is not supposed to be used
|
|
|
|
// polymorphically and adding a virtual table to it would add unwanted
|
|
|
|
// overhead
|
|
|
|
~wxTrackable()
|
|
|
|
{
|
|
|
|
// Notify all registered refs
|
|
|
|
while ( m_first )
|
|
|
|
{
|
|
|
|
wxTrackerNode * const first = m_first;
|
|
|
|
m_first = first->m_nxt;
|
|
|
|
first->OnObjectDestroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wxTrackerNode *m_first;
|
2008-01-06 13:01:28 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // _WX_TRACKER_H_
|
|
|
|
|