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/parser/ParserScope.cpp | 135 ++++++++++++++++------------------------ src/core/parser/ParserScope.hpp | 37 ++++++----- 2 files changed, 75 insertions(+), 97 deletions(-) (limited to 'src/core/parser') diff --git a/src/core/parser/ParserScope.cpp b/src/core/parser/ParserScope.cpp index c7a9f2a..efd07d1 100644 --- a/src/core/parser/ParserScope.cpp +++ b/src/core/parser/ParserScope.cpp @@ -18,6 +18,7 @@ #include #include +#include #include #include "ParserScope.hpp" @@ -319,6 +320,44 @@ bool ParserScope::resolveType(const std::string &name, Handle owner, return resolveType(Utils::split(name, '.'), owner, logger, resultCallback); } +bool ParserScope::resolveValue(Variant &data, Handle type, + Handle owner, Logger &logger) +{ + return type->build( + data, logger, + [&](Variant &innerData, + const Type *innerType) mutable -> Type::MagicCallbackResult { + // Try to resolve the node + Rooted constant = + ParserScopeBase::resolve(RttiTypes::Constant, + Utils::split(innerData.asMagic(), '.'), + logger).cast(); + + // Abort if nothing was found + if (constant == nullptr) { + return Type::MagicCallbackResult::NOT_FOUND; + } + + // Set the data to the value of the constant + innerData = constant->getValue(); + + // Check whether the inner type of the constant is correct + // TODO: Use correct "isa" provided by Type + Rooted constantType = constant->getType(); + if (innerType != constantType) { + logger.error(std::string("Expected value of type \"") + + innerType->getName() + + std::string("\" but found constant \"") + + constant->getName() + + std::string("\" of type \"") + + constantType->getName() + "\" instead.", + *owner); + return Type::MagicCallbackResult::FOUND_INVALID; + } + return Type::MagicCallbackResult::FOUND_VALID; + }); +} + bool ParserScope::resolveTypeWithValue(const std::vector &path, Handle owner, Variant &value, Logger &logger, @@ -326,90 +365,20 @@ bool ParserScope::resolveTypeWithValue(const std::vector &path, { // Fork the parser scope -- constants need to be resolved in the same // context as this resolve call - std::shared_ptr scope = std::make_shared(fork()); - - return resolveType( - path, owner, logger, - [=](Handle resolved, Handle owner, Logger &logger) mutable { - // Abort if the lookup failed - if (resolved == nullptr) { - resultCallback(resolved, owner, logger); - return; - } - - // Fetch the type reference and the manager reference - Rooted type = resolved.cast(); - Manager *mgr = &type->getManager(); - - // The type has been resolved, try to resolve magic values as - // constants and postpone calling the callback function until - // all magic values have been resolved - std::shared_ptr isAsync = std::make_shared(false); - std::shared_ptr magicCount = std::make_shared(0); - type->build(value, logger, [=](Variant &magicValue, bool isValid, - ManagedUid innerTypeUid) mutable { - // Fetch the inner type - Rooted innerType = - dynamic_cast(mgr->getManaged(innerTypeUid)); - if (innerType == nullptr) { - return; - } - - // Fetch a pointer at the variant - Variant *magicValuePtr = &magicValue; - - // Increment the number of encountered magic values - (*magicCount)++; - - // Try to resolve the value as constant - std::string constantName = magicValue.asMagic(); - scope->resolve(constantName, owner, logger, - [=](Handle resolved, - Handle owner, - Logger &logger) mutable { - if (resolved != nullptr) { - // Make sure the constant is of the correct inner type - Rooted constant = resolved.cast(); - Rooted constantType = constant->getType(); - if (constantType != innerType) { - logger.error( - std::string("Expected value of type \"") + - innerType->getName() + - std::string("\" but found constant \"") + - constant->getName() + - std::string(" of type \"") + - constantType->getName() + "\" instead.", - *owner); - } else if (!isValid) { - logger.error("Identifier \"" + constantName + - "\" is not a valid " + - innerType->getName(), - *owner); - } - - // Nevertheless, no matter what happened, set the value - // of the original magic variant to the given constant - *magicValuePtr = constant->getValue(); - } - - // Decrement the number of magic values, call the callback - // function if all magic values have been resolved - (*magicCount)--; - if ((*magicCount) == 0 && (*isAsync)) { - resultCallback(resolved, owner, logger); - } - }); - }); - - // Now we are asynchronous - (*isAsync) = true; + ParserScope scope = fork(); + Variant *valuePtr = &value; + + return resolveType(path, owner, logger, + [=](Handle resolved, Handle owner, + Logger &logger) mutable { + if (resolved != nullptr) { + Rooted type = resolved.cast(); + scope.resolveValue(*valuePtr, type, owner, logger); + } - // Directly call the callback function if there were no magic values - // involved - if ((*magicCount) == 0) { - resultCallback(resolved, owner, logger); - } - }); + // Call the result callback with the type + resultCallback(resolved, owner, logger); + }); } bool ParserScope::resolveTypeWithValue(const std::string &name, diff --git a/src/core/parser/ParserScope.hpp b/src/core/parser/ParserScope.hpp index c49acd3..2ef33d9 100644 --- a/src/core/parser/ParserScope.hpp +++ b/src/core/parser/ParserScope.hpp @@ -44,6 +44,7 @@ namespace ousia { class CharReader; class Logger; class ParserScope; +class Type; class Variant; /** @@ -60,8 +61,8 @@ using ResolutionImposterCallback = std::function()>; * @param owner is the node that was passed as "owner". * @param logger is the logger to which errors should be logged. */ -using ResolutionResultCallback = std::function resolved, - Handle owner, Logger &logger)>; +using ResolutionResultCallback = std::function< + void(Handle resolved, Handle owner, Logger &logger)>; /** * Base class for the @@ -620,15 +621,26 @@ public: bool resolveType(const std::string &name, Handle owner, Logger &logger, ResolutionResultCallback resultCallback); + /** + * Build and resolves a (possibly) magic value with the given typesystem + * type. This function does not perform any deferred lookups. + * + * @param data is a reference at a variant that may contain magic values + * (even in inner structures). The data will be passed to the "build" + * function of the given type. + * @param type is the Typesystem type the data should be interpreted with. + * @param owner is the node for which the resolution takes place. + * @param logger is the logger instance into which resolution problems + * should be logged. + * @return true if the value was successfully built. + */ + bool resolveValue(Variant &data, Handle type, Handle owner, + Logger &logger); + /** * Resolves a type and makes sure the corresponding value is of the correct * type. * - * Warning: This function is extremely dangerous as you have to make - * sure that the "value" reference stays alife as long as the "owner" is - * valid. This is especially problematic as internally references at parts - * of "value" may be kept. Test usages of this function well! - * * @tparam T is the type of the node that should be resolved. * @param path is the path for which a node should be resolved. * @param owner is the node for which the resolution takes place. @@ -646,17 +658,13 @@ public: */ bool resolveTypeWithValue(const std::vector &path, Handle owner, Variant &value, - Logger &logger, ResolutionResultCallback resultCallback); + Logger &logger, + ResolutionResultCallback resultCallback); /** * Resolves a type and makes sure the corresponding value is of the correct * type. * - * Warning: This function is extremely dangerous as you have to make - * sure that the "value" reference stays alife as long as the "owner" is - * valid. This is especially problematic as internally references at parts - * of "value" may be kept. Test usages of this function well! - * * @tparam T is the type of the node that should be resolved. * @param name is the path for which a node should be resolved. The name is * split at '.' to form a path. @@ -674,7 +682,8 @@ public: * later. */ bool resolveTypeWithValue(const std::string &name, Handle owner, - Variant &value, Logger &logger, ResolutionResultCallback resultCallback); + Variant &value, Logger &logger, + ResolutionResultCallback resultCallback); /** * Tries to resolve all currently deferred resolution steps. The list of -- cgit v1.2.3