supervisor_config_asio.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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/supervisor_config.h"
  8. #include <boost/asio.hpp>
  9. #include <memory>
  10. namespace rotor {
  11. namespace asio {
  12. /** \struct supervisor_config_asio_t
  13. * \brief boost::asio supervisor config, which holds pointer to strand */
  14. struct supervisor_config_asio_t : public supervisor_config_t {
  15. /** \brief alias for boost::asio strand type */
  16. using strand_t = boost::asio::io_context::strand;
  17. /** \brief type for strand shared pointer */
  18. using strand_ptr_t = std::shared_ptr<strand_t>;
  19. /** \brief boost::asio execution strand (shared pointer) */
  20. strand_ptr_t strand;
  21. /** \brief should supervisor take ownership on the io_context */
  22. bool guard_context = false;
  23. using supervisor_config_t::supervisor_config_t;
  24. };
  25. /** \brief CRTP supervisor asio config builder */
  26. template <typename Supervisor> struct supervisor_config_asio_builder_t : supervisor_config_builder_t<Supervisor> {
  27. /** \brief final builder class */
  28. using builder_t = typename Supervisor::template config_builder_t<Supervisor>;
  29. /** \brief parent config builder */
  30. using parent_t = supervisor_config_builder_t<Supervisor>;
  31. using parent_t::parent_t;
  32. /** \brief alias for strand smart pointer */
  33. using strand_ptr_t = supervisor_config_asio_t::strand_ptr_t;
  34. /** \brief bit mask for strand validation */
  35. constexpr static const std::uint32_t STRAND = 1 << 2;
  36. /** \brief bit mask for all required fields */
  37. constexpr static const std::uint32_t requirements_mask = parent_t::requirements_mask | STRAND;
  38. /** \brief strand setter */
  39. builder_t &&strand(strand_ptr_t &strand) && {
  40. parent_t::config.strand = strand;
  41. parent_t::mask = (parent_t::mask & ~STRAND);
  42. return std::move(*static_cast<builder_t *>(this));
  43. }
  44. /** \brief instructs to take ownership of the io_context */
  45. builder_t &&guard_context(bool value) && {
  46. parent_t::config.guard_context = value;
  47. return std::move(*static_cast<builder_t *>(this));
  48. }
  49. };
  50. } // namespace asio
  51. } // namespace rotor