fdiodispatcher.h 3.7 KB

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