supervisor_wx.h 3.5 KB

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