From: spo Date: Tue, 30 Jun 2015 06:02:40 +0000 (+0300) Subject: Replace void* with shared_ptr in GeomAPI_Interface X-Git-Tag: V_1.3.0~155 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=3afc257344780d0447d4fca3f26c91d932d0a0b0;p=modules%2Fshaper.git Replace void* with shared_ptr in GeomAPI_Interface --- diff --git a/src/GeomAPI/GeomAPI_AISObject.cpp b/src/GeomAPI/GeomAPI_AISObject.cpp index 681926a68..236574665 100644 --- a/src/GeomAPI/GeomAPI_AISObject.cpp +++ b/src/GeomAPI/GeomAPI_AISObject.cpp @@ -43,10 +43,10 @@ GeomAPI_AISObject::GeomAPI_AISObject() GeomAPI_AISObject::~GeomAPI_AISObject() { - if (myImpl) { + if (!empty()) { // This is necessary for correct deletion of Handle entity. // Without this Handle does not decremented counter to 0 - Handle(AIS_InteractiveObject) *anAIS = (Handle(AIS_InteractiveObject)*)myImpl; + Handle(AIS_InteractiveObject) *anAIS = implPtr(); anAIS->Nullify(); } } diff --git a/src/GeomAPI/GeomAPI_Ax1.cpp b/src/GeomAPI/GeomAPI_Ax1.cpp index dcf5a987a..65bc65bb4 100644 --- a/src/GeomAPI/GeomAPI_Ax1.cpp +++ b/src/GeomAPI/GeomAPI_Ax1.cpp @@ -8,7 +8,7 @@ #include -#define MY_AX1 static_cast(myImpl) +#define MY_AX1 implPtr() //================================================================================================= GeomAPI_Ax1::GeomAPI_Ax1() @@ -63,4 +63,4 @@ std::shared_ptr GeomAPI_Ax1::reversed() std::shared_ptr aPnt(new GeomAPI_Pnt(anAxis.Location().X(), anAxis.Location().Y(), anAxis.Location().Z())); std::shared_ptr aDir(new GeomAPI_Dir(anAxis.Direction().X(), anAxis.Direction().Y(), anAxis.Direction().Z())); return std::shared_ptr(new GeomAPI_Ax1(aPnt, aDir)); -} \ No newline at end of file +} diff --git a/src/GeomAPI/GeomAPI_Ax3.cpp b/src/GeomAPI/GeomAPI_Ax3.cpp index 892869a4c..0ff482b85 100644 --- a/src/GeomAPI/GeomAPI_Ax3.cpp +++ b/src/GeomAPI/GeomAPI_Ax3.cpp @@ -15,7 +15,7 @@ #include -#define MY_AX3 static_cast(myImpl) +#define MY_AX3 implPtr() GeomAPI_Ax3::GeomAPI_Ax3() @@ -102,4 +102,4 @@ std::shared_ptr GeomAPI_Ax3::to2D(double theX, double theY, doubl double aX = aVec.X() * aXDir.X() + aVec.Y() * aXDir.Y() + aVec.Z() * aXDir.Z(); double aY = aVec.X() * aYDir.X() + aVec.Y() * aYDir.Y() + aVec.Z() * aYDir.Y(); return std::shared_ptr(new GeomAPI_Pnt2d(aX, aY)); -} \ No newline at end of file +} diff --git a/src/GeomAPI/GeomAPI_Circ.cpp b/src/GeomAPI/GeomAPI_Circ.cpp index 807390364..34332272c 100644 --- a/src/GeomAPI/GeomAPI_Circ.cpp +++ b/src/GeomAPI/GeomAPI_Circ.cpp @@ -16,7 +16,7 @@ #include #include -#define MY_CIRC static_cast(myImpl) +#define MY_CIRC implPtr() static gp_Circ* newCirc(const gp_Pnt& theCenter, const gp_Dir& theDir, const double theRadius) { diff --git a/src/GeomAPI/GeomAPI_Circ2d.cpp b/src/GeomAPI/GeomAPI_Circ2d.cpp index 9871e60fb..0e0a21a31 100644 --- a/src/GeomAPI/GeomAPI_Circ2d.cpp +++ b/src/GeomAPI/GeomAPI_Circ2d.cpp @@ -18,7 +18,7 @@ #include -#define MY_CIRC2D static_cast(myImpl) +#define MY_CIRC2D implPtr() static gp_Circ2d* newCirc2d(const double theCenterX, const double theCenterY, const gp_Dir2d theDir, const double theRadius) diff --git a/src/GeomAPI/GeomAPI_Curve.cpp b/src/GeomAPI/GeomAPI_Curve.cpp index 46a1ea19f..f96fd7b78 100644 --- a/src/GeomAPI/GeomAPI_Curve.cpp +++ b/src/GeomAPI/GeomAPI_Curve.cpp @@ -16,7 +16,7 @@ #include #include -#define MY_CURVE (*(static_cast(myImpl))) +#define MY_CURVE (*(implPtr())) GeomAPI_Curve::GeomAPI_Curve() : GeomAPI_Interface(new Handle_Geom_Curve()), myStart(0), myEnd(1) diff --git a/src/GeomAPI/GeomAPI_DataMapOfShapeShape.cpp b/src/GeomAPI/GeomAPI_DataMapOfShapeShape.cpp index 97e3f1068..2d0ce7924 100644 --- a/src/GeomAPI/GeomAPI_DataMapOfShapeShape.cpp +++ b/src/GeomAPI/GeomAPI_DataMapOfShapeShape.cpp @@ -57,8 +57,8 @@ bool GeomAPI_DataMapOfShapeShape::unBind(std::shared_ptr theKey) GeomAPI_DataMapOfShapeShape::~GeomAPI_DataMapOfShapeShape() { - if (myImpl) { - implPtr()->Clear(); + if (!empty()) { + implPtr()->Clear(); //delete myImpl; } - } \ No newline at end of file + } diff --git a/src/GeomAPI/GeomAPI_Dir.cpp b/src/GeomAPI/GeomAPI_Dir.cpp index 5c42d68a0..3174cea0e 100644 --- a/src/GeomAPI/GeomAPI_Dir.cpp +++ b/src/GeomAPI/GeomAPI_Dir.cpp @@ -9,7 +9,7 @@ #include -#define MY_DIR static_cast(myImpl) +#define MY_DIR implPtr() GeomAPI_Dir::GeomAPI_Dir(const double theX, const double theY, const double theZ) : GeomAPI_Interface(new gp_Dir(theX, theY, theZ)) diff --git a/src/GeomAPI/GeomAPI_Dir2d.cpp b/src/GeomAPI/GeomAPI_Dir2d.cpp index abb954f29..4cb8149a3 100644 --- a/src/GeomAPI/GeomAPI_Dir2d.cpp +++ b/src/GeomAPI/GeomAPI_Dir2d.cpp @@ -9,7 +9,7 @@ #include -#define MY_DIR static_cast(myImpl) +#define MY_DIR implPtr() GeomAPI_Dir2d::GeomAPI_Dir2d(const double theX, const double theY) : GeomAPI_Interface(new gp_Dir2d(theX, theY)) diff --git a/src/GeomAPI/GeomAPI_Interface.cpp b/src/GeomAPI/GeomAPI_Interface.cpp index 4db15c996..05f182755 100644 --- a/src/GeomAPI/GeomAPI_Interface.cpp +++ b/src/GeomAPI/GeomAPI_Interface.cpp @@ -8,25 +8,15 @@ GeomAPI_Interface::GeomAPI_Interface() { - myImpl = 0; -} -GeomAPI_Interface::GeomAPI_Interface(void* theImpl) -{ - myImpl = theImpl; } GeomAPI_Interface::~GeomAPI_Interface() { - if (myImpl) { - delete myImpl; - myImpl = 0; - } + } -void GeomAPI_Interface::setImpl(void* theImpl) +bool GeomAPI_Interface::empty() const { - if (myImpl) - delete myImpl; - myImpl = theImpl; + return myImpl.get() == 0; } diff --git a/src/GeomAPI/GeomAPI_Interface.h b/src/GeomAPI/GeomAPI_Interface.h index 49db964fc..e5fac636a 100644 --- a/src/GeomAPI/GeomAPI_Interface.h +++ b/src/GeomAPI/GeomAPI_Interface.h @@ -9,6 +9,8 @@ #include +#include + /**\class GeomAPI_Interface * \ingroup DataModel * \brief General base class for all interfaces in this package @@ -16,15 +18,18 @@ class GEOMAPI_EXPORT GeomAPI_Interface { - protected: - void* myImpl; ///< pointer to the internal impl object + private: + std::shared_ptr myImpl; ///< pointer to the internal impl object public: /// None - constructor GeomAPI_Interface(); /// Constructor by the impl pointer (used for internal needs) - GeomAPI_Interface(void* theImpl); + template explicit GeomAPI_Interface(T* theImpl) + { + myImpl.reset(theImpl); + } /// Destructor virtual ~GeomAPI_Interface(); @@ -32,15 +37,26 @@ class GEOMAPI_EXPORT GeomAPI_Interface /// Returns the pointer to the impl template inline T* implPtr() { - return static_cast(myImpl); + return static_cast(myImpl.get()); + } + /// Returns the pointer to the impl + template inline const T* implPtr() const + { + return static_cast(myImpl.get()); } /// Returns the reference object of the impl template inline const T& impl() const { - return *(static_cast(myImpl)); + return *(static_cast(myImpl.get())); } /// Updates the impl (deletes the old one) - void setImpl(void* theImpl); + template inline void setImpl(T* theImpl) + { + myImpl.reset(theImpl); + } + + // Returns true if the impl is empty + bool empty() const; }; #endif diff --git a/src/GeomAPI/GeomAPI_Lin.cpp b/src/GeomAPI/GeomAPI_Lin.cpp index 8b64190ba..79db6e186 100644 --- a/src/GeomAPI/GeomAPI_Lin.cpp +++ b/src/GeomAPI/GeomAPI_Lin.cpp @@ -20,7 +20,7 @@ #include #include -#define MY_LIN static_cast(myImpl) +#define MY_LIN implPtr() static gp_Lin* newLine(const double theStartX, const double theStartY, const double theStartZ, const double theEndX, const double theEndY, const double theEndZ) diff --git a/src/GeomAPI/GeomAPI_Lin2d.cpp b/src/GeomAPI/GeomAPI_Lin2d.cpp index 3c784eefd..6b53b8fdc 100644 --- a/src/GeomAPI/GeomAPI_Lin2d.cpp +++ b/src/GeomAPI/GeomAPI_Lin2d.cpp @@ -15,7 +15,7 @@ #include -#define MY_LIN2D static_cast(myImpl) +#define MY_LIN2D implPtr() static gp_Lin2d* newLine2d(const double theStartX, const double theStartY, const double theEndX, const double theEndY) diff --git a/src/GeomAPI/GeomAPI_Pnt.cpp b/src/GeomAPI/GeomAPI_Pnt.cpp index 29fe933a6..0cf67c2ad 100644 --- a/src/GeomAPI/GeomAPI_Pnt.cpp +++ b/src/GeomAPI/GeomAPI_Pnt.cpp @@ -14,7 +14,7 @@ #include #include -#define MY_PNT static_cast(myImpl) +#define MY_PNT implPtr() GeomAPI_Pnt::GeomAPI_Pnt(const double theX, const double theY, const double theZ) : GeomAPI_Interface(new gp_Pnt(theX, theY, theZ)) diff --git a/src/GeomAPI/GeomAPI_Pnt2d.cpp b/src/GeomAPI/GeomAPI_Pnt2d.cpp index 2bd8e3937..a2bab72a7 100644 --- a/src/GeomAPI/GeomAPI_Pnt2d.cpp +++ b/src/GeomAPI/GeomAPI_Pnt2d.cpp @@ -14,7 +14,7 @@ #include -#define MY_PNT2D static_cast(myImpl) +#define MY_PNT2D implPtr() GeomAPI_Pnt2d::GeomAPI_Pnt2d(const double theX, const double theY) : GeomAPI_Interface(new gp_Pnt2d(theX, theY)) diff --git a/src/GeomAPI/GeomAPI_Shape.cpp b/src/GeomAPI/GeomAPI_Shape.cpp index f8139103c..41f127860 100644 --- a/src/GeomAPI/GeomAPI_Shape.cpp +++ b/src/GeomAPI/GeomAPI_Shape.cpp @@ -13,7 +13,7 @@ #include -#define MY_SHAPE static_cast(myImpl) +#define MY_SHAPE implPtr() GeomAPI_Shape::GeomAPI_Shape() : GeomAPI_Interface(new TopoDS_Shape()) diff --git a/src/GeomAPI/GeomAPI_ShapeExplorer.cpp b/src/GeomAPI/GeomAPI_ShapeExplorer.cpp index 3732d5afe..6b94439c0 100644 --- a/src/GeomAPI/GeomAPI_ShapeExplorer.cpp +++ b/src/GeomAPI/GeomAPI_ShapeExplorer.cpp @@ -9,7 +9,7 @@ #include #include -#define MY_EXPLORER static_cast(myImpl) +#define MY_EXPLORER implPtr() //================================================================================================= GeomAPI_ShapeExplorer::GeomAPI_ShapeExplorer() diff --git a/src/GeomAPI/GeomAPI_XY.cpp b/src/GeomAPI/GeomAPI_XY.cpp index 5cfeff6ba..52561d580 100644 --- a/src/GeomAPI/GeomAPI_XY.cpp +++ b/src/GeomAPI/GeomAPI_XY.cpp @@ -8,7 +8,7 @@ #include -#define MY_XY static_cast(myImpl) +#define MY_XY implPtr() GeomAPI_XY::GeomAPI_XY(const double theX, const double theY) : GeomAPI_Interface(new gp_XY(theX, theY)) diff --git a/src/GeomAPI/GeomAPI_XYZ.cpp b/src/GeomAPI/GeomAPI_XYZ.cpp index 1b8e0e457..22588c1ed 100644 --- a/src/GeomAPI/GeomAPI_XYZ.cpp +++ b/src/GeomAPI/GeomAPI_XYZ.cpp @@ -8,7 +8,7 @@ #include -#define MY_XYZ static_cast(myImpl) +#define MY_XYZ implPtr() GeomAPI_XYZ::GeomAPI_XYZ(const double theX, const double theY, const double theZ) : GeomAPI_Interface(new gp_XYZ(theX, theY, theZ)) diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_Extrusion.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_Extrusion.cpp index 29c4aec27..eecff212a 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_Extrusion.cpp +++ b/src/GeomAlgoAPI/GeomAlgoAPI_Extrusion.cpp @@ -132,7 +132,7 @@ GeomAlgoAPI_MakeShape * GeomAlgoAPI_Extrusion::makeShape() const //============================================================================ GeomAlgoAPI_Extrusion::~GeomAlgoAPI_Extrusion() { - if (myImpl) { - myMap.clear(); + if (!empty()) { + myMap.clear(); } -} \ No newline at end of file +} diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_Placement.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_Placement.cpp index 58a4684dd..651e03601 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_Placement.cpp +++ b/src/GeomAlgoAPI/GeomAlgoAPI_Placement.cpp @@ -255,6 +255,6 @@ GeomAlgoAPI_MakeShape * GeomAlgoAPI_Placement::makeShape() const //============================================================================ GeomAlgoAPI_Placement::~GeomAlgoAPI_Placement() { - if (myImpl) + if (!empty()) myMap.clear(); }