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

(-)a/src/simulator/nstime.h (-38 / +162 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
// \class TimeUnit<1>
304
305
template <>
306
class TimeUnit<1>
307
{
308
  // -*- New methods -*-
309
public:
310
311
  /**
312
   * \returns an approximation in seconds of the time stored in this
313
   *          instance.
314
   */
315
  double GetSeconds (void) const;
316
  /**
317
   * \returns an approximation in milliseconds of the time stored in this
318
   *          instance.
319
   */
320
  int32_t GetMilliSeconds (void) const;
321
  /**
322
   * \returns an approximation in microseconds of the time stored in this
323
   *          instance.
324
   */
325
  int64_t GetMicroSeconds (void) const;
326
  /**
327
   * \returns an approximation in nanoseconds of the time stored in this
328
   *          instance.
329
   */
330
  int64_t GetNanoSeconds (void) const;
331
332
  // -*- The rest is the the same as in the generic template class -*-
333
public:
334
  TimeUnit ()
335
    : m_data () 
336
  {}
337
  TimeUnit (TimeUnit const &o)
338
    : m_data (o.m_data) {}
339
  TimeUnit operator = (TimeUnit const &o) {
340
    m_data = o.m_data;
341
    return *this;
342
  }
343
  TimeUnit (HighPrecision data)
344
    : m_data (data) {}
345
  bool IsZero (void) const {
346
    return m_data.Compare (HighPrecision::Zero ()) == 0;
347
  }
348
  bool IsNegative (void) const {
349
    return m_data.Compare (HighPrecision::Zero ()) <= 0;
350
  }
351
  bool IsPositive (void) const {
352
    return m_data.Compare (HighPrecision::Zero ()) >= 0;
353
  }
354
  bool IsStrictlyNegative (void) const {
355
    return m_data.Compare (HighPrecision::Zero ()) < 0;
356
  }
357
  bool IsStrictlyPositive (void) const {
358
    return m_data.Compare (HighPrecision::Zero ()) > 0;
359
  }
360
  HighPrecision const &GetHighPrecision (void) const {
361
    return m_data;
362
  }
363
  HighPrecision *PeekHighPrecision (void) {
364
    return &m_data;
365
  }
366
private:
367
  HighPrecision m_data;
368
};
300
369
301
/**
370
/**
302
 * \brief keep track of seconds.
371
 * \brief keep track of seconds.
 Lines 322-356   TimeUnit<N> Min (TimeUnit<N> const &ta, Link Here 
322
 *  - \ref ns3-Time-Abs ns3::Abs
391
 *  - \ref ns3-Time-Abs ns3::Abs
323
 *  - \ref ns3-Time-Max ns3::Max
392
 *  - \ref ns3-Time-Max ns3::Max
324
 *  - \ref ns3-Time-Min ns3::Min
393
 *  - \ref ns3-Time-Min ns3::Min
325
 */
394
 *
326
class Time : public TimeUnit<1>
395
 * The Time class has the following additional methods not available in
327
{
396
 * the generic TimeUnit template:
328
public:
397
 * 
329
  Time ();
398
 * \code
330
  Time (TimeUnit<1> time);
399
 * double GetSeconds (void) const;
331
  Time (HighPrecision const& value);
400
 * \endcode
332
401
 * returns an approximation in seconds of the time stored in this
333
  /**
402
 *          instance.
334
   * \returns an approximation in seconds of the time stored in this
403
 *
335
   *          instance.
404
 * \code
336
   */
405
 * int32_t GetMilliSeconds (void) const;
337
  double GetSeconds (void) const;
406
 * \endcode
338
  /**
407
 * returns an approximation in milliseconds of the time stored in this
339
   * \returns an approximation in milliseconds of the time stored in this
408
 *          instance.
340
   *          instance.
409
 *
341
   */
410
 * \code
342
  int32_t GetMilliSeconds (void) const;
411
 * int64_t GetMicroSeconds (void) const;
343
  /**
412
 * \endcode
344
   * \returns an approximation in microseconds of the time stored in this
413
 * returns an approximation in microseconds of the time stored in this
345
   *          instance.
414
 *          instance.
346
   */
415
 *
347
  int64_t GetMicroSeconds (void) const;
416
 * \code
348
  /**
417
 * int64_t GetNanoSeconds (void) const;
349
   * \returns an approximation in nanoseconds of the time stored in this
418
 * \endcode
350
   *          instance.
419
 * returns an approximation in nanoseconds of the time stored in this
351
   */
420
 *          instance.
352
  int64_t GetNanoSeconds (void) const;
421
 */
353
};
422
typedef TimeUnit<1> Time;
423
354
424
355
std::ostream& operator<< (std::ostream& os, Time const& time);
425
std::ostream& operator<< (std::ostream& os, Time const& time);
356
426
 Lines 408-413   Time NanoSeconds (uint64_t ns); Link Here 
408
 * \endcode
478
 * \endcode
409
 */
479
 */
410
Time Now (void);
480
Time Now (void);
481
482
483
// Explicit instatiation of the TimeUnit template for N=0, with a few
484
// additional methods that should not be available for N != 0
485
template <>
486
class TimeUnit<0>
487
{
488
  // -*- New methods -*-
489
public:
490
  double GetDouble (void) const;
491
  TimeUnit<0> (double scalar);
492
493
  // -*- The rest is the the same as in the generic template class -*-
494
public:
495
  TimeUnit ()
496
    : m_data () 
497
  {};
498
  TimeUnit (TimeUnit const &o)
499
    : m_data (o.m_data) {}
500
  TimeUnit operator = (TimeUnit const &o) {
501
    m_data = o.m_data;
502
    return *this;
503
  }
504
  TimeUnit (HighPrecision data)
505
    : m_data (data) {}
506
  bool IsZero (void) const {
507
    return m_data.Compare (HighPrecision::Zero ()) == 0;
508
  }
509
  bool IsNegative (void) const {
510
    return m_data.Compare (HighPrecision::Zero ()) <= 0;
511
  }
512
  bool IsPositive (void) const {
513
    return m_data.Compare (HighPrecision::Zero ()) >= 0;
514
  }
515
  bool IsStrictlyNegative (void) const {
516
    return m_data.Compare (HighPrecision::Zero ()) < 0;
517
  }
518
  bool IsStrictlyPositive (void) const {
519
    return m_data.Compare (HighPrecision::Zero ()) > 0;
520
  }
521
  HighPrecision const &GetHighPrecision (void) const {
522
    return m_data;
523
  }
524
  HighPrecision *PeekHighPrecision (void) {
525
    return &m_data;
526
  }
527
private:
528
  HighPrecision m_data;
529
};
411
530
412
/**
531
/**
413
 * \brief hold scalar values
532
 * \brief hold scalar values
 Lines 423-437   Time Now (void); Link Here 
423
 * Scalar s1 = Seconds (15.0) / Seconds (10.0);
542
 * Scalar s1 = Seconds (15.0) / Seconds (10.0);
424
 * std::cout << s1.GetDouble () << std::endl;
543
 * std::cout << s1.GetDouble () << std::endl;
425
 * \endcode
544
 * \endcode
426
 */
545
 *
427
class Scalar : public TimeUnit<0>
546
 * The Scalar class has the following additional methods not available in
428
{
547
 * the generic TimeUnit template:
429
public:
548
 * \code
430
  Scalar ();
549
 * double GetDouble (void) const;
431
  Scalar (double scalar);
550
 * \endcode
432
  Scalar (TimeUnit<0> scalar);
551
 * returns the C double contained in the Scalar instance
433
  double GetDouble (void) const;
552
 *
434
};
553
 * \code
554
 * Scalar(double scalar);
555
 * \endcode
556
 * Constructs a Scalar instance from a C double.
557
 */
558
typedef TimeUnit<0> Scalar;
435
559
436
typedef TimeUnit<-1> TimeInvert;
560
typedef TimeUnit<-1> TimeInvert;
437
typedef TimeUnit<2> TimeSquare;
561
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