[Orxonox-commit 5701] r10361 - code/branches/core7/src/libraries/core/class

landauf at orxonox.net landauf at orxonox.net
Sun Apr 12 23:05:10 CEST 2015


Author: landauf
Date: 2015-04-12 23:05:10 +0200 (Sun, 12 Apr 2015)
New Revision: 10361

Modified:
   code/branches/core7/src/libraries/core/class/Identifier.h
   code/branches/core7/src/libraries/core/class/IdentifierManager.cc
   code/branches/core7/src/libraries/core/class/IdentifierManager.h
Log:
added run-time check for class hierarchy

Modified: code/branches/core7/src/libraries/core/class/Identifier.h
===================================================================
--- code/branches/core7/src/libraries/core/class/Identifier.h	2015-04-12 19:41:22 UTC (rev 10360)
+++ code/branches/core7/src/libraries/core/class/Identifier.h	2015-04-12 21:05:10 UTC (rev 10361)
@@ -222,6 +222,7 @@
             void addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container);
             XMLPortObjectContainer* getXMLPortObjectContainer(const std::string& sectionname);
 
+            virtual bool canDynamicCastObjectToIdentifierClass(Identifiable* object) = 0;
 
         protected:
             virtual void createSuperFunctionCaller() const = 0;
@@ -288,6 +289,9 @@
             virtual const std::string& getTypeidName()
                 { return this->typeidName_; }
 
+            virtual bool canDynamicCastObjectToIdentifierClass(Identifiable* object)
+                { return dynamic_cast<T*>(object) != 0; }
+
         private:
             static void initializeIdentifier();
 

Modified: code/branches/core7/src/libraries/core/class/IdentifierManager.cc
===================================================================
--- code/branches/core7/src/libraries/core/class/IdentifierManager.cc	2015-04-12 19:41:22 UTC (rev 10360)
+++ code/branches/core7/src/libraries/core/class/IdentifierManager.cc	2015-04-12 21:05:10 UTC (rev 10361)
@@ -145,11 +145,46 @@
                 orxout(internal_error) << "Identifier was registered late and is not initialized: " << it->second->getName() << " / " << it->second->getTypeidName() << endl;
         }
 
+        this->verifyClassHierarchy();
+
         this->stopCreatingHierarchy();
         orxout(internal_status) << "Finished class-hierarchy creation" << endl;
     }
 
     /**
+     * Verifies if the class hierarchy is consistent with the RTTI.
+     */
+    void IdentifierManager::verifyClassHierarchy()
+    {
+        Context temporaryContext(NULL);
+        for (std::map<std::string, Identifier*>::const_iterator it1 = this->identifierByTypeidName_.begin(); it1 != this->identifierByTypeidName_.end(); ++it1)
+        {
+            if (!it1->second->hasFactory())
+                continue;
+
+            Identifiable* temp = it1->second->fabricate(&temporaryContext);
+
+            for (std::map<std::string, Identifier*>::const_iterator it2 = this->identifierByTypeidName_.begin(); it2 != this->identifierByTypeidName_.end(); ++it2)
+            {
+                bool isA_AccordingToRtti = it2->second->canDynamicCastObjectToIdentifierClass(temp);
+                bool isA_AccordingToClassHierarchy = temp->isA(it2->second);
+
+                if (isA_AccordingToRtti != isA_AccordingToClassHierarchy)
+                {
+                    orxout(internal_error) << "Class hierarchy does not match RTTI: Class hierarchy claims that " << it1->second->getName() <<
+                        (isA_AccordingToClassHierarchy ? " is a " : " is not a ") << it2->second->getName() << " but RTTI says the opposite." << endl;
+                }
+            }
+
+            delete temp;
+        }
+
+        size_t numberOfObjects = temporaryContext.getObjectList<Listable>()->size();
+        if (numberOfObjects > 0)
+            orxout(internal_warning) << "There are still " << numberOfObjects << " listables left after creating the class hierarchy" << endl;
+    }
+
+    /**
         @brief Destroys all Identifiers. Called when exiting the program.
     */
     void IdentifierManager::destroyAllIdentifiers()

Modified: code/branches/core7/src/libraries/core/class/IdentifierManager.h
===================================================================
--- code/branches/core7/src/libraries/core/class/IdentifierManager.h	2015-04-12 19:41:22 UTC (rev 10360)
+++ code/branches/core7/src/libraries/core/class/IdentifierManager.h	2015-04-12 21:05:10 UTC (rev 10361)
@@ -58,6 +58,7 @@
             ////// Class Hierarchy //////
             /////////////////////////////
             void createClassHierarchy();
+            void verifyClassHierarchy();
             void destroyAllIdentifiers();
 
             void createdObject(Identifiable* identifiable);




More information about the Orxonox-commit mailing list