From f236e4d7401d55d18707ac3374f913ada41e5b7b Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Thu, 9 Apr 2015 16:02:21 +0200 Subject: added serialization support for attributes descriptors. --- src/core/model/Typesystem.cpp | 8 ++++++ src/core/model/Typesystem.hpp | 26 +++++++++++------- src/core/parser/stack/OntologyHandler.cpp | 2 +- src/plugins/xml/XmlOutput.cpp | 45 ++++++++++++++++++++++++------- 4 files changed, 61 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/core/model/Typesystem.cpp b/src/core/model/Typesystem.cpp index df2b9fb..688cb60 100644 --- a/src/core/model/Typesystem.cpp +++ b/src/core/model/Typesystem.cpp @@ -618,6 +618,14 @@ bool StructType::cast(Variant &data, Logger &logger) const return buildFromArrayOrMap(data, logger, NullMagicCallback, true); } +NodeVector StructType::getOwnAttributes() const +{ + NodeVector res; + res.insert(res.end(), attributes.begin() + attributeStart, + attributes.end()); + return res; +} + ssize_t StructType::indexOf(const std::string &name) const { size_t res; diff --git a/src/core/model/Typesystem.hpp b/src/core/model/Typesystem.hpp index 559a74c..193726d 100644 --- a/src/core/model/Typesystem.hpp +++ b/src/core/model/Typesystem.hpp @@ -64,20 +64,20 @@ public: */ enum class MagicCallbackResult { /** - * A magic value with the given name could not be resolved. - */ + * A magic value with the given name could not be resolved. + */ NOT_FOUND, /** - * A magic value with the given name could be resolved, but is of the - * wrong type. - */ + * A magic value with the given name could be resolved, but is of the + * wrong type. + */ FOUND_INVALID, /** - * A magic value with the given name could be resolved and is of the - * correct type. - */ + * A magic value with the given name could be resolved and is of the + * correct type. + */ FOUND_VALID }; @@ -953,7 +953,15 @@ public: * * @return a const reference pointing at the attribute list. */ - const NodeVector &getAttributes() const { return attributes; }; + const NodeVector &getAttributes() const { return attributes; } + + /** + * Returns a vector of all attributes that belong to this StructType itself, + * excluding the attributes of the parent structure. + * + * @return a vector of all attributes that belong to this StructType itself. + */ + NodeVector getOwnAttributes() const; /** * Returns the index of the given attribute in a data array representing diff --git a/src/core/parser/stack/OntologyHandler.cpp b/src/core/parser/stack/OntologyHandler.cpp index 0a33c97..0cd8140 100644 --- a/src/core/parser/stack/OntologyHandler.cpp +++ b/src/core/parser/stack/OntologyHandler.cpp @@ -108,7 +108,7 @@ void OntologyAnnotationHandler::end() { scope().pop(logger()); } bool OntologyAttributesHandler::startCommand(Variant::mapType &args) { - // Fetch the current typesystem and create the struct node + // Fetch the current descriptor and add the attributes descriptor Rooted parent = scope().selectOrThrow(); Rooted attrDesc = parent->getAttributesDescriptor(); diff --git a/src/plugins/xml/XmlOutput.cpp b/src/plugins/xml/XmlOutput.cpp index 8c45a52..cdca730 100644 --- a/src/plugins/xml/XmlOutput.cpp +++ b/src/plugins/xml/XmlOutput.cpp @@ -51,7 +51,8 @@ struct TransformParams { }; /* - * These are method declarations to allow for cross-references of methods. + * These are forward method declarations to allow for cross-references of + * methods. */ /* @@ -69,6 +70,18 @@ static Rooted transformOntology(Handle parent, static std::string getTypeRef(Handle referencing, Handle referenced); +static Rooted transformStructTypeEntry(Handle parent, + const std::string &tagName, + Handle t, + Handle a, + TransformParams &P); + +static Rooted transformStructType(Handle parent, + const std::string &structTagName, + const std::string &fieldTagName, + Handle t, + TransformParams &P); + static Rooted transformTypesystem(Handle parent, Handle t, TransformParams &P); @@ -301,7 +314,12 @@ static void transformDescriptor(Handle elem, Handle syntax, { // add name. addNameAttribute(d, elem->getAttributes()); - // TODO: transform the attributes descriptor. + // transform the attributes descriptor. + Rooted attributes = transformStructType( + elem, "attributes", "attribute", d->getAttributesDescriptor(), P); + // remove the parent entry if it is there. + attributes->getAttributes().erase("parent"); + elem->addChild(attributes); // transform the syntactic sugar description. { Rooted open = @@ -434,12 +452,13 @@ std::string getTypeRef(Handle referencing, Handle referenced) } Rooted transformStructTypeEntry(Handle parent, + const std::string &tagName, Handle t, Handle a, TransformParams &P) { // create an xml element for the attribute. - Rooted attribute{new Element(P.mgr, parent, "field")}; + Rooted attribute{new Element(P.mgr, parent, tagName)}; addNameAttribute(a, attribute->getAttributes()); // add the type reference { @@ -451,22 +470,27 @@ Rooted transformStructTypeEntry(Handle parent, attribute->getAttributes().emplace("default", toString(a->getDefaultValue(), P)); } - // set the optional flag. - attribute->getAttributes().emplace("optional", - getStringForBool(a->isOptional())); return attribute; } Rooted transformStructType(Handle parent, + const std::string &structTagName, + const std::string &fieldTagName, Handle t, TransformParams &P) { // create an xml element for the struct type itself. - Rooted structType{new Element(P.mgr, parent, "struct")}; + Rooted structType{new Element(P.mgr, parent, structTagName)}; addNameAttribute(t, structType->getAttributes()); + // transformt the parent reference. + if (t->getParentStructure() != nullptr) { + std::string typeRef = + getTypeRef(t->getTypesystem(), t->getParentStructure()); + structType->getAttributes().emplace("parent", typeRef); + } // transform all attributes. - for (auto &a : t->getAttributes()) { + for (auto &a : t->getOwnAttributes()) { Rooted attribute = - transformStructTypeEntry(structType, t, a, P); + transformStructTypeEntry(structType, fieldTagName, t, a, P); structType->addChild(attribute); } return structType; @@ -504,7 +528,8 @@ Rooted transformTypesystem(Handle parent, for (auto tp : t->getTypes()) { Rooted type; if (tp->isa(&RttiTypes::StructType)) { - type = transformStructType(typesystem, tp.cast(), P); + type = transformStructType(typesystem, "struct", "field", + tp.cast(), P); } else { P.logger.warning(std::string("Type ") + tp->getName() + " can not be serialized, because it is neither a " -- cgit v1.2.3