supervisor_wx.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #pragma once
  2. //
  3. // Copyright (c) 2019-2022 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
  4. //
  5. // Distributed under the MIT Software License
  6. //
  7. #include "rotor/wx/export.h"
  8. #include "rotor/supervisor.h"
  9. #include "rotor/wx/supervisor_config_wx.h"
  10. #include "rotor/wx/system_context_wx.h"
  11. #include <wx/event.h>
  12. #include <wx/timer.h>
  13. #include <memory>
  14. #include <unordered_map>
  15. namespace rotor {
  16. namespace wx {
  17. /** \struct supervisor_wx_t
  18. * \brief delivers rotor-messages on top of wx event
  19. *
  20. * Basically wx event handler is used as transport for wx-rotor-events,
  21. * which wrap rotor-messages.
  22. *
  23. * Since wx-event loop is mainly GUI-loop, i.e. singleton, there are no
  24. * different execution contexts, i.e. only one (main) executing thread;
  25. * hence, there is no sense of having multiple supervisors.
  26. *
  27. * The wx-supervisor (and it's actors) role in `rotor` messaging is
  28. * to **abstract** destination endpoints from other non-wx (I/O) event
  29. * loops, i.e. show some information in corresponding field/window/frame/...
  30. * Hence, the receiving rotor-messages actors should be aware of your
  31. * wx-application system, i.e. hold refences to appropriate fields/windows
  32. * etc.
  33. *
  34. * The commands translator (i.e. from click on the button to the `rotor`
  35. * -message send) should be also performed on wx-specific actors.
  36. *
  37. */
  38. struct ROTOR_WX_API supervisor_wx_t : public supervisor_t {
  39. /** \brief injects an alias for supervisor_config_wx_t */
  40. using config_t = supervisor_config_wx_t;
  41. /** \brief injects templated supervisor_config_wx_builder_t */
  42. template <typename Supervisor> using config_builder_t = supervisor_config_wx_builder_t<Supervisor>;
  43. /** \brief constructs new supervisor from supervisor config */
  44. supervisor_wx_t(supervisor_config_wx_t &config);
  45. void start() noexcept override;
  46. void shutdown() noexcept override;
  47. void enqueue(message_ptr_t message) noexcept override;
  48. // void on_timer_trigger(request_id_t timer_id) noexcept override;
  49. /** \brief returns pointer to the wx system context */
  50. inline system_context_wx_t *get_context() noexcept { return static_cast<system_context_wx_t *>(context); }
  51. protected:
  52. /** \struct timer_t
  53. * \brief timer structure, adoped for wx-supervisor needs.
  54. */
  55. struct ROTOR_WX_API timer_t : public wxTimer {
  56. /** \brief alias for intrusive pointer for the supervisor */
  57. using supervisor_ptr_t = intrusive_ptr_t<supervisor_wx_t>;
  58. /** \brief non-owning pointer to timer handler */
  59. timer_handler_base_t *handler;
  60. /** \brief intrusive pointer to the supervisor */
  61. supervisor_ptr_t sup;
  62. /** \brief constructs timer from wx supervisor */
  63. timer_t(timer_handler_base_t *handler, supervisor_ptr_t &&sup_);
  64. /** \brief invokes `shutdown_timer_trigger` method if shutdown timer triggers*/
  65. virtual void Notify() noexcept override;
  66. };
  67. friend struct timer_t;
  68. void do_start_timer(const pt::time_duration &interval, timer_handler_base_t &handler) noexcept override;
  69. void do_cancel_timer(request_id_t timer_id) noexcept override;
  70. /** \brief unique pointer to timer */
  71. using timer_ptr_t = std::unique_ptr<timer_t>;
  72. /** \brief timer id to timer pointer mapping type */
  73. using timers_map_t = std::unordered_map<request_id_t, timer_ptr_t>;
  74. /** \brief non-owning pointer to the wx application (copied from config) */
  75. wxEvtHandler *handler;
  76. /** \brief timer id to timer pointer mapping */
  77. timers_map_t timers_map;
  78. };
  79. } // namespace wx
  80. } // namespace rotor