diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/core/model/Typesystem.cpp | 41 | ||||
-rw-r--r-- | src/core/model/Typesystem.hpp | 10 |
2 files changed, 45 insertions, 6 deletions
diff --git a/src/core/model/Typesystem.cpp b/src/core/model/Typesystem.cpp index 561692a..812d85c 100644 --- a/src/core/model/Typesystem.cpp +++ b/src/core/model/Typesystem.cpp @@ -313,30 +313,50 @@ bool StructType::doBuild(Variant &data, Logger &logger) const return buildFromArrayOrMap(data, logger, false); } -Rooted<StructType> StructType::createValidated( - Manager &mgr, std::string name, Handle<Typesystem> system, - Handle<StructType> parent, NodeVector<Attribute> attributes, Logger &logger) +Rooted<StructType> StructType::createValidated(Manager &mgr, std::string name, + Handle<Typesystem> system, + Handle<StructType> parent, + NodeVector<Attribute> attributes, + Logger &logger) { // Check the attributes for validity and uniqueness std::map<std::string, size_t> attributeNames; + NodeVector<Attribute> collectedAttributes; + + // Copy the attributes from the parent structure + if (parent != nullptr) { + attributeNames = parent->attributeNames; + collectedAttributes = parent->attributes; + } + + // Check the attributes for validity and uniqueness for (size_t idx = 0; idx < attributes.size(); idx++) { // Check for valid attribute names const std::string &attrName = attributes[idx]->getName(); if (!Utils::isIdentifier(name)) { - logger.error(std::string("Invalid attribute name \"") + name + + logger.error(std::string("Invalid attribute name \"") + attrName + std::string("\"")); } // Check for uniqueness auto res = attributeNames.emplace(attrName, idx); if (!res.second) { - logger.error(std::string("Attribute with name \"") + name + + logger.error(std::string("Attribute with name \"") + attrName + std::string("\" defined multiple times")); + if (parent != nullptr && parent->indexOf(attrName) >= 0) { + logger.note(std::string("Attribute \"") + attrName + + std::string("\" was defined in parent class \"") + + parent->getName() + std::string("\"")); + } } + + // Store the attribute in the complete attribute list + collectedAttributes.push_back(attributes[idx]); } // Call the private constructor - return new StructType(mgr, name, system, parent, attributes, attributeNames); + return new StructType(mgr, name, system, parent, collectedAttributes, + attributeNames); } Variant StructType::create() const @@ -369,6 +389,15 @@ Variant StructType::cast(Variant &data, Logger &logger) const return buildFromArrayOrMap(data, logger, true); } +ssize_t StructType::indexOf(const std::string &name) const +{ + size_t res; + if (resolveIdentifierKey(name, res)) { + return res; + } + return -1; +} + /* Class ArrayType */ bool ArrayType::doBuild(Variant &data, Logger &logger) const diff --git a/src/core/model/Typesystem.hpp b/src/core/model/Typesystem.hpp index 7eedc67..c279b5d 100644 --- a/src/core/model/Typesystem.hpp +++ b/src/core/model/Typesystem.hpp @@ -617,6 +617,16 @@ public: * struct type has no parent. */ Rooted<StructType> getParent() const { return parent; } + + /** + * Returns the index of the given attribute in a data array representing + * the StructType. + * + * @param name is the name of the attribute for which the index is + * requested. + * @return the index or -1 if the attribute does not exist. + */ + ssize_t indexOf(const std::string &name) const; }; /** |