factor out the fd<->handlers map from wxFDIODispatcher into a separate wxMappedFDIODispatcher class (patch 1729395)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@46274 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2007-06-02 13:51:05 +00:00
parent f463afe347
commit ad8d42f83c
6 changed files with 49 additions and 43 deletions

View File

@@ -39,6 +39,31 @@ enum wxFDIODispatcherEntryFlags
wxFDIO_ALL = wxFDIO_INPUT | wxFDIO_OUTPUT | wxFDIO_EXCEPTION wxFDIO_ALL = wxFDIO_INPUT | wxFDIO_OUTPUT | wxFDIO_EXCEPTION
}; };
// base class for wxSelectDispatcher and wxEpollDispatcher
class WXDLLIMPEXP_BASE wxFDIODispatcher
{
public:
enum { TIMEOUT_INFINITE = -1 };
// register handler for the given descriptor with the dispatcher, return
// true on success or false on error
virtual bool RegisterFD(int fd, wxFDIOHandler *handler, int flags) = 0;
// modify descriptor flags or handler, return true on success
virtual bool ModifyFD(int fd, wxFDIOHandler *handler, int flags) = 0;
// unregister descriptor previously registered with RegisterFD(), the
// caller is responsible for deleting the returned handler pointer if
// necessary
virtual bool UnregisterFD(int fd, int flags) = 0;
// loops waiting for an event to happen on any of the descriptors
virtual void RunLoop(int timeout) = 0;
virtual ~wxFDIODispatcher() { }
};
//entry for wxFDIOHandlerMap
struct wxFDIOHandlerEntry struct wxFDIOHandlerEntry
{ {
wxFDIOHandlerEntry() wxFDIOHandlerEntry()
@@ -64,34 +89,28 @@ WX_DECLARE_HASH_MAP(
wxFDIOHandlerMap wxFDIOHandlerMap
); );
// base class for wxSelectDispatcher and wxEpollDispatcher // FDIODispatcher that holds map fd <-> FDIOHandler, this should be used if
// this map isn't maintained elsewhere already as it is usually needed anyhow
// //
// notice that all pure virtual functions for FD management have implementation // notice that all functions for FD management have implementation
// in the base class and should be called from the derived classes // in the base class and should be called from the derived classes
class WXDLLIMPEXP_BASE wxFDIODispatcher class WXDLLIMPEXP_BASE wxMappedFDIODispatcher : public wxFDIODispatcher {
{
public: public:
enum { TIMEOUT_INFINITE = -1 };
// find the handler for the given fd, return NULL if none // find the handler for the given fd, return NULL if none
wxFDIOHandler *FindHandler(int fd) const; wxFDIOHandler *FindHandler(int fd) const;
// register handler for the given descriptor with the dispatcher, return // register handler for the given descriptor with the dispatcher, return
// true on success or false on error // true on success or false on error
virtual bool RegisterFD(int fd, wxFDIOHandler *handler, int flags) = 0; virtual bool RegisterFD(int fd, wxFDIOHandler *handler, int flags);
// modify descriptor flags or handler, return true on success // modify descriptor flags or handler, return true on success
virtual bool ModifyFD(int fd, wxFDIOHandler *handler, int flags) = 0; virtual bool ModifyFD(int fd, wxFDIOHandler *handler, int flags);
// unregister descriptor previously registered with RegisterFD(), the // unregister descriptor previously registered with RegisterFD(), the
// caller is responsible for deleting the returned handler pointer if // caller is responsible for deleting the returned handler pointer if
// necessary // necessary
virtual wxFDIOHandler *UnregisterFD(int fd, int flags) = 0; virtual bool UnregisterFD(int fd, int flags);
// loops waiting for an event to happen on any of the descriptors virtual ~wxMappedFDIODispatcher() { }
virtual void RunLoop(int timeout) = 0;
virtual ~wxFDIODispatcher() { }
protected: protected:
// the fd -> handler map containing all the registered handlers // the fd -> handler map containing all the registered handlers

View File

@@ -71,7 +71,7 @@ private:
static Callback ms_handlers[Max]; static Callback ms_handlers[Max];
}; };
class WXDLLIMPEXP_BASE wxSelectDispatcher : public wxFDIODispatcher class WXDLLIMPEXP_BASE wxSelectDispatcher : public wxMappedFDIODispatcher
{ {
public: public:
// returns the unique instance of this class, the pointer shouldn't be // returns the unique instance of this class, the pointer shouldn't be
@@ -86,7 +86,7 @@ public:
// implement pure virtual methods of the base class // implement pure virtual methods of the base class
virtual bool RegisterFD(int fd, wxFDIOHandler *handler, int flags = wxFDIO_ALL); virtual bool RegisterFD(int fd, wxFDIOHandler *handler, int flags = wxFDIO_ALL);
virtual bool ModifyFD(int fd, wxFDIOHandler *handler, int flags = wxFDIO_ALL); virtual bool ModifyFD(int fd, wxFDIOHandler *handler, int flags = wxFDIO_ALL);
virtual wxFDIOHandler *UnregisterFD(int fd, int flags = wxFDIO_ALL); virtual bool UnregisterFD(int fd, int flags = wxFDIO_ALL);
virtual void RunLoop(int timeout = TIMEOUT_INFINITE); virtual void RunLoop(int timeout = TIMEOUT_INFINITE);
protected: protected:

View File

@@ -29,7 +29,7 @@ public:
// implement base class pure virtual methods // implement base class pure virtual methods
virtual bool RegisterFD(int fd, wxFDIOHandler* handler, int flags = wxFDIO_ALL); virtual bool RegisterFD(int fd, wxFDIOHandler* handler, int flags = wxFDIO_ALL);
virtual bool ModifyFD(int fd, wxFDIOHandler* handler, int flags = wxFDIO_ALL); virtual bool ModifyFD(int fd, wxFDIOHandler* handler, int flags = wxFDIO_ALL);
virtual wxFDIOHandler *UnregisterFD(int fd, int flags = wxFDIO_ALL); virtual bool UnregisterFD(int fd, int flags = wxFDIO_ALL);
virtual void RunLoop(int timeout = TIMEOUT_INFINITE); virtual void RunLoop(int timeout = TIMEOUT_INFINITE);
private: private:

View File

@@ -32,7 +32,7 @@
// implementation // implementation
// ============================================================================ // ============================================================================
wxFDIOHandler *wxFDIODispatcher::FindHandler(int fd) const wxFDIOHandler *wxMappedFDIODispatcher::FindHandler(int fd) const
{ {
const wxFDIOHandlerMap::const_iterator it = m_handlers.find(fd); const wxFDIOHandlerMap::const_iterator it = m_handlers.find(fd);
@@ -40,7 +40,7 @@ wxFDIOHandler *wxFDIODispatcher::FindHandler(int fd) const
} }
bool wxFDIODispatcher::RegisterFD(int fd, wxFDIOHandler *handler, int flags) bool wxMappedFDIODispatcher::RegisterFD(int fd, wxFDIOHandler *handler, int flags)
{ {
wxUnusedVar(flags); wxUnusedVar(flags);
@@ -63,7 +63,7 @@ bool wxFDIODispatcher::RegisterFD(int fd, wxFDIOHandler *handler, int flags)
return true; return true;
} }
bool wxFDIODispatcher::ModifyFD(int fd, wxFDIOHandler *handler, int flags) bool wxMappedFDIODispatcher::ModifyFD(int fd, wxFDIOHandler *handler, int flags)
{ {
wxUnusedVar(flags); wxUnusedVar(flags);
@@ -78,13 +78,12 @@ bool wxFDIODispatcher::ModifyFD(int fd, wxFDIOHandler *handler, int flags)
return true; return true;
} }
wxFDIOHandler *wxFDIODispatcher::UnregisterFD(int fd, int flags) bool wxMappedFDIODispatcher::UnregisterFD(int fd, int flags)
{ {
wxFDIOHandlerMap::iterator i = m_handlers.find(fd); wxFDIOHandlerMap::iterator i = m_handlers.find(fd);
wxCHECK_MSG( i != m_handlers.end(), NULL, if( i == m_handlers.end())
_T("unregistering unregistered handler?") ); return false;
wxFDIOHandler * const handler = i->second.handler;
i->second.flags &= ~flags; i->second.flags &= ~flags;
if ( !i->second.flags ) if ( !i->second.flags )
{ {
@@ -92,6 +91,6 @@ wxFDIOHandler *wxFDIODispatcher::UnregisterFD(int fd, int flags)
m_handlers.erase(i); m_handlers.erase(i);
} }
return handler; return true;
} }

View File

@@ -160,7 +160,7 @@ wxSelectDispatcher::wxSelectDispatcher()
bool wxSelectDispatcher::RegisterFD(int fd, wxFDIOHandler *handler, int flags) bool wxSelectDispatcher::RegisterFD(int fd, wxFDIOHandler *handler, int flags)
{ {
if ( !wxFDIODispatcher::RegisterFD(fd, handler, flags) ) if ( !wxMappedFDIODispatcher::RegisterFD(fd, handler, flags) )
return false; return false;
if ( !m_sets.SetFD(fd, flags) ) if ( !m_sets.SetFD(fd, flags) )
@@ -174,7 +174,7 @@ bool wxSelectDispatcher::RegisterFD(int fd, wxFDIOHandler *handler, int flags)
bool wxSelectDispatcher::ModifyFD(int fd, wxFDIOHandler *handler, int flags) bool wxSelectDispatcher::ModifyFD(int fd, wxFDIOHandler *handler, int flags)
{ {
if ( !wxFDIODispatcher::ModifyFD(fd, handler, flags) ) if ( !wxMappedFDIODispatcher::ModifyFD(fd, handler, flags) )
return false; return false;
wxASSERT_MSG( fd <= m_maxFD, _T("logic error: registered fd > m_maxFD?") ); wxASSERT_MSG( fd <= m_maxFD, _T("logic error: registered fd > m_maxFD?") );
@@ -182,10 +182,8 @@ bool wxSelectDispatcher::ModifyFD(int fd, wxFDIOHandler *handler, int flags)
return m_sets.SetFD(fd, flags); return m_sets.SetFD(fd, flags);
} }
wxFDIOHandler *wxSelectDispatcher::UnregisterFD(int fd, int flags) bool wxSelectDispatcher::UnregisterFD(int fd, int flags)
{ {
wxFDIOHandler * const handler = wxFDIODispatcher::UnregisterFD(fd, flags);
m_sets.ClearFD(fd, flags); m_sets.ClearFD(fd, flags);
// remove the handler if we don't need it any more // remove the handler if we don't need it any more
@@ -205,7 +203,7 @@ wxFDIOHandler *wxSelectDispatcher::UnregisterFD(int fd, int flags)
} }
} }
return handler; return true;
} }
void wxSelectDispatcher::ProcessSets(const wxSelectSets& sets) void wxSelectDispatcher::ProcessSets(const wxSelectSets& sets)

View File

@@ -86,9 +86,6 @@ wxEpollDispatcher::wxEpollDispatcher()
bool wxEpollDispatcher::RegisterFD(int fd, wxFDIOHandler* handler, int flags) bool wxEpollDispatcher::RegisterFD(int fd, wxFDIOHandler* handler, int flags)
{ {
if ( !wxFDIODispatcher::RegisterFD(fd, handler, flags) )
return false;
epoll_event ev; epoll_event ev;
ev.events = GetEpollMask(flags, fd); ev.events = GetEpollMask(flags, fd);
ev.data.ptr = handler; ev.data.ptr = handler;
@@ -107,9 +104,6 @@ bool wxEpollDispatcher::RegisterFD(int fd, wxFDIOHandler* handler, int flags)
bool wxEpollDispatcher::ModifyFD(int fd, wxFDIOHandler* handler, int flags) bool wxEpollDispatcher::ModifyFD(int fd, wxFDIOHandler* handler, int flags)
{ {
if ( !wxFDIODispatcher::ModifyFD(fd, handler, flags) )
return false;
epoll_event ev; epoll_event ev;
ev.events = GetEpollMask(flags, fd); ev.events = GetEpollMask(flags, fd);
ev.data.ptr = handler; ev.data.ptr = handler;
@@ -126,12 +120,8 @@ bool wxEpollDispatcher::ModifyFD(int fd, wxFDIOHandler* handler, int flags)
return true; return true;
} }
wxFDIOHandler *wxEpollDispatcher::UnregisterFD(int fd, int flags) bool wxEpollDispatcher::UnregisterFD(int fd, int flags)
{ {
wxFDIOHandler * const handler = wxFDIODispatcher::UnregisterFD(fd, flags);
if ( !handler )
return NULL;
epoll_event ev; epoll_event ev;
ev.events = 0; ev.events = 0;
ev.data.ptr = NULL; ev.data.ptr = NULL;
@@ -142,7 +132,7 @@ wxFDIOHandler *wxEpollDispatcher::UnregisterFD(int fd, int flags)
fd, m_epollDescriptor); fd, m_epollDescriptor);
} }
return handler; return true;
} }
void wxEpollDispatcher::RunLoop(int timeout) void wxEpollDispatcher::RunLoop(int timeout)