解读Time类

来源:互联网 发布:淘宝信用贷款还款 编辑:程序博客网 时间:2024/06/05 10:14
  • 头文件:nstime.h
  • 源文件:time.cc
    这是一个带单位的ns3中的表示时间的类,以定义了+-*/ << >>操作符。

创建时间

都是类的函数

Time From (const int64x64_t & value);Time FromInteger (uint64_t value, enum Unit unit);Time From (const int64x64_t & value, enum Unit unit);Time FromDouble (double value, enum Unit unit);inline Time Years (double value);inline Time Days (double value);inline Time Hours (double value);inline Time Minutes (double value);inline Time Seconds (double value);inline Time MilliSeconds (uint64_t value);inline Time MicroSeconds (uint64_t value);inline Time NanoSeconds (uint64_t value);inline Time PicoSeconds (uint64_t value);inline Time FemtoSeconds (uint64_t value);

使用示例

Time t = Seconds (2.0);Simulator::Schedule (Seconds (5.0), ...);

时间转换

Get**

如GetYears,GetDays等。返回值为double,通过调用ToDouble(单位)实现

To**

inline int64_t ToInteger (enum Unit unit) constinline double ToDouble (enum Unit unit) const

time对象的输入输出

Time:: As

TimeWithUnit As (const enum Unit unit) const;
生成一个TimeWithUnit对象。重定义输出操作符时通过调用该函数实现

<<

调用TimeWithUnit的输出(也就是输出是带单位的)

>>

调用explicit Time (const std::string & s)(只接受类似”1ms”的形式)

数据结构

enum Unit  {    Y   = 0,   //!< year, 365 days    D   = 1,   //!< day, 24 hours    H   = 2,   //!< hour, 60 minutes    MIN = 3,   //!< minute, 60 seconds    S   = 4,   //!< second    MS  = 5,   //!< millisecond    US  = 6,   //!< microsecond    NS  = 7,   //!< nanosecond    PS  = 8,   //!< picosecond    FS  = 9,   //!< femtosecond    LAST = 10  };struct Information  {    bool toMul;                     //!< Multiply when converting To, otherwise divide    bool fromMul;                   //!< Multiple when converting From, otherwise divide    int64_t factor;                 //!< 此单位和当前单位的比率因子。两者之间的较大者比上较小者的比值。    int64x64_t timeTo;              //!< Multiplier to convert to this unit    int64x64_t timeFrom;            //!< Multiplier to convert from this unit  };  /** Current time unit, and conversion info. */  struct Resolution  {    struct Information info[LAST];  //!<  Conversion info from current unit    enum Time::Unit unit;           //!<  Current time unit  };

类型重定义

typedef std::set< Time * > MarkedTimes;

重要成员变量

static MarkedTimes * g_markingTimes;//记录所有time类的实例 int64_t m_data;  //!< Virtual time value, in the current unit.

构造函数

  • 复制控制
    传入参数类型有:Time&,以及赋值操作符=
  • 形参为基本数据类型的构造函数
    int,long int等各种基本数据类型,用于初始化m_data;

- explicit Time (const std::string & s)(只接受类似”1ms”的形式)

析构函数

 ~Time (){    if (g_markingTimes)        Clear (this); }

初始化

  • 静态成员g_markingTimes初始化为0
  • GetMarkingMutex()
SystemMutex &GetMarkingMutex (){  static SystemMutex g_markingMutex;  return g_markingMutex;}
  • Time::StaticInit ()
bool Time::StaticInit (){  static bool firstTime = true;  CriticalSection critical (GetMarkingMutex ());  if(firsttime && !g_markingTimes){  static MarkedTimes markingTimes;  g_markingTimes = & markingTimes;  }  else  NS_LOG_ERROR ();  return (firstTime = false);  }

重要函数

Time::Mark (Time * const time)

根据g_markingMutex创建临界区,在临界区去中将time指向的时间插入g_markingTimes中。

Time::Clear (Time * const time)

根据g_markingMutex创建临界区,在临界区去中从g_markingTimes中删除time

Time:: ConvertTimes

static void ConvertTimes (const enum Unit unit);
创建临界访问区,将g_markingTimes中的所有时间改成目标单位下的值

分辨率相关函数

SetResolution

设置分辨率也就是设置当前使用的时间单位,以及其它单位与该单位相互转换时需要参考的转换信息。(设置Resolution:: info[LAST];)

void Time::SetResolution (enum Unit unit, struct Resolution *resolution, const bool convert /* = true */){if (convert)  ConvertTimes (unit);for (int i = 0; i < Time::LAST; i++)    {    struct Information *info = &resolution->info[i];    info->factor = factor;    if (shift == 0 && quotient == 1)    //info[i]为基准单位到自身的转换参考信息        {          info->timeFrom = int64x64_t (1);          info->timeTo = int64x64_t (1);          info->toMul = true;          info->fromMul = true;        }      else if (realFactor > 1)      //info[i]为比基准单位大的单位与基准单位相互转换的参考信息        {          info->timeFrom = int64x64_t (factor);          info->timeTo = int64x64_t::Invert (factor);          info->toMul = false;          info->fromMul = true;//该单位比基准单位大,因此从此单位转换到基准单位时应该要乘以info->timeFrom。此种情况下factor=realFactor。        }      else        {          NS_ASSERT (realFactor < 1);          info->timeFrom = int64x64_t::Invert (factor);          info->timeTo = int64x64_t (factor);          info->toMul = true;//该单位比基准单位小,因此从基准单位转换到该单位时应该要乘以info->timeto。此种情况下factor=realFactor。          info->fromMul = false;        }}

SetDefaultNsResolution

struct Resolution resolution;SetResolution (Time::NS, &resolution, false);return resolution;static struct Resolution SetDefaultNsResolution (void)

PeekResolution

static inline struct Resolution *PeekResolution (void)  {    static struct Time::Resolution resolution = SetDefaultNsResolution ();//只有该函数被第一次执行的时候才会执行这个句子。实现单例。    return & resolution;  }

SetResolution

voidTime::SetResolution (enum Unit resolution){  NS_LOG_FUNCTION (resolution);  SetResolution (resolution, PeekResolution ());}
inline double ToDouble (enum Unit unit) const  {    return To (unit).GetDouble ();//调用To函数得到一个int64x64_t对象,然后调用该对对象的方法的方法得到一个Double数。  }  inline int64x64_t To (enum Unit unit) const  {    struct Information *info = PeekInformation (unit);    int64x64_t retval = int64x64_t (m_data);    if (info->toMul)        retval *= info->timeTo;    else        retval.MulByInvert (info->timeTo);    return retval;  }

相关函数调用链

ToDouble->To->PeekResolution->SetDefaultNsResolution (void)

读不懂的地方

强制调用初始化函数

宏定义在used.h文件中
static bool NS_UNUSED_GLOBAL (g_TimeStaticInit) = Time::StaticInit ();

Abs

friend Time Abs (const Time & time)
为什么 time.m_data会出现小于零的情况呢?

TimeStep

这个函数的具体作用会是什么呢
inline Time TimeStep (uint64_t ts)
{
return Time (ts);
}

NS_UNUSED_GLOBAL宏

-这个宏和括号中的变量是什么时候定义的

/// Force static initialization of Time,StaticInit函数返回布尔值,所以我猜这个地方是定义一个g_TimeStaticInit全局变量,并让其等于函数返回值static bool NS_UNUSED_GLOBAL (g_TimeStaticInit) = Time::StaticInit ();

测试demo

#include "ns3/nstime.h"#include "iostream"using namespace ns3;using namespace std;int main(){    Time t3;    cin >> t3;//输入支持数值+单位的形式,如1h,2min.支持的单位如下  /**- `s`  (seconds)   * - `ms` (milliseconds)   * - `us` (microseconds)   * - `ns` (nanoseconds)   * - `ps` (picoseconds)   * - `fs` (femtoseconds)   * - `min`  (minutes)   * - `h`  (hours)   * - `d`  (days)   * - `y`  (years)*/    cout << "t3 is:" << t3.As(Time::MIN)<< endl;    t3  = Minutes(3);//令t3值为3分钟    cout << "new value of t3:"<< t3 << endl;    Time t4 = Seconds(60);//令t3值为60s.    /*此外常见的创建时间的函数有Years (double value);Days (double value);Hours (double value);Minutes (double value);Seconds (double value);*/    cout << t4 << endl;    cout << (t3/t4) << endl;//此处得到的结果为3,合乎逻辑    return 0;}
0 0