[Orxonox-commit 5717] r10377 - code/branches/core7/src/libraries/core/class

landauf at orxonox.net landauf at orxonox.net
Sun Apr 19 22:41:10 CEST 2015


Author: landauf
Date: 2015-04-19 22:41:09 +0200 (Sun, 19 Apr 2015)
New Revision: 10377

Modified:
   code/branches/core7/src/libraries/core/class/Identifier.cc
   code/branches/core7/src/libraries/core/class/Identifier.h
Log:
added a check which tries to detect wrongly configured class hierarchy definitions

Modified: code/branches/core7/src/libraries/core/class/Identifier.cc
===================================================================
--- code/branches/core7/src/libraries/core/class/Identifier.cc	2015-04-19 20:13:42 UTC (rev 10376)
+++ code/branches/core7/src/libraries/core/class/Identifier.cc	2015-04-19 20:41:09 UTC (rev 10377)
@@ -191,6 +191,8 @@
             for (std::list<const Identifier*>::const_iterator it_parent = this->parents_.begin(); it_parent != this->parents_.end(); ++it_parent)
                 for (std::list<const Identifier*>::const_iterator it_parent_parent = const_cast<Identifier*>(*it_parent)->parents_.begin(); it_parent_parent != const_cast<Identifier*>(*it_parent)->parents_.end(); ++it_parent_parent)
                     this->directParents_.remove(*it_parent_parent);
+
+            this->verifyIdentifierTrace();
         }
         else if (!this->directParents_.empty())
         {
@@ -233,6 +235,56 @@
     }
 
     /**
+     * Verifies if the recorded trace of parent identifiers matches the expected trace according to the class hierarchy. If it doesn't match, the class
+     * hierarchy is likely wrong, e.g. due to wrong inheritsFrom<>() definitions in abstract classes.
+     */
+    void Identifier::verifyIdentifierTrace() const
+    {
+
+        std::list<const Identifier*> expectedIdentifierTrace;
+
+        // if any parent class is virtual, it will be instantiated first, so we need to add them first.
+        for (std::list<const Identifier*>::const_iterator it_parent = this->parents_.begin(); it_parent != this->parents_.end(); ++it_parent)
+        {
+            if ((*it_parent)->isVirtualBase())
+            {
+                for (std::list<const Identifier*>::const_iterator it_parent_parent = const_cast<Identifier*>(*it_parent)->parents_.begin(); it_parent_parent != const_cast<Identifier*>(*it_parent)->parents_.end(); ++it_parent_parent)
+                    this->addIfNotExists(expectedIdentifierTrace, *it_parent_parent);
+                this->addIfNotExists(expectedIdentifierTrace, *it_parent);
+            }
+        }
+
+        // now all direct parents get created recursively. already added identifiers (e.g. virtual base classes) are not added again.
+        for (std::list<const Identifier*>::const_iterator it_parent = this->directParents_.begin(); it_parent != this->directParents_.end(); ++it_parent)
+        {
+            for (std::list<const Identifier*>::const_iterator it_parent_parent = const_cast<Identifier*>(*it_parent)->parents_.begin(); it_parent_parent != const_cast<Identifier*>(*it_parent)->parents_.end(); ++it_parent_parent)
+                this->addIfNotExists(expectedIdentifierTrace, *it_parent_parent);
+            this->addIfNotExists(expectedIdentifierTrace, *it_parent);
+        }
+
+        // check if the expected trace matches the actual trace (which was defined by creating a sample instance)
+        if (expectedIdentifierTrace != this->parents_)
+        {
+            orxout(internal_warning) << this->getName() << " has an unexpected initialization trace:" << endl;
+
+            orxout(internal_warning) << "  Actual trace (after creating a sample instance):" << endl << "    ";
+            for (std::list<const Identifier*>::const_iterator it_parent = this->parents_.begin(); it_parent != this->parents_.end(); ++it_parent)
+                orxout(internal_warning) << " " << (*it_parent)->getName();
+            orxout(internal_warning) << endl;
+
+            orxout(internal_warning) << "  Expected trace (according to class hierarchy definitions):" << endl << "    ";
+            for (std::list<const Identifier*>::const_iterator it_parent = expectedIdentifierTrace.begin(); it_parent != expectedIdentifierTrace.end(); ++it_parent)
+                orxout(internal_warning) << " " << (*it_parent)->getName();
+            orxout(internal_warning) << endl;
+
+            orxout(internal_warning) << "  Direct parents (according to class hierarchy definitions):" << endl << "    ";
+            for (std::list<const Identifier*>::const_iterator it_parent = this->directParents_.begin(); it_parent != this->directParents_.end(); ++it_parent)
+                orxout(internal_warning) << " " << (*it_parent)->getName();
+            orxout(internal_warning) << endl;
+        }
+    }
+
+    /**
      * Adds @param identifierToAdd to @param list if this identifier is not already contained in the list.
      */
     void Identifier::addIfNotExists(std::list<const Identifier*>& list, const Identifier* identifierToAdd) const

Modified: code/branches/core7/src/libraries/core/class/Identifier.h
===================================================================
--- code/branches/core7/src/libraries/core/class/Identifier.h	2015-04-19 20:13:42 UTC (rev 10376)
+++ code/branches/core7/src/libraries/core/class/Identifier.h	2015-04-19 20:41:09 UTC (rev 10377)
@@ -214,6 +214,7 @@
             virtual void createSuperFunctionCaller() const = 0;
 
         private:
+            void verifyIdentifierTrace() const;
             void addIfNotExists(std::list<const Identifier*>& list, const Identifier* identifierToAdd) const;
 
             std::list<const Identifier*> directParents_;                    //!< The direct parents of the class the Identifier belongs to (sorted by their order of initialization)




More information about the Orxonox-commit mailing list