I'm using C++0x's lambdas to define boost functions. I have some classes where I define some of their behaviour using lambdas/boost functions. What I was wondering is, is it possible to expose data about the classes into the lambda's context without altering the function definition? For example, I have a set of class like this:
class ConsumableItem : public Item
{
public:
//Constructors here
virtual bool Use(std::set<Character::BaseCharacter*> Users, std::set<Character::BaseCharacter*> Targets);
virtual bool Use(std::set<Battles::BattleCharacter> Users, std::set<Battles::BattleCharacter> Targets, Battles::BattleField &field);
protected:
bool UseRegularFunction;
boost::function<void(std::set<Character::BaseCharacter*>, std::set<Character::BaseCharacter*>)> RegularUse;
bool UseBattleFunction;
boost::function<void(std::set<Battles::BattleCharacter>, std::set<Battles::BattleCharacter>, Battles::BattleField &)> BattleUse;
};
class StatBoostingItem : public ConsumableItem
{
public:
StatBoostingItem(int UID, std::string &name, std::string &descript, boost::unordered_set<ItemFlags> Flags, boost::unordered_map<Stat, int> &boosts, int value);
protected:
virtual bool Use(std::set<Character::BaseCharacter*> Users, std::set<Character::BaseCharacter*> Targets) override;
virtual bool Use(std::set<Battles::BattleCharacter> Users, std::set<Battles::BattleCharacter> Targets, Battles::BattleField &field) override;
private:
boost::unordered_map<Stat, int> StatBoosts;
}
And inside the constructor for stat boosting item, I'd like to be able to do this:
StatBoostingItem::StatBoostingItem(int UID, std::string &name, std::string &descript, boost::unordered_set<ItemFlags> Flags, boost::unordered_map<Stat, int> &boosts, int value)
: ConsumableItem(UID, name, descript, Flags, ItemClass::STAT_BOOSTING_ITEM, value)
{
StatBoosts = boosts;
RegularUse = [](std::set<Character::BaseCharacter*> users, std::set<Character::BaseCharacter*> targets)
{
for (auto character = targets.begin(); character != targets.end(); ++character)
{
for (auto statBoost = StatBoosts.begin(); statBoost != StatBoosts.end(); ++statBoost)
{
//Item Effect in here
}
}
};
}
This is more a curiosity thing as I know I can simply extend the boost::function definition to include a pointer to the item in question and get the data that way, or override my Use functions. But if I could somehow expose information into lambda about the class that contains it, that would be ideal as I'd be able to make use of it elsewhere as well