thread_id_cp.hpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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_THREAD_ID_COMPONENT_HPP__
  11. #define __ASIO2_THREAD_ID_COMPONENT_HPP__
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. #pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <thread>
  16. #include <asio2/base/error.hpp>
  17. namespace asio2::detail
  18. {
  19. template<class derived_t, class args_t = void>
  20. class thread_id_cp
  21. {
  22. public:
  23. /**
  24. * @brief constructor
  25. */
  26. thread_id_cp() = default;
  27. /**
  28. * @brief destructor
  29. */
  30. ~thread_id_cp() = default;
  31. thread_id_cp(thread_id_cp&&) noexcept = default;
  32. thread_id_cp(thread_id_cp const&) = default;
  33. thread_id_cp& operator=(thread_id_cp&&) noexcept = default;
  34. thread_id_cp& operator=(thread_id_cp const&) = default;
  35. public:
  36. /**
  37. * @brief Determine whether the object is running in the current thread.
  38. */
  39. inline bool running_in_this_thread() const noexcept
  40. {
  41. derived_t& derive = const_cast<derived_t&>(static_cast<const derived_t&>(*this));
  42. return (derive.io_->get_thread_id() == std::this_thread::get_id());
  43. }
  44. /**
  45. * @brief return the thread id of the current object running in.
  46. */
  47. inline std::thread::id get_thread_id() const noexcept
  48. {
  49. derived_t& derive = const_cast<derived_t&>(static_cast<const derived_t&>(*this));
  50. return derive.io_->get_thread_id();
  51. }
  52. protected:
  53. };
  54. }
  55. #endif // !__ASIO2_THREAD_ID_COMPONENT_HPP__