src/core/model/time.cc
author Peter D. Barnes, Jr. <barnes26@llnl.gov>
Wed, 14 Aug 2013 16:55:47 -0700
changeset 10175 d7b91afc71c6
parent 10174 09eca1533421
child 10176 73dfb0c88ed6
permissions -rw-r--r--
Handle showpos bug in gcc libstc++ 4.2.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
150
663120712cd9 fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 144
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
25
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     2
/*
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     3
 * Copyright (c) 2005,2006 INRIA
680
0bbe0e4d73c6 add reference to Emmanuelle in Authors/Copyright
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 679
diff changeset
     4
 * Copyright (c) 2007 Emmanuelle Laprise
25
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     5
 *
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     6
 * This program is free software; you can redistribute it and/or modify
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     7
 * it under the terms of the GNU General Public License version 2 as
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     8
 * published by the Free Software Foundation;
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     9
 *
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    13
 * GNU General Public License for more details.
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    14
 *
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    16
 * along with this program; if not, write to the Free Software
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    18
 *
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    19
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
680
0bbe0e4d73c6 add reference to Emmanuelle in Authors/Copyright
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 679
diff changeset
    20
 * TimeStep support by Emmanuelle Laprise <emmanuelle.laprise@bluekazoo.ca>
25
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    21
 */
679
0c4773ca0f29 avoid cyclic dependency between Simulator and Time code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 678
diff changeset
    22
#include "nstime.h"
7383
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 7169
diff changeset
    23
#include "abort.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 7169
diff changeset
    24
#include "global-value.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 7169
diff changeset
    25
#include "enum.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 7169
diff changeset
    26
#include "string.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 7169
diff changeset
    27
#include "object.h"
c5e131450339 remove ns3/ prefix which is un-needed now that all files are in same module.
Mathieu Lacage <mathieu.lacage@gmail.com>
parents: 7169
diff changeset
    28
#include "config.h"
9195
f0be2daaa38f bug 954: back out changesets 1a2abe07b53d, 8934b7c0c1cb, and 8ef8d8bae350
Tom Henderson <tomh@tomh.org>
parents: 9187
diff changeset
    29
#include "log.h"
9063
32755d0516f4 Bug 1237 - code cleanups related to includes
Vedran Miletić <rivanvx@gmail.com>
parents: 7579
diff changeset
    30
#include <cmath>
10174
09eca1533421 Restore "x.0ns" to result of operator<< (Time)
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10173
diff changeset
    31
#include <iomanip>  // showpos
6511
d9f23a956ef2 add missing include
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6506
diff changeset
    32
#include <sstream>
25
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    33
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    34
NS_LOG_COMPONENT_DEFINE ("Time");
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    35
25
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    36
namespace ns3 {
9b3bb088c560 first cut at george's ideas on api
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    37
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    38
// The set of marked times
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    39
// static
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    40
Time::MarkedTimes * Time::g_markingTimes = 0;
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    41
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
    42
// Function called to force static initialization
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    43
// static
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    44
bool Time::StaticInit ()
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    45
{
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    46
  static bool firstTime = true;
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    47
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    48
  if (firstTime)
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    49
    {
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    50
      if (! g_markingTimes)
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    51
	{
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    52
	  g_markingTimes = new Time::MarkedTimes;
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    53
	}
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
    54
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    55
      // Schedule the cleanup.
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    56
      // We'd really like:
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    57
      //   NS_LOG_LOGIC ("scheduling ClearMarkedTimes()");
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    58
      //   Simulator::Schedule ( Seconds (0), & ClearMarkedTimes);
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    59
      //   [or even better:  Simulator::AtStart ( & ClearMarkedTimes ); ]
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    60
      // But this triggers a static initialization order error,
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    61
      // since the Simulator static initialization may not have occurred.
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    62
      // Instead, we call ClearMarkedTimes directly from Simulator::Run ()
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    63
      firstTime = false;
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    64
    }
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
    65
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    66
  return firstTime;
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    67
}
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    68
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
    69
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
    70
Time::Time (const std::string& s)
372
c242f488ef0b Added back corrected version of TimeUnit(string)
Raj@rajib-bhattacharjeas-computer.local
parents: 367
diff changeset
    71
{
9195
f0be2daaa38f bug 954: back out changesets 1a2abe07b53d, 8934b7c0c1cb, and 8ef8d8bae350
Tom Henderson <tomh@tomh.org>
parents: 9187
diff changeset
    72
  NS_LOG_FUNCTION (this << &s);
7579
91a0ff9ee4eb Bug 1204 - Can't Parse Time +100000000.0ns
Mitch Watrous <watrous@u.washington.edu>
parents: 7383
diff changeset
    73
  std::string::size_type n = s.find_first_not_of ("+-0123456789.");
372
c242f488ef0b Added back corrected version of TimeUnit(string)
Raj@rajib-bhattacharjeas-computer.local
parents: 367
diff changeset
    74
  if (n != std::string::npos)
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
    75
    { // Found non-numeric
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
    76
      std::istringstream iss;
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
    77
      iss.str (s.substr (0, n));
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
    78
      double r;
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
    79
      iss >> r;
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
    80
      std::string trailer = s.substr (n, std::string::npos);
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
    81
      if (trailer == std::string ("s"))
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
    82
        {
6496
ed210e7279a7 rewrite time integer support for 32bit systems.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6418
diff changeset
    83
          *this = Time::FromDouble (r, Time::S);
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
    84
        }
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    85
      else if (trailer == std::string ("ms"))
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
    86
        {
6496
ed210e7279a7 rewrite time integer support for 32bit systems.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6418
diff changeset
    87
          *this = Time::FromDouble (r, Time::MS);
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
    88
        }
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    89
      else if (trailer == std::string ("us"))
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
    90
        {
6496
ed210e7279a7 rewrite time integer support for 32bit systems.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6418
diff changeset
    91
          *this = Time::FromDouble (r, Time::US);
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
    92
        }
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    93
      else if (trailer == std::string ("ns"))
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
    94
        {
6496
ed210e7279a7 rewrite time integer support for 32bit systems.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6418
diff changeset
    95
          *this = Time::FromDouble (r, Time::NS);
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
    96
        }
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
    97
      else if (trailer == std::string ("ps"))
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
    98
        {
6496
ed210e7279a7 rewrite time integer support for 32bit systems.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6418
diff changeset
    99
          *this = Time::FromDouble (r, Time::PS);
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   100
        }
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   101
      else if (trailer == std::string ("fs"))
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   102
        {
6496
ed210e7279a7 rewrite time integer support for 32bit systems.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6418
diff changeset
   103
          *this = Time::FromDouble (r, Time::FS);
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   104
        }
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   105
      else
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   106
        {
10172
22b4d3a7c333 Trim trailing whitespace, doxygen comments
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10171
diff changeset
   107
          NS_ABORT_MSG ("Can't Parse Time " << s);
22b4d3a7c333 Trim trailing whitespace, doxygen comments
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10171
diff changeset
   108
        }
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   109
    }
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   110
  else
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   111
    {
10172
22b4d3a7c333 Trim trailing whitespace, doxygen comments
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10171
diff changeset
   112
      // they didn't provide units, assume seconds
22b4d3a7c333 Trim trailing whitespace, doxygen comments
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10171
diff changeset
   113
      std::istringstream iss;
22b4d3a7c333 Trim trailing whitespace, doxygen comments
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10171
diff changeset
   114
      iss.str (s);
22b4d3a7c333 Trim trailing whitespace, doxygen comments
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10171
diff changeset
   115
      double v;
22b4d3a7c333 Trim trailing whitespace, doxygen comments
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10171
diff changeset
   116
      iss >> v;
22b4d3a7c333 Trim trailing whitespace, doxygen comments
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10171
diff changeset
   117
      *this = Time::FromDouble (v, Time::S);
22b4d3a7c333 Trim trailing whitespace, doxygen comments
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10171
diff changeset
   118
    }
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   119
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   120
  if (g_markingTimes)
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   121
    {
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   122
      Mark (this);
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   123
    }
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   124
}
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   125
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   126
// static
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   127
struct Time::Resolution
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   128
Time::SetDefaultNsResolution (void)
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   129
{
9195
f0be2daaa38f bug 954: back out changesets 1a2abe07b53d, 8934b7c0c1cb, and 8ef8d8bae350
Tom Henderson <tomh@tomh.org>
parents: 9187
diff changeset
   130
  NS_LOG_FUNCTION_NOARGS ();
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   131
  struct Resolution resolution;
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   132
  SetResolution (Time::NS, &resolution, false);
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   133
  return resolution;
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   134
}
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   135
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   136
// static
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   137
void
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   138
Time::SetResolution (enum Unit resolution)
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   139
{
9195
f0be2daaa38f bug 954: back out changesets 1a2abe07b53d, 8934b7c0c1cb, and 8ef8d8bae350
Tom Henderson <tomh@tomh.org>
parents: 9187
diff changeset
   140
  NS_LOG_FUNCTION (resolution);
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   141
  SetResolution (resolution, PeekResolution ());
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   142
}
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   143
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   144
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   145
// static
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   146
void
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   147
Time::SetResolution (enum Unit unit, struct Resolution *resolution,
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   148
                     const bool convert /* = true */)
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   149
{
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   150
  NS_LOG_FUNCTION (resolution);
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   151
  if (convert)
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   152
    { // We have to convert old values
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   153
      ConvertTimes (unit);
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   154
    }
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   155
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   156
  int8_t power [LAST] = { 15, 12, 9, 6, 3, 0};
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   157
  for (int i = 0; i < Time::LAST; i++)
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   158
    {
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   159
      int shift = power[i] - power[(int)unit];
9972
7805771a836a Fix unit conversion of negative Times
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9891
diff changeset
   160
      int64_t factor = (int64_t) std::pow (10, std::fabs (shift));
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   161
      struct Information *info = &resolution->info[i];
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   162
      info->factor = factor;
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   163
      if (shift == 0)
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   164
        {
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   165
          info->timeFrom = int64x64_t (1);
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   166
          info->timeTo = int64x64_t (1);
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   167
          info->toMul = true;
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   168
          info->fromMul = true;
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   169
        }
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   170
      else if (shift > 0)
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   171
        {
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   172
          info->timeFrom = int64x64_t (factor);
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   173
          info->timeTo = int64x64_t::Invert (factor);
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   174
          info->toMul = false;
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   175
          info->fromMul = true;
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   176
        }
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   177
      else
7169
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   178
        {
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   179
          NS_ASSERT (shift < 0);
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   180
          info->timeFrom = int64x64_t::Invert (factor);
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   181
          info->timeTo = int64x64_t (factor);
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   182
          info->toMul = true;
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   183
          info->fromMul = false;
358f71a624d8 core coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7045
diff changeset
   184
        }
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   185
    }
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   186
  resolution->unit = unit;
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   187
}
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   188
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   189
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   190
// static
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   191
void
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   192
Time::ClearMarkedTimes ()
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   193
{
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   194
  NS_LOG_FUNCTION_NOARGS ();
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   195
  if (g_markingTimes)
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   196
    {
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   197
      NS_LOG_LOGIC ("clearing MarkedTimes");
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   198
      delete g_markingTimes;
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   199
      g_markingTimes = 0;
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   200
    }
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   201
}  // Time::ClearMarkedTimes
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   202
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   203
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   204
// static
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   205
void
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   206
Time::Mark (Time * const time)
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   207
{
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   208
  NS_LOG_FUNCTION (time);
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   209
  NS_ASSERT (time != 0);
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   210
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   211
  if (g_markingTimes)
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   212
    {
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   213
      std::pair< MarkedTimes::iterator, bool> ret;
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   214
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   215
      ret = g_markingTimes->insert ( time);
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   216
      NS_LOG_LOGIC ("\t[" << g_markingTimes->size () << "] recording " << time);
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   217
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   218
      if (ret.second == false)
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   219
        {
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   220
          NS_LOG_WARN ("already recorded " << time << "!");
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   221
        }
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   222
   }
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   223
}  // Time::Mark ()
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   224
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   225
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   226
// static
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   227
void
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   228
Time::Clear (Time * const time)
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   229
{
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   230
  NS_LOG_FUNCTION (time);
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   231
  NS_ASSERT (time != 0);
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   232
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   233
  if (g_markingTimes)
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   234
    {
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   235
      NS_ASSERT_MSG (g_markingTimes->count (time) == 1,
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   236
		     "Time object " << time <<
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   237
		     " registered " << g_markingTimes->count (time) <<
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   238
		     " times (should be 1)." );
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   239
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   240
      MarkedTimes::size_type num = g_markingTimes->erase (time);
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   241
      if (num != 1)
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   242
	{
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   243
	  NS_LOG_WARN ("unexpected result erasing " << time << "!");
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   244
	  NS_LOG_WARN ("got " << num << ", expected 1");
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   245
	}
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   246
      else
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   247
	{
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   248
	  NS_LOG_LOGIC ("\t[" << g_markingTimes->size () << "] removing  " << time);
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   249
	}
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   250
    }
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   251
}  // Time::Clear ()
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   252
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   253
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   254
// static
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   255
void
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   256
Time::ConvertTimes (const enum Unit unit)
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   257
{
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   258
  NS_LOG_FUNCTION_NOARGS();
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   259
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   260
  NS_ASSERT_MSG (g_markingTimes != 0,
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   261
		 "No MarkedTimes registry. "
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   262
		 "Time::SetResolution () called more than once?");
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   263
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   264
  for ( MarkedTimes::iterator it = g_markingTimes->begin();
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   265
	it != g_markingTimes->end();
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   266
	it++ )
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   267
    {
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   268
      Time * const tp = *it;
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   269
      if ( ! (    (tp->m_data == std::numeric_limits<int64_t>::min ())
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   270
               || (tp->m_data == std::numeric_limits<int64_t>::max ())
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   271
             )
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   272
         )
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   273
        {
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   274
      tp->m_data = tp->ToInteger (unit);
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   275
    }
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   276
    }
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   277
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   278
  NS_LOG_LOGIC ("logged " << g_markingTimes->size () << " Time objects.");
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   279
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   280
  ClearMarkedTimes ();
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   281
}  // Time::ConvertTimes ()
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   282
10171
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   283
0b3ce943b7c8 bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9972
diff changeset
   284
// static
9195
f0be2daaa38f bug 954: back out changesets 1a2abe07b53d, 8934b7c0c1cb, and 8ef8d8bae350
Tom Henderson <tomh@tomh.org>
parents: 9187
diff changeset
   285
enum Time::Unit
f0be2daaa38f bug 954: back out changesets 1a2abe07b53d, 8934b7c0c1cb, and 8ef8d8bae350
Tom Henderson <tomh@tomh.org>
parents: 9187
diff changeset
   286
Time::GetResolution (void)
9186
8934b7c0c1cb bug 954: avoid segfault upon startup due to recursion in TimeSet code to Simulator::Schedule
Mathieu Lacage <mathieu.lacage@cutebugs.net>
parents: 9183
diff changeset
   287
{
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   288
  // No function log b/c it interferes with operator<<
9195
f0be2daaa38f bug 954: back out changesets 1a2abe07b53d, 8934b7c0c1cb, and 8ef8d8bae350
Tom Henderson <tomh@tomh.org>
parents: 9187
diff changeset
   289
  return PeekResolution ()->unit;
9183
1a2abe07b53d bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9134
diff changeset
   290
}
1a2abe07b53d bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9134
diff changeset
   291
1a2abe07b53d bug 954: Changing the simulation time resolution does not work well with attributes
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9134
diff changeset
   292
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   293
std::ostream&
2409
8744ec60e8e6 convert RandomVariable, Rectangle, Vector, and Time to the new Class Helper
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2396
diff changeset
   294
operator<< (std::ostream& os, const Time & time)
209
8b343b9727d7 fix EventId::IsExpired and Simulator::IsExpired, add EventId::IsRunning, add relevant tests, replace Seconds, MilliSeconds, MicroSeconds, and, NanoSeconds classes by functions named similarly
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 199
diff changeset
   295
{
911
de8102a51fbf append correct unit when printing Time objects
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 684
diff changeset
   296
  std::string unit;
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   297
  Time::Unit res = Time::GetResolution ();
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   298
  switch (res)
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   299
    {
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   300
    case Time::S:
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   301
      unit = "s";
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   302
      break;
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   303
    case Time::MS:
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   304
      unit = "ms";
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   305
      break;
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   306
    case Time::US:
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   307
      unit = "us";
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   308
      break;
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   309
    case Time::NS:
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   310
      unit = "ns";
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   311
      break;
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   312
    case Time::PS:
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   313
      unit = "ps";
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   314
      break;
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   315
    case Time::FS:
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   316
      unit = "fs";
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   317
      break;
6514
92a84235d8f2 get rid of TimeUnit<N>
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6511
diff changeset
   318
    case Time::LAST:
6496
ed210e7279a7 rewrite time integer support for 32bit systems.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6418
diff changeset
   319
      NS_ABORT_MSG ("can't be reached");
ed210e7279a7 rewrite time integer support for 32bit systems.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6418
diff changeset
   320
      unit = "unreachable";
ed210e7279a7 rewrite time integer support for 32bit systems.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6418
diff changeset
   321
      break;
6180
cd0d8ba00e6c coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6177
diff changeset
   322
    }
10173
5cfdaed4d34e Fix bug affecting conversion of Time::Min/Time::Max during Time::SetResolution
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10172
diff changeset
   323
  int64_t v = time.ToInteger (res);
10174
09eca1533421 Restore "x.0ns" to result of operator<< (Time)
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10173
diff changeset
   324
  
09eca1533421 Restore "x.0ns" to result of operator<< (Time)
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10173
diff changeset
   325
  std::ios_base::fmtflags ff = os.flags ();
10175
d7b91afc71c6 Handle showpos bug in gcc libstc++ 4.2.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10174
diff changeset
   326
  { // See bug 1737:  gcc libstc++ 4.2 bug
d7b91afc71c6 Handle showpos bug in gcc libstc++ 4.2.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10174
diff changeset
   327
    if (v == 0)
d7b91afc71c6 Handle showpos bug in gcc libstc++ 4.2.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10174
diff changeset
   328
      { 
d7b91afc71c6 Handle showpos bug in gcc libstc++ 4.2.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10174
diff changeset
   329
	os << '+';
d7b91afc71c6 Handle showpos bug in gcc libstc++ 4.2.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10174
diff changeset
   330
      }
d7b91afc71c6 Handle showpos bug in gcc libstc++ 4.2.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10174
diff changeset
   331
    else
d7b91afc71c6 Handle showpos bug in gcc libstc++ 4.2.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10174
diff changeset
   332
      {
d7b91afc71c6 Handle showpos bug in gcc libstc++ 4.2.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10174
diff changeset
   333
	os << std::showpos;
d7b91afc71c6 Handle showpos bug in gcc libstc++ 4.2.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10174
diff changeset
   334
      }
d7b91afc71c6 Handle showpos bug in gcc libstc++ 4.2.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10174
diff changeset
   335
  }
d7b91afc71c6 Handle showpos bug in gcc libstc++ 4.2.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10174
diff changeset
   336
  os << v << ".0" << unit;
10174
09eca1533421 Restore "x.0ns" to result of operator<< (Time)
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 10173
diff changeset
   337
  os.flags (ff);  // Restore stream flags
209
8b343b9727d7 fix EventId::IsExpired and Simulator::IsExpired, add EventId::IsRunning, add relevant tests, replace Seconds, MilliSeconds, MicroSeconds, and, NanoSeconds classes by functions named similarly
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 199
diff changeset
   338
  return os;
8b343b9727d7 fix EventId::IsExpired and Simulator::IsExpired, add EventId::IsRunning, add relevant tests, replace Seconds, MilliSeconds, MicroSeconds, and, NanoSeconds classes by functions named similarly
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 199
diff changeset
   339
}
2409
8744ec60e8e6 convert RandomVariable, Rectangle, Vector, and Time to the new Class Helper
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2396
diff changeset
   340
std::istream& operator>> (std::istream& is, Time & time)
8744ec60e8e6 convert RandomVariable, Rectangle, Vector, and Time to the new Class Helper
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2396
diff changeset
   341
{
8744ec60e8e6 convert RandomVariable, Rectangle, Vector, and Time to the new Class Helper
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2396
diff changeset
   342
  std::string value;
8744ec60e8e6 convert RandomVariable, Rectangle, Vector, and Time to the new Class Helper
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2396
diff changeset
   343
  is >> value;
6496
ed210e7279a7 rewrite time integer support for 32bit systems.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6418
diff changeset
   344
  time = Time (value);
2409
8744ec60e8e6 convert RandomVariable, Rectangle, Vector, and Time to the new Class Helper
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2396
diff changeset
   345
  return is;
8744ec60e8e6 convert RandomVariable, Rectangle, Vector, and Time to the new Class Helper
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2396
diff changeset
   346
}
143
552ff26b4224 new Time implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 131
diff changeset
   347
2447
eee344085a96 convert to new helpers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2436
diff changeset
   348
ATTRIBUTE_VALUE_IMPLEMENT (Time);
9891
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   349
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   350
Ptr<const AttributeChecker>
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   351
MakeTimeChecker (const Time min, const Time max)
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   352
{
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   353
  NS_LOG_FUNCTION (min << max);
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   354
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   355
  struct Checker : public AttributeChecker
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   356
  {
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   357
    Checker (const Time minValue, const Time maxValue)
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   358
      : m_minValue (minValue),
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   359
        m_maxValue (maxValue) {}
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   360
    virtual bool Check (const AttributeValue &value) const {
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   361
      NS_LOG_FUNCTION (&value);
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   362
      const TimeValue *v = dynamic_cast<const TimeValue *> (&value);
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   363
      if (v == 0)
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   364
        {
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   365
          return false;
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   366
        }
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   367
      return v->Get () >= m_minValue && v->Get () <= m_maxValue;
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   368
    }
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   369
    virtual std::string GetValueTypeName (void) const {
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   370
      NS_LOG_FUNCTION_NOARGS ();
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   371
      return "ns3::TimeValue";
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   372
    }
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   373
    virtual bool HasUnderlyingTypeInformation (void) const {
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   374
      NS_LOG_FUNCTION_NOARGS ();
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   375
      return true;
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   376
    }
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   377
    virtual std::string GetUnderlyingTypeInformation (void) const {
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   378
      NS_LOG_FUNCTION_NOARGS ();
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   379
      std::ostringstream oss;
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   380
      oss << "Time" << " " << m_minValue << ":" << m_maxValue;
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   381
      return oss.str ();
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   382
    }
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   383
    virtual Ptr<AttributeValue> Create (void) const {
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   384
      NS_LOG_FUNCTION_NOARGS ();
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   385
      return ns3::Create<TimeValue> ();
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   386
    }
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   387
    virtual bool Copy (const AttributeValue &source, AttributeValue &destination) const {
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   388
      NS_LOG_FUNCTION (&source << &destination);
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   389
      const TimeValue *src = dynamic_cast<const TimeValue *> (&source);
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   390
      TimeValue *dst = dynamic_cast<TimeValue *> (&destination);
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   391
      if (src == 0 || dst == 0)
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   392
        {
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   393
          return false;
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   394
        }
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   395
      *dst = *src;
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   396
      return true;
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   397
    }
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   398
    Time m_minValue;
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   399
    Time m_maxValue;
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   400
  } *checker = new Checker (min, max);
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   401
  return Ptr<const AttributeChecker> (checker, false);
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   402
}
e12094c8b31b Time attributes with enforced bounds.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9196
diff changeset
   403
2427
9245ec163111 split checker from ParamSpec.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2409
diff changeset
   404
2390
7e6dfae536cd add Value support to Time
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2170
diff changeset
   405
} // namespace ns3
143
552ff26b4224 new Time implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 131
diff changeset
   406