subscription_point.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/forward.hpp"
  8. #include "rotor/address.hpp"
  9. #include <vector>
  10. #include <list>
  11. namespace rotor {
  12. struct actor_base_t;
  13. /** \brief who owns the subscription point
  14. *
  15. * - plugin (i.e. subscription was invoked from plugin)
  16. *
  17. * - supervisor (temporally instantiated subscription for request/response handling)
  18. *
  19. * - foreign (when it is subscribed for foreign address)
  20. *
  21. * - anonymous (subscription point was created from actor)
  22. *
  23. */
  24. enum class owner_tag_t { PLUGIN, SUPERVISOR, FOREIGN, ANONYMOUS, NOT_AVAILABLE };
  25. /** \struct subscription_point_t
  26. * \brief pair of {@link handler_base_t} linked to particular {@link address_t}
  27. */
  28. struct subscription_point_t {
  29. /** \brief intrusive pointer to messages' handler */
  30. handler_ptr_t handler;
  31. /** \brief intrusive pointer to address */
  32. address_ptr_t address;
  33. /** \brief non-owning pointer to an actor, which performs subscription */
  34. const actor_base_t *owner_ptr;
  35. /** \brief kind of ownership of the subscription point */
  36. owner_tag_t owner_tag;
  37. /** \brief ctor from handler and related address (used for comparison) */
  38. subscription_point_t(const handler_ptr_t &handler_, const address_ptr_t &address_) noexcept
  39. : handler{handler_}, address{address_}, owner_ptr{nullptr}, owner_tag{owner_tag_t::NOT_AVAILABLE} {}
  40. /** \brief full ctor (handler, address, actor, and owner tag) */
  41. subscription_point_t(const handler_ptr_t &handler_, const address_ptr_t &address_, const actor_base_t *owner_ptr_,
  42. owner_tag_t owner_tag_) noexcept
  43. : handler{handler_}, address{address_}, owner_ptr{owner_ptr_}, owner_tag{owner_tag_} {}
  44. /** \brief copy-ctor */
  45. subscription_point_t(const subscription_point_t &) = default;
  46. /** \brief move-ctor */
  47. subscription_point_t(subscription_point_t &&) = default;
  48. /** \brief patrial comparison by handler and address */
  49. bool operator==(const subscription_point_t &other) const noexcept;
  50. };
  51. /** \struct subscription_info_t
  52. * \brief {@link subscription_point_t} with extended information (e.g. state)
  53. */
  54. struct subscription_info_t : public arc_base_t<subscription_info_t>, subscription_point_t {
  55. /** \brief subscription info state (subscribing, established, unsubscribing */
  56. enum state_t { SUBSCRIBING, ESTABLISHED, UNSUBSCRIBING };
  57. /** \brief ctor from subscription point, internal address and internal handler and state */
  58. subscription_info_t(const subscription_point_t &point, bool internal_address_, bool internal_handler_,
  59. state_t state_) noexcept
  60. : subscription_point_t{point}, internal_address{internal_address_},
  61. internal_handler{internal_handler_}, state{state_} {}
  62. ~subscription_info_t();
  63. /** \brief uses {@link subscription_point_t} comparison */
  64. inline bool operator==(const subscription_point_t &point) const noexcept {
  65. return (subscription_point_t &)(*this) == point;
  66. }
  67. /** \brief whether the subscription point (info) belongs to internal address, i.e. to the "my" supervisor */
  68. bool internal_address;
  69. /** \brief whether the subscription point (info) has internal handler, i.e. will be processed by "my" supervisor */
  70. bool internal_handler;
  71. /** \brief subscription state */
  72. state_t state;
  73. };
  74. /** \brief intrusive pointer for {@link subscription_info_t} */
  75. using subscription_info_ptr_t = intrusive_ptr_t<subscription_info_t>;
  76. /** \struct subscription_container_t
  77. * \brief list of {@link subscription_info_ptr_t} with possibility to find via {@link subscription_point_t}
  78. */
  79. struct subscription_container_t : public std::list<subscription_info_ptr_t> {
  80. /** \brief looks up for the subscription info pointer (returned as iterator) via the subscription point */
  81. iterator find(const subscription_point_t &point) noexcept;
  82. };
  83. } // namespace rotor