Index: ps/trunk/source/graphics/Unit.h =================================================================== --- ps/trunk/source/graphics/Unit.h +++ ps/trunk/source/graphics/Unit.h @@ -44,8 +44,9 @@ public: // Attempt to create a unit with the given actor. // If specific selections are wanted, call SetEntitySelection or SetActorSelections after creation. - // Returns NULL on failure. - static CUnit* Create(const CStrW& actorName, const entity_id_t id, const uint32_t seed, CObjectManager& objectManager); + // Returns an empty `std::unique_ptr` on failure. + static std::unique_ptr Create(const CStrW& actorName, const entity_id_t id, + const uint32_t seed, CObjectManager& objectManager); ~CUnit(); Index: ps/trunk/source/graphics/Unit.cpp =================================================================== --- ps/trunk/source/graphics/Unit.cpp +++ ps/trunk/source/graphics/Unit.cpp @@ -47,18 +47,16 @@ delete m_Animation; } -CUnit* CUnit::Create(const CStrW& actorName, const entity_id_t id, const uint32_t seed, CObjectManager& objectManager) +std::unique_ptr CUnit::Create(const CStrW& actorName, const entity_id_t id, const uint32_t seed, CObjectManager& objectManager) { auto [success, actor] = objectManager.FindActorDef(actorName); - + UNUSED2(success); - CUnit* unit = new CUnit(objectManager, actor, id, seed); + std::unique_ptr unit{new CUnit(objectManager, actor, id, seed)}; if (!unit->m_Model) - { - delete unit; return nullptr; - } + return unit; } Index: ps/trunk/source/graphics/UnitManager.h =================================================================== --- ps/trunk/source/graphics/UnitManager.h +++ ps/trunk/source/graphics/UnitManager.h @@ -25,8 +25,9 @@ #include "ps/CStrForward.h" #include "simulation2/system/Entity.h" -#include +#include #include +#include class CUnit; class CObjectManager; @@ -41,20 +42,13 @@ ~CUnitManager(); // add given unit to world - void AddUnit(CUnit* unit); - // remove given unit from world, but don't delete it - void RemoveUnit(CUnit* unit); + CUnit* AddUnit(std::unique_ptr unit); // remove given unit from world and delete it void DeleteUnit(CUnit* unit); - // remove and delete all units - void DeleteAll(); // creates a new unit and adds it to the world CUnit* CreateUnit(const CStrW& actorName, const entity_id_t id, const uint32_t seed); - // return the units - const std::vector& GetUnits() const { return m_Units; } - void SetObjectManager(CObjectManager& objectManager) { m_ObjectManager = &objectManager; } /** @@ -65,7 +59,7 @@ private: // list of all known units - std::vector m_Units; + std::vector> m_Units; // graphical object manager; may be NULL if not set up CObjectManager* m_ObjectManager; }; Index: ps/trunk/source/graphics/UnitManager.cpp =================================================================== --- ps/trunk/source/graphics/UnitManager.cpp +++ ps/trunk/source/graphics/UnitManager.cpp @@ -36,66 +36,48 @@ /////////////////////////////////////////////////////////////////////////////// // CUnitManager destructor -CUnitManager::~CUnitManager() -{ - DeleteAll(); -} +CUnitManager::~CUnitManager() = default; /////////////////////////////////////////////////////////////////////////////// // AddUnit: add given unit to world -void CUnitManager::AddUnit(CUnit* unit) +CUnit* CUnitManager::AddUnit(std::unique_ptr unit) { - m_Units.push_back(unit); + return m_Units.emplace_back(std::move(unit)).get(); } -/////////////////////////////////////////////////////////////////////////////// -// RemoveUnit: remove given unit from world, but don't delete it -void CUnitManager::RemoveUnit(CUnit* unit) -{ - // find entry in list - typedef std::vector::iterator Iter; - Iter i=std::find(m_Units.begin(),m_Units.end(),unit); - if (i!=m_Units.end()) { - m_Units.erase(i); - } -} /////////////////////////////////////////////////////////////////////////////// // DeleteUnit: remove given unit from world and delete it void CUnitManager::DeleteUnit(CUnit* unit) { - RemoveUnit(unit); - delete unit; -} + const auto it = std::find_if(m_Units.begin(), m_Units.end(), [&](const std::unique_ptr& elem) + { + return elem.get() == unit; + }); -/////////////////////////////////////////////////////////////////////////////// -// DeleteAll: remove and delete all units -void CUnitManager::DeleteAll() -{ - for (size_t i=0;i unit{CUnit::Create(actorName, id, seed, *m_ObjectManager)}; + if (!unit) + return nullptr; - CUnit* unit = CUnit::Create(actorName, id, seed, *m_ObjectManager); - if (unit) - AddUnit(unit); - return unit; + return AddUnit(std::move(unit)); } void CUnitManager::MakeTerrainDirty(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1, int dirtyFlags) { if (!(dirtyFlags & RENDERDATA_UPDATE_VERTICES)) return; - for (CUnit* unit : m_Units) + for (const std::unique_ptr& unit : m_Units) unit->GetModel().SetTerrainDirty(i0, j0, i1, j1); }