Use unique_ptr for ServerInventoryManager::DetachedInventory::inventory

This commit is contained in:
Desour 2023-03-03 16:44:04 +01:00 committed by DS
parent 1780d1bbde
commit 1b51ff333a
2 changed files with 12 additions and 18 deletions

@ -29,14 +29,6 @@ ServerInventoryManager::ServerInventoryManager() : InventoryManager()
{ {
} }
ServerInventoryManager::~ServerInventoryManager()
{
// Delete detached inventories
for (auto &detached_inventory : m_detached_inventories) {
delete detached_inventory.second.inventory;
}
}
Inventory *ServerInventoryManager::getInventory(const InventoryLocation &loc) Inventory *ServerInventoryManager::getInventory(const InventoryLocation &loc)
{ {
// No m_env check here: allow creation and modification of detached inventories // No m_env check here: allow creation and modification of detached inventories
@ -51,7 +43,7 @@ Inventory *ServerInventoryManager::getInventory(const InventoryLocation &loc)
RemotePlayer *player = m_env->getPlayer(loc.name.c_str()); RemotePlayer *player = m_env->getPlayer(loc.name.c_str());
if (!player) if (!player)
return NULL; return nullptr;
PlayerSAO *playersao = player->getPlayerSAO(); PlayerSAO *playersao = player->getPlayerSAO();
return playersao ? playersao->getInventory() : nullptr; return playersao ? playersao->getInventory() : nullptr;
@ -67,13 +59,13 @@ Inventory *ServerInventoryManager::getInventory(const InventoryLocation &loc)
auto it = m_detached_inventories.find(loc.name); auto it = m_detached_inventories.find(loc.name);
if (it == m_detached_inventories.end()) if (it == m_detached_inventories.end())
return nullptr; return nullptr;
return it->second.inventory; return it->second.inventory.get();
} break; } break;
default: default:
sanity_check(false); // abort sanity_check(false); // abort
break; break;
} }
return NULL; return nullptr;
} }
void ServerInventoryManager::setInventoryModified(const InventoryLocation &loc) void ServerInventoryManager::setInventoryModified(const InventoryLocation &loc)
@ -113,15 +105,16 @@ Inventory *ServerInventoryManager::createDetachedInventory(
if (m_detached_inventories.count(name) > 0) { if (m_detached_inventories.count(name) > 0) {
infostream << "Server clearing detached inventory \"" << name << "\"" infostream << "Server clearing detached inventory \"" << name << "\""
<< std::endl; << std::endl;
delete m_detached_inventories[name].inventory; m_detached_inventories[name].inventory.reset();
} else { } else {
infostream << "Server creating detached inventory \"" << name << "\"" infostream << "Server creating detached inventory \"" << name << "\""
<< std::endl; << std::endl;
} }
Inventory *inv = new Inventory(idef); auto inv_u = std::make_unique<Inventory>(idef);
auto inv = inv_u.get();
sanity_check(inv); sanity_check(inv);
m_detached_inventories[name].inventory = inv; m_detached_inventories[name].inventory = std::move(inv_u);
if (!player.empty()) { if (!player.empty()) {
m_detached_inventories[name].owner = player; m_detached_inventories[name].owner = player;
@ -152,7 +145,7 @@ bool ServerInventoryManager::removeDetachedInventory(const std::string &name)
if (inv_it == m_detached_inventories.end()) if (inv_it == m_detached_inventories.end())
return false; return false;
delete inv_it->second.inventory; inv_it->second.inventory.reset();
const std::string &owner = inv_it->second.owner; const std::string &owner = inv_it->second.owner;
if (!owner.empty()) { if (!owner.empty()) {
@ -205,6 +198,6 @@ void ServerInventoryManager::sendDetachedInventories(const std::string &peer_nam
continue; continue;
} }
apply_cb(detached_inventory.first, detached_inventory.second.inventory); apply_cb(detached_inventory.first, detached_inventory.second.inventory.get());
} }
} }

@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "inventorymanager.h" #include "inventorymanager.h"
#include <cassert> #include <cassert>
#include <functional> #include <functional>
#include <memory>
#include <unordered_map> #include <unordered_map>
class IItemDefManager; class IItemDefManager;
@ -31,7 +32,7 @@ class ServerInventoryManager : public InventoryManager
{ {
public: public:
ServerInventoryManager(); ServerInventoryManager();
virtual ~ServerInventoryManager(); virtual ~ServerInventoryManager() = default;
void setEnv(ServerEnvironment *env) void setEnv(ServerEnvironment *env)
{ {
@ -54,7 +55,7 @@ public:
private: private:
struct DetachedInventory struct DetachedInventory
{ {
Inventory *inventory; std::unique_ptr<Inventory> inventory;
std::string owner; std::string owner;
}; };