[geos-commits] [SCM] GEOS branch master updated. a4aaa4e18dcb13b0804143d047e8b2d9b7c4aeeb

git at osgeo.org git at osgeo.org
Sun Sep 15 13:18:36 PDT 2019


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GEOS".

The branch, master has been updated
       via  a4aaa4e18dcb13b0804143d047e8b2d9b7c4aeeb (commit)
      from  ea5689eb7b308c0b743d86e6c98882bb6d8f1450 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit a4aaa4e18dcb13b0804143d047e8b2d9b7c4aeeb
Author: Daniel Baston <dbaston at gmail.com>
Date:   Fri Sep 13 23:04:27 2019 -0400

    Simplify Geometry methods with new GeometryFactory methods
    
    Avoid heap-allocating vectors and remove memory leaks on exception.

diff --git a/capi/geos_ts_c.cpp b/capi/geos_ts_c.cpp
index 9c8853f..9d3bd7c 100644
--- a/capi/geos_ts_c.cpp
+++ b/capi/geos_ts_c.cpp
@@ -2589,7 +2589,7 @@ extern "C" {
                 handle->ERROR_MESSAGE("Argument is not a LineString");
                 return NULL;
             }
-            return ls->getPointN(n);
+            return ls->getPointN(n).release();
         }
         catch(const std::exception& e) {
             handle->ERROR_MESSAGE("%s", e.what());
@@ -2624,7 +2624,7 @@ extern "C" {
                 handle->ERROR_MESSAGE("Argument is not a LineString");
                 return NULL;
             }
-            return ls->getStartPoint();
+            return ls->getStartPoint().release();
         }
         catch(const std::exception& e) {
             handle->ERROR_MESSAGE("%s", e.what());
@@ -2659,7 +2659,7 @@ extern "C" {
                 handle->ERROR_MESSAGE("Argument is not a LineString");
                 return NULL;
             }
-            return ls->getEndPoint();
+            return ls->getEndPoint().release();
         }
         catch(const std::exception& e) {
             handle->ERROR_MESSAGE("%s", e.what());
diff --git a/include/geos/geom/LineString.h b/include/geos/geom/LineString.h
index a262d8d..0316d62 100644
--- a/include/geos/geom/LineString.h
+++ b/include/geos/geom/LineString.h
@@ -116,19 +116,19 @@ public:
 
     std::size_t getNumPoints() const override;
 
-    virtual Point* getPointN(std::size_t n) const;
+    virtual std::unique_ptr<Point> getPointN(std::size_t n) const;
 
     /// \brief
     /// Return the start point of the LineString
     /// or NULL if this is an EMPTY LineString.
     ///
-    virtual Point* getStartPoint() const;
+    virtual std::unique_ptr<Point> getStartPoint() const;
 
     /// \brief
     /// Return the end point of the LineString
     /// or NULL if this is an EMPTY LineString.
     ///
-    virtual Point* getEndPoint() const;
+    virtual std::unique_ptr<Point> getEndPoint() const;
 
     virtual bool isClosed() const;
 
diff --git a/src/geom/LineString.cpp b/src/geom/LineString.cpp
index 3ee0bbb..cf52831 100644
--- a/src/geom/LineString.cpp
+++ b/src/geom/LineString.cpp
@@ -163,15 +163,15 @@ LineString::getNumPoints() const
     return points->getSize();
 }
 
-Point*
+std::unique_ptr<Point>
 LineString::getPointN(size_t n) const
 {
     assert(getFactory());
     assert(points.get());
-    return getFactory()->createPoint(points->getAt(n));
+    return std::unique_ptr<Point>(getFactory()->createPoint(points->getAt(n)));
 }
 
-Point*
+std::unique_ptr<Point>
 LineString::getStartPoint() const
 {
     if(isEmpty()) {
@@ -181,7 +181,7 @@ LineString::getStartPoint() const
     return getPointN(0);
 }
 
-Point*
+std::unique_ptr<Point>
 LineString::getEndPoint() const
 {
     if(isEmpty()) {
@@ -224,11 +224,10 @@ LineString::getBoundary() const
     if(isClosed()) {
         return std::unique_ptr<Geometry>(getFactory()->createMultiPoint());
     }
-    vector<Geometry*>* pts = new vector<Geometry*>();
-    pts->push_back(getStartPoint());
-    pts->push_back(getEndPoint());
-    MultiPoint* mp = getFactory()->createMultiPoint(pts);
-    return std::unique_ptr<Geometry>(mp);
+    std::vector<std::unique_ptr<Point>> pts(2);
+    pts[0] = getStartPoint();
+    pts[1] = getEndPoint();
+    return getFactory()->createMultiPoint(std::move(pts));
 }
 
 bool
diff --git a/src/geom/LinearRing.cpp b/src/geom/LinearRing.cpp
index 7775720..de3fd9e 100644
--- a/src/geom/LinearRing.cpp
+++ b/src/geom/LinearRing.cpp
@@ -28,8 +28,6 @@
 #include <string>
 #include <memory>
 
-using namespace std;
-
 namespace geos {
 namespace geom { // geos::geom
 
@@ -99,7 +97,7 @@ LinearRing::isClosed() const
     return LineString::isClosed();
 }
 
-string
+std::string
 LinearRing::getGeometryType() const
 {
     return "LinearRing";
diff --git a/src/geom/MultiLineString.cpp b/src/geom/MultiLineString.cpp
index a52a9cc..42bf4dc 100644
--- a/src/geom/MultiLineString.cpp
+++ b/src/geom/MultiLineString.cpp
@@ -129,13 +129,13 @@ MultiLineString::reverse() const
     }
 
     size_t nLines = geometries.size();
-    Geometry::NonConstVect* revLines = new Geometry::NonConstVect(nLines);
+    std::vector<std::unique_ptr<Geometry>> revLines(nLines);
+
     for(size_t i = 0; i < nLines; ++i) {
-        LineString* iLS = dynamic_cast<LineString*>(geometries[i].get());
-        assert(iLS);
-        (*revLines)[nLines - 1 - i] = iLS->reverse().release();
+        const LineString* iLS = static_cast<LineString*>(geometries[i].get());
+        revLines[nLines - 1 - i] = iLS->reverse();
     }
-    return std::unique_ptr<Geometry>(getFactory()->createMultiLineString(revLines));
+    return getFactory()->createMultiLineString(std::move(revLines));
 }
 
 } // namespace geos::geom
diff --git a/src/geom/MultiPolygon.cpp b/src/geom/MultiPolygon.cpp
index efc78aa..0c16059 100644
--- a/src/geom/MultiPolygon.cpp
+++ b/src/geom/MultiPolygon.cpp
@@ -78,27 +78,22 @@ MultiPolygon::getBoundary() const
     if(isEmpty()) {
         return std::unique_ptr<Geometry>(getFactory()->createMultiLineString());
     }
-    vector<Geometry*>* allRings = new vector<Geometry*>();
-    for(size_t i = 0; i < geometries.size(); i++) {
-        Polygon* pg = dynamic_cast<Polygon*>(geometries[i].get());
-        assert(pg);
+
+    vector<std::unique_ptr<Geometry>> allRings;
+    for(const auto& pg : geometries) {
         auto g = pg->getBoundary();
-        if(LineString* ls = dynamic_cast<LineString*>(g.get())) {
-            allRings->push_back(ls);
-            g.release();
-        }
-        else {
-            GeometryCollection* rings = dynamic_cast<GeometryCollection*>(g.get());
-            for(size_t j = 0, jn = rings->getNumGeometries();
-                    j < jn; ++j) {
-                allRings->push_back(rings->getGeometryN(j)->clone().release());
+
+        if(g->getNumGeometries() == 1) {
+            allRings.push_back(std::move(g));
+        } else {
+            for(size_t i = 0; i < g->getNumGeometries(); ++i) {
+                // TODO avoid this clone
+                allRings.push_back(g->getGeometryN(i)->clone());
             }
         }
     }
 
-    Geometry* ret = getFactory()->createMultiLineString(allRings);
-
-    return std::unique_ptr<Geometry>(ret);
+    return getFactory()->createMultiLineString(std::move(allRings));
 }
 
 bool
@@ -122,16 +117,16 @@ MultiPolygon::reverse() const
         return clone();
     }
 
-    auto* reversed = new std::vector<Geometry*> {geometries.size()};
+    std::vector<std::unique_ptr<Geometry>> reversed(geometries.size());
 
     std::transform(geometries.begin(),
                    geometries.end(),
-                   reversed->begin(),
+                   reversed.begin(),
     [](const std::unique_ptr<Geometry> & g) {
-        return g->reverse().release();
+        return g->reverse();
     });
 
-    return std::unique_ptr<Geometry>(getFactory()->createMultiPolygon(reversed));
+    return getFactory()->createMultiPolygon(std::move(reversed));
 }
 
 } // namespace geos::geom
diff --git a/src/geom/Polygon.cpp b/src/geom/Polygon.cpp
index 37624ac..0e95a8f 100644
--- a/src/geom/Polygon.cpp
+++ b/src/geom/Polygon.cpp
@@ -230,23 +230,22 @@ Polygon::getBoundary() const
         return std::unique_ptr<Geometry>(gf->createLineString(*shell));
     }
 
-    std::vector<Geometry*>* rings = new std::vector<Geometry*>(holes.size() + 1);
+    std::vector<std::unique_ptr<Geometry>> rings(holes.size() + 1);
 
-    (*rings)[0] = gf->createLineString(*shell).release();
+    rings[0] = gf->createLineString(*shell);
     for(size_t i = 0, n = holes.size(); i < n; ++i) {
         const LinearRing* hole = holes[i].get();
-        assert(hole);
-        LineString* ls = gf->createLineString(*hole).release();
-        (*rings)[i + 1] = ls;
+        std::unique_ptr<LineString> ls = gf->createLineString(*hole);
+        rings[i + 1] = std::move(ls);
     }
-    MultiLineString* ret = getFactory()->createMultiLineString(rings);
-    return std::unique_ptr<Geometry>(ret);
+
+    return getFactory()->createMultiLineString(std::move(rings));
 }
 
 Envelope::Ptr
 Polygon::computeEnvelopeInternal() const
 {
-    return Envelope::Ptr(new Envelope(*(shell->getEnvelopeInternal())));
+    return detail::make_unique<Envelope>(*(shell->getEnvelopeInternal()));
 }
 
 bool
@@ -511,17 +510,17 @@ Polygon::reverse() const
         return clone();
     }
 
-    auto* exteriorRingReversed = dynamic_cast<LinearRing*>(shell->reverse().release());
-    auto* interiorRingsReversed = new std::vector<LinearRing*> {holes.size()};
+    std::unique_ptr<LinearRing> exteriorRingReversed(static_cast<LinearRing*>(shell->reverse().release()));
+    std::vector<std::unique_ptr<LinearRing>> interiorRingsReversed(holes.size());
 
     std::transform(holes.begin(),
                    holes.end(),
-                   interiorRingsReversed->begin(),
+                   interiorRingsReversed.begin(),
     [](const std::unique_ptr<LinearRing> & g) {
-        return dynamic_cast<LinearRing*>(g->reverse().release());
+        return std::unique_ptr<LinearRing>(static_cast<LinearRing*>(g->reverse().release()));
     });
 
-    return std::unique_ptr<Geometry>(getFactory()->createPolygon(exteriorRingReversed, interiorRingsReversed));
+    return getFactory()->createPolygon(std::move(exteriorRingReversed), std::move(interiorRingsReversed));
 }
 
 } // namespace geos::geom
diff --git a/src/operation/valid/MakeValid.cpp b/src/operation/valid/MakeValid.cpp
index a85eb41..8a36315 100644
--- a/src/operation/valid/MakeValid.cpp
+++ b/src/operation/valid/MakeValid.cpp
@@ -72,13 +72,13 @@ nodeLineWithFirstCoordinate(const geom::Geometry* geom)
   if( geomType == GEOS_LINESTRING ) {
       auto line = dynamic_cast<const geom::LineString*>(geom);
       assert(line);
-      point.reset(line->getPointN(0));
+      point = line->getPointN(0);
   } else {
       auto mls = dynamic_cast<const geom::MultiLineString*>(geom);
       assert(mls);
       auto line = dynamic_cast<const geom::LineString*>(mls->getGeometryN(0));
       assert(line);
-      point.reset(line->getPointN(0));
+      point = line->getPointN(0);
   }
 
   return geom->Union(point.get());

-----------------------------------------------------------------------

Summary of changes:
 capi/geos_ts_c.cpp                |  6 +++---
 include/geos/geom/LineString.h    |  6 +++---
 src/geom/LineString.cpp           | 17 ++++++++---------
 src/geom/LinearRing.cpp           |  4 +---
 src/geom/MultiLineString.cpp      | 10 +++++-----
 src/geom/MultiPolygon.cpp         | 35 +++++++++++++++--------------------
 src/geom/Polygon.cpp              | 25 ++++++++++++-------------
 src/operation/valid/MakeValid.cpp |  4 ++--
 8 files changed, 49 insertions(+), 58 deletions(-)


hooks/post-receive
-- 
GEOS


More information about the geos-commits mailing list