CTimer.cpp 907 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <LibCore/CTimer.h>
  2. CTimer::CTimer(CObject* parent)
  3. : CObject(parent)
  4. {
  5. }
  6. CTimer::CTimer(int interval, Function<void()>&& timeout_handler, CObject* parent)
  7. : CObject(parent)
  8. , on_timeout(move(timeout_handler))
  9. {
  10. start(interval);
  11. }
  12. CTimer::~CTimer()
  13. {
  14. }
  15. void CTimer::start()
  16. {
  17. start(m_interval);
  18. }
  19. void CTimer::start(int interval)
  20. {
  21. if (m_active)
  22. return;
  23. m_interval = interval;
  24. start_timer(interval);
  25. m_active = true;
  26. }
  27. void CTimer::restart(int interval)
  28. {
  29. if (m_active)
  30. stop();
  31. start(interval);
  32. }
  33. void CTimer::stop()
  34. {
  35. if (!m_active)
  36. return;
  37. stop_timer();
  38. m_active = false;
  39. }
  40. void CTimer::timer_event(CTimerEvent&)
  41. {
  42. if (m_single_shot)
  43. stop();
  44. else {
  45. if (m_interval_dirty) {
  46. stop();
  47. start(m_interval);
  48. }
  49. }
  50. if (on_timeout)
  51. on_timeout();
  52. }