Timer.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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()const {return m_isRunning;}
  22. bool expired(T msPeriod);
  23. T elapsed();
  24. protected:
  25. T started()const {return m_started;}
  26. private:
  27. bool m_isRunning;
  28. T m_started;
  29. };
  30. /**
  31. * @brief Timer unsigned long specialization
  32. *
  33. * Maximum period is at least 49 days.
  34. */
  35. #if __cplusplus>=201103L
  36. using LongTimer = Timer<unsigned long>;
  37. #else
  38. typedef Timer<unsigned long> LongTimer;
  39. #endif
  40. /**
  41. * @brief Timer unsigned short specialization
  42. *
  43. * Maximum period is at least 65 seconds.
  44. */
  45. #if __cplusplus>=201103L
  46. using ShortTimer = Timer<unsigned short>;
  47. #else
  48. typedef Timer<unsigned short> ShortTimer;
  49. #endif
  50. #endif /* TIMER_H */