summaryrefslogtreecommitdiff
path: root/src/core/variant/Variant.hpp
diff options
context:
space:
mode:
authorAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2014-12-10 02:41:23 +0100
committerAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2014-12-10 02:41:23 +0100
commita8f78252f05327d37aec3b853109b6d1359af452 (patch)
tree1eb136057bc59c6cf19ac52d2830c5a186149ce8 /src/core/variant/Variant.hpp
parentd84efdc312a9c5d0b6757c826810809a8e78f1e2 (diff)
parent325d78169620094178513303d65c71d933182ae4 (diff)
Merge branch 'master' of somweyr.de:ousia
Diffstat (limited to 'src/core/variant/Variant.hpp')
-rw-r--r--src/core/variant/Variant.hpp73
1 files changed, 71 insertions, 2 deletions
diff --git a/src/core/variant/Variant.hpp b/src/core/variant/Variant.hpp
index 6476780..1e62644 100644
--- a/src/core/variant/Variant.hpp
+++ b/src/core/variant/Variant.hpp
@@ -65,8 +65,7 @@ public:
/**
* Exception thrown whenever a variant is accessed via a getter function
- * that
- * is not supported for the current variant type.
+ * that is not supported for the current variant type.
*/
class TypeException : public OusiaException {
private:
@@ -686,6 +685,76 @@ public:
// TODO: Use proper serialization function
return os << "\"" << v.first << "\": " << v.second.toString(true);
}
+
+ /*
+ * Comprison operators.
+ */
+
+ friend bool operator<(const Variant &lhs, const Variant &rhs)
+ {
+ // If the types do not match, we can not do a meaningful comparison.
+ if (lhs.getType() != rhs.getType()) {
+ throw TypeException(lhs.getType(), rhs.getType());
+ }
+ switch (lhs.getType()) {
+ case Type::NULLPTR:
+ return false;
+ case Type::BOOL:
+ return lhs.boolVal < rhs.boolVal;
+ case Type::INT:
+ return lhs.intVal < rhs.intVal;
+ case Type::DOUBLE:
+ return lhs.doubleVal < rhs.doubleVal;
+ case Type::STRING:
+ return lhs.asString() < rhs.asString();
+ case Type::ARRAY:
+ return lhs.asArray() < rhs.asArray();
+ case Type::MAP:
+ return lhs.asMap() < rhs.asMap();
+ }
+ throw OusiaException("Internal Error! Unknown type!");
+ }
+ friend bool operator>(const Variant &lhs, const Variant &rhs)
+ {
+ return rhs < lhs;
+ }
+ friend bool operator<=(const Variant &lhs, const Variant &rhs)
+ {
+ return !(lhs > rhs);
+ }
+ friend bool operator>=(const Variant &lhs, const Variant &rhs)
+ {
+ return !(lhs < rhs);
+ }
+
+ friend bool operator==(const Variant &lhs, const Variant &rhs)
+ {
+ if (lhs.getType() != rhs.getType()) {
+ return false;
+ }
+ switch (lhs.getType()) {
+ case Type::NULLPTR:
+ return true;
+ case Type::BOOL:
+ return lhs.boolVal == rhs.boolVal;
+ case Type::INT:
+ return lhs.intVal == rhs.intVal;
+ case Type::DOUBLE:
+ return lhs.doubleVal == rhs.doubleVal;
+ case Type::STRING:
+ return lhs.asString() == rhs.asString();
+ case Type::ARRAY:
+ return lhs.asArray() == rhs.asArray();
+ case Type::MAP:
+ return lhs.asMap() == rhs.asMap();
+ }
+ throw OusiaException("Internal Error! Unknown type!");
+ }
+
+ friend bool operator!=(const Variant &lhs, const Variant &rhs)
+ {
+ return !(lhs == rhs);
+ }
};
}