timer.cpp 932 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "timer.h"
  2. timer::timer(clock::duration duration, bool autostart) noexcept :
  3. set(duration),
  4. remaining(set),
  5. target(clock::now() + set),
  6. _paused(!autostart)
  7. {}
  8. bool timer::check() noexcept
  9. {
  10. if(_paused)
  11. return false;
  12. remaining = target - clock::now();
  13. if(target <= clock::now())
  14. {
  15. return true;
  16. }
  17. return false;
  18. }
  19. auto timer::duration() const noexcept
  20. -> clock::duration
  21. {
  22. return set;
  23. }
  24. auto timer::remaining_duration() const noexcept
  25. -> clock::duration
  26. {
  27. return remaining;
  28. }
  29. auto timer::target_time_point() const noexcept
  30. -> clock::time_point
  31. {
  32. if(_paused)
  33. return clock::now() + remaining;
  34. else
  35. return target;
  36. }
  37. void timer::pause(bool value) noexcept
  38. {
  39. if(value)
  40. pause();
  41. else
  42. resume();
  43. }
  44. void timer::pause() noexcept
  45. {
  46. _paused = true;
  47. }
  48. void timer::resume() noexcept
  49. {
  50. _paused = false;
  51. target = clock::now() + remaining;
  52. }
  53. bool timer::paused() const noexcept
  54. {
  55. return _paused;
  56. }