1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-29 08:45:36 +00:00

Deregister only registered LiveCellRefBase

This commit is contained in:
elsid 2024-06-15 00:30:14 +02:00
parent 4565152b3d
commit d998faec1b
No known key found for this signature in database
GPG key ID: 4DE04C198CBA7625
4 changed files with 48 additions and 3 deletions

View file

@ -38,9 +38,27 @@ namespace MWWorld
{ {
} }
LiveCellRefBase::LiveCellRefBase(LiveCellRefBase&& other) noexcept
: mClass(other.mClass)
, mRef(std::move(other.mRef))
, mData(std::move(other.mData))
, mWorldModel(std::exchange(other.mWorldModel, nullptr))
{
}
LiveCellRefBase::~LiveCellRefBase() LiveCellRefBase::~LiveCellRefBase()
{ {
MWBase::Environment::get().getWorldModel()->deregisterLiveCellRef(*this); if (mWorldModel != nullptr)
mWorldModel->deregisterLiveCellRef(*this);
}
LiveCellRefBase& LiveCellRefBase::operator=(LiveCellRefBase&& other) noexcept
{
mClass = other.mClass;
mRef = std::move(other.mRef);
mData = std::move(other.mData);
mWorldModel = std::exchange(other.mWorldModel, nullptr);
return *this;
} }
void LiveCellRefBase::loadImp(const ESM::ObjectState& state) void LiveCellRefBase::loadImp(const ESM::ObjectState& state)

View file

@ -17,6 +17,7 @@ namespace MWWorld
class Ptr; class Ptr;
class ESMStore; class ESMStore;
class Class; class Class;
class WorldModel;
template <typename X> template <typename X>
struct LiveCellRef; struct LiveCellRef;
@ -34,12 +35,23 @@ namespace MWWorld
/** runtime-data */ /** runtime-data */
RefData mData; RefData mData;
WorldModel* mWorldModel = nullptr;
LiveCellRefBase(unsigned int type, const ESM::CellRef& cref = ESM::CellRef()); LiveCellRefBase(unsigned int type, const ESM::CellRef& cref = ESM::CellRef());
LiveCellRefBase(unsigned int type, const ESM4::Reference& cref); LiveCellRefBase(unsigned int type, const ESM4::Reference& cref);
LiveCellRefBase(unsigned int type, const ESM4::ActorCharacter& cref); LiveCellRefBase(unsigned int type, const ESM4::ActorCharacter& cref);
LiveCellRefBase(const LiveCellRefBase& other) = default;
LiveCellRefBase(LiveCellRefBase&& other) noexcept;
/* Need this for the class to be recognized as polymorphic */ /* Need this for the class to be recognized as polymorphic */
virtual ~LiveCellRefBase(); virtual ~LiveCellRefBase();
LiveCellRefBase& operator=(const LiveCellRefBase& other) = default;
LiveCellRefBase& operator=(LiveCellRefBase&& other) noexcept;
virtual void load(const ESM::ObjectState& state) = 0; virtual void load(const ESM::ObjectState& state) = 0;
///< Load state into a LiveCellRef, that has already been initialised with base and class. ///< Load state into a LiveCellRef, that has already been initialised with base and class.
/// ///

View file

@ -3,6 +3,7 @@
#include <algorithm> #include <algorithm>
#include <cassert> #include <cassert>
#include <optional> #include <optional>
#include <stdexcept>
#include <components/debug/debuglog.hpp> #include <components/debug/debuglog.hpp>
#include <components/esm/defs.hpp> #include <components/esm/defs.hpp>
@ -339,6 +340,20 @@ namespace MWWorld
throw std::runtime_error(std::string("Can't find cell with name ") + std::string(name)); throw std::runtime_error(std::string("Can't find cell with name ") + std::string(name));
return *result; return *result;
} }
void WorldModel::registerPtr(const Ptr& ptr)
{
if (ptr.mRef == nullptr)
throw std::logic_error("Ptr with nullptr mRef is not allowed to be registered");
mPtrRegistry.insert(ptr);
ptr.mRef->mWorldModel = this;
}
void WorldModel::deregisterLiveCellRef(LiveCellRefBase& ref) noexcept
{
mPtrRegistry.remove(ref);
ref.mWorldModel = nullptr;
}
} }
MWWorld::Ptr MWWorld::WorldModel::getPtrByRefId(const ESM::RefId& name) MWWorld::Ptr MWWorld::WorldModel::getPtrByRefId(const ESM::RefId& name)

View file

@ -77,9 +77,9 @@ namespace MWWorld
std::size_t getPtrRegistryRevision() const { return mPtrRegistry.getRevision(); } std::size_t getPtrRegistryRevision() const { return mPtrRegistry.getRevision(); }
void registerPtr(const Ptr& ptr) { mPtrRegistry.insert(ptr); } void registerPtr(const Ptr& ptr);
void deregisterLiveCellRef(const LiveCellRefBase& ref) noexcept { mPtrRegistry.remove(ref); } void deregisterLiveCellRef(LiveCellRefBase& ref) noexcept;
void assignSaveFileRefNum(ESM::CellRef& ref) { mPtrRegistry.assign(ref); } void assignSaveFileRefNum(ESM::CellRef& ref) { mPtrRegistry.assign(ref); }