当前位置:编程学习 > C/C++ >>

boost::function 用来注册函数(switch N多case的解决方案)

[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); 

补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,