【分析总结框架记录】boost使用

使用boost向回调函数传值

[html]  view plain copy print ?
  1. Function(boost::bind(&callback,_1,_2,yourParam1, yourParam2)) // _1,_2是占位符,表明callback本来需要的参数,这里对应int a int b 两个  
  2.   
  3. void callback(int a, int b ,param1,param2)  
  4. {  
  5.     .....  
  6. }  

[html]  view plain copy print ?
  1. _game->iServer->iDbServer.GetRoleContainerList(session, roleId,  
  2.     boost::bind(&ContainerManager::OnReturnGetRoleConInfo, this, _1, roleId, onLoadComplete));  

  1. void ContainerManager::OnReturnGetRoleConInfo(Message* message



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

  1. <span style="font-family: Arial, Helvetica, sans-serif;">在现在做的游戏开发的项目中,需要根据道具类型调用不同的接口,通常的写法是用</span>  
  1. switch(itemType)  
  2. {  
  3.  case ...;  
  4. case ...;  
  5. .  
  6. .  
  7. .  
  8. }  

这样写的话,如果将来有新增情况会导致case越来越多 ,不好。

正好项目中使用了boost库,结果老大给了个解决方案:

  1. typedef boost::function<void(long longint)> TypeOnUseItemHandler; // 回调函数返回void 参数一个是long long型,一个是int型  
  2. map<int, TypeOnUseItemHandler> _handlers; // 定义map<key, value>  key是case的条件  value是执行的接口  
  1. <span style="white-space:pre">  </span>// 注册  
  2.     _handlers[ItemSpecialAttrId::TimeOfDuration]    = boost::bind(&ItemManager::UseTimeOfDuration, this, _1, _2);  
  3.     _handlers[ItemSpecialAttrId::StorgeHpVolume]    = boost::bind(&ItemManager::UseStorgeHpVolume, this, _1, _2);  
  4.     _handlers[ItemSpecialAttrId::IsGetItem]         = boost::bind(&ItemManager::UseIsGetItem, this, _1, _2);  
  5.     _handlers[ItemSpecialAttrId::ItemBuff]          = boost::bind(&ItemManager::UseItemBuff, this, _1, _2);  
  6.     _handlers[ItemSpecialAttrId::Bind]              = boost::bind(&ItemManager::UseBind, this, _1, _2);  
  7.     _handlers[ItemSpecialAttrId::ReinforceLevel]    = boost::bind(&ItemManager::UseReinforceLevel, this, _1, _2);  
  8.     _handlers[ItemSpecialAttrId::AddPartnerExp]     = boost::bind(&ItemManager::UseAddPartnerExp, this, _1, _2);  
  9.     _handlers[ItemSpecialAttrId::AddRoleExp]        = boost::bind(&ItemManager::UseAddRoleExp, this, _1, _2);  
  10.     _handlers[ItemSpecialAttrId::AddSliver]         = boost::bind(&ItemManager::UseAddSliver, this, _1, _2);  
  11.     _handlers[ItemSpecialAttrId::AddGold]           = boost::bind(&ItemManager::UseAddGold, this, _1, _2);  
  12.     _handlers[ItemSpecialAttrId::AddGoldNote]       = boost::bind(&ItemManager::UseAddGoldNote, this, _1, _2);  
  13.     _handlers[ItemSpecialAttrId::SynthMaterial]     = boost::bind(&ItemManager::UseSynthMaterial, this, _1, _2);  
  14.     _handlers[ItemSpecialAttrId::EnhanceNpcExp]     = boost::bind(&ItemManager::UseEnhanceNpcExp, this, _1, _2);  
  15.     _handlers[ItemSpecialAttrId::GetAttribute]      = boost::bind(&ItemManager::UseGetAttribute, this, _1, _2);  
  16.     _handlers[ItemSpecialAttrId::GainReinforceProp] = boost::bind(&ItemManager::UseGainReinforceProp, this, _1, _2);  
  1. <span style="white-space:pre">  </span>//<span style="font-family: Arial, Helvetica, sans-serif;">specialAttrId 是外部传递过来的参数 对应map中的key</span>  
  2.     map<int, TypeOnUseItemHandler>::iterator itFind = _handlers.find(specialAttrId);  
  3.     if (itFind == _handlers.end())  
  4.         return;  
  5.     if (itFind->second)  
  6.         itFind->second(roleId, specialAttrValue);  

这样写 通过注册的方式来调用不同的函数接口,看着舒服多了,也灵活多了, 到有新增的case情况 直接填写一个:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值