View | Details | Raw Unified | Return to bug 9
Collapse All | Expand All

(-)a/src/simulator/nstime.h (-36 / +120 lines)
 Lines 297-302   TimeUnit<N> Min (TimeUnit<N> const &ta, Link Here 
297
  HighPrecision b = tb.GetHighPrecision ();  
297
  HighPrecision b = tb.GetHighPrecision ();  
298
  return TimeUnit<N> (Max (a, b));
298
  return TimeUnit<N> (Max (a, b));
299
}
299
}
300
301
// Explicit instatiation of the TimeUnit template for N=1, with a few
302
// additional methods that should not be available for N!=1
303
304
template <>
305
class TimeUnit<1>
306
{
307
  // -*- New methods -*-
308
public:
309
310
  /**
311
   * \returns an approximation in seconds of the time stored in this
312
   *          instance.
313
   */
314
  double GetSeconds (void) const;
315
  /**
316
   * \returns an approximation in milliseconds of the time stored in this
317
   *          instance.
318
   */
319
  int32_t GetMilliSeconds (void) const;
320
  /**
321
   * \returns an approximation in microseconds of the time stored in this
322
   *          instance.
323
   */
324
  int64_t GetMicroSeconds (void) const;
325
  /**
326
   * \returns an approximation in nanoseconds of the time stored in this
327
   *          instance.
328
   */
329
  int64_t GetNanoSeconds (void) const;
330
331
  // -*- The rest is the the same as in the generic template class -*-
332
public:
333
  TimeUnit ()
334
    : m_data () 
335
  {}
336
  TimeUnit (TimeUnit const &o)
337
    : m_data (o.m_data) {}
338
  TimeUnit operator = (TimeUnit const &o) {
339
    m_data = o.m_data;
340
    return *this;
341
  }
342
  TimeUnit (HighPrecision data)
343
    : m_data (data) {}
344
  bool IsZero (void) const {
345
    return m_data.Compare (HighPrecision::Zero ()) == 0;
346
  }
347
  bool IsNegative (void) const {
348
    return m_data.Compare (HighPrecision::Zero ()) <= 0;
349
  }
350
  bool IsPositive (void) const {
351
    return m_data.Compare (HighPrecision::Zero ()) >= 0;
352
  }
353
  bool IsStrictlyNegative (void) const {
354
    return m_data.Compare (HighPrecision::Zero ()) < 0;
355
  }
356
  bool IsStrictlyPositive (void) const {
357
    return m_data.Compare (HighPrecision::Zero ()) > 0;
358
  }
359
  HighPrecision const &GetHighPrecision (void) const {
360
    return m_data;
361
  }
362
  HighPrecision *PeekHighPrecision (void) {
363
    return &m_data;
364
  }
365
private:
366
  HighPrecision m_data;
367
};
300
368
301
/**
369
/**
302
 * \brief keep track of seconds.
370
 * \brief keep track of seconds.
 Lines 323-356   TimeUnit<N> Min (TimeUnit<N> const &ta, Link Here 
323
 *  - \ref ns3-Time-Max ns3::Max
391
 *  - \ref ns3-Time-Max ns3::Max
324
 *  - \ref ns3-Time-Min ns3::Min
392
 *  - \ref ns3-Time-Min ns3::Min
325
 */
393
 */
326
class Time : public TimeUnit<1>
394
typedef TimeUnit<1> Time;
327
{
395
328
public:
329
  Time ();
330
  Time (TimeUnit<1> time);
331
  Time (HighPrecision const& value);
332
333
  /**
334
   * \returns an approximation in seconds of the time stored in this
335
   *          instance.
336
   */
337
  double GetSeconds (void) const;
338
  /**
339
   * \returns an approximation in milliseconds of the time stored in this
340
   *          instance.
341
   */
342
  int32_t GetMilliSeconds (void) const;
343
  /**
344
   * \returns an approximation in microseconds of the time stored in this
345
   *          instance.
346
   */
347
  int64_t GetMicroSeconds (void) const;
348
  /**
349
   * \returns an approximation in nanoseconds of the time stored in this
350
   *          instance.
351
   */
352
  int64_t GetNanoSeconds (void) const;
353
};
354
396
355
std::ostream& operator<< (std::ostream& os, Time const& time);
397
std::ostream& operator<< (std::ostream& os, Time const& time);
356
398
 Lines 408-413   Time NanoSeconds (uint64_t ns); Link Here 
408
 * \endcode
450
 * \endcode
409
 */
451
 */
410
Time Now (void);
452
Time Now (void);
453
454
455
// Explicit instatiation of the TimeUnit template for N=0, with a few
456
// additional methods that should not be available for N != 0
457
template <>
458
class TimeUnit<0>
459
{
460
  // -*- New methods -*-
461
public:
462
  double GetDouble (void) const;
463
  TimeUnit<0> (double scalar);
464
465
  // -*- The rest is the the same as in the generic template class -*-
466
public:
467
  TimeUnit ()
468
    : m_data () 
469
  {};
470
  TimeUnit (TimeUnit const &o)
471
    : m_data (o.m_data) {}
472
  TimeUnit operator = (TimeUnit const &o) {
473
    m_data = o.m_data;
474
    return *this;
475
  }
476
  TimeUnit (HighPrecision data)
477
    : m_data (data) {}
478
  bool IsZero (void) const {
479
    return m_data.Compare (HighPrecision::Zero ()) == 0;
480
  }
481
  bool IsNegative (void) const {
482
    return m_data.Compare (HighPrecision::Zero ()) <= 0;
483
  }
484
  bool IsPositive (void) const {
485
    return m_data.Compare (HighPrecision::Zero ()) >= 0;
486
  }
487
  bool IsStrictlyNegative (void) const {
488
    return m_data.Compare (HighPrecision::Zero ()) < 0;
489
  }
490
  bool IsStrictlyPositive (void) const {
491
    return m_data.Compare (HighPrecision::Zero ()) > 0;
492
  }
493
  HighPrecision const &GetHighPrecision (void) const {
494
    return m_data;
495
  }
496
  HighPrecision *PeekHighPrecision (void) {
497
    return &m_data;
498
  }
499
private:
500
  HighPrecision m_data;
501
};
411
502
412
/**
503
/**
413
 * \brief hold scalar values
504
 * \brief hold scalar values
 Lines 424-437   Time Now (void); Link Here 
424
 * std::cout << s1.GetDouble () << std::endl;
515
 * std::cout << s1.GetDouble () << std::endl;
425
 * \endcode
516
 * \endcode
426
 */
517
 */
427
class Scalar : public TimeUnit<0>
518
typedef TimeUnit<0> Scalar;
428
{
429
public:
430
  Scalar ();
431
  Scalar (double scalar);
432
  Scalar (TimeUnit<0> scalar);
433
  double GetDouble (void) const;
434
};
435
519
436
typedef TimeUnit<-1> TimeInvert;
520
typedef TimeUnit<-1> TimeInvert;
437
typedef TimeUnit<2> TimeSquare;
521
typedef TimeUnit<2> TimeSquare;
(-)a/src/simulator/time.cc (-25 / +14 lines)
 Lines 23-59    Link Here 
23
23
24
namespace ns3 {
24
namespace ns3 {
25
25
26
Time::Time ()
27
  : TimeUnit<1> ()
28
{}
29
Time::Time (TimeUnit<1> time)
30
  : TimeUnit<1> (time)
31
{}
32
Time::Time (HighPrecision const& value)
33
  : TimeUnit<1> (value)
34
{}
35
36
double 
26
double 
37
Time::GetSeconds (void) const
27
TimeUnit<1>::GetSeconds (void) const
38
{
28
{
39
  double ns = GetHighPrecision ().GetDouble ();
29
  double ns = GetHighPrecision ().GetDouble ();
40
  return ns/1000000000.0;
30
  return ns/1000000000.0;
41
}
31
}
42
int32_t 
32
int32_t 
43
Time::GetMilliSeconds (void) const
33
TimeUnit<1>::GetMilliSeconds (void) const
44
{
34
{
45
  int64_t ns = GetHighPrecision ().GetInteger ();
35
  int64_t ns = GetHighPrecision ().GetInteger ();
46
  ns /= 1000000;
36
  ns /= 1000000;
47
  return ns;
37
  return ns;
48
}
38
}
49
int64_t 
39
int64_t 
50
Time::GetMicroSeconds (void) const
40
TimeUnit<1>::GetMicroSeconds (void) const
51
{
41
{
52
  int64_t ns = GetHighPrecision ().GetInteger ();
42
  int64_t ns = GetHighPrecision ().GetInteger ();
53
  return ns/1000;
43
  return ns/1000;
54
}
44
}
55
int64_t 
45
int64_t 
56
Time::GetNanoSeconds (void) const
46
TimeUnit<1>::GetNanoSeconds (void) const
57
{
47
{
58
  return GetHighPrecision ().GetInteger ();
48
  return GetHighPrecision ().GetInteger ();
59
}
49
}
 Lines 86-104   Time Now (void) Link Here 
86
  return Time (Simulator::Now ());
76
  return Time (Simulator::Now ());
87
}
77
}
88
78
89
Scalar::Scalar ()
79
TimeUnit<0>::TimeUnit (double scalar)
90
  : TimeUnit<0> ()
80
  : m_data (HighPrecision (scalar))
91
{}
92
Scalar::Scalar (TimeUnit<0> scalar)
93
  : TimeUnit<0> (scalar)
94
{}
95
96
Scalar::Scalar (double scalar)
97
  : TimeUnit<0> (HighPrecision (scalar))
98
{}
81
{}
99
82
100
double
83
double
101
Scalar::GetDouble (void) const
84
TimeUnit<0>::GetDouble (void) const
102
{
85
{
103
  return GetHighPrecision ().GetDouble ();
86
  return GetHighPrecision ().GetDouble ();
104
}
87
}
 Lines 201-207   bool TimeTests::RunTests (void) Link Here 
201
  //std::cout << "10.0s * 1.5 = " << t4.GetSeconds () << "s" << std::endl;
184
  //std::cout << "10.0s * 1.5 = " << t4.GetSeconds () << "s" << std::endl;
202
  Time t5;
185
  Time t5;
203
  t5 = NanoSeconds (10) * Scalar (1.5);
186
  t5 = NanoSeconds (10) * Scalar (1.5);
204
  //std::cout << "10ns * 1.5 = " << t5.GetNanoSeconds () << "ns" << std::endl;
187
  //std::cout << "10ns * 1.5 = " << t5.GetNanoSeconds () << "ns" <<
188
  //std::endl;
189
190
  double foo = (t1 + t2).GetSeconds ();
191
  foo += (t4 == t5)? 1 : 0;
192
193
  foo = (t1/t2).GetDouble ();
205
194
206
  return ok;
195
  return ok;
207
}
196
}

Return to bug 9