Refactored some QString usage in the backend to std::string

stl_dim
Marius Kintel 2010-11-07 16:29:34 -05:00
parent d310e364d1
commit e0a068a0e8
19 changed files with 156 additions and 121 deletions

View File

@ -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 << ")";

View File

@ -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);
}

View File

@ -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")

View File

@ -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);

View File

@ -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;

View File

@ -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("[");
for (int i=0; i < children.size(); i++) {
if (i > 0)
text += QString(", ");
text += children[i]->dump();
}
return 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] << ")";
}
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("(");
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();
}
return text + QString(")");
else if (this->type == "?:") {
stream << "(" << *children[0] << " ? " << this->type << " : " << *children[1] << ")";
}
abort();
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) stream << ", ";
stream << *children[i];
}
stream << "]";
}
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) stream << ", ";
if (!call_argnames[i].isEmpty()) stream << call_argnames[i] << " = ";
stream << *children[i];
}
stream << ")";
}
else {
assert(false && "Illegal expression type");
}
return stream.str();
}
std::ostream &operator<<(std::ostream &stream, const Expression &expr)
{
stream << expr.toString();
return stream;
}

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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);

View File

@ -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();

View File

@ -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

View File

@ -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 {

View File

@ -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);

View File

@ -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) {

View File

@ -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) {

View File

@ -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;

View File

@ -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;
}

View File

@ -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