Chronometer.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. sftools - Copyright (c) 2012-2013 Marco Antognini <antognini.marco@gmail.com>
  3. This software is provided 'as-is', without any express or implied warranty. In
  4. no event will the authors be held liable for any damages arising from the use
  5. of this software.
  6. Permission is granted to anyone to use this software for any purpose, including
  7. commercial applications, and to alter it and redistribute it freely, subject to
  8. the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim
  10. that you wrote the original software. If you use this software in a product,
  11. an acknowledgment in the product documentation would be appreciated but is
  12. not required.
  13. 2. Altered source versions must be plainly marked as such, and must not be
  14. misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source distribution.
  16. */
  17. /*!
  18. @file sftools/Chronometer.hpp
  19. @brief Defines Chronometer
  20. */
  21. #ifndef __SFTOOLS_BASE_CHRONOMETER_HPP__
  22. #define __SFTOOLS_BASE_CHRONOMETER_HPP__
  23. #include <cpp3ds/System/Clock.hpp>
  24. /*!
  25. @namespace sftools
  26. @brief Simple and Fast Tools
  27. */
  28. namespace sftools
  29. {
  30. /*!
  31. @class Chronometer
  32. @brief Provide functionalities of a chronometer, aka stop watch
  33. */
  34. class Chronometer
  35. {
  36. public:
  37. /*!
  38. @brief Constructor
  39. @param initialTime Initial time elapsed
  40. */
  41. Chronometer(cpp3ds::Time initialTime = cpp3ds::Time::Zero)
  42. {
  43. reset();
  44. add(initialTime);
  45. }
  46. /*!
  47. @brief Add some time
  48. @param time Time to be added to the time elapsed
  49. @return Time elapsed
  50. */
  51. cpp3ds::Time add(cpp3ds::Time time)
  52. {
  53. m_time += time;
  54. if (m_state == STOPPED) m_state = PAUSED;
  55. return getElapsedTime();
  56. }
  57. /*!
  58. @brief Reset the chronometer
  59. @param start if true the chronometer automatically starts
  60. @return Time elapsed on the chronometer before the reset
  61. */
  62. cpp3ds::Time reset(bool start = false)
  63. {
  64. cpp3ds::Time time = getElapsedTime();
  65. m_time = cpp3ds::Time::Zero;
  66. m_state = STOPPED;
  67. if (start) resume();
  68. return time;
  69. }
  70. /*!
  71. @brief Pause the chronometer
  72. @return Time elapsed
  73. @see toggle
  74. */
  75. cpp3ds::Time pause()
  76. {
  77. if (isRunning())
  78. {
  79. m_state = PAUSED;
  80. m_time += m_clock.getElapsedTime();
  81. }
  82. return getElapsedTime();
  83. }
  84. /*!
  85. @brief Resume the chronometer
  86. @return Time elapsed
  87. @see toggle
  88. */
  89. cpp3ds::Time resume()
  90. {
  91. if (!isRunning())
  92. {
  93. m_state = RUNNING;
  94. m_clock.restart();
  95. }
  96. return getElapsedTime();
  97. }
  98. /*!
  99. @brief Pause or resume the chronometer
  100. If the chronometer is running the it is paused;
  101. otherwise it is resumes.
  102. @return Time elapsed
  103. @see pause
  104. @see resume
  105. */
  106. cpp3ds::Time toggle()
  107. {
  108. if (isRunning()) pause();
  109. else resume();
  110. return getElapsedTime();
  111. }
  112. /*!
  113. @brief Tell the chronometer is running or not
  114. @brief chronometer's status
  115. */
  116. bool isRunning() const
  117. {
  118. return m_state == RUNNING;
  119. }
  120. /*!
  121. @brief Give the amount of time elapsed since the chronometer was started
  122. @return Time elapsed
  123. */
  124. cpp3ds::Time getElapsedTime() const
  125. {
  126. switch (m_state) {
  127. case STOPPED:
  128. return cpp3ds::Time::Zero;
  129. case RUNNING:
  130. return m_time + m_clock.getElapsedTime();
  131. case PAUSED:
  132. return m_time;
  133. }
  134. }
  135. /*!
  136. @brief Implicit conversion to cpp3ds::Time
  137. @return Time elapsed
  138. @see getElapsedTime
  139. */
  140. operator cpp3ds::Time() const
  141. {
  142. return getElapsedTime();
  143. }
  144. private:
  145. enum { STOPPED, RUNNING, PAUSED } m_state; //!< state
  146. cpp3ds::Time m_time; //!< time counter
  147. cpp3ds::Clock m_clock; //!< clock
  148. };
  149. }
  150. #endif // __SFTOOLS_BASE_CHRONOMETER_HPP__