]> granicus.if.org Git - graphviz/commitdiff
remove manual memory management of _points in Path class
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Tue, 20 Apr 2021 04:17:56 +0000 (21:17 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Mon, 26 Apr 2021 14:50:02 +0000 (07:50 -0700)
plugin/visio/VisioGraphic.cpp
plugin/visio/VisioGraphic.h

index 529bb181fc3bdc4fff03df1be4aab33d7d26cfba..be8f71b32b3e546294d8848d9ccfc97884dc197a 100644 (file)
@@ -120,35 +120,30 @@ namespace Visio
        Path::Path(pointf* points, int pointCount)
        {
                /* copy over the points */
-               _points = (pointf*)malloc(sizeof(_points[0]) * pointCount);
-               memcpy(_points, points, sizeof(_points[0]) * pointCount);
-               _pointCount = pointCount;
-       }
-       
-       Path::~Path()
-       {
-               /* since we copied, we need to free */
-               free(_points);
+               _points.reserve(pointCount);
+               for (int i = 0; i< pointCount; ++i) {
+                       _points.push_back(points[i]);
+               }
        }
        
        boxf Path::GetBounds() const
        {
                boxf bounds;
-               if (_points && _pointCount > 0)
+               if (!_points.empty())
                {
                        /* lower left is the minimal point, upper right is maximal point */
                        bounds.LL.x = bounds.UR.x = _points[0].x;
                        bounds.LL.y = bounds.UR.y = _points[0].y;
-                       for (int i = 1; i < _pointCount; ++i)
+                       for (const pointf &p : _points)
                        {
-                               if (bounds.LL.x > _points[i].x)
-                                       bounds.LL.x = _points[i].x;
-                               if (bounds.LL.y > _points[i].y)
-                                       bounds.LL.y = _points[i].y;
-                               if (bounds.UR.x < _points[i].x)
-                                       bounds.UR.x = _points[i].x;
-                               if (bounds.UR.y < _points[i].y)
-                                       bounds.UR.y = _points[i].y;
+                               if (bounds.LL.x > p.x)
+                                       bounds.LL.x = p.x;
+                               if (bounds.LL.y > p.y)
+                                       bounds.LL.y = p.y;
+                               if (bounds.UR.x < p.x)
+                                       bounds.UR.x = p.x;
+                               if (bounds.UR.y < p.y)
+                                       bounds.UR.y = p.y;
                        }
                }
                else
@@ -183,15 +178,15 @@ namespace Visio
        
        pointf Bezier::GetCenter() const
        {
-               if (_pointCount >= 4 && _pointCount % 2 == 0)
+               if (_points.size() >= 4 && _points.size() % 2 == 0)
                {
                        pointf center;
                        
                        /* the central control polygon for the bezier curve */
-                       pointf p0 = _points[_pointCount / 2 - 2];
-                       pointf p1 = _points[_pointCount / 2 - 1];
-                       pointf p2 = _points[_pointCount / 2];
-                       pointf p3 = _points[_pointCount / 2 + 1];
+                       pointf p0 = _points[_points.size() / 2 - 2];
+                       pointf p1 = _points[_points.size() / 2 - 1];
+                       pointf p2 = _points[_points.size() / 2];
+                       pointf p3 = _points[_points.size() / 2 + 1];
                        
                        /* use de Casteljou's algorithm to get a midpoint */
                        center.x = 0.125 * p0.x + 0.375 * p1.x + 0.375 * p2.x + 0.125 * p3.x;
@@ -200,7 +195,7 @@ namespace Visio
                }
                else
                /* just return the middle point */
-                       return _points[_pointCount / 2];
+                       return _points[_points.size() / 2];
        }
        
        void Bezier::Print(GVJ_t* job, pointf first, pointf last, bool allowCurves) const
@@ -208,7 +203,7 @@ namespace Visio
                gvputs(job, "<Geom>\n");
                if (!_filled)
                        gvputs(job, "<NoFill>1</NoFill>\n");            
-               if (_pointCount > 0)
+               if (!_points.empty())
                {
                        double xscale = 1.0 / (last.x - first.x);
                        double yscale = 1.0 / (last.y - first.y);
@@ -232,13 +227,13 @@ namespace Visio
                                gvputs(job, "<NURBSTo>");
                                
                                /* Ctl[P-1].X */
-                               gvprintf(job, "<X F='Width*%f'/>", (_points[_pointCount - 1].x - first.x) * xscale);
+                               gvprintf(job, "<X F='Width*%f'/>", (_points.back().x - first.x) * xscale);
                                
                                /* Ctl[P-1].Y */
-                               gvprintf(job, "<Y F='Height*%f'/>", (_points[_pointCount - 1].y - first.y) * yscale);
+                               gvprintf(job, "<Y F='Height*%f'/>", (_points.back().y - first.y) * yscale);
                                
                                /* Knot[P-1] */
-                               gvprintf(job, "<A>%d</A>", max(_pointCount - 4, 0));
+                               gvprintf(job, "<A>%zu</A>", _points.size() < 4 ? 0 : (_points.size() - 4));
                                
                                /* Ctl[P-1].Weight */
                                gvputs(job, "<B>1</B>");        
@@ -250,13 +245,13 @@ namespace Visio
                                gvputs(job, "<D>1</D>");        
                                
                                /* Knot[P], Degree, XType, YType */
-                               gvprintf(job, "<E F='NURBS(%d, 3, 0, 0", max(_pointCount - 3, 0));                              
-                               for (int i = 1; i < _pointCount; ++i)
+                               gvprintf(job, "<E F='NURBS(%zu, 3, 0, 0", _points.size() < 3 ? 0 : (_points.size() - 3));
+                               for (size_t i = 1; i < _points.size(); ++i)
                                /* Ctl[i].X, Ctl[i].Y, Knot[i], Ctl[i].Weight */
-                                       gvprintf(job, ", %f, %f, %d, 1",                                        
+                                       gvprintf(job, ", %f, %f, %zu, 1",                                       
                                                         (_points[i].x - first.x) * xscale,
                                                         (_points[i].y - first.y) * yscale,
-                                                        max(i - 3, 0));        
+                                                        i < 3 ? 0 : (i - 3));  
                                gvputs(job, ")'/>");
                                
                                gvputs(job, "</NURBSTo>\n");
@@ -265,7 +260,7 @@ namespace Visio
                        {
                                /* output lines only, so skip all the Bezier control points i.e. use every 3rd point */
                                
-                               if (_pointCount == 4)
+                               if (_points.size() == 4)
                                {
                                        /* single point, use VDX LineTo */
                                        gvputs(job, "<LineTo>");
@@ -277,10 +272,10 @@ namespace Visio
                                {
                                        /* multiple points, use VDX PolylineTo */
                                        gvputs(job, "<PolylineTo>");
-                                       gvprintf(job, "<X F='Width*%f' />", (_points[_pointCount - 1].x - first.x) * xscale);
-                                       gvprintf(job, "<Y F='Height*%f' />", (_points[_pointCount - 1].y - first.y) * yscale);
+                                       gvprintf(job, "<X F='Width*%f' />", (_points.back().x - first.x) * xscale);
+                                       gvprintf(job, "<Y F='Height*%f' />", (_points.back().y - first.y) * yscale);
                                        gvputs(job, "<A F='POLYLINE(0, 0");
-                                       for (int i = 3; i < _pointCount - 1; i += 3)
+                                       for (size_t i = 3; i + 1 < _points.size(); i += 3)
                                                gvprintf(job, ", %f, %f", (_points[i].x - first.x) * xscale, (_points[i].y - first.y) * yscale);
                                        gvputs(job, ")' />");
                                        gvputs(job, "</PolylineTo>\n");
@@ -306,7 +301,7 @@ namespace Visio
                gvputs(job, "<Geom>\n");
                if (!_filled)
                        gvputs(job, "<NoFill>1</NoFill>\n");            
-               if (_pointCount > 0)
+               if (!_points.empty())
                {
                        /* compute scale. if infinite, scale by 0 instead */
                        double xscale = 1.0 / (last.x - first.x);
@@ -321,7 +316,7 @@ namespace Visio
                        gvprintf(job, "<Y F='Height*%f' />", (_points[0].y - first.y) * yscale);
                        gvputs(job, "</MoveTo>\n");
                        
-                       if (_pointCount == 1)
+                       if (_points.size() == 1)
                        {
                                /* single point, use VDX LineTo */
                                gvputs(job, "<LineTo>");
@@ -336,7 +331,7 @@ namespace Visio
                                gvprintf(job, "<X F='Width*%f' />", (_points[0].x - first.x) * xscale);
                                gvprintf(job, "<Y F='Height*%f' />", (_points[0].y - first.y) * yscale);
                                gvputs(job, "<A F='POLYLINE(0, 0");
-                               for (int i = 1; i < _pointCount; ++i)
+                               for (size_t i = 1; i < _points.size(); ++i)
                                        gvprintf(job, ", %f, %f", (_points[i].x - first.x) * xscale, (_points[i].y - first.y) * yscale);
                                gvputs(job, ")' />");
                                gvputs(job, "</PolylineTo>\n");
@@ -358,7 +353,7 @@ namespace Visio
        void Polyline::Print(GVJ_t* job, pointf first, pointf last, bool) const
        {
                gvputs(job, "<Geom>\n");
-               if (_pointCount > 0)
+               if (!_points.empty())
                {
                        /* compute scale. if infinite, scale by 0 instead */
                        double xscale = 1.0 / (last.x - first.x);
@@ -373,7 +368,7 @@ namespace Visio
                        gvprintf(job, "<Y F='Height*%f' />", (_points[0].y - first.y) * yscale);
                        gvputs(job, "</MoveTo>\n");
                        
-                       if (_pointCount == 2)
+                       if (_points.size() == 2)
                        {
                                /* single point, use VDX LineTo */
                                gvputs(job, "<LineTo>");
@@ -385,10 +380,10 @@ namespace Visio
                        {
                                /* multiple points, use VDX PolylineTo */
                                gvputs(job, "<PolylineTo>");
-                               gvprintf(job, "<X F='Width*%f' />", (_points[_pointCount - 1].x - first.x) * xscale);
-                               gvprintf(job, "<Y F='Height*%f' />", (_points[_pointCount - 1].y - first.y) * yscale);
+                               gvprintf(job, "<X F='Width*%f' />", (_points.back().x - first.x) * xscale);
+                               gvprintf(job, "<Y F='Height*%f' />", (_points.back().y - first.y) * yscale);
                                gvputs(job, "<A F='POLYLINE(0, 0");
-                               for (int i = 1; i < _pointCount - 1; ++i)
+                               for (size_t i = 1; i + 1 < _points.size(); ++i)
                                        gvprintf(job, ", %f, %f", (_points[i].x - first.x) * xscale, (_points[i].y - first.y) * yscale);
                                gvputs(job, ")' />");
                                gvputs(job, "</PolylineTo>\n");
index 6c8183cdd56628e6f36ac46e9cf7487ee7a11631..02d9c5d0c4d58db65f47d9343dd10828993b5b5d 100644 (file)
@@ -12,6 +12,7 @@
 
 #include <common/types.h>
 #include <memory>
+#include <vector>
 
 namespace Visio
 {
@@ -93,13 +94,12 @@ namespace Visio
        {
        public:
                Path(pointf* points, int pointCount);
-               ~Path();
+               virtual ~Path() = default;
                
                virtual boxf GetBounds() const;
                
        protected:
-               pointf* _points;
-               int _pointCount;
+               std::vector<pointf> _points;
        };
                
        class Bezier: public Path, public Connection