system_context.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 "address.hpp"
  8. #include "error_code.h"
  9. #include <system_error>
  10. namespace rotor {
  11. struct supervisor_t;
  12. using supervisor_ptr_t = intrusive_ptr_t<supervisor_t>;
  13. /** \struct system_context_t
  14. * \brief The system context holds root {@link supervisor_t}
  15. * (intrusive pointer) and may be loop-related details in derived classes
  16. *
  17. */
  18. struct system_context_t : arc_base_t<system_context_t> {
  19. public:
  20. /** \brief creates root supervior. `args` are forwared for supervisor constructor */
  21. template <typename Supervisor = supervisor_t, typename... Args>
  22. auto create_supervisor(Args &&... args) -> intrusive_ptr_t<Supervisor>;
  23. /** \brief returns root supervisor */
  24. inline supervisor_ptr_t get_supervisor() noexcept { return supervisor; }
  25. system_context_t();
  26. system_context_t(const system_context_t &) = delete;
  27. system_context_t(system_context_t &&) = delete;
  28. virtual ~system_context_t();
  29. /** \brief fatal error handler
  30. *
  31. * The error is fatal, is further `rotor` behavior is undefined. The method should
  32. * be overriden in derived classes for error propagation/notification. The default
  33. * implementation is to output the error to `std::err` and invoke `std::abort()`.
  34. *
  35. */
  36. virtual void on_error(const std::error_code &ec) noexcept;
  37. private:
  38. friend struct supervisor_t;
  39. supervisor_ptr_t supervisor;
  40. };
  41. /** \brief intrusive pointer for system context */
  42. using system_context_ptr_t = intrusive_ptr_t<system_context_t>;
  43. } // namespace rotor