subscription.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #pragma once
  2. //
  3. // Copyright (c) 2019 Ivan Baidakou (basiliscos) (the dot dmol at gmail dot com)
  4. //
  5. // Distributed under the MIT Software License
  6. //
  7. #include "handler.hpp"
  8. #include "message.h"
  9. #include <typeindex>
  10. #include <map>
  11. #include <vector>
  12. namespace rotor {
  13. /** \struct subscription_t
  14. * \brief Holds and classifies message handlers on behalf of supervisor
  15. *
  16. * The handlers are classified by message type and by the source supervisor, i.e.
  17. * whether the hander's supervisor is external or not.
  18. *
  19. */
  20. struct subscription_t {
  21. /** \struct classified_handlers_t
  22. * \brief Holds @{link handler_base_t} and flag wheher it belongs to the
  23. * source supervisor
  24. */
  25. struct classified_handlers_t {
  26. /** \brief intrusive pointer to the handler */
  27. handler_ptr_t handler;
  28. /** \brief true if the hanlder is local */
  29. bool mine;
  30. };
  31. /** \brief list of classified handlers */
  32. using list_t = std::vector<classified_handlers_t>;
  33. /** \brief alias for message type */
  34. using slot_t = const void *;
  35. /** \brief constructor which takes the source @{link supervisor_t} reference */
  36. subscription_t(supervisor_t &sup);
  37. /** \brief records the subscription for the handler */
  38. void subscribe(handler_ptr_t handler);
  39. /** \brief removes the recorded subscriptios and returns amount of left subscriptions */
  40. std::size_t unsubscribe(handler_ptr_t handler);
  41. /** \brief optioally returns classified list of subscribers to the message type */
  42. list_t *get_recipients(const slot_t &slot) noexcept;
  43. private:
  44. using map_t = std::map<slot_t, list_t>;
  45. supervisor_t &supervisor;
  46. map_t map;
  47. };
  48. } // namespace rotor