A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
time.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* Copyright (c) 2005,2006 INRIA
4
* Copyright (c) 2007 Emmanuelle Laprise
5
*
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License version 2 as
8
* published by the Free Software Foundation;
9
*
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
14
*
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
*
19
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
20
* TimeStep support by Emmanuelle Laprise <emmanuelle.laprise@bluekazoo.ca>
21
*/
22
#include "
nstime.h
"
23
#include "
abort.h
"
24
#include "
global-value.h
"
25
#include "
enum.h
"
26
#include "
string.h
"
27
#include "
object.h
"
28
#include "
config.h
"
29
#include "
log.h
"
30
#include <cmath>
31
#include <sstream>
32
33
namespace
ns3 {
34
35
NS_LOG_COMPONENT_DEFINE
(
"Time"
);
36
37
Time::Time
(
const
std::string& s)
38
{
39
NS_LOG_FUNCTION
(
this
<< &s);
40
std::string::size_type n = s.find_first_not_of (
"+-0123456789."
);
41
if
(n != std::string::npos)
42
{
// Found non-numeric
43
std::istringstream iss;
44
iss.str (s.substr (0, n));
45
double
r;
46
iss >> r;
47
std::string trailer = s.substr (n, std::string::npos);
48
if
(trailer == std::string (
"s"
))
49
{
50
*
this
=
Time::FromDouble
(r,
Time::S
);
51
return
;
52
}
53
if
(trailer == std::string (
"ms"
))
54
{
55
*
this
=
Time::FromDouble
(r,
Time::MS
);
56
return
;
57
}
58
if
(trailer == std::string (
"us"
))
59
{
60
*
this
=
Time::FromDouble
(r,
Time::US
);
61
return
;
62
}
63
if
(trailer == std::string (
"ns"
))
64
{
65
*
this
=
Time::FromDouble
(r,
Time::NS
);
66
return
;
67
}
68
if
(trailer == std::string (
"ps"
))
69
{
70
*
this
=
Time::FromDouble
(r,
Time::PS
);
71
return
;
72
}
73
if
(trailer == std::string (
"fs"
))
74
{
75
*
this
=
Time::FromDouble
(r,
Time::FS
);
76
return
;
77
}
78
NS_ABORT_MSG
(
"Can't Parse Time "
<< s);
79
}
80
// else
81
// they didn't provide units, assume seconds
82
std::istringstream iss;
83
iss.str (s);
84
double
v;
85
iss >> v;
86
*
this
=
Time::FromDouble
(v,
Time::S
);
87
}
88
89
struct
Time::Resolution
90
Time
::GetNsResolution (void)
91
{
92
NS_LOG_FUNCTION_NOARGS
();
93
struct
Resolution
resolution;
94
SetResolution
(
Time::NS
, &resolution);
95
return
resolution;
96
}
97
void
98
Time::SetResolution
(
enum
Unit
resolution)
99
{
100
NS_LOG_FUNCTION
(resolution);
101
SetResolution
(resolution,
PeekResolution
());
102
}
103
void
104
Time::SetResolution
(
enum
Unit
unit,
struct
Resolution
*resolution)
105
{
106
NS_LOG_FUNCTION
(unit << resolution);
107
int8_t power [
LAST
] = { 15, 12, 9, 6, 3, 0};
108
for
(
int
i = 0; i <
Time::LAST
; i++)
109
{
110
int
shift = power[i] - power[(int)unit];
111
uint64_t factor = (uint64_t) std::pow (10, std::fabs (shift));
112
struct
Information
*info = &resolution->
info
[i];
113
info->
factor
= factor;
114
if
(shift == 0)
115
{
116
info->
timeFrom
= int64x64_t (1);
117
info->
timeTo
= int64x64_t (1);
118
info->
toMul
=
true
;
119
info->
fromMul
=
true
;
120
}
121
else
if
(shift > 0)
122
{
123
info->
timeFrom
= int64x64_t (factor);
124
info->
timeTo
= int64x64_t::Invert (factor);
125
info->
toMul
=
false
;
126
info->
fromMul
=
true
;
127
}
128
else
129
{
130
NS_ASSERT
(shift < 0);
131
info->
timeFrom
= int64x64_t::Invert (factor);
132
info->
timeTo
= int64x64_t (factor);
133
info->
toMul
=
true
;
134
info->
fromMul
=
false
;
135
}
136
}
137
resolution->
unit
= unit;
138
}
139
enum
Time::Unit
140
Time::GetResolution
(
void
)
141
{
142
NS_LOG_FUNCTION_NOARGS
();
143
return
PeekResolution
()->
unit
;
144
}
145
146
147
std::ostream&
148
operator<<
(std::ostream& os,
const
Time
& time)
149
{
150
std::string unit;
151
switch
(
Time::GetResolution
())
152
{
153
case
Time::S
:
154
unit =
"s"
;
155
break
;
156
case
Time::MS
:
157
unit =
"ms"
;
158
break
;
159
case
Time::US
:
160
unit =
"us"
;
161
break
;
162
case
Time::NS
:
163
unit =
"ns"
;
164
break
;
165
case
Time::PS
:
166
unit =
"ps"
;
167
break
;
168
case
Time::FS
:
169
unit =
"fs"
;
170
break
;
171
case
Time::LAST
:
172
NS_ABORT_MSG
(
"can't be reached"
);
173
unit =
"unreachable"
;
174
break
;
175
}
176
int64x64_t v = time;
177
os << v << unit;
178
return
os;
179
}
180
std::istream&
operator>>
(std::istream& is,
Time
& time)
181
{
182
std::string value;
183
is >> value;
184
time =
Time
(value);
185
return
is;
186
}
187
188
ATTRIBUTE_VALUE_IMPLEMENT
(Time);
189
ATTRIBUTE_CHECKER_IMPLEMENT
(Time);
190
191
}
// namespace ns3
192
src
core
model
time.cc
Generated on Fri Dec 21 2012 19:00:33 for ns-3 by
1.8.1.2