From e5e508c326462ee7896060f5ed025acd3f349848 Mon Sep 17 00:00:00 2001 From: Abdullah Tahiri Date: Sun, 11 Dec 2022 17:26:35 +0100 Subject: [PATCH] Sketcher: SketchObject new addGeometry method for smart pointers ================================================================ This new facility avoids to have to create a new copy() when a user copy is already created. As the user copy is reused via move semantics, memory management is simplified. CAVEAT: When this facility is used, the client code has to ensure whether a copy() or a clone() of the Part::Geometry should be undertaken. The different between both is that the former creates a new uuid (tag), whereas the latter does not. --- src/Mod/Sketcher/App/SketchObject.cpp | 10 +++++++++- src/Mod/Sketcher/App/SketchObject.h | 13 ++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Mod/Sketcher/App/SketchObject.cpp b/src/Mod/Sketcher/App/SketchObject.cpp index b28bf6cf7c..f6be46daf5 100644 --- a/src/Mod/Sketcher/App/SketchObject.cpp +++ b/src/Mod/Sketcher/App/SketchObject.cpp @@ -886,6 +886,14 @@ int SketchObject::addGeometry(const std::vector &geoList, bool } int SketchObject::addGeometry(const Part::Geometry *geo, bool construction/*=false*/) +{ + // this copy has a new random tag (see copy() vs clone()) + auto geoNew = std::unique_ptr(geo->copy()); + + return addGeometry(std::move(geoNew),construction); +} + +int SketchObject::addGeometry(std::unique_ptr newgeo, bool construction/*=false*/) { Base::StateLocker lock(managedoperation, true); // no need to check input data validity as this is an sketchobject managed operation. @@ -893,7 +901,7 @@ int SketchObject::addGeometry(const Part::Geometry *geo, bool construction/*=fal std::vector< Part::Geometry * > newVals(vals); - Part::Geometry *geoNew = geo->copy(); + auto *geoNew = newgeo.release(); if( geoNew->getTypeId() == Part::GeomPoint::getClassTypeId()) { // creation mode for points is always construction not to diff --git a/src/Mod/Sketcher/App/SketchObject.h b/src/Mod/Sketcher/App/SketchObject.h index b7eb2143ea..9447fec986 100644 --- a/src/Mod/Sketcher/App/SketchObject.h +++ b/src/Mod/Sketcher/App/SketchObject.h @@ -94,12 +94,23 @@ public: */ bool isSupportedGeometry(const Part::Geometry *geo) const; /*! - \brief Add geometry to a sketch + \brief Add geometry to a sketch - It adds a copy with a different uuid (internally uses copy() instead of clone()) \param geo - geometry to add \param construction - true for construction lines \retval int - GeoId of added element */ int addGeometry(const Part::Geometry *geo, bool construction=false); + + /*! + \brief Add geometry to a sketch using up the provided newgeo. Caveat: It will use the provided newgeo with the uuid it has. + This is different from the addGeometry method with a naked pointer, where a different uuid is ensured. The caller is responsible + for provided a new or existing uuid, as necessary. + \param geo - geometry to add + \param construction - true for construction lines + \retval int - GeoId of added element + */ + int addGeometry(std::unique_ptr newgeo, bool construction=false); + /*! \brief Add multiple geometry elements to a sketch \param geoList - geometry to add