Timer.h 1.0 KB

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