程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> boost::function 用來注冊函數(switch N多case的解決方案)

boost::function 用來注冊函數(switch N多case的解決方案)

編輯:C++入門知識

[cpp] 
<SPAN style="FONT-FAMILY: Arial, Helvetica, sans-serif">在現在做的游戲開發的項目中,需要根據道具類型調用不同的接口,通常的寫法是用</SPAN> 

在現在做的游戲開發的項目中,需要根據道具類型調用不同的接口,通常的寫法是用[cpp] view plaincopyprint?
switch(itemType) 

 case ...; 
case ...; 



switch(itemType)
{
 case ...;
case ...;
.
.
.
}這樣寫的話,如果將來有新增情況會導致case越來越多 ,不好。

 


正好項目中使用了boost庫,結果老大給了個解決方案:

 

 

[cpp] 
typedef boost::function<void(long long, int)> TypeOnUseItemHandler; // 回調函數返回void 參數一個是long long型,一個是int型  
map<int, TypeOnUseItemHandler> _handlers; // 定義map<key, value>  key是case的條件  value是執行的接口 

 typedef boost::function<void(long long, int)> TypeOnUseItemHandler; // 回調函數返回void 參數一個是long long型,一個是int型
 map<int, TypeOnUseItemHandler> _handlers; // 定義map<key, value>  key是case的條件  value是執行的接口[cpp] view plaincopyprint?
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 注冊  
    _handlers[ItemSpecialAttrId::TimeOfDuration]    = boost::bind(&ItemManager::UseTimeOfDuration, this, _1, _2); 
    _handlers[ItemSpecialAttrId::StorgeHpVolume]    = boost::bind(&ItemManager::UseStorgeHpVolume, this, _1, _2); 
    _handlers[ItemSpecialAttrId::IsGetItem]         = boost::bind(&ItemManager::UseIsGetItem, this, _1, _2); 
    _handlers[ItemSpecialAttrId::ItemBuff]          = boost::bind(&ItemManager::UseItemBuff, this, _1, _2); 
    _handlers[ItemSpecialAttrId::Bind]              = boost::bind(&ItemManager::UseBind, this, _1, _2); 
    _handlers[ItemSpecialAttrId::ReinforceLevel]    = boost::bind(&ItemManager::UseReinforceLevel, this, _1, _2); 
    _handlers[ItemSpecialAttrId::AddPartnerExp]     = boost::bind(&ItemManager::UseAddPartnerExp, this, _1, _2); 
    _handlers[ItemSpecialAttrId::AddRoleExp]        = boost::bind(&ItemManager::UseAddRoleExp, this, _1, _2); 
    _handlers[ItemSpecialAttrId::AddSliver]         = boost::bind(&ItemManager::UseAddSliver, this, _1, _2); 
    _handlers[ItemSpecialAttrId::AddGold]           = boost::bind(&ItemManager::UseAddGold, this, _1, _2); 
    _handlers[ItemSpecialAttrId::AddGoldNote]       = boost::bind(&ItemManager::UseAddGoldNote, this, _1, _2); 
    _handlers[ItemSpecialAttrId::SynthMaterial]     = boost::bind(&ItemManager::UseSynthMaterial, this, _1, _2); 
    _handlers[ItemSpecialAttrId::EnhanceNpcExp]     = boost::bind(&ItemManager::UseEnhanceNpcExp, this, _1, _2); 
    _handlers[ItemSpecialAttrId::GetAttribute]      = boost::bind(&ItemManager::UseGetAttribute, this, _1, _2); 
    _handlers[ItemSpecialAttrId::GainReinforceProp] = boost::bind(&ItemManager::UseGainReinforceProp, this, _1, _2); 

 // 注冊
 _handlers[ItemSpecialAttrId::TimeOfDuration] = boost::bind(&ItemManager::UseTimeOfDuration, this, _1, _2);
 _handlers[ItemSpecialAttrId::StorgeHpVolume] = boost::bind(&ItemManager::UseStorgeHpVolume, this, _1, _2);
 _handlers[ItemSpecialAttrId::IsGetItem]   = boost::bind(&ItemManager::UseIsGetItem, this, _1, _2);
 _handlers[ItemSpecialAttrId::ItemBuff]   = boost::bind(&ItemManager::UseItemBuff, this, _1, _2);
 _handlers[ItemSpecialAttrId::Bind]    = boost::bind(&ItemManager::UseBind, this, _1, _2);
 _handlers[ItemSpecialAttrId::ReinforceLevel] = boost::bind(&ItemManager::UseReinforceLevel, this, _1, _2);
 _handlers[ItemSpecialAttrId::AddPartnerExp]  = boost::bind(&ItemManager::UseAddPartnerExp, this, _1, _2);
 _handlers[ItemSpecialAttrId::AddRoleExp]  = boost::bind(&ItemManager::UseAddRoleExp, this, _1, _2);
 _handlers[ItemSpecialAttrId::AddSliver]   = boost::bind(&ItemManager::UseAddSliver, this, _1, _2);
 _handlers[ItemSpecialAttrId::AddGold]   = boost::bind(&ItemManager::UseAddGold, this, _1, _2);
 _handlers[ItemSpecialAttrId::AddGoldNote]  = boost::bind(&ItemManager::UseAddGoldNote, this, _1, _2);
 _handlers[ItemSpecialAttrId::SynthMaterial]  = boost::bind(&ItemManager::UseSynthMaterial, this, _1, _2);
 _handlers[ItemSpecialAttrId::EnhanceNpcExp]  = boost::bind(&ItemManager::UseEnhanceNpcExp, this, _1, _2);
 _handlers[ItemSpecialAttrId::GetAttribute]  = boost::bind(&ItemManager::UseGetAttribute, this, _1, _2);
 _handlers[ItemSpecialAttrId::GainReinforceProp] = boost::bind(&ItemManager::UseGainReinforceProp, this, _1, _2);

[cpp] 
<SPAN style="WHITE-SPACE: pre"> </SPAN>//<SPAN style="FONT-FAMILY: Arial, Helvetica, sans-serif">specialAttrId 是外部傳遞過來的參數 對應map中的key</SPAN>  
    map<int, TypeOnUseItemHandler>::iterator itFind = _handlers.find(specialAttrId); 
    if (itFind == _handlers.end()) 
        return; 
    if (itFind->second) 
        itFind->second(roleId, specialAttrValue); 

 //specialAttrId 是外部傳遞過來的參數 對應map中的key
 map<int, TypeOnUseItemHandler>::iterator itFind = _handlers.find(specialAttrId);
 if (itFind == _handlers.end())
  return;
 if (itFind->second)
  itFind->second(roleId, specialAttrValue);
這樣寫 通過注冊的方式來調用不同的函數接口,看著舒服多了,也靈活多了, 到有新增的case情況 直接填寫一個:


[cpp] 
_handlers[ItemSpecialAttrId::GainReinforceProp] = boost::bind(&ItemManager::UseGainReinforceProp, this, _1, _2); 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved