Timer.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <chrono>
  2. #include "Util.h"
  3. #include "Timer.h"
  4. namespace PresenceDetection {
  5. namespace Util {
  6. Timer::Timer() :
  7. m_run(false),
  8. m_identifier(CreateRandomString(10))
  9. {
  10. }
  11. Timer::~Timer()
  12. {
  13. Wait();
  14. }
  15. Timer::Timer(Timer&& other) :
  16. m_run(static_cast<bool>(other.m_run)),
  17. m_identifier(other.m_identifier),
  18. m_thread(std::move(other.m_thread))
  19. {
  20. }
  21. Timer& Timer::operator=(Timer&& other)
  22. {
  23. m_run = static_cast<bool>(other.m_run);
  24. m_identifier = std::move(other.m_identifier);
  25. m_thread = std::move(other.m_thread);
  26. return *this;
  27. }
  28. bool Timer::operator==(const Timer& other) const
  29. {
  30. return m_identifier == other.Identifier();
  31. }
  32. bool Timer::operator==(const std::string& identifier) const
  33. {
  34. return m_identifier == identifier;
  35. }
  36. std::string Timer::Identifier() const
  37. {
  38. return m_identifier;
  39. }
  40. void Timer::Stop()
  41. {
  42. m_run.store(false, std::memory_order_release);
  43. }
  44. bool Timer::IsRunning() const noexcept
  45. {
  46. return (m_run.load(std::memory_order_acquire) && m_thread.joinable());
  47. }
  48. void Timer::Wait()
  49. {
  50. if (m_thread.joinable())
  51. m_thread.join();
  52. }
  53. } // namespace Util
  54. } // namespace PresenceDetection