NS3 GlobalValue類API以及使用說明

GlobalValue類

位置:

core/model/global-value.cc

功能:

可從四個地方獲取值:

    1  定義時設(shè)定的初始值
    2  從NS_GLOBAL_VALUE環(huán)境變量
    3 從命令行
    4 可明確調(diào)用SetValue()或者 Bind()函數(shù)

Instances of this class are expected to be allocated as static global variables and should be used to store configurable global state.

該類的實例應該被分配為一個靜態(tài)的全局變量,并且應該被用于存儲配置的全局狀態(tài)。

// source.cc:
static GlobalValue g_myGlobal =
  GlobalValue ("myGlobal", "My global value for ...",
               IntegerValue (12),
               MakeIntegerChecker ());

GlobalValue::GlobalValue (std::string name, std::string help,
                          const AttributeValue &initialValue,
                          Ptr<const AttributeChecker> checker)

GlobalValue 定義包含四個參數(shù):

第一個參數(shù):變量名

第二個參數(shù):該變量的幫助信息,一般說明定義該變量的意義

第三個參數(shù):該變量的初始值,初始值不能為空,否則出錯。

第四個參數(shù):該變量的值的校驗。該值不能為空,否則出錯。這個參數(shù)是為了確保設(shè)置的值是合法的。AttributeChecker有許多子類。

函數(shù):

void ns3::GlobalValue::Bind     (   std::string     name,
        const AttributeValue &      value 
    )   

bool ns3::GlobalValue::BindFailSafe     (   std::string     name,
        const AttributeValue &      value 
    )   

這兩個函數(shù)都是設(shè)定對應的name的value值。
區(qū)別在于第二個函數(shù)返回bool值指示是否設(shè)定成功。第一個函數(shù)如果設(shè)定失敗,直接崩潰。

void ns3::GlobalValue::GetValueByName   (   std::string     name,
        AttributeValue &    value 
    )   

bool ns3::GlobalValue::GetValueByNameFailSafe   (   std::string     name,
        AttributeValue &    value 
    )   

兩個函數(shù)都是根據(jù)name獲取對應的value值。
區(qū)別在于,第一個函數(shù)失敗情況下,會觸發(fā)NS_FATAL_ERROR
第二個函數(shù)返回一個bool值。指示成功與否。

使用

第一步:定義

使用全局變量,首先定義全局變量:

static ns3::GlobalValue g_rngSeed ("RngSeed", 
                                   "The global seed of all rng streams",
                                   ns3::IntegerValue(1),
                                   ns3::MakeIntegerChecker<uint32_t> ());

static ns3::GlobalValue g_rngRun ("RngRun", 
                                  "The substream index used for all streams",
                                  ns3::IntegerValue (1),
                                  ns3::MakeIntegerChecker<int64_t> ());

定義了兩個全局變量:RngSeed和RngRun。

上面的代碼位置/core/model/rng-seed-manager.cc

第二步設(shè)置變量值:

第一種方法:

Config::SetGlobal ("RngSeed", IntegerValue(seed));
}
Config::SetGlobal ("RngRun", IntegerValue (run));

第二種方法:
GlobalValue::Bind ("RngSeed", value);
GlobalValue::BindFailSafe ("RngRun", value);

第三種方法:
設(shè)置環(huán)境變量:
NS_GLOBAL_VALUE='RngSeed=Value;RngRun=OtherValue;'

第四種方法:
命令行方式
./waf --run="source file name --RngSeed=value --RngRun=OtherValue"

第三步:獲取變量值

IntegerValue value
bool GlobalValue::GetValueByNameFailSafe ("RngSeed", value);
void GlobalValue::GetValueByName ("RngRun", value);

獲取全部全局變量:

for (GlobalValue::Iterator i = GlobalValue::Begin (); i != GlobalValue::End (); ++i)
{
      //獲取變量名
      std::stringstream ss;
      ss << "    --" << (*i)->GetName () << "=[";
      
      Ptr<const AttributeChecker> checker = (*i)->GetChecker ();
      
      //獲取變量值
      StringValue v;
      (*i)->GetValue (v);
      ss << v.Get () << "]" << std::endl;

      //獲取變量幫助信息
      ss << "        " << (*i)->GetHelp () << std::endl;
      globals.push_back (ss.str ());
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容