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