address.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 "arc.hpp"
  8. namespace rotor {
  9. struct actor_base_t;
  10. struct supervisor_t;
  11. /** \struct address_t
  12. * \brief Message subscription and delivery point
  13. *
  14. * Address is an abstraction of "point of service", i.e. any actor can send
  15. * an message to an address, and any actor can subscribe on any address to
  16. * handle certain kind of messages.
  17. *
  18. * An actor has "main" address, and in addition it may have "virtual" private
  19. * addresses to perform routing messages for the specific methods.
  20. *
  21. * Addresses are produced by {@link supervisor_t}, which also is responsible
  22. * for initial delivery of messages on the address. Address lifetime *should*
  23. * be no longer then corresponding supervisor lifetime.
  24. *
  25. * Addresses are non-copyable and non-moveable. The constructor is private
  26. * and it is intended to be created by supervisor only.
  27. *
  28. */
  29. struct address_t : public arc_base_t<address_t> {
  30. /// reference to {@link supervisor_t}, which generated the address
  31. supervisor_t &supervisor;
  32. /** \brief runtime label, describing some execution group */
  33. const void *locality;
  34. address_t(const address_t &) = delete;
  35. address_t(address_t &&) = delete;
  36. /** \brief returns true if two addresses are the same, i.e. are located in the
  37. * same memory region
  38. */
  39. inline bool operator==(const address_t &other) const noexcept { return this == &other; }
  40. /** \brief compares locality fields of the addresses */
  41. inline bool same_locality(const address_t &other) const noexcept { return this->locality == other.locality; }
  42. private:
  43. friend struct supervisor_t;
  44. address_t(supervisor_t &sup, const void *locality_) : supervisor{sup}, locality{locality_} {}
  45. };
  46. /** \brief intrusive pointer for address */
  47. using address_ptr_t = intrusive_ptr_t<address_t>;
  48. } // namespace rotor
  49. namespace std {
  50. /** \struct hash<rotor::address_ptr_t>
  51. * \brief Hash calculator for address
  52. */
  53. template <> struct hash<rotor::address_ptr_t> {
  54. /** \brief Calculates hash for the address */
  55. inline size_t operator()(const rotor::address_ptr_t &address) const noexcept {
  56. return reinterpret_cast<size_t>(address.get());
  57. }
  58. };
  59. } // namespace std