fix EventId::IsExpired and Simulator::IsExpired, add EventId::IsRunning, add relevant tests, replace Seconds, MilliSeconds, MicroSeconds, and, NanoSeconds classes by functions named similarly
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Thu, 01 Feb 2007 18:52:55 +0100
changeset 209 8b343b9727d7
parent 208 408c964b19e7
child 210 41b8b1f8c4d4
fix EventId::IsExpired and Simulator::IsExpired, add EventId::IsRunning, add relevant tests, replace Seconds, MilliSeconds, MicroSeconds, and, NanoSeconds classes by functions named similarly
src/simulator/event-id.cc
src/simulator/event-id.h
src/simulator/event-impl.cc
src/simulator/event-impl.h
src/simulator/nstime.h
src/simulator/simulator.cc
src/simulator/time.cc
--- a/src/simulator/event-id.cc	Thu Feb 01 18:50:58 2007 +0100
+++ b/src/simulator/event-id.cc	Thu Feb 01 18:52:55 2007 +0100
@@ -44,6 +44,11 @@
 {
   return Simulator::IsExpired (*this);
 }
+bool 
+EventId::IsRunning (void)
+{
+  return !IsExpired ();
+}
 EventImpl *
 EventId::GetEventImpl (void) const
 {
--- a/src/simulator/event-id.h	Thu Feb 01 18:50:58 2007 +0100
+++ b/src/simulator/event-id.h	Thu Feb 01 18:52:55 2007 +0100
@@ -45,6 +45,7 @@
    * \returns true if the event has expired, false otherwise.
    */
   bool IsExpired (void);
+  bool IsRunning (void);
 public:
   /* The following methods are semi-private
    * they are supposed to be invoked only by
--- a/src/simulator/event-impl.cc	Thu Feb 01 18:50:58 2007 +0100
+++ b/src/simulator/event-impl.cc	Thu Feb 01 18:52:55 2007 +0100
@@ -45,4 +45,10 @@
   m_cancel = true;
 }
 
+bool 
+EventImpl::IsCancelled (void)
+{
+  return m_cancel;
+}
+
 }; // namespace ns3
--- a/src/simulator/event-impl.h	Thu Feb 01 18:50:58 2007 +0100
+++ b/src/simulator/event-impl.h	Thu Feb 01 18:52:55 2007 +0100
@@ -31,6 +31,7 @@
   virtual ~EventImpl () = 0;
   void Invoke (void);
   void Cancel (void);
+  bool IsCancelled (void);
 protected:
   virtual void Notify (void) = 0;
 private:
--- a/src/simulator/nstime.h	Thu Feb 01 18:50:58 2007 +0100
+++ b/src/simulator/nstime.h	Thu Feb 01 18:52:55 2007 +0100
@@ -23,6 +23,7 @@
 
 #include <stdint.h>
 #include <cassert>
+#include <ostream>
 #include "high-precision.h"
 
 namespace ns3 {
@@ -110,6 +111,7 @@
    *         stored in this Time<N> type.
    */
   HighPrecision GetHighPrecision (void) const;
+  HighPrecision *PeekHighPrecision (void);
 
 private:
   HighPrecision m_data;
@@ -142,6 +144,12 @@
   return m_data;
 }
 template <int N>
+HighPrecision *
+TimeUnit<N>::PeekHighPrecision (void)
+{
+  return &m_data;
+}
+template <int N>
 bool
 TimeUnit<N>::IsZero (void) const
 {
@@ -180,6 +188,12 @@
 }
 template <int N>
 bool 
+operator != (TimeUnit<N> const &lhs, TimeUnit<N> const &rhs)
+{
+  return lhs.GetHighPrecision ().Compare (rhs.GetHighPrecision ()) != 0;
+}
+template <int N>
+bool 
 operator <= (TimeUnit<N> const &lhs, TimeUnit<N> const &rhs)
 {
   return lhs.GetHighPrecision ().Compare (rhs.GetHighPrecision ()) <= 0;
@@ -230,6 +244,19 @@
   retval.Div (rhs.GetHighPrecision ());
   return TimeUnit<N1-N2> (retval);
 }
+template <int N>
+TimeUnit<N> &operator += (TimeUnit<N> &lhs, TimeUnit<N> const &rhs) {
+  HighPrecision *lhsv = lhs.PeekHighPrecision ();
+  lhsv->Add (rhs.GetHighPrecision ());
+  return lhs;
+}
+template <int N>
+TimeUnit<N> &operator -= (TimeUnit<N> &lhs, TimeUnit<N> const &rhs) {
+  HighPrecision *lhsv = lhs.PeekHighPrecision ();
+  lhsv->Sub (rhs.GetHighPrecision ());
+  return lhs;
+}
+
 
 /**
  * \anchor ns3-Time-Abs
@@ -301,6 +328,9 @@
 public:
   Time ();
   Time (TimeUnit<1> time);
+  Time (HighPrecision const& value);
+
+  static Time Seconds (double seconds);
 
   /**
    * \returns an approximation in seconds of the time stored in this
@@ -324,6 +354,8 @@
   int64_t GetNanoSeconds (void) const;
 };
 
+std::ostream& operator<< (std::ostream& os, Time const& time);
+
 /**
  * \brief create ns3::Time instances in units of seconds.
  *
@@ -333,12 +365,7 @@
  * Simulator::Schedule (NanoSeconds (5.0), ...);
  * \endcode
  */
-class Seconds : public TimeUnit<1>
-{
-public:
-  Seconds ();
-  Seconds (double seconds);
-};
+Time Seconds (double seconds);
 
 /**
  * \brief create ns3::Time instances in units of milliseconds.
@@ -349,12 +376,7 @@
  * Simulator::Schedule (MilliSeconds (5), ...);
  * \endcode
  */
-class MilliSeconds : public TimeUnit<1>
-{
-public:
-  MilliSeconds ();
-  MilliSeconds (uint32_t ms);
-};
+Time MilliSeconds (uint32_t ms);
 /**
  * \brief create ns3::Time instances in units of microseconds.
  *
@@ -364,12 +386,7 @@
  * Simulator::Schedule (MicroSeconds (5), ...);
  * \endcode
  */
-class MicroSeconds : public TimeUnit<1>
-{
-public:
-  MicroSeconds ();
-  MicroSeconds (uint64_t ms);
-};
+Time MicroSeconds (uint64_t us);
 /**
  * \brief create ns3::Time instances in units of nanoseconds.
  *
@@ -379,12 +396,7 @@
  * Simulator::Schedule (NanoSeconds (5), ...);
  * \endcode
  */
-class NanoSeconds : public TimeUnit<1>
-{
-public:
-  NanoSeconds ();
-  NanoSeconds (uint64_t ms);
-};
+Time NanoSeconds (uint64_t ns);
 
 /**
  * \brief create an ns3::Time instance which contains the
@@ -392,16 +404,12 @@
  *
  * This is really a shortcut for the ns3::Simulator::Now method.
  * It is typically used as shown below to schedule an event
- * which expires in 2 seconds from now:
+ * which expires at the absolute time "2 seconds":
  * \code
- * Simulator::Schedule (Seconds (2.0), &my_function);
+ * Simulator::Schedule (Seconds (2.0) - Now (), &my_function);
  * \endcode
  */
-class Now : public Time
-{
-public:
-  Now ();
-};
+Time Now (void);
 
 /**
  * \brief hold scalar values
--- a/src/simulator/simulator.cc	Thu Feb 01 18:50:58 2007 +0100
+++ b/src/simulator/simulator.cc	Thu Feb 01 18:52:55 2007 +0100
@@ -251,13 +251,18 @@
 bool
 SimulatorPrivate::IsExpired (EventId ev)
 {
-  if (ev.GetEventImpl () != 0 &&
-      ev.GetNs () <= m_currentNs &&
-      ev.GetUid () < m_currentUid) 
+  if (ev.GetEventImpl () == 0 ||
+      ev.GetNs () < m_currentNs ||
+      (ev.GetNs () == m_currentNs &&
+       ev.GetUid () <= m_currentUid) ||
+      ev.GetEventImpl ()->IsCancelled ()) 
+    {
+      return true;
+    }
+  else
     {
       return false;
     }
-  return true;
 }
 
 
@@ -570,7 +575,19 @@
   Simulator::Schedule (MicroSeconds (11), &SimulatorTests::B, this, 2);
   m_idC = Simulator::Schedule (MicroSeconds (12), &SimulatorTests::C, this, 3);
 
+  if (m_idC.IsExpired ()) 
+    {
+      ok = false;
+    }
+  if (a.IsExpired ())
+    {
+      ok = false;
+    }
   Simulator::Cancel (a);
+  if (!a.IsExpired ())
+    {
+      ok = false;
+    }
   Simulator::Run ();
 
   if (!m_a || !m_b || !m_c || !m_d) 
--- a/src/simulator/time.cc	Thu Feb 01 18:50:58 2007 +0100
+++ b/src/simulator/time.cc	Thu Feb 01 18:52:55 2007 +0100
@@ -29,6 +29,15 @@
 Time::Time (TimeUnit<1> time)
   : TimeUnit<1> (time)
 {}
+Time::Time (HighPrecision const& value)
+  : TimeUnit<1> (value)
+{}
+
+Time 
+Time::Seconds (double seconds)
+{
+  return Seconds (seconds);
+}
 
 double 
 Time::GetSeconds (void) const
@@ -57,35 +66,33 @@
   return GetHighPrecision ().GetInteger ();
 }
 
+std::ostream& 
+operator<< (std::ostream& os, Time const& time)
+{
+  os << time.GetNanoSeconds () << "ns";
+  return os;
+}
 
-Seconds::Seconds ()
-  : TimeUnit<1> ()
-{}
-Seconds::Seconds (double seconds)
-  : TimeUnit<1> (HighPrecision (seconds * 1000000000.0))
-{}
-MilliSeconds::MilliSeconds ()
-  : TimeUnit<1> ()
-{}
-MilliSeconds::MilliSeconds (uint32_t ms)
-  : TimeUnit<1> (HighPrecision (ms * 1000000, false))
-{}
-MicroSeconds::MicroSeconds ()
-  : TimeUnit<1> ()
-{}
-MicroSeconds::MicroSeconds (uint64_t us)
-  : TimeUnit<1> (HighPrecision (us * 1000, false))
-{}
-NanoSeconds::NanoSeconds ()
-  : TimeUnit<1> ()
-{}
-NanoSeconds::NanoSeconds (uint64_t ns)
-  : TimeUnit<1> (HighPrecision (ns, false))
-{}
-
-Now::Now ()
-  : Time (Simulator::Now ())
-{}
+Time Seconds (double seconds)
+{
+  return Time (HighPrecision (seconds * 1000000000.0));
+}
+Time MilliSeconds (uint32_t ms)
+{
+  return Time (HighPrecision (ms * 1000000, false));
+}
+Time MicroSeconds (uint64_t us)
+{
+  return Time (HighPrecision (us * 1000, false));
+}
+Time NanoSeconds (uint64_t ns)
+{
+  return Time (HighPrecision (ns, false));
+}
+Time Now (void)
+{
+  return Time (Simulator::Now ());
+}
 
 Scalar::Scalar ()
   : TimeUnit<0> ()