From 586b4486f4661681b8fe0c639830bd9db3986d5a Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Wed, 15 Apr 2015 20:42:01 +0200 Subject: Simplify resolution callback, move NullResolveCallback to ResolutionCallbacks.cpp --- src/core/model/ResolutionCallbacks.cpp | 5 ++++- src/core/model/ResolutionCallbacks.hpp | 19 +++++++++---------- src/core/model/Typesystem.cpp | 25 +++++++------------------ src/core/parser/ParserScope.cpp | 23 +++++++++-------------- src/core/parser/ParserScope.hpp | 4 +--- 5 files changed, 30 insertions(+), 46 deletions(-) diff --git a/src/core/model/ResolutionCallbacks.cpp b/src/core/model/ResolutionCallbacks.cpp index 8c920f1..88f0aa8 100644 --- a/src/core/model/ResolutionCallbacks.cpp +++ b/src/core/model/ResolutionCallbacks.cpp @@ -19,5 +19,8 @@ #include "ResolutionCallbacks.hpp" namespace ousia { - // Do nothing here, just make sure the header compiles +Rooted NullResolveCallback(const Rtti *, const std::vector &) +{ + return nullptr; +} } diff --git a/src/core/model/ResolutionCallbacks.hpp b/src/core/model/ResolutionCallbacks.hpp index 2d9465d..2ac78a6 100644 --- a/src/core/model/ResolutionCallbacks.hpp +++ b/src/core/model/ResolutionCallbacks.hpp @@ -57,20 +57,19 @@ using ResolutionResultCallback = std::function< /** * The ResolveCallback can be used to trigger the resolution of a certain node. * - * @param async if true, the resolution may be deferred. In this case the - * resultCallback may be called at any later point in the program. * @param type is the type of node that should be resolved. * @param path is the path for which a node should be resolved. - * @param resultCallback is the callback function to which the result of - * the resolution process is passed. This function is called once the - * resolution was successful. - * @return true if the resolution was immediately successful. This does not - * mean, that the resolved object does not exist, as it may be resolved - * later. + * @return a the resolved node or nullptr if the resolution was not successful. */ using ResolveCallback = std::function< - bool(bool async, const Rtti *type, const std::vector &path, - ResolutionResultCallback resultCallback)>; + Rooted(const Rtti *type, const std::vector &path)>; + +/** + * Empty implementation of the ResolveCallback, always returns nullptr. + */ +Rooted NullResolveCallback(const Rtti *, + const std::vector &); + } #endif /* _OUSIA_RESOLUTION_CALLBACKS_HPP_ */ diff --git a/src/core/model/Typesystem.cpp b/src/core/model/Typesystem.cpp index 48e8d90..86ee811 100644 --- a/src/core/model/Typesystem.cpp +++ b/src/core/model/Typesystem.cpp @@ -25,15 +25,6 @@ namespace ousia { -/* Static helper functions */ - -static bool NullResolveCallback(bool, const Rtti *, - const std::vector &, - ResolutionResultCallback) -{ - return false; -} - /* Class Type */ bool Type::build(Variant &data, Logger &logger, @@ -41,15 +32,13 @@ bool Type::build(Variant &data, Logger &logger, { // If the given variant is marked as "magic", try to resolve the real value if (data.isMagic()) { - Rooted constant; - if (resolveCallback( - false, &RttiTypes::Constant, Utils::split(data.asMagic(), '.'), - [&constant](Handle resolved, Handle owner, - Logger &logger) mutable { - // Copy the resolution result out of this function - constant = resolved.cast(); - })) { - // Check whether the inner type of the constant is correct + // Try to resolve a constant + Rooted constant = + resolveCallback(&RttiTypes::Constant, + Utils::split(data.asMagic(), '.')).cast(); + + // Check whether the inner type of the constant is correct + if (constant != nullptr) { Rooted constantType = constant->getType(); if (!constantType->checkIsa(this)) { logger.error( diff --git a/src/core/parser/ParserScope.cpp b/src/core/parser/ParserScope.cpp index 5e4b37f..7a7cb74 100644 --- a/src/core/parser/ParserScope.cpp +++ b/src/core/parser/ParserScope.cpp @@ -409,20 +409,15 @@ bool ParserScope::resolveType(const std::string &name, Handle owner, return resolveType(Utils::split(name, '.'), owner, logger, resultCallback); } -bool ParserScope::resolveValue(Variant &data, Handle owner, - Handle type, Logger &logger) +bool ParserScope::resolveValue(Variant &data, Handle type, Logger &logger) { - 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); - }); + // Run the build function + return type->build( + data, logger, + [&logger, this](const Rtti *rttiType, + const std::vector &path) mutable { + return ParserScopeBase::resolve(rttiType, path, logger); + }); } bool ParserScope::resolveTypeWithValue(const std::vector &path, @@ -440,7 +435,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, owner, type, logger); + scope.resolveValue(*valuePtr, type, logger); } // Call the result callback with the type diff --git a/src/core/parser/ParserScope.hpp b/src/core/parser/ParserScope.hpp index eec4751..b4ce037 100644 --- a/src/core/parser/ParserScope.hpp +++ b/src/core/parser/ParserScope.hpp @@ -700,14 +700,12 @@ 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 owner, Handle type, - Logger &logger); + bool resolveValue(Variant &data, Handle type, Logger &logger); /** * Resolves a type and makes sure the corresponding value is of the correct -- cgit v1.2.3