Skip to content

Commit

Permalink
Use unique_ptr for ServerInventoryManager::DetachedInventory::inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
Desour committed Jun 5, 2023
1 parent 1780d1b commit 1b51ff3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
25 changes: 9 additions & 16 deletions src/server/serverinventorymgr.cpp
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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());
}
}
5 changes: 3 additions & 2 deletions src/server/serverinventorymgr.h
Expand Up @@ -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;
Expand All @@ -31,7 +32,7 @@ class ServerInventoryManager : public InventoryManager
{
public:
ServerInventoryManager();
virtual ~ServerInventoryManager();
virtual ~ServerInventoryManager() = default;

void setEnv(ServerEnvironment *env)
{
Expand All @@ -54,7 +55,7 @@ class ServerInventoryManager : public InventoryManager
private:
struct DetachedInventory
{
Inventory *inventory;
std::unique_ptr<Inventory> inventory;
std::string owner;
};

Expand Down

0 comments on commit 1b51ff3

Please sign in to comment.