A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
nstime.h
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005,2006 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #ifndef TIME_H
21 #define TIME_H
22 
23 #include "assert.h"
24 #include "attribute.h"
25 #include "attribute-helper.h"
26 #include "int64x64.h"
27 #include <stdint.h>
28 #include <cmath>
29 #include <ostream>
30 
31 namespace ns3 {
32 
149 class Time
150 {
151 public:
155  enum Unit
156  {
157  S = 0,
158  MS = 1,
159  US = 2,
160  NS = 3,
161  PS = 4,
162  FS = 5,
163  LAST = 6
164  };
165 
166  inline Time &operator = (const Time &o)
167  {
168  m_data = o.m_data;
169  return *this;
170  }
171  inline Time ()
172  : m_data ()
173  {}
174  inline Time(const Time &o)
175  : m_data (o.m_data)
176  {}
177  explicit inline Time (double v)
178  : m_data (lround (v))
179  {}
180  explicit inline Time (int v)
181  : m_data (v)
182  {}
183  explicit inline Time (long int v)
184  : m_data (v)
185  {}
186  explicit inline Time (long long int v)
187  : m_data (v)
188  {}
189  explicit inline Time (unsigned int v)
190  : m_data (v)
191  {}
192  explicit inline Time (unsigned long int v)
193  : m_data (v)
194  {}
195  explicit inline Time (unsigned long long int v)
196  : m_data (v)
197  {}
198 
215  explicit Time (const std::string & s);
216 
220  inline bool IsZero (void) const
221  {
222  return m_data == 0;
223  }
227  inline bool IsNegative (void) const
228  {
229  return m_data <= 0;
230  }
234  inline bool IsPositive (void) const
235  {
236  return m_data >= 0;
237  }
241  inline bool IsStrictlyNegative (void) const
242  {
243  return m_data < 0;
244  }
248  inline bool IsStrictlyPositive (void) const
249  {
250  return m_data > 0;
251  }
252 
253  inline int Compare (const Time &o) const
254  {
255  return (m_data < o.m_data) ? -1 : (m_data == o.m_data) ? 0 : 1;
256  }
257 
262  inline double GetSeconds (void) const
263  {
264  return ToDouble (Time::S);
265  }
266 
271  inline int64_t GetMilliSeconds (void) const
272  {
273  return ToInteger (Time::MS);
274  }
279  inline int64_t GetMicroSeconds (void) const
280  {
281  return ToInteger (Time::US);
282  }
287  inline int64_t GetNanoSeconds (void) const
288  {
289  return ToInteger (Time::NS);
290  }
295  inline int64_t GetPicoSeconds (void) const
296  {
297  return ToInteger (Time::PS);
298  }
303  inline int64_t GetFemtoSeconds (void) const
304  {
305  return ToInteger (Time::FS);
306  }
311  inline int64_t GetTimeStep (void) const
312  {
313  return m_data;
314  }
315  inline double GetDouble (void) const
316  {
317  return m_data;
318  }
319  inline int64_t GetInteger (void) const
320  {
321  return GetTimeStep ();
322  }
323 
324 
332  static void SetResolution (enum Unit resolution);
336  static enum Unit GetResolution (void);
347  inline static Time FromInteger (uint64_t value, enum Unit timeUnit)
348  {
349  struct Information *info = PeekInformation (timeUnit);
350  if (info->fromMul)
351  {
352  value *= info->factor;
353  }
354  else
355  {
356  value /= info->factor;
357  }
358  return Time (value);
359  }
367  inline int64_t ToInteger (enum Unit timeUnit) const
368  {
369  struct Information *info = PeekInformation (timeUnit);
370  int64_t v = m_data;
371  if (info->toMul)
372  {
373  v *= info->factor;
374  }
375  else
376  {
377  v /= info->factor;
378  }
379  return v;
380  }
388  inline static Time FromDouble (double value, enum Unit timeUnit)
389  {
390  return From (int64x64_t (value), timeUnit);
391  }
399  inline double ToDouble (enum Unit timeUnit) const
400  {
401  return To (timeUnit).GetDouble ();
402  }
403  static inline Time From (const int64x64_t &from, enum Unit timeUnit)
404  {
405  struct Information *info = PeekInformation (timeUnit);
406  // DO NOT REMOVE this temporary variable. It's here
407  // to work around a compiler bug in gcc 3.4
408  int64x64_t retval = from;
409  if (info->fromMul)
410  {
411  retval *= info->timeFrom;
412  }
413  else
414  {
415  retval.MulByInvert (info->timeFrom);
416  }
417  return Time (retval);
418  }
419  inline int64x64_t To (enum Unit timeUnit) const
420  {
421  struct Information *info = PeekInformation (timeUnit);
422  int64x64_t retval = int64x64_t (m_data);
423  if (info->toMul)
424  {
425  retval *= info->timeTo;
426  }
427  else
428  {
429  retval.MulByInvert (info->timeTo);
430  }
431  return retval;
432  }
433  inline operator int64x64_t () const
434  {
435  return int64x64_t (m_data);
436  }
437  explicit inline Time (const int64x64_t &value)
438  : m_data (value.GetHigh ())
439  {}
440  inline static Time From (const int64x64_t &value)
441  {
442  return Time (value);
443  }
444 
445 private:
446  struct Information
447  {
448  bool toMul;
449  bool fromMul;
450  uint64_t factor;
451  int64x64_t timeTo;
452  int64x64_t timeFrom;
453  };
454  struct Resolution
455  {
456  struct Information info[LAST];
457  enum Time::Unit unit;
458  };
459 
460  static inline struct Resolution *PeekResolution (void)
461  {
462  static struct Time::Resolution resolution = GetNsResolution ();
463  return &resolution;
464  }
465  static inline struct Information *PeekInformation (enum Unit timeUnit)
466  {
467  return &(PeekResolution ()->info[timeUnit]);
468  }
469 
470  static struct Resolution GetNsResolution (void);
471  static void SetResolution (enum Unit unit, struct Resolution *resolution);
472 
473  friend bool operator == (const Time &lhs, const Time &rhs);
474  friend bool operator != (const Time &lhs, const Time &rhs);
475  friend bool operator <= (const Time &lhs, const Time &rhs);
476  friend bool operator >= (const Time &lhs, const Time &rhs);
477  friend bool operator < (const Time &lhs, const Time &rhs);
478  friend bool operator > (const Time &lhs, const Time &rhs);
479  friend Time operator + (const Time &lhs, const Time &rhs);
480  friend Time operator - (const Time &lhs, const Time &rhs);
481  friend Time &operator += (Time &lhs, const Time &rhs);
482  friend Time &operator -= (Time &lhs, const Time &rhs);
483  friend Time Abs (const Time &time);
484  friend Time Max (const Time &ta, const Time &tb);
485  friend Time Min (const Time &ta, const Time &tb);
486 
487  int64_t m_data;
488 };
489 
490 inline bool
491 operator == (const Time &lhs, const Time &rhs)
492 {
493  return lhs.m_data == rhs.m_data;
494 }
495 inline bool
496 operator != (const Time &lhs, const Time &rhs)
497 {
498  return lhs.m_data != rhs.m_data;
499 }
500 inline bool
501 operator <= (const Time &lhs, const Time &rhs)
502 {
503  return lhs.m_data <= rhs.m_data;
504 }
505 inline bool
506 operator >= (const Time &lhs, const Time &rhs)
507 {
508  return lhs.m_data >= rhs.m_data;
509 }
510 inline bool
511 operator < (const Time &lhs, const Time &rhs)
512 {
513  return lhs.m_data < rhs.m_data;
514 }
515 inline bool
516 operator > (const Time &lhs, const Time &rhs)
517 {
518  return lhs.m_data > rhs.m_data;
519 }
520 inline Time operator + (const Time &lhs, const Time &rhs)
521 {
522  return Time (lhs.m_data + rhs.m_data);
523 }
524 inline Time operator - (const Time &lhs, const Time &rhs)
525 {
526  return Time (lhs.m_data - rhs.m_data);
527 }
528 inline Time &operator += (Time &lhs, const Time &rhs)
529 {
530  lhs.m_data += rhs.m_data;
531  return lhs;
532 }
533 inline Time &operator -= (Time &lhs, const Time &rhs)
534 {
535  lhs.m_data -= rhs.m_data;
536  return lhs;
537 }
538 
545 inline Time Abs (const Time &time)
546 {
547  return Time ((time.m_data < 0) ? -time.m_data : time.m_data);
548 }
556 inline Time Max (const Time &ta, const Time &tb)
557 {
558  return Time ((ta.m_data < tb.m_data) ? tb : ta);
559 }
567 inline Time Min (const Time &ta, const Time &tb)
568 {
569  return Time ((ta.m_data > tb.m_data) ? tb : ta);
570 }
571 
572 
573 std::ostream& operator<< (std::ostream& os, const Time & time);
574 std::istream& operator>> (std::istream& is, Time & time);
575 
586 inline Time Seconds (double seconds)
587 {
588  return Time::FromDouble (seconds, Time::S);
589 }
590 
601 inline Time MilliSeconds (uint64_t ms)
602 {
603  return Time::FromInteger (ms, Time::MS);
604 }
615 inline Time MicroSeconds (uint64_t us)
616 {
617  return Time::FromInteger (us, Time::US);
618 }
629 inline Time NanoSeconds (uint64_t ns)
630 {
631  return Time::FromInteger (ns, Time::NS);
632 }
643 inline Time PicoSeconds (uint64_t ps)
644 {
645  return Time::FromInteger (ps, Time::PS);
646 }
657 inline Time FemtoSeconds (uint64_t fs)
658 {
659  return Time::FromInteger (fs, Time::FS);
660 }
661 
662 
663 inline Time Seconds (int64x64_t seconds)
664 {
665  return Time::From (seconds, Time::S);
666 }
667 inline Time MilliSeconds (int64x64_t ms)
668 {
669  return Time::From (ms, Time::MS);
670 }
671 inline Time MicroSeconds (int64x64_t us)
672 {
673  return Time::From (us, Time::US);
674 }
675 inline Time NanoSeconds (int64x64_t ns)
676 {
677  return Time::From (ns, Time::NS);
678 }
679 inline Time PicoSeconds (int64x64_t ps)
680 {
681  return Time::From (ps, Time::PS);
682 }
683 inline Time FemtoSeconds (int64x64_t fs)
684 {
685  return Time::From (fs, Time::FS);
686 }
687 
688 // internal function not publicly documented
689 inline Time TimeStep (uint64_t ts)
690 {
691  return Time (ts);
692 }
693 
703 
704 } // namespace ns3
705 
706 #endif /* TIME_H */
Time NanoSeconds(uint64_t ns)
create ns3::Time instances in units of nanoseconds.
Definition: nstime.h:629
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:49
keep track of time unit.
Definition: nstime.h:149
bool IsPositive(void) const
Definition: nstime.h:234
double ToDouble(enum Unit timeUnit) const
Definition: nstime.h:399
bool IsZero(void) const
Definition: nstime.h:220
int64_t ToInteger(enum Unit timeUnit) const
Definition: nstime.h:367
int64_t GetFemtoSeconds(void) const
Definition: nstime.h:303
Time PicoSeconds(uint64_t ps)
create ns3::Time instances in units of picoseconds.
Definition: nstime.h:643
static Time FromDouble(double value, enum Unit timeUnit)
Definition: nstime.h:388
double GetSeconds(void) const
Definition: nstime.h:262
static enum Unit GetResolution(void)
Definition: time.cc:140
int64_t GetMicroSeconds(void) const
Definition: nstime.h:279
bool IsStrictlyPositive(void) const
Definition: nstime.h:248
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:43
bool IsNegative(void) const
Definition: nstime.h:227
#define ATTRIBUTE_CHECKER_DEFINE(type)
#define ATTRIBUTE_VALUE_DEFINE(type)
int64_t GetTimeStep(void) const
Definition: nstime.h:311
Time FemtoSeconds(uint64_t fs)
create ns3::Time instances in units of femtoseconds.
Definition: nstime.h:657
int64_t GetNanoSeconds(void) const
Definition: nstime.h:287
static Time FromInteger(uint64_t value, enum Unit timeUnit)
Definition: nstime.h:347
int64_t GetPicoSeconds(void) const
Definition: nstime.h:295
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
#define ATTRIBUTE_ACCESSOR_DEFINE(type)
bool IsStrictlyNegative(void) const
Definition: nstime.h:241
static void SetResolution(enum Unit resolution)
Definition: time.cc:98
Time MilliSeconds(uint64_t ms)
create ns3::Time instances in units of milliseconds.
Definition: nstime.h:601
int64_t GetMilliSeconds(void) const
Definition: nstime.h:271
Time MicroSeconds(uint64_t us)
create ns3::Time instances in units of microseconds.
Definition: nstime.h:615