Convert Assignment to use shared pointers for Expressions (fixes #709).

master
Torsten Paul 2014-03-23 19:38:28 +01:00
parent d253d66000
commit d8a1b5f7eb
17 changed files with 47 additions and 41 deletions

2
.gitignore vendored
View File

@ -20,3 +20,5 @@ testdata/scad/features/import_dxf-tests.scad
testdata/scad/features/import_stl-tests.scad
testdata/scad/misc/include-tests.scad
testdata/scad/misc/use-tests.scad
/mingw32
/mingw64

View File

@ -90,18 +90,18 @@ std::string Builtins::isDeprecated(const std::string &name)
Builtins::Builtins()
{
this->globalscope.assignments.push_back(Assignment("$fn", new Expression(Value(0.0))));
this->globalscope.assignments.push_back(Assignment("$fs", new Expression(Value(2.0))));
this->globalscope.assignments.push_back(Assignment("$fa", new Expression(Value(12.0))));
this->globalscope.assignments.push_back(Assignment("$t", new Expression(Value(0.0))));
this->globalscope.assignments.push_back(Assignment("$fn", boost::shared_ptr<Expression>(new Expression(Value(0.0)))));
this->globalscope.assignments.push_back(Assignment("$fs", boost::shared_ptr<Expression>(new Expression(Value(2.0)))));
this->globalscope.assignments.push_back(Assignment("$fa", boost::shared_ptr<Expression>(new Expression(Value(12.0)))));
this->globalscope.assignments.push_back(Assignment("$t", boost::shared_ptr<Expression>(new Expression(Value(0.0)))));
Value::VectorType zero3;
zero3.push_back(Value(0.0));
zero3.push_back(Value(0.0));
zero3.push_back(Value(0.0));
Value zero3val(zero3);
this->globalscope.assignments.push_back(Assignment("$vpt", new Expression(zero3val)));
this->globalscope.assignments.push_back(Assignment("$vpr", new Expression(zero3val)));
this->globalscope.assignments.push_back(Assignment("$vpt", boost::shared_ptr<Expression>(new Expression(zero3val))));
this->globalscope.assignments.push_back(Assignment("$vpr", boost::shared_ptr<Expression>(new Expression(zero3val))));
}
Builtins::~Builtins()

View File

@ -49,16 +49,16 @@ AbstractNode *CgaladvModule::instantiate(const Context *ctx, const ModuleInstant
AssignmentList args;
if (type == MINKOWSKI)
args += Assignment("convexity", NULL);
args += Assignment("convexity");
if (type == GLIDE)
args += Assignment("path", NULL), Assignment("convexity", NULL);
args += Assignment("path"), Assignment("convexity");
if (type == SUBDIV)
args += Assignment("type", NULL), Assignment("level", NULL), Assignment("convexity", NULL);
args += Assignment("type"), Assignment("level"), Assignment("convexity");
if (type == RESIZE)
args += Assignment("newsize", NULL), Assignment("auto", NULL);
args += Assignment("newsize"), Assignment("auto");
Context c(ctx);
c.setVariables(args, evalctx);

View File

@ -56,7 +56,7 @@ AbstractNode *ColorModule::instantiate(const Context *ctx, const ModuleInstantia
AssignmentList args;
args += Assignment("c", NULL), Assignment("alpha", NULL);
args += Assignment("c"), Assignment("alpha");
Context c(ctx);
c.setVariables(args, evalctx);

View File

@ -83,7 +83,6 @@ std::string AbstractFunction::dump(const std::string &indent, const std::string
Function::~Function()
{
BOOST_FOREACH(const Assignment &arg, this->definition_arguments) delete arg.second;
delete expr;
}

View File

@ -67,8 +67,8 @@ public:
AbstractNode *ImportModule::instantiate(const Context *ctx, const ModuleInstantiation *inst, const EvalContext *evalctx) const
{
AssignmentList args;
args += Assignment("file", NULL), Assignment("layer", NULL), Assignment("convexity", NULL), Assignment("origin", NULL), Assignment("scale", NULL);
args += Assignment("filename",NULL), Assignment("layername", NULL);
args += Assignment("file"), Assignment("layer"), Assignment("convexity"), Assignment("origin"), Assignment("scale");
args += Assignment("filename"), Assignment("layername");
// FIXME: This is broken. Tag as deprecated and fix
// Map old argnames to new argnames for compatibility

View File

@ -54,7 +54,7 @@ AbstractNode *LinearExtrudeModule::instantiate(const Context *ctx, const ModuleI
LinearExtrudeNode *node = new LinearExtrudeNode(inst);
AssignmentList args;
args += Assignment("file", NULL), Assignment("layer", NULL), Assignment("height", NULL), Assignment("origin", NULL), Assignment("scale", NULL), Assignment("center", NULL), Assignment("twist", NULL), Assignment("slices", NULL);
args += Assignment("file"), Assignment("layer"), Assignment("height"), Assignment("origin"), Assignment("scale"), Assignment("center"), Assignment("twist"), Assignment("slices");
Context c(ctx);
c.setVariables(args, evalctx);

View File

@ -14,7 +14,6 @@ LocalScope::LocalScope()
LocalScope::~LocalScope()
{
BOOST_FOREACH (ModuleInstantiation *v, children) delete v;
BOOST_FOREACH (const Assignment &v, assignments) delete v.second;
BOOST_FOREACH (FunctionContainer::value_type &f, functions) delete f.second;
BOOST_FOREACH (AbstractModuleContainer::value_type &m, modules) delete m.second;
}

View File

@ -65,7 +65,6 @@ std::string AbstractModule::dump(const std::string &indent, const std::string &n
ModuleInstantiation::~ModuleInstantiation()
{
BOOST_FOREACH(const Assignment &arg, this->arguments) delete arg.second;
}
IfElseModuleInstantiation::~IfElseModuleInstantiation()

View File

@ -178,13 +178,13 @@ assignment:
bool found = false;
foreach (Assignment& iter, scope_stack.top()->assignments) {
if (iter.first == $1) {
iter.second = $3;
iter.second = boost::shared_ptr<Expression>($3);
found = true;
break;
}
}
if (!found) {
scope_stack.top()->assignments.push_back(Assignment($1, $3));
scope_stack.top()->assignments.push_back(Assignment($1, boost::shared_ptr<Expression>($3)));
}
free($1);
}
@ -247,7 +247,7 @@ if_statement:
TOK_IF '(' expr ')'
{
$<ifelse>$ = new IfElseModuleInstantiation();
$<ifelse>$->arguments.push_back(Assignment("", $3));
$<ifelse>$->arguments.push_back(Assignment("", boost::shared_ptr<Expression>($3)));
$<ifelse>$->setPath(parser_source_path);
scope_stack.push(&$<ifelse>$->scope);
}
@ -477,12 +477,12 @@ arguments_decl:
argument_decl:
TOK_ID
{
$$ = new Assignment($1, NULL);
$$ = new Assignment($1);
free($1);
}
| TOK_ID '=' expr
{
$$ = new Assignment($1, $3);
$$ = new Assignment($1, boost::shared_ptr<Expression>($3));
free($1);
}
;
@ -509,11 +509,11 @@ arguments_call:
argument_call:
expr
{
$$ = new Assignment("", $1);
$$ = new Assignment("", boost::shared_ptr<Expression>($1));
}
| TOK_ID '=' expr
{
$$ = new Assignment($1, $3);
$$ = new Assignment($1, boost::shared_ptr<Expression>($3));
free($1);
}
;

View File

@ -152,25 +152,25 @@ AbstractNode *PrimitiveModule::instantiate(const Context *ctx, const ModuleInsta
switch (this->type) {
case CUBE:
args += Assignment("size", NULL), Assignment("center", NULL);
args += Assignment("size"), Assignment("center");
break;
case SPHERE:
args += Assignment("r", NULL);
args += Assignment("r");
break;
case CYLINDER:
args += Assignment("h", NULL), Assignment("r1", NULL), Assignment("r2", NULL), Assignment("center", NULL);
args += Assignment("h"), Assignment("r1"), Assignment("r2"), Assignment("center");
break;
case POLYHEDRON:
args += Assignment("points", NULL), Assignment("faces", NULL), Assignment("convexity", NULL);
args += Assignment("points"), Assignment("faces"), Assignment("convexity");
break;
case SQUARE:
args += Assignment("size", NULL), Assignment("center", NULL);
args += Assignment("size"), Assignment("center");
break;
case CIRCLE:
args += Assignment("r", NULL);
args += Assignment("r");
break;
case POLYGON:
args += Assignment("points", NULL), Assignment("paths", NULL), Assignment("convexity", NULL);
args += Assignment("points"), Assignment("paths"), Assignment("convexity");
break;
default:
assert(false && "PrimitiveModule::instantiate(): Unknown node type");

View File

@ -49,7 +49,7 @@ AbstractNode *ProjectionModule::instantiate(const Context *ctx, const ModuleInst
ProjectionNode *node = new ProjectionNode(inst);
AssignmentList args;
args += Assignment("cut", NULL);
args += Assignment("cut");
Context c(ctx);
c.setVariables(args, evalctx);

View File

@ -46,7 +46,7 @@ AbstractNode *RenderModule::instantiate(const Context *ctx, const ModuleInstanti
RenderNode *node = new RenderNode(inst);
AssignmentList args;
args += Assignment("convexity", NULL);
args += Assignment("convexity");
Context c(ctx);
c.setVariables(args, evalctx);

View File

@ -52,7 +52,7 @@ AbstractNode *RotateExtrudeModule::instantiate(const Context *ctx, const ModuleI
RotateExtrudeNode *node = new RotateExtrudeNode(inst);
AssignmentList args;
args += Assignment("file", NULL), Assignment("layer", NULL), Assignment("origin", NULL), Assignment("scale", NULL);
args += Assignment("file"), Assignment("layer"), Assignment("origin"), Assignment("scale");
Context c(ctx);
c.setVariables(args, evalctx);

View File

@ -88,7 +88,7 @@ AbstractNode *SurfaceModule::instantiate(const Context *ctx, const ModuleInstant
node->convexity = 1;
AssignmentList args;
args += Assignment("file", NULL), Assignment("center", NULL), Assignment("convexity", NULL);
args += Assignment("file"), Assignment("center"), Assignment("convexity");
Context c(ctx);
c.setVariables(args, evalctx);

View File

@ -63,19 +63,19 @@ AbstractNode *TransformModule::instantiate(const Context *ctx, const ModuleInsta
switch (this->type) {
case SCALE:
args += Assignment("v", NULL);
args += Assignment("v");
break;
case ROTATE:
args += Assignment("a", NULL), Assignment("v", NULL);
args += Assignment("a"), Assignment("v");
break;
case MIRROR:
args += Assignment("v", NULL);
args += Assignment("v");
break;
case TRANSLATE:
args += Assignment("v", NULL);
args += Assignment("v");
break;
case MULTMATRIX:
args += Assignment("m", NULL);
args += Assignment("m");
break;
default:
assert(false);

View File

@ -3,8 +3,15 @@
#include <string>
#include <vector>
#include <boost/shared_ptr.hpp>
class Assignment : public std::pair<std::string, boost::shared_ptr<class Expression> >
{
public:
Assignment(std::string name) : pair(name, boost::shared_ptr<class Expression>()) {}
Assignment(std::string name, boost::shared_ptr<class Expression> expr) : pair(name, expr) {}
};
typedef std::pair<std::string, class Expression*> Assignment;
typedef std::vector<Assignment> AssignmentList;
typedef std::vector<class ModuleInstantiation*> ModuleInstantiationList;