1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

IBonusBearer: split cached methods

This commit is contained in:
Konstantin P 2023-05-05 20:39:57 +03:00
parent f2827834c0
commit 8600e3035a
2 changed files with 47 additions and 28 deletions

View File

@ -15,44 +15,21 @@
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
int IBonusBearer::valOfBonuses(BonusType type, std::optional<int> subtype) const
{
//This part is performance-critical
std::string cachingStr = "type_" + std::to_string(static_cast<int>(type)) + (subtype ? "_" + std::to_string(*subtype) : "");
CSelector s = Selector::type()(type);
if(subtype)
s = s.And(Selector::subtype()(*subtype));
return valOfBonuses(s, cachingStr);
}
int IBonusBearer::valOfBonuses(const CSelector &selector, const std::string &cachingStr) const int IBonusBearer::valOfBonuses(const CSelector &selector, const std::string &cachingStr) const
{ {
TConstBonusListPtr hlp = getAllBonuses(selector, nullptr, nullptr, cachingStr); TConstBonusListPtr hlp = getAllBonuses(selector, nullptr, nullptr, cachingStr);
return hlp->totalValue(); return hlp->totalValue();
} }
bool IBonusBearer::hasBonus(const CSelector &selector, const std::string &cachingStr) const bool IBonusBearer::hasBonus(const CSelector &selector, const std::string &cachingStr) const
{ {
//TODO: We don't need to count all bonuses and could break on first matching //TODO: We don't need to count all bonuses and could break on first matching
return getBonuses(selector, cachingStr)->size() > 0; return !getBonuses(selector, cachingStr)->empty();
} }
bool IBonusBearer::hasBonus(const CSelector &selector, const CSelector &limit, const std::string &cachingStr) const bool IBonusBearer::hasBonus(const CSelector &selector, const CSelector &limit, const std::string &cachingStr) const
{ {
return getBonuses(selector, limit, cachingStr)->size() > 0; return !getBonuses(selector, limit, cachingStr)->empty();
}
bool IBonusBearer::hasBonusOfType(BonusType type, std::optional<int> subtype) const
{
//This part is performance-ciritcal
std::string cachingStr = "type_" + std::to_string(static_cast<int>(type)) + (subtype ? "_" + std::to_string(*subtype) : "");
CSelector s = Selector::type()(type);
if(subtype)
s = s.And(Selector::subtype()(*subtype));
return hasBonus(s, cachingStr);
} }
TConstBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const std::string &cachingStr) const TConstBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const std::string &cachingStr) const
@ -65,6 +42,46 @@ TConstBonusListPtr IBonusBearer::getBonuses(const CSelector &selector, const CSe
return getAllBonuses(selector, limit, nullptr, cachingStr); return getAllBonuses(selector, limit, nullptr, cachingStr);
} }
int IBonusBearer::valOfBonuses(BonusType type) const
{
//This part is performance-critical
std::string cachingStr = "type_" + std::to_string(static_cast<int>(type));
CSelector s = Selector::type()(type);
return valOfBonuses(s, cachingStr);
}
bool IBonusBearer::hasBonusOfType(BonusType type) const
{
//This part is performance-critical
std::string cachingStr = "type_" + std::to_string(static_cast<int>(type));
CSelector s = Selector::type()(type);
return hasBonus(s, cachingStr);
}
int IBonusBearer::valOfBonuses(BonusType type, int subtype) const
{
//This part is performance-critical
std::string cachingStr = "type_" + std::to_string(static_cast<int>(type)) + "_" + std::to_string(subtype);
CSelector s = Selector::typeSubtype(type, subtype);
return valOfBonuses(s, cachingStr);
}
bool IBonusBearer::hasBonusOfType(BonusType type, int subtype) const
{
//This part is performance-critical
std::string cachingStr = "type_" + std::to_string(static_cast<int>(type)) + "_" + std::to_string(subtype);
CSelector s = Selector::typeSubtype(type, subtype);
return hasBonus(s, cachingStr);
}
bool IBonusBearer::hasBonusFrom(BonusSource source, ui32 sourceID) const bool IBonusBearer::hasBonusFrom(BonusSource source, ui32 sourceID) const
{ {
boost::format fmt("source_%did_%d"); boost::format fmt("source_%did_%d");

View File

@ -33,8 +33,10 @@ public:
std::shared_ptr<const Bonus> getBonus(const CSelector &selector) const; //returns any bonus visible on node that matches (or nullptr if none matches) std::shared_ptr<const Bonus> getBonus(const CSelector &selector) const; //returns any bonus visible on node that matches (or nullptr if none matches)
//Optimized interface (with auto-caching) //Optimized interface (with auto-caching)
int valOfBonuses(BonusType type, std::optional<int> subtype = std::nullopt) const; //subtype -> subtype of bonus; int valOfBonuses(BonusType type) const; //subtype -> subtype of bonus;
bool hasBonusOfType(BonusType type, std::optional<int> subtype = std::nullopt) const;//determines if hero has a bonus of given type (and optionally subtype) bool hasBonusOfType(BonusType type) const;//determines if hero has a bonus of given type (and optionally subtype)
int valOfBonuses(BonusType type, int subtype) const; //subtype -> subtype of bonus;
bool hasBonusOfType(BonusType type, int subtype) const;//determines if hero has a bonus of given type (and optionally subtype)
bool hasBonusFrom(BonusSource source, ui32 sourceID) const; bool hasBonusFrom(BonusSource source, ui32 sourceID) const;
virtual int64_t getTreeVersion() const = 0; virtual int64_t getTreeVersion() const = 0;