From 5c53ea8fb93f611a90099000fb0ba3b0874f99c5 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Sun, 18 Jan 2015 00:41:56 +0100 Subject: Implemented some helper functions to facilitate implementing doValidate methods. This includes the "validateName" function (which makes sure the Node has a valid identifier), the "continueValidation" function (which descends into the given list of child nodes) and the "continueValidationCheckDuplicates" which descends into the given child list and makes sure the names of the child nodes are unique. --- src/core/model/Node.hpp | 74 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 4 deletions(-) (limited to 'src/core/model/Node.hpp') diff --git a/src/core/model/Node.hpp b/src/core/model/Node.hpp index f8893a7..0168a3e 100644 --- a/src/core/model/Node.hpp +++ b/src/core/model/Node.hpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -196,6 +197,19 @@ private: */ bool continueResolveIndex(const Index &index, ResolutionState &state); + /** + * Checks whether the name of the given node is already stored in the given + * set, if yes, logs a corresponding error message. + * + * @param node is the node of which the name should be checked. + * @param names is a set in which all encountered names are stored. + * @param logger is the logger instance to which error messages are written. + * @return true if the given node has a unique name, false otherwise. + */ + bool checkDuplicate(Handle node, + std::unordered_set &names, + Logger &logger) const; + protected: /** * Function which should be overwritten by derived classes in order to @@ -329,6 +343,56 @@ protected: */ virtual bool doValidate(Logger &logger) const; + /** + * Makes sure the name of this node is a valid identifier and loggs a + * corresponding error message. + * + * @param logger is the logger to which the error message is logged. + * @return true if the name is valid, false otherwise. + */ + bool validateName(Logger &logger) const; + + /** + * Helper function that can be used to forward the validation process to + * child nodes. + * + * @tparam T is the type of the list that should be handled. + * @param list is a list of arbitrary kind. The "validate" function is + * called for all elementsd of the list. + * @param logger is the logger to which any errors should be reported. + */ + template + bool continueValidation(const T &list, Logger &logger) const + { + bool res = true; + for (auto elem : list) { + res = elem->validate(logger) & res; + } + return res; + } + + /** + * Helper function that can be used to forward the validation process to + * child nodes while at the same time checking that the children have no + * duplicated names. + * + * @tparam T is the type of the list that should be handled. + * @param list is a list of arbitrary kind. The "validate" function is + * called for all elementsd of the list. + * @param logger is the logger to which any errors should be reported. + */ + template + bool continueValidationCheckDuplicates(const T &list, Logger &logger) const + { + bool res = true; + std::unordered_set names; + for (auto elem : list) { + res = elem->validate(logger) & checkDuplicate(elem, names, logger) & + res; + } + return res; + } + public: /** * Initializes the node with empty name and parent. @@ -460,8 +524,9 @@ class NodeVector : public ManagedGenericList>, ListAccessor>, Listener> { public: - using ManagedGenericList>, ListAccessor>, - Listener>::ManagedGenericList; + using Base = ManagedGenericList>, ListAccessor>, + Listener>; + using Base::Base; /** * Returns the reference to the internal index. @@ -490,9 +555,10 @@ class NodeMap : public ManagedGenericMap>, MapAccessor>>, Listener> { public: - using ManagedGenericMap>, + using Base = ManagedGenericMap>, MapAccessor>>, - Listener>::ManagedGenericMap; + Listener>; + using Base::Base; /** * Returns the reference to the internal index. -- cgit v1.2.3