/src/wxwidgets/include/wx/private/fdiodispatcher.h
Line | Count | Source |
1 | | ///////////////////////////////////////////////////////////////////////////// |
2 | | // Name: wx/private/fdiodispatcher.h |
3 | | // Purpose: classes for dispatching IO notifications for file descriptors |
4 | | // Authors: Lukasz Michalski |
5 | | // Created: December 2006 |
6 | | // Copyright: (c) Lukasz Michalski |
7 | | // Licence: wxWindows licence |
8 | | ///////////////////////////////////////////////////////////////////////////// |
9 | | |
10 | | #ifndef _WX_PRIVATE_FDIODISPATCHER_H_ |
11 | | #define _WX_PRIVATE_FDIODISPATCHER_H_ |
12 | | |
13 | | #include "wx/private/fdiohandler.h" |
14 | | |
15 | | #include <unordered_map> |
16 | | |
17 | | // those flags describes sets where descriptor should be added |
18 | | enum wxFDIODispatcherEntryFlags |
19 | | { |
20 | | wxFDIO_INPUT = 1, |
21 | | wxFDIO_OUTPUT = 2, |
22 | | wxFDIO_EXCEPTION = 4, |
23 | | wxFDIO_ALL = wxFDIO_INPUT | wxFDIO_OUTPUT | wxFDIO_EXCEPTION |
24 | | }; |
25 | | |
26 | | // base class for wxSelectDispatcher and wxEpollDispatcher |
27 | | class WXDLLIMPEXP_BASE wxFDIODispatcher |
28 | | { |
29 | | public: |
30 | | enum { TIMEOUT_INFINITE = -1 }; |
31 | | |
32 | | // return the global dispatcher to be used for IO events, can be null only |
33 | | // if wxSelectDispatcher wasn't compiled into the library at all as |
34 | | // creating it never fails |
35 | | // |
36 | | // don't delete the returned pointer |
37 | | static wxFDIODispatcher *Get(); |
38 | | |
39 | | // if we have any registered handlers, check for any pending events to them |
40 | | // and dispatch them -- this is used from wxX11 and wxDFB event loops |
41 | | // implementation |
42 | | static void DispatchPending(); |
43 | | |
44 | | // register handler for the given descriptor with the dispatcher, return |
45 | | // true on success or false on error |
46 | | virtual bool RegisterFD(int fd, wxFDIOHandler *handler, int flags) = 0; |
47 | | |
48 | | // modify descriptor flags or handler, return true on success |
49 | | virtual bool ModifyFD(int fd, wxFDIOHandler *handler, int flags) = 0; |
50 | | |
51 | | // unregister descriptor previously registered with RegisterFD() |
52 | | virtual bool UnregisterFD(int fd) = 0; |
53 | | |
54 | | // check if any events are currently available without dispatching them |
55 | | virtual bool HasPending() const = 0; |
56 | | |
57 | | // wait for an event for at most timeout milliseconds and process it; |
58 | | // return the number of events processed (possibly 0 if timeout expired) or |
59 | | // -1 if an error occurred |
60 | | virtual int Dispatch(int timeout = TIMEOUT_INFINITE) = 0; |
61 | | |
62 | 0 | virtual ~wxFDIODispatcher() = default; |
63 | | }; |
64 | | |
65 | | //entry for wxFDIOHandlerMap |
66 | | struct wxFDIOHandlerEntry |
67 | | { |
68 | | wxFDIOHandlerEntry() |
69 | 0 | { |
70 | 0 | handler = nullptr; |
71 | 0 | flags = 0; |
72 | 0 | } |
73 | | |
74 | | wxFDIOHandlerEntry(wxFDIOHandler *handler_, int flags_) |
75 | 0 | : handler(handler_), |
76 | 0 | flags(flags_) |
77 | 0 | { |
78 | 0 | } |
79 | | |
80 | | wxFDIOHandler *handler; |
81 | | int flags; |
82 | | }; |
83 | | |
84 | | // this hash is used to map file descriptors to their handlers |
85 | | using wxFDIOHandlerMap = std::unordered_map<int, wxFDIOHandlerEntry>; |
86 | | |
87 | | // FDIODispatcher that holds map fd <-> FDIOHandler, this should be used if |
88 | | // this map isn't maintained elsewhere already as it is usually needed anyhow |
89 | | // |
90 | | // notice that all functions for FD management have implementation |
91 | | // in the base class and should be called from the derived classes |
92 | | class WXDLLIMPEXP_BASE wxMappedFDIODispatcher : public wxFDIODispatcher |
93 | | { |
94 | | public: |
95 | | // find the handler for the given fd, return nullptr if none |
96 | | wxFDIOHandler *FindHandler(int fd) const; |
97 | | |
98 | | // register handler for the given descriptor with the dispatcher, return |
99 | | // true on success or false on error |
100 | | virtual bool RegisterFD(int fd, wxFDIOHandler *handler, int flags) override; |
101 | | |
102 | | // modify descriptor flags or handler, return true on success |
103 | | virtual bool ModifyFD(int fd, wxFDIOHandler *handler, int flags) override; |
104 | | |
105 | | // unregister descriptor previously registered with RegisterFD() |
106 | | virtual bool UnregisterFD(int fd) override; |
107 | | |
108 | 0 | virtual ~wxMappedFDIODispatcher() = default; |
109 | | |
110 | | protected: |
111 | | // the fd -> handler map containing all the registered handlers |
112 | | wxFDIOHandlerMap m_handlers; |
113 | | }; |
114 | | |
115 | | #endif // _WX_PRIVATE_FDIODISPATCHER_H_ |