diff options
author | Marius Kintel <marius@kintel.net> | 2012-03-27 22:05:00 (GMT) |
---|---|---|
committer | Marius Kintel <marius@kintel.net> | 2012-03-27 22:05:58 (GMT) |
commit | 327310f190bbd81c7b71b568d5bf72bb900cc9db (patch) | |
tree | 9399bb490ecafe9f0c7fd209c680311d829eb631 /src/value.h | |
parent | 4394c7a030ce7a08c95bd1af2e8c38ffcf972439 (diff) |
Rewrote the Value class to be based on boost::variant - this should reduce memory footprint and improve performance
Diffstat (limited to 'src/value.h')
-rw-r--r-- | src/value.h | 149 |
1 files changed, 89 insertions, 60 deletions
diff --git a/src/value.h b/src/value.h index 4a67fbc..8534e62 100644 --- a/src/value.h +++ b/src/value.h @@ -3,6 +3,8 @@ #include <vector> #include <string> +#include <boost/variant.hpp> +#include <boost/lexical_cast.hpp> class QuotedString : public std::string { @@ -23,68 +25,95 @@ std::ostream &operator<<(std::ostream &stream, const Filename &filename); class Value { public: - enum type_e { - UNDEFINED, - BOOL, - NUMBER, - RANGE, - VECTOR, - STRING - }; - - enum type_e type; - - bool b; - double num; - std::vector<Value*> vec; - double range_begin; - double range_step; - double range_end; - std::string text; - - Value(); - ~Value(); - - Value(bool v); - Value(double v); - Value(const std::string &t); - - Value(const Value &v); - Value& operator = (const Value &v); - - Value operator ! () const; - Value operator && (const Value &v) const; - Value operator || (const Value &v) const; - - Value operator + (const Value &v) const; - Value operator - (const Value &v) const; - Value operator * (const Value &v) const; - Value operator / (const Value &v) const; - Value operator % (const Value &v) const; - - Value operator < (const Value &v) const; - Value operator <= (const Value &v) const; - Value operator == (const Value &v) const; - Value operator != (const Value &v) const; - Value operator >= (const Value &v) const; - Value operator > (const Value &v) const; - - Value inv() const; - - bool getnum(double &v) const; - bool getv2(double &x, double &y) const; - bool getv3(double &x, double &y, double &z, double defaultval = 0.0) const; - - std::string toString() const; - - bool toBool() const; - - void append(Value *val); + struct RangeType { + RangeType(double begin, double step, double end) + : begin(begin), step(step), end(end) {} + + bool operator==(const RangeType &other) const { + return this->begin == other.begin && + this->step == other.step && + this->end == other.end; + } + + double begin; + double step; + double end; + }; + + typedef std::vector<Value> VectorType; + + enum ValueType { + UNDEFINED, + BOOL, + NUMBER, + STRING, + VECTOR, + RANGE + }; + static Value undefined; + + Value(); + Value(bool v); + Value(int v); + Value(double v); + Value(const std::string &v); + Value(const char *v); + Value(const char v); + Value(const VectorType &v); + Value(const RangeType &v); + Value(double begin, double step, double end); + ~Value() {} + + ValueType type() const; + bool isUndefined() const; + + double toDouble() const; + bool getDouble(double &v) const; + bool toBool() const; + std::string toString() const; + const VectorType &toVector() const; + bool getVec2(double &x, double &y) const; + bool getVec3(double &x, double &y, double &z, double defaultval = 0.0) const; + RangeType toRange() const; + + Value &operator=(const Value &v); + Value operator!() const; + bool operator==(const Value &v) const; + bool operator!=(const Value &v) const; + bool operator&&(const Value &v) const; + bool operator||(const Value &v) const; + bool operator<(const Value &v) const; + bool operator<=(const Value &v) const; + bool operator>=(const Value &v) const; + bool operator>(const Value &v) const; + Value operator-() const; + Value operator[](const Value &v); + Value operator+(const Value &v) const; + Value operator-(const Value &v) const; + Value operator*(const Value &v) const; + Value operator/(const Value &v) const; + Value operator%(const Value &v) const; + + /* + bool getnum(double &v) const; + bool getv2(double &x, double &y) const; + bool getv3(double &x, double &y, double &z, double defaultval = 0.0) const; + + bool toBool() const; + + void append(Value *val); + */ + + friend std::ostream &operator<<(std::ostream &stream, const Value &value) { + if (value.type() == Value::STRING) stream << QuotedString(value.toString()); + else stream << value.toString(); + return stream; + } + + typedef boost::variant< boost::blank, bool, double, std::string, VectorType, RangeType > Variant; private: - void reset_undef(); + Variant value; }; -std::ostream &operator<<(std::ostream &stream, const Value &value); - #endif |