connect_time_cp.hpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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_CONNECT_TIME_COMPONENT_HPP__
  11. #define __ASIO2_CONNECT_TIME_COMPONENT_HPP__
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. #pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <chrono>
  16. namespace asio2::detail
  17. {
  18. template<class derived_t, class args_t>
  19. class connect_time_cp
  20. {
  21. public:
  22. /**
  23. * @brief constructor
  24. */
  25. connect_time_cp() = default;
  26. /**
  27. * @brief destructor
  28. */
  29. ~connect_time_cp() = default;
  30. connect_time_cp(connect_time_cp&&) noexcept = default;
  31. connect_time_cp(connect_time_cp const&) = default;
  32. connect_time_cp& operator=(connect_time_cp&&) noexcept = default;
  33. connect_time_cp& operator=(connect_time_cp const&) = default;
  34. public:
  35. /**
  36. * @brief get build connection time
  37. */
  38. inline std::chrono::time_point<std::chrono::system_clock> get_connect_time() const noexcept
  39. {
  40. return this->connect_time_;
  41. }
  42. /**
  43. * @brief reset build connection time to system_clock::now()
  44. */
  45. inline derived_t & reset_connect_time() noexcept
  46. {
  47. this->connect_time_ = std::chrono::system_clock::now();
  48. return (static_cast<derived_t &>(*this));
  49. }
  50. /**
  51. * @brief get connection duration of std::chrono::duration
  52. */
  53. inline std::chrono::system_clock::duration get_connect_duration() const noexcept
  54. {
  55. return std::chrono::duration_cast<std::chrono::system_clock::duration>(
  56. std::chrono::system_clock::now() - this->connect_time_);
  57. }
  58. protected:
  59. /// build connection time
  60. decltype(std::chrono::system_clock::now()) connect_time_ = std::chrono::system_clock::now();
  61. };
  62. }
  63. #endif // !__ASIO2_CONNECT_TIME_COMPONENT_HPP__