make the timer a tristate object

This commit is contained in:
Mathieu Lacage
2007-10-04 09:14:16 +02:00
parent 855d52908b
commit b28727ef8c
2 changed files with 38 additions and 3 deletions

View File

@@ -72,18 +72,35 @@ Timer::Remove (void)
bool
Timer::IsExpired (void) const
{
return m_event.IsExpired ();
return !IsSuspended () && m_event.IsExpired ();
}
bool
Timer::IsRunning (void) const
{
return m_event.IsRunning ();
return !IsSuspended () && m_event.IsRunning ();
}
bool
Timer::IsSuspended (void) const
{
return (m_flags & TIMER_SUSPENDED) == TIMER_SUSPENDED;
}
enum Timer::State
Timer::GetState (void) const
{
if (IsRunning ())
{
return Timer::RUNNING;
}
else if (IsExpired ())
{
return Timer::EXPIRED;
}
else
{
NS_ASSERT (IsSuspended ());
return Timer::SUSPENDED;
}
}
void
Timer::Schedule (void)
@@ -200,18 +217,27 @@ TimerTests::RunTests (void)
NS_TEST_ASSERT (!timer.IsRunning ());
NS_TEST_ASSERT (timer.IsExpired ());
NS_TEST_ASSERT (!timer.IsSuspended ());
NS_TEST_ASSERT_EQUAL (timer.GetState (), Timer::EXPIRED);
timer.Schedule ();
NS_TEST_ASSERT (timer.IsRunning ());
NS_TEST_ASSERT (!timer.IsExpired ());
NS_TEST_ASSERT (!timer.IsSuspended ());
NS_TEST_ASSERT_EQUAL (timer.GetState (), Timer::RUNNING);
timer.Suspend ();
NS_TEST_ASSERT (!timer.IsRunning ());
NS_TEST_ASSERT (timer.IsExpired ());
NS_TEST_ASSERT (!timer.IsExpired ());
NS_TEST_ASSERT (timer.IsSuspended ());
NS_TEST_ASSERT_EQUAL (timer.GetState (), Timer::SUSPENDED);
timer.Resume ();
NS_TEST_ASSERT (timer.IsRunning ());
NS_TEST_ASSERT (!timer.IsExpired ());
NS_TEST_ASSERT (!timer.IsSuspended ());
NS_TEST_ASSERT_EQUAL (timer.GetState (), Timer::RUNNING);
timer.Cancel ();
NS_TEST_ASSERT (!timer.IsRunning ());
NS_TEST_ASSERT (timer.IsExpired ());
NS_TEST_ASSERT (!timer.IsSuspended ());
NS_TEST_ASSERT_EQUAL (timer.GetState (), Timer::EXPIRED);
int a = 0;
int &b = a;

View File

@@ -67,6 +67,11 @@ public:
*/
GARBAGE_COLLECT = (1<<6)
};
enum State {
RUNNING,
EXPIRED,
SUSPENDED,
};
/**
* create a timer with a default event lifetime management policy:
* - CHECK_ON_SCHEDULE
@@ -195,6 +200,10 @@ public:
* otherwise.
*/
bool IsSuspended (void) const;
/**
* \returns the current state of the timer.
*/
enum Timer::State GetState (void) const;
/**
* Schedule a new event using the currently-configured delay, function,
* and arguments.