Timer.h 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * @file
  3. * @author Marek Bel
  4. */
  5. #ifndef TIMER_H
  6. #define TIMER_H
  7. /**
  8. * @brief simple timer
  9. *
  10. * Simple and memory saving implementation. Should handle timer register wrap around well.
  11. * Resolution is one millisecond. To save memory, doesn't store timer period.
  12. * If you wish timer which is storing period, derive from this.
  13. */
  14. template <class T>
  15. class Timer
  16. {
  17. public:
  18. Timer();
  19. void start();
  20. void stop(){m_isRunning = false;}
  21. bool running(){return m_isRunning;}
  22. bool expired(T msPeriod);
  23. protected:
  24. T started(){return m_started;}
  25. private:
  26. bool m_isRunning;
  27. T m_started;
  28. };
  29. /**
  30. * @brief Timer unsigned long specialization
  31. *
  32. * Maximum period is at least 49 days.
  33. */
  34. #if __cplusplus>=201103L
  35. using LongTimer = Timer<unsigned long>;
  36. #else
  37. typedef Timer<unsigned long> LongTimer;
  38. #endif
  39. /**
  40. * @brief Timer unsigned short specialization
  41. *
  42. * Maximum period is at least 65 seconds.
  43. */
  44. #if __cplusplus>=201103L
  45. using ShortTimer = Timer<unsigned short>;
  46. #else
  47. typedef Timer<unsigned short> ShortTimer;
  48. #endif
  49. #endif /* TIMER_H */