summaryrefslogtreecommitdiff
path: root/src/value.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/value.cc')
-rw-r--r--src/value.cc123
1 files changed, 78 insertions, 45 deletions
diff --git a/src/value.cc b/src/value.cc
index b0a79a4..139bd1c 100644
--- a/src/value.cc
+++ b/src/value.cc
@@ -26,6 +26,8 @@
#include "value.h"
#include "mathc99.h"
+#include <assert.h>
+#include <sstream>
Value::Value()
{
@@ -34,8 +36,7 @@ Value::Value()
Value::~Value()
{
- for (int i = 0; i < this->vec.size(); i++)
- delete this->vec[i];
+ for (int i = 0; i < this->vec.size(); i++) delete this->vec[i];
this->vec.clear();
}
@@ -53,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;
@@ -71,8 +72,9 @@ Value& Value::operator = (const Value &v)
this->type = v.type;
this->b = v.b;
this->num = v.num;
- for (int i = 0; i < v.vec.size(); i++)
- this->vec.append(new Value(*v.vec[i]));
+ for (int i = 0; i < v.vec.size(); i++) {
+ this->vec.push_back(new Value(*v.vec[i]));
+ }
this->range_begin = v.range_begin;
this->range_step = v.range_step;
this->range_end = v.range_end;
@@ -110,7 +112,7 @@ Value Value::operator + (const Value &v) const
Value r;
r.type = VECTOR;
for (int i = 0; i < this->vec.size() && i < v.vec.size(); i++)
- r.vec.append(new Value(*this->vec[i] + *v.vec[i]));
+ r.vec.push_back(new Value(*this->vec[i] + *v.vec[i]));
return r;
}
if (this->type == NUMBER && v.type == NUMBER) {
@@ -125,7 +127,7 @@ Value Value::operator - (const Value &v) const
Value r;
r.type = VECTOR;
for (int i = 0; i < this->vec.size() && i < v.vec.size(); i++)
- r.vec.append(new Value(*this->vec[i] - *v.vec[i]));
+ r.vec.push_back(new Value(*this->vec[i] - *v.vec[i]));
return r;
}
if (this->type == NUMBER && v.type == NUMBER) {
@@ -140,14 +142,14 @@ Value Value::operator * (const Value &v) const
Value r;
r.type = VECTOR;
for (int i = 0; i < this->vec.size(); i++)
- r.vec.append(new Value(*this->vec[i] * v));
+ r.vec.push_back(new Value(*this->vec[i] * v));
return r;
}
if (this->type == NUMBER && v.type == VECTOR) {
Value r;
r.type = VECTOR;
for (int i = 0; i < v.vec.size(); i++)
- r.vec.append(new Value(*this * *v.vec[i]));
+ r.vec.push_back(new Value(*this * *v.vec[i]));
return r;
}
if (this->type == NUMBER && v.type == NUMBER) {
@@ -162,14 +164,14 @@ Value Value::operator / (const Value &v) const
Value r;
r.type = VECTOR;
for (int i = 0; i < this->vec.size(); i++)
- r.vec.append(new Value(*this->vec[i] / v));
+ r.vec.push_back(new Value(*this->vec[i] / v));
return r;
}
if (this->type == NUMBER && v.type == VECTOR) {
Value r;
r.type = VECTOR;
for (int i = 0; i < v.vec.size(); i++)
- r.vec.append(new Value(v / *v.vec[i]));
+ r.vec.push_back(new Value(v / *v.vec[i]));
return r;
}
if (this->type == NUMBER && v.type == NUMBER) {
@@ -255,7 +257,7 @@ Value Value::inv() const
Value r;
r.type = VECTOR;
for (int i = 0; i < this->vec.size(); i++)
- r.vec.append(new Value(this->vec[i]->inv()));
+ r.vec.push_back(new Value(this->vec[i]->inv()));
return r;
}
if (this->type == NUMBER)
@@ -307,46 +309,77 @@ 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;
this->b = false;
this->num = 0;
- for (int i = 0; i < this->vec.size(); i++)
- delete this->vec[i];
+ for (int i = 0; i < this->vec.size(); i++) delete this->vec[i];
this->vec.clear();
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;
+ stream.precision(16);
+
+ 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();
+}
+
+/*!
+ Append a value to this vector.
+ This must be of type VECTOR.
+*/
+void Value::append(Value *val)
+{
+ assert(this->type == VECTOR);
+ this->vec.push_back(val);
+}
+
+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;
+}
+
contact: Jan Huwald // Impressum