src/core/model/time.cc
changeset 9134 7a750f032acd
parent 9063 32755d0516f4
child 9183 1a2abe07b53d
equal deleted inserted replaced
9133:bcf7cef191c1 9134:7a750f032acd
    24 #include "global-value.h"
    24 #include "global-value.h"
    25 #include "enum.h"
    25 #include "enum.h"
    26 #include "string.h"
    26 #include "string.h"
    27 #include "object.h"
    27 #include "object.h"
    28 #include "config.h"
    28 #include "config.h"
       
    29 #include "log.h"
    29 #include <cmath>
    30 #include <cmath>
    30 #include <sstream>
    31 #include <sstream>
    31 
    32 
    32 namespace ns3 {
    33 namespace ns3 {
    33 
    34 
       
    35 NS_LOG_COMPONENT_DEFINE ("Time");
       
    36 
    34 Time::Time (const std::string& s)
    37 Time::Time (const std::string& s)
    35 {
    38 {
       
    39   NS_LOG_FUNCTION (this << &s);
    36   std::string::size_type n = s.find_first_not_of ("+-0123456789.");
    40   std::string::size_type n = s.find_first_not_of ("+-0123456789.");
    37   if (n != std::string::npos)
    41   if (n != std::string::npos)
    38     { // Found non-numeric
    42     { // Found non-numeric
    39       std::istringstream iss;
    43       std::istringstream iss;
    40       iss.str (s.substr (0, n));
    44       iss.str (s.substr (0, n));
    83 }
    87 }
    84 
    88 
    85 struct Time::Resolution
    89 struct Time::Resolution
    86 Time::GetNsResolution (void)
    90 Time::GetNsResolution (void)
    87 {
    91 {
       
    92   NS_LOG_FUNCTION_NOARGS ();
    88   struct Resolution resolution;
    93   struct Resolution resolution;
    89   SetResolution (Time::NS, &resolution);
    94   SetResolution (Time::NS, &resolution);
    90   return resolution;
    95   return resolution;
    91 }
    96 }
    92 void 
    97 void 
    93 Time::SetResolution (enum Unit resolution)
    98 Time::SetResolution (enum Unit resolution)
    94 {
    99 {
       
   100   NS_LOG_FUNCTION (resolution);
    95   SetResolution (resolution, PeekResolution ());
   101   SetResolution (resolution, PeekResolution ());
    96 }
   102 }
    97 void 
   103 void 
    98 Time::SetResolution (enum Unit unit, struct Resolution *resolution)
   104 Time::SetResolution (enum Unit unit, struct Resolution *resolution)
    99 {
   105 {
       
   106   NS_LOG_FUNCTION (unit << resolution);
   100   int8_t power [LAST] = { 15, 12, 9, 6, 3, 0};
   107   int8_t power [LAST] = { 15, 12, 9, 6, 3, 0};
   101   for (int i = 0; i < Time::LAST; i++)
   108   for (int i = 0; i < Time::LAST; i++)
   102     {
   109     {
   103       int shift = power[i] - power[(int)unit];
   110       int shift = power[i] - power[(int)unit];
   104       uint64_t factor = (uint64_t) std::pow (10, std::fabs (shift));
   111       uint64_t factor = (uint64_t) std::pow (10, std::fabs (shift));
   130   resolution->unit = unit;
   137   resolution->unit = unit;
   131 }
   138 }
   132 enum Time::Unit
   139 enum Time::Unit
   133 Time::GetResolution (void)
   140 Time::GetResolution (void)
   134 {
   141 {
       
   142   NS_LOG_FUNCTION_NOARGS ();
   135   return PeekResolution ()->unit;
   143   return PeekResolution ()->unit;
   136 }
   144 }
   137 
   145 
   138 
   146 
   139 std::ostream&
   147 std::ostream&
   140 operator<< (std::ostream& os, const Time & time)
   148 operator<< (std::ostream& os, const Time & time)
   141 {
   149 {
       
   150   NS_LOG_FUNCTION (&os << time);
   142   std::string unit;
   151   std::string unit;
   143   switch (Time::GetResolution ())
   152   switch (Time::GetResolution ())
   144     {
   153     {
   145     case Time::S:
   154     case Time::S:
   146       unit = "s";
   155       unit = "s";
   169   os << v << unit;
   178   os << v << unit;
   170   return os;
   179   return os;
   171 }
   180 }
   172 std::istream& operator>> (std::istream& is, Time & time)
   181 std::istream& operator>> (std::istream& is, Time & time)
   173 {
   182 {
       
   183   NS_LOG_FUNCTION (&is << time);
   174   std::string value;
   184   std::string value;
   175   is >> value;
   185   is >> value;
   176   time = Time (value);
   186   time = Time (value);
   177   return is;
   187   return is;
   178 }
   188 }