moved a bit of code back in the header to avoid many template specialization

crashfix_debugdraw
florent.teppe 2 years ago
parent 0dd529ab1d
commit e23d9c11a8

@ -147,63 +147,6 @@ namespace MWWorld
IDMap mIds;
IDMap mStaticIds;
template<typename T>
static const T* esm3StoreInsert(ESMStore& stores, const T &toInsert)
{
const std::string id = "$dynamic" + std::to_string(stores.mDynamicCount++);
Store<T> &store = stores.getWritable<T>();
if (store.search(id) != nullptr)
{
const std::string msg = "Try to override existing record '" + id + "'";
throw std::runtime_error(msg);
}
T record = toInsert;
record.mId = id;
T *ptr = store.insert(record);
auto esm3RecordType_find = stores.mStoreImp->mStoreToRecName.find(&stores.get<T>());
if (esm3RecordType_find != stores.mStoreImp->mStoreToRecName.end())
{
stores.mStoreImp->mIds[ptr->mId] = esm3RecordType_find->second;
}
return ptr;
}
template <class T>
static const T * esm3overrideRecord(ESMStore& stores, const T &x) {
Store<T> &store = stores.getWritable<T>();
T *ptr = store.insert(x);
auto esm3RecordType_find = stores.mStoreImp->mStoreToRecName.find(&stores.get<T>());
if (esm3RecordType_find != stores.mStoreImp->mStoreToRecName.end())
{
stores.mStoreImp->mIds[ptr->mId] = esm3RecordType_find->second;
}
return ptr;
}
template <class T>
static const T *esm3insertStatic(ESMStore& stores, const T &x)
{
Store<T> &store = stores.getWritable<T>();
if (store.search(x.mId) != nullptr)
{
const std::string msg = "Try to override existing record '" + x.mId + "'";
throw std::runtime_error(msg);
}
T *ptr = store.insertStatic(x);
auto esm3RecordType_find = stores.mStoreImp->mStoreToRecName.find(&stores.get<T>());
if (esm3RecordType_find != stores.mStoreImp->mStoreToRecName.end())
{
stores.mStoreImp->mIds[ptr->mId] = esm3RecordType_find->second;
}
return ptr;
}
template<typename T>
static int AssignStoreToIndex(ESMStore& stores, Store<T>& store)
{
@ -382,6 +325,11 @@ void ESMStore::load(ESM::ESMReader &esm, Loading::Listener* listener, ESM::Dialo
}
}
int& ESMStore::getIdType(std::string& id)
{
return mStoreImp->mIds[id];
}
static std::size_t sTypeIndexCounter = 0;
std::size_t& ESMStore::getTypeIndexCounter()
@ -742,28 +690,4 @@ void ESMStore::removeMissingObjects(Store<T>& store)
return ptr;
}
template<> const ESM::Book* ESMStore::insert<ESM::Book>(const ESM::Book &toInsert) { return ESMStoreImp::esm3StoreInsert(*this, toInsert);}
template<> const ESM::Armor* ESMStore::insert<ESM::Armor>(const ESM::Armor &toInsert) { return ESMStoreImp::esm3StoreInsert(*this, toInsert);}
template<> const ESM::Class* ESMStore::insert<ESM::Class>(const ESM::Class &toInsert) { return ESMStoreImp::esm3StoreInsert(*this, toInsert);}
template<> const ESM::Enchantment* ESMStore::insert<ESM::Enchantment>(const ESM::Enchantment &toInsert) { return ESMStoreImp::esm3StoreInsert(*this, toInsert);}
template<> const ESM::Potion* ESMStore::insert<ESM::Potion>(const ESM::Potion &toInsert) { return ESMStoreImp::esm3StoreInsert(*this, toInsert);}
template<> const ESM::Weapon* ESMStore::insert<ESM::Weapon>(const ESM::Weapon &toInsert) { return ESMStoreImp::esm3StoreInsert(*this, toInsert);}
template<> const ESM::Clothing* ESMStore::insert<ESM::Clothing>(const ESM::Clothing &toInsert) { return ESMStoreImp::esm3StoreInsert(*this, toInsert);}
template<> const ESM::Spell* ESMStore::insert<ESM::Spell>(const ESM::Spell &toInsert) { return ESMStoreImp::esm3StoreInsert(*this, toInsert);}
template<> const ESM::GameSetting* ESMStore::insertStatic<ESM::GameSetting>(const ESM::GameSetting &toInsert) { return ESMStoreImp::esm3insertStatic(*this, toInsert); }
template<> const ESM::Static* ESMStore::insertStatic<ESM::Static>(const ESM::Static &toInsert) { return ESMStoreImp::esm3insertStatic(*this, toInsert); }
template<> const ESM::Door* ESMStore::insertStatic<ESM::Door>(const ESM::Door &toInsert) { return ESMStoreImp::esm3insertStatic(*this, toInsert); }
template<> const ESM::Global* ESMStore::insertStatic<ESM::Global>(const ESM::Global &toInsert) { return ESMStoreImp::esm3insertStatic(*this, toInsert); }
template<> const ESM::NPC* ESMStore::insertStatic<ESM::NPC>(const ESM::NPC &toInsert) { return ESMStoreImp::esm3insertStatic(*this, toInsert); }
template<> const ESM::Container* ESMStore::overrideRecord<ESM::Container>(const ESM::Container &toInsert) { return ESMStoreImp::esm3overrideRecord(*this, toInsert); }
template<> const ESM::Creature* ESMStore::overrideRecord<ESM::Creature>(const ESM::Creature &toInsert) { return ESMStoreImp::esm3overrideRecord(*this, toInsert); }
template<> const ESM::CreatureLevList* ESMStore::overrideRecord<ESM::CreatureLevList>(const ESM::CreatureLevList &toInsert) { return ESMStoreImp::esm3overrideRecord(*this, toInsert); }
template<> const ESM::Door* ESMStore::overrideRecord<ESM::Door>(const ESM::Door &toInsert) { return ESMStoreImp::esm3overrideRecord(*this, toInsert); }
template<> const ESM::ItemLevList* ESMStore::overrideRecord<ESM::ItemLevList>(const ESM::ItemLevList &toInsert) { return ESMStoreImp::esm3overrideRecord(*this, toInsert); }
template<> const ESM::NPC* ESMStore::overrideRecord<ESM::NPC>(const ESM::NPC &toInsert) { return ESMStoreImp::esm3overrideRecord(*this, toInsert); }
} // end namespace

@ -165,6 +165,8 @@ namespace MWWorld
template<class T>
void removeMissingObjects(Store<T>& store);
int& getIdType(std::string& id);
using LuaContent = std::variant<
ESM::LuaScriptsCfg, // data from an omwaddon
std::string>; // path to an omwscripts file
@ -208,14 +210,58 @@ namespace MWWorld
/// Insert a custom record (i.e. with a generated ID that will not clash will pre-existing records)
template <class T>
const T* insert(const T& x);
const T *insert(const T &x)
{
const std::string id = "$dynamic" + std::to_string(mDynamicCount++);
Store<T> &store = getWritable<T>();
if (store.search(id) != nullptr)
{
const std::string msg = "Try to override existing record '" + id + "'";
throw std::runtime_error(msg);
}
T record = x;
record.mId = id;
T *ptr = store.insert(record);
if constexpr (std::is_convertible<Store<T>*, DynamicStore*>::value)
{
getIdType(ptr->mId) = T::sRecordId;
}
return ptr;
}
/// Insert a record with set ID, and allow it to override a pre-existing static record.
template <class T>
const T *overrideRecord(const T &x);
const T *overrideRecord(const T &x) {
Store<T> &store = getWritable<T>();
T *ptr = store.insert(x);
if constexpr (std::is_convertible<Store<T>*, DynamicStore*>::value)
{
getIdType(ptr->mId) = T::sRecordId;
}
return ptr;
}
template <class T>
const T *insertStatic(const T &x);
const T *insertStatic(const T &x)
{
Store<T>& store = getWritable<T>();
if (store.search(x.mId) != nullptr)
{
const std::string msg = "Try to override existing record '" + x.mId + "'";
throw std::runtime_error(msg);
}
T *ptr = store.insertStatic(x);
if constexpr (std::is_convertible<Store<T>*, DynamicStore*>::value)
{
getIdType(ptr->mId) = T::sRecordId;
}
return ptr;
}
// This method must be called once, after loading all master/plugin files. This can only be done
// from the outside, so it must be public.
@ -238,6 +284,12 @@ namespace MWWorld
/// Actors with the same ID share spells, abilities, etc.
/// @return The shared spell list to use for this actor and whether or not it has already been initialized.
std::pair<std::shared_ptr<MWMechanics::SpellList>, bool> getSpellList(const std::string& id) const;
template <>
const ESM::Cell* insert<ESM::Cell>(const ESM::Cell& cell);
template <>
const ESM::NPC* insert<ESM::NPC>(const ESM::NPC& npc);
};
}

@ -20,6 +20,7 @@
#include <components/esm3/loadregn.hpp>
#include <components/esm3/loadench.hpp>
#include <components/esm3/loadmgef.hpp>
#include <components/esm3/loadclas.hpp>
#include <components/misc/constants.hpp>
#include <components/misc/mathutil.hpp>

Loading…
Cancel
Save