From b28727ef8c9dc2f34ba56fcbd4886231cb21c31d Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 4 Oct 2007 09:14:16 +0200 Subject: [PATCH] make the timer a tristate object --- src/simulator/timer.cc | 32 +++++++++++++++++++++++++++++--- src/simulator/timer.h | 9 +++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/simulator/timer.cc b/src/simulator/timer.cc index b6f8f5929..d17a69c11 100644 --- a/src/simulator/timer.cc +++ b/src/simulator/timer.cc @@ -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; diff --git a/src/simulator/timer.h b/src/simulator/timer.h index 4539626f1..2d573c3df 100644 --- a/src/simulator/timer.h +++ b/src/simulator/timer.h @@ -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.