mirror of
https://github.com/OpenMW/openmw.git
synced 2025-06-29 23:11:34 +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
|
||||
if (cancelOther)
|
||||
{
|
||||
mAiState.moveIn(new AiCombatStorage());
|
||||
mAiState.moveIn(new AiFollowStorage());
|
||||
mAiState.moveIn(new AiWanderStorage());
|
||||
mAiState.moveIn(std::make_unique<AiCombatStorage>());
|
||||
mAiState.moveIn(std::make_unique<AiFollowStorage>());
|
||||
mAiState.moveIn(std::make_unique<AiWanderStorage>());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
#include "aistatefwd.hpp"
|
||||
#include "aitemporarybase.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace MWMechanics
|
||||
{
|
||||
|
||||
|
@ -14,28 +16,24 @@ namespace MWMechanics
|
|||
*/
|
||||
template< class Base >
|
||||
class DerivedClassStorage
|
||||
{
|
||||
{
|
||||
private:
|
||||
Base* mStorage;
|
||||
|
||||
//if needed you have to provide a clone member function
|
||||
DerivedClassStorage( const DerivedClassStorage& other );
|
||||
DerivedClassStorage& operator=( const DerivedClassStorage& );
|
||||
|
||||
std::unique_ptr<Base> mStorage;
|
||||
|
||||
public:
|
||||
/// \brief returns reference to stored object or deletes it and creates a fitting
|
||||
template< class Derived >
|
||||
Derived& get()
|
||||
{
|
||||
Derived* result = dynamic_cast<Derived*>(mStorage);
|
||||
|
||||
if(!result)
|
||||
Derived* result = dynamic_cast<Derived*>(mStorage.get());
|
||||
|
||||
if (result == nullptr)
|
||||
{
|
||||
if(mStorage)
|
||||
delete mStorage;
|
||||
mStorage = result = new Derived();
|
||||
auto storage = std::make_unique<Derived>();
|
||||
result = storage.get();
|
||||
mStorage = std::move(storage);
|
||||
}
|
||||
|
||||
|
||||
//return a reference to the (new allocated) object
|
||||
return *result;
|
||||
}
|
||||
|
@ -43,33 +41,22 @@ namespace MWMechanics
|
|||
template< class Derived >
|
||||
void copy(DerivedClassStorage& destination) const
|
||||
{
|
||||
Derived* result = dynamic_cast<Derived*>(mStorage);
|
||||
Derived* result = dynamic_cast<Derived*>(mStorage.get());
|
||||
if (result != nullptr)
|
||||
destination.store<Derived>(*result);
|
||||
}
|
||||
|
||||
|
||||
template< class Derived >
|
||||
void store( const Derived& payload )
|
||||
{
|
||||
if(mStorage)
|
||||
delete mStorage;
|
||||
mStorage = new Derived(payload);
|
||||
}
|
||||
|
||||
/// \brief takes ownership of the passed object
|
||||
template< class Derived >
|
||||
void moveIn( Derived* p )
|
||||
{
|
||||
if(mStorage)
|
||||
delete mStorage;
|
||||
mStorage = p;
|
||||
mStorage = std::make_unique<Derived>(payload);
|
||||
}
|
||||
|
||||
DerivedClassStorage():mStorage(nullptr){}
|
||||
~DerivedClassStorage()
|
||||
/// \brief takes ownership of the passed object
|
||||
template <class Derived>
|
||||
void moveIn(std::unique_ptr<Derived>&& storage)
|
||||
{
|
||||
if(mStorage)
|
||||
delete mStorage;
|
||||
mStorage = std::move(storage);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -767,7 +767,7 @@ namespace MWMechanics
|
|||
|
||||
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));
|
||||
MWBase::Environment::get().getWorld()->moveObject(actor, pos);
|
||||
|
|
Loading…
Reference in a new issue