Deregister only registered LiveCellRefBase

pull/3236/head
elsid 7 months ago
parent 4565152b3d
commit d998faec1b
No known key found for this signature in database
GPG Key ID: 4DE04C198CBA7625

@ -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()
{
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)

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

@ -3,6 +3,7 @@
#include <algorithm>
#include <cassert>
#include <optional>
#include <stdexcept>
#include <components/debug/debuglog.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));
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)

@ -77,9 +77,9 @@ namespace MWWorld
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); }

Loading…
Cancel
Save