]> git.saurik.com Git - wxWidgets.git/blob - src/common/fdiodispatcher.cpp
607519b365e4833cf35971cb52eb988134924ad1
[wxWidgets.git] / src / common / fdiodispatcher.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/fdiodispatcher.cpp
3 // Purpose: Implementation of common wxFDIODispatcher methods
4 // Author: Vadim Zeitlin
5 // Created: 2007-05-13
6 // RCS-ID: $Id$
7 // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #ifndef WX_PRECOMP
27 #include "wx/module.h"
28 #endif //WX_PRECOMP
29
30 #include "wx/private/fdiodispatcher.h"
31
32 #include "wx/private/selectdispatcher.h"
33 #ifdef __UNIX__
34 #include "wx/unix/private/epolldispatcher.h"
35 #endif
36
37 wxFDIODispatcher *gs_dispatcher = NULL;
38
39 // ============================================================================
40 // implementation
41 // ============================================================================
42
43 // ----------------------------------------------------------------------------
44 // wxFDIODispatcher
45 // ----------------------------------------------------------------------------
46
47 /* static */
48 wxFDIODispatcher *wxFDIODispatcher::Get()
49 {
50 if ( !gs_dispatcher )
51 {
52 #if wxUSE_EPOLL_DISPATCHER
53 gs_dispatcher = wxEpollDispatcher::Create();
54 if ( !gs_dispatcher )
55 #endif // wxUSE_EPOLL_DISPATCHER
56 #if wxUSE_SELECT_DISPATCHER
57 gs_dispatcher = new wxSelectDispatcher();
58 #endif // wxUSE_SELECT_DISPATCHER
59 }
60
61 wxASSERT_MSG( gs_dispatcher, "failed to create any IO dispatchers" );
62
63 return gs_dispatcher;
64 }
65
66 /* static */
67 void wxFDIODispatcher::DispatchPending()
68 {
69 if ( gs_dispatcher )
70 gs_dispatcher->Dispatch(0);
71 }
72
73 // ----------------------------------------------------------------------------
74 // wxMappedFDIODispatcher
75 // ----------------------------------------------------------------------------
76
77 wxFDIOHandler *wxMappedFDIODispatcher::FindHandler(int fd) const
78 {
79 const wxFDIOHandlerMap::const_iterator it = m_handlers.find(fd);
80
81 return it == m_handlers.end() ? NULL : it->second.handler;
82 }
83
84
85 bool
86 wxMappedFDIODispatcher::RegisterFD(int fd, wxFDIOHandler *handler, int flags)
87 {
88 wxCHECK_MSG( handler, false, "handler can't be NULL" );
89
90 // notice that it's not an error to register a handler for the same fd
91 // twice as it can be done with different flags -- but it is an error to
92 // register different handlers
93 wxFDIOHandlerMap::iterator i = m_handlers.find(fd);
94 if ( i != m_handlers.end() )
95 {
96 wxASSERT_MSG( i->second.handler == handler,
97 "registering different handler for the same fd?" );
98 wxASSERT_MSG( i->second.flags != flags,
99 "reregistering with the same flags?" );
100 }
101
102 m_handlers[fd] = wxFDIOHandlerEntry(handler, flags);
103
104 return true;
105 }
106
107 bool
108 wxMappedFDIODispatcher::ModifyFD(int fd, wxFDIOHandler *handler, int flags)
109 {
110 wxCHECK_MSG( handler, false, "handler can't be NULL" );
111
112 wxFDIOHandlerMap::iterator i = m_handlers.find(fd);
113 wxCHECK_MSG( i != m_handlers.end(), false,
114 "modifying unregistered handler?" );
115
116 i->second = wxFDIOHandlerEntry(handler, flags);
117
118 return true;
119 }
120
121 bool wxMappedFDIODispatcher::UnregisterFD(int fd)
122 {
123 wxFDIOHandlerMap::iterator i = m_handlers.find(fd);
124 if ( i == m_handlers.end() )
125 return false;
126
127 m_handlers.erase(i);
128
129 return true;
130 }
131
132 // ----------------------------------------------------------------------------
133 // wxSelectDispatcherModule
134 // ----------------------------------------------------------------------------
135
136 class wxFDIODispatcherModule : public wxModule
137 {
138 public:
139 virtual bool OnInit() { return true; }
140 virtual void OnExit() { wxDELETE(gs_dispatcher); }
141
142 private:
143 DECLARE_DYNAMIC_CLASS(wxFDIODispatcherModule)
144 };
145
146 IMPLEMENT_DYNAMIC_CLASS(wxFDIODispatcherModule, wxModule)