1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-30 22:45:34 +00:00

Merge branch 'ref_data_move' into 'master'

Add move constructor and move assignment operator to RefData (#5893)

See merge request OpenMW/openmw!705
This commit is contained in:
Andrei Kortunov 2021-04-05 10:51:28 +00:00
commit d3e185623d
11 changed files with 50 additions and 69 deletions

View file

@ -46,11 +46,6 @@ namespace MWClass
mStore.readState(inventory); mStore.readState(inventory);
} }
MWWorld::CustomData *ContainerCustomData::clone() const
{
return new ContainerCustomData (*this);
}
ContainerCustomData& ContainerCustomData::asContainerCustomData() ContainerCustomData& ContainerCustomData::asContainerCustomData()
{ {
return *this; return *this;
@ -72,7 +67,7 @@ namespace MWClass
MWWorld::LiveCellRef<ESM::Container> *ref = ptr.get<ESM::Container>(); MWWorld::LiveCellRef<ESM::Container> *ref = ptr.get<ESM::Container>();
// store // store
ptr.getRefData().setCustomData (std::make_unique<ContainerCustomData>(*ref->mBase, ptr.getCell()).release()); ptr.getRefData().setCustomData (std::make_unique<ContainerCustomData>(*ref->mBase, ptr.getCell()));
MWBase::Environment::get().getWorld()->addContainerScripts(ptr, ptr.getCell()); MWBase::Environment::get().getWorld()->addContainerScripts(ptr, ptr.getCell());
} }
@ -317,7 +312,7 @@ namespace MWClass
return; return;
const ESM::ContainerState& containerState = state.asContainerState(); const ESM::ContainerState& containerState = state.asContainerState();
ptr.getRefData().setCustomData(std::make_unique<ContainerCustomData>(containerState.mInventory).release()); ptr.getRefData().setCustomData(std::make_unique<ContainerCustomData>(containerState.mInventory));
} }
void Container::writeAdditionalState (const MWWorld::ConstPtr& ptr, ESM::ObjectState& state) const void Container::writeAdditionalState (const MWWorld::ConstPtr& ptr, ESM::ObjectState& state) const

View file

@ -13,15 +13,13 @@ namespace ESM
namespace MWClass namespace MWClass
{ {
class ContainerCustomData : public MWWorld::CustomData class ContainerCustomData : public MWWorld::TypedCustomData<ContainerCustomData>
{ {
MWWorld::ContainerStore mStore; MWWorld::ContainerStore mStore;
public: public:
ContainerCustomData(const ESM::Container& container, MWWorld::CellStore* cell); ContainerCustomData(const ESM::Container& container, MWWorld::CellStore* cell);
ContainerCustomData(const ESM::InventoryState& inventory); ContainerCustomData(const ESM::InventoryState& inventory);
MWWorld::CustomData *clone() const override;
ContainerCustomData& asContainerCustomData() override; ContainerCustomData& asContainerCustomData() override;
const ContainerCustomData& asContainerCustomData() const override; const ContainerCustomData& asContainerCustomData() const override;

View file

@ -51,14 +51,16 @@ namespace
namespace MWClass namespace MWClass
{ {
class CreatureCustomData : public MWWorld::CustomData class CreatureCustomData : public MWWorld::TypedCustomData<CreatureCustomData>
{ {
public: public:
MWMechanics::CreatureStats mCreatureStats; MWMechanics::CreatureStats mCreatureStats;
MWWorld::ContainerStore* mContainerStore; // may be InventoryStore for some creatures std::unique_ptr<MWWorld::ContainerStore> mContainerStore; // may be InventoryStore for some creatures
MWMechanics::Movement mMovement; MWMechanics::Movement mMovement;
MWWorld::CustomData *clone() const override; CreatureCustomData() = default;
CreatureCustomData(const CreatureCustomData& other);
CreatureCustomData(CreatureCustomData&& other) noexcept = default;
CreatureCustomData& asCreatureCustomData() override CreatureCustomData& asCreatureCustomData() override
{ {
@ -68,16 +70,13 @@ namespace MWClass
{ {
return *this; return *this;
} }
CreatureCustomData() : mContainerStore(nullptr) {}
virtual ~CreatureCustomData() { delete mContainerStore; }
}; };
MWWorld::CustomData *CreatureCustomData::clone() const CreatureCustomData::CreatureCustomData(const CreatureCustomData& other)
: mCreatureStats(other.mCreatureStats),
mContainerStore(other.mContainerStore->clone()),
mMovement(other.mMovement)
{ {
CreatureCustomData* cloned = new CreatureCustomData (*this);
cloned->mContainerStore = mContainerStore->clone();
return cloned;
} }
const Creature::GMST& Creature::getGmst() const Creature::GMST& Creature::getGmst()
@ -148,16 +147,16 @@ namespace MWClass
// inventory // inventory
bool hasInventory = hasInventoryStore(ptr); bool hasInventory = hasInventoryStore(ptr);
if (hasInventory) if (hasInventory)
data->mContainerStore = new MWWorld::InventoryStore(); data->mContainerStore = std::make_unique<MWWorld::InventoryStore>();
else else
data->mContainerStore = new MWWorld::ContainerStore(); data->mContainerStore = std::make_unique<MWWorld::ContainerStore>();
data->mCreatureStats.setGoldPool(ref->mBase->mData.mGold); data->mCreatureStats.setGoldPool(ref->mBase->mData.mGold);
data->mCreatureStats.setNeedRecalcDynamicStats(false); data->mCreatureStats.setNeedRecalcDynamicStats(false);
// store // store
ptr.getRefData().setCustomData(data.release()); ptr.getRefData().setCustomData(std::move(data));
getContainerStore(ptr).fill(ref->mBase->mInventory, ptr.getCellRef().getRefId()); getContainerStore(ptr).fill(ref->mBase->mInventory, ptr.getCellRef().getRefId());
@ -758,11 +757,11 @@ namespace MWClass
std::unique_ptr<CreatureCustomData> data (new CreatureCustomData); std::unique_ptr<CreatureCustomData> data (new CreatureCustomData);
if (hasInventoryStore(ptr)) if (hasInventoryStore(ptr))
data->mContainerStore = new MWWorld::InventoryStore(); data->mContainerStore = std::make_unique<MWWorld::InventoryStore>();
else else
data->mContainerStore = new MWWorld::ContainerStore(); data->mContainerStore = std::make_unique<MWWorld::ContainerStore>();
ptr.getRefData().setCustomData (data.release()); ptr.getRefData().setCustomData (std::move(data));
} }
} }
else else

View file

@ -10,15 +10,13 @@
namespace MWClass namespace MWClass
{ {
class CreatureLevListCustomData : public MWWorld::CustomData class CreatureLevListCustomData : public MWWorld::TypedCustomData<CreatureLevListCustomData>
{ {
public: public:
// actorId of the creature we spawned // actorId of the creature we spawned
int mSpawnActorId; int mSpawnActorId;
bool mSpawn; // Should a new creature be spawned? bool mSpawn; // Should a new creature be spawned?
MWWorld::CustomData *clone() const override;
CreatureLevListCustomData& asCreatureLevListCustomData() override CreatureLevListCustomData& asCreatureLevListCustomData() override
{ {
return *this; return *this;
@ -29,11 +27,6 @@ namespace MWClass
} }
}; };
MWWorld::CustomData *CreatureLevListCustomData::clone() const
{
return new CreatureLevListCustomData (*this);
}
std::string CreatureLevList::getName (const MWWorld::ConstPtr& ptr) const std::string CreatureLevList::getName (const MWWorld::ConstPtr& ptr) const
{ {
return ""; return "";
@ -138,11 +131,11 @@ namespace MWClass
{ {
if (!ptr.getRefData().getCustomData()) if (!ptr.getRefData().getCustomData())
{ {
std::unique_ptr<CreatureLevListCustomData> data (new CreatureLevListCustomData); std::unique_ptr<CreatureLevListCustomData> data = std::make_unique<CreatureLevListCustomData>();
data->mSpawnActorId = -1; data->mSpawnActorId = -1;
data->mSpawn = true; data->mSpawn = true;
ptr.getRefData().setCustomData(data.release()); ptr.getRefData().setCustomData(std::move(data));
} }
} }

View file

@ -31,13 +31,11 @@
namespace MWClass namespace MWClass
{ {
class DoorCustomData : public MWWorld::CustomData class DoorCustomData : public MWWorld::TypedCustomData<DoorCustomData>
{ {
public: public:
MWWorld::DoorState mDoorState = MWWorld::DoorState::Idle; MWWorld::DoorState mDoorState = MWWorld::DoorState::Idle;
MWWorld::CustomData *clone() const override;
DoorCustomData& asDoorCustomData() override DoorCustomData& asDoorCustomData() override
{ {
return *this; return *this;
@ -48,11 +46,6 @@ namespace MWClass
} }
}; };
MWWorld::CustomData *DoorCustomData::clone() const
{
return new DoorCustomData (*this);
}
void Door::insertObjectRendering (const MWWorld::Ptr& ptr, const std::string& model, MWRender::RenderingInterface& renderingInterface) const void Door::insertObjectRendering (const MWWorld::Ptr& ptr, const std::string& model, MWRender::RenderingInterface& renderingInterface) const
{ {
if (!model.empty()) if (!model.empty())
@ -327,8 +320,7 @@ namespace MWClass
{ {
if (!ptr.getRefData().getCustomData()) if (!ptr.getRefData().getCustomData())
{ {
std::unique_ptr<DoorCustomData> data(new DoorCustomData); ptr.getRefData().setCustomData(std::make_unique<DoorCustomData>());
ptr.getRefData().setCustomData(data.release());
} }
} }

View file

@ -246,15 +246,13 @@ namespace
namespace MWClass namespace MWClass
{ {
class NpcCustomData : public MWWorld::CustomData class NpcCustomData : public MWWorld::TypedCustomData<NpcCustomData>
{ {
public: public:
MWMechanics::NpcStats mNpcStats; MWMechanics::NpcStats mNpcStats;
MWMechanics::Movement mMovement; MWMechanics::Movement mMovement;
MWWorld::InventoryStore mInventoryStore; MWWorld::InventoryStore mInventoryStore;
MWWorld::CustomData *clone() const override;
NpcCustomData& asNpcCustomData() override NpcCustomData& asNpcCustomData() override
{ {
return *this; return *this;
@ -265,11 +263,6 @@ namespace MWClass
} }
}; };
MWWorld::CustomData *NpcCustomData::clone() const
{
return new NpcCustomData (*this);
}
const Npc::GMST& Npc::getGmst() const Npc::GMST& Npc::getGmst()
{ {
static GMST gmst; static GMST gmst;
@ -397,7 +390,7 @@ namespace MWClass
data->mNpcStats.setGoldPool(gold); data->mNpcStats.setGoldPool(gold);
// store // store
ptr.getRefData().setCustomData (data.release()); ptr.getRefData().setCustomData(std::move(data));
getInventoryStore(ptr).autoEquip(ptr); getInventoryStore(ptr).autoEquip(ptr);
} }
@ -1302,8 +1295,7 @@ namespace MWClass
if (!ptr.getRefData().getCustomData()) if (!ptr.getRefData().getCustomData())
{ {
// Create a CustomData, but don't fill it from ESM records (not needed) // Create a CustomData, but don't fill it from ESM records (not needed)
std::unique_ptr<NpcCustomData> data (new NpcCustomData); ptr.getRefData().setCustomData(std::make_unique<NpcCustomData>());
ptr.getRefData().setCustomData (data.release());
} }
} }
else else

View file

@ -153,7 +153,7 @@ namespace MWWorld
virtual ~ContainerStore(); virtual ~ContainerStore();
virtual ContainerStore* clone() { return new ContainerStore(*this); } virtual std::unique_ptr<ContainerStore> clone() { return std::make_unique<ContainerStore>(*this); }
ConstContainerStoreIterator cbegin (int mask = Type_All) const; ConstContainerStoreIterator cbegin (int mask = Type_All) const;
ConstContainerStoreIterator cend() const; ConstContainerStoreIterator cend() const;

View file

@ -1,6 +1,8 @@
#ifndef GAME_MWWORLD_CUSTOMDATA_H #ifndef GAME_MWWORLD_CUSTOMDATA_H
#define GAME_MWWORLD_CUSTOMDATA_H #define GAME_MWWORLD_CUSTOMDATA_H
#include <memory>
namespace MWClass namespace MWClass
{ {
class CreatureCustomData; class CreatureCustomData;
@ -19,7 +21,7 @@ namespace MWWorld
virtual ~CustomData() {} virtual ~CustomData() {}
virtual CustomData *clone() const = 0; virtual std::unique_ptr<CustomData> clone() const = 0;
// Fast version of dynamic_cast<X&>. Needs to be overridden in the respective class. // Fast version of dynamic_cast<X&>. Needs to be overridden in the respective class.
@ -38,6 +40,15 @@ namespace MWWorld
virtual MWClass::CreatureLevListCustomData& asCreatureLevListCustomData(); virtual MWClass::CreatureLevListCustomData& asCreatureLevListCustomData();
virtual const MWClass::CreatureLevListCustomData& asCreatureLevListCustomData() const; virtual const MWClass::CreatureLevListCustomData& asCreatureLevListCustomData() const;
}; };
template <class T>
struct TypedCustomData : CustomData
{
std::unique_ptr<CustomData> clone() const final
{
return std::make_unique<T>(*static_cast<const T*>(this));
}
};
} }
#endif #endif

View file

@ -123,7 +123,7 @@ namespace MWWorld
InventoryStore& operator= (const InventoryStore& store); InventoryStore& operator= (const InventoryStore& store);
InventoryStore* clone() override { return new InventoryStore(*this); } std::unique_ptr<ContainerStore> clone() override { return std::make_unique<InventoryStore>(*this); }
ContainerStoreIterator add (const Ptr& itemPtr, int count, const Ptr& actorPtr, bool allowAutoEquip = true, bool resolve = true) override; ContainerStoreIterator add (const Ptr& itemPtr, int count, const Ptr& actorPtr, bool allowAutoEquip = true, bool resolve = true) override;
///< Add the item pointed to by \a ptr to this container. (Stacks automatically if needed) ///< Add the item pointed to by \a ptr to this container. (Stacks automatically if needed)

View file

@ -40,8 +40,6 @@ namespace MWWorld
void RefData::cleanup() void RefData::cleanup()
{ {
mBaseNode = nullptr; mBaseNode = nullptr;
delete mCustomData;
mCustomData = nullptr; mCustomData = nullptr;
} }
@ -223,21 +221,20 @@ namespace MWWorld
return mPosition; return mPosition;
} }
void RefData::setCustomData (CustomData *data) void RefData::setCustomData(std::unique_ptr<CustomData>&& value) noexcept
{ {
mChanged = true; // We do not currently track CustomData, so assume anything with a CustomData is changed mChanged = true; // We do not currently track CustomData, so assume anything with a CustomData is changed
delete mCustomData; mCustomData = std::move(value);
mCustomData = data;
} }
CustomData *RefData::getCustomData() CustomData *RefData::getCustomData()
{ {
return mCustomData; return mCustomData.get();
} }
const CustomData *RefData::getCustomData() const const CustomData *RefData::getCustomData() const
{ {
return mCustomData; return mCustomData.get();
} }
bool RefData::hasChanged() const bool RefData::hasChanged() const

View file

@ -5,8 +5,10 @@
#include <components/esm/animationstate.hpp> #include <components/esm/animationstate.hpp>
#include "../mwscript/locals.hpp" #include "../mwscript/locals.hpp"
#include "../mwworld/customdata.hpp"
#include <string> #include <string>
#include <memory>
namespace SceneUtil namespace SceneUtil
{ {
@ -44,7 +46,7 @@ namespace MWWorld
ESM::AnimationState mAnimationState; ESM::AnimationState mAnimationState;
CustomData *mCustomData; std::unique_ptr<CustomData> mCustomData;
void copy (const RefData& refData); void copy (const RefData& refData);
@ -68,6 +70,7 @@ namespace MWWorld
/// perform these operations). /// perform these operations).
RefData (const RefData& refData); RefData (const RefData& refData);
RefData (RefData&& other) noexcept = default;
~RefData(); ~RefData();
@ -76,6 +79,7 @@ namespace MWWorld
/// perform this operations). /// perform this operations).
RefData& operator= (const RefData& refData); RefData& operator= (const RefData& refData);
RefData& operator= (RefData&& other) noexcept = default;
/// Return base node (can be a null pointer). /// Return base node (can be a null pointer).
SceneUtil::PositionAttitudeTransform* getBaseNode(); SceneUtil::PositionAttitudeTransform* getBaseNode();
@ -117,7 +121,7 @@ namespace MWWorld
void setPosition (const ESM::Position& pos); void setPosition (const ESM::Position& pos);
const ESM::Position& getPosition() const; const ESM::Position& getPosition() const;
void setCustomData (CustomData *data); void setCustomData(std::unique_ptr<CustomData>&& value) noexcept;
///< Set custom data (potentially replacing old custom data). The ownership of \a data is ///< Set custom data (potentially replacing old custom data). The ownership of \a data is
/// transferred to this. /// transferred to this.