From 729ea17ed17cf81eb19847216406e40686df679d Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Fri, 30 Jan 2015 14:42:06 +0100 Subject: Finished implementing constant importing --- src/core/model/Typesystem.cpp | 34 ++++++++++++++++++++++++++-------- src/core/model/Typesystem.hpp | 37 +++++++++++++++++++++++++++---------- 2 files changed, 53 insertions(+), 18 deletions(-) (limited to 'src/core/model') diff --git a/src/core/model/Typesystem.cpp b/src/core/model/Typesystem.cpp index 591dcbe..dc6df63 100644 --- a/src/core/model/Typesystem.cpp +++ b/src/core/model/Typesystem.cpp @@ -26,7 +26,10 @@ namespace ousia { /* Static helper functions */ -static void NullMagicCallback(Variant &, bool, ManagedUid) {} +static Type::MagicCallbackResult NullMagicCallback(Variant &, const Type *) +{ + return Type::MagicCallbackResult::NOT_FOUND; +} /* Class Type */ @@ -35,13 +38,18 @@ bool Type::build(Variant &data, Logger &logger, { // If the given variant is marked as "magic", try to resolve the real value if (data.isMagic()) { - Variant strData = Variant::fromString(data.asString()); - Logger nullLogger; - bool valid = isValid(strData, nullLogger); - magicCallback(data, valid, - getUid()); - build(strData, nullLogger); - return true; // Just return true for now + switch (magicCallback(data, this)) { + case MagicCallbackResult::NOT_FOUND: + break; + case MagicCallbackResult::FOUND_INVALID: { + // The magic callback has probably already issued an error + // message -- do not print more errors + Logger nullLogger; + return build(data, nullLogger); + } + case MagicCallbackResult::FOUND_VALID: + return true; + } } try { @@ -216,6 +224,10 @@ void Attribute::setDefaultValue(const Variant &defaultValue, Logger &logger) initialize(logger); } +const Variant &Attribute::getDefaultValue() const { return defaultValue; } + +Variant &Attribute::getDefaultValue() { return defaultValue; } + void Attribute::removeDefaultValue() { invalidate(); @@ -224,6 +236,8 @@ void Attribute::removeDefaultValue() optional = false; } +bool Attribute::isOptional() const { return optional; } + void Attribute::setType(Handle type, Logger &logger) { invalidate(); @@ -232,6 +246,8 @@ void Attribute::setType(Handle type, Logger &logger) initialize(logger); } +Rooted Attribute::getType() const { return type; } + /* Class StructType */ bool StructType::resolveIndexKey(const std::string &key, size_t &idx) const @@ -591,6 +607,8 @@ void Constant::setType(Handle type, Logger &logger) this->type->build(this->value, logger); } +const Variant &Constant::getValue() const { return value; } + Variant &Constant::getValue() { return value; } void Constant::setValue(Variant value, Logger &logger) diff --git a/src/core/model/Typesystem.hpp b/src/core/model/Typesystem.hpp index d88a7e9..9d85d80 100644 --- a/src/core/model/Typesystem.hpp +++ b/src/core/model/Typesystem.hpp @@ -57,19 +57,21 @@ class SystemTypesystem; */ class Type : public Node { public: + enum class MagicCallbackResult { NOT_FOUND, FOUND_INVALID, FOUND_VALID }; + /** * Callback function called when a variant with "magic" value is reached. * This callback allows to transform these magic values into something else. * This mechanism is used to resolve constants. * - * @param data is the magic value that should be looked up. - * @param isValid is set to true if the magic value does not necessarily - * have to be looked up, in this case no error message has to be generated - * if the lookup for a constant fails. - * @param type is the managed uid of the underlying Type for which the magic - * value should be looked up. + * @param data is the magic value that should be looked up and the variant + * to which the value of the looked up constant should be written. + * @param type is a const pointer at the type. TODO: Replace this with a + * "ConstHandle". + * @return true if a constant was found, false otherwise. */ - using MagicCallback = std::function; + using MagicCallback = + std::function; protected: /** @@ -503,7 +505,14 @@ public: * @return the default value of the attribute. If no default value has been * given a null variant is returned (the opposite does not hold). */ - Variant& getDefaultValue() { return defaultValue; } + const Variant &getDefaultValue() const; + + /** + * Returns a reference at the default value. + * + * @return a reference at the default value of the attribute. + */ + Variant &getDefaultValue(); /** * Removes any default value from the attribute, making this attribute @@ -517,7 +526,7 @@ public: * * @return true if the attribute is optional, false otherwise. */ - bool isOptional() const { return optional; } + bool isOptional() const; /** * Sets the type of the attribute to the specified value. This will @@ -536,7 +545,7 @@ public: * * @return the underlying type of the Rooted object. */ - Rooted getType() const { return type; } + Rooted getType() const; }; /** @@ -992,6 +1001,14 @@ public: * * @return a const reference to the actual value of the constant. */ + const Variant &getValue() const; + + /** + * Returns a reference pointing at the value of the constant. The value must + * be interpreted with the help of the type of the constant. + * + * @return a reference to the actual value of the constant. + */ Variant &getValue(); /** -- cgit v1.2.3