1
0

Moved a few objects to unique_ptr

This commit is contained in:
tycho 2014-10-10 15:33:19 +01:00
parent a47d9e5334
commit 473c0425d3
4 changed files with 41 additions and 39 deletions

View File

@ -1160,7 +1160,7 @@ static int tolua_cWorld_QueueTask(lua_State * tolua_S)
return lua_do_error(tolua_S, "Error in function call '#funcname#': Could not get function reference of parameter #1"); return lua_do_error(tolua_S, "Error in function call '#funcname#': Could not get function reference of parameter #1");
} }
self->QueueTask(new cLuaWorldTask(*Plugin, FnRef)); self->QueueTask(make_unique<cLuaWorldTask>(*Plugin, FnRef));
return 0; return 0;
} }

View File

@ -419,6 +419,12 @@ typename std::enable_if<std::is_arithmetic<T>::value, C>::type CeilC(T a_Value)
//temporary replacement for std::make_unique until we get c++14
template< class T, class... Args >
std::unique_ptr<T> make_unique( Args&&... args )
{
return std::unique_ptr<T>(new T(args...));
}
#ifndef TOLUA_TEMPLATE_BIND #ifndef TOLUA_TEMPLATE_BIND
@ -436,3 +442,4 @@ typename std::enable_if<std::is_arithmetic<T>::value, C>::type CeilC(T a_Value)
#include "BlockInfo.h" #include "BlockInfo.h"

View File

@ -256,14 +256,14 @@ cWorld::cWorld(const AString & a_WorldName, eDimension a_Dimension, const AStrin
m_IsDeepSnowEnabled(false), m_IsDeepSnowEnabled(false),
m_ShouldLavaSpawnFire(true), m_ShouldLavaSpawnFire(true),
m_VillagersShouldHarvestCrops(true), m_VillagersShouldHarvestCrops(true),
m_SimulatorManager(NULL), m_SimulatorManager(),
m_SandSimulator(NULL), m_SandSimulator(),
m_WaterSimulator(NULL), m_WaterSimulator(NULL),
m_LavaSimulator(NULL), m_LavaSimulator(nullptr),
m_FireSimulator(NULL), m_FireSimulator(),
m_RedstoneSimulator(NULL), m_RedstoneSimulator(NULL),
m_MaxPlayers(10), m_MaxPlayers(10),
m_ChunkMap(NULL), m_ChunkMap(),
m_bAnimals(true), m_bAnimals(true),
m_Weather(eWeather_Sunny), m_Weather(eWeather_Sunny),
m_WeatherInterval(24000), // Guaranteed 1 day of sunshine at server start :) m_WeatherInterval(24000), // Guaranteed 1 day of sunshine at server start :)
@ -303,11 +303,8 @@ cWorld::cWorld(const AString & a_WorldName, eDimension a_Dimension, const AStrin
cWorld::~cWorld() cWorld::~cWorld()
{ {
delete m_SimulatorManager; m_SimulatorManager = NULL;
delete m_SandSimulator; m_SandSimulator = NULL;
delete m_WaterSimulator; m_WaterSimulator = NULL; delete m_WaterSimulator; m_WaterSimulator = NULL;
delete m_LavaSimulator; m_LavaSimulator = NULL; delete m_LavaSimulator; m_LavaSimulator = NULL;
delete m_FireSimulator; m_FireSimulator = NULL;
delete m_RedstoneSimulator; m_RedstoneSimulator = NULL; delete m_RedstoneSimulator; m_RedstoneSimulator = NULL;
UnloadUnusedChunks(); UnloadUnusedChunks();
@ -319,8 +316,6 @@ cWorld::~cWorld()
Serializer.Save(); Serializer.Save();
m_MapManager.SaveMapData(); m_MapManager.SaveMapData();
delete m_ChunkMap;
} }
@ -631,7 +626,7 @@ void cWorld::Start(void)
InitialiseAndLoadMobSpawningValues(IniFile); InitialiseAndLoadMobSpawningValues(IniFile);
SetTimeOfDay(IniFile.GetValueSetI("General", "TimeInTicks", m_TimeOfDay)); SetTimeOfDay(IniFile.GetValueSetI("General", "TimeInTicks", m_TimeOfDay));
m_ChunkMap = new cChunkMap(this); m_ChunkMap = make_unique<cChunkMap>(this);
m_LastSave = 0; m_LastSave = 0;
m_LastUnload = 0; m_LastUnload = 0;
@ -641,16 +636,16 @@ void cWorld::Start(void)
m_BlockTickQueueCopy.reserve(1000); m_BlockTickQueueCopy.reserve(1000);
// Simulators: // Simulators:
m_SimulatorManager = new cSimulatorManager(*this); m_SimulatorManager = make_unique<cSimulatorManager>(*this);
m_WaterSimulator = InitializeFluidSimulator(IniFile, "Water", E_BLOCK_WATER, E_BLOCK_STATIONARY_WATER); m_WaterSimulator = InitializeFluidSimulator(IniFile, "Water", E_BLOCK_WATER, E_BLOCK_STATIONARY_WATER);
m_LavaSimulator = InitializeFluidSimulator(IniFile, "Lava", E_BLOCK_LAVA, E_BLOCK_STATIONARY_LAVA); m_LavaSimulator = InitializeFluidSimulator(IniFile, "Lava", E_BLOCK_LAVA, E_BLOCK_STATIONARY_LAVA);
m_SandSimulator = new cSandSimulator(*this, IniFile); m_SandSimulator = make_unique<cSandSimulator>(*this, IniFile);
m_FireSimulator = new cFireSimulator(*this, IniFile); m_FireSimulator = make_unique<cFireSimulator>(*this, IniFile);
m_RedstoneSimulator = InitializeRedstoneSimulator(IniFile); m_RedstoneSimulator = InitializeRedstoneSimulator(IniFile);
// Water, Lava and Redstone simulators get registered in their initialize function. // Water, Lava and Redstone simulators get registered in their initialize function.
m_SimulatorManager->RegisterSimulator(m_SandSimulator, 1); m_SimulatorManager->RegisterSimulator(m_SandSimulator.get(), 1);
m_SimulatorManager->RegisterSimulator(m_FireSimulator, 1); m_SimulatorManager->RegisterSimulator(m_FireSimulator.get(), 1);
m_Lighting.Start(this); m_Lighting.Start(this);
m_Storage.Start(this, m_StorageSchema, m_StorageCompressionFactor); m_Storage.Start(this, m_StorageSchema, m_StorageCompressionFactor);
@ -1059,7 +1054,6 @@ void cWorld::TickQueuedTasks(void)
for (cTasks::iterator itr = Tasks.begin(), end = Tasks.end(); itr != end; ++itr) for (cTasks::iterator itr = Tasks.begin(), end = Tasks.end(); itr != end; ++itr)
{ {
(*itr)->Run(*this); (*itr)->Run(*this);
delete *itr;
} // for itr - m_Tasks[] } // for itr - m_Tasks[]
} }
@ -1073,18 +1067,19 @@ void cWorld::TickScheduledTasks(void)
cScheduledTasks Tasks; cScheduledTasks Tasks;
{ {
cCSLock Lock(m_CSScheduledTasks); cCSLock Lock(m_CSScheduledTasks);
while (!m_ScheduledTasks.empty() && (m_ScheduledTasks.front()->m_TargetTick < m_WorldAge)) std::move(
{ m_ScheduledTasks.begin(),
Tasks.push_back(m_ScheduledTasks.front()); std::find_if(
m_ScheduledTasks.pop_front(); m_ScheduledTasks.begin(),
} m_ScheduledTasks.end(),
[m_WorldAge] (std::unique_ptr<cScheduledTask>& Task) { return Task->m_TargetTick < m_WorldAge;}),
std::back_inserter(Tasks));
} }
// Execute and delete each task: // Execute and delete each task:
for (cScheduledTasks::iterator itr = Tasks.begin(), end = Tasks.end(); itr != end; ++itr) for (cScheduledTasks::iterator itr = Tasks.begin(), end = Tasks.end(); itr != end; ++itr)
{ {
(*itr)->m_Task->Run(*this); (*itr)->m_Task->Run(*this);
delete *itr;
} // for itr - m_Tasks[] } // for itr - m_Tasks[]
} }
@ -2593,7 +2588,7 @@ void cWorld::UnloadUnusedChunks(void)
void cWorld::QueueUnloadUnusedChunks(void) void cWorld::QueueUnloadUnusedChunks(void)
{ {
QueueTask(new cWorld::cTaskUnloadUnusedChunks); QueueTask(make_unique<cWorld::cTaskUnloadUnusedChunks>());
} }
@ -3049,17 +3044,17 @@ void cWorld::SaveAllChunks(void)
void cWorld::QueueSaveAllChunks(void) void cWorld::QueueSaveAllChunks(void)
{ {
QueueTask(new cWorld::cTaskSaveAllChunks); QueueTask(make_unique<cWorld::cTaskSaveAllChunks>());
} }
void cWorld::QueueTask(cTask * a_Task) void cWorld::QueueTask(std::unique_ptr<cTask> a_Task)
{ {
cCSLock Lock(m_CSTasks); cCSLock Lock(m_CSTasks);
m_Tasks.push_back(a_Task); m_Tasks.push_back(std::move(a_Task));
} }
@ -3076,11 +3071,11 @@ void cWorld::ScheduleTask(int a_DelayTicks, cTask * a_Task)
{ {
if ((*itr)->m_TargetTick >= TargetTick) if ((*itr)->m_TargetTick >= TargetTick)
{ {
m_ScheduledTasks.insert(itr, new cScheduledTask(TargetTick, a_Task)); m_ScheduledTasks.insert(itr, make_unique<cScheduledTask>(TargetTick, a_Task));
return; return;
} }
} }
m_ScheduledTasks.push_back(new cScheduledTask(TargetTick, a_Task)); m_ScheduledTasks.push_back(make_unique<cScheduledTask>(TargetTick, a_Task));
} }

View File

@ -103,7 +103,7 @@ public:
virtual void Run(cWorld & a_World) = 0; virtual void Run(cWorld & a_World) = 0;
} ; } ;
typedef std::vector<cTask *> cTasks; typedef std::vector<std::unique_ptr<cTask>> cTasks;
class cTaskSaveAllChunks : class cTaskSaveAllChunks :
@ -506,7 +506,7 @@ public:
// tolua_end // tolua_end
inline cSimulatorManager * GetSimulatorManager(void) { return m_SimulatorManager; } inline cSimulatorManager * GetSimulatorManager(void) { return m_SimulatorManager.get(); }
inline cFluidSimulator * GetWaterSimulator(void) { return m_WaterSimulator; } inline cFluidSimulator * GetWaterSimulator(void) { return m_WaterSimulator; }
inline cFluidSimulator * GetLavaSimulator (void) { return m_LavaSimulator; } inline cFluidSimulator * GetLavaSimulator (void) { return m_LavaSimulator; }
@ -671,7 +671,7 @@ public:
void QueueSaveAllChunks(void); // tolua_export void QueueSaveAllChunks(void); // tolua_export
/** Queues a task onto the tick thread. The task object will be deleted once the task is finished */ /** Queues a task onto the tick thread. The task object will be deleted once the task is finished */
void QueueTask(cTask * a_Task); // Exported in ManualBindings.cpp void QueueTask(std::unique_ptr<cTask> a_Task); // Exported in ManualBindings.cpp
/** Queues a task onto the tick thread, with the specified delay. /** Queues a task onto the tick thread, with the specified delay.
The task object will be deleted once the task is finished */ The task object will be deleted once the task is finished */
@ -764,7 +764,7 @@ public:
cChunkGenerator & GetGenerator(void) { return m_Generator; } cChunkGenerator & GetGenerator(void) { return m_Generator; }
cWorldStorage & GetStorage (void) { return m_Storage; } cWorldStorage & GetStorage (void) { return m_Storage; }
cChunkMap * GetChunkMap (void) { return m_ChunkMap; } cChunkMap * GetChunkMap (void) { return m_ChunkMap.get(); }
/** Sets the blockticking to start at the specified block. Only one blocktick per chunk may be set, second call overwrites the first call */ /** Sets the blockticking to start at the specified block. Only one blocktick per chunk may be set, second call overwrites the first call */
void SetNextBlockTick(int a_BlockX, int a_BlockY, int a_BlockZ); // tolua_export void SetNextBlockTick(int a_BlockX, int a_BlockY, int a_BlockZ); // tolua_export
@ -861,7 +861,7 @@ private:
} }
}; };
typedef std::list<cScheduledTask *> cScheduledTasks; typedef std::list<std::unique_ptr<cScheduledTask>> cScheduledTasks;
AString m_WorldName; AString m_WorldName;
@ -913,11 +913,11 @@ private:
std::vector<BlockTickQueueItem *> m_BlockTickQueue; std::vector<BlockTickQueueItem *> m_BlockTickQueue;
std::vector<BlockTickQueueItem *> m_BlockTickQueueCopy; // Second is for safely removing the objects from the queue std::vector<BlockTickQueueItem *> m_BlockTickQueueCopy; // Second is for safely removing the objects from the queue
cSimulatorManager * m_SimulatorManager; std::unique_ptr<cSimulatorManager> m_SimulatorManager;
cSandSimulator * m_SandSimulator; std::unique_ptr<cSandSimulator> m_SandSimulator;
cFluidSimulator * m_WaterSimulator; cFluidSimulator * m_WaterSimulator;
cFluidSimulator * m_LavaSimulator; cFluidSimulator * m_LavaSimulator;
cFireSimulator * m_FireSimulator; std::unique_ptr<cFireSimulator> m_FireSimulator;
cRedstoneSimulator<cChunk, cWorld> * m_RedstoneSimulator; cRedstoneSimulator<cChunk, cWorld> * m_RedstoneSimulator;
cCriticalSection m_CSPlayers; cCriticalSection m_CSPlayers;
@ -927,7 +927,7 @@ private:
unsigned int m_MaxPlayers; unsigned int m_MaxPlayers;
cChunkMap * m_ChunkMap; std::unique_ptr<cChunkMap> m_ChunkMap;
bool m_bAnimals; bool m_bAnimals;
std::set<eMonsterType> m_AllowedMobs; std::set<eMonsterType> m_AllowedMobs;