]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/fdiodispatcher.cpp | |
3 | // Purpose: Implementation of common wxFDIODispatcher methods | |
4 | // Author: Vadim Zeitlin | |
5 | // Created: 2007-05-13 | |
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 | |
26 | #include "wx/module.h" | |
27 | #endif //WX_PRECOMP | |
28 | ||
29 | #include "wx/private/fdiodispatcher.h" | |
30 | ||
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 | ||
38 | // ============================================================================ | |
39 | // implementation | |
40 | // ============================================================================ | |
41 | ||
42 | // ---------------------------------------------------------------------------- | |
43 | // wxFDIODispatcher | |
44 | // ---------------------------------------------------------------------------- | |
45 | ||
46 | /* static */ | |
47 | wxFDIODispatcher *wxFDIODispatcher::Get() | |
48 | { | |
49 | if ( !gs_dispatcher ) | |
50 | { | |
51 | #if wxUSE_EPOLL_DISPATCHER | |
52 | gs_dispatcher = wxEpollDispatcher::Create(); | |
53 | if ( !gs_dispatcher ) | |
54 | #endif // wxUSE_EPOLL_DISPATCHER | |
55 | #if wxUSE_SELECT_DISPATCHER | |
56 | gs_dispatcher = new wxSelectDispatcher(); | |
57 | #endif // wxUSE_SELECT_DISPATCHER | |
58 | } | |
59 | ||
60 | wxASSERT_MSG( gs_dispatcher, "failed to create any IO dispatchers" ); | |
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 | ||
76 | wxFDIOHandler *wxMappedFDIODispatcher::FindHandler(int fd) const | |
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 | ||
84 | bool | |
85 | wxMappedFDIODispatcher::RegisterFD(int fd, wxFDIOHandler *handler, int flags) | |
86 | { | |
87 | wxCHECK_MSG( handler, false, "handler can't be NULL" ); | |
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, | |
96 | "registering different handler for the same fd?" ); | |
97 | wxASSERT_MSG( i->second.flags != flags, | |
98 | "reregistering with the same flags?" ); | |
99 | } | |
100 | ||
101 | m_handlers[fd] = wxFDIOHandlerEntry(handler, flags); | |
102 | ||
103 | return true; | |
104 | } | |
105 | ||
106 | bool | |
107 | wxMappedFDIODispatcher::ModifyFD(int fd, wxFDIOHandler *handler, int flags) | |
108 | { | |
109 | wxCHECK_MSG( handler, false, "handler can't be NULL" ); | |
110 | ||
111 | wxFDIOHandlerMap::iterator i = m_handlers.find(fd); | |
112 | wxCHECK_MSG( i != m_handlers.end(), false, | |
113 | "modifying unregistered handler?" ); | |
114 | ||
115 | i->second = wxFDIOHandlerEntry(handler, flags); | |
116 | ||
117 | return true; | |
118 | } | |
119 | ||
120 | bool wxMappedFDIODispatcher::UnregisterFD(int fd) | |
121 | { | |
122 | wxFDIOHandlerMap::iterator i = m_handlers.find(fd); | |
123 | if ( i == m_handlers.end() ) | |
124 | return false; | |
125 | ||
126 | m_handlers.erase(i); | |
127 | ||
128 | return true; | |
129 | } | |
130 | ||
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) |