From 057b7e5a6505508aa20cf5a89483312e87d10b7f Mon Sep 17 00:00:00 2001
From: Thomas Fischer <thomas.fischer@ufz.de>
Date: Wed, 27 May 2020 18:41:57 +0200
Subject: [PATCH] [BL || A/IO] Subst. geometryCleanup and clearData by
 cleanupVectorElements.

---
 .../FileIO/GocadIO/GocadAsciiReader.cpp       | 16 +----
 Applications/FileIO/SWMM/SWMMInterface.cpp    | 62 +++++++++----------
 BaseLib/Algorithm.h                           | 16 +++++
 3 files changed, 48 insertions(+), 46 deletions(-)

diff --git a/Applications/FileIO/GocadIO/GocadAsciiReader.cpp b/Applications/FileIO/GocadIO/GocadAsciiReader.cpp
index 0a0fad11df1..26147fa5b8a 100644
--- a/Applications/FileIO/GocadIO/GocadAsciiReader.cpp
+++ b/Applications/FileIO/GocadIO/GocadAsciiReader.cpp
@@ -64,20 +64,6 @@ bool isCommentLine(std::string const& str)
     return (str.substr(0, 1) == "#");
 }
 
-/// Clears data vectors if an error occured
-void clearData(std::vector<MeshLib::Node*> const& nodes,
-               std::vector<MeshLib::Element*> const& elems)
-{
-    for (MeshLib::Element* e : elems)
-    {
-        delete e;
-    }
-    for (MeshLib::Node* n : nodes)
-    {
-        delete n;
-    }
-}
-
 /// Parses current section until END-tag is reached
 bool skipToEND(std::ifstream& in)
 {
@@ -537,7 +523,7 @@ MeshLib::Mesh* createMesh(std::ifstream& in, DataType type,
         return new MeshLib::Mesh(mesh_name, nodes, elems, mesh_prop);
     }
     ERR("Error parsing {:s} {:s}.", dataType2ShortString(type), mesh_name);
-    clearData(nodes, elems);
+    BaseLib::cleanupVectorElements(nodes, elems);
     return nullptr;
 }
 
diff --git a/Applications/FileIO/SWMM/SWMMInterface.cpp b/Applications/FileIO/SWMM/SWMMInterface.cpp
index 072afa33128..a4810875431 100644
--- a/Applications/FileIO/SWMM/SWMMInterface.cpp
+++ b/Applications/FileIO/SWMM/SWMMInterface.cpp
@@ -350,16 +350,6 @@ bool SwmmInterface::readLinksAsPolylines(std::ifstream &in,
     return true;
 }
 
-/// Deletes the geometric objects and returns false
-bool geometryCleanup(std::vector<GeoLib::Point*> &points, std::vector<GeoLib::Polyline*> &lines)
-{
-    for (auto line : lines)
-        delete line;
-    for (auto point : points)
-        delete point;
-    return false;
-}
-
 bool SwmmInterface::convertSwmmInputToGeometry(std::string const& inp_file_name,
     GeoLib::GEOObjects &geo_objects, bool add_subcatchments)
 {
@@ -380,27 +370,24 @@ bool SwmmInterface::convertSwmmInputToGeometry(std::string const& inp_file_name,
 
     std::string geo_name = BaseLib::extractBaseNameWithoutExtension(inp_file_name);
     std::string line;
-    while ( std::getline(in, line) )
+    while (std::getline(in, line))
     {
-        if (line == "[COORDINATES]")
-        {
-            if (!readCoordinates<GeoLib::Point>(in, *points, pnt_names))
-                return geometryCleanup(*points, *lines);
-        }
-        if (line == "[VERTICES]")
+        if (line == "[COORDINATES]" || line == "[VERTICES]" ||
+            line == "[SYMBOLS]")
         {
             if (!readCoordinates<GeoLib::Point>(in, *points, pnt_names))
-                return geometryCleanup(*points, *lines);
+            {
+                BaseLib::cleanupVectorElements(*points, *lines);
+                return false;
+            }
         }
         if (line == "[Polygons]" && add_subcatchments)
         {
             if (!readPolygons(in, *lines, line_names, *points, pnt_names))
-                return geometryCleanup(*points, *lines);
-        }
-        if (line == "[SYMBOLS]")
-        {
-            if (!readCoordinates<GeoLib::Point>(in, *points, pnt_names))
-                return geometryCleanup(*points, *lines);
+            {
+                BaseLib::cleanupVectorElements(*points, *lines);
+                return false;
+            }
         }
     }
 
@@ -411,8 +398,9 @@ bool SwmmInterface::convertSwmmInputToGeometry(std::string const& inp_file_name,
     }
     if (points->size() != pnt_names.size())
     {
-        ERR ("Length of point vector and point name vector do not match.");
-        return geometryCleanup(*points, *lines);
+        ERR("Length of point vector and point name vector do not match.");
+        BaseLib::cleanupVectorElements(*points, *lines);
+        return false;
     }
 
     auto name_id_map = std::make_unique<std::map<std::string, std::size_t>>();
@@ -429,31 +417,43 @@ bool SwmmInterface::convertSwmmInputToGeometry(std::string const& inp_file_name,
     in.clear();
     in.seekg(0, in.beg);
 
-    while ( std::getline(in, line) )
+    while (std::getline(in, line))
     {
         if (line == "[JUNCTIONS]")
         {
             INFO ("Reading point elevation...");
             if (!addPointElevation(in, *points, *name_id_map))
-                return geometryCleanup(*points, *lines);
+            {
+                BaseLib::cleanupVectorElements(*points, *lines);
+                return false;
+            }
         }
         if (line == "[CONDUITS]")
         {
             INFO ("Reading conduits...");
             if (!readLinksAsPolylines(in, *lines, line_names, *points, *name_id_map))
-                return geometryCleanup(*points, *lines);
+            {
+                BaseLib::cleanupVectorElements(*points, *lines);
+                return false;
+            }
         }
         else if (line == "[PUMPS]")
         {
             INFO ("Reading pumps...");
             if (!readLinksAsPolylines(in, *lines, line_names, *points, *name_id_map))
-                return geometryCleanup(*points, *lines);
+            {
+                BaseLib::cleanupVectorElements(*points, *lines);
+                return false;
+            }
         }
         else if (line == "[WEIRS]")
         {
             INFO ("Reading weirs...");
             if (!readLinksAsPolylines(in, *lines, line_names, *points, *name_id_map))
-                return geometryCleanup(*points, *lines);
+            {
+                BaseLib::cleanupVectorElements(*points, *lines);
+                return false;
+            }
         }
     }
 
diff --git a/BaseLib/Algorithm.h b/BaseLib/Algorithm.h
index 62a47c1dc84..a01a42ad5e1 100644
--- a/BaseLib/Algorithm.h
+++ b/BaseLib/Algorithm.h
@@ -284,4 +284,20 @@ std::size_t findIndex(Container const& container,
     }
     return std::distance(container.begin(), it);
 }
+
+/** Util function to cleanup vectors */
+template <typename T1, typename T2>
+void cleanupVectorElements(std::vector<T1*> const& items,
+                           std::vector<T2*> const& dependent_items)
+{
+    for (auto dependent_item : dependent_items)
+    {
+        delete dependent_item;
+    }
+    for (auto item : items)
+    {
+        delete item;
+    }
+}
+
 }  // namespace BaseLib
-- 
GitLab