From 85d72823ef18711fe7a29f5b23cc37b318766332 Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Fri, 23 Jan 2015 15:47:52 +0100 Subject: Introduced cardinality type. Tests are still needed, though, especially for variantReader and type conversion. --- src/core/common/Variant.hpp | 99 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 86 insertions(+), 13 deletions(-) (limited to 'src/core/common/Variant.hpp') diff --git a/src/core/common/Variant.hpp b/src/core/common/Variant.hpp index 381a13e..f623b6b 100644 --- a/src/core/common/Variant.hpp +++ b/src/core/common/Variant.hpp @@ -40,6 +40,7 @@ // http://nikic.github.io/2012/02/02/Pointer-magic-for-efficient-dynamic-value-representations.html // later (will allow to use 8 bytes for a variant) +#include #include #include "Exceptions.hpp" @@ -63,6 +64,7 @@ enum class VariantType : int16_t { ARRAY, MAP, OBJECT, + CARDINALITY, FUNCTION }; @@ -111,6 +113,8 @@ public: using arrayType = std::vector; using mapType = std::map; using objectType = Rooted; + using cardinalityType = RangeSet; + using rangeType = Range; using functionType = std::shared_ptr; private: @@ -192,6 +196,9 @@ private: case VariantType::OBJECT: ptrVal = new objectType(v.asObject()); break; + case VariantType::CARDINALITY: + ptrVal = new cardinalityType(v.asCardinality()); + break; case VariantType::FUNCTION: ptrVal = new functionType(v.asFunction()); break; @@ -225,6 +232,7 @@ private: case VariantType::ARRAY: case VariantType::MAP: case VariantType::OBJECT: + case VariantType::CARDINALITY: case VariantType::FUNCTION: ptrVal = v.ptrVal; v.ptrVal = nullptr; @@ -253,6 +261,9 @@ private: case VariantType::OBJECT: delete static_cast(ptrVal); break; + case VariantType::CARDINALITY: + delete static_cast(ptrVal); + break; case VariantType::FUNCTION: delete static_cast(ptrVal); break; @@ -346,7 +357,7 @@ public: * * @param o is an object that can be converted to a Rooted handle. */ - template + template static Variant fromObject(T o) { Variant res; @@ -354,6 +365,18 @@ public: return res; } + /** + * Constructor for cardinality values. The given cardinality is copied and + *managed by the + * new Variant instance. + * + * @param c is a reference to the cardinality. + */ + Variant(cardinalityType c) : ptrVal(nullptr) + { + setCardinality(std::move(c)); + } + /** * Named constructor for function values. * @@ -521,6 +544,13 @@ public: */ bool isObject() const { return type == VariantType::OBJECT; } + /** + * Checks whether this Variant instance is a cardinality. + * + * @return true if the Variant instance is an cardinality, false otherwise. + */ + bool isCardinality() const { return type == VariantType::CARDINALITY; } + /** * Checks whether this Variant instance is a function. * @@ -666,13 +696,12 @@ public: const mapType &asMap() const { return asObj(VariantType::MAP); } /** - * Returns a pointer pointing at the stored managed object. Performs no type - * conversion. Throws an exception if the underlying type is not a managed - * object. + * Returns a reference to the map value. Performs no type conversion. + * Throws an exception if the underlying type is not a map. * - * @return pointer at the stored managed object. + * @return the map value as reference. */ - objectType asObject() { return asObj(VariantType::OBJECT); } + mapType &asMap() { return asObj(VariantType::MAP); } /** * Returns a pointer pointing at the stored managed object. Performs no type @@ -687,12 +716,37 @@ public: } /** - * Returns a reference to the map value. Performs no type conversion. - * Throws an exception if the underlying type is not a map. + * Returns a pointer pointing at the stored managed object. Performs no type + * conversion. Throws an exception if the underlying type is not a managed + * object. * - * @return the map value as reference. + * @return pointer at the stored managed object. */ - mapType &asMap() { return asObj(VariantType::MAP); } + objectType asObject() { return asObj(VariantType::OBJECT); } + + /** + * Returns a reference to the cardinality value. Performs no type + *conversion. + * Throws an exception if the underlying type is not a cardinality. + * + * @return the cardinality value as reference. + */ + const cardinalityType &asCardinality() const + { + return asObj(VariantType::CARDINALITY); + } + + /** + * Returns a reference to the cardinality value. Performs no type + * conversion. + * Throws an exception if the underlying type is not a cardinality. + * + * @return the cardinality value as reference. + */ + cardinalityType &asCardinality() + { + return asObj(VariantType::CARDINALITY); + } /** * Returns a shared pointer pointing at the stored function object. Performs @@ -783,6 +837,13 @@ public: */ mapType toMap(const Rtti &innerType) const; + /** + * Returns the value of the Variant as cardinality. + * + * @return the value of the variant as cardinality. + */ + cardinalityType toCardinality() const; + /** * Sets the variant to null. */ @@ -832,7 +893,7 @@ public: /** * Sets the variant to the given string value. * - * @param d is the new string value. + * @param s is the new string value. */ void setString(const char *s) { @@ -849,7 +910,7 @@ public: /** * Sets the variant to the given magic string value. * - * @param d is the new magic string value. + * @param s is the new magic string value. */ void setMagic(const char *s) { @@ -882,7 +943,7 @@ public: /** * Sets the variant to the given map value. * - * @param a is the new map value. + * @param m is the new map value. */ void setMap(mapType m) { @@ -907,6 +968,18 @@ public: ptrVal = new objectType(o); } + /** + * Sets the variant to the given cardinality value. + * + * @param c is the new cardinality value. + */ + void setCardinality(cardinalityType c) + { + destroy(); + type = VariantType::CARDINALITY; + ptrVal = new cardinalityType(std::move(c)); + } + /** * Sets the variant to the given function. * -- cgit v1.2.3