diff --git a/apps/openmw/mwworld/esmstore.cpp b/apps/openmw/mwworld/esmstore.cpp index dc24d857a6..8b794cb776 100644 --- a/apps/openmw/mwworld/esmstore.cpp +++ b/apps/openmw/mwworld/esmstore.cpp @@ -129,7 +129,7 @@ namespace static int sRecordTypeCounter = 0; -#define OPENMW_ESM_ADD_STORE_TYPE(__Type)template<> const int MWWorld::SRecordType<__Type>::sRecordId = sRecordTypeCounter++ +#define OPENMW_ESM_ADD_STORE_TYPE(__Type)template<> const int MWWorld::SRecordType<__Type>::sStoreIndex = sRecordTypeCounter++ OPENMW_ESM_ADD_STORE_TYPE(ESM::Activator); OPENMW_ESM_ADD_STORE_TYPE(ESM::Potion); @@ -185,6 +185,9 @@ static const int sRecordTypeCount = sRecordTypeCounter; namespace MWWorld { + using IDMap = std::unordered_map; + + struct ESMStoreImp { //These 3 don't inherit from store base @@ -192,9 +195,14 @@ namespace MWWorld Store mSkills; Store mAttributes; - std::map mESM3RecordToStore; + std::map mESM3RecordToStore; std::unordered_map mStoreToEsm3Record; + // Lookup of all IDs. Makes looking up references faster. Just + // maps the id name to the record type. + IDMap mIds; + IDMap mStaticIds; + template static const T* esm3StoreInsert(ESMStore& stores, const T &toInsert) @@ -216,7 +224,7 @@ namespace MWWorld if (esm3RecordType_find != stores.mStoreImp->mStoreToEsm3Record.end()) { - stores.mIds[ptr->mId] = esm3RecordType_find->second; + stores.mStoreImp->mIds[ptr->mId] = esm3RecordType_find->second; } return ptr; } @@ -229,7 +237,7 @@ namespace MWWorld auto esm3RecordType_find = stores.mStoreImp->mStoreToEsm3Record.find(&stores.get()); if (esm3RecordType_find != stores.mStoreImp->mStoreToEsm3Record.end()) { - stores.mIds[ptr->mId] = esm3RecordType_find->second; + stores.mStoreImp->mIds[ptr->mId] = esm3RecordType_find->second; } return ptr; } @@ -251,7 +259,7 @@ namespace MWWorld auto esm3RecordType_find = stores.mStoreImp->mStoreToEsm3Record.find(&stores.get()); if (esm3RecordType_find != stores.mStoreImp->mStoreToEsm3Record.end()) { - stores.mIds[ptr->mId] = esm3RecordType_find->second; + stores.mStoreImp->mIds[ptr->mId] = esm3RecordType_find->second; } return ptr; } @@ -308,11 +316,31 @@ namespace MWWorld template static void createStore(ESMStore& stores) { - stores.mStores[SRecordType::sRecordId] = std::make_unique>(); + stores.mStores[SRecordType::sStoreIndex] = std::make_unique>(); } }; + int ESMStore::find(const std::string& id) const + { + + IDMap::const_iterator it = mStoreImp->mIds.find(id); + if (it == mStoreImp->mIds.end()) { + return 0; + } + return it->second; + + } + + int ESMStore::findStatic(const std::string& id) const + { + IDMap::const_iterator it = mStoreImp-> mStaticIds.find(id); + if (it == mStoreImp->mStaticIds.end()) { + return 0; + } + return it->second; + } + ESMStore::ESMStore() { mStores.resize(sRecordTypeCount); @@ -492,7 +520,7 @@ ESM::LuaScriptsCfg ESMStore::getLuaScriptsCfg() const void ESMStore::setUp() { - mIds.clear(); + mStoreImp->mIds.clear(); std::map::iterator storeIt = mStoreImp->mESM3RecordToStore.begin(); for (; storeIt != mStoreImp->mESM3RecordToStore.end(); ++storeIt) { @@ -504,13 +532,13 @@ void ESMStore::setUp() storeIt->second->listIdentifier(identifiers); for (std::vector::const_iterator record = identifiers.begin(); record != identifiers.end(); ++record) - mIds[*record] = storeIt->first; + mStoreImp->mIds[*record] = storeIt->first; } } - if (mStaticIds.empty()) - for (const auto& [k, v] : mIds) - mStaticIds.emplace(Misc::StringUtils::lowerCase(k), v); + if (mStoreImp->mStaticIds.empty()) + for (const auto& [k, v] : mStoreImp->mIds) + mStoreImp->mStaticIds.emplace(Misc::StringUtils::lowerCase(k), v); getWritable().setUp(); getWritable().setUp();; @@ -812,7 +840,7 @@ void ESMStore::removeMissingObjects(Store& store) record.mId = id; ESM::NPC *ptr = npcs.insert(record); - mIds[ptr->mId] = ESM::REC_NPC_; + mStoreImp->mIds[ptr->mId] = ESM::REC_NPC_; return ptr; } diff --git a/apps/openmw/mwworld/esmstore.hpp b/apps/openmw/mwworld/esmstore.hpp index cb77c9aaae..276b004a89 100644 --- a/apps/openmw/mwworld/esmstore.hpp +++ b/apps/openmw/mwworld/esmstore.hpp @@ -28,7 +28,7 @@ namespace MWWorld struct ESMStoreImp; template struct SRecordType { - static const int sRecordId; + static const int sStoreIndex; }; class ESMStore @@ -36,12 +36,6 @@ namespace MWWorld friend struct ESMStoreImp; //This allows StoreImp to extend esmstore without beeing included everywhere std::unique_ptr mStoreImp; - // Lookup of all IDs. Makes looking up references faster. Just - // maps the id name to the record type. - using IDMap = std::unordered_map; - IDMap mIds; - std::unordered_map mStaticIds; - std::unordered_map mRefCount; std::vector> mStores; @@ -51,7 +45,7 @@ namespace MWWorld mutable std::unordered_map, Misc::StringUtils::CiHash, Misc::StringUtils::CiEqual> mSpellListCache; template - Store& getWritable() {return static_cast&>(*mStores[SRecordType::sRecordId]);} + Store& getWritable() {return static_cast&>(*mStores[SRecordType::sStoreIndex]);} /// Validate entries in store after setup void validate(); @@ -82,22 +76,10 @@ namespace MWWorld } /// Look up the given ID in 'all'. Returns 0 if not found. - int find(std::string_view id) const - { - IDMap::const_iterator it = mIds.find(id); - if (it == mIds.end()) { - return 0; - } - return it->second; - } - int findStatic(const std::string &id) const - { - IDMap::const_iterator it = mStaticIds.find(id); - if (it == mStaticIds.end()) { - return 0; - } - return it->second; - } + int find(const std::string& id) const; + + int findStatic(const std::string& id) const; + ESMStore(); ~ESMStore(); @@ -112,7 +94,7 @@ namespace MWWorld void load(ESM::ESMReader &esm, Loading::Listener* listener, ESM::Dialogue*& dialogue); template - const Store& get() const {return static_cast&>(*mStores[SRecordType::sRecordId]);} + const Store& get() const {return static_cast&>(*mStores[SRecordType::sStoreIndex]);} /// Insert a custom record (i.e. with a generated ID that will not clash will pre-existing records) template