From 65bd007baa267a6e8f1ec9f5d9374cac31a0e275 Mon Sep 17 00:00:00 2001 From: "florent.teppe" Date: Fri, 9 Sep 2022 18:28:48 +0200 Subject: [PATCH] simplified some big switch/case --- apps/openmw/mwworld/cellstore.cpp | 99 ++++++++++++------------------- components/misc/tuplemeta.hpp | 6 ++ 2 files changed, 44 insertions(+), 61 deletions(-) diff --git a/apps/openmw/mwworld/cellstore.cpp b/apps/openmw/mwworld/cellstore.cpp index 96239cc48d..2be8db1bf4 100644 --- a/apps/openmw/mwworld/cellstore.cpp +++ b/apps/openmw/mwworld/cellstore.cpp @@ -36,6 +36,7 @@ namespace { + template MWWorld::Ptr searchInContainerList(MWWorld::CellRefList& containerList, std::string_view id) { @@ -244,17 +245,18 @@ namespace MWWorld if (stores.mCellRefLists.size() <= storeIndex) stores.mCellRefLists.resize(storeIndex + 1); - assert(&store == &std::get>(stores.mCellStoreImp->mRefLists)); + assert(&refList == &std::get>(stores.mCellStoreImp->mRefLists)); stores.mCellRefLists[storeIndex] = &refList; } - template - static void ReadReferenceSwitcher(CellRefList& collection, ESM::ESMReader& reader,const ESM::CellRef& cref, const std::map& contentFileMap, MWWorld::CellStore* cellstore, ESM::RecNameInts recnNameInt) + //this function allows us to link a CellRefList to the associated recNameInt, and apply a function + template + static void recNameSwitcher(CellRefList& store, Callable&& f, ESM::RecNameInts recnNameInt) { if (RecordType::sRecordId == recnNameInt) { - readReferenceCollection(reader, collection, cref, contentFileMap, cellstore); + f(store); } } }; @@ -421,8 +423,8 @@ namespace MWWorld , mState(State_Unloaded) , mHasState(false) , mLastRespawn(0, 0) - , mRechargingItemsUpToDate(false) , mCellStoreImp(std::make_unique()) + , mRechargingItemsUpToDate(false) { std::apply([this](auto& ...x) {(CellStoreImp::assignStoreToIndex(*this, x), ...); }, mCellStoreImp->mRefLists); mWaterLevel = cell->mWater; @@ -739,64 +741,35 @@ namespace MWWorld if (it->second != ref.mRefID) { // refID was modified, make sure we don't end up with duplicated refs - switch (store.find(it->second)) - { - case ESM::REC_ACTI: get().remove(ref.mRefNum); break; - case ESM::REC_ALCH: get().remove(ref.mRefNum); break; - case ESM::REC_APPA: get().remove(ref.mRefNum); break; - case ESM::REC_ARMO: get().remove(ref.mRefNum); break; - case ESM::REC_BOOK: get().remove(ref.mRefNum); break; - case ESM::REC_CLOT: get().remove(ref.mRefNum); break; - case ESM::REC_CONT: get().remove(ref.mRefNum); break; - case ESM::REC_CREA: get().remove(ref.mRefNum); break; - case ESM::REC_DOOR: get().remove(ref.mRefNum); break; - case ESM::REC_INGR: get().remove(ref.mRefNum); break; - case ESM::REC_LEVC: get().remove(ref.mRefNum); break; - case ESM::REC_LEVI: get().remove(ref.mRefNum); break; - case ESM::REC_LIGH: get().remove(ref.mRefNum); break; - case ESM::REC_LOCK: get().remove(ref.mRefNum); break; - case ESM::REC_MISC: get().remove(ref.mRefNum); break; - case ESM::REC_NPC_: get().remove(ref.mRefNum); break; - case ESM::REC_PROB: get().remove(ref.mRefNum); break; - case ESM::REC_REPA: get().remove(ref.mRefNum); break; - case ESM::REC_STAT: get().remove(ref.mRefNum); break; - case ESM::REC_WEAP: get().remove(ref.mRefNum); break; - case ESM::REC_BODY: get().remove(ref.mRefNum); break; - default: - break; - } + ESM::RecNameInts foundType = (ESM::RecNameInts)store.find(it->second); + Misc::tupleForEach(this->mCellStoreImp->mRefLists, [&ref, foundType](auto& x) {CellStoreImp::recNameSwitcher(x, [&ref](auto& storeIn) + { + storeIn.remove(ref.mRefNum); + }, foundType); + }); } } - switch (store.find (ref.mRefID)) + ESM::RecNameInts foundType = (ESM::RecNameInts)store.find(ref.mRefID); + bool handledType = false; + if (foundType != 0) { - case ESM::REC_ACTI: get().load(ref, deleted, store); break; - case ESM::REC_ALCH: get().load(ref, deleted,store); break; - case ESM::REC_APPA: get().load(ref, deleted, store); break; - case ESM::REC_ARMO: get().load(ref, deleted, store); break; - case ESM::REC_BOOK: get().load(ref, deleted, store); break; - case ESM::REC_CLOT: get().load(ref, deleted, store); break; - case ESM::REC_CONT: get().load(ref, deleted, store); break; - case ESM::REC_CREA: get().load(ref, deleted, store); break; - case ESM::REC_DOOR: get().load(ref, deleted, store); break; - case ESM::REC_INGR: get().load(ref, deleted, store); break; - case ESM::REC_LEVC: get().load(ref, deleted, store); break; - case ESM::REC_LEVI: get().load(ref, deleted, store); break; - case ESM::REC_LIGH: get().load(ref, deleted, store); break; - case ESM::REC_LOCK: get().load(ref, deleted, store); break; - case ESM::REC_MISC: get().load(ref, deleted, store); break; - case ESM::REC_NPC_: get().load(ref, deleted, store); break; - case ESM::REC_PROB: get().load(ref, deleted, store); break; - case ESM::REC_REPA: get().load(ref, deleted, store); break; - case ESM::REC_STAT: get().load(ref, deleted, store); break; - case ESM::REC_WEAP: get().load(ref, deleted, store); break; - case ESM::REC_BODY: get().load(ref, deleted, store); break; + Misc::tupleForEach(this->mCellStoreImp->mRefLists, [&ref, &deleted, &store, foundType, &handledType](auto& x) {CellStoreImp::recNameSwitcher(x, [&ref, &deleted, &store, &handledType](auto& storeIn) + { + handledType = true; + storeIn.load(ref, deleted, store); + }, foundType); + }); + } + else + { + Log(Debug::Error) << "Cell reference '" + ref.mRefID + "' not found!"; return; + } - case 0: Log(Debug::Error) << "Cell reference '" + ref.mRefID + "' not found!"; return; - - default: - Log(Debug::Error) << "Error: Ignoring reference '" << ref.mRefID << "' of unhandled type"; - return; + if (!handledType) + { + Log(Debug::Error) << "Error: Ignoring reference '" << ref.mRefID << "' of unhandled type"; + return; } refNumToID[ref.mRefNum] = ref.mRefID; @@ -915,9 +888,13 @@ namespace MWWorld case ESM::REC_STAT: case ESM::REC_WEAP: case ESM::REC_BODY: - std::apply([&reader, this, &cref, &contentFileMap, type](auto& ...x){ - (CellStoreImp::ReadReferenceSwitcher(x, reader, cref, contentFileMap, this, (ESM::RecNameInts)type),... ); - }, this->mCellStoreImp->mRefLists); + Misc::tupleForEach(this->mCellStoreImp->mRefLists, [&reader, this, &cref, &contentFileMap, type](auto&& x) { + CellStoreImp::recNameSwitcher( + x, + [&reader, this, &cref, &contentFileMap]( + auto& store) { readReferenceCollection(reader, store, cref, contentFileMap, this); }, + (ESM::RecNameInts)type); + }); break; case ESM::REC_CONT: readReferenceCollection (reader, get(), cref, contentFileMap, this); diff --git a/components/misc/tuplemeta.hpp b/components/misc/tuplemeta.hpp index fda6fb32e3..af92066959 100644 --- a/components/misc/tuplemeta.hpp +++ b/components/misc/tuplemeta.hpp @@ -30,6 +30,12 @@ namespace Misc { static constexpr std::size_t value = 1 + TupleTypeIndex>::value; }; + + template + void tupleForEach(TupleType& tuple, Callable&& f) + { + std::apply([&f](auto& ...x) {(f(x), ...); }, tuple); + } } #endif