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

View File

@ -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)
{
// 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());
if (!player)
return NULL;
return nullptr;
PlayerSAO *playersao = player->getPlayerSAO();
return playersao ? playersao->getInventory() : nullptr;
@ -67,13 +59,13 @@ Inventory *ServerInventoryManager::getInventory(const InventoryLocation &loc)
auto it = m_detached_inventories.find(loc.name);
if (it == m_detached_inventories.end())
return nullptr;
return it->second.inventory;
return it->second.inventory.get();
} break;
default:
sanity_check(false); // abort
break;
}
return NULL;
return nullptr;
}
void ServerInventoryManager::setInventoryModified(const InventoryLocation &loc)
@ -113,15 +105,16 @@ Inventory *ServerInventoryManager::createDetachedInventory(
if (m_detached_inventories.count(name) > 0) {
infostream << "Server clearing detached inventory \"" << name << "\""
<< std::endl;
delete m_detached_inventories[name].inventory;
m_detached_inventories[name].inventory.reset();
} else {
infostream << "Server creating detached inventory \"" << name << "\""
<< std::endl;
}
Inventory *inv = new Inventory(idef);
auto inv_u = std::make_unique<Inventory>(idef);
auto inv = inv_u.get();
sanity_check(inv);
m_detached_inventories[name].inventory = inv;
m_detached_inventories[name].inventory = std::move(inv_u);
if (!player.empty()) {
m_detached_inventories[name].owner = player;
@ -152,7 +145,7 @@ bool ServerInventoryManager::removeDetachedInventory(const std::string &name)
if (inv_it == m_detached_inventories.end())
return false;
delete inv_it->second.inventory;
inv_it->second.inventory.reset();
const std::string &owner = inv_it->second.owner;
if (!owner.empty()) {
@ -205,6 +198,6 @@ void ServerInventoryManager::sendDetachedInventories(const std::string &peer_nam
continue;
}
apply_cb(detached_inventory.first, detached_inventory.second.inventory);
apply_cb(detached_inventory.first, detached_inventory.second.inventory.get());
}
}

View File

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