diff options
author | Benjamin Paassen <bpaassen@techfak.uni-bielefeld.de> | 2015-02-08 19:37:51 +0100 |
---|---|---|
committer | Benjamin Paassen <bpaassen@techfak.uni-bielefeld.de> | 2015-02-08 19:37:51 +0100 |
commit | 5eca67b6cab7031d8203b1403ba5cddaef833e76 (patch) | |
tree | ddb626aa00a0da75d038b08025e4c4fd7760ec5c /src/core/model/Document.cpp | |
parent | c2b9597c49abeef3f333b1bf7221a51019d53668 (diff) |
restructured the FieldDescriptor mechanism.
Diffstat (limited to 'src/core/model/Document.cpp')
-rw-r--r-- | src/core/model/Document.cpp | 161 |
1 files changed, 69 insertions, 92 deletions
diff --git a/src/core/model/Document.cpp b/src/core/model/Document.cpp index de73bb8..4579383 100644 --- a/src/core/model/Document.cpp +++ b/src/core/model/Document.cpp @@ -28,53 +28,6 @@ namespace ousia { /* Class DocumentEntity */ -int DocumentEntity::getFieldDescriptorIndex(const std::string &fieldName, - bool enforce) const -{ - const NodeVector<FieldDescriptor> &fds = descriptor->getFieldDescriptors(); - unsigned int f = 0; - - // otherwise we return the FieldDescriptor with the correct name (if - // such a descriptor exists). - for (auto &fd : fds) { - if (fd->getName() == fieldName) { - return f; - } - f++; - } - - if (enforce) { - throw OusiaException(std::string("\"") + descriptor->getName() + - "\" has no field with name \"" + fieldName + "\""); - } else { - return -1; - } -} - -int DocumentEntity::getFieldDescriptorIndex( - Handle<FieldDescriptor> fieldDescriptor, bool enforce) const -{ - if (fieldDescriptor.isNull()) { - throw OusiaException("The given FieldDescriptor handle is null!"); - } - const NodeVector<FieldDescriptor> &fds = descriptor->getFieldDescriptors(); - int f = 0; - for (auto &fd : fds) { - if (fd->getName() == fieldDescriptor->getName() && - fd->getFieldType() == fieldDescriptor->getFieldType()) { - return f; - } - f++; - } - if (enforce) { - throw OusiaException(std::string("\"") + descriptor->getName() + - "\" has no field with name \"" + - fieldDescriptor->getName() + "\""); - } else { - return -1; - } -} - void DocumentEntity::invalidateSubInstance() { if (subInst->isa(&RttiTypes::StructuredEntity)) { @@ -105,18 +58,10 @@ void DocumentEntity::setDescriptor(Handle<Descriptor> d) } invalidateSubInstance(); descriptor = subInst->acquire(d); - // get the effective field descriptors in the descriptor. - NodeVector<FieldDescriptor> fieldDescs; - if (descriptor->isa(&RttiTypes::StructuredClass)) { - fieldDescs = - descriptor.cast<StructuredClass>()->getEffectiveFieldDescriptors(); - } else { - fieldDescs = descriptor->getFieldDescriptors(); - } // clear the fields vector. fields.clear(); // fill it again. - for (size_t f = 0; f < fieldDescs.size(); f++) { + for (size_t f = 0; f < descriptor->getFieldDescriptors().size(); f++) { fields.push_back(NodeVector<StructureNode>(subInst)); } } @@ -148,13 +93,7 @@ bool DocumentEntity::doValidate(Logger &logger) const * gather all fields of superclasses as well, that have not been * overridden in the subclasses. */ - NodeVector<FieldDescriptor> fieldDescs; - if (descriptor->isa(&RttiTypes::StructuredClass)) { - fieldDescs = - descriptor.cast<StructuredClass>()->getEffectiveFieldDescriptors(); - } else { - fieldDescs = descriptor->getFieldDescriptors(); - } + NodeVector<FieldDescriptor> fieldDescs = descriptor->getFieldDescriptors(); // iterate over every field for (unsigned int f = 0; f < fields.size(); f++) { // we have a special check for primitive fields. @@ -332,6 +271,43 @@ void DocumentEntity::setAttributes(const Variant &a) attributes = a; } +static int enforceGetFieldDescriptorIndex(Handle<Descriptor> desc, + const std::string &fieldName) +{ + int idx = desc->getFieldDescriptorIndex(fieldName); + if (idx == -1) { + throw OusiaException( + std::string("Descriptor \"") + desc->getName() + + "\" has no field with the name \"" + fieldName + "\""); + } + return idx; +} + +static int enforceGetFieldDescriptorIndex( + Handle<Descriptor> desc, Handle<FieldDescriptor> fieldDescriptor) +{ + int idx = desc->getFieldDescriptorIndex(fieldDescriptor); + if (idx == -1) { + throw OusiaException(std::string("Descriptor \"") + + desc->getName() + + "\" does not reference the given field \"" + + fieldDescriptor->getName() + "\""); + } + return idx; +} + +const NodeVector<StructureNode> &DocumentEntity::getField( + const std::string &fieldName) const +{ + return fields[enforceGetFieldDescriptorIndex(descriptor, fieldName)]; +} + +const NodeVector<StructureNode> &DocumentEntity::getField( + Handle<FieldDescriptor> fieldDescriptor) const +{ + return fields[enforceGetFieldDescriptorIndex(descriptor, fieldDescriptor)]; +} + void DocumentEntity::addStructureNode(Handle<StructureNode> s, const int &i) { // only add the new node if we don't have it already. @@ -354,62 +330,63 @@ void DocumentEntity::addStructureNode(Handle<StructureNode> s, const int &i) } } -bool DocumentEntity::removeStructureNodeFromField(Handle<StructureNode> s, - const int &i) +void DocumentEntity::addStructureNode(Handle<StructureNode> s, + Handle<FieldDescriptor> fieldDescriptor) { - auto it = fields[i].find(s); - if (it != fields[i].end()) { - invalidateSubInstance(); - fields[i].erase(it); - s->setParent(nullptr); - return true; + addStructureNode( + s, enforceGetFieldDescriptorIndex(descriptor, fieldDescriptor)); +} + +void DocumentEntity::addStructureNodes( + const std::vector<Handle<StructureNode>> &ss, + Handle<FieldDescriptor> fieldDescriptor) +{ + const int i = enforceGetFieldDescriptorIndex(descriptor, fieldDescriptor); + for (Handle<StructureNode> s : ss) { + addStructureNode(s, i); } - return false; } void DocumentEntity::addStructureNode(Handle<StructureNode> s, const std::string &fieldName) { - addStructureNode(s, getFieldDescriptorIndex(fieldName, true)); + addStructureNode(s, enforceGetFieldDescriptorIndex(descriptor, fieldName)); } void DocumentEntity::addStructureNodes( const std::vector<Handle<StructureNode>> &ss, const std::string &fieldName) { - const int i = getFieldDescriptorIndex(fieldName, true); + const int idx = enforceGetFieldDescriptorIndex(descriptor, fieldName); for (Handle<StructureNode> s : ss) { - addStructureNode(s, i); + addStructureNode(s, idx); } } bool DocumentEntity::removeStructureNodeFromField(Handle<StructureNode> s, - const std::string &fieldName) -{ - return removeStructureNodeFromField( - s, getFieldDescriptorIndex(fieldName, true)); -} - -void DocumentEntity::addStructureNode(Handle<StructureNode> s, - Handle<FieldDescriptor> fieldDescriptor) + const int &i) { - addStructureNode(s, getFieldDescriptorIndex(fieldDescriptor, true)); + 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::addStructureNodes( - const std::vector<Handle<StructureNode>> &ss, - Handle<FieldDescriptor> fieldDescriptor) +bool DocumentEntity::removeStructureNodeFromField(Handle<StructureNode> s, + const std::string &fieldName) { - const int i = getFieldDescriptorIndex(fieldDescriptor, true); - for (Handle<StructureNode> s : ss) { - addStructureNode(s, i); - } + return removeStructureNodeFromField( + s, enforceGetFieldDescriptorIndex(descriptor, fieldName)); } bool DocumentEntity::removeStructureNodeFromField( Handle<StructureNode> s, Handle<FieldDescriptor> fieldDescriptor) { return removeStructureNodeFromField( - s, getFieldDescriptorIndex(fieldDescriptor, true)); + s, enforceGetFieldDescriptorIndex(descriptor, fieldDescriptor)); } bool DocumentEntity::removeStructureNode(Handle<StructureNode> s) |