1
0

Merge pull request #1529 from mc-server/unique_ptr

Moved a few objects to unique_ptr
This commit is contained in:
Mattes D 2014-10-12 22:08:28 +02:00
commit 53410a9271
4 changed files with 45 additions and 42 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");
}
self->QueueTask(new cLuaWorldTask(*Plugin, FnRef));
self->QueueTask(make_unique<cLuaWorldTask>(*Plugin, FnRef));
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
@ -436,3 +442,4 @@ typename std::enable_if<std::is_arithmetic<T>::value, C>::type CeilC(T a_Value)
#include "BlockInfo.h"

View File

@ -256,14 +256,14 @@ cWorld::cWorld(const AString & a_WorldName, eDimension a_Dimension, const AStrin
m_IsDeepSnowEnabled(false),
m_ShouldLavaSpawnFire(true),
m_VillagersShouldHarvestCrops(true),
m_SimulatorManager(NULL),
m_SandSimulator(NULL),
m_SimulatorManager(),
m_SandSimulator(),
m_WaterSimulator(NULL),
m_LavaSimulator(NULL),
m_FireSimulator(NULL),
m_LavaSimulator(nullptr),
m_FireSimulator(),
m_RedstoneSimulator(NULL),
m_MaxPlayers(10),
m_ChunkMap(NULL),
m_ChunkMap(),
m_bAnimals(true),
m_Weather(eWeather_Sunny),
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()
{
delete m_SimulatorManager; m_SimulatorManager = NULL;
delete m_SandSimulator; m_SandSimulator = NULL;
delete m_WaterSimulator; m_WaterSimulator = NULL;
delete m_LavaSimulator; m_LavaSimulator = NULL;
delete m_FireSimulator; m_FireSimulator = NULL;
delete m_RedstoneSimulator; m_RedstoneSimulator = NULL;
UnloadUnusedChunks();
@ -319,8 +316,6 @@ cWorld::~cWorld()
Serializer.Save();
m_MapManager.SaveMapData();
delete m_ChunkMap;
}
@ -631,7 +626,7 @@ void cWorld::Start(void)
InitialiseAndLoadMobSpawningValues(IniFile);
SetTimeOfDay(IniFile.GetValueSetI("General", "TimeInTicks", m_TimeOfDay));
m_ChunkMap = new cChunkMap(this);
m_ChunkMap = make_unique<cChunkMap>(this);
m_LastSave = 0;
m_LastUnload = 0;
@ -641,16 +636,16 @@ void cWorld::Start(void)
m_BlockTickQueueCopy.reserve(1000);
// 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_LavaSimulator = InitializeFluidSimulator(IniFile, "Lava", E_BLOCK_LAVA, E_BLOCK_STATIONARY_LAVA);
m_SandSimulator = new cSandSimulator(*this, IniFile);
m_FireSimulator = new cFireSimulator(*this, IniFile);
m_SandSimulator = make_unique<cSandSimulator>(*this, IniFile);
m_FireSimulator = make_unique<cFireSimulator>(*this, IniFile);
m_RedstoneSimulator = InitializeRedstoneSimulator(IniFile);
// Water, Lava and Redstone simulators get registered in their initialize function.
m_SimulatorManager->RegisterSimulator(m_SandSimulator, 1);
m_SimulatorManager->RegisterSimulator(m_FireSimulator, 1);
m_SimulatorManager->RegisterSimulator(m_SandSimulator.get(), 1);
m_SimulatorManager->RegisterSimulator(m_FireSimulator.get(), 1);
m_Lighting.Start(this);
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)
{
(*itr)->Run(*this);
delete *itr;
} // for itr - m_Tasks[]
}
@ -1069,22 +1063,24 @@ void cWorld::TickQueuedTasks(void)
void cWorld::TickScheduledTasks(void)
{
// Make a copy of the tasks to avoid deadlocks on accessing m_Tasks
// Move the tasks to be executed to a seperate vector to avoid deadlocks on accessing m_Tasks
cScheduledTasks Tasks;
{
cCSLock Lock(m_CSScheduledTasks);
while (!m_ScheduledTasks.empty() && (m_ScheduledTasks.front()->m_TargetTick < m_WorldAge))
{
Tasks.push_back(m_ScheduledTasks.front());
m_ScheduledTasks.pop_front();
}
auto WorldAge = m_WorldAge;
std::move(
m_ScheduledTasks.begin(),
std::find_if(
m_ScheduledTasks.begin(),
m_ScheduledTasks.end(),
[WorldAge] (std::unique_ptr<cScheduledTask> & Task) { return Task->m_TargetTick < WorldAge;}),
std::back_inserter(Tasks));
}
// Execute and delete each task:
for (cScheduledTasks::iterator itr = Tasks.begin(), end = Tasks.end(); itr != end; ++itr)
{
(*itr)->m_Task->Run(*this);
delete *itr;
} // for itr - m_Tasks[]
}
@ -2593,7 +2589,7 @@ void cWorld::UnloadUnusedChunks(void)
void cWorld::QueueUnloadUnusedChunks(void)
{
QueueTask(new cWorld::cTaskUnloadUnusedChunks);
QueueTask(make_unique<cWorld::cTaskUnloadUnusedChunks>());
}
@ -3049,17 +3045,17 @@ void cWorld::SaveAllChunks(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);
m_Tasks.push_back(a_Task);
m_Tasks.push_back(std::move(a_Task));
}
@ -3076,11 +3072,11 @@ void cWorld::ScheduleTask(int a_DelayTicks, cTask * a_Task)
{
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;
}
}
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;
} ;
typedef std::vector<cTask *> cTasks;
typedef std::vector<std::unique_ptr<cTask>> cTasks;
class cTaskSaveAllChunks :
@ -506,7 +506,7 @@ public:
// 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 * GetLavaSimulator (void) { return m_LavaSimulator; }
@ -671,7 +671,7 @@ public:
void QueueSaveAllChunks(void); // tolua_export
/** 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.
The task object will be deleted once the task is finished */
@ -764,7 +764,7 @@ public:
cChunkGenerator & GetGenerator(void) { return m_Generator; }
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 */
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;
@ -913,11 +913,11 @@ private:
std::vector<BlockTickQueueItem *> m_BlockTickQueue;
std::vector<BlockTickQueueItem *> m_BlockTickQueueCopy; // Second is for safely removing the objects from the queue
cSimulatorManager * m_SimulatorManager;
cSandSimulator * m_SandSimulator;
std::unique_ptr<cSimulatorManager> m_SimulatorManager;
std::unique_ptr<cSandSimulator> m_SandSimulator;
cFluidSimulator * m_WaterSimulator;
cFluidSimulator * m_LavaSimulator;
cFireSimulator * m_FireSimulator;
std::unique_ptr<cFireSimulator> m_FireSimulator;
cRedstoneSimulator<cChunk, cWorld> * m_RedstoneSimulator;
cCriticalSection m_CSPlayers;
@ -927,7 +927,7 @@ private:
unsigned int m_MaxPlayers;
cChunkMap * m_ChunkMap;
std::unique_ptr<cChunkMap> m_ChunkMap;
bool m_bAnimals;
std::set<eMonsterType> m_AllowedMobs;