periodiccondition.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. ** Copyright (C) 1999 Microsoft Corporation. All Rights Reserved.
  3. **
  4. ** File: periodiccondition.cpp
  5. **
  6. ** Author:
  7. **
  8. ** Description:
  9. ** Implementation of the training library "periodiccondition" interface.
  10. **
  11. ** History:
  12. */
  13. #include "pch.h"
  14. #include "PeriodicCondition.h"
  15. namespace Training
  16. {
  17. //------------------------------------------------------------------------------
  18. // class methods
  19. //------------------------------------------------------------------------------
  20. /* void */ PeriodicCondition::PeriodicCondition (Condition* pCondition, float fPeriod) :
  21. m_pCondition (pCondition),
  22. m_dwPeriod (static_cast<DWORD> (fPeriod * Time::fResolution ()))
  23. {
  24. }
  25. //------------------------------------------------------------------------------
  26. /* void */ PeriodicCondition::~PeriodicCondition (void)
  27. {
  28. // assume no one else will be cleaning this up
  29. delete m_pCondition;
  30. }
  31. //------------------------------------------------------------------------------
  32. bool PeriodicCondition::Start (void)
  33. {
  34. // record the start time
  35. m_dwStartTime = Time::Now ().clock ();
  36. // always evaluate the condition the first time through
  37. return m_pCondition->Start ();
  38. }
  39. //------------------------------------------------------------------------------
  40. void PeriodicCondition::Stop (void)
  41. {
  42. m_pCondition->Stop ();
  43. }
  44. //------------------------------------------------------------------------------
  45. bool PeriodicCondition::Evaluate (void)
  46. {
  47. // get the current clock time, and compute how much time has elapsed.
  48. DWORD dwElapsedTime = (Time::Now ().clock () - m_dwStartTime);
  49. // if the elapsed time is longer than the desired period
  50. if (dwElapsedTime > m_dwPeriod)
  51. {
  52. // advance the clock base to the beginning of the next period. This is clock accurate, as
  53. // opposed to just using the current time for the beginning of the next cycle. This method
  54. // will be better for avoiding sampling errors, even though nobody would ever notice.
  55. m_dwStartTime += m_dwPeriod;
  56. // return the result of evaluating the enclosed condition
  57. return m_pCondition->Evaluate ();
  58. }
  59. // return false to indicate the condition hasn't been evaluated because the period hasn't
  60. // elapsed yet.
  61. return false;
  62. }
  63. //------------------------------------------------------------------------------
  64. }