src/simulator/time.cc
changeset 156 3b1563e95ea7
parent 151 8395af452e55
child 157 19ffb62f521e
equal deleted inserted replaced
155:cf4d45ad2da9 156:3b1563e95ea7
    22 #include "simulator.h"
    22 #include "simulator.h"
    23 
    23 
    24 namespace ns3 {
    24 namespace ns3 {
    25 
    25 
    26 Time::Time ()
    26 Time::Time ()
    27   : m_ns (0)
    27   : TimeUnit<1> ()
    28 {}
    28 {}
    29 Time::Time (Time const &o)
    29 Time::Time (TimeUnit<1> time)
    30   : m_ns (o.m_ns)
    30   : TimeUnit<1> (time)
    31 {}
    31 {}
    32 Time &
       
    33 Time::operator = (Time const &o)
       
    34 {
       
    35   m_ns = o.m_ns;
       
    36   return *this;
       
    37 }
       
    38 Time::Time (int64_t ns)
       
    39   : m_ns (ns)
       
    40 {}
       
    41 
       
    42 
       
    43 bool 
       
    44 Time::IsNegative (void) const
       
    45 {
       
    46   return m_ns <= 0;
       
    47 }
       
    48   
       
    49 bool 
       
    50 Time::IsPositive (void) const
       
    51 {
       
    52   return m_ns >= 0;
       
    53 }
       
    54   
       
    55 bool 
       
    56 Time::IsStrictlyNegative (void) const
       
    57 {
       
    58   return m_ns < 0;
       
    59 }
       
    60 bool 
       
    61 Time::IsStrictlyPositive (void) const
       
    62 {
       
    63   return m_ns > 0;
       
    64 }
       
    65 bool 
       
    66 Time::IsZero (void) const
       
    67 {
       
    68   return m_ns == 0;
       
    69 }
       
    70 
       
    71 Time 
       
    72 Time::operator += (Time const &o)
       
    73 {
       
    74   m_ns += o.m_ns;
       
    75   return *this;
       
    76 }
       
    77   
       
    78 Time 
       
    79 Time::operator -= (Time const &o)
       
    80 {
       
    81   m_ns -= o.m_ns;
       
    82   return *this;
       
    83 }
       
    84 
    32 
    85 double 
    33 double 
    86 Time::ApproximateToSeconds (void) const
    34 Time::ApproximateToSeconds (void) const
    87 {
    35 {
    88   double s = m_ns;
    36   HighPrecision seconds = GetHighPrecision ();
    89   s /= 1000000000;
    37   seconds.Div (HighPrecision (1000000000, 0));
    90   return s;
    38   return seconds.GetDouble ();
    91 }
    39 }
    92 int64_t 
    40 int32_t 
    93 Time::ApproximateToMilliSeconds (void) const
    41 Time::ApproximateToMilliSeconds (void) const
    94 {
    42 {
    95   int64_t ms = m_ns;
    43   HighPrecision ms = GetHighPrecision ();
    96   ms /= 1000000;
    44   ms.Div (HighPrecision (1000000, 0));
    97   return ms;
    45   return (int32_t) ms.GetHigh ();
    98 }
    46 }
    99 int64_t 
    47 int64_t 
   100 Time::ApproximateToMicroSeconds (void) const
    48 Time::ApproximateToMicroSeconds (void) const
   101 {
    49 {
   102   int64_t us = m_ns;
    50   HighPrecision us = GetHighPrecision ();
   103   us /= 1000;
    51   us.Div (HighPrecision (1000, 0));
   104   return us;
    52   return us.GetHigh ();
   105 }
    53 }
   106 int64_t 
    54 int64_t 
   107 Time::ApproximateToNanoSeconds (void) const
    55 Time::ApproximateToNanoSeconds (void) const
   108 {
    56 {
   109   return m_ns;
    57   return GetHighPrecision ().GetHigh ();
   110 }
    58 }
   111 
    59 
   112 
    60 
   113 /* To decrease the number of keystrokes
    61 Seconds::Seconds ()
   114  */
    62   : TimeUnit<1> ()
   115 static uint64_t 
    63 {}
   116 GetNs (Time const &time)
    64 Seconds::Seconds (double seconds)
   117 {
    65   : TimeUnit<1> (HighPrecision (seconds * 1000000000.0))
   118   return time.ApproximateToNanoSeconds ();
    66 {}
   119 }
    67 MilliSeconds::MilliSeconds ()
   120 
    68   : TimeUnit<1> ()
   121 Time Scale (Time const &time, double scale)
    69 {}
   122 {
    70 MilliSeconds::MilliSeconds (uint32_t ms)
   123   return NanoSeconds ((int64_t) (GetNs (time) * scale));
    71   : TimeUnit<1> (HighPrecision (ms * 1000000, 0))
   124 }
    72 {}
   125 Time Abs (Time const &time)
    73 MicroSeconds::MicroSeconds ()
   126 {
    74   : TimeUnit<1> ()
   127   int64_t retval = GetNs (time);
    75 {}
   128   retval = (retval<0)?(-retval):(retval);
    76 MicroSeconds::MicroSeconds (uint32_t us)
   129   return NanoSeconds (retval);
    77   : TimeUnit<1> (HighPrecision (us * 1000, 0))
   130 }
    78 {}
   131 Time Max (Time const &ta, Time const &tb)
    79 NanoSeconds::NanoSeconds ()
   132 {
    80   : TimeUnit<1> ()
   133   int64_t a = GetNs (ta);
    81 {}
   134   int64_t b = GetNs (tb);
    82 NanoSeconds::NanoSeconds (uint32_t ns)
   135   int64_t retval = (a>b)?(a):(b);
    83   : TimeUnit<1> (HighPrecision (ns, 0))
   136   return NanoSeconds (retval);
    84 {}
   137 }
       
   138 Time Min (Time const &ta, Time const &tb)
       
   139 {
       
   140   int64_t a = GetNs (ta);
       
   141   int64_t b = GetNs (tb);
       
   142   int64_t retval = (a<b)?(a):(b);
       
   143   return NanoSeconds (retval);
       
   144 }
       
   145 
       
   146 
       
   147 Time operator + (Time const &lhs, Time const &rhs)
       
   148 {
       
   149   return NanoSeconds (GetNs (lhs) + GetNs (rhs));
       
   150 }  
       
   151 Time operator - (Time const &lhs, Time const &rhs)
       
   152 {
       
   153   return NanoSeconds (GetNs (lhs) - GetNs (rhs));
       
   154 }
       
   155 bool operator == (Time const &lhs, Time const &rhs)
       
   156 {
       
   157   return GetNs (lhs) == GetNs (rhs);
       
   158 }
       
   159 
       
   160 bool operator != (Time const &lhs, Time const &rhs)
       
   161 {
       
   162   return GetNs (lhs) != GetNs (rhs);
       
   163 }
       
   164 bool operator <  (Time const &lhs, Time const &rhs)
       
   165 {
       
   166   return GetNs (lhs) < GetNs (rhs);
       
   167 }
       
   168 bool operator <= (Time const &lhs, Time const &rhs)
       
   169 {
       
   170   return GetNs (lhs) <= GetNs (rhs);
       
   171 }
       
   172 bool operator >  (Time const &lhs, Time const &rhs)
       
   173 {
       
   174   return GetNs (lhs) > GetNs (rhs);
       
   175 }
       
   176 bool operator >= (Time const &lhs, Time const &rhs)
       
   177 {
       
   178   return GetNs (lhs) >= GetNs (rhs);
       
   179 }
       
   180 
    85 
   181 Now::Now ()
    86 Now::Now ()
   182   : Time (Simulator::Now ())
    87   : Time (Simulator::Now ())
   183 {}
    88 {}
   184 Seconds::Seconds (double s)
    89 
   185   : Time ((int64_t)(s * 1000000000))
    90 Scalar::Scalar ()
       
    91   : TimeUnit<0> ()
   186 {}
    92 {}
   187 MilliSeconds::MilliSeconds (int32_t ms)
    93 Scalar::Scalar (TimeUnit<0> scalar)
   188   : Time ((int64_t)(ms * 1000000))
    94   : TimeUnit<0> (scalar)
   189 {}
    95 {}
   190 MicroSeconds::MicroSeconds (int32_t us)
    96 
   191   : Time ((int64_t)(us * 1000))
    97 Scalar::Scalar (double scalar)
       
    98   : TimeUnit<0> (HighPrecision (scalar))
   192 {}
    99 {}
   193 NanoSeconds::NanoSeconds (int64_t ns)
   100 
   194   : Time (ns)
   101 }; // namespace ns3
   195 {}
       
   196   
       
   197 
   102 
   198 
   103 
   199 
       
   200 }; // namespace ns3