rttime.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. #ifndef RTTIME_H
  19. #define RTTIME_H
  20. //////////////////////////////////////////////////////////////////////////////
  21. // Headers.
  22. //////////////////////////////////////////////////////////////////////////////
  23. #include "bdebug.h"
  24. #include "btime.h"
  25. //////////////////////////////////////////////////////////////////////////////
  26. // Macros.
  27. //////////////////////////////////////////////////////////////////////////////
  28. //////////////////////////////////////////////////////////////////////////////
  29. // Typedefs.
  30. //////////////////////////////////////////////////////////////////////////////
  31. typedef long (*RTTIMEFUNC)(void);
  32. class CRtTime
  33. {
  34. public: // Construction/Destruction.
  35. // Default constructor.
  36. CRtTime()
  37. {
  38. m_fnTime = NULL;
  39. m_lOffset = 0L;
  40. m_sSuspended = 0;
  41. // Might as well start out at 0.
  42. SetTime(0L);
  43. }
  44. // Destructor.
  45. ~CRtTime()
  46. { }
  47. public: // Methods.
  48. // Sets (or clears, if NULL) the timer override function.
  49. void SetTimeFunc(RTTIMEFUNC fnTime)
  50. { m_fnTime = fnTime; }
  51. // Set the current time. Sets the time offset to specified time minus
  52. // the current GetTime() so that a subsequent call to GetTime() would.
  53. void SetTime(long lTime)
  54. {
  55. // Zero the offset so we get the actual reported time.
  56. m_lOffset = 0;
  57. m_lOffset = lTime - GetTime();
  58. }
  59. // Suspends the timer. Stores the current time.
  60. // Every call to this requires a subsequent Resume().
  61. void Suspend(void)
  62. {
  63. // If this is the first . . .
  64. if (m_sSuspended++ == 0)
  65. {
  66. // Get the current time.
  67. m_lSuspended = GetTime();
  68. }
  69. }
  70. // Resumes the timer. Basically, does a SetTime() with the time that
  71. // Suspend was called.
  72. void Resume(void)
  73. {
  74. ASSERT(m_lSuspended > 0);
  75. // If this is the last necessary resume to release the timer . . .
  76. if (--m_sSuspended == 0)
  77. {
  78. // Set the time to the time the timer was first suspended.
  79. SetTime(m_lSuspended);
  80. }
  81. }
  82. public: // Querries.
  83. // Returns the time from the override function if set or, if not set,
  84. // from Blu_GetTime(). If suspended, this function returns the time
  85. // of the suspension.
  86. long GetTime(void)
  87. {
  88. if (m_sSuspended == 0)
  89. return (m_fnTime != NULL ? (*m_fnTime)() : Blu_GetTime()) + m_lOffset;
  90. else
  91. return m_lSuspended;
  92. }
  93. // Static version of above for those that need to call via a ptr and
  94. // don't know anything about this object. Send this a long and it
  95. // will use it to call the appropriate 'this'-based GetTime().
  96. static long GetTime(long l_pRtTime)
  97. {
  98. return ((CRtTime*)l_pRtTime)->GetTime();
  99. }
  100. protected: // Internal methods.
  101. public: // Members.
  102. protected: // Members.
  103. RTTIMEFUNC m_fnTime; // If set, used to get time.
  104. long m_lOffset; // Added to the time to manipulate it.
  105. long m_lSuspended; // Time at which the timer was suspended.
  106. short m_sSuspended; // TRUE if the timer is currently suspended.
  107. };
  108. #endif // RTTIME_H
  109. //////////////////////////////////////////////////////////////////////////////
  110. // EOF
  111. //////////////////////////////////////////////////////////////////////////////