A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
nstime.h
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  *
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 "unused.h"
28 #include <stdint.h>
29 #include <limits>
30 #include <cmath>
31 #include <ostream>
32 #include <set>
33 
34 namespace ns3 {
35 
81 class Time
82 {
83 public:
87  enum Unit
88  {
89  S = 0,
90  MS = 1,
91  US = 2,
92  NS = 3,
93  PS = 4,
94  FS = 5,
95  LAST = 6
96  };
97 
98  inline Time &operator = (const Time &o)
99  {
100  m_data = o.m_data;
101  return *this;
102  }
103  inline Time ()
104  : m_data ()
105  {
106  if (g_markingTimes)
107  {
108  Mark (this);
109  }
110  }
111  inline Time(const Time &o)
112  : m_data (o.m_data)
113  {
114  if (g_markingTimes)
115  {
116  Mark (this);
117  }
118  }
119  explicit inline Time (double v)
120  : m_data (lround (v))
121  {
122  if (g_markingTimes)
123  {
124  Mark (this);
125  }
126  }
127  explicit inline Time (int v)
128  : m_data (v)
129  {
130  if (g_markingTimes)
131  {
132  Mark (this);
133  }
134  }
135  explicit inline Time (long int v)
136  : m_data (v)
137  {
138  if (g_markingTimes)
139  {
140  Mark (this);
141  }
142  }
143  explicit inline Time (long long int v)
144  : m_data (v)
145  {
146  if (g_markingTimes)
147  {
148  Mark (this);
149  }
150  }
151  explicit inline Time (unsigned int v)
152  : m_data (v)
153  {
154  if (g_markingTimes)
155  {
156  Mark (this);
157  }
158  }
159  explicit inline Time (unsigned long int v)
160  : m_data (v)
161  {
162  if (g_markingTimes)
163  {
164  Mark (this);
165  }
166  }
167  explicit inline Time (unsigned long long int v)
168  : m_data (v)
169  {
170  if (g_markingTimes)
171  {
172  Mark (this);
173  }
174  }
191  explicit Time (const std::string & s);
192 
196  static Time Min ()
197  {
198  return Time (std::numeric_limits<int64_t>::min ());
199  }
203  static Time Max ()
204  {
205  return Time (std::numeric_limits<int64_t>::max ());
206  }
207 
211  ~Time ()
212  {
213  if (g_markingTimes)
214  {
215  Clear (this);
216  }
217  }
218 
222  inline bool IsZero (void) const
223  {
224  return m_data == 0;
225  }
229  inline bool IsNegative (void) const
230  {
231  return m_data <= 0;
232  }
236  inline bool IsPositive (void) const
237  {
238  return m_data >= 0;
239  }
243  inline bool IsStrictlyNegative (void) const
244  {
245  return m_data < 0;
246  }
250  inline bool IsStrictlyPositive (void) const
251  {
252  return m_data > 0;
253  }
257  inline int Compare (const Time &o) const
258  {
259  return (m_data < o.m_data) ? -1 : (m_data == o.m_data) ? 0 : 1;
260  }
261 
266  inline double GetSeconds (void) const
267  {
268  return ToDouble (Time::S);
269  }
270 
275  inline int64_t GetMilliSeconds (void) const
276  {
277  return ToInteger (Time::MS);
278  }
283  inline int64_t GetMicroSeconds (void) const
284  {
285  return ToInteger (Time::US);
286  }
291  inline int64_t GetNanoSeconds (void) const
292  {
293  return ToInteger (Time::NS);
294  }
299  inline int64_t GetPicoSeconds (void) const
300  {
301  return ToInteger (Time::PS);
302  }
307  inline int64_t GetFemtoSeconds (void) const
308  {
309  return ToInteger (Time::FS);
310  }
314  inline int64_t GetTimeStep (void) const
315  {
316  return m_data;
317  }
318  inline double GetDouble (void) const
319  {
320  return m_data;
321  }
322  inline int64_t GetInteger (void) const
323  {
324  return GetTimeStep ();
325  }
326 
327 
335  static void SetResolution (enum Unit resolution);
339  static enum Unit GetResolution (void);
350  inline static Time FromInteger (uint64_t value, enum Unit timeUnit)
351  {
352  struct Information *info = PeekInformation (timeUnit);
353  if (info->fromMul)
354  {
355  value *= info->factor;
356  }
357  else
358  {
359  value /= info->factor;
360  }
361  return Time (value);
362  }
370  inline int64_t ToInteger (enum Unit timeUnit) const
371  {
372  struct Information *info = PeekInformation (timeUnit);
373  int64_t v = m_data;
374  if (info->toMul)
375  {
376  v *= info->factor;
377  }
378  else
379  {
380  v /= info->factor;
381  }
382  return v;
383  }
391  inline static Time FromDouble (double value, enum Unit timeUnit)
392  {
393  return From (int64x64_t (value), timeUnit);
394  }
402  inline double ToDouble (enum Unit timeUnit) const
403  {
404  return To (timeUnit).GetDouble ();
405  }
406  static inline Time From (const int64x64_t &from, enum Unit timeUnit)
407  {
408  struct Information *info = PeekInformation (timeUnit);
409  // DO NOT REMOVE this temporary variable. It's here
410  // to work around a compiler bug in gcc 3.4
411  int64x64_t retval = from;
412  if (info->fromMul)
413  {
414  retval *= info->timeFrom;
415  }
416  else
417  {
418  retval.MulByInvert (info->timeFrom);
419  }
420  return Time (retval);
421  }
422  inline int64x64_t To (enum Unit timeUnit) const
423  {
424  struct Information *info = PeekInformation (timeUnit);
425  int64x64_t retval = int64x64_t (m_data);
426  if (info->toMul)
427  {
428  retval *= info->timeTo;
429  }
430  else
431  {
432  retval.MulByInvert (info->timeTo);
433  }
434  return retval;
435  }
436  inline operator int64x64_t () const
437  {
438  return int64x64_t (m_data);
439  }
440  explicit inline Time (const int64x64_t &value)
441  : m_data (value.GetHigh ())
442  {
443  if (g_markingTimes)
444  {
445  Mark (this);
446  }
447  }
448  inline static Time From (const int64x64_t &value)
449  {
450  return Time (value);
451  }
452 
453 private:
457  struct Information
458  {
459  bool toMul;
460  bool fromMul;
461  int64_t factor;
462  int64x64_t timeTo;
463  int64x64_t timeFrom;
464  };
468  struct Resolution
469  {
472  };
473 
474  static inline struct Resolution *PeekResolution (void)
475  {
476  static struct Time::Resolution resolution = SetDefaultNsResolution ();
477  return &resolution;
478  }
479  static inline struct Information *PeekInformation (enum Unit timeUnit)
480  {
481  return &(PeekResolution ()->info[timeUnit]);
482  }
483 
484  static struct Resolution SetDefaultNsResolution (void);
485  static void SetResolution (enum Unit unit, struct Resolution *resolution,
486  const bool convert = true);
487 
507  typedef std::set< Time * > MarkedTimes;
523 public:
527  static bool StaticInit ();
528 private:
529 
530  /* Friend the Simulator class so it can call the private function
531  ClearMarkedTimes ()
532  */
533  friend class Simulator;
540  static void ClearMarkedTimes ();
544  static void Mark (Time * const time);
548  static void Clear (Time * const time);
552  static void ConvertTimes (const enum Unit unit);
553 
554  friend bool operator == (const Time &lhs, const Time &rhs);
555  friend bool operator != (const Time &lhs, const Time &rhs);
556  friend bool operator <= (const Time &lhs, const Time &rhs);
557  friend bool operator >= (const Time &lhs, const Time &rhs);
558  friend bool operator < (const Time &lhs, const Time &rhs);
559  friend bool operator > (const Time &lhs, const Time &rhs);
560  friend Time operator + (const Time &lhs, const Time &rhs);
561  friend Time operator - (const Time &lhs, const Time &rhs);
562  friend Time &operator += (Time &lhs, const Time &rhs);
563  friend Time &operator -= (Time &lhs, const Time &rhs);
564  friend Time Abs (const Time &time);
565  friend Time Max (const Time &ta, const Time &tb);
566  friend Time Min (const Time &ta, const Time &tb);
567 
568 
569  int64_t m_data;
570 
571 }; // class Time
572 
573 
574 // Force static initialization of Time
575 static bool NS_UNUSED_GLOBAL (g_TimeStaticInit) = Time::StaticInit ();
576 
577 inline bool
578 operator == (const Time &lhs, const Time &rhs)
579 {
580  return lhs.m_data == rhs.m_data;
581 }
582 inline bool
583 operator != (const Time &lhs, const Time &rhs)
584 {
585  return lhs.m_data != rhs.m_data;
586 }
587 inline bool
588 operator <= (const Time &lhs, const Time &rhs)
589 {
590  return lhs.m_data <= rhs.m_data;
591 }
592 inline bool
593 operator >= (const Time &lhs, const Time &rhs)
594 {
595  return lhs.m_data >= rhs.m_data;
596 }
597 inline bool
598 operator < (const Time &lhs, const Time &rhs)
599 {
600  return lhs.m_data < rhs.m_data;
601 }
602 inline bool
603 operator > (const Time &lhs, const Time &rhs)
604 {
605  return lhs.m_data > rhs.m_data;
606 }
607 inline Time operator + (const Time &lhs, const Time &rhs)
608 {
609  return Time (lhs.m_data + rhs.m_data);
610 }
611 inline Time operator - (const Time &lhs, const Time &rhs)
612 {
613  return Time (lhs.m_data - rhs.m_data);
614 }
615 inline Time &operator += (Time &lhs, const Time &rhs)
616 {
617  lhs.m_data += rhs.m_data;
618  return lhs;
619 }
620 inline Time &operator -= (Time &lhs, const Time &rhs)
621 {
622  lhs.m_data -= rhs.m_data;
623  return lhs;
624 }
625 
633 inline Time Abs (const Time &time)
634 {
635  return Time ((time.m_data < 0) ? -time.m_data : time.m_data);
636 }
644 inline Time Max (const Time &ta, const Time &tb)
645 {
646  return Time ((ta.m_data < tb.m_data) ? tb : ta);
647 }
653 inline Time Min (const Time &ta, const Time &tb)
654 {
655  return Time ((ta.m_data > tb.m_data) ? tb : ta);
656 }
657 
658 
665 std::ostream& operator<< (std::ostream& os, const Time & time);
672 std::istream& operator>> (std::istream& is, Time & time);
673 
685 inline Time Seconds (double seconds)
686 {
687  return Time::FromDouble (seconds, Time::S);
688 }
689 
701 inline Time MilliSeconds (uint64_t ms)
702 {
703  return Time::FromInteger (ms, Time::MS);
704 }
716 inline Time MicroSeconds (uint64_t us)
717 {
718  return Time::FromInteger (us, Time::US);
719 }
731 inline Time NanoSeconds (uint64_t ns)
732 {
733  return Time::FromInteger (ns, Time::NS);
734 }
746 inline Time PicoSeconds (uint64_t ps)
747 {
748  return Time::FromInteger (ps, Time::PS);
749 }
761 inline Time FemtoSeconds (uint64_t fs)
762 {
763  return Time::FromInteger (fs, Time::FS);
764 }
765 
766 
771 inline Time Seconds (int64x64_t seconds)
772 {
773  return Time::From (seconds, Time::S);
774 }
779 inline Time MilliSeconds (int64x64_t ms)
780 {
781  return Time::From (ms, Time::MS);
782 }
787 inline Time MicroSeconds (int64x64_t us)
788 {
789  return Time::From (us, Time::US);
790 }
795 inline Time NanoSeconds (int64x64_t ns)
796 {
797  return Time::From (ns, Time::NS);
798 }
803 inline Time PicoSeconds (int64x64_t ps)
804 {
805  return Time::From (ps, Time::PS);
806 }
811 inline Time FemtoSeconds (int64x64_t fs)
812 {
813  return Time::From (fs, Time::FS);
814 }
815 
816 // internal function not publicly documented
817 inline Time TimeStep (uint64_t ts)
818 {
819  return Time (ts);
820 }
821 
830 
838 
844 inline
846 {
847  return MakeTimeChecker (Time::Min (), Time::Max ());
848 }
849 
855 inline
857 {
858  return MakeTimeChecker (min, Time::Max ());
859 }
860 
861 
862 } // namespace ns3
863 
864 #endif /* TIME_H */
static struct Resolution * PeekResolution(void)
Definition: nstime.h:474
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:685
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:49
nanosecond
Definition: nstime.h:92
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
#define ATTRIBUTE_VALUE_DEFINE(type)
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
bool IsPositive(void) const
Definition: nstime.h:236
microsecond
Definition: nstime.h:91
int64x64_t Abs(const int64x64_t &value)
Definition: int64x64.h:85
Time(const int64x64_t &value)
Definition: nstime.h:440
Time MilliSeconds(uint64_t ms)
create ns3::Time instances in units of milliseconds.
Definition: nstime.h:701
double ToDouble(enum Unit timeUnit) const
Definition: nstime.h:402
Control the scheduling of simulation events.
Definition: simulator.h:62
Time MilliSeconds(int64x64_t ms)
Definition: nstime.h:779
int64x64_t timeFrom
Multiplier to convert from this unit.
Definition: nstime.h:463
static Time Min()
Minimum representable Time.
Definition: nstime.h:196
Time TimeStep(uint64_t ts)
Definition: nstime.h:817
Time PicoSeconds(uint64_t ps)
create ns3::Time instances in units of picoseconds.
Definition: nstime.h:746
Time FemtoSeconds(int64x64_t fs)
Definition: nstime.h:811
bool IsZero(void) const
Definition: nstime.h:222
friend bool operator<(const Time &lhs, const Time &rhs)
Definition: nstime.h:598
friend bool operator!=(const Time &lhs, const Time &rhs)
Definition: nstime.h:583
int64_t ToInteger(enum Unit timeUnit) const
Definition: nstime.h:370
bool operator>(const Time &lhs, const Time &rhs)
Definition: nstime.h:603
static Time From(const int64x64_t &value)
Definition: nstime.h:448
bool operator<(const Room &a, const Room &b)
int64_t GetFemtoSeconds(void) const
Definition: nstime.h:307
Time NanoSeconds(int64x64_t ns)
Definition: nstime.h:795
Time(unsigned long int v)
Definition: nstime.h:159
Time FemtoSeconds(uint64_t fs)
create ns3::Time instances in units of femtoseconds.
Definition: nstime.h:761
int Compare(const Time &o) const
Definition: nstime.h:257
Time Seconds(int64x64_t seconds)
Definition: nstime.h:771
Time MicroSeconds(int64x64_t us)
Definition: nstime.h:787
friend Time operator-(const Time &lhs, const Time &rhs)
Definition: nstime.h:611
static Time FromDouble(double value, enum Unit timeUnit)
Definition: nstime.h:391
struct Information info[LAST]
Conversion info from current unit.
Definition: nstime.h:470
static bool NS_UNUSED_GLOBAL(g_TimeStaticInit)
friend Time operator+(const Time &lhs, const Time &rhs)
Definition: nstime.h:607
static Time Max()
Maximum representable Time.
Definition: nstime.h:203
friend Time Abs(const Time &time)
double GetSeconds(void) const
Definition: nstime.h:266
Time & operator+=(Time &lhs, const Time &rhs)
Definition: nstime.h:615
Time(int v)
Definition: nstime.h:127
picosecond
Definition: nstime.h:93
static enum Unit GetResolution(void)
Definition: time.cc:339
int64_t GetMicroSeconds(void) const
Definition: nstime.h:283
friend Time & operator-=(Time &lhs, const Time &rhs)
Definition: nstime.h:620
Time(long long int v)
Definition: nstime.h:143
static bool StaticInit()
Definition: time.cc:58
Time MicroSeconds(uint64_t us)
create ns3::Time instances in units of microseconds.
Definition: nstime.h:716
bool IsStrictlyPositive(void) const
Definition: nstime.h:250
bool fromMul
Multiple when converting From, otherwise divide.
Definition: nstime.h:460
Time NanoSeconds(uint64_t ns)
create ns3::Time instances in units of nanoseconds.
Definition: nstime.h:731
Ptr< SampleEmitter > s
static void ConvertTimes(const enum Unit unit)
Definition: time.cc:302
Time PicoSeconds(int64x64_t ps)
Definition: nstime.h:803
friend bool operator>=(const Time &lhs, const Time &rhs)
Definition: nstime.h:593
Time operator+(const Time &lhs, const Time &rhs)
Definition: nstime.h:607
int64x64_t Min(const int64x64_t &a, const int64x64_t &b)
Definition: int64x64.h:90
ATTRIBUTE_ACCESSOR_DEFINE(Boolean)
Time(long int v)
Definition: nstime.h:135
static struct Resolution SetDefaultNsResolution(void)
Definition: time.cc:149
int64_t factor
Ratio of this unit / current unit.
Definition: nstime.h:461
int64x64_t Max(const int64x64_t &a, const int64x64_t &b)
Definition: int64x64.h:95
bool operator<=(const Time &lhs, const Time &rhs)
Definition: nstime.h:588
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:43
Time & operator-=(Time &lhs, const Time &rhs)
Definition: nstime.h:620
Time(double v)
Definition: nstime.h:119
bool operator!=(Callback< R, T1, T2, T3, T4, T5, T6, T7, T8, T9 > a, Callback< R, T1, T2, T3, T4, T5, T6, T7, T8, T9 > b)
Definition: callback.h:1213
Time(unsigned long long int v)
Definition: nstime.h:167
bool operator>=(const Time &lhs, const Time &rhs)
Definition: nstime.h:593
friend Time & operator+=(Time &lhs, const Time &rhs)
Definition: nstime.h:615
friend bool operator==(const Time &lhs, const Time &rhs)
Definition: nstime.h:578
bool IsNegative(void) const
Definition: nstime.h:229
int64_t GetTimeStep(void) const
Definition: nstime.h:314
Time()
Definition: nstime.h:103
int64_t GetInteger(void) const
Definition: nstime.h:322
Time operator-(const Time &lhs, const Time &rhs)
Definition: nstime.h:611
~Time()
Definition: nstime.h:211
double GetDouble(void) const
Definition: nstime.h:318
static struct Information * PeekInformation(enum Unit timeUnit)
Definition: nstime.h:479
int64_t GetNanoSeconds(void) const
Definition: nstime.h:291
Time(unsigned int v)
Definition: nstime.h:151
static void Mark(Time *const time)
Definition: time.cc:246
friend bool operator>(const Time &lhs, const Time &rhs)
Definition: nstime.h:603
static Time FromInteger(uint64_t value, enum Unit timeUnit)
Definition: nstime.h:350
int64_t GetPicoSeconds(void) const
Definition: nstime.h:299
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.cc:89
enum Time::Unit unit
Current time unit.
Definition: nstime.h:471
bool IsStrictlyNegative(void) const
Definition: nstime.h:243
static void SetResolution(enum Unit resolution)
Definition: time.cc:159
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range. Both limits are inclusive.
Definition: time.cc:404
static void Clear(Time *const time)
Definition: time.cc:272
second
Definition: nstime.h:89
std::set< Time * > MarkedTimes
Definition: nstime.h:507
static MarkedTimes * g_markingTimes
Definition: nstime.h:522
static void ClearMarkedTimes()
Definition: time.cc:215
femtosecond
Definition: nstime.h:94
millisecond
Definition: nstime.h:90
friend bool operator<=(const Time &lhs, const Time &rhs)
Definition: nstime.h:588
int64_t GetMilliSeconds(void) const
Definition: nstime.h:275
Time & operator=(const Time &o)
Definition: nstime.h:98
static Time From(const int64x64_t &from, enum Unit timeUnit)
Definition: nstime.h:406
int64x64_t timeTo
Multiplier to convert to this unit.
Definition: nstime.h:462
Time(const Time &o)
Definition: nstime.h:111
int64_t m_data
Virtual time value, in the current unit.
Definition: nstime.h:569
bool toMul
Multiply when converting To, otherwise divide.
Definition: nstime.h:459
int64x64_t To(enum Unit timeUnit) const
Definition: nstime.h:422