From 1e3b7044502893b548b443820a3a9f2440cb9596 Mon Sep 17 00:00:00 2001 From: Thomas Fischer <thomas.fischer@ufz.de> Date: Thu, 28 Apr 2016 07:15:34 +0200 Subject: [PATCH] [DH] Using unique_ptr in Project. --- DataHolderLib/Project.cpp | 30 ++++++++++++------------------ DataHolderLib/Project.h | 22 +++++++++++++--------- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/DataHolderLib/Project.cpp b/DataHolderLib/Project.cpp index 51f7139c635..9e89b74941d 100644 --- a/DataHolderLib/Project.cpp +++ b/DataHolderLib/Project.cpp @@ -22,31 +22,25 @@ namespace DataHolderLib { -Project::~Project() -{ - for (MeshLib::Mesh* m : _mesh_vec) - delete m; -} - -void Project::addMesh(MeshLib::Mesh* mesh) +void Project::addMesh(std::unique_ptr<MeshLib::Mesh> mesh) { std::string name = mesh->getName(); getUniqueName(name); mesh->setName(name); - _mesh_vec.push_back(mesh); + _mesh_vec.push_back(std::move(mesh)); } -std::vector<MeshLib::Mesh*>::const_iterator Project::findMeshByName( - std::string const& name) const +std::vector<std::unique_ptr<MeshLib::Mesh>>::const_iterator +Project::findMeshByName(std::string const& name) const { return const_cast<Project&>(*this).findMeshByName(name); } -std::vector<MeshLib::Mesh*>::iterator Project::findMeshByName( - std::string const& name) +std::vector<std::unique_ptr<MeshLib::Mesh>>::iterator +Project::findMeshByName(std::string const& name) { return std::find_if(_mesh_vec.begin(), _mesh_vec.end(), - [&name](MeshLib::Mesh* mesh) + [&name](std::unique_ptr<MeshLib::Mesh> & mesh) { return mesh && (name == mesh->getName()); }); @@ -54,16 +48,16 @@ std::vector<MeshLib::Mesh*>::iterator Project::findMeshByName( const MeshLib::Mesh* Project::getMesh(const std::string &name) const { - std::vector<MeshLib::Mesh*>::const_iterator it = findMeshByName(name); - return (it == _mesh_vec.end() ? nullptr : *it); + auto it = findMeshByName(name); + return (it == _mesh_vec.end() ? nullptr : it->get()); } bool Project::removeMesh(const std::string &name) { - std::vector<MeshLib::Mesh*>::iterator it = findMeshByName(name); + auto it = findMeshByName(name); if (it != _mesh_vec.end()) { - delete *it; + delete it->release(); _mesh_vec.erase(it); return true; } @@ -92,7 +86,7 @@ bool Project::getUniqueName(std::string &name) const if (count > 1) cpName = cpName + "-" + std::to_string(count); - for (MeshLib::Mesh* mesh :_mesh_vec) + for (auto & mesh : _mesh_vec) if ( cpName == mesh->getName()) isUnique = false; } diff --git a/DataHolderLib/Project.h b/DataHolderLib/Project.h index de44c37c38d..670e66f7f58 100644 --- a/DataHolderLib/Project.h +++ b/DataHolderLib/Project.h @@ -14,9 +14,11 @@ #include "GeoLib/GEOObjects.h" -namespace MeshLib { - class Mesh; -} +#include "MeshLib/Mesh.h" + +//namespace MeshLib { +// class Mesh; +//} namespace DataHolderLib { @@ -34,21 +36,21 @@ public: Project(Project&) = delete; - ~Project(); + ~Project() = default; /// Returns the GEOObjects containing all points, polylines and surfaces. GeoLib::GEOObjects& getGEOObjects() { return _geoObjects; } /// Adds a new mesh under a (possibly new) unique name. /// \attention This might change the given mesh's name. - void addMesh(MeshLib::Mesh* mesh); + void addMesh(std::unique_ptr<MeshLib::Mesh> mesh); /// Returns the mesh with the given name or a \c nullptr if the mesh was not /// found. const MeshLib::Mesh* getMesh(const std::string &name) const; /// Returns all the meshes with their respective names - const std::vector<MeshLib::Mesh*>& getMeshObjects() const + const std::vector<std::unique_ptr<MeshLib::Mesh>>& getMeshObjects() const { return _mesh_vec; } @@ -69,11 +71,13 @@ private: /// Returns an iterator to the first found mesh with the given name. - std::vector<MeshLib::Mesh*>::const_iterator findMeshByName(std::string const& name) const; - std::vector<MeshLib::Mesh*>::iterator findMeshByName(std::string const& name); + std::vector<std::unique_ptr<MeshLib::Mesh>>::const_iterator findMeshByName( + std::string const& name) const; + std::vector<std::unique_ptr<MeshLib::Mesh>>::iterator findMeshByName( + std::string const& name); GeoLib::GEOObjects _geoObjects; - std::vector<MeshLib::Mesh*> _mesh_vec; + std::vector<std::unique_ptr<MeshLib::Mesh>> _mesh_vec; }; } // namespace -- GitLab