From 2a5b3632fb49452adb6ecedb9c678909341ef662 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Wed, 15 Apr 2015 20:40:01 +0200 Subject: Derive structure entities and annotation entities from common base class to allow resolution for both --- src/core/model/Document.cpp | 32 +++++++++++++++++++++++++++----- src/core/model/Document.hpp | 33 ++++++++++++++++++++++++++------- 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/src/core/model/Document.cpp b/src/core/model/Document.cpp index 411a755..3a076c4 100644 --- a/src/core/model/Document.cpp +++ b/src/core/model/Document.cpp @@ -598,7 +598,7 @@ bool StructureNode::doValidate(Logger &logger) const StructureNode::StructureNode(Manager &mgr, std::string name, Handle parent, const std::string &fieldName) - : Node(mgr, std::move(name), parent) + : DocumentNode(mgr, std::move(name), parent) { if (parent->isa(&RttiTypes::StructuredEntity)) { parent.cast()->addStructureNode(this, fieldName); @@ -611,7 +611,7 @@ StructureNode::StructureNode(Manager &mgr, std::string name, StructureNode::StructureNode(Manager &mgr, std::string name, Handle parent, size_t fieldIdx) - : Node(mgr, std::move(name), parent) + : DocumentNode(mgr, std::move(name), parent) { if (parent->isa(&RttiTypes::StructuredEntity)) { parent.cast()->addStructureNode(this, fieldIdx); @@ -622,6 +622,12 @@ StructureNode::StructureNode(Manager &mgr, std::string name, } } +StructureNode::StructureNode(Manager &mgr, std::string name, + Handle parent) + : DocumentNode(mgr, std::move(name), parent) +{ +} + /* Class StructuredEntity */ StructuredEntity::StructuredEntity(Manager &mgr, Handle doc, @@ -641,6 +647,13 @@ StructuredEntity::StructuredEntity(Manager &mgr, Handle parent, { } +void StructuredEntity::doResolve(ResolutionState &state) +{ + for (const auto &field : getFields()) { + continueResolveComposita(field, field.getIndex(), state); + } +} + bool StructuredEntity::doValidate(Logger &logger) const { bool valid = true; @@ -732,7 +745,7 @@ AnnotationEntity::AnnotationEntity(Manager &mgr, Handle parent, Handle descriptor, Handle start, Handle end, Variant attributes, std::string name) - : Node(mgr, std::move(name), parent), + : DocumentNode(mgr, std::move(name), parent), DocumentEntity(this, descriptor, attributes) { if (parent != nullptr) { @@ -742,6 +755,13 @@ AnnotationEntity::AnnotationEntity(Manager &mgr, Handle parent, setEnd(end); } +void AnnotationEntity::doResolve(ResolutionState &state) +{ + for (const auto &field : getFields()) { + continueResolveComposita(field, field.getIndex(), state); + } +} + bool AnnotationEntity::doValidate(Logger &logger) const { bool valid = true; @@ -986,8 +1006,10 @@ namespace RttiTypes { const Rtti Document = RttiBuilder("Document") .parent(&RootNode) .composedOf({&AnnotationEntity, &StructuredEntity}); +const Rtti DocumentNode = + RttiBuilder("DocumentNode").parent(&Node); const Rtti StructureNode = - RttiBuilder("StructureNode").parent(&Node); + RttiBuilder("StructureNode").parent(&DocumentNode); const Rtti StructuredEntity = RttiBuilder("StructuredEntity") .parent(&StructureNode) @@ -997,7 +1019,7 @@ const Rtti DocumentPrimitive = RttiBuilder( const Rtti Anchor = RttiBuilder("Anchor").parent(&StructureNode); const Rtti AnnotationEntity = RttiBuilder("AnnotationEntity") - .parent(&Node) + .parent(&DocumentNode) .composedOf({&StructuredEntity, &DocumentPrimitive, &Anchor}); } } diff --git a/src/core/model/Document.hpp b/src/core/model/Document.hpp index 1ac913d..b2691c0 100644 --- a/src/core/model/Document.hpp +++ b/src/core/model/Document.hpp @@ -172,6 +172,8 @@ private: protected: bool doValidate(Logger &logger) const; + std::vector> &getFields() { return fields; } + public: /** * The constructor for a DocumentEntity. Node that this does not inherit @@ -497,11 +499,20 @@ public: const std::string &name = ""); }; +/** + * The DocumentNode class is a dummy class for being capable of selecting + * all nodes in the document graph e.g. when resolving element names. + */ +class DocumentNode : public Node { +public: + using Node::Node; +}; + /** * A StructureNode is a Node of the StructureTree of the document. This is a * common superclass for StructuredEntity, Anchor and DocumentPrimitive. */ -class StructureNode : public Node { +class StructureNode : public DocumentNode { friend DocumentEntity; protected: @@ -524,10 +535,7 @@ public: * Constructor for an empty StructureNode. */ StructureNode(Manager &mgr, std::string name = "", - Handle parent = nullptr) - : Node(mgr, std::move(name), parent) - { - } + Handle parent = nullptr); }; /** @@ -541,6 +549,7 @@ private: bool transparent = false; protected: + void doResolve(ResolutionState &state) override; bool doValidate(Logger &logger) const override; public: @@ -675,6 +684,7 @@ public: : StructureNode(mgr, "", parent, fieldName), content(content) { } + /** * Constructor for a DocumentPrimitive. * @@ -696,12 +706,19 @@ public: { } + /** + * Returns the content of this DocumentPrimitive as a const reference. + * + * @return the content of this DocumentPrimitive. + */ + const Variant &getContent() const { return content; } + /** * Returns the content of this DocumentPrimitive. * * @return the content of this DocumentPrimitive. */ - Variant getContent() const { return content; } + Variant &getContent() { return content; } /** * Sets the content of this DocumentPrimitive to the given Variant. @@ -828,7 +845,7 @@ public: * the two text exerpts "emphasized" and "and" separately. * */ -class AnnotationEntity : public Node, public DocumentEntity { +class AnnotationEntity : public DocumentNode, public DocumentEntity { friend DocumentEntity; friend Document; @@ -837,6 +854,7 @@ private: Owned end; protected: + void doResolve(ResolutionState &state) override; bool doValidate(Logger &logger) const override; public: @@ -1081,6 +1099,7 @@ public: namespace RttiTypes { extern const Rtti Document; +extern const Rtti DocumentNode; extern const Rtti DocumentEntity; extern const Rtti AnnotationEntity; extern const Rtti StructureNode; -- cgit v1.2.3