From 3803f1238f061a4cc962e67630c3bbbbae01eae5 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Wed, 15 Apr 2015 00:37:39 +0200 Subject: Replace Typesystem "MagicCallback" by a more generic one. --- src/core/parser/ParserScope.cpp | 53 ++++++++----------------------- src/core/parser/ParserScope.hpp | 32 ++++++------------- src/core/parser/stack/DocumentHandler.cpp | 4 ++- 3 files changed, 26 insertions(+), 63 deletions(-) (limited to 'src/core/parser') diff --git a/src/core/parser/ParserScope.cpp b/src/core/parser/ParserScope.cpp index ed35839..5e4b37f 100644 --- a/src/core/parser/ParserScope.cpp +++ b/src/core/parser/ParserScope.cpp @@ -409,45 +409,20 @@ bool ParserScope::resolveType(const std::string &name, Handle owner, return resolveType(Utils::split(name, '.'), owner, logger, resultCallback); } -bool ParserScope::resolveValue(Variant &data, Handle type, Logger &logger) +bool ParserScope::resolveValue(Variant &data, Handle owner, + Handle type, 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; - } - - // Check whether the inner type of the constant is correct - Type::MagicCallbackResult res = - Type::MagicCallbackResult::FOUND_VALID; - Rooted constantType = constant->getType(); - if (!constantType->checkIsa(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.", - innerData); - logger.note("Constant was defined here:", *constant); - res = Type::MagicCallbackResult::FOUND_INVALID; - } - - // Set the data to the value of the constant (even if an error - // happend -- probably the type was not that wrong -- who knows?) - innerData = constant->getValue(); - - return res; - }); + return type->build(data, logger, + [&](bool async, const Rtti *rttiType, + const std::vector &path, + ResolutionResultCallback resultCallback) mutable { + if (!async) { + Rooted resolved = ParserScopeBase::resolve(rttiType, path, logger); + resultCallback(resolved, owner, logger); + return resolved != nullptr; + } + return resolve(rttiType, path, type, logger, resultCallback); + }); } bool ParserScope::resolveTypeWithValue(const std::vector &path, @@ -465,7 +440,7 @@ bool ParserScope::resolveTypeWithValue(const std::vector &path, [=](Handle resolved, Handle owner, Logger &logger) mutable { if (resolved != nullptr) { Rooted type = resolved.cast(); - scope.resolveValue(*valuePtr, type, logger); + scope.resolveValue(*valuePtr, owner, type, logger); } // Call the result callback with the type diff --git a/src/core/parser/ParserScope.hpp b/src/core/parser/ParserScope.hpp index 85b5d8b..eec4751 100644 --- a/src/core/parser/ParserScope.hpp +++ b/src/core/parser/ParserScope.hpp @@ -28,6 +28,7 @@ #include #include #include +#include /** * @file ParserScope.hpp @@ -47,23 +48,6 @@ class ParserScope; class Type; class Variant; -/** - * Callback function type used for creating a dummy object while no correct - * object is available for resolution. - */ -using ResolutionImposterCallback = std::function()>; - -/** - * Callback function type called whenever the result of a resolution is - * available. - * - * @param resolved is the new, resolved node. - * @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< - void(Handle resolved, Handle owner, Logger &logger)>; - /** * Base class for the ParserScope, does not contain the mechanisms for deferred * lookup, only maintains the stack of nodes. @@ -288,14 +272,14 @@ enum class ParserFlag { POST_HEAD, /** - * Set to the boolean value "true" if explicit fields may no longer be - * defined inside a structure element. - */ + * Set to the boolean value "true" if explicit fields may no longer be + * defined inside a structure element. + */ POST_EXPLICIT_FIELDS, /** - * Set to true if all user defined tokens have been registered. - */ + * Set to true if all user defined tokens have been registered. + */ POST_USER_DEFINED_TOKEN_REGISTRATION }; @@ -716,12 +700,14 @@ public: * @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 owner is the node for which the resolution takes place. * @param type is the Typesystem type the data should be interpreted with. * @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, Logger &logger); + bool resolveValue(Variant &data, Handle owner, Handle type, + Logger &logger); /** * Resolves a type and makes sure the corresponding value is of the correct diff --git a/src/core/parser/stack/DocumentHandler.cpp b/src/core/parser/stack/DocumentHandler.cpp index 7564fad..2228f11 100644 --- a/src/core/parser/stack/DocumentHandler.cpp +++ b/src/core/parser/stack/DocumentHandler.cpp @@ -768,7 +768,9 @@ bool DocumentChildHandler::convertData(Handle field, } // Now try to resolve the value for the primitive type - return valid && scope().resolveValue(data, type, logger); + return valid && + scope().resolveValue(data, field->getParent().cast(), type, + logger); } bool DocumentChildHandler::data() -- cgit v1.2.3