1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

unique_ptr

This commit is contained in:
Lars 2025-06-03 17:05:28 -07:00
parent 1771b8c9a1
commit f00e53d398
4 changed files with 23 additions and 25 deletions

View file

@ -399,7 +399,7 @@ void ClientMap::updateDrawList()
// Loop through all blocks // Loop through all blocks
for (const auto &entry : m_blocks) { for (const auto &entry : m_blocks) {
MapBlock *block = entry.second; MapBlock *block = entry.second.get();
MapBlockMesh *mesh = block->mesh; MapBlockMesh *mesh = block->mesh;
// Calculate the coordinates for range and frustum culling // Calculate the coordinates for range and frustum culling
@ -699,7 +699,7 @@ void ClientMap::touchMapBlocks()
u32 blocks_in_range_with_mesh = 0; u32 blocks_in_range_with_mesh = 0;
for (const auto &entry : m_blocks) { for (const auto &entry : m_blocks) {
MapBlock *block = entry.second; MapBlock *block = entry.second.get();
MapBlockMesh *mesh = block->mesh; MapBlockMesh *mesh = block->mesh;
// Calculate the coordinates for range and frustum culling // Calculate the coordinates for range and frustum culling
@ -1484,7 +1484,7 @@ void ClientMap::updateDrawListShadow(v3f shadow_light_pos, v3f shadow_light_dir,
u32 blocks_in_range_with_mesh = 0; u32 blocks_in_range_with_mesh = 0;
for (const auto &entry : m_blocks) { for (const auto &entry : m_blocks) {
MapBlock *block = entry.second; MapBlock *block = entry.second.get();
MapBlockMesh *mesh = block->mesh; MapBlockMesh *mesh = block->mesh;
if (!mesh) { if (!mesh) {
// Ignore if mesh doesn't exist // Ignore if mesh doesn't exist

View file

@ -32,9 +32,7 @@ Map::~Map()
/* /*
Free all MapBlocks Free all MapBlocks
*/ */
for (auto &entry : m_blocks) { m_blocks.clear();
delete entry.second;
}
} }
void Map::addEventReceiver(MapEventReceiver *event_receiver) void Map::addEventReceiver(MapEventReceiver *event_receiver)
@ -54,19 +52,20 @@ void Map::dispatchEvent(const MapEditEvent &event)
} }
} }
MapBlock* Map::createBlankBlockNoInsert(v3s16 p) std::unique_ptr<MapBlock> Map::createBlankBlockNoInsert(v3s16 p)
{ {
if (blockpos_over_max_limit(v3s16(p))) if (blockpos_over_max_limit(v3s16(p)))
throw InvalidPositionException("createBlankBlockNoInsert(): pos over max mapgen limit"); throw InvalidPositionException("createBlankBlockNoInsert(): pos over max mapgen limit");
return new MapBlock(p, m_gamedef); return std::make_unique<MapBlock>(p, m_gamedef);
} }
MapBlock *Map::createBlankBlock(v3s16 p) MapBlock *Map::createBlankBlock(v3s16 p)
{ {
MapBlock *block = createBlankBlockNoInsert(p); std::unique_ptr<MapBlock> block_u = createBlankBlockNoInsert(p);
MapBlock *block = block_u.get();
m_blocks[p] = block; m_blocks[p] = std::move(block_u);
return block; return block;
} }
@ -82,7 +81,7 @@ std::unique_ptr<MapBlock> Map::detachBlock(MapBlock *block)
// Remove from container // Remove from container
auto it = m_blocks.find(block->getPos()); auto it = m_blocks.find(block->getPos());
assert(it != m_blocks.end()); assert(it != m_blocks.end());
std::unique_ptr<MapBlock> ret(it->second); std::unique_ptr<MapBlock> ret = std::move(it->second);
assert(ret.get() == block); assert(ret.get() == block);
m_blocks.erase(it); m_blocks.erase(it);
@ -92,7 +91,7 @@ std::unique_ptr<MapBlock> Map::detachBlock(MapBlock *block)
return ret; return ret;
} }
void Map::insertBlock(MapBlock *block) void Map::insertBlock(std::unique_ptr<MapBlock> block)
{ {
v3s16 pos = block->getPos(); v3s16 pos = block->getPos();
@ -102,13 +101,13 @@ void Map::insertBlock(MapBlock *block)
} }
// Insert into container // Insert into container
m_blocks[pos] = block; m_blocks[pos] = std::move(block);
} }
MapBlock *Map::getBlockNoCreateNoEx(v3s16 p3d) MapBlock *Map::getBlockNoCreateNoEx(v3s16 p3d)
{ {
auto it = m_blocks.find(p3d); auto it = m_blocks.find(p3d);
return it != m_blocks.end() ? it->second : nullptr; return it != m_blocks.end() ? it->second.get() : nullptr;
} }
MapBlock *Map::getBlockNoCreate(v3s16 p3d) MapBlock *Map::getBlockNoCreate(v3s16 p3d)
@ -310,7 +309,7 @@ void Map::timerUpdate(float dtime, float unload_timeout, s32 max_loaded_blocks,
// If there is no practical limit, we spare creation of mapblock_queue // If there is no practical limit, we spare creation of mapblock_queue
if (max_loaded_blocks < 0) { if (max_loaded_blocks < 0) {
for (auto it = m_blocks.begin(); it != m_blocks.end();) { for (auto it = m_blocks.begin(); it != m_blocks.end();) {
MapBlock *block = it->second; MapBlock *block = it->second.get();
block->incrementUsageTimer(dtime); block->incrementUsageTimer(dtime);
if (block->refGet() == 0 if (block->refGet() == 0
@ -330,7 +329,6 @@ void Map::timerUpdate(float dtime, float unload_timeout, s32 max_loaded_blocks,
// Delete directly from container // Delete directly from container
it = m_blocks.erase(it); it = m_blocks.erase(it);
delete block;
if (unloaded_blocks) if (unloaded_blocks)
unloaded_blocks->push_back(p); unloaded_blocks->push_back(p);
@ -344,7 +342,7 @@ void Map::timerUpdate(float dtime, float unload_timeout, s32 max_loaded_blocks,
} else { } else {
std::priority_queue<TimeOrderedMapBlock> mapblock_queue; std::priority_queue<TimeOrderedMapBlock> mapblock_queue;
for (auto &entry : m_blocks) { for (auto &entry : m_blocks) {
MapBlock *block = entry.second; MapBlock *block = entry.second.get();
block->incrementUsageTimer(dtime); block->incrementUsageTimer(dtime);
mapblock_queue.push(TimeOrderedMapBlock(block)); mapblock_queue.push(TimeOrderedMapBlock(block));
} }

View file

@ -118,9 +118,9 @@ public:
// Returns NULL if not found // Returns NULL if not found
MapBlock * getBlockNoCreateNoEx(v3s16 p); MapBlock * getBlockNoCreateNoEx(v3s16 p);
MapBlock* createBlankBlockNoInsert(v3s16 p); std::unique_ptr<MapBlock> createBlankBlockNoInsert(v3s16 p);
MapBlock *createBlankBlock(v3s16 p); MapBlock *createBlankBlock(v3s16 p);
void insertBlock(MapBlock *block); void insertBlock(std::unique_ptr<MapBlock> block);
void deleteBlockImmediate(MapBlock *block); void deleteBlockImmediate(MapBlock *block);
// Remove a block from the map without deleting it // Remove a block from the map without deleting it
@ -274,7 +274,7 @@ protected:
std::set<MapEventReceiver*> m_event_receivers; std::set<MapEventReceiver*> m_event_receivers;
std::unordered_map<v3s16, MapBlock*> m_blocks; std::unordered_map<v3s16, std::unique_ptr<MapBlock>> m_blocks;
// This stores the properties of the nodes on the map. // This stores the properties of the nodes on the map.
const NodeDefManager *m_nodedef; const NodeDefManager *m_nodedef;

View file

@ -449,7 +449,7 @@ void ServerMap::save(ModifiedState save_level)
bool save_started = false; bool save_started = false;
for (auto &entry : m_blocks) { for (auto &entry : m_blocks) {
MapBlock *block = entry.second; MapBlock *block = entry.second.get();
block_count_all++; block_count_all++;
if(block->getModified() >= (u32)save_level) { if(block->getModified() >= (u32)save_level) {
@ -498,7 +498,7 @@ void ServerMap::listAllLoadableBlocks(std::vector<v3s16> &dst)
void ServerMap::listAllLoadedBlocks(std::vector<v3s16> &dst) void ServerMap::listAllLoadedBlocks(std::vector<v3s16> &dst)
{ {
for (auto &entry : m_blocks) { for (auto &entry : m_blocks) {
MapBlock *block = entry.second; MapBlock *block = entry.second.get();
v3s16 p = block->getPos(); v3s16 p = block->getPos();
dst.push_back(p); dst.push_back(p);
} }
@ -600,11 +600,11 @@ MapBlock *ServerMap::loadBlock(const std::string &blob, v3s16 p3d, bool save_aft
bool created_new = false; bool created_new = false;
try { try {
MapBlock* block_created_new = nullptr; std::unique_ptr<MapBlock> block_created_new;
block = getBlockNoCreateNoEx(p3d); block = getBlockNoCreateNoEx(p3d);
if (!block) { if (!block) {
block_created_new = createBlankBlockNoInsert(p3d); block_created_new = createBlankBlockNoInsert(p3d);
block = block_created_new; block = block_created_new.get();
} }
{ {
@ -614,7 +614,7 @@ MapBlock *ServerMap::loadBlock(const std::string &blob, v3s16 p3d, bool save_aft
// If it's a new block, insert it to the map // If it's a new block, insert it to the map
if (block_created_new) { if (block_created_new) {
insertBlock(block_created_new); insertBlock(std::move(block_created_new));
created_new = true; created_new = true;
} }
} catch (SerializationError &e) { } catch (SerializationError &e) {