Browse Source

Implement type checking for XS objects

Type handling is mainly done using templates.
Template Slic3r::ClassTraits is used to store info about exported types (perl class name). Currently only perl class name and refference name is used.
Template values are initialized by REGISTER_CLASS macro. This macro is used in .cpp file of class ( it needs to be used exactly for each type).

Ref<type> class is used to return value as perl reference. Operator overloading is used to make c++ and XSpp happy, only pointer value should be possible to return.

Clone<type> class is used to return copy of value ( using new and copy constructor). Copy is created on assigment, this should be probably improved (memory leak on multiple assignments).
It is overloaded to be able to return type, type* and type&.

Typechecking in ExtrusionEntityCollection updated to check all passed types.
Petr Ledvina 11 years ago
parent
commit
115aa6885f

+ 1 - 0
xs/MANIFEST

@@ -1675,6 +1675,7 @@ src/Model.hpp
 src/MultiPoint.cpp
 src/MultiPoint.hpp
 src/myinit.h
+src/perlglue.hpp
 src/Point.cpp
 src/Point.hpp
 src/Polygon.cpp

+ 9 - 0
xs/src/BoundingBox.cpp

@@ -1,5 +1,8 @@
 #include "BoundingBox.hpp"
 #include <algorithm>
+#ifdef SLIC3RXS
+#include "perlglue.hpp"
+#endif
 
 namespace Slic3r {
 
@@ -156,4 +159,10 @@ BoundingBox3Base<PointClass>::center() const
 }
 template Pointf3 BoundingBox3Base<Pointf3>::center() const;
 
+#ifdef SLIC3RXS
+REGISTER_CLASS(BoundingBox, "Geometry::BoundingBox");
+REGISTER_CLASS(BoundingBoxf, "Geometry::BoundingBoxf");
+REGISTER_CLASS(BoundingBoxf3, "Geometry::BoundingBoxf3");
+#endif
+
 }

+ 10 - 4
xs/src/ExPolygon.cpp

@@ -4,6 +4,9 @@
 #include "Line.hpp"
 #include "ClipperUtils.hpp"
 #include "polypartition.h"
+#ifdef SLIC3RXS
+#include "perlglue.hpp"
+#endif
 
 #include <list>
 
@@ -240,6 +243,9 @@ ExPolygon::triangulate2(Polygons* polygons) const
 }
 
 #ifdef SLIC3RXS
+
+REGISTER_CLASS(ExPolygon, "ExPolygon");
+
 SV*
 ExPolygon::to_AV() {
     const unsigned int num_holes = this->holes.size();
@@ -257,14 +263,14 @@ ExPolygon::to_AV() {
 SV*
 ExPolygon::to_SV_ref() {
     SV* sv = newSV(0);
-    sv_setref_pv( sv, "Slic3r::ExPolygon::Ref", this );
+    sv_setref_pv( sv, perl_class_name_ref(this), this );
     return sv;
 }
 
 SV*
 ExPolygon::to_SV_clone_ref() const {
     SV* sv = newSV(0);
-    sv_setref_pv( sv, "Slic3r::ExPolygon", new ExPolygon(*this) );
+    sv_setref_pv( sv, perl_class_name(this), new ExPolygon(*this) );
     return sv;
 }
 
@@ -300,8 +306,8 @@ void
 ExPolygon::from_SV_check(SV* expoly_sv)
 {
     if (sv_isobject(expoly_sv) && (SvTYPE(SvRV(expoly_sv)) == SVt_PVMG)) {
-        if (!sv_isa(expoly_sv, "Slic3r::ExPolygon") && !sv_isa(expoly_sv, "Slic3r::ExPolygon::Ref"))
-            CONFESS("Not a valid Slic3r::ExPolygon object");
+        if (!sv_isa(expoly_sv, perl_class_name(this)) && !sv_isa(expoly_sv, perl_class_name_ref(this)))
+          CONFESS("Not a valid %s object", perl_class_name(this));
         // a XS ExPolygon was supplied
         *this = *(ExPolygon *)SvIV((SV*)SvRV( expoly_sv ));
     } else {

+ 7 - 0
xs/src/ExPolygonCollection.cpp

@@ -1,5 +1,8 @@
 #include "ExPolygonCollection.hpp"
 #include "Geometry.hpp"
+#ifdef SLIC3RXS
+#include "perlglue.hpp"
+#endif
 
 namespace Slic3r {
 
@@ -67,4 +70,8 @@ ExPolygonCollection::convex_hull(Polygon* hull) const
     Slic3r::Geometry::convex_hull(pp, hull);
 }
 
+#ifdef SLIC3RXS
+REGISTER_CLASS(ExPolygonCollection, "ExPolygon::Collection");
+#endif
+
 }

+ 11 - 1
xs/src/ExtrusionEntity.cpp

@@ -1,8 +1,11 @@
-#include <sstream>
 #include "ExtrusionEntity.hpp"
 #include "ExtrusionEntityCollection.hpp"
 #include "ExPolygonCollection.hpp"
 #include "ClipperUtils.hpp"
+#include <sstream>
+#ifdef SLIC3RXS
+#include "perlglue.hpp"
+#endif
 
 namespace Slic3r {
 
@@ -102,6 +105,9 @@ ExtrusionPath::_inflate_collection(const Polylines &polylines, ExtrusionEntityCo
 }
 
 #ifdef SLIC3RXS
+
+REGISTER_CLASS(ExtrusionPath, "ExtrusionPath");
+
 std::string
 ExtrusionPath::gcode(SV* extruder, double e, double F,
     double xofs, double yofs, std::string extrusion_axis,
@@ -212,4 +218,8 @@ ExtrusionLoop::last_point() const
     return this->polygon.points.front();  // in polygons, first == last
 }
 
+#ifdef SLIC3RXS
+REGISTER_CLASS(ExtrusionLoop, "ExtrusionLoop");
+#endif
+
 }

+ 8 - 0
xs/src/ExtrusionEntityCollection.cpp

@@ -1,6 +1,9 @@
 #include "ExtrusionEntityCollection.hpp"
 #include <algorithm>
 #include <map>
+#ifdef SLIC3RXS
+#include "perlglue.hpp"
+#endif
 
 namespace Slic3r {
 
@@ -107,4 +110,9 @@ ExtrusionEntityCollection::chained_path_from(Point start_near, ExtrusionEntityCo
     }
 }
 
+#ifdef SLIC3RXS
+// there is no ExtrusionLoop::Collection or ExtrusionEntity::Collection
+REGISTER_CLASS(ExtrusionEntityCollection, "ExtrusionPath::Collection");
+#endif
+
 }

+ 7 - 0
xs/src/Flow.cpp

@@ -1,5 +1,8 @@
 #include "Flow.hpp"
 #include <cmath>
+#ifdef SLIC3RXS
+#include "perlglue.hpp"
+#endif
 
 namespace Slic3r {
 
@@ -112,4 +115,8 @@ Flow::_spacing(float width, float nozzle_diameter, float height, float bridge_fl
     return width - OVERLAP_FACTOR * (width - min_flow_spacing);
 }
 
+#ifdef SLIC3RXS
+REGISTER_CLASS(Flow, "Flow");
+#endif
+
 }

+ 12 - 6
xs/src/Line.cpp

@@ -3,6 +3,9 @@
 #include <algorithm>
 #include <cmath>
 #include <sstream>
+#ifdef SLIC3RXS
+#include "perlglue.hpp"
+#endif
 
 namespace Slic3r {
 
@@ -115,6 +118,9 @@ Line::vector() const
 }
 
 #ifdef SLIC3RXS
+
+REGISTER_CLASS(Line, "Line");
+
 void
 Line::from_SV(SV* line_sv)
 {
@@ -127,8 +133,8 @@ void
 Line::from_SV_check(SV* line_sv)
 {
     if (sv_isobject(line_sv) && (SvTYPE(SvRV(line_sv)) == SVt_PVMG)) {
-        if (!sv_isa(line_sv, "Slic3r::Line") && !sv_isa(line_sv, "Slic3r::Line::Ref"))
-            CONFESS("Not a valid Slic3r::Line object");
+        if (!sv_isa(line_sv, perl_class_name(this)) && !sv_isa(line_sv, perl_class_name_ref(this)))
+            CONFESS("Not a valid %s object", perl_class_name(this));
         *this = *(Line*)SvIV((SV*)SvRV( line_sv ));
     } else {
         this->from_SV(line_sv);
@@ -141,11 +147,11 @@ Line::to_AV() {
     av_extend(av, 1);
     
     SV* sv = newSV(0);
-    sv_setref_pv( sv, "Slic3r::Point::Ref", &(this->a) );
+    sv_setref_pv( sv, perl_class_name_ref(&this->a), &(this->a) );
     av_store(av, 0, sv);
     
     sv = newSV(0);
-    sv_setref_pv( sv, "Slic3r::Point::Ref", &(this->b) );
+    sv_setref_pv( sv, perl_class_name_ref(&this->b), &(this->b) );
     av_store(av, 1, sv);
     
     return newRV_noinc((SV*)av);
@@ -154,14 +160,14 @@ Line::to_AV() {
 SV*
 Line::to_SV_ref() {
     SV* sv = newSV(0);
-    sv_setref_pv( sv, "Slic3r::Line::Ref", this );
+    sv_setref_pv( sv, perl_class_name_ref(this), this );
     return sv;
 }
 
 SV*
 Line::to_SV_clone_ref() const {
     SV* sv = newSV(0);
-    sv_setref_pv( sv, "Slic3r::Line", new Line(*this) );
+    sv_setref_pv( sv, perl_class_name(this), new Line(*this) );
     return sv;
 }
 

+ 36 - 20
xs/src/Point.cpp

@@ -1,7 +1,10 @@
-#include <cmath>
-#include <sstream>
 #include "Point.hpp"
 #include "Line.hpp"
+#include <cmath>
+#include <sstream>
+#ifdef SLIC3RXS
+#include "perlglue.hpp"
+#endif
 
 namespace Slic3r {
 
@@ -139,17 +142,20 @@ Point::ccw(const Line &line) const
 }
 
 #ifdef SLIC3RXS
+
+REGISTER_CLASS(Point, "Point");
+
 SV*
 Point::to_SV_ref() {
     SV* sv = newSV(0);
-    sv_setref_pv( sv, "Slic3r::Point::Ref", (void*)this );
+    sv_setref_pv( sv, perl_class_name_ref(this), (void*)this );
     return sv;
 }
 
 SV*
 Point::to_SV_clone_ref() const {
     SV* sv = newSV(0);
-    sv_setref_pv( sv, "Slic3r::Point", new Point(*this) );
+    sv_setref_pv( sv, perl_class_name(this), new Point(*this) );
     return sv;
 }
 
@@ -176,14 +182,34 @@ void
 Point::from_SV_check(SV* point_sv)
 {
     if (sv_isobject(point_sv) && (SvTYPE(SvRV(point_sv)) == SVt_PVMG)) {
-        if (!sv_isa(point_sv, "Slic3r::Point") && !sv_isa(point_sv, "Slic3r::Point::Ref"))
-            CONFESS("Not a valid Slic3r::Point object");
+        if (!sv_isa(point_sv, perl_class_name(this)) && !sv_isa(point_sv, perl_class_name_ref(this)))
+            CONFESS("Not a valid %s object (got %s)", perl_class_name(this), HvNAME(SvSTASH(SvRV(point_sv))));
         *this = *(Point*)SvIV((SV*)SvRV( point_sv ));
     } else {
         this->from_SV(point_sv);
     }
 }
 
+#endif
+
+void
+Pointf::scale(double factor)
+{
+    this->x *= factor;
+    this->y *= factor;
+}
+
+void
+Pointf::translate(double x, double y)
+{
+    this->x += x;
+    this->y += y;
+}
+
+#ifdef SLIC3RXS
+
+REGISTER_CLASS(Pointf, "Pointf");
+
 SV*
 Pointf::to_SV_pureperl() const {
     AV* av = newAV();
@@ -207,20 +233,6 @@ Pointf::from_SV(SV* point_sv)
 }
 #endif
 
-void
-Pointf::scale(double factor)
-{
-    this->x *= factor;
-    this->y *= factor;
-}
-
-void
-Pointf::translate(double x, double y)
-{
-    this->x += x;
-    this->y += y;
-}
-
 void
 Pointf3::scale(double factor)
 {
@@ -235,4 +247,8 @@ Pointf3::translate(double x, double y, double z)
     this->z += z;
 }
 
+#ifdef SLIC3RXS
+REGISTER_CLASS(Pointf3, "Pointf3");
+#endif
+
 }

+ 10 - 4
xs/src/Polygon.cpp

@@ -2,6 +2,9 @@
 #include "ClipperUtils.hpp"
 #include "Polygon.hpp"
 #include "Polyline.hpp"
+#ifdef SLIC3RXS
+#include "perlglue.hpp"
+#endif
 
 namespace Slic3r {
 
@@ -175,25 +178,28 @@ Polygon::triangulate_convex(Polygons* polygons) const
 }
 
 #ifdef SLIC3RXS
+
+REGISTER_CLASS(Polygon, "Polygon");
+
 SV*
 Polygon::to_SV_ref() {
     SV* sv = newSV(0);
-    sv_setref_pv( sv, "Slic3r::Polygon::Ref", (void*)this );
+    sv_setref_pv( sv, perl_class_name_ref(this), (void*)this );
     return sv;
 }
 
 SV*
 Polygon::to_SV_clone_ref() const {
     SV* sv = newSV(0);
-    sv_setref_pv( sv, "Slic3r::Polygon", new Polygon(*this) );
+    sv_setref_pv( sv, perl_class_name(this), new Polygon(*this) );
     return sv;
 }
 
 void
 Polygon::from_SV_check(SV* poly_sv)
 {
-    if (sv_isobject(poly_sv) && !sv_isa(poly_sv, "Slic3r::Polygon") && !sv_isa(poly_sv, "Slic3r::Polygon::Ref"))
-        CONFESS("Not a valid Slic3r::Polygon object");
+    if (sv_isobject(poly_sv) && !sv_isa(poly_sv, perl_class_name(this)) && !sv_isa(poly_sv, perl_class_name_ref(this)))
+        CONFESS("Not a valid %s object", perl_class_name(this));
     
     MultiPoint::from_SV_check(poly_sv);
 }

Some files were not shown because too many files changed in this diff