system_context.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 "address.hpp"
  8. #include "supervisor_config.h"
  9. #include "extended_error.h"
  10. #include "message_stringifier.h"
  11. #include <system_error>
  12. #if defined(_MSC_VER)
  13. #pragma warning(push)
  14. #pragma warning(disable : 4251)
  15. #endif
  16. namespace rotor {
  17. struct supervisor_t;
  18. struct actor_base_t;
  19. using supervisor_ptr_t = intrusive_ptr_t<supervisor_t>;
  20. /** \struct system_context_t
  21. * \brief The system context holds root {@link supervisor_t}
  22. * (intrusive pointer) and may be loop-related details in derived classes
  23. *
  24. */
  25. struct ROTOR_API system_context_t : arc_base_t<system_context_t> {
  26. public:
  27. /** \brief returns builder for root supervisor */
  28. template <typename Supervisor = supervisor_t> auto create_supervisor();
  29. /** \brief returns root supervisor */
  30. inline supervisor_ptr_t get_supervisor() noexcept { return supervisor; }
  31. system_context_t() = default;
  32. system_context_t(const system_context_t &) = delete;
  33. system_context_t(system_context_t &&) = delete;
  34. virtual ~system_context_t();
  35. /** \brief fatal error handler
  36. *
  37. * The error is fatal, is further `rotor` behavior is undefined. The method should
  38. * be overriden in derived classes for error propagation/notification. The default
  39. * implementation is to output the error to `std::err` and invoke `std::abort()`.
  40. *
  41. */
  42. virtual void on_error(actor_base_t *actor, const extended_error_ptr_t &ec) noexcept;
  43. /** \brief identifies the context.
  44. *
  45. * By default, is is just human-readable address of the system context
  46. *
  47. */
  48. virtual std::string identity() noexcept;
  49. /** \brief generic non-public fields accessor */
  50. template <typename T> auto &access() noexcept;
  51. /** \brief returns the default stringifier
  52. *
  53. * The stringifier is lazily constructed on demand via `make_stringifier` method.
  54. *
  55. */
  56. const message_stringifier_t &get_stringifier();
  57. protected:
  58. /** \brief constructs message stringifier */
  59. virtual message_stringifier_ptr_t make_stringifier() const noexcept;
  60. private:
  61. friend struct supervisor_t;
  62. supervisor_ptr_t supervisor;
  63. message_stringifier_ptr_t stringifier;
  64. };
  65. /** \brief intrusive pointer for system context */
  66. using system_context_ptr_t = intrusive_ptr_t<system_context_t>;
  67. } // namespace rotor
  68. #if defined(_MSC_VER)
  69. #pragma warning(pop)
  70. #endif