From 33008f1110523ae9c9b9e1d2ca24ed642637c40d Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Thu, 22 Jan 2015 00:43:40 +0100 Subject: added move semantics do Domain and Document classes. --- src/core/model/Document.cpp | 89 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 83 insertions(+), 6 deletions(-) (limited to 'src/core/model/Document.cpp') diff --git a/src/core/model/Document.cpp b/src/core/model/Document.cpp index f29af1c..2ae9107 100644 --- a/src/core/model/Document.cpp +++ b/src/core/model/Document.cpp @@ -335,13 +335,39 @@ void DocumentEntity::setAttributes(const Variant &a) void DocumentEntity::addStructureNode(Handle s, const int &i) { - invalidateSubInstance(); - fields[i].push_back(s); - if (s->getParent() != subInst) { + // only add the new node if we don't have it already. + auto it = fields[i].find(s); + if (it == fields[i].end()) { + invalidateSubInstance(); + fields[i].push_back(s); + } + Handle par = s->getParent(); + if (par != subInst) { + // if a previous parent existed, remove the StructureNode from it + if (par != nullptr) { + if (par->isa(RttiTypes::StructuredEntity)) { + par.cast()->removeStructureNode(s); + } else { + par.cast()->removeStructureNode(s); + } + } s->setParent(subInst); } } +bool DocumentEntity::removeStructureNodeFromField(Handle s, + const int &i) +{ + auto it = fields[i].find(s); + if (it != fields[i].end()) { + invalidateSubInstance(); + fields[i].erase(it); + s->setParent(nullptr); + return true; + } + return false; +} + void DocumentEntity::addStructureNode(Handle s, const std::string &fieldName) { @@ -357,6 +383,13 @@ void DocumentEntity::addStructureNodes( } } +bool DocumentEntity::removeStructureNodeFromField( + Handle s, const std::string &fieldName) +{ + return removeStructureNodeFromField( + s, getFieldDescriptorIndex(fieldName, true)); +} + void DocumentEntity::addStructureNode(Handle s, Handle fieldDescriptor) { @@ -373,6 +406,28 @@ void DocumentEntity::addStructureNodes( } } +bool DocumentEntity::removeStructureNodeFromField( + Handle s, Handle fieldDescriptor) +{ + return removeStructureNodeFromField( + s, getFieldDescriptorIndex(fieldDescriptor, true)); +} + +bool DocumentEntity::removeStructureNode(Handle s) +{ + for (auto field : fields) { + auto it = field.find(s); + if (it != field.end()) { + invalidateSubInstance(); + field.erase(it); + s->setParent(nullptr); + return true; + } + } + + return false; +} + /* Class StructureNode */ bool StructureNode::doValidate(Logger &logger) const @@ -558,9 +613,17 @@ bool Document::doValidate(Logger &logger) const void Document::addAnnotation(Handle a) { - invalidate(); - annotations.push_back(a); - if (a->getParent() != this) { + // only add it if we need to. + if (annotations.find(a) == annotations.end()) { + invalidate(); + annotations.push_back(a); + } + Handle par = a->getParent(); + if (par != this) { + if (par != nullptr) { + // remove the StructuredClass from the old parent. + par.cast()->removeAnnotation(a); + } a->setParent(this); } } @@ -572,6 +635,20 @@ void Document::addAnnotations(const std::vector> &as) } } + + +bool Document::removeAnnotation(Handle a) +{ + auto it = annotations.find(a); + if (it != annotations.end()) { + invalidate(); + annotations.erase(it); + a->setParent(nullptr); + return true; + } + return false; +} + bool Document::hasChild(Handle s) const { Rooted parent = s->getParent(); -- cgit v1.2.3