io_context_cp.hpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (c) 2017-2023 zhllxt
  3. *
  4. * author : zhllxt
  5. * email : 37792738@qq.com
  6. *
  7. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. */
  10. #ifndef __ASIO2_IO_CONTEXT_COMPONENT_HPP__
  11. #define __ASIO2_IO_CONTEXT_COMPONENT_HPP__
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. #pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <asio2/base/iopool.hpp>
  16. namespace asio2::detail
  17. {
  18. template<class derived_t, class args_t = void>
  19. class io_context_cp
  20. {
  21. public:
  22. /**
  23. * @brief constructor
  24. */
  25. explicit io_context_cp(std::shared_ptr<io_t> rwio) : io_(std::move(rwio))
  26. {
  27. }
  28. /**
  29. * @brief destructor
  30. */
  31. ~io_context_cp()
  32. {
  33. }
  34. /**
  35. * @brief get the io object reference
  36. */
  37. inline io_t& io() noexcept
  38. {
  39. return *(this->io_);
  40. }
  41. /**
  42. * @brief get the io object reference
  43. */
  44. inline io_t const& io() const noexcept
  45. {
  46. return *(this->io_);
  47. }
  48. /**
  49. * @brief get the io object shared_ptr
  50. */
  51. inline std::shared_ptr<io_t> io_ptr() noexcept
  52. {
  53. return this->io_;
  54. }
  55. protected:
  56. /// The io_context wrapper used to handle the recv/send event.
  57. /// the io_context should be destroyed after socket, beacuse the socket used the
  58. /// io_context, so if the io_context is destroyed before socket, then maybe cause crash.
  59. std::shared_ptr<io_t> io_;
  60. };
  61. }
  62. #endif // !__ASIO2_IO_CONTEXT_COMPONENT_HPP__