From 6c2ce2b2a199543f9a10c13f886dd82e95323c93 Mon Sep 17 00:00:00 2001 From: Rafael Moura Date: Tue, 28 Feb 2017 14:43:20 +0000 Subject: [PATCH] Porting more ContainerStoreIterator usage to const version #4 removed const_cast revision --- apps/openmw/mwrender/npcanimation.cpp | 14 ++++---- apps/openmw/mwrender/weaponanimation.cpp | 6 ++-- apps/openmw/mwscript/containerextensions.cpp | 14 ++++---- apps/openmw/mwscript/miscextensions.cpp | 2 +- apps/openmw/mwworld/inventorystore.cpp | 35 +++++++++++--------- apps/openmw/mwworld/inventorystore.hpp | 1 + 6 files changed, 39 insertions(+), 33 deletions(-) diff --git a/apps/openmw/mwrender/npcanimation.cpp b/apps/openmw/mwrender/npcanimation.cpp index 3174cbf9d..d6eeea4be 100644 --- a/apps/openmw/mwrender/npcanimation.cpp +++ b/apps/openmw/mwrender/npcanimation.cpp @@ -765,8 +765,8 @@ bool NpcAnimation::addOrReplaceIndividualPart(ESM::PartReferenceType type, int g if (!mSoundsDisabled) { - MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr); - MWWorld::ContainerStoreIterator csi = inv.getSlot(group < 0 ? MWWorld::InventoryStore::Slot_Helmet : group); + const MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr); + MWWorld::ConstContainerStoreIterator csi = inv.getSlot(group < 0 ? MWWorld::InventoryStore::Slot_Helmet : group); if (csi != inv.end()) { mSoundIds[type] = csi->getClass().getSound(*csi); @@ -899,8 +899,8 @@ void NpcAnimation::showWeapons(bool showWeapon) mShowWeapons = showWeapon; if(showWeapon) { - MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr); - MWWorld::ContainerStoreIterator weapon = inv.getSlot(MWWorld::InventoryStore::Slot_CarriedRight); + const MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr); + MWWorld::ConstContainerStoreIterator weapon = inv.getSlot(MWWorld::InventoryStore::Slot_CarriedRight); if(weapon != inv.end()) { osg::Vec4f glowColor = getEnchantmentColor(*weapon); @@ -912,7 +912,7 @@ void NpcAnimation::showWeapons(bool showWeapon) if (weapon->getTypeName() == typeid(ESM::Weapon).name() && weapon->get()->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()->mBase->mData.mType == ESM::Weapon::Bolt) attachArrow(); else @@ -931,8 +931,8 @@ void NpcAnimation::showWeapons(bool showWeapon) void NpcAnimation::showCarriedLeft(bool show) { mShowCarriedLeft = show; - MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr); - MWWorld::ContainerStoreIterator iter = inv.getSlot(MWWorld::InventoryStore::Slot_CarriedLeft); + const MWWorld::InventoryStore& inv = mPtr.getClass().getInventoryStore(mPtr); + MWWorld::ConstContainerStoreIterator iter = inv.getSlot(MWWorld::InventoryStore::Slot_CarriedLeft); if(show && iter != inv.end()) { osg::Vec4f glowColor = getEnchantmentColor(*iter); diff --git a/apps/openmw/mwrender/weaponanimation.cpp b/apps/openmw/mwrender/weaponanimation.cpp index d5fb70d16..0b3d000f3 100644 --- a/apps/openmw/mwrender/weaponanimation.cpp +++ b/apps/openmw/mwrender/weaponanimation.cpp @@ -56,8 +56,8 @@ WeaponAnimation::~WeaponAnimation() void WeaponAnimation::attachArrow(MWWorld::Ptr actor) { - MWWorld::InventoryStore& inv = actor.getClass().getInventoryStore(actor); - MWWorld::ContainerStoreIterator weaponSlot = inv.getSlot(MWWorld::InventoryStore::Slot_CarriedRight); + const MWWorld::InventoryStore& inv = actor.getClass().getInventoryStore(actor); + MWWorld::ConstContainerStoreIterator weaponSlot = inv.getSlot(MWWorld::InventoryStore::Slot_CarriedRight); if (weaponSlot == inv.end()) return; if (weaponSlot->getTypeName() != typeid(ESM::Weapon).name()) @@ -79,7 +79,7 @@ void WeaponAnimation::attachArrow(MWWorld::Ptr actor) if (!parent) return; - MWWorld::ContainerStoreIterator ammo = inv.getSlot(MWWorld::InventoryStore::Slot_Ammunition); + MWWorld::ConstContainerStoreIterator ammo = inv.getSlot(MWWorld::InventoryStore::Slot_Ammunition); if (ammo == inv.end()) return; std::string model = ammo->getClass().getModel(*ammo); diff --git a/apps/openmw/mwscript/containerextensions.cpp b/apps/openmw/mwscript/containerextensions.cpp index e5880ee37..8009345f2 100644 --- a/apps/openmw/mwscript/containerextensions.cpp +++ b/apps/openmw/mwscript/containerextensions.cpp @@ -256,9 +256,9 @@ namespace MWScript throw std::runtime_error ("armor index out of range"); } - MWWorld::InventoryStore& invStore = ptr.getClass().getInventoryStore (ptr); - - MWWorld::ContainerStoreIterator it = invStore.getSlot (slot); + const MWWorld::InventoryStore& invStore = ptr.getClass().getInventoryStore (ptr); + MWWorld::ConstContainerStoreIterator it = invStore.getSlot (slot); + if (it == invStore.end() || it->getTypeName () != typeid(ESM::Armor).name()) { runtime.push(-1); @@ -289,10 +289,10 @@ namespace MWScript std::string item = runtime.getStringLiteral (runtime[0].mInteger); 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) { - MWWorld::ContainerStoreIterator it = invStore.getSlot (slot); + MWWorld::ConstContainerStoreIterator it = invStore.getSlot (slot); if (it != invStore.end() && ::Misc::StringUtils::ciEqual(it->getCellRef().getRefId(), item)) { runtime.push(1); @@ -336,8 +336,8 @@ namespace MWScript { MWWorld::Ptr ptr = R()(runtime); - MWWorld::InventoryStore& invStore = ptr.getClass().getInventoryStore (ptr); - MWWorld::ContainerStoreIterator it = invStore.getSlot (MWWorld::InventoryStore::Slot_CarriedRight); + const MWWorld::InventoryStore& invStore = ptr.getClass().getInventoryStore (ptr); + MWWorld::ConstContainerStoreIterator it = invStore.getSlot (MWWorld::InventoryStore::Slot_CarriedRight); if (it == invStore.end() || it->getTypeName () != typeid(ESM::Weapon).name()) { runtime.push(-1); diff --git a/apps/openmw/mwscript/miscextensions.cpp b/apps/openmw/mwscript/miscextensions.cpp index 88b7e5262..98b1a6b47 100644 --- a/apps/openmw/mwscript/miscextensions.cpp +++ b/apps/openmw/mwscript/miscextensions.cpp @@ -519,7 +519,7 @@ namespace MWScript int numNotEquipped = invStorePtr->count(item); 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)) { numNotEquipped -= it->getRefData().getCount(); diff --git a/apps/openmw/mwworld/inventorystore.cpp b/apps/openmw/mwworld/inventorystore.cpp index fd3049f7c..c6d67b75d 100644 --- a/apps/openmw/mwworld/inventorystore.cpp +++ b/apps/openmw/mwworld/inventorystore.cpp @@ -199,25 +199,12 @@ void MWWorld::InventoryStore::unequipAll(const MWWorld::Ptr& actor) MWWorld::ContainerStoreIterator MWWorld::InventoryStore::getSlot (int slot) { - if (slot<0 || slot>=static_cast (mSlots.size())) - 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]; + return findSlot (slot); } MWWorld::ConstContainerStoreIterator MWWorld::InventoryStore::getSlot (int slot) const { - return const_cast(this)->getSlot (slot); + return findSlot (slot); } 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; } +MWWorld::ContainerStoreIterator MWWorld::InventoryStore::findSlot (int slot) const +{ + if (slot<0 || slot>=static_cast (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) { const MWBase::World *world = MWBase::Environment::get().getWorld(); diff --git a/apps/openmw/mwworld/inventorystore.hpp b/apps/openmw/mwworld/inventorystore.hpp index 93350e7c7..eff4092d1 100644 --- a/apps/openmw/mwworld/inventorystore.hpp +++ b/apps/openmw/mwworld/inventorystore.hpp @@ -116,6 +116,7 @@ namespace MWWorld virtual void readEquipmentState (const MWWorld::ContainerStoreIterator& iter, int index, const ESM::InventoryState& inventory); bool canActorAutoEquip(const MWWorld::Ptr& actor, const MWWorld::Ptr& item); + ContainerStoreIterator findSlot (int slot) const; public: