diff options
author | Marius Kintel <marius@kintel.net> | 2010-11-07 21:29:34 (GMT) |
---|---|---|
committer | Marius Kintel <marius@kintel.net> | 2010-11-07 21:29:34 (GMT) |
commit | e0a068a0e8678da426e1edd398feab5f4ea4d0f0 (patch) | |
tree | 5eaa05991d1652fd10692eb7410143d973db6643 | |
parent | d310e364d14444a1a27ae2337cfb4bd0ab061113 (diff) |
Refactored some QString usage in the backend to std::string
-rw-r--r-- | src/cgaladv.cc | 4 | ||||
-rw-r--r-- | src/control.cc | 2 | ||||
-rw-r--r-- | src/dxfdim.cc | 10 | ||||
-rw-r--r-- | src/dxflinextrude.cc | 6 | ||||
-rw-r--r-- | src/dxfrotextrude.cc | 6 | ||||
-rw-r--r-- | src/expr.cc | 85 | ||||
-rw-r--r-- | src/expression.h | 4 | ||||
-rw-r--r-- | src/func.cc | 18 | ||||
-rw-r--r-- | src/import.cc | 6 | ||||
-rw-r--r-- | src/module.cc | 6 | ||||
-rw-r--r-- | src/node.cc | 6 | ||||
-rw-r--r-- | src/node.h | 2 | ||||
-rw-r--r-- | src/parser.y | 2 | ||||
-rw-r--r-- | src/primitives.cc | 6 | ||||
-rw-r--r-- | src/surface.cc | 2 | ||||
-rw-r--r-- | src/traverser.cc | 6 | ||||
-rw-r--r-- | src/traverser.h | 2 | ||||
-rw-r--r-- | src/value.cc | 83 | ||||
-rw-r--r-- | src/value.h | 13 |
19 files changed, 152 insertions, 117 deletions
diff --git a/src/cgaladv.cc b/src/cgaladv.cc index d71e5b0..0e89ed5 100644 --- a/src/cgaladv.cc +++ b/src/cgaladv.cc @@ -80,7 +80,7 @@ public: } Value path; - QString subdiv_type; + std::string subdiv_type; int convexity, level; cgaladv_type_e type; }; @@ -155,7 +155,7 @@ std::string CgaladvNode::toString() const stream << "(convexity = " << this->convexity << ")"; break; case GLIDE: - stream << "(path = " << this->path.dump() << ", convexity = " << this->convexity << ")"; + stream << "(path = " << this->path << ", convexity = " << this->convexity << ")"; break; case SUBDIV: stream << "(level = " << this->level << ", convexity = " << this->convexity << ")"; diff --git a/src/control.cc b/src/control.cc index 5b7e1b1..7b631f9 100644 --- a/src/control.cc +++ b/src/control.cc @@ -123,7 +123,7 @@ AbstractNode *ControlModule::evaluate(const Context*, const ModuleInstantiation msg += QString(", "); if (!inst->argnames[i].isEmpty()) msg += inst->argnames[i] + QString(" = "); - msg += inst->argvalues[i].dump(); + msg += QString::fromStdString(inst->argvalues[i].toString()); } PRINT(msg); } diff --git a/src/dxfdim.cc b/src/dxfdim.cc index 02a1274..160fb4a 100644 --- a/src/dxfdim.cc +++ b/src/dxfdim.cc @@ -51,15 +51,15 @@ Value builtin_dxf_dim(const Context *ctx, const QVector<QString> &argnames, cons for (int i = 0; i < argnames.count() && i < args.count(); i++) { if (argnames[i] == "file") - filename = ctx->get_absolute_path(args[i].text); + filename = ctx->get_absolute_path(QString::fromStdString(args[i].text)); if (argnames[i] == "layer") - layername = args[i].text; + layername = QString::fromStdString(args[i].text); if (argnames[i] == "origin") args[i].getv2(xorigin, yorigin); if (argnames[i] == "scale") args[i].getnum(scale); if (argnames[i] == "name") - name = args[i].text; + name = QString::fromStdString(args[i].text); } struct stat st; @@ -135,9 +135,9 @@ Value builtin_dxf_cross(const Context *ctx, const QVector<QString> &argnames, co for (int i = 0; i < argnames.count() && i < args.count(); i++) { if (argnames[i] == "file") - filename = ctx->get_absolute_path(args[i].text); + filename = ctx->get_absolute_path(QString::fromStdString(args[i].text)); if (argnames[i] == "layer") - layername = args[i].text; + layername = QString::fromStdString(args[i].text); if (argnames[i] == "origin") args[i].getv2(xorigin, yorigin); if (argnames[i] == "scale") diff --git a/src/dxflinextrude.cc b/src/dxflinextrude.cc index eb47b93..aa45d79 100644 --- a/src/dxflinextrude.cc +++ b/src/dxflinextrude.cc @@ -77,10 +77,10 @@ AbstractNode *DxfLinearExtrudeModule::evaluate(const Context *ctx, const ModuleI Value twist = c.lookup_variable("twist", true); Value slices = c.lookup_variable("slices", true); - if (!file.text.isNull()) - node->filename = c.get_absolute_path(file.text); + if (!file.text.empty()) + node->filename = c.get_absolute_path(QString::fromStdString(file.text)); - node->layername = layer.text; + node->layername = QString::fromStdString(layer.text); node->height = height.num; node->convexity = (int)convexity.num; origin.getv2(node->origin_x, node->origin_y); diff --git a/src/dxfrotextrude.cc b/src/dxfrotextrude.cc index 7903579..b59270f 100644 --- a/src/dxfrotextrude.cc +++ b/src/dxfrotextrude.cc @@ -71,10 +71,10 @@ AbstractNode *DxfRotateExtrudeModule::evaluate(const Context *ctx, const ModuleI Value origin = c.lookup_variable("origin", true); Value scale = c.lookup_variable("scale", true); - if (!file.text.isNull()) - node->filename = c.get_absolute_path(file.text); + if (!file.text.empty()) + node->filename = c.get_absolute_path(QString::fromStdString(file.text)); - node->layername = layer.text; + node->layername = QString::fromStdString(layer.text); node->convexity = (int)convexity.num; origin.getv2(node->origin_x, node->origin_y); node->scale = scale.num; diff --git a/src/expr.cc b/src/expr.cc index 23bcc32..f8169c8 100644 --- a/src/expr.cc +++ b/src/expr.cc @@ -26,6 +26,8 @@ #include "expression.h" #include "value.h" #include "context.h" +#include <assert.h> +#include <sstream> Expression::Expression() { @@ -142,45 +144,62 @@ Value Expression::evaluate(const Context *context) const abort(); } -QString Expression::dump() const +std::string Expression::toString() const { - if (type == "*" || type == "/" || type == "%" || type == "+" || type == "-" || - type == "<" || type == "<=" || type == "==" || type == "!=" || type == ">=" || type == ">") - return QString("(%1 %2 %3)").arg(children[0]->dump(), QString(type), children[1]->dump()); - if (type == "?:") - return QString("(%1 ? %2 : %3)").arg(children[0]->dump(), children[1]->dump(), children[2]->dump()); - if (type == "[]") - return QString("(%1[%2])").arg(children[0]->dump(), children[1]->dump()); - if (type == "I") - return QString("(-%1)").arg(children[0]->dump()); - if (type == "C") - return const_value->dump(); - if (type == "R") - return QString("[%1 : %2 : %3]").arg(children[0]->dump(), children[1]->dump(), children[2]->dump()); - if (type == "V") { - QString text = QString("["); + std::stringstream stream; + + if (this->type == "*" || this->type == "/" || this->type == "%" || this->type == "+" || + this->type == "-" || this->type == "<" || this->type == "<=" || this->type == "==" || + this->type == "!=" || this->type == ">=" || this->type == ">") { + stream << "(" << *children[0] << " " << this->type << " " << *children[1] << ")"; + } + else if (this->type == "?:") { + stream << "(" << *children[0] << " ? " << this->type << " : " << *children[1] << ")"; + } + else if (this->type == "[]") { + stream << "(" << *children[0] << "[" << *children[1] << "])"; + } + else if (this->type == "I") { + stream << "(-" << *children[0] << ")"; + } + else if (this->type == "C") { + stream << *const_value; + } + else if (this->type == "R") { + stream << "[" << *children[0] << " : " << *children[1] << " : " << children[2] << "]"; + } + else if (this->type == "V") { + stream << "["; for (int i=0; i < children.size(); i++) { - if (i > 0) - text += QString(", "); - text += children[i]->dump(); + if (i > 0) stream << ", "; + stream << *children[i]; } - return text + QString("]"); + stream << "]"; } - if (type == "L") - return var_name; - if (type == "N") - return QString("(%1.%2)").arg(children[0]->dump(), var_name); - if (type == "F") { - QString text = call_funcname + QString("("); + else if (this->type == "L") { + stream << var_name; + } + else if (this->type == "N") { + stream << "(" << *children[0] << "." << var_name << ")"; + } + else if (this->type == "F") { + stream << call_funcname << "("; for (int i=0; i < children.size(); i++) { - if (i > 0) - text += QString(", "); - if (!call_argnames[i].isEmpty()) - text += call_argnames[i] + QString(" = "); - text += children[i]->dump(); + if (i > 0) stream << ", "; + if (!call_argnames[i].isEmpty()) stream << call_argnames[i] << " = "; + stream << *children[i]; } - return text + QString(")"); + stream << ")"; } - abort(); + else { + assert(false && "Illegal expression type"); + } + + return stream.str(); } +std::ostream &operator<<(std::ostream &stream, const Expression &expr) +{ + stream << expr.toString(); + return stream; +} diff --git a/src/expression.h b/src/expression.h index dadcea0..38043db 100644 --- a/src/expression.h +++ b/src/expression.h @@ -34,7 +34,9 @@ public: ~Expression(); Value evaluate(const class Context *context) const; - QString dump() const; + std::string toString() const; }; +std::ostream &operator<<(std::ostream &stream, const Expression &expr); + #endif diff --git a/src/func.cc b/src/func.cc index eb0afcd..d73b152 100644 --- a/src/func.cc +++ b/src/func.cc @@ -29,6 +29,7 @@ #include "dxfdim.h" #include "builtin.h" #include <math.h> +#include <sstream> AbstractFunction::~AbstractFunction() { @@ -68,9 +69,9 @@ QString Function::dump(QString indent, QString name) const text += QString(", "); text += argnames[i]; if (argexpr[i]) - text += QString(" = ") + argexpr[i]->dump(); + text += QString(" = ") + QString::fromStdString(argexpr[i]->toString()); } - text += QString(") = %1;\n").arg(expr->dump()); + text += QString(") = %1;\n").arg(QString::fromStdString(expr->toString())); return text; } @@ -257,15 +258,12 @@ Value builtin_ln(const Context *, const QVector<QString>&, const QVector<Value> Value builtin_str(const Context *, const QVector<QString>&, const QVector<Value> &args) { - QString str; - for (int i = 0; i < args.size(); i++) - { - if (args[i].type == Value::STRING) - str += args[i].text; - else - str += args[i].dump(); + std::stringstream stream; + + for (int i = 0; i < args.size(); i++) { + stream << args[i]; } - return Value(str); + return Value(stream.str()); } Value builtin_lookup(const Context *, const QVector<QString>&, const QVector<Value> &args) diff --git a/src/import.cc b/src/import.cc index aeeeb71..ba65cbb 100644 --- a/src/import.cc +++ b/src/import.cc @@ -76,8 +76,10 @@ AbstractNode *ImportModule::evaluate(const Context *ctx, const ModuleInstantiati node->fs = c.lookup_variable("$fs").num; node->fa = c.lookup_variable("$fa").num; - node->filename = c.get_absolute_path(c.lookup_variable("file").text); - node->layername = c.lookup_variable("layer", true).text; + Value v = c.lookup_variable("file"); + node->filename = c.get_absolute_path(QString::fromStdString(v.text)); +// node->filename = c.get_absolute_path(QString::fromStdString(c.lookup_variable("file").text)); + node->layername = QString::fromStdString(c.lookup_variable("layer", true).text); node->convexity = c.lookup_variable("convexity", true).num; if (node->convexity <= 0) diff --git a/src/module.cc b/src/module.cc index 1a1e30e..49a599a 100644 --- a/src/module.cc +++ b/src/module.cc @@ -78,7 +78,7 @@ QString ModuleInstantiation::dump(QString indent) const text += QString(", "); if (!argnames[i].isEmpty()) text += argnames[i] + QString(" = "); - text += argexpr[i]->dump(); + text += QString::fromStdString(argexpr[i]->toString()); } if (children.size() == 0) { text += QString(");\n"); @@ -166,7 +166,7 @@ QString Module::dump(QString indent, QString name) const text += QString(", "); text += argnames[i]; if (argexpr[i]) - text += QString(" = ") + argexpr[i]->dump(); + text += QString(" = ") + QString::fromStdString(argexpr[i]->toString()); } text += QString(") {\n"); tab = "\t"; @@ -186,7 +186,7 @@ QString Module::dump(QString indent, QString name) const } } for (int i = 0; i < assignments_var.size(); i++) { - text += QString("%1%2 = %3;\n").arg(indent + tab, assignments_var[i], assignments_expr[i]->dump()); + text += QString("%1%2 = %3;\n").arg(indent + tab, assignments_var[i], QString::fromStdString(assignments_expr[i]->toString())); } for (int i = 0; i < children.size(); i++) { text += children[i]->dump(indent + tab); diff --git a/src/node.cc b/src/node.cc index e1dd282..884e983 100644 --- a/src/node.cc +++ b/src/node.cc @@ -96,12 +96,6 @@ void AbstractNode::progress_report() const progress_update(this, this->progress_mark); } -std::ostream &operator<<(std::ostream &stream, const QString &str) -{ - stream << str.toStdString(); - return stream; -} - std::ostream &operator<<(std::ostream &stream, const AbstractNode &node) { stream << node.toString(); @@ -87,7 +87,5 @@ public: }; std::ostream &operator<<(std::ostream &stream, const AbstractNode &node); -// FIXME: Doesn't belong here.. -std::ostream &operator<<(std::ostream &stream, const QString &str); #endif diff --git a/src/parser.y b/src/parser.y index 0cace25..f0a06f5 100644 --- a/src/parser.y +++ b/src/parser.y @@ -331,7 +331,7 @@ expr: TOK_STRING { $$ = new Expression(); $$->type = "C"; - $$->const_value = new Value(QString($1)); + $$->const_value = new Value(std::string($1)); free($1); } | TOK_NUMBER { diff --git a/src/primitives.cc b/src/primitives.cc index dd284fa..187a4ea 100644 --- a/src/primitives.cc +++ b/src/primitives.cc @@ -579,8 +579,8 @@ std::string PrimitiveNode::toString() const << ", r2 = " << this->r2 << ", center = " << (center ? "true" : "false") << ")"; break; case POLYHEDRON: - stream << "(points = " << this->points.dump() - << ", triangles = " << this->triangles.dump() + stream << "(points = " << this->points + << ", triangles = " << this->triangles << ", convexity = " << this->convexity << ")"; break; case SQUARE: @@ -592,7 +592,7 @@ std::string PrimitiveNode::toString() const << ", $fs = " << this->fs << ", r = " << this->r1 << ")"; break; case POLYGON: - stream << "(points = " << this->points.dump() << ", paths = " << this->paths.dump() << ", convexity = " << this->convexity << ")"; + stream << "(points = " << this->points << ", paths = " << this->paths << ", convexity = " << this->convexity << ")"; break; default: assert(false); diff --git a/src/surface.cc b/src/surface.cc index 725c1b1..c1cbe07 100644 --- a/src/surface.cc +++ b/src/surface.cc @@ -71,7 +71,7 @@ AbstractNode *SurfaceModule::evaluate(const Context *ctx, const ModuleInstantiat Context c(ctx); c.args(argnames, argexpr, inst->argnames, inst->argvalues); - node->filename = c.get_absolute_path(c.lookup_variable("file").text); + node->filename = c.get_absolute_path(QString::fromStdString(c.lookup_variable("file").text)); Value center = c.lookup_variable("center", true); if (center.type == Value::BOOL) { diff --git a/src/traverser.cc b/src/traverser.cc index af29132..27bb116 100644 --- a/src/traverser.cc +++ b/src/traverser.cc @@ -6,10 +6,10 @@ void Traverser::execute() { State state(NULL); - traverse(state, this->root); + traverse(this->root, state); } -void Traverser::traverse(const State &state, const AbstractNode &node) +void Traverser::traverse(const AbstractNode &node, const State &state) { // FIXME: Handle abort @@ -28,7 +28,7 @@ void Traverser::traverse(const State &state, const AbstractNode &node) iter != children.end(); iter++) { - traverse(newstate, **iter); + traverse(**iter, newstate); } if (traversaltype == POSTFIX || traversaltype == PRE_AND_POSTFIX) { diff --git a/src/traverser.h b/src/traverser.h index 80913e3..a96b05b 100644 --- a/src/traverser.h +++ b/src/traverser.h @@ -16,7 +16,7 @@ public: void execute(); private: // FIXME: reverse parameters - void traverse(const class State &state, const AbstractNode &node); + void traverse(const AbstractNode &node, const class State &state); Visitor &visitor; const AbstractNode &root; diff --git a/src/value.cc b/src/value.cc index a237c5a..e934ff7 100644 --- a/src/value.cc +++ b/src/value.cc @@ -25,6 +25,8 @@ #include "value.h" #include <math.h> +#include <sstream> +#include <QString> Value::Value() { @@ -52,7 +54,7 @@ Value::Value(double v) this->num = v; } -Value::Value(const QString &t) +Value::Value(const std::string &t) { reset_undef(); this->type = STRING; @@ -306,36 +308,6 @@ bool Value::getv3(double &x, double &y, double &z) const return true; } -QString Value::dump() const -{ - if (this->type == STRING) { - return QString("\"") + this->text + QString("\""); - } - if (this->type == VECTOR) { - QString text = "["; - for (int i = 0; i < this->vec.size(); i++) { - if (i > 0) - text += ", "; - text += this->vec[i]->dump(); - } - return text + "]"; - } - if (this->type == RANGE) { - QString text; - text.sprintf("[ %g : %g : %g ]", this->range_begin, this->range_step, this->range_end); - return text; - } - if (this->type == NUMBER) { - QString text; - text.sprintf("%g", this->num); - return text; - } - if (this->type == BOOL) { - return QString(this->b ? "true" : "false"); - } - return QString("undef"); -} - void Value::reset_undef() { this->type = UNDEFINED; @@ -347,5 +319,52 @@ void Value::reset_undef() this->range_begin = 0; this->range_step = 0; this->range_end = 0; - this->text = QString(); + this->text = ""; } + +std::string Value::toString() const +{ + std::stringstream stream; + + switch (this->type) { + case STRING: + stream << '"' << this->text << '"'; + break; + case VECTOR: + stream << '['; + for (int i = 0; i < this->vec.size(); i++) { + if (i > 0) stream << ", "; + stream << *(this->vec[i]); + } + stream << ']'; + break; + case RANGE: + stream << "[ " + << this->range_begin << " : " << this->range_step << " : " << this->range_end + << " ]"; + break; + case NUMBER: + stream << this->num; + break; + case BOOL: + stream << this->b; + break; + default: + stream << "undef"; + } + + return stream.str(); +} + +std::ostream &operator<<(std::ostream &stream, const Value &value) +{ + stream << value.toString(); + return stream; +} + +std::ostream &operator<<(std::ostream &stream, const QString &str) +{ + stream << str.toStdString(); + return stream; +} + diff --git a/src/value.h b/src/value.h index 982d5d8..3f9e9ba 100644 --- a/src/value.h +++ b/src/value.h @@ -2,7 +2,6 @@ #define VALUE_H_ #include <QVector> -#include <QString> class Value { @@ -24,14 +23,14 @@ public: double range_begin; double range_step; double range_end; - QString text; + std::string text; Value(); ~Value(); Value(bool v); Value(double v); - Value(const QString &t); + Value(const std::string &t); Value(const Value &v); Value& operator = (const Value &v); @@ -59,11 +58,15 @@ public: bool getv2(double &x, double &y) const; bool getv3(double &x, double &y, double &z) const; - // FIXME: stream support - QString dump() const; + std::string toString() const; private: void reset_undef(); }; +std::ostream &operator<<(std::ostream &stream, const Value &value); + +// FIXME: Doesn't belong here.. +std::ostream &operator<<(std::ostream &stream, const QString &str); + #endif |