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