Porting more ContainerStoreIterator usage to const version #4

removed const_cast revision
This commit is contained in:
Rafael Moura 2017-02-28 14:43:20 +00:00
parent 7fa2703715
commit 6c2ce2b2a1
6 changed files with 39 additions and 33 deletions

View file

@ -765,8 +765,8 @@ bool NpcAnimation::addOrReplaceIndividualPart(ESM::PartReferenceType type, int g
if (!mSoundsDisabled) if (!mSoundsDisabled)
{ {
MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr); const MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr);
MWWorld::ContainerStoreIterator csi = inv.getSlot(group < 0 ? MWWorld::InventoryStore::Slot_Helmet : group); MWWorld::ConstContainerStoreIterator csi = inv.getSlot(group < 0 ? MWWorld::InventoryStore::Slot_Helmet : group);
if (csi != inv.end()) if (csi != inv.end())
{ {
mSoundIds[type] = csi->getClass().getSound(*csi); mSoundIds[type] = csi->getClass().getSound(*csi);
@ -899,8 +899,8 @@ void NpcAnimation::showWeapons(bool showWeapon)
mShowWeapons = showWeapon; mShowWeapons = showWeapon;
if(showWeapon) if(showWeapon)
{ {
MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr); const MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr);
MWWorld::ContainerStoreIterator weapon = inv.getSlot(MWWorld::InventoryStore::Slot_CarriedRight); MWWorld::ConstContainerStoreIterator weapon = inv.getSlot(MWWorld::InventoryStore::Slot_CarriedRight);
if(weapon != inv.end()) if(weapon != inv.end())
{ {
osg::Vec4f glowColor = getEnchantmentColor(*weapon); osg::Vec4f glowColor = getEnchantmentColor(*weapon);
@ -912,7 +912,7 @@ void NpcAnimation::showWeapons(bool showWeapon)
if (weapon->getTypeName() == typeid(ESM::Weapon).name() && if (weapon->getTypeName() == typeid(ESM::Weapon).name() &&
weapon->get<ESM::Weapon>()->mBase->mData.mType == ESM::Weapon::MarksmanCrossbow) weapon->get<ESM::Weapon>()->mBase->mData.mType == ESM::Weapon::MarksmanCrossbow)
{ {
MWWorld::ContainerStoreIterator ammo = inv.getSlot(MWWorld::InventoryStore::Slot_Ammunition); MWWorld::ConstContainerStoreIterator ammo = inv.getSlot(MWWorld::InventoryStore::Slot_Ammunition);
if (ammo != inv.end() && ammo->get<ESM::Weapon>()->mBase->mData.mType == ESM::Weapon::Bolt) if (ammo != inv.end() && ammo->get<ESM::Weapon>()->mBase->mData.mType == ESM::Weapon::Bolt)
attachArrow(); attachArrow();
else else
@ -931,8 +931,8 @@ void NpcAnimation::showWeapons(bool showWeapon)
void NpcAnimation::showCarriedLeft(bool show) void NpcAnimation::showCarriedLeft(bool show)
{ {
mShowCarriedLeft = show; mShowCarriedLeft = show;
MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr); const MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr);
MWWorld::ContainerStoreIterator iter = inv.getSlot(MWWorld::InventoryStore::Slot_CarriedLeft); MWWorld::ConstContainerStoreIterator iter = inv.getSlot(MWWorld::InventoryStore::Slot_CarriedLeft);
if(show && iter != inv.end()) if(show && iter != inv.end())
{ {
osg::Vec4f glowColor = getEnchantmentColor(*iter); osg::Vec4f glowColor = getEnchantmentColor(*iter);

View file

@ -56,8 +56,8 @@ WeaponAnimation::~WeaponAnimation()
void WeaponAnimation::attachArrow(MWWorld::Ptr actor) void WeaponAnimation::attachArrow(MWWorld::Ptr actor)
{ {
MWWorld::InventoryStore& inv = actor.getClass().getInventoryStore(actor); const MWWorld::InventoryStore& inv = actor.getClass().getInventoryStore(actor);
MWWorld::ContainerStoreIterator weaponSlot = inv.getSlot(MWWorld::InventoryStore::Slot_CarriedRight); MWWorld::ConstContainerStoreIterator weaponSlot = inv.getSlot(MWWorld::InventoryStore::Slot_CarriedRight);
if (weaponSlot == inv.end()) if (weaponSlot == inv.end())
return; return;
if (weaponSlot->getTypeName() != typeid(ESM::Weapon).name()) if (weaponSlot->getTypeName() != typeid(ESM::Weapon).name())
@ -79,7 +79,7 @@ void WeaponAnimation::attachArrow(MWWorld::Ptr actor)
if (!parent) if (!parent)
return; return;
MWWorld::ContainerStoreIterator ammo = inv.getSlot(MWWorld::InventoryStore::Slot_Ammunition); MWWorld::ConstContainerStoreIterator ammo = inv.getSlot(MWWorld::InventoryStore::Slot_Ammunition);
if (ammo == inv.end()) if (ammo == inv.end())
return; return;
std::string model = ammo->getClass().getModel(*ammo); std::string model = ammo->getClass().getModel(*ammo);

View file

@ -256,9 +256,9 @@ namespace MWScript
throw std::runtime_error ("armor index out of range"); throw std::runtime_error ("armor index out of range");
} }
MWWorld::InventoryStore& invStore = ptr.getClass().getInventoryStore (ptr); const MWWorld::InventoryStore& invStore = ptr.getClass().getInventoryStore (ptr);
MWWorld::ConstContainerStoreIterator it = invStore.getSlot (slot);
MWWorld::ContainerStoreIterator it = invStore.getSlot (slot);
if (it == invStore.end() || it->getTypeName () != typeid(ESM::Armor).name()) if (it == invStore.end() || it->getTypeName () != typeid(ESM::Armor).name())
{ {
runtime.push(-1); runtime.push(-1);
@ -289,10 +289,10 @@ namespace MWScript
std::string item = runtime.getStringLiteral (runtime[0].mInteger); std::string item = runtime.getStringLiteral (runtime[0].mInteger);
runtime.pop(); runtime.pop();
MWWorld::InventoryStore& invStore = ptr.getClass().getInventoryStore (ptr); const MWWorld::InventoryStore& invStore = ptr.getClass().getInventoryStore (ptr);
for (int slot = 0; slot < MWWorld::InventoryStore::Slots; ++slot) for (int slot = 0; slot < MWWorld::InventoryStore::Slots; ++slot)
{ {
MWWorld::ContainerStoreIterator it = invStore.getSlot (slot); MWWorld::ConstContainerStoreIterator it = invStore.getSlot (slot);
if (it != invStore.end() && ::Misc::StringUtils::ciEqual(it->getCellRef().getRefId(), item)) if (it != invStore.end() && ::Misc::StringUtils::ciEqual(it->getCellRef().getRefId(), item))
{ {
runtime.push(1); runtime.push(1);
@ -336,8 +336,8 @@ namespace MWScript
{ {
MWWorld::Ptr ptr = R()(runtime); MWWorld::Ptr ptr = R()(runtime);
MWWorld::InventoryStore& invStore = ptr.getClass().getInventoryStore (ptr); const MWWorld::InventoryStore& invStore = ptr.getClass().getInventoryStore (ptr);
MWWorld::ContainerStoreIterator it = invStore.getSlot (MWWorld::InventoryStore::Slot_CarriedRight); MWWorld::ConstContainerStoreIterator it = invStore.getSlot (MWWorld::InventoryStore::Slot_CarriedRight);
if (it == invStore.end() || it->getTypeName () != typeid(ESM::Weapon).name()) if (it == invStore.end() || it->getTypeName () != typeid(ESM::Weapon).name())
{ {
runtime.push(-1); runtime.push(-1);

View file

@ -519,7 +519,7 @@ namespace MWScript
int numNotEquipped = invStorePtr->count(item); int numNotEquipped = invStorePtr->count(item);
for (int slot = 0; slot < MWWorld::InventoryStore::Slots; ++slot) for (int slot = 0; slot < MWWorld::InventoryStore::Slots; ++slot)
{ {
MWWorld::ContainerStoreIterator it = invStorePtr->getSlot (slot); MWWorld::ConstContainerStoreIterator it = invStorePtr->getSlot (slot);
if (it != invStorePtr->end() && ::Misc::StringUtils::ciEqual(it->getCellRef().getRefId(), item)) if (it != invStorePtr->end() && ::Misc::StringUtils::ciEqual(it->getCellRef().getRefId(), item))
{ {
numNotEquipped -= it->getRefData().getCount(); numNotEquipped -= it->getRefData().getCount();

View file

@ -199,25 +199,12 @@ void MWWorld::InventoryStore::unequipAll(const MWWorld::Ptr& actor)
MWWorld::ContainerStoreIterator MWWorld::InventoryStore::getSlot (int slot) MWWorld::ContainerStoreIterator MWWorld::InventoryStore::getSlot (int slot)
{ {
if (slot<0 || slot>=static_cast<int> (mSlots.size())) return findSlot (slot);
throw std::runtime_error ("slot number out of range");
if (mSlots[slot]==end())
return end();
if (mSlots[slot]->getRefData().getCount()<1)
{
// Object has been deleted
// This should no longer happen, since the new remove function will unequip first
throw std::runtime_error("Invalid slot, make sure you are not calling RefData::setCount for a container object");
}
return mSlots[slot];
} }
MWWorld::ConstContainerStoreIterator MWWorld::InventoryStore::getSlot (int slot) const MWWorld::ConstContainerStoreIterator MWWorld::InventoryStore::getSlot (int slot) const
{ {
return const_cast<InventoryStore*>(this)->getSlot (slot); return findSlot (slot);
} }
bool MWWorld::InventoryStore::canActorAutoEquip(const MWWorld::Ptr& actor, const MWWorld::Ptr& item) bool MWWorld::InventoryStore::canActorAutoEquip(const MWWorld::Ptr& actor, const MWWorld::Ptr& item)
@ -240,6 +227,24 @@ bool MWWorld::InventoryStore::canActorAutoEquip(const MWWorld::Ptr& actor, const
return true; return true;
} }
MWWorld::ContainerStoreIterator MWWorld::InventoryStore::findSlot (int slot) const
{
if (slot<0 || slot>=static_cast<int> (mSlots.size()))
throw std::runtime_error ("slot number out of range");
if (mSlots[slot]==end())
return mSlots[slot];
if (mSlots[slot]->getRefData().getCount()<1)
{
// Object has been deleted
// This should no longer happen, since the new remove function will unequip first
throw std::runtime_error("Invalid slot, make sure you are not calling RefData::setCount for a container object");
}
return mSlots[slot];
}
void MWWorld::InventoryStore::autoEquip (const MWWorld::Ptr& actor) void MWWorld::InventoryStore::autoEquip (const MWWorld::Ptr& actor)
{ {
const MWBase::World *world = MWBase::Environment::get().getWorld(); const MWBase::World *world = MWBase::Environment::get().getWorld();

View file

@ -116,6 +116,7 @@ namespace MWWorld
virtual void readEquipmentState (const MWWorld::ContainerStoreIterator& iter, int index, const ESM::InventoryState& inventory); virtual void readEquipmentState (const MWWorld::ContainerStoreIterator& iter, int index, const ESM::InventoryState& inventory);
bool canActorAutoEquip(const MWWorld::Ptr& actor, const MWWorld::Ptr& item); bool canActorAutoEquip(const MWWorld::Ptr& actor, const MWWorld::Ptr& item);
ContainerStoreIterator findSlot (int slot) const;
public: public: