supervisor_fltk.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #pragma once
  2. //
  3. // Copyright (c) 2019-2024 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/fltk/export.h"
  9. #include "rotor/fltk/supervisor_config_fltk.h"
  10. #include <memory>
  11. #include <unordered_map>
  12. namespace rotor {
  13. namespace fltk {
  14. /** \struct supervisor_fltk_t
  15. * \brief delivers rotor-messages on top of fltk async callback
  16. *
  17. * The fltk-supervisor (and it's actors) role in `rotor` messaging is
  18. * to **abstract** destination endpoints from other non-fltk (I/O) event
  19. * loops, i.e. show some information in corresponding field/window/frame/...
  20. * Hence, the receiving rotor-messages actors should be aware of your
  21. * fltk-application system, i.e. hold refences to appropriate fields/windows
  22. * /widgets etc.
  23. *
  24. * Since conceptually, fltk-event loop is main GUI-loop, i.e. singleton,
  25. * there are no different execution contexts, i.e. only one (main)
  26. * executing thread; hence, there is no sense of having multiple supervisors.
  27. *
  28. * The commands translator (i.e. from click on the button to the `rotor`
  29. * -message send) should be also performed fltk-widgets which hold corresponding
  30. * fltk- actor/supervisor.
  31. *
  32. */
  33. struct ROTOR_FLTK_API supervisor_fltk_t : public supervisor_t {
  34. /** \brief injects an alias for supervisor_config_fltk_t */
  35. using config_t = supervisor_config_fltk_t;
  36. /** \brief injects templated supervisor_config_fltk_builder_t */
  37. template <typename Supervisor> using config_builder_t = supervisor_config_fltk_builder_t<Supervisor>;
  38. /** \brief constructs new supervisor from supervisor config */
  39. supervisor_fltk_t(supervisor_config_fltk_t &config);
  40. void start() noexcept override;
  41. void shutdown() noexcept override;
  42. void enqueue(message_ptr_t message) noexcept override;
  43. /** \brief generic non-public fields accessor */
  44. template <typename T> auto &access() noexcept;
  45. /** \struct timer_t
  46. * \brief timer structure, adopted for fltk-supervisor needs.
  47. */
  48. struct timer_t {
  49. /** \brief alias for intrusive pointer for the fltk supervisor */
  50. using supervisor_ptr_t = intrusive_ptr_t<supervisor_fltk_t>;
  51. /** \brief constructs timer from flt supervisor and timer handler */
  52. timer_t(supervisor_ptr_t supervisor, timer_handler_base_t *handler);
  53. /** \brief intrusive pointer to the supervisor */
  54. supervisor_ptr_t sup;
  55. /** \brief non-owning pointer to timer handler */
  56. timer_handler_base_t *handler;
  57. };
  58. protected:
  59. /** \brief unique pointer to timer */
  60. using timer_ptr_t = std::unique_ptr<timer_t>;
  61. /** \brief timer id to timer pointer mapping type */
  62. using timers_map_t = std::unordered_map<request_id_t, timer_ptr_t>;
  63. void do_start_timer(const pt::time_duration &interval, timer_handler_base_t &handler) noexcept override;
  64. void do_cancel_timer(request_id_t timer_id) noexcept override;
  65. /** \brief timer id to timer pointer mapping */
  66. timers_map_t timers_map;
  67. };
  68. } // namespace fltk
  69. } // namespace rotor