mirror of
https://github.com/OpenMW/openmw.git
synced 2025-12-08 20:04:32 +00:00
Avoid manual memory management for MWMechanics::DerivedClassStorage
This commit is contained in:
parent
d2b7253c7f
commit
b8937a493a
3 changed files with 23 additions and 36 deletions
|
|
@ -419,9 +419,9 @@ void AiSequence::stack (const AiPackage& package, const MWWorld::Ptr& actor, boo
|
||||||
// Make sure that temporary storage is empty
|
// Make sure that temporary storage is empty
|
||||||
if (cancelOther)
|
if (cancelOther)
|
||||||
{
|
{
|
||||||
mAiState.moveIn(new AiCombatStorage());
|
mAiState.moveIn(std::make_unique<AiCombatStorage>());
|
||||||
mAiState.moveIn(new AiFollowStorage());
|
mAiState.moveIn(std::make_unique<AiFollowStorage>());
|
||||||
mAiState.moveIn(new AiWanderStorage());
|
mAiState.moveIn(std::make_unique<AiWanderStorage>());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@
|
||||||
#include "aistatefwd.hpp"
|
#include "aistatefwd.hpp"
|
||||||
#include "aitemporarybase.hpp"
|
#include "aitemporarybase.hpp"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
namespace MWMechanics
|
namespace MWMechanics
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
@ -16,24 +18,20 @@ namespace MWMechanics
|
||||||
class DerivedClassStorage
|
class DerivedClassStorage
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
Base* mStorage;
|
std::unique_ptr<Base> mStorage;
|
||||||
|
|
||||||
//if needed you have to provide a clone member function
|
|
||||||
DerivedClassStorage( const DerivedClassStorage& other );
|
|
||||||
DerivedClassStorage& operator=( const DerivedClassStorage& );
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/// \brief returns reference to stored object or deletes it and creates a fitting
|
/// \brief returns reference to stored object or deletes it and creates a fitting
|
||||||
template< class Derived >
|
template< class Derived >
|
||||||
Derived& get()
|
Derived& get()
|
||||||
{
|
{
|
||||||
Derived* result = dynamic_cast<Derived*>(mStorage);
|
Derived* result = dynamic_cast<Derived*>(mStorage.get());
|
||||||
|
|
||||||
if(!result)
|
if (result == nullptr)
|
||||||
{
|
{
|
||||||
if(mStorage)
|
auto storage = std::make_unique<Derived>();
|
||||||
delete mStorage;
|
result = storage.get();
|
||||||
mStorage = result = new Derived();
|
mStorage = std::move(storage);
|
||||||
}
|
}
|
||||||
|
|
||||||
//return a reference to the (new allocated) object
|
//return a reference to the (new allocated) object
|
||||||
|
|
@ -43,7 +41,7 @@ namespace MWMechanics
|
||||||
template< class Derived >
|
template< class Derived >
|
||||||
void copy(DerivedClassStorage& destination) const
|
void copy(DerivedClassStorage& destination) const
|
||||||
{
|
{
|
||||||
Derived* result = dynamic_cast<Derived*>(mStorage);
|
Derived* result = dynamic_cast<Derived*>(mStorage.get());
|
||||||
if (result != nullptr)
|
if (result != nullptr)
|
||||||
destination.store<Derived>(*result);
|
destination.store<Derived>(*result);
|
||||||
}
|
}
|
||||||
|
|
@ -51,25 +49,14 @@ namespace MWMechanics
|
||||||
template< class Derived >
|
template< class Derived >
|
||||||
void store( const Derived& payload )
|
void store( const Derived& payload )
|
||||||
{
|
{
|
||||||
if(mStorage)
|
mStorage = std::make_unique<Derived>(payload);
|
||||||
delete mStorage;
|
|
||||||
mStorage = new Derived(payload);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \brief takes ownership of the passed object
|
/// \brief takes ownership of the passed object
|
||||||
template< class Derived >
|
template <class Derived>
|
||||||
void moveIn( Derived* p )
|
void moveIn(std::unique_ptr<Derived>&& storage)
|
||||||
{
|
{
|
||||||
if(mStorage)
|
mStorage = std::move(storage);
|
||||||
delete mStorage;
|
|
||||||
mStorage = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
DerivedClassStorage():mStorage(nullptr){}
|
|
||||||
~DerivedClassStorage()
|
|
||||||
{
|
|
||||||
if(mStorage)
|
|
||||||
delete mStorage;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -767,7 +767,7 @@ namespace MWMechanics
|
||||||
|
|
||||||
ToWorldCoordinates(dest, actor.getCell()->getCell());
|
ToWorldCoordinates(dest, actor.getCell()->getCell());
|
||||||
|
|
||||||
state.moveIn(new AiWanderStorage());
|
state.moveIn(std::make_unique<AiWanderStorage>());
|
||||||
|
|
||||||
osg::Vec3f pos(static_cast<float>(dest.mX), static_cast<float>(dest.mY), static_cast<float>(dest.mZ));
|
osg::Vec3f pos(static_cast<float>(dest.mX), static_cast<float>(dest.mY), static_cast<float>(dest.mZ));
|
||||||
MWBase::Environment::get().getWorld()->moveObject(actor, pos);
|
MWBase::Environment::get().getWorld()->moveObject(actor, pos);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue