Boost: function is used to register a function (switch N multi-case solution)

Source: Internet
Author: User

[Cpp]
<SPAN style = "FONT-FAMILY: Arial, Helvetica, sans-serif"> in the current game development project, you need to call different interfaces based on the item type, generally, </SPAN>

In the current game development project, different interfaces need to be called according to the item type. The common method is to use [cpp] view plaincopyprint?
Switch (itemType)
{
Case ...;
Case ...;
.
.
.
}

Switch (itemType)
{
Case ...;
Case ...;
.
.
.
} In this case, it will be difficult to add more cases in the future.

 


The boost library was used in the project, and the boss gave a solution:

 

 

[Cpp]
Typedef boost: function <void (long, int)> TypeOnUseItemHandler; // The callback function returns the void parameter. One is long and the other is int.
Map <int, TypeOnUseItemHandler> _ handlers; // defines map <key, value> key as the condition of case. value is the execution interface.

Typedef boost: function <void (long, int)> TypeOnUseItemHandler; // The callback function returns the void parameter. One is long and the other is int.
Map <int, TypeOnUseItemHandler> _ handlers; // defines map <key, value> key as the condition of case. value is the execution interface [cpp] view plaincopyprint?
<SPAN style = "WHITE-SPACE: pre"> </SPAN> // register
_ 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 );

// Register
_ 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 is the key in the map corresponding to the externally passed parameter </SPAN>
Map <int, TypeOnUseItemHandler>: iterator itFind = _ handlers. find (specialAttrId );
If (itFind = _ handlers. end ())
Return;
If (itFind-> second)
ItFind-> second (roleId, specialAttrValue );

// SpecialAttrId is the key of the map mapped to the externally passed Parameter
Map <int, TypeOnUseItemHandler>: iterator itFind = _ handlers. find (specialAttrId );
If (itFind = _ handlers. end ())
Return;
If (itFind-> second)
ItFind-> second (roleId, specialAttrValue );
In this way, the registration method is used to call different function interfaces. It is much more comfortable and flexible. When there is a new case, enter one directly:


[Cpp]
_ Handlers [ItemSpecialAttrId: GainReinforceProp] = boost: bind (& ItemManager: UseGainReinforceProp, this, _ 1, _ 2 );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.