From 8810fc675a07e0ed62bcc391bb0464bc04dd3e37 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Wed, 15 Apr 2015 20:40:25 +0200 Subject: Implement generic "inheritsFrom" method for Descriptor class --- src/core/model/Ontology.cpp | 15 +++++++++++++++ src/core/model/Ontology.hpp | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+) (limited to 'src/core') diff --git a/src/core/model/Ontology.cpp b/src/core/model/Ontology.cpp index 89710d3..fa0cd1e 100644 --- a/src/core/model/Ontology.cpp +++ b/src/core/model/Ontology.cpp @@ -571,6 +571,16 @@ bool Descriptor::doValidate(Logger &logger) const return valid & continueValidationCheckDuplicates(fds, logger); } +bool Descriptor::doCheckInheritsFrom(Handle c) const +{ + return false; +} + +bool Descriptor::inheritsFrom(Handle c) const +{ + return (c == this) || ((c != nullptr) && (c->type() == type()) && doCheckInheritsFrom(c)); +} + NodeVector Descriptor::pathTo(Handle target, Logger &logger) const { @@ -813,6 +823,11 @@ StructuredClass::StructuredClass(Manager &mgr, std::string name, } } +bool StructuredClass::doCheckInheritsFrom(Handle c) const +{ + return isSubclassOf(c.cast()); +} + bool StructuredClass::doValidate(Logger &logger) const { bool valid = true; diff --git a/src/core/model/Ontology.hpp b/src/core/model/Ontology.hpp index 014f912..ddfa468 100644 --- a/src/core/model/Ontology.hpp +++ b/src/core/model/Ontology.hpp @@ -618,6 +618,16 @@ protected: bool doValidate(Logger &logger) const override; + /** + * Called when the "inheritsFrom" function is called, and this instance and + * c are of the same type, but not the same instance. Default implementation + * return false. + * + * @param c is the descriptor that should be checked for inheritance. + * @return true if this instance inherits from c. + */ + virtual bool doCheckInheritsFrom(Handle c) const; + public: Descriptor(Manager &mgr, std::string name, Handle ontology) : Node(mgr, std::move(name), ontology), @@ -626,6 +636,16 @@ public: { } + /** + * Returns true if this class is a subclass of the given class or the given + * class itself. + * + * @param c is the class that should be checked for inheriting from this + * class. + * @return true if c either is this class or c inherits from this class. + */ + bool inheritsFrom(Handle c) const; + /** * Returns a reference to the StructType that specifies the attribute keys * as well as value ontologies for this Descriptor. @@ -1042,6 +1062,7 @@ private: protected: bool doValidate(Logger &logger) const override; + bool doCheckInheritsFrom(Handle c) const override; public: /** -- cgit v1.2.3