From a517f4f9bad647489c654429285aef2770d08b9e Mon Sep 17 00:00:00 2001 From: scrawl Date: Sun, 6 Dec 2015 20:19:07 +0100 Subject: [PATCH] Add CellStore::forEachType to help with porting over game logic to the new interfaces --- apps/openmw/mwworld/cellstore.hpp | 177 +++++++++++++++++++++--------- 1 file changed, 127 insertions(+), 50 deletions(-) diff --git a/apps/openmw/mwworld/cellstore.hpp b/apps/openmw/mwworld/cellstore.hpp index 542d4700e..ae8ac8ae5 100644 --- a/apps/openmw/mwworld/cellstore.hpp +++ b/apps/openmw/mwworld/cellstore.hpp @@ -116,15 +116,6 @@ namespace MWWorld /// Repopulate mMergedRefs. void updateMergedRefs(); - template - LiveCellRefBase* insertBase(CellRefList& list, const LiveCellRef* ref) - { - mHasState = true; - LiveCellRefBase* ret = &list.insert(*ref); - updateMergedRefs(); - return ret; - } - // helper function for forEachInternal template bool forEachImp (Visitor& visitor, List& list) @@ -167,6 +158,11 @@ namespace MWWorld forEachImp (visitor, mCreatureLists); } + /// @note If you get a linker error here, this means the given type can not be stored in a cell. The supported types are + /// defined at the bottom of this file. + template + CellRefList& get(); + public: /// Moves object from this cell to the given cell. @@ -179,7 +175,14 @@ namespace MWWorld /// @note If you get a linker error here, this means the given type can not be inserted into a cell. /// The supported types are defined at the bottom of this file. template - LiveCellRefBase* insert(const LiveCellRef* ref); + LiveCellRefBase* insert(const LiveCellRef* ref) + { + mHasState = true; + CellRefList& list = get(); + LiveCellRefBase* ret = &list.insert(*ref); + updateMergedRefs(); + return ret; + } /// @param readerList The readers to use for loading of the cell on-demand. CellStore (const ESM::Cell *cell_, @@ -246,6 +249,41 @@ namespace MWWorld return true; } + /// Call visitor (ref) for each reference of given type. visitor must return a bool. Returning + /// false will abort the iteration. + /// \attention This function also lists deleted (count 0) objects! + /// \return Iteration completed? + template + bool forEachType(Visitor& visitor) + { + if (mState != State_Loaded) + return false; + + mHasState = true; + + CellRefList& list = get(); + + for (typename CellRefList::List::iterator it (list.mList.begin()); it!=list.mList.end(); ++it) + { + LiveCellRefBase* base = &*it; + if (mMovedToAnotherCell.find(base) != mMovedToAnotherCell.end()) + continue; + if (base->mData.isDeletedByContentFile()) + continue; + if (!visitor(MWWorld::Ptr(base, this))) + return false; + } + + for (MovedRefTracker::const_iterator it = mMovedHere.begin(); it != mMovedHere.end(); ++it) + { + LiveCellRefBase* base = it->first; + if (dynamic_cast*>(base)) + if (!visitor(MWWorld::Ptr(base, this))) + return false; + } + return true; + } + /// \todo add const version of forEach bool isExterior() const; @@ -295,104 +333,143 @@ namespace MWWorld }; template<> - inline LiveCellRefBase* CellStore::insert(const LiveCellRef* ref) + inline CellRefList& CellStore::get() { - return insertBase(mActivators, ref); + mHasState = true; + return mActivators; } + template<> - inline LiveCellRefBase* CellStore::insert(const LiveCellRef* ref) + inline CellRefList& CellStore::get() { - return insertBase(mPotions, ref); + mHasState = true; + return mPotions; } + template<> - inline LiveCellRefBase* CellStore::insert(const LiveCellRef* ref) + inline CellRefList& CellStore::get() { - return insertBase(mAppas, ref); + mHasState = true; + return mAppas; } + template<> - inline LiveCellRefBase* CellStore::insert(const LiveCellRef* ref) + inline CellRefList& CellStore::get() { - return insertBase(mArmors, ref); + mHasState = true; + return mArmors; } + template<> - inline LiveCellRefBase* CellStore::insert(const LiveCellRef* ref) + inline CellRefList& CellStore::get() { - return insertBase(mBooks, ref); + mHasState = true; + return mBooks; } + template<> - inline LiveCellRefBase* CellStore::insert(const LiveCellRef* ref) + inline CellRefList& CellStore::get() { - return insertBase(mClothes, ref); + mHasState = true; + return mClothes; } + template<> - inline LiveCellRefBase* CellStore::insert(const LiveCellRef* ref) + inline CellRefList& CellStore::get() { - return insertBase(mContainers, ref); + mHasState = true; + return mContainers; } + template<> - inline LiveCellRefBase* CellStore::insert(const LiveCellRef* ref) + inline CellRefList& CellStore::get() { - return insertBase(mCreatures, ref); + mHasState = true; + return mCreatures; } + template<> - inline LiveCellRefBase* CellStore::insert(const LiveCellRef* ref) + inline CellRefList& CellStore::get() { - return insertBase(mDoors, ref); + mHasState = true; + return mDoors; } + template<> - inline LiveCellRefBase* CellStore::insert(const LiveCellRef* ref) + inline CellRefList& CellStore::get() { - return insertBase(mIngreds, ref); + mHasState = true; + return mIngreds; } + template<> - inline LiveCellRefBase* CellStore::insert(const LiveCellRef* ref) + inline CellRefList& CellStore::get() { - return insertBase(mCreatureLists, ref); + mHasState = true; + return mCreatureLists; } + template<> - inline LiveCellRefBase* CellStore::insert(const LiveCellRef* ref) + inline CellRefList& CellStore::get() { - return insertBase(mItemLists, ref); + mHasState = true; + return mItemLists; } + template<> - inline LiveCellRefBase* CellStore::insert(const LiveCellRef* ref) + inline CellRefList& CellStore::get() { - return insertBase(mLights, ref); + mHasState = true; + return mLights; } + template<> - inline LiveCellRefBase* CellStore::insert(const LiveCellRef* ref) + inline CellRefList& CellStore::get() { - return insertBase(mLockpicks, ref); + mHasState = true; + return mLockpicks; } + template<> - inline LiveCellRefBase* CellStore::insert(const LiveCellRef* ref) + inline CellRefList& CellStore::get() { - return insertBase(mMiscItems, ref); + mHasState = true; + return mMiscItems; } + template<> - inline LiveCellRefBase* CellStore::insert(const LiveCellRef* ref) + inline CellRefList& CellStore::get() { - return insertBase(mNpcs, ref); + mHasState = true; + return mNpcs; } + template<> - inline LiveCellRefBase* CellStore::insert(const LiveCellRef* ref) + inline CellRefList& CellStore::get() { - return insertBase(mProbes, ref); + mHasState = true; + return mProbes; } + template<> - inline LiveCellRefBase* CellStore::insert(const LiveCellRef* ref) + inline CellRefList& CellStore::get() { - return insertBase(mRepairs, ref); + mHasState = true; + return mRepairs; } + template<> - inline LiveCellRefBase* CellStore::insert(const LiveCellRef* ref) + inline CellRefList& CellStore::get() { - return insertBase(mStatics, ref); + mHasState = true; + return mStatics; } + template<> - inline LiveCellRefBase* CellStore::insert(const LiveCellRef* ref) + inline CellRefList& CellStore::get() { - return insertBase(mWeapons, ref); + mHasState = true; + return mWeapons; } bool operator== (const CellStore& left, const CellStore& right);