From 4284cda04b3a1b1f4c5d4368cc8663d01b6925c6 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Tue, 3 Feb 2015 02:28:20 +0100 Subject: Some refactoring, added function for extracting current type signature --- src/core/parser/ParserScope.cpp | 70 ++++++++++++++++----------- src/core/parser/ParserScope.hpp | 103 ++++++++++++++++++++++++---------------- 2 files changed, 105 insertions(+), 68 deletions(-) (limited to 'src') diff --git a/src/core/parser/ParserScope.cpp b/src/core/parser/ParserScope.cpp index efd07d1..42225ca 100644 --- a/src/core/parser/ParserScope.cpp +++ b/src/core/parser/ParserScope.cpp @@ -61,6 +61,38 @@ Rooted ParserScopeBase::resolve(const Rtti &type, return nullptr; } +bool ParserScopeBase::isEmpty() const { return nodes.empty(); } + +Rooted ParserScopeBase::getRoot() const { return nodes.front(); } + +Rooted ParserScopeBase::getLeaf() const { return nodes.back(); } + +const NodeVector &ParserScopeBase::getStack() const { return nodes; } + +std::vector ParserScopeBase::getStackTypeSignature() const +{ + std::vector res; + res.reserve(nodes.size()); + for (size_t i = 0; i < nodes.size(); i++) { + res.push_back(&(nodes[i]->type())); + } + return res; +} + +Rooted ParserScopeBase::select(RttiSet types, int maxDepth) +{ + ssize_t minDepth = 0; + if (maxDepth >= 0) { + minDepth = static_cast(nodes.size()) - (maxDepth + 1); + } + for (ssize_t i = nodes.size() - 1; i >= minDepth; i--) { + if (nodes[i]->type().isOneOf(types)) { + return nodes[i]; + } + } + return nullptr; +} + /* Class DeferredResolution */ DeferredResolution::DeferredResolution(const NodeVector &nodes, @@ -190,24 +222,6 @@ void ParserScope::pop() NodeVector ParserScope::getTopLevelNodes() const { return topLevelNodes; } -Rooted ParserScope::getRoot() const { return nodes.front(); } - -Rooted ParserScope::getLeaf() const { return nodes.back(); } - -Rooted ParserScope::select(RttiSet types, int maxDepth) -{ - ssize_t minDepth = 0; - if (maxDepth >= 0) { - minDepth = static_cast(nodes.size()) - (maxDepth + 1); - } - for (ssize_t i = nodes.size() - 1; i >= minDepth; i--) { - if (nodes[i]->type().isOneOf(types)) { - return nodes[i]; - } - } - return nullptr; -} - void ParserScope::setFlag(ParserFlag flag, bool value) { // Fetch the current stack depth @@ -368,17 +382,17 @@ bool ParserScope::resolveTypeWithValue(const std::vector &path, 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); - } + 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); + } - // Call the result callback with the type - 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 2ef33d9..30db92f 100644 --- a/src/core/parser/ParserScope.hpp +++ b/src/core/parser/ParserScope.hpp @@ -104,6 +104,69 @@ public: */ Rooted resolve(const Rtti &type, const std::vector &path, Logger &logger); + + /** + * Returns true if the stack is empty. + * + * @return true if there is no element on the stack. + */ + bool isEmpty() const; + + /** + * Returns a reference at the internal node stack. + * + * @return a const reference at the internal node stack. + */ + const NodeVector &getStack() const; + + /** + * Returns a list containing the Rtti type of each Node that is currently + * in the stack. + * + * @return the type signature of the current node stack. + */ + std::vector getStackTypeSignature() const; + + /** + * Returns the top-most Node instance in the ParserScopeBase hirarchy. + * + * @return a reference at the root node. + */ + Rooted getRoot() const; + + /** + * Returns the bottom-most Node instance in the ParserScopeBase hirarchy, + * e.g. the node that was pushed last onto the stack. + * + * @return a reference at the leaf node. + */ + Rooted getLeaf() const; + + /** + * Ascends in the stack starting with the leaf node, returns the first node + * that matches the type given in the RttiSet or nullptr if none matches. + * + * @param types is a set of Rtti types for which should be searched in the + * stack. + * @param maxDepth is the maximum number of stack entries the selection + * function may ascend. A negative value indicates no limitation. + */ + Rooted select(RttiSet types, int maxDepth = -1); + + /** + * Ascends in the stack starting with the leaf node, returns the first node + * that matches the given type or nullptr if none matches. + * + * @tparam T is the type that should be searched in the stack. + * @param maxDepth is the maximum number of stack entries the selection + * function may ascend. A negative value indicates no limitation. + */ + template + Rooted select(int maxDepth = -1) + { + return select(RttiSet{&typeOf()}, maxDepth).cast(); + } + }; /** @@ -343,46 +406,6 @@ public: */ NodeVector getTopLevelNodes() const; - /** - * Returns the top-most Node instance in the ParserScope hirarchy. - * - * @return a reference at the root node. - */ - Rooted getRoot() const; - - /** - * Returns the bottom-most Node instance in the ParserScope hirarchy, e.g. - * the node that was pushed last onto the stack. - * - * @return a reference at the leaf node. - */ - Rooted getLeaf() const; - - /** - * Ascends in the stack starting with the leaf node, returns the first node - * that matches the type given in the RttiSet or nullptr if none matches. - * - * @param types is a set of Rtti types for which should be searched in the - * stack. - * @param maxDepth is the maximum number of stack entries the selection - * function may ascend. A negative value indicates no limitation. - */ - Rooted select(RttiSet types, int maxDepth = -1); - - /** - * Ascends in the stack starting with the leaf node, returns the first node - * that matches the given type or nullptr if none matches. - * - * @tparam T is the type that should be searched in the stack. - * @param maxDepth is the maximum number of stack entries the selection - * function may ascend. A negative value indicates no limitation. - */ - template - Rooted select(int maxDepth = -1) - { - return select(RttiSet{&typeOf()}, maxDepth).cast(); - } - /** * Sets a parser flag for the current stack depth. * -- cgit v1.2.3