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

本文介绍了一种使用Boost库中的function和bind功能替代传统switch-case的方法,通过创建函数对象映射来简化代码管理和扩展性,特别适用于处理多种类型的物品使用情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

工作中需要根据不同类型来调用不同函数接口时,通常使用switch case来实现

switch(itemType)  
{  
 case ...;  
case ...;  
.  
.  
.  
} 

不断的添加新的需求会导致case分支很多,

使用boost库解决方案:

typedef boost::function<void(long, int)> TypeOnUseItemHandler; // 回调函数返回void 参数一个是long long型,一个是int型  
map<int, TypeOnUseItemHandler> _handlers; // 定义map<key, value>  key是case的条件  value是执行的接口 

_handlers[ItemSpecialAttrId::TimeOfDuration]    = boost::bind(&ItemManager::UseTimeOfDuration, this, boost::lambda::_1, boost::lambda::_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);  

    map<int, TypeOnUseItemHandler>::iterator itFind = _handlers.find(specialAttrId);//switch 中的判断分支条件  
    if (itFind == _handlers.end())  
        return;  
	if (itFind->second) 
	{
		TypeOnUseItemHandler = itFind->second(roleId, specialAttrValue);  
		TypeOnUseItemHandler(10.000,10);
	}


这样写 通过注册的方式来调用不同的函数接口,方便,灵活, 如果有新增的case情况 直接填写一个:

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



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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值