Return objects by reference instead of always cloning

This commit is contained in:
Alessandro Ranellucci 2013-09-02 20:22:20 +02:00
parent 1741301973
commit c0789506e4
30 changed files with 158 additions and 54 deletions

View file

@ -339,9 +339,9 @@ polynode2perl(const ClipperLib::PolyNode& node)
Slic3r::Polygon p;
ClipperPolygon_to_Slic3rPolygon(node.Contour, p);
if (node.IsHole()) {
(void)hv_stores( hv, "hole", p.to_SV() );
(void)hv_stores( hv, "hole", p.to_SV_clone_ref() );
} else {
(void)hv_stores( hv, "outer", p.to_SV() );
(void)hv_stores( hv, "outer", p.to_SV_clone_ref() );
}
(void)hv_stores( hv, "children", polynode_children_2_perl(node) );
return (SV*)newRV_noinc((SV*)hv);

View file

@ -57,19 +57,23 @@ ExPolygon::to_SV() {
av_extend(av, num_holes); // -1 +1
SV* sv = newSV(0);
sv_setref_pv( sv, "Slic3r::Polygon", new Polygon(this->contour) );
av_store(av, 0, sv);
av_store(av, 0, this->contour.to_SV_ref());
for (unsigned int i = 0; i < num_holes; i++) {
sv = newSV(0);
sv_setref_pv( sv, "Slic3r::Polygon", new Polygon(this->holes[i]) );
av_store(av, i+1, sv);
av_store(av, i+1, this->holes[i].to_SV_ref());
}
return newRV_noinc((SV*)av);
}
SV*
ExPolygon::to_SV_ref() {
SV* sv = newSV(0);
sv_setref_pv( sv, "Slic3r::ExPolygon::Ref", this );
return sv;
}
SV*
ExPolygon::to_SV_clone_ref() {
SV* sv = newSV(0);
sv_setref_pv( sv, "Slic3r::ExPolygon", new ExPolygon(*this) );
return sv;

View file

@ -15,6 +15,7 @@ class ExPolygon
void from_SV_check(SV* poly_sv);
SV* to_SV();
SV* to_SV_ref();
SV* to_SV_clone_ref();
SV* to_SV_pureperl();
void scale(double factor);
void translate(double x, double y);

View file

@ -66,11 +66,11 @@ Line::to_SV() {
av_extend(av, 1);
SV* sv = newSV(0);
sv_setref_pv( sv, "Slic3r::Point", new Point(this->a) );
sv_setref_pv( sv, "Slic3r::Point::Ref", &(this->a) );
av_store(av, 0, sv);
sv = newSV(0);
sv_setref_pv( sv, "Slic3r::Point", new Point(this->b) );
sv_setref_pv( sv, "Slic3r::Point::Ref", &(this->b) );
av_store(av, 1, sv);
return newRV_noinc((SV*)av);
@ -78,6 +78,13 @@ Line::to_SV() {
SV*
Line::to_SV_ref() {
SV* sv = newSV(0);
sv_setref_pv( sv, "Slic3r::Line::Ref", this );
return sv;
}
SV*
Line::to_SV_clone_ref() {
SV* sv = newSV(0);
sv_setref_pv( sv, "Slic3r::Line", new Line(*this) );
return sv;

View file

@ -17,6 +17,7 @@ class Line
void from_SV_check(SV* line_sv);
SV* to_SV();
SV* to_SV_ref();
SV* to_SV_clone_ref();
SV* to_SV_pureperl();
void scale(double factor);
void translate(double x, double y);

View file

@ -32,16 +32,16 @@ MultiPoint::reverse()
std::reverse(this->points.begin(), this->points.end());
}
const Point*
MultiPoint::first_point() const
Point*
MultiPoint::first_point()
{
return &(this->points.front());
}
const Point*
MultiPoint::last_point() const
MultiPoint::first_point() const
{
return &(this->points.back());
return &(this->points.front());
}
void
@ -73,9 +73,7 @@ MultiPoint::to_SV() {
AV* av = newAV();
av_extend(av, num_points-1);
for (unsigned int i = 0; i < num_points; i++) {
SV* sv = newSV(0);
sv_setref_pv( sv, "Slic3r::Point", new Point(this->points[i]) );
av_store(av, i, sv);
av_store(av, i, this->points[i].to_SV_ref());
}
return newRV_noinc((SV*)av);
}

View file

@ -19,8 +19,9 @@ class MultiPoint
void translate(double x, double y);
void rotate(double angle, Point* center);
void reverse();
Point* first_point();
const Point* first_point() const;
const Point* last_point() const;
virtual Point* last_point() = 0;
};
}

View file

@ -73,7 +73,21 @@ Point::distance_to(const Point* point) const
}
SV*
Point::to_SV_pureperl() {
Point::to_SV_ref() const {
SV* sv = newSV(0);
sv_setref_pv( sv, "Slic3r::Point::Ref", (void*)this );
return sv;
}
SV*
Point::to_SV_clone_ref() const {
SV* sv = newSV(0);
sv_setref_pv( sv, "Slic3r::Point", new Point(*this) );
return sv;
}
SV*
Point::to_SV_pureperl() const {
AV* av = newAV();
av_fill(av, 1);
av_store(av, 0, newSViv(this->x));

View file

@ -18,7 +18,9 @@ class Point
explicit Point(long _x = 0, long _y = 0): x(_x), y(_y) {};
void from_SV(SV* point_sv);
void from_SV_check(SV* point_sv);
SV* to_SV_pureperl();
SV* to_SV_ref() const;
SV* to_SV_clone_ref() const;
SV* to_SV_pureperl() const;
void scale(double factor);
void translate(double x, double y);
void rotate(double angle, Point* center);

View file

@ -5,8 +5,21 @@
namespace Slic3r {
Point*
Polygon::last_point()
{
return &(this->points.front()); // last point == first point for polygons
}
SV*
Polygon::to_SV_ref() {
Polygon::to_SV_ref() const {
SV* sv = newSV(0);
sv_setref_pv( sv, "Slic3r::Polygon::Ref", (void*)this );
return sv;
}
SV*
Polygon::to_SV_clone_ref() const {
SV* sv = newSV(0);
sv_setref_pv( sv, "Slic3r::Polygon", new Polygon(*this) );
return sv;

View file

@ -11,7 +11,9 @@ namespace Slic3r {
class Polygon : public MultiPoint {
public:
SV* to_SV_ref();
Point* last_point();
SV* to_SV_ref() const;
SV* to_SV_clone_ref() const;
Lines lines();
Polyline* split_at(const Point* point);
Polyline* split_at_index(int index);

View file

@ -2,6 +2,18 @@
namespace Slic3r {
Point*
Polyline::last_point()
{
return &(this->points.back());
}
const Point*
Polyline::last_point() const
{
return &(this->points.back());
}
Lines
Polyline::lines()
{
@ -14,6 +26,14 @@ Polyline::lines()
SV*
Polyline::to_SV_ref() const
{
SV* sv = newSV(0);
sv_setref_pv( sv, "Slic3r::Polyline::Ref", (void*)this );
return sv;
}
SV*
Polyline::to_SV_clone_ref() const
{
SV* sv = newSV(0);
sv_setref_pv( sv, "Slic3r::Polyline", new Polyline(*this) );

View file

@ -8,8 +8,11 @@ namespace Slic3r {
class Polyline : public MultiPoint {
public:
Point* last_point();
const Point* last_point() const;
Lines lines();
SV* to_SV_ref() const;
SV* to_SV_clone_ref() const;
};
typedef std::vector<Polyline> Polylines;

View file

@ -5,9 +5,7 @@ namespace Slic3r {
PolylineCollection*
PolylineCollection::chained_path(bool no_reverse) const
{
if (this->polylines.empty()) {
return new PolylineCollection ();
}
if (this->polylines.empty()) return new PolylineCollection ();
return this->chained_path_from(this->polylines.front().first_point(), no_reverse);
}